From 5abde301c146181da26bbe8746ebb85624ce8e34 Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Sat, 3 Jan 2004 16:27:39 +0000 Subject: [PATCH] Changed the behavior of Contains(const char*, int32): It does not longer return true, if the directory is not initialized, or if the directory does not contain the entry, but the given path is absolute. This change makes it less compatible with R5, but more consistent with the BEntry* version and undoubtfully more compatible with common sense. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@5884 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/storage/Directory.cpp | 40 +++++++++++++----------- src/tests/kits/storage/DirectoryTest.cpp | 11 ++++++- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/kits/storage/Directory.cpp b/src/kits/storage/Directory.cpp index 6c8237236f..84e1d5ef5a 100644 --- a/src/kits/storage/Directory.cpp +++ b/src/kits/storage/Directory.cpp @@ -409,9 +409,14 @@ BDirectory::FindEntry(const char *path, BEntry *entry, bool traverse) const at any level contain the entry referred to by the supplied path name. Only entries that match the node flavor specified by \a nodeFlags are considered. - If the BDirectory is not properly initialized, the method returns \c true, - if the entry exists and its kind does match. A non-absolute path is - considered relative to the current directory. + If the BDirectory is not properly initialized, the method returns \c false. + A non-absolute path is considered relative to the current directory. + + \note R5's implementation always returns \c true given an absolute path or + an unitialized directory. This implementation is not compatible with that + behavior. Instead it converts the path into a BEntry and passes it to the + other version of Contains(). + \param path the entry's path name. May be relative to this directory or absolute. \param nodeFlags Any of the following: @@ -421,26 +426,25 @@ BDirectory::FindEntry(const char *path, BEntry *entry, bool traverse) const - \c B_ANY_NODE: The entry may be of any kind. \return - \c true, if the entry exists, its kind does match \nodeFlags and the - BDirectory is either not properly initialized or it does contain the - entry at any level, + BDirectory is properly initialized and does contain the entry at any + level, - \c false, otherwise */ bool BDirectory::Contains(const char *path, int32 nodeFlags) const { - bool result = true; - if (path) { - BEntry entry; - if (InitCheck() == B_OK && !BPrivate::Storage::is_absolute_path(path)) - entry.SetTo(this, path); - else - entry.SetTo(path); - result = Contains(&entry, nodeFlags); - } else { - // R5 behavior - result = (InitCheck() == B_OK); - } - return result; + // check initialization and parameters + if (InitCheck() != B_OK) + return false; + if (!path) + return true; // mimic R5 behavior + // turn the path into a BEntry and let the other version do the work + BEntry entry; + if (BPrivate::Storage::is_absolute_path(path)) + entry.SetTo(path); + else + entry.SetTo(this, path); + return Contains(&entry, nodeFlags); } // Contains diff --git a/src/tests/kits/storage/DirectoryTest.cpp b/src/tests/kits/storage/DirectoryTest.cpp index 2d7fc32d94..0fc4209e95 100644 --- a/src/tests/kits/storage/DirectoryTest.cpp +++ b/src/tests/kits/storage/DirectoryTest.cpp @@ -807,10 +807,14 @@ DirectoryTest::ContainsTest() CPPUNIT_ASSERT( dir.Contains(existingSub) == true ); dir.Unset(); // existing entry, uninitialized BDirectory - // R5 returns true! NextSubTest(); CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT ); +// R5 returns true! +#if TEST_R5 CPPUNIT_ASSERT( dir.Contains(existing) == true ); +#else + CPPUNIT_ASSERT( dir.Contains(existing) == false ); +#endif dir.Unset(); // non-existing entry, uninitialized BDirectory NextSubTest(); @@ -821,7 +825,12 @@ DirectoryTest::ContainsTest() NextSubTest(); CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND ); CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND ); +// R5 returns true! +#if TEST_R5 CPPUNIT_ASSERT( dir.Contains(existing) == true ); +#else + CPPUNIT_ASSERT( dir.Contains(existing) == false ); +#endif dir.Unset(); // non-existing entry, badly initialized BDirectory NextSubTest();