Skip to content

Commit

Permalink
cp: add error to not overwrite recently created files shelljs#631
Browse files Browse the repository at this point in the history
  • Loading branch information
uttpal committed Apr 11, 2017
1 parent 80d590e commit 7f1b998
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/cp.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) {
} // for files
} // cpdirSyncRecursive

// Checks if cureent file was created recently
function checkRecentCreated(sources, index) {
var lookedSource = sources[index];
return sources.slice(0, index).some(function (src) {
return path.basename(src) === path.basename(lookedSource);
});
}

function cpcheckcycle(sourceDir, srcFile) {
var srcFileStat = fs.lstatSync(srcFile);
if (srcFileStat.isSymbolicLink()) {
Expand Down Expand Up @@ -224,7 +232,7 @@ function _cp(options, sources, dest) {
return new common.ShellString('', '', 0);
}

sources.forEach(function (src) {
sources.forEach(function (src, srcIndex) {
if (!fs.existsSync(src)) {
common.error('no such file or directory: ' + src, { continue: true });
return; // skip file
Expand Down Expand Up @@ -259,7 +267,16 @@ function _cp(options, sources, dest) {
thisDest = path.normalize(dest + '/' + path.basename(src));
}

if (fs.existsSync(thisDest) && options.no_force) {
var thisDestExists = fs.existsSync(thisDest);
if (thisDestExists && checkRecentCreated(sources, srcIndex)) {
// cannot overwrite file created recently in current execution, but we want to continue copying other files
if (!options.no_force) {
common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true });
}
return;
}

if (thisDestExists && options.no_force) {
return; // skip file
}

Expand Down

0 comments on commit 7f1b998

Please sign in to comment.