Skip to content

Commit

Permalink
fix: should ignore broken symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
blond committed May 18, 2016
1 parent c854cdc commit f851122
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/copy-file-stream.js
Expand Up @@ -20,7 +20,13 @@ module.exports = class CopyFileStream extends stream.Writable {

fs.stat(filename, (err, stats) => {
if (err) {
return this.emit('error', err);
return fs.lstat(filename, lerr => {
// file not found
if (lerr) { return callback(err); }

// ignore broken symlink
callback();
});
}

if (stats.isDirectory() || !this._emptyFiles && stats.size === 0) {
Expand Down
8 changes: 7 additions & 1 deletion lib/tar-stream.js
Expand Up @@ -39,7 +39,13 @@ module.exports = class TarStream extends stream.Writable {

fs.stat(filename, (err, stats) => {
if (err) {
return this.emit('error', err);
return fs.lstat(filename, lerr => {
// file not found
if (lerr) { return callback(err); }

// ignore broken symlink
callback();
});
}

if (stats.isDirectory() || !this._emptyFiles && stats.size === 0) {
Expand Down
18 changes: 18 additions & 0 deletions test/copy-file-stream.test.js
Expand Up @@ -117,6 +117,24 @@ test('should copy source file of symlink', t => {
});
});

test('should ignore broken symlinks', t => {
mockFs({
'source-dir': {
'file-1.txt': 'Hi!',
'symlink.txt': mockFs.symlink({
path: 'no-file'
})
}
});

return copyFiles(['file-1.txt', 'symlink.txt'])
.then(() => {
const files = fs.readdirSync(path.join(dest, 'source-dir'));

t.deepEqual(files, ['file-1.txt']);
});
});

test('should ignore empty file', t => {
mockFs({
'source-dir': {
Expand Down
17 changes: 17 additions & 0 deletions test/tar-stream.test.js
Expand Up @@ -101,6 +101,23 @@ test('should take into account symlink', t => {
});
});

test('should ignore broken symlinks', t => {
mockFs({
'source-dir': {
'file-1.txt': 'Hi!',
'symlink.txt': mockFs.symlink({
path: '../no-file'
})
}
});

return packFiles(['file-1.txt', 'symlink.txt'])
.then(() => extractFiles())
.then(files => {
t.deepEqual(files, ['file-1.txt']);
});
});

test('should ignore empty file', t => {
mockFs({
'source-dir': {
Expand Down

0 comments on commit f851122

Please sign in to comment.