Skip to content

Commit 18d8bbf

Browse files
joshi-shnfischer
authored andcommitted
feat(cp): support update flag when recursing (#889)
Fixed `cp -Ru` ignoring `-u`, and added a test. Fixes #808
1 parent 5da1dda commit 18d8bbf

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/cp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ function _cp(options, sources, dest) {
260260

261261
try {
262262
common.statFollowLinks(path.dirname(dest));
263-
cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink });
263+
cpdirSyncRecursive(src, newDest, 0, { no_force: options.no_force, followsymlink: options.followsymlink, update: options.update });
264264
} catch (e) {
265265
/* istanbul ignore next */
266266
common.error("cannot create directory '" + dest + "': No such file or directory");

test/cp.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,50 @@ test('-R implies -P', t => {
451451
});
452452
});
453453

454+
test('-Ru respects the -u flag recursively (don\'t update newer file)', t => {
455+
// Setup code
456+
const TWO_DAYS_IN_MS = 2 * 24 * 60 * 60 * 1000;
457+
const dir = `${t.context.tmp}/cp-Ru`;
458+
const sourceDir = `${dir}/old`;
459+
const sourceFile = `${sourceDir}/file`;
460+
const destDir = `${dir}/new`;
461+
const destFile = `${destDir}/file`;
462+
[sourceDir, destDir].forEach(d => shell.mkdir('-p', d));
463+
shell.ShellString('Source File Contents\n').to(sourceFile);
464+
shell.ShellString('Destination File Contents\n').to(destFile);
465+
// End setup
466+
// Get the old mtime for dest
467+
const oldTime = fs.statSync(destFile).mtimeMs;
468+
// Set the source file to be older than the destination file
469+
shell.touch('-m', oldTime - TWO_DAYS_IN_MS, sourceFile);
470+
// Now, copy the old dir to the new one
471+
shell.cp('-Ru', sourceDir, destDir);
472+
// Check that dest has not been updated
473+
t.is(shell.cat(destFile).stdout, 'Destination File Contents\n');
474+
});
475+
476+
test('-Ru respects the -u flag recursively (update older file)', t => {
477+
// Setup code
478+
const TWO_DAYS_IN_MS = 2 * 24 * 60 * 60 * 1000;
479+
const dir = `${t.context.tmp}/cp-Ru`;
480+
const sourceDir = `${dir}/old`;
481+
const sourceFile = `${sourceDir}/file`;
482+
const destDir = `${dir}/new`;
483+
const destFile = `${destDir}/file`;
484+
[sourceDir, destDir].forEach(d => shell.mkdir('-p', d));
485+
shell.ShellString('Source File Contents\n').to(sourceFile);
486+
shell.ShellString('Destination File Contents\n').to(destFile);
487+
// End setup
488+
// Get the old mtime for dest
489+
const oldTime = fs.statSync(destFile).mtimeMs;
490+
// Set the destination file to be older than the source file
491+
shell.touch('-m', oldTime + TWO_DAYS_IN_MS, sourceFile);
492+
// Now, copy the old dir to the new one
493+
shell.cp('-Ru', sourceDir, destDir);
494+
// Check that dest has been updated
495+
t.is(shell.cat(sourceFile).stdout, 'Source File Contents\n');
496+
});
497+
454498
test('using -P explicitly works', t => {
455499
utils.skipOnWin(t, () => {
456500
shell.cp('-P', 'test/resources/cp/links/sym.lnk', t.context.tmp);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy