Skip to content

Commit 0d547f8

Browse files
anonrigRafaelGSS
authored andcommitted
fixup! lib,src,permissions: port path.resolve to C++
1 parent a92a92f commit 0d547f8

File tree

3 files changed

+45
-42
lines changed

3 files changed

+45
-42
lines changed

src/util.cc

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -703,28 +703,24 @@ RAIIIsolate::RAIIIsolate(const SnapshotData* data)
703703
RAIIIsolate::~RAIIIsolate() {}
704704

705705
#ifdef _WIN32
706-
// windows
707-
708-
bool IsPathSeparator(const char c) {
706+
bool IsPathSeparator(const char c) noexcept {
709707
return c == '\\' || c == '/';
710708
}
711-
#else
712-
// posix
713-
714-
bool IsPathSeparator(const char c) {
709+
#else // POSIX
710+
bool IsPathSeparator(const char c) noexcept {
715711
return c == '/';
716712
}
717-
#endif
713+
#endif // _WIN32
718714

719-
std::string NormalizeString(const std::string path,
715+
std::string NormalizeString(const std::string_view path,
720716
bool allowAboveRoot,
721-
const std::string separator) {
722-
std::string res = "";
717+
const std::string_view separator) {
718+
std::string res;
723719
int lastSegmentLength = 0;
724720
int lastSlash = -1;
725721
int dots = 0;
726722
char code;
727-
const auto pathLen = path.length();
723+
const auto pathLen = path.size();
728724
for (uint8_t i = 0; i <= pathLen; ++i) {
729725
if (i < pathLen) {
730726
code = path[i];
@@ -764,12 +760,13 @@ std::string NormalizeString(const std::string path,
764760
}
765761

766762
if (allowAboveRoot) {
767-
res += res.length() > 0 ? separator + ".." : "..";
763+
res += res.length() > 0 ? std::string(separator) + ".." : "..";
768764
lastSegmentLength = 2;
769765
}
770766
} else {
771-
if (res.length() > 0) {
772-
res += separator + path.substr(lastSlash + 1, i - (lastSlash + 1));
767+
if (!res.empty()) {
768+
res += std::string(separator) +
769+
std::string(path.substr(lastSlash + 1, i - (lastSlash + 1)));
773770
} else {
774771
res = path.substr(lastSlash + 1, i - (lastSlash + 1));
775772
}
@@ -788,9 +785,7 @@ std::string NormalizeString(const std::string path,
788785
}
789786

790787
#ifdef _WIN32
791-
// windows
792-
793-
bool IsWindowsDeviceRoot(const char c) {
788+
bool IsWindowsDeviceRoot(const char c) const noexcept {
794789
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
795790
}
796791

@@ -800,14 +795,14 @@ std::string PathResolve(Environment* env,
800795
std::string resolvedTail = "";
801796
bool resolvedAbsolute = false;
802797
const size_t numArgs = paths.size();
798+
auto cwd = env->GetCwd(env->exec_path());
803799

804800
for (int i = numArgs - 1; i >= -1 && !resolvedAbsolute; i--) {
805-
std::string path = "";
801+
std::string path;
806802
if (i >= 0) {
807803
path = std::string(paths[i]);
808-
809804
} else if (resolvedDevice.empty()) {
810-
path = env->GetCwd();
805+
path = cwd;
811806
} else {
812807
// Windows has the concept of drive-specific current working
813808
// directories. If we've resolved a drive letter but not yet an
@@ -817,7 +812,7 @@ std::string PathResolve(Environment* env,
817812
std::string resolvedDevicePath;
818813
const std::string envvar = "=" + resolvedDevice;
819814
credentials::SafeGetenv(envvar.c_str(), &resolvedDevicePath);
820-
path = resolvedDevicePath.empty() ? env->GetCwd() : resolvedDevicePath;
815+
path = resolvedDevicePath.empty() ? cwd : resolvedDevicePath;
821816

822817
// Verify that a cwd was found and that it actually points
823818
// to our drive. If not, default to the drive's root.
@@ -932,32 +927,39 @@ std::string PathResolve(Environment* env,
932927

933928
return ".";
934929
}
935-
#else
936-
// posix
930+
#else // _WIN32
937931
std::string PathResolve(Environment* env,
938932
const std::vector<std::string_view>& paths) {
939-
std::string resolvedPath = "";
933+
std::string resolvedPath;
940934
bool resolvedAbsolute = false;
941-
const size_t numArgs = paths.size();
935+
auto cwd = env->GetCwd(env->exec_path());
942936

943937
for (int i = numArgs - 1; i >= -1 && !resolvedAbsolute; i--) {
944938
const std::string& path = (i >= 0) ? std::string(paths[i]) : env->GetCwd(env->exec_path());
945939
/* validateString(path, "paths[" + std::to_string(i) + "]"); */
946940

947941
if (!path.empty()) {
948-
resolvedPath = std::string(path + "/" + resolvedPath);
949-
resolvedAbsolute = (path[0] == '/');
942+
resolvedPath = std::string(path) + "/" + resolvedPath;
943+
944+
if (path.front() == '/') {
945+
resolvedAbsolute = true;
946+
break;
947+
}
950948
}
951949
}
952950

953951
// Normalize the path
954-
resolvedPath = NormalizeString(resolvedPath, !resolvedAbsolute, "/");
952+
auto normalizedPath = NormalizeString(resolvedPath, !resolvedAbsolute, "/");
955953

956954
if (resolvedAbsolute) {
957-
return "/" + resolvedPath;
955+
return "/" + normalizedPath;
956+
}
957+
958+
if (normalizedPath.empty()) {
959+
return ".";
958960
}
959-
return (!resolvedPath.empty()) ? resolvedPath : ".";
960-
}
961-
#endif
962961

962+
return normalizedPath;
963+
}
964+
#endif // _WIN32
963965
} // namespace node

src/util.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -999,14 +999,14 @@ class RAIIIsolate {
999999
class Environment;
10001000

10011001
#ifdef _WIN32
1002-
bool IsWindowsDeviceRoot(const char c);
1003-
#endif
1002+
bool IsWindowsDeviceRoot(const char c) const noexcept;
1003+
#endif // _WIN32
10041004

1005-
bool IsPathSeparator(const char c);
1005+
bool IsPathSeparator(const char c) noexcept;
10061006

1007-
std::string NormalizeString(const std::string path,
1007+
std::string NormalizeString(const std::string_view path,
10081008
bool allowAboveRoot,
1009-
const std::string separator);
1009+
const std::string_view separator);
10101010

10111011
std::string PathResolve(Environment* env,
10121012
const std::vector<std::string_view>& args);

test/cctest/test_util.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,15 @@ TEST_F(UtilTest, SPrintF) {
305305
EXPECT_EQ(SPrintF("%s", with_zero), with_zero);
306306
}
307307

308-
TEST(UtilTest, DumpJavaScriptStackWithNoIsolate) {
308+
TEST_F(UtilTest, DumpJavaScriptStackWithNoIsolate) {
309309
node::DumpJavaScriptBacktrace(stderr);
310310
}
311311

312312
TEST_F(UtilTest, PathResolve) {
313313
const v8::HandleScope handle_scope(isolate_);
314314
Argv argv;
315315
Env env{handle_scope, argv, node::EnvironmentFlags::kNoBrowserGlobals};
316+
auto cwd = (*env)->GetCwd((*env)->exec_path());
316317
#ifdef _WIN32
317318
EXPECT_EQ(PathResolve(*env, {"c:/blah\\blah", "d:/games", "c:../a"}),
318319
"c:\\blah\\a");
@@ -321,7 +322,7 @@ TEST_F(UtilTest, PathResolve) {
321322
EXPECT_EQ(PathResolve(*env, {"c:/ignore", "c:/some/file"}), "c:\\some\\file");
322323
EXPECT_EQ(PathResolve(*env, {"d:/ignore", "d:some/dir//"}),
323324
"d:\\ignore\\some\\dir");
324-
EXPECT_EQ(PathResolve(*env, {"."}), (*env)->GetCwd((*env)->exec_path()));
325+
EXPECT_EQ(PathResolve(*env, {"."}), cwd);
325326
EXPECT_EQ(PathResolve(*env, {"//server/share", "..", "relative\\"}),
326327
"\\\\server\\share\\relative");
327328
EXPECT_EQ(PathResolve(*env, {"c:/", "//"}), "c:\\");
@@ -337,8 +338,8 @@ TEST_F(UtilTest, PathResolve) {
337338
#else
338339
EXPECT_EQ(PathResolve(*env, {"/var/lib", "../", "file/"}), "/var/file");
339340
EXPECT_EQ(PathResolve(*env, {"/var/lib", "/../", "file/"}), "/file");
340-
EXPECT_EQ(PathResolve(*env, {"a/b/c/", "../../.."}), (*env)->GetCwd((*env)->exec_path()));
341-
EXPECT_EQ(PathResolve(*env, {"."}), (*env)->GetCwd((*env)->exec_path()));
341+
EXPECT_EQ(PathResolve(*env, {"a/b/c/", "../../.."}), cwd);
342+
EXPECT_EQ(PathResolve(*env, {"."}), cwd);
342343
EXPECT_EQ(PathResolve(*env, {"/some/dir", ".", "/absolute/"}), "/absolute");
343344
EXPECT_EQ(PathResolve(*env, {"/foo/tmp.3/", "../tmp.3/cycles/root.js"}),
344345
"/foo/tmp.3/cycles/root.js");

0 commit comments

Comments
 (0)