Skip to content

Commit bbe521b

Browse files
uttpalnfischer
authored andcommitted
Fix shelljs#631 throw error when overwriting recently created file (shelljs#702)
* cp: add error to not overwrite recently created files shelljs#631 * cp: add tests for errors not overwrite recently created files shelljs#631 * mv: show error when overwriting recently created file shelljs#631 * mv: add tests for error on recently created files shelljs#631 * mv: test remove unnecessary steps shelljs#631
1 parent d8ac4d3 commit bbe521b

File tree

5 files changed

+102
-3
lines changed

5 files changed

+102
-3
lines changed

src/cp.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,14 @@ function cpdirSyncRecursive(sourceDir, destDir, currentDepth, opts) {
153153
} // for files
154154
} // cpdirSyncRecursive
155155

156+
// Checks if cureent file was created recently
157+
function checkRecentCreated(sources, index) {
158+
var lookedSource = sources[index];
159+
return sources.slice(0, index).some(function (src) {
160+
return path.basename(src) === path.basename(lookedSource);
161+
});
162+
}
163+
156164
function cpcheckcycle(sourceDir, srcFile) {
157165
var srcFileStat = fs.lstatSync(srcFile);
158166
if (srcFileStat.isSymbolicLink()) {
@@ -227,7 +235,7 @@ function _cp(options, sources, dest) {
227235
return new common.ShellString('', '', 0);
228236
}
229237

230-
sources.forEach(function (src) {
238+
sources.forEach(function (src, srcIndex) {
231239
if (!fs.existsSync(src)) {
232240
common.error('no such file or directory: ' + src, { continue: true });
233241
return; // skip file
@@ -262,7 +270,16 @@ function _cp(options, sources, dest) {
262270
thisDest = path.normalize(dest + '/' + path.basename(src));
263271
}
264272

265-
if (fs.existsSync(thisDest) && options.no_force) {
273+
var thisDestExists = fs.existsSync(thisDest);
274+
if (thisDestExists && checkRecentCreated(sources, srcIndex)) {
275+
// cannot overwrite file created recently in current execution, but we want to continue copying other files
276+
if (!options.no_force) {
277+
common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true });
278+
}
279+
return;
280+
}
281+
282+
if (thisDestExists && options.no_force) {
266283
return; // skip file
267284
}
268285

src/mv.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ common.register('mv', _mv, {
1111
},
1212
});
1313

14+
// Checks if cureent file was created recently
15+
function checkRecentCreated(sources, index) {
16+
var lookedSource = sources[index];
17+
return sources.slice(0, index).some(function (src) {
18+
return path.basename(src) === path.basename(lookedSource);
19+
});
20+
}
21+
1422
//@
1523
//@ ### mv([options ,] source [, source ...], dest')
1624
//@ ### mv([options ,] source_array, dest')
@@ -55,7 +63,7 @@ function _mv(options, sources, dest) {
5563
common.error('dest file already exists: ' + dest);
5664
}
5765

58-
sources.forEach(function (src) {
66+
sources.forEach(function (src, srcIndex) {
5967
if (!fs.existsSync(src)) {
6068
common.error('no such file or directory: ' + src, { continue: true });
6169
return; // skip file
@@ -70,6 +78,16 @@ function _mv(options, sources, dest) {
7078
thisDest = path.normalize(dest + '/' + path.basename(src));
7179
}
7280

81+
var thisDestExists = fs.existsSync(thisDest);
82+
83+
if (thisDestExists && checkRecentCreated(sources, srcIndex)) {
84+
// cannot overwrite file created recently in current execution, but we want to continue copying other files
85+
if (!options.no_force) {
86+
common.error("will not overwrite just-created '" + thisDest + "' with '" + src + "'", { continue: true });
87+
}
88+
return;
89+
}
90+
7391
if (fs.existsSync(thisDest) && options.no_force) {
7492
common.error('dest file already exists: ' + thisDest, { continue: true });
7593
return; // skip file

test/cp.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,3 +712,39 @@ test('copy mutliple files to same location', t => {
712712
"cp: 'resources/file2' and 'resources/file2' are the same file"
713713
);
714714
});
715+
716+
test('should not overwrite recently created files', t => {
717+
const result = shell.cp('resources/file1', 'resources/cp/file1', t.context.tmp);
718+
t.truthy(shell.error());
719+
t.is(result.code, 1);
720+
721+
// Ensure First file is copied
722+
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'test1');
723+
t.is(
724+
result.stderr,
725+
`cp: will not overwrite just-created '${t.context.tmp}/file1' with 'resources/cp/file1'`
726+
);
727+
});
728+
729+
730+
test('should not overwrite recently created files (in recursive Mode)', t => {
731+
const result = shell.cp('-R', 'resources/file1', 'resources/cp/file1', t.context.tmp);
732+
t.truthy(shell.error());
733+
t.is(result.code, 1);
734+
735+
// Ensure First file is copied
736+
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'test1');
737+
t.is(
738+
result.stderr,
739+
`cp: will not overwrite just-created '${t.context.tmp}/file1' with 'resources/cp/file1'`
740+
);
741+
});
742+
743+
test('should not overwrite recently created files (not give error no-force mode)', t => {
744+
const result = shell.cp('-n', 'resources/file1', 'resources/cp/file1', t.context.tmp);
745+
t.falsy(shell.error());
746+
t.is(result.code, 0);
747+
748+
// Ensure First file is copied
749+
t.is(shell.cat(`${t.context.tmp}/file1`).toString(), 'test1');
750+
});

test/mv.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,3 +206,30 @@ test('dest exists, but -f given', t => {
206206
t.falsy(fs.existsSync('file1'));
207207
t.truthy(fs.existsSync('file2'));
208208
});
209+
210+
test('should not overwrite recently created files', t => {
211+
shell.mkdir('-p', 't');
212+
const result = shell.mv('file1', 'cp/file1', 't/');
213+
t.truthy(shell.error());
214+
t.is(result.code, 1);
215+
216+
// Ensure First file is copied
217+
t.is(shell.cat('t/file1').toString(), 'test1');
218+
t.is(
219+
result.stderr,
220+
"mv: will not overwrite just-created 't/file1' with 'cp/file1'"
221+
);
222+
t.truthy(fs.existsSync('cp/file1'));
223+
});
224+
225+
226+
test('should not overwrite recently created files (not give error no-force mode)', t => {
227+
shell.mkdir('-p', 't');
228+
const result = shell.mv('-n', 'file1', 'cp/file1', 't/');
229+
t.falsy(shell.error());
230+
t.is(result.code, 0);
231+
232+
// Ensure First file is moved
233+
t.is(shell.cat('t/file1').toString(), 'test1');
234+
t.truthy(fs.existsSync('cp/file1'));
235+
});

test/resources/cp/file1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
test2

0 commit comments

Comments
 (0)