Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support directory symlinks #468

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ Archiver.prototype._moduleAppend = function(source, data, callback) {
return;
}

if (data.type === 'symlink' && data.name.slice(-1) === "/") {
data.name = data.name.slice(0, -1);
}

/**
* Fires when the entry's input has been processed and appended to the archive.
*
Expand Down Expand Up @@ -867,6 +871,14 @@ Archiver.prototype.symlink = function(filepath, target) {
return this;
}

var filepathHasTrailingSlash = filepath.slice(-1) === '/';
var targetHasTrailingSlash = target.slice(-1) === '/';

if ((filepathHasTrailingSlash && !targetHasTrailingSlash) || (!filepathHasTrailingSlash && targetHasTrailingSlash)) {
this.emit('error', new ArchiverError("SYMLINKDIRECTORYSUFFIXESMISMATCHED", { filepath: filepath, target: target }))
return this;
}

if (!this._moduleSupports('symlink')) {
this.emit('error', new ArchiverError('SYMLINKNOTSUPPORTED', { filepath: filepath }));
return this;
Expand Down
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const ERROR_CODES = {
'SYMLINKNOTSUPPORTED': 'support for symlink entries not defined by module',
'SYMLINKFILEPATHREQUIRED': 'symlink filepath argument must be a non-empty string value',
'SYMLINKTARGETREQUIRED': 'symlink target argument must be a non-empty string value',
'SYMLINKDIRECTORYSUFFIXESMISMATCHED': 'directory symlink arguments must both have / suffixes',
'ENTRYNOTSUPPORTED': 'entry not supported'
};

Expand Down
36 changes: 36 additions & 0 deletions test/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,42 @@ describe('archiver', function() {
});
});

describe('#symlink', function() {
var actual;
var archive;
var entries = {};

before(function(done) {
archive = archiver('json');
var testStream = new WriteStream('tmp/symlink.json');

testStream.on('close', function() {
actual = helpers.readJSON('tmp/symlink.json');

actual.forEach(function(entry) {
entries[entry.name] = entry;
});

done();
});

archive.pipe(testStream);

archive
.append("file-a", { name: "file-a" })
.symlink("directory-a/symlink-to-file-a", "../file-a")
.symlink("directory-b/directory-c/symlink-to-directory-a/", "../../directory-a/")
.finalize();
});

it('should append multiple entries', () => {
assert.isArray(actual);
assert.property(entries, 'file-a');
assert.property(entries, 'directory-a/symlink-to-file-a');
assert.property(entries, 'directory-b/directory-c/symlink-to-directory-a');
});
});

describe('#glob', function() {
var actual;
var archive;
Expand Down
7 changes: 7 additions & 0 deletions test/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ describe('plugins', function() {
.file('test/fixtures/executable.sh', { name: 'executable.sh', mode: win32 ? 0777 : null })
.directory('test/fixtures/directory', 'directory')
.symlink('manual-link.txt', 'manual-link-target.txt')
.symlink('dir-symlink-test/nested/symlink-to-subsub/', '../../directory/subdir/subsub/')
.finalize();
});

Expand Down Expand Up @@ -205,6 +206,12 @@ describe('plugins', function() {
assert.propertyVal(entries['manual-link.txt'], 'externalFileAttributes', 2684354592);
});

it('should append manual directory symlink', function() {
assert.property(entries, 'dir-symlink-test/nested/symlink-to-subsub');
assert.propertyVal(entries['dir-symlink-test/nested/symlink-to-subsub'], 'externalFileAttributes', 2733834272);
assert.equal((entries['dir-symlink-test/nested/symlink-to-subsub'].externalFileAttributes >>> 16) & 0xFFF, 755);
});

it('should allow for custom unix mode', function() {
assert.property(entries, 'executable.sh');
assert.propertyVal(entries['executable.sh'], 'externalFileAttributes', 2180972576);
Expand Down