/*static*/ status_t FSUtils::CompareSymLinks(BSymLink& symLink1, BSymLink& symLink2, bool& _equal) { char buffer1[B_PATH_NAME_LENGTH]; ssize_t bytesRead1 = symLink1.ReadLink(buffer1, sizeof(buffer1)); if (bytesRead1 < 0) return bytesRead1; char buffer2[B_PATH_NAME_LENGTH]; ssize_t bytesRead2 = symLink2.ReadLink(buffer2, sizeof(buffer2)); if (bytesRead2 < 0) return bytesRead2; _equal = bytesRead1 == bytesRead2 && memcmp(buffer1, buffer2, bytesRead1) == 0; return B_OK; }
// ReadLinkTest void SymLinkTest::ReadLinkTest() { const char *dirLink = dirLinkname; const char *fileLink = fileLinkname; const char *badLink = badLinkname; const char *cyclicLink1 = cyclicLinkname1; const char *cyclicLink2 = cyclicLinkname2; const char *existingDir = existingDirname; const char *existingFile = existingFilename; const char *nonExisting = nonExistingDirname; BSymLink link; char buffer[B_PATH_NAME_LENGTH + 1]; // uninitialized // R5: returns B_BAD_ADDRESS instead of (as doc'ed) B_FILE_ERROR NextSubTest(); CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT ); CPPUNIT_ASSERT( equals(link.ReadLink(buffer, sizeof(buffer)), B_BAD_ADDRESS, B_FILE_ERROR) ); // existing dir link NextSubTest(); CPPUNIT_ASSERT( link.SetTo(dirLink) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == (ssize_t)strlen(existingDir) ); CPPUNIT_ASSERT( strcmp(buffer, existingDir) == 0 ); // existing file link NextSubTest(); CPPUNIT_ASSERT( link.SetTo(fileLink) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == (ssize_t)strlen(existingFile) ); CPPUNIT_ASSERT( strcmp(buffer, existingFile) == 0 ); // existing cyclic link NextSubTest(); CPPUNIT_ASSERT( link.SetTo(cyclicLink1) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == (ssize_t)strlen(cyclicLink2) ); CPPUNIT_ASSERT( strcmp(buffer, cyclicLink2) == 0 ); // existing link to non-existing entry NextSubTest(); CPPUNIT_ASSERT( link.SetTo(badLink) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == (ssize_t)strlen(nonExisting) ); CPPUNIT_ASSERT( strcmp(buffer, nonExisting) == 0 ); // non-existing link // R5: returns B_BAD_ADDRESS instead of (as doc'ed) B_FILE_ERROR NextSubTest(); CPPUNIT_ASSERT( link.SetTo(nonExisting) == B_ENTRY_NOT_FOUND ); CPPUNIT_ASSERT( equals(link.ReadLink(buffer, sizeof(buffer)), B_BAD_ADDRESS, B_FILE_ERROR) ); // dir NextSubTest(); CPPUNIT_ASSERT( link.SetTo(existingDir) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == B_BAD_VALUE ); // file NextSubTest(); CPPUNIT_ASSERT( link.SetTo(existingFile) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(buffer, sizeof(buffer)) == B_BAD_VALUE ); // small buffer // R5: returns the size of the contents, not the number of bytes copied // OBOS: ... so do we NextSubTest(); char smallBuffer[2]; CPPUNIT_ASSERT( link.SetTo(dirLink) == B_OK ); CPPUNIT_ASSERT( link.ReadLink(smallBuffer, sizeof(smallBuffer)) == (ssize_t)strlen(dirLink) ); CPPUNIT_ASSERT( strncmp(smallBuffer, existingDir, sizeof(smallBuffer)) == 0 ); // bad args NextSubTest(); CPPUNIT_ASSERT( link.SetTo(fileLink) == B_OK ); CPPUNIT_ASSERT( equals(link.ReadLink(NULL, sizeof(buffer)), B_BAD_ADDRESS, B_BAD_VALUE) ); }