Skip to content

Commit

Permalink
mv: show error when overwriting recently created file shelljs#631
Browse files Browse the repository at this point in the history
  • Loading branch information
uttpal committed Apr 11, 2017
1 parent 49ce086 commit fe8f43d
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/mv.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ common.register('mv', _mv, {
},
});

// 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);
});
}

//@
//@ ### mv([options ,] source [, source ...], dest')
//@ ### mv([options ,] source_array, dest')
Expand Down Expand Up @@ -55,7 +63,7 @@ function _mv(options, sources, dest) {
common.error('dest file already exists: ' + dest);
}

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 All @@ -70,6 +78,16 @@ function _mv(options, sources, dest) {
thisDest = path.normalize(dest + '/' + path.basename(src));
}

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 (fs.existsSync(thisDest) && options.no_force) {
common.error('dest file already exists: ' + thisDest, { continue: true });
return; // skip file
Expand Down

0 comments on commit fe8f43d

Please sign in to comment.