From e1b4aed0cbcb476a8207f75af63dc15f1ac54d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sun, 30 Apr 2017 13:07:48 +0200 Subject: [PATCH] KPath: Fixed Normalize() return code, changed Leaf(). * Normalize() now returns the error code that vfs_normalize_path() returns. * Leaf() now returns "" instead of "/" for the root. It's not used outside of KPath. * Adapted RemoveLeaf() to deal with this correctly. * "KPath = string" no longer changes the buffer size. * Added missing operator tests for =, ==, and !=. --- src/system/kernel/fs/KPath.cpp | 23 ++++---- src/tests/system/kernel/fs/KPathTest.cpp | 68 ++++++++++++++++++++++-- src/tests/system/kernel/fs/KPathTest.h | 2 + 3 files changed, 76 insertions(+), 17 deletions(-) diff --git a/src/system/kernel/fs/KPath.cpp b/src/system/kernel/fs/KPath.cpp index f172adb595..4e76e7fd3a 100644 --- a/src/system/kernel/fs/KPath.cpp +++ b/src/system/kernel/fs/KPath.cpp @@ -1,5 +1,6 @@ /* * Copyright 2004-2008, Ingo Weinhold, ingo_weinhold@gmx.de. + * Copyright 2008-2017, Axel Dörfler, axeld@pinc-software.de. * Distributed under the terms of the MIT License. */ @@ -106,7 +107,7 @@ KPath::Adopt(KPath& other) status_t KPath::InitCheck() const { - return fBuffer ? B_OK : B_NO_MEMORY; + return fBuffer != NULL ? B_OK : B_NO_MEMORY; } @@ -203,14 +204,11 @@ KPath::Leaf() const if (fBuffer == NULL) return NULL; - // only "/" has trailing slashes -- then we have to return the complete - // buffer, as we have to do in case there are no slashes at all - if (fPathLength != 1 || fBuffer[0] != '/') { - for (int32 i = fPathLength - 1; i >= 0; i--) { - if (fBuffer[i] == '/') - return fBuffer + i + 1; - } + for (int32 i = fPathLength - 1; i >= 0; i--) { + if (fBuffer[i] == '/') + return fBuffer + i + 1; } + return fBuffer; } @@ -242,7 +240,7 @@ KPath::RemoveLeaf() { // get the leaf -- bail out, if not initialized or only the "/" is left const char* leaf = Leaf(); - if (leaf == NULL || leaf == fBuffer) + if (leaf == NULL || leaf == fBuffer || leaf[0] == '\0') return false; // chop off the leaf @@ -305,6 +303,7 @@ KPath::Normalize(bool traverseLeafLink) // it completely to avoid weird problems. fBuffer[0] = '\0'; fPathLength = 0; + return error; } fPathLength = strlen(fBuffer); @@ -323,7 +322,7 @@ KPath::operator=(const KPath& other) KPath& KPath::operator=(const char* path) { - SetTo(path); + SetPath(path); return *this; } @@ -331,7 +330,7 @@ KPath::operator=(const char* path) bool KPath::operator==(const KPath& other) const { - if (!fBuffer) + if (fBuffer == NULL) return !other.fBuffer; return other.fBuffer @@ -343,7 +342,7 @@ KPath::operator==(const KPath& other) const bool KPath::operator==(const char* path) const { - if (!fBuffer) + if (fBuffer == NULL) return (!path); return path && strcmp(fBuffer, path) == 0; diff --git a/src/tests/system/kernel/fs/KPathTest.cpp b/src/tests/system/kernel/fs/KPathTest.cpp index 3108478cc5..117c605c5a 100644 --- a/src/tests/system/kernel/fs/KPathTest.cpp +++ b/src/tests/system/kernel/fs/KPathTest.cpp @@ -97,8 +97,7 @@ KPathTest::TestLeaf() path.SetTo("/"); CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0); -// CPPUNIT_ASSERT(path.Leaf() == NULL); -// TODO: why '/'? + CPPUNIT_ASSERT(strcmp(path.Leaf(), "") == 0); path.SetTo("a/b"); CPPUNIT_ASSERT(strcmp(path.Path(), "a/b") == 0); @@ -150,19 +149,30 @@ KPathTest::TestRemoveLeaf() path.SetTo("a//b/c"); removed = path.RemoveLeaf(); - CPPUNIT_ASSERT(removed); + CPPUNIT_ASSERT_MESSAGE("1. removed", removed); CPPUNIT_ASSERT(strcmp(path.Path(), "a//b") == 0); CPPUNIT_ASSERT(path.Length() == 4); removed = path.RemoveLeaf(); - CPPUNIT_ASSERT(removed); + CPPUNIT_ASSERT_MESSAGE("2. removed", removed); CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); CPPUNIT_ASSERT(path.Length() == 1); removed = path.RemoveLeaf(); - CPPUNIT_ASSERT(!removed); + CPPUNIT_ASSERT_MESSAGE("3. !removed", !removed); CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); CPPUNIT_ASSERT(path.Length() == 1); + + path.SetTo("/a"); + removed = path.RemoveLeaf(); + CPPUNIT_ASSERT_MESSAGE("4. removed", removed); + CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0); + CPPUNIT_ASSERT(path.Length() == 1); + + removed = path.RemoveLeaf(); + CPPUNIT_ASSERT_MESSAGE("5. !removed", !removed); + CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0); + CPPUNIT_ASSERT(path.Length() == 1); } @@ -218,9 +228,53 @@ KPathTest::TestNormalize() } +void +KPathTest::TestAssign() +{ + KPath one("first", false, 10); + CPPUNIT_ASSERT(one.Length() == 5); + KPath two("second", false, 20); + + two = one; + CPPUNIT_ASSERT(strcmp(two.Path(), one.Path()) == 0); + CPPUNIT_ASSERT(two.Path() != one.Path()); + CPPUNIT_ASSERT(two.BufferSize() == one.BufferSize()); + CPPUNIT_ASSERT(two.Length() == one.Length()); + + one = "/whatever"; + CPPUNIT_ASSERT(one.Length() == 9); + CPPUNIT_ASSERT(one.BufferSize() == two.BufferSize()); + CPPUNIT_ASSERT(strcmp(one.Path(), "/whatever") == 0); +} + + void KPathTest::TestEquals() { + KPath a("one"); + KPath b("two"); + CPPUNIT_ASSERT_MESSAGE("1.", !(a == b)); + + b = a; + CPPUNIT_ASSERT_MESSAGE("2.", a == b); + + b = "ones"; + CPPUNIT_ASSERT_MESSAGE("3.", !(a == b)); +} + + +void +KPathTest::TestNotEquals() +{ + KPath a("one"); + KPath b("two"); + CPPUNIT_ASSERT_MESSAGE("1.", a != b); + + b = a; + CPPUNIT_ASSERT_MESSAGE("2.", !(a != b)); + + b = "ones"; + CPPUNIT_ASSERT_MESSAGE("3.", a != b); } @@ -243,8 +297,12 @@ KPathTest::AddTests(BTestSuite& parent) "KPathTest::TestDetachBuffer", &KPathTest::TestDetachBuffer)); suite.addTest(new CppUnit::TestCaller( "KPathTest::TestNormalize", &KPathTest::TestNormalize)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestAssign", &KPathTest::TestAssign)); suite.addTest(new CppUnit::TestCaller( "KPathTest::TestEquals", &KPathTest::TestEquals)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestNotEquals", &KPathTest::TestNotEquals)); parent.addTest("KPathTest", &suite); } diff --git a/src/tests/system/kernel/fs/KPathTest.h b/src/tests/system/kernel/fs/KPathTest.h index dd32bbb17d..fceea30ae9 100644 --- a/src/tests/system/kernel/fs/KPathTest.h +++ b/src/tests/system/kernel/fs/KPathTest.h @@ -22,7 +22,9 @@ public: void TestAdopt(); void TestDetachBuffer(); void TestNormalize(); + void TestAssign(); void TestEquals(); + void TestNotEquals(); static void AddTests(BTestSuite& suite); };