Skip to content

Commit 85b1a5a

Browse files
committed
src: replace kPathSeparator with std::filesystem
1 parent 17a7663 commit 85b1a5a

15 files changed

Lines changed: 41 additions & 46 deletions

src/compile_cache.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,9 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(
205205
result->code_hash = code_hash;
206206
result->code_size = code_utf8.length();
207207
result->cache_key = key;
208-
result->cache_filename =
209-
compile_cache_dir_ + kPathSeparator + Uint32ToHex(result->cache_key);
208+
result->cache_filename = compile_cache_dir_ +
209+
std::filesystem::path::preferred_separator +
210+
Uint32ToHex(result->cache_key);
210211
result->source_filename = filename_utf8.ToString();
211212
result->cache = nullptr;
212213
result->type = type;

src/compile_cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

66
#include <cinttypes>
7+
#include <filesystem>
78
#include <memory>
89
#include <string>
910
#include <unordered_map>

src/env.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ std::string Environment::GetCwd(const std::string& exec_path) {
713713

714714
// This can fail if the cwd is deleted. In that case, fall back to
715715
// exec_path.
716-
return exec_path.substr(0, exec_path.find_last_of(kPathSeparator));
716+
return exec_path.substr(
717+
0, exec_path.find_last_of(std::filesystem::path::preferred_separator));
717718
}
718719

719720
void Environment::add_refs(int64_t diff) {
@@ -2064,7 +2065,8 @@ size_t Environment::NearHeapLimitCallback(void* data,
20642065
dir = Environment::GetCwd(env->exec_path_);
20652066
}
20662067
DiagnosticFilename name(env, "Heap", "heapsnapshot");
2067-
std::string filename = dir + kPathSeparator + (*name);
2068+
std::string filename =
2069+
dir + std::filesystem::path::preferred_separator + (*name);
20682070

20692071
Debug(env, DebugCategory::DIAGNOSTICS, "Start generating %s...\n", *name);
20702072

src/env.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include <array>
5858
#include <atomic>
5959
#include <cstdint>
60+
#include <filesystem>
6061
#include <functional>
6162
#include <list>
6263
#include <memory>

src/inspector_profiler.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) {
248248

249249
std::string filename = GetFilename();
250250
DCHECK(!filename.empty());
251-
std::string path = directory + kPathSeparator + filename;
251+
auto path = std::filesystem::path(directory) / filename;
252252

253253
WriteResult(env_, path.c_str(), profile);
254254
}
@@ -304,7 +304,7 @@ void V8CoverageConnection::WriteProfile(simdjson::ondemand::object* result) {
304304

305305
std::string filename = GetFilename();
306306
DCHECK(!filename.empty());
307-
std::string path = directory + kPathSeparator + filename;
307+
auto path = std::filesystem::path(directory) / filename;
308308

309309
// Only insert source map cache when there's source map data at all.
310310
if (!source_map_cache_v->IsUndefined()) {

src/inspector_profiler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#error("This header can only be used when inspector is enabled")
88
#endif
99

10+
#include <filesystem>
1011
#include <optional>
1112
#include <unordered_set>
1213
#include "inspector_agent.h"

src/node_file.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,10 @@ using v8::Value;
7676
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
7777
#endif
7878

79-
#ifdef __POSIX__
80-
constexpr char kPathSeparator = '/';
81-
#else
82-
const char* const kPathSeparator = "\\/";
83-
#endif
84-
8579
std::string Basename(const std::string& str, const std::string& extension) {
8680
// Remove everything leading up to and including the final path separator.
87-
std::string::size_type pos = str.find_last_of(kPathSeparator);
81+
std::string::size_type pos =
82+
str.find_last_of(std::filesystem::path::preferred_separator);
8883

8984
// Starting index for the resulting string
9085
std::size_t start_pos = 0;
@@ -1614,8 +1609,10 @@ int MKDirpSync(uv_loop_t* loop,
16141609
return err;
16151610
}
16161611
case UV_ENOENT: {
1617-
std::string dirname = next_path.substr(0,
1618-
next_path.find_last_of(kPathSeparator));
1612+
std::string dirname =
1613+
next_path.substr(0,
1614+
next_path.find_last_of(
1615+
std::filesystem::path::preferred_separator));
16191616
if (dirname != next_path) {
16201617
req_wrap->continuation_data()->PushPath(std::move(next_path));
16211618
req_wrap->continuation_data()->PushPath(std::move(dirname));
@@ -1694,8 +1691,8 @@ int MKDirpAsync(uv_loop_t* loop,
16941691
break;
16951692
}
16961693
case UV_ENOENT: {
1697-
std::string dirname = path.substr(0,
1698-
path.find_last_of(kPathSeparator));
1694+
std::string dirname = path.substr(
1695+
0, path.find_last_of(std::filesystem::path::preferred_separator));
16991696
if (dirname != path) {
17001697
req_wrap->continuation_data()->PushPath(path);
17011698
req_wrap->continuation_data()->PushPath(std::move(dirname));

src/node_file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
55

6+
#include <filesystem>
67
#include <optional>
78
#include "aliased_buffer.h"
89
#include "node_messaging.h"

src/node_report.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,9 @@ std::string TriggerNodeReport(Isolate* isolate,
887887
report_directory = per_process::cli_options->report_directory;
888888
}
889889
// Regular file. Append filename to directory path if one was specified
890-
if (report_directory.length() > 0) {
890+
if (!report_directory.empty()) {
891891
std::string pathname = report_directory;
892-
pathname += kPathSeparator;
892+
pathname += std::filesystem::path::preferred_separator;
893893
pathname += filename;
894894
outfile.open(pathname, std::ios::out | std::ios::binary);
895895
} else {

src/node_report.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <unistd.h>
1414
#endif
1515

16+
#include <filesystem>
1617
#include <iomanip>
1718
#include <sstream>
1819

0 commit comments

Comments
 (0)