Skip to content

Commit

Permalink
Patch up race condition in symlink copying.
Browse files Browse the repository at this point in the history
  • Loading branch information
vladikoff committed May 10, 2022
1 parent ac667b2 commit 58016ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v1.5.3
date: 2022-04-23
changes:
- Patch up race condition in symlink copying.
v1.5.2
date: 2022-04-12
changes:
Expand Down
9 changes: 2 additions & 7 deletions lib/grunt/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,6 @@ file.write = function(filepath, contents, options) {
// processing content, writing output.
// Handles symlinks by coping them as files or directories.
file.copy = function copy(srcpath, destpath, options) {
if (file.isLink(destpath)) {
// in case destpath is a symlink, avoid following the symlink, instead overwrite it later
fs.unlinkSync(destpath);
}

if (file.isLink(srcpath)) {
file._copySymbolicLink(srcpath, destpath);
} else if (file.isDir(srcpath)) {
Expand Down Expand Up @@ -338,8 +333,8 @@ file._copy = function(srcpath, destpath, options) {
}
}
// Abort copy if the process function returns false.
if (contents === false) {
grunt.verbose.writeln('Write aborted.');
if (contents === false || file.isLink(destpath)) {
grunt.verbose.writeln('Write aborted. Either the process function returned false or the destination is a symlink');
} else {
file.write(destpath, contents, readWriteOptions);
}
Expand Down
10 changes: 9 additions & 1 deletion test/grunt/file_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,5 +916,13 @@ exports.file = {
test.ok(fs.lstatSync(path.join(destdir.path, path.basename(fixtures))).isSymbolicLink());
test.done();
},
}
},
'symbolicLinkDestError': function(test) {
test.expect(1);
var tmpfile = new Tempdir();
fs.symlinkSync(path.resolve('test/fixtures/octocat.png'), path.join(tmpfile.path, 'octocat.png'), 'file');
grunt.file.copy(path.resolve('test/fixtures/octocat.png'), path.join(tmpfile.path, 'octocat.png'));
test.ok(fs.lstatSync(path.join(tmpfile.path, 'octocat.png')).isSymbolicLink());
test.done();
},
};

0 comments on commit 58016ff

Please sign in to comment.