Skip to content

Commit

Permalink
src: replace kPathSeparator with std::filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed May 28, 2024
1 parent 8e9686d commit 3b9bb49
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/compile_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ CompileCacheEntry* CompileCacheHandler::GetOrInsert(
result->code_hash = code_hash;
result->code_size = code_utf8.length();
result->cache_key = key;
result->cache_filename =
compile_cache_dir_ + kPathSeparator + Uint32ToHex(result->cache_key);
result->cache_filename = compile_cache_dir_ +
std::filesystem::path::preferred_separator +
Uint32ToHex(result->cache_key);
result->source_filename = filename_utf8.ToString();
result->cache = nullptr;
result->type = type;
Expand Down
1 change: 1 addition & 0 deletions src/compile_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <cinttypes>
#include <filesystem>
#include <memory>
#include <string>
#include <unordered_map>
Expand Down
6 changes: 4 additions & 2 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,8 @@ std::string Environment::GetCwd(const std::string& exec_path) {

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

void Environment::add_refs(int64_t diff) {
Expand Down Expand Up @@ -2064,7 +2065,8 @@ size_t Environment::NearHeapLimitCallback(void* data,
dir = Environment::GetCwd(env->exec_path_);
}
DiagnosticFilename name(env, "Heap", "heapsnapshot");
std::string filename = dir + kPathSeparator + (*name);
std::string filename =
dir + std::filesystem::path::preferred_separator + (*name);

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

Expand Down
1 change: 1 addition & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
#include <array>
#include <atomic>
#include <cstdint>
#include <filesystem>
#include <functional>
#include <list>
#include <memory>
Expand Down
4 changes: 2 additions & 2 deletions src/inspector_profiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ void V8ProfilerConnection::WriteProfile(simdjson::ondemand::object* result) {

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

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

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

// Only insert source map cache when there's source map data at all.
if (!source_map_cache_v->IsUndefined()) {
Expand Down
1 change: 1 addition & 0 deletions src/inspector_profiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#error("This header can only be used when inspector is enabled")
#endif

#include <filesystem>
#include <optional>
#include <unordered_set>
#include "inspector_agent.h"
Expand Down
16 changes: 6 additions & 10 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,6 @@ using v8::Value;
# define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif

#ifdef __POSIX__
constexpr char kPathSeparator = '/';
#else
const char* const kPathSeparator = "\\/";
#endif

inline int64_t GetOffset(Local<Value> value) {
return IsSafeJsInt(value) ? value.As<Integer>()->Value() : -1;
}
Expand Down Expand Up @@ -1591,8 +1585,10 @@ int MKDirpSync(uv_loop_t* loop,
return err;
}
case UV_ENOENT: {
std::string dirname = next_path.substr(0,
next_path.find_last_of(kPathSeparator));
std::string dirname =
next_path.substr(0,
next_path.find_last_of(
std::filesystem::path::preferred_separator));
if (dirname != next_path) {
req_wrap->continuation_data()->PushPath(std::move(next_path));
req_wrap->continuation_data()->PushPath(std::move(dirname));
Expand Down Expand Up @@ -1671,8 +1667,8 @@ int MKDirpAsync(uv_loop_t* loop,
break;
}
case UV_ENOENT: {
std::string dirname = path.substr(0,
path.find_last_of(kPathSeparator));
std::string dirname = path.substr(
0, path.find_last_of(std::filesystem::path::preferred_separator));
if (dirname != path) {
req_wrap->continuation_data()->PushPath(path);
req_wrap->continuation_data()->PushPath(std::move(dirname));
Expand Down
1 change: 1 addition & 0 deletions src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include <filesystem>
#include <optional>
#include "aliased_buffer.h"
#include "node_messaging.h"
Expand Down
4 changes: 2 additions & 2 deletions src/node_report.cc
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,9 @@ std::string TriggerNodeReport(Isolate* isolate,
report_directory = per_process::cli_options->report_directory;
}
// Regular file. Append filename to directory path if one was specified
if (report_directory.length() > 0) {
if (!report_directory.empty()) {
std::string pathname = report_directory;
pathname += kPathSeparator;
pathname += std::filesystem::path::preferred_separator;
pathname += filename;
outfile.open(pathname, std::ios::out | std::ios::binary);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/node_report.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <unistd.h>
#endif

#include <filesystem>
#include <iomanip>
#include <sstream>

Expand Down
8 changes: 4 additions & 4 deletions src/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
namespace node {

#ifdef _WIN32
bool IsPathSeparator(const char c) noexcept {
return c == kPathSeparator || c == '/';
constexpr bool IsPathSeparator(const char c) noexcept {
return c == '\\' || c == '/';
}
#else // POSIX
bool IsPathSeparator(const char c) noexcept {
return c == kPathSeparator;
constexpr bool IsPathSeparator(const char c) noexcept {
return c == '/';
}
#endif // _WIN32

Expand Down
2 changes: 1 addition & 1 deletion src/path.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace node {

class Environment;

bool IsPathSeparator(const char c) noexcept;
constexpr bool IsPathSeparator(const char c) noexcept;

std::string NormalizeString(const std::string_view path,
bool allowAboveRoot,
Expand Down
4 changes: 2 additions & 2 deletions src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ std::string WildcardIfDir(const std::string& res) noexcept {
const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr);
if ((s->st_mode & S_IFMT) == S_IFDIR) {
// add wildcard when directory
if (res.back() == node::kPathSeparator) {
if (res.back() == std::filesystem::path::preferred_separator) {
return res + "*";
}
return res + node::kPathSeparator + "*";
return res + std::filesystem::path::preferred_separator + "*";
}
}
uv_fs_req_cleanup(&req);
Expand Down
3 changes: 2 additions & 1 deletion src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "v8.h"

#include <filesystem>
#include <unordered_map>
#include "permission/permission_base.h"
#include "util.h"
Expand Down Expand Up @@ -106,7 +107,7 @@ class FSPermission final : public PermissionBase {
// path = /home/subdirectory
// child = subdirectory/*
if (idx >= path.length() &&
child->prefix[i] == node::kPathSeparator) {
child->prefix[i] == std::filesystem::path::preferred_separator) {
continue;
}

Expand Down
3 changes: 0 additions & 3 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@

namespace node {

// Maybe remove kPathSeparator when cpp17 is ready
#ifdef _WIN32
constexpr char kPathSeparator = '\\';
/* MAX_PATH is in characters, not bytes. Make sure we have enough headroom. */
#define PATH_MAX_BYTES (MAX_PATH * 4)
#else
constexpr char kPathSeparator = '/';
#define PATH_MAX_BYTES (PATH_MAX)
#endif

Expand Down

0 comments on commit 3b9bb49

Please sign in to comment.