Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3260,24 +3260,25 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
if (!error_code) {
// Check if src and dest are identical.
if (std::filesystem::equivalent(src_path, dest_path)) {
std::string message = "src and dest cannot be the same %s";
return THROW_ERR_FS_CP_EINVAL(env, message.c_str(), dest_path_str);
static constexpr const char* message =
"src and dest cannot be the same %s";
return THROW_ERR_FS_CP_EINVAL(env, message, dest_path_str);
}

const bool dest_is_dir =
dest_status.type() == std::filesystem::file_type::directory;
if (src_is_dir && !dest_is_dir) {
std::string message =
static constexpr const char* message =
"Cannot overwrite non-directory %s with directory %s";
return THROW_ERR_FS_CP_DIR_TO_NON_DIR(
env, message.c_str(), dest_path_str, src_path_str);
env, message, dest_path_str, src_path_str);
}

if (!src_is_dir && dest_is_dir) {
std::string message =
static constexpr const char* message =
"Cannot overwrite directory %s with non-directory %s";
return THROW_ERR_FS_CP_NON_DIR_TO_DIR(
env, message.c_str(), dest_path_str, src_path_str);
env, message, dest_path_str, src_path_str);
}
}

Expand All @@ -3286,9 +3287,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
}
// Check if dest_path is a subdirectory of src_path.
if (src_is_dir && dest_path_str.starts_with(src_path_str)) {
std::string message = "Cannot copy %s to a subdirectory of self %s";
return THROW_ERR_FS_CP_EINVAL(
env, message.c_str(), src_path_str, dest_path_str);
static constexpr const char* message =
"Cannot copy %s to a subdirectory of self %s";
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
}

auto dest_parent = dest_path.parent_path();
Expand All @@ -3299,9 +3300,9 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
dest_parent.parent_path() != dest_parent) {
if (std::filesystem::equivalent(
src_path, dest_path.parent_path(), error_code)) {
std::string message = "Cannot copy %s to a subdirectory of self %s";
return THROW_ERR_FS_CP_EINVAL(
env, message.c_str(), src_path_str, dest_path_str);
static constexpr const char* message =
"Cannot copy %s to a subdirectory of self %s";
return THROW_ERR_FS_CP_EINVAL(env, message, src_path_str, dest_path_str);
}

// If equivalent fails, it's highly likely that dest_parent does not exist
Expand All @@ -3313,23 +3314,24 @@ static void CpSyncCheckPaths(const FunctionCallbackInfo<Value>& args) {
}

if (src_is_dir && !recursive) {
std::string message =
static constexpr const char* message =
"Recursive option not enabled, cannot copy a directory: %s";
return THROW_ERR_FS_EISDIR(env, message.c_str(), src_path_str);
return THROW_ERR_FS_EISDIR(env, message, src_path_str);
}

switch (src_status.type()) {
case std::filesystem::file_type::socket: {
std::string message = "Cannot copy a socket file: %s";
return THROW_ERR_FS_CP_SOCKET(env, message.c_str(), dest_path_str);
static constexpr const char* message = "Cannot copy a socket file: %s";
return THROW_ERR_FS_CP_SOCKET(env, message, dest_path_str);
}
case std::filesystem::file_type::fifo: {
std::string message = "Cannot copy a FIFO pipe: %s";
return THROW_ERR_FS_CP_FIFO_PIPE(env, message.c_str(), dest_path_str);
static constexpr const char* message = "Cannot copy a FIFO pipe: %s";
return THROW_ERR_FS_CP_FIFO_PIPE(env, message, dest_path_str);
}
case std::filesystem::file_type::unknown: {
std::string message = "Cannot copy an unknown file type: %s";
return THROW_ERR_FS_CP_UNKNOWN(env, message.c_str(), dest_path_str);
static constexpr const char* message =
"Cannot copy an unknown file type: %s";
return THROW_ERR_FS_CP_UNKNOWN(env, message, dest_path_str);
}
default:
break;
Expand Down
Loading