Skip to content

Commit

Permalink
Remove get_unambiguous_path function
Browse files Browse the repository at this point in the history
  • Loading branch information
Eden-D-Zhang committed Jan 7, 2025
1 parent 5951036 commit f0af89a
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 110 deletions.
36 changes: 0 additions & 36 deletions components/core/src/clp/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> path_components;
boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on);

// Remove ambiguous components
list<string> 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<string>& paths) {
unique_ptr<FileReader> file_reader;
try {
Expand Down
7 changes: 0 additions & 7 deletions components/core/src/clp/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 0 additions & 36 deletions components/core/src/glt/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> path_components;
boost::split(path_components, path, boost::is_any_of("/"), boost::token_compress_on);

// Remove ambiguous components
list<string> 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<string>& paths) {
FileReader file_reader;
ErrorCode error_code = file_reader.try_open(list_path);
Expand Down
7 changes: 0 additions & 7 deletions components/core/src/glt/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 1 addition & 24 deletions components/core/tests/test-Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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]") {
Expand Down Expand Up @@ -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");
}
}

0 comments on commit f0af89a

Please sign in to comment.