Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4207,6 +4207,10 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
if (!success) {
return false;
}
if (preserve_timestamps &&
!CopyUtimes(entry_dir_path, dest_file_path, env)) {
return false;
}
Comment on lines +4210 to +4213

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be feasible to cover this in tests? I.e. create a situation where CopyUtimes returns false here.

} else if (dir_entry.is_regular_file()) {
std::filesystem::copy_file(
dir_entry.path(), dest_file_path, file_copy_opts, error);
Expand All @@ -4231,7 +4235,9 @@ static void CpSyncCopyDir(const FunctionCallbackInfo<Value>& args) {
return true;
};

copy_dir_contents(src_path, dest_path);
if (copy_dir_contents(src_path, dest_path) && preserve_timestamps) {
CopyUtimes(src_path, dest_path, env);
}
}

BindingData::FilePathIsFileReturnType BindingData::FilePathIsFile(
Expand Down
23 changes: 21 additions & 2 deletions test/parallel/test-fs-cp-sync-preserve-timestamps-dir.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// This tests that cpSync with a filter preserves directory timestamps
// when preserveTimestamps is true.
// This tests that cpSync preserves directory timestamps
// when preserveTimestamps is true, both on the JS fallback path (with filter)
// and the native fast path (without filter).
import '../common/index.mjs';
import { nextdir } from '../common/fs.js';
import assert from 'node:assert';
Expand Down Expand Up @@ -40,3 +41,21 @@ assert.strictEqual(srcDirStat.mtime.getTime(), destDirStat.mtime.getTime());
const srcRootStat = statSync(src);
const destRootStat = statSync(dest);
assert.strictEqual(srcRootStat.mtime.getTime(), destRootStat.mtime.getTime());

// Copy with preserveTimestamps and NO filter (to exercise the native fast path).
const destFast = nextdir();
cpSync(src, destFast, {
recursive: true,
preserveTimestamps: true,
});

// Verify file timestamps are preserved.
const destFastFileStat = statSync(join(destFast, 'subdir', 'file.txt'));
assert.strictEqual(srcFileStat.mtime.getTime(), destFastFileStat.mtime.getTime());

// Verify directory timestamps are preserved.
const destFastDirStat = statSync(join(destFast, 'subdir'));
assert.strictEqual(srcDirStat.mtime.getTime(), destFastDirStat.mtime.getTime());

const destFastRootStat = statSync(destFast);
assert.strictEqual(srcRootStat.mtime.getTime(), destFastRootStat.mtime.getTime());
Loading