From 59510367b31670f5ff79ca06bf42f8341dad17eb Mon Sep 17 00:00:00 2001 From: Eden Zhang Date: Tue, 7 Jan 2025 17:01:53 +0000 Subject: [PATCH 1/4] Remove get_parent_directory function --- components/core/src/clp/Utils.cpp | 15 --------------- components/core/src/clp/Utils.hpp | 15 --------------- components/core/src/glt/Utils.cpp | 15 --------------- components/core/src/glt/Utils.hpp | 15 --------------- components/core/tests/test-Utils.cpp | 17 ----------------- 5 files changed, 77 deletions(-) diff --git a/components/core/src/clp/Utils.cpp b/components/core/src/clp/Utils.cpp index f487a3880..44c343cea 100644 --- a/components/core/src/clp/Utils.cpp +++ b/components/core/src/clp/Utils.cpp @@ -88,21 +88,6 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) { return ErrorCode_Success; } -string get_parent_directory_path(string const& path) { - string dirname = get_unambiguous_path(path); - - size_t last_slash_pos = dirname.find_last_of('/'); - if (0 == last_slash_pos) { - dirname = "/"; - } else if (string::npos == last_slash_pos) { - dirname = "."; - } else { - dirname.resize(last_slash_pos); - } - - return dirname; -} - string get_unambiguous_path(string const& path) { string unambiguous_path; if (path.empty()) { diff --git a/components/core/src/clp/Utils.hpp b/components/core/src/clp/Utils.hpp index de7f81aae..7a9ffd33a 100644 --- a/components/core/src/clp/Utils.hpp +++ b/components/core/src/clp/Utils.hpp @@ -35,21 +35,6 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok); */ ErrorCode create_directory_structure(std::string const& path, mode_t mode); -/** - * Gets the parent directory path for a given path - * Corner cases: - * - get_dirname("abc") = "." - * - get_dirname(".") = "." - * - get_dirname("..") = "." - * - get_dirname("/") = "/" - * - get_dirname("/.") = "/" - * - get_dirname("/..") = "/" - * - get_dirname("/abc") = "/" - * @param path - * @return Parent directory path - */ -std::string get_parent_directory_path(std::string const& path); - /** * Removes ".", "..", and consecutive "/" from a given path and returns the result * @param path The given path diff --git a/components/core/src/glt/Utils.cpp b/components/core/src/glt/Utils.cpp index 64b2ed36d..d39291339 100644 --- a/components/core/src/glt/Utils.cpp +++ b/components/core/src/glt/Utils.cpp @@ -84,21 +84,6 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) { return ErrorCode_Success; } -string get_parent_directory_path(string const& path) { - string dirname = get_unambiguous_path(path); - - size_t last_slash_pos = dirname.find_last_of('/'); - if (0 == last_slash_pos) { - dirname = "/"; - } else if (string::npos == last_slash_pos) { - dirname = "."; - } else { - dirname.resize(last_slash_pos); - } - - return dirname; -} - string get_unambiguous_path(string const& path) { string unambiguous_path; if (path.empty()) { diff --git a/components/core/src/glt/Utils.hpp b/components/core/src/glt/Utils.hpp index 2e473ef5f..851a1ea72 100644 --- a/components/core/src/glt/Utils.hpp +++ b/components/core/src/glt/Utils.hpp @@ -33,21 +33,6 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok); */ ErrorCode create_directory_structure(std::string const& path, mode_t mode); -/** - * Gets the parent directory path for a given path - * Corner cases: - * - get_dirname("abc") = "." - * - get_dirname(".") = "." - * - get_dirname("..") = "." - * - get_dirname("/") = "/" - * - get_dirname("/.") = "/" - * - get_dirname("/..") = "/" - * - get_dirname("/abc") = "/" - * @param path - * @return Parent directory path - */ -std::string get_parent_directory_path(std::string const& path); - /** * Removes ".", "..", and consecutive "/" from a given path and returns the result * @param path The given path diff --git a/components/core/tests/test-Utils.cpp b/components/core/tests/test-Utils.cpp index 603fb4be0..c12039e3f 100644 --- a/components/core/tests/test-Utils.cpp +++ b/components/core/tests/test-Utils.cpp @@ -14,7 +14,6 @@ using clp::create_directory_structure; using clp::ErrorCode_Success; -using clp::get_parent_directory_path; using clp::get_unambiguous_path; using std::string; @@ -45,22 +44,6 @@ TEST_CASE("create_directory_structure", "[create_directory_structure]") { REQUIRE(0 == rmdir("/tmp/5807")); } -TEST_CASE("get_parent_directory_path", "[get_parent_directory_path]") { - // Corner cases - // Anything without a slash should return "." - REQUIRE(get_parent_directory_path(".") == "."); - REQUIRE(get_parent_directory_path("..") == "."); - REQUIRE(get_parent_directory_path("abc") == "."); - // A single slash, at the beginning, should always return "/" - REQUIRE(get_parent_directory_path("/") == "/"); - REQUIRE(get_parent_directory_path("/.") == "/"); - REQUIRE(get_parent_directory_path("/..") == "/"); - REQUIRE(get_parent_directory_path("/abc") == "/"); - - // Normal cases - REQUIRE(get_parent_directory_path("//abc/./def//../def/.///") == "/abc"); -} - TEST_CASE("get_unambiguous_path", "[get_unambiguous_path]") { // Base cases (should not modify anything) REQUIRE(get_unambiguous_path("/") == "/"); From f0af89ad6fee292d7919e1681b0cb5bea9f30b8c Mon Sep 17 00:00:00 2001 From: Eden Zhang Date: Tue, 7 Jan 2025 17:20:38 +0000 Subject: [PATCH 2/4] Remove get_unambiguous_path function --- components/core/src/clp/Utils.cpp | 36 ---------------------------- components/core/src/clp/Utils.hpp | 7 ------ components/core/src/glt/Utils.cpp | 36 ---------------------------- components/core/src/glt/Utils.hpp | 7 ------ components/core/tests/test-Utils.cpp | 25 +------------------ 5 files changed, 1 insertion(+), 110 deletions(-) diff --git a/components/core/src/clp/Utils.cpp b/components/core/src/clp/Utils.cpp index 44c343cea..1c0fc05ca 100644 --- a/components/core/src/clp/Utils.cpp +++ b/components/core/src/clp/Utils.cpp @@ -88,42 +88,6 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) { return ErrorCode_Success; } -string get_unambiguous_path(string const& path) { - string unambiguous_path; - if (path.empty()) { - return unambiguous_path; - } - - // Break path into components - vector path_components; - boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on); - - // Remove ambiguous components - list unambiguous_components; - size_t num_components_to_ignore = 0; - for (size_t i = path_components.size(); i-- > 0;) { - if (".." == path_components[i]) { - ++num_components_to_ignore; - } else if ("." == path_components[i] || path_components[i].empty()) { - // Do nothing - } else if (num_components_to_ignore > 0) { - --num_components_to_ignore; - } else { - unambiguous_components.emplace_front(path_components[i]); - } - } - - // Assemble unambiguous path from leading slash (if any) and the unambiguous components - if ('/' == path[0]) { - unambiguous_path += '/'; - } - if (!unambiguous_components.empty()) { - unambiguous_path += boost::join(unambiguous_components, "/"); - } - - return unambiguous_path; -} - ErrorCode read_list_of_paths(string const& list_path, vector& paths) { unique_ptr file_reader; try { diff --git a/components/core/src/clp/Utils.hpp b/components/core/src/clp/Utils.hpp index 7a9ffd33a..3238e551b 100644 --- a/components/core/src/clp/Utils.hpp +++ b/components/core/src/clp/Utils.hpp @@ -35,13 +35,6 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok); */ ErrorCode create_directory_structure(std::string const& path, mode_t mode); -/** - * Removes ".", "..", and consecutive "/" from a given path and returns the result - * @param path The given path - * @return The unambiguous path - */ -std::string get_unambiguous_path(std::string const& path); - /** * Read a list of paths from a file * @param list_path diff --git a/components/core/src/glt/Utils.cpp b/components/core/src/glt/Utils.cpp index d39291339..0434372fe 100644 --- a/components/core/src/glt/Utils.cpp +++ b/components/core/src/glt/Utils.cpp @@ -84,42 +84,6 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) { return ErrorCode_Success; } -string get_unambiguous_path(string const& path) { - string unambiguous_path; - if (path.empty()) { - return unambiguous_path; - } - - // Break path into components - vector path_components; - boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on); - - // Remove ambiguous components - list unambiguous_components; - size_t num_components_to_ignore = 0; - for (size_t i = path_components.size(); i-- > 0;) { - if (".." == path_components[i]) { - ++num_components_to_ignore; - } else if ("." == path_components[i] || path_components[i].empty()) { - // Do nothing - } else if (num_components_to_ignore > 0) { - --num_components_to_ignore; - } else { - unambiguous_components.emplace_front(path_components[i]); - } - } - - // Assemble unambiguous path from leading slash (if any) and the unambiguous components - if ('/' == path[0]) { - unambiguous_path += '/'; - } - if (!unambiguous_components.empty()) { - unambiguous_path += boost::join(unambiguous_components, "/"); - } - - return unambiguous_path; -} - ErrorCode read_list_of_paths(string const& list_path, vector& paths) { FileReader file_reader; ErrorCode error_code = file_reader.try_open(list_path); diff --git a/components/core/src/glt/Utils.hpp b/components/core/src/glt/Utils.hpp index 851a1ea72..302a13c73 100644 --- a/components/core/src/glt/Utils.hpp +++ b/components/core/src/glt/Utils.hpp @@ -33,13 +33,6 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok); */ ErrorCode create_directory_structure(std::string const& path, mode_t mode); -/** - * Removes ".", "..", and consecutive "/" from a given path and returns the result - * @param path The given path - * @return The unambiguous path - */ -std::string get_unambiguous_path(std::string const& path); - /** * Read a list of paths from a file * @param list_path diff --git a/components/core/tests/test-Utils.cpp b/components/core/tests/test-Utils.cpp index c12039e3f..fcc6bf262 100644 --- a/components/core/tests/test-Utils.cpp +++ b/components/core/tests/test-Utils.cpp @@ -14,7 +14,6 @@ using clp::create_directory_structure; using clp::ErrorCode_Success; -using clp::get_unambiguous_path; using std::string; TEST_CASE("create_directory_structure", "[create_directory_structure]") { @@ -42,26 +41,4 @@ TEST_CASE("create_directory_structure", "[create_directory_structure]") { REQUIRE(0 == rmdir("d")); REQUIRE(0 == rmdir("/tmp/5807")); -} - -TEST_CASE("get_unambiguous_path", "[get_unambiguous_path]") { - // Base cases (should not modify anything) - REQUIRE(get_unambiguous_path("/") == "/"); - REQUIRE(get_unambiguous_path("abc") == "abc"); - REQUIRE(get_unambiguous_path("/abc") == "/abc"); - REQUIRE(get_unambiguous_path("/abc/def") == "/abc/def"); - - // Corner cases - REQUIRE(get_unambiguous_path(".").empty()); - REQUIRE(get_unambiguous_path("..").empty()); - REQUIRE(get_unambiguous_path("////") == "/"); - REQUIRE(get_unambiguous_path("/./.././//../") == "/"); - REQUIRE(get_unambiguous_path("./.././//../").empty()); - REQUIRE(get_unambiguous_path("/abc/def/.././../") == "/"); - REQUIRE(get_unambiguous_path("abc/def/.././../").empty()); - - // Normal cases - REQUIRE(get_unambiguous_path("/abc///def/../ghi/./") == "/abc/ghi"); - REQUIRE(get_unambiguous_path("abc///def/../ghi/./") == "abc/ghi"); - REQUIRE(get_unambiguous_path("../abc///def/../ghi/./") == "abc/ghi"); -} +} \ No newline at end of file From dbd19c676d6bfcb268fc52e31ca7079ad8dce780 Mon Sep 17 00:00:00 2001 From: Eden Zhang Date: Tue, 7 Jan 2025 19:22:26 +0000 Subject: [PATCH 3/4] Add empty line --- components/core/tests/test-Utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/core/tests/test-Utils.cpp b/components/core/tests/test-Utils.cpp index fcc6bf262..21d070f92 100644 --- a/components/core/tests/test-Utils.cpp +++ b/components/core/tests/test-Utils.cpp @@ -41,4 +41,4 @@ TEST_CASE("create_directory_structure", "[create_directory_structure]") { REQUIRE(0 == rmdir("d")); REQUIRE(0 == rmdir("/tmp/5807")); -} \ No newline at end of file +} From d9424313f8ee8c2ec9af4291a3a7d5348eb04a8e Mon Sep 17 00:00:00 2001 From: Eden Zhang Date: Tue, 7 Jan 2025 20:24:32 +0000 Subject: [PATCH 4/4] Revert changes to glt Utils --- components/core/src/glt/Utils.cpp | 51 +++++++++++++++++++++++++++++++ components/core/src/glt/Utils.hpp | 22 +++++++++++++ 2 files changed, 73 insertions(+) diff --git a/components/core/src/glt/Utils.cpp b/components/core/src/glt/Utils.cpp index 0434372fe..64b2ed36d 100644 --- a/components/core/src/glt/Utils.cpp +++ b/components/core/src/glt/Utils.cpp @@ -84,6 +84,57 @@ ErrorCode create_directory_structure(string const& path, mode_t mode) { return ErrorCode_Success; } +string get_parent_directory_path(string const& path) { + string dirname = get_unambiguous_path(path); + + size_t last_slash_pos = dirname.find_last_of('/'); + if (0 == last_slash_pos) { + dirname = "/"; + } else if (string::npos == last_slash_pos) { + dirname = "."; + } else { + dirname.resize(last_slash_pos); + } + + return dirname; +} + +string get_unambiguous_path(string const& path) { + string unambiguous_path; + if (path.empty()) { + return unambiguous_path; + } + + // Break path into components + vector path_components; + boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on); + + // Remove ambiguous components + list unambiguous_components; + size_t num_components_to_ignore = 0; + for (size_t i = path_components.size(); i-- > 0;) { + if (".." == path_components[i]) { + ++num_components_to_ignore; + } else if ("." == path_components[i] || path_components[i].empty()) { + // Do nothing + } else if (num_components_to_ignore > 0) { + --num_components_to_ignore; + } else { + unambiguous_components.emplace_front(path_components[i]); + } + } + + // Assemble unambiguous path from leading slash (if any) and the unambiguous components + if ('/' == path[0]) { + unambiguous_path += '/'; + } + if (!unambiguous_components.empty()) { + unambiguous_path += boost::join(unambiguous_components, "/"); + } + + return unambiguous_path; +} + ErrorCode read_list_of_paths(string const& list_path, vector& paths) { FileReader file_reader; ErrorCode error_code = file_reader.try_open(list_path); diff --git a/components/core/src/glt/Utils.hpp b/components/core/src/glt/Utils.hpp index 302a13c73..2e473ef5f 100644 --- a/components/core/src/glt/Utils.hpp +++ b/components/core/src/glt/Utils.hpp @@ -33,6 +33,28 @@ ErrorCode create_directory(std::string const& path, mode_t mode, bool exist_ok); */ ErrorCode create_directory_structure(std::string const& path, mode_t mode); +/** + * Gets the parent directory path for a given path + * Corner cases: + * - get_dirname("abc") = "." + * - get_dirname(".") = "." + * - get_dirname("..") = "." + * - get_dirname("/") = "/" + * - get_dirname("/.") = "/" + * - get_dirname("/..") = "/" + * - get_dirname("/abc") = "/" + * @param path + * @return Parent directory path + */ +std::string get_parent_directory_path(std::string const& path); + +/** + * Removes ".", "..", and consecutive "/" from a given path and returns the result + * @param path The given path + * @return The unambiguous path + */ +std::string get_unambiguous_path(std::string const& path); + /** * Read a list of paths from a file * @param list_path