From fa4f29a52106f6a23b972d9847b1cb08357e8540 Mon Sep 17 00:00:00 2001 From: Frans Date: Sun, 13 Nov 2016 21:28:57 +0100 Subject: [PATCH 1/3] Fix: copy recursively from read only location. (fixes #98) --- src/cp.js | 7 +++++-- test/cp.js | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cp.js b/src/cp.js index 4214b0bd..d12ede3e 100644 --- a/src/cp.js +++ b/src/cp.js @@ -99,8 +99,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { // Create the directory where all our junk is moving to; read the mode of the // source directory and mirror it try { - var checkDir = common.statFollowLinks(sourceDir); - fs.mkdirSync(destDir, checkDir.mode); + fs.mkdirSync(destDir); } catch (e) { // if the directory already exists, that's okay if (e.code !== 'EEXIST') throw e; @@ -151,6 +150,10 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { } } } // for files + + // finally change the mode for the newly created directory. + var checkDir = common.statFollowLinks(sourceDir); + fs.chmod(destDir, checkDir.mode); } // cpdirSyncRecursive // Checks if cureent file was created recently diff --git a/test/cp.js b/test/cp.js index 8ada327a..428a800b 100644 --- a/test/cp.js +++ b/test/cp.js @@ -756,3 +756,27 @@ test('should not overwrite recently created files (not give error no-force mode) // Ensure First file is copied t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'test1'); }); + +// cp -R should be able to copy a readonly src (issue #98). +// On Windows, chmod acts VERY differently so skip these tests for now +test('cp -R should be able to copy a readonly src. issue #98; (Non window platforms only)', t => { + if (common.platform !== 'win') { + shell.cp('-r', 'test/resources/cp', t.context.tmp); + shell.chmod('555', `${t.context.tmp}/cp/`); + shell.chmod('555', `${t.context.tmp}/cp/dir_a`); + shell.chmod('555', `${t.context.tmp}/cp/dir_b`); + shell.chmod('555', `${t.context.tmp}/cp/a`); + + const result = shell.cp('-r', `${t.context.tmp}/cp`, `${t.context.tmp}/cp_cp`); + t.falsy(shell.error()); + t.falsy(result.stderr); + t.is(result.code, 0); + + t.is(shell.ls('-R', `${t.context.tmp}/cp`) + '', shell.ls('-R', `${t.context.tmp}/cp_cp`) + ''); + t.is(fs.statSync(`${t.context.tmp}/cp_cp`).mode & parseInt('777', 8), parseInt('555', 8)); + t.is(fs.statSync(`${t.context.tmp}/cp_cp/dir_a`).mode & parseInt('777', 8), parseInt('555', 8)); + t.is(fs.statSync(`${t.context.tmp}/cp_cp/a`).mode & parseInt('777', 8), parseInt('555', 8)); + + shell.chmod('-R', '755', t.context.tmp); + } +}); From aef3f8b3da7efaa1ad8d11b031492fed0ff06ef9 Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 3 Jul 2018 21:48:48 -0700 Subject: [PATCH 2/3] fix async bug, change test to use skipOnWin --- src/cp.js | 2 +- test/cp.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cp.js b/src/cp.js index d12ede3e..44a0b423 100644 --- a/src/cp.js +++ b/src/cp.js @@ -153,7 +153,7 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { // finally change the mode for the newly created directory. var checkDir = common.statFollowLinks(sourceDir); - fs.chmod(destDir, checkDir.mode); + fs.chmodSync(destDir, checkDir.mode); } // cpdirSyncRecursive // Checks if cureent file was created recently diff --git a/test/cp.js b/test/cp.js index 428a800b..7007917d 100644 --- a/test/cp.js +++ b/test/cp.js @@ -760,7 +760,7 @@ test('should not overwrite recently created files (not give error no-force mode) // cp -R should be able to copy a readonly src (issue #98). // On Windows, chmod acts VERY differently so skip these tests for now test('cp -R should be able to copy a readonly src. issue #98; (Non window platforms only)', t => { - if (common.platform !== 'win') { + utils.skipOnWin(t, () => { shell.cp('-r', 'test/resources/cp', t.context.tmp); shell.chmod('555', `${t.context.tmp}/cp/`); shell.chmod('555', `${t.context.tmp}/cp/dir_a`); @@ -778,5 +778,5 @@ test('cp -R should be able to copy a readonly src. issue #98; (Non window platfo t.is(fs.statSync(`${t.context.tmp}/cp_cp/a`).mode & parseInt('777', 8), parseInt('555', 8)); shell.chmod('-R', '755', t.context.tmp); - } + }); }); From bedc5387d3ed94cd14100b3ca9c9fbeb9fa51c8c Mon Sep 17 00:00:00 2001 From: Nate Fischer Date: Tue, 3 Jul 2018 21:51:52 -0700 Subject: [PATCH 3/3] add comment --- src/cp.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cp.js b/src/cp.js index 44a0b423..6218f670 100644 --- a/src/cp.js +++ b/src/cp.js @@ -151,7 +151,8 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) { } } // for files - // finally change the mode for the newly created directory. + // finally change the mode for the newly created directory (otherwise, we + // couldn't add files to a read-only directory). var checkDir = common.statFollowLinks(sourceDir); fs.chmodSync(destDir, checkDir.mode); } // cpdirSyncRecursive