From 654135466f2ad0a080c41ddda423291a14e5fe61 Mon Sep 17 00:00:00 2001 From: Kyle Ambroff-Kao Date: Wed, 15 Jan 2020 00:14:10 -0800 Subject: [PATCH] support/BPath: Flatten should work on empty BPath MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit BPath::Flatten() on an empty path returns B_OK in BeOS R5, just writing an empty entry_ref to the povided buffer. The Haiku implementation has some additional validation that causes B_NO_INIT to be returned instead. This patch attempts to recreate the same behavior of BeOS in this situation. * Don't check for initialization in BPath::Flatten(). Instead, just write an empty entry_ref to the provided buffer if the BPath is empty. * Fix estimation of expected size when testing the return value of BPath::FlattenedSize(). * Clean up warning by removing unecessary forward-declaration of CppUnit::Test. Change-Id: I88880cbb298bdcb594c9c8fef48314165c49e9e5 Reviewed-on: https://review.haiku-os.org/c/haiku/+/2115 Reviewed-by: Adrien Destugues Reviewed-by: Axel Dörfler --- src/kits/storage/Path.cpp | 16 ++++++++-------- src/tests/kits/storage/PathTest.cpp | 4 ++-- src/tests/kits/storage/PathTest.h | 2 -- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/kits/storage/Path.cpp b/src/kits/storage/Path.cpp index 194b70b7eb..85f2d5b6fa 100644 --- a/src/kits/storage/Path.cpp +++ b/src/kits/storage/Path.cpp @@ -431,18 +431,18 @@ BPath::Flatten(void* buffer, ssize_t size) const return flattenedSize; if (size < flattenedSize) return B_BAD_VALUE; - status_t status = InitCheck(); - if (status != B_OK) - return status; // convert the path to an entry_ref BEntry entry; entry_ref ref; - status = entry.SetTo(Path()); - if (status == B_OK) - status = entry.GetRef(&ref); - if (status != B_OK) - return status; + + if (Path() != NULL) { + status_t status = entry.SetTo(Path()); + if (status == B_OK) + status = entry.GetRef(&ref); + if (status != B_OK) + return status; + } // store the entry_ref in the buffer flattened_entry_ref& fref = *(flattened_entry_ref*)buffer; diff --git a/src/tests/kits/storage/PathTest.cpp b/src/tests/kits/storage/PathTest.cpp index e4df8a1ce6..446048fba5 100644 --- a/src/tests/kits/storage/PathTest.cpp +++ b/src/tests/kits/storage/PathTest.cpp @@ -1352,8 +1352,8 @@ PathTest::FlattenableTest() // flatten the path struct flattened_ref { dev_t device; ino_t directory; char name[1]; }; size = path.FlattenedSize(); - ssize_t expectedSize // hehe, that's hacky ;-) - = (ssize_t)((flattened_ref*)NULL)->name + strlen(ref.name) + 1; + ssize_t expectedSize + = sizeof(dev_t) + sizeof(ino_t) + strlen(ref.name) + 1; CPPUNIT_ASSERT( size == expectedSize); CPPUNIT_ASSERT( path.Flatten(buffer, sizeof(buffer)) == B_OK ); // check the flattened data diff --git a/src/tests/kits/storage/PathTest.h b/src/tests/kits/storage/PathTest.h index 78a2a25819..8518e58a8a 100644 --- a/src/tests/kits/storage/PathTest.h +++ b/src/tests/kits/storage/PathTest.h @@ -8,8 +8,6 @@ #include #include -class CppUnit::Test; - class PathTest : public BasicTest { public: