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
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5071,7 +5071,7 @@ CpError CopyDirRecursive(const std::filesystem::path& src_path,
return CpError::Std(error, dest_str);
}

if (options.preserve_timestamps) {
if (copied && options.preserve_timestamps) {
CpError utimes = CopyUtimes(dir_entry.path(), dest_file_path);
if (utimes.kind != CpError::kNone) {
return utimes;
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-fs-cp-sync-preserve-timestamps-force-false.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// This tests that cpSync with force: false and preserveTimestamps: true leaves
// the timestamps of the destination files it skips untouched.
import { mustNotMutateObjectDeep } from '../common/index.mjs';
import { nextdir } from '../common/fs.js';
import assert from 'node:assert';
import { cpSync, mkdirSync, readFileSync, statSync, utimesSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import tmpdir from '../common/tmpdir.js';

tmpdir.refresh();

const src = nextdir();
mkdirSync(src, { recursive: true });
writeFileSync(join(src, 'file.txt'), 'src', 'utf8');
utimesSync(join(src, 'file.txt'), 1000, 1000);

// Without a filter the tree is copied in C++, with one it is walked in
// JavaScript.
for (const filter of [undefined, () => true]) {
const dest = nextdir();
mkdirSync(dest, { recursive: true });
writeFileSync(join(dest, 'file.txt'), 'dest', 'utf8');
utimesSync(join(dest, 'file.txt'), 2000, 2000);

cpSync(src, dest, mustNotMutateObjectDeep({
filter,
force: false,
preserveTimestamps: true,
recursive: true,
}));

assert.strictEqual(readFileSync(join(dest, 'file.txt'), 'utf8'), 'dest');
assert.strictEqual(statSync(join(dest, 'file.txt')).mtime.getTime(), 2000 * 1000);
}
Loading