Skip to content

Commit

Permalink
tar: properly handle tar spec in _prepNumeric. ref #45.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctalkington committed Sep 23, 2013
1 parent 484f3b4 commit 4f88aae
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/headers/tar.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ var MAXNUM = {
HeaderTarFile.prototype._prepNumeric = function(num, len) {
num = num || 0;

var wLen = len - 1;
var max = MAXNUM[len] || 0;

if (num > max || num < 0) {
Expand All @@ -165,15 +166,17 @@ HeaderTarFile.prototype._prepNumeric = function(num, len) {

var str = Math.floor(num).toString(8);

if (num < MAXNUM[len - 1]) {
// tar spec calls for field length - 1 + null (\0)
// but if short enough should have a space before null (\0)
if (num < MAXNUM[wLen - 1]) {
str += ' ';
}

if (str.length < len) {
str = util.repeat('0', len - str.length) + str;
if (str.length < wLen) {
str = util.repeat('0', wLen - str.length) + str;
}

return str;
return str.substr(0, len - 1) + '\0';
};

HeaderTarFile.prototype._parseNumeric = function(str) {
Expand Down
10 changes: 5 additions & 5 deletions test/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('archiver', function() {
var testStream = new WriteHashStream('tmp/buffer.tar');

testStream.on('close', function() {
assert.equal(testStream.digest, '3a8a9ff17087129cc05dc88be86573ecf3f9ca56');
assert.equal(testStream.digest, 'bc84fec33e7a4f6c8777cabd0beba503a7bce331');
done();
});

Expand All @@ -81,7 +81,7 @@ describe('archiver', function() {
var testStream = new WriteHashStream('tmp/stream.tar');

testStream.on('close', function() {
assert.equal(testStream.digest, '3e3271c503deeba9abe5031df6bca636f1eb115b');
assert.equal(testStream.digest, 'b3bf662968c87989431a25b2f699eae213392e82');
done();
});

Expand All @@ -97,7 +97,7 @@ describe('archiver', function() {
var testStream = new WriteHashStream('tmp/multiple.tar');

testStream.on('close', function() {
assert.equal(testStream.digest, '1437ef391a7c2c0de180bddff21b98c2f9778331');
assert.equal(testStream.digest, '0c4e2a79d0d2c41ae5eb2e1e70d315a617583e4d');
done();
});

Expand All @@ -115,7 +115,7 @@ describe('archiver', function() {
var testStream = new WriteHashStream('tmp/feature-prefix.tar');

testStream.on('close', function() {
assert.equal(testStream.digest, 'aeb2496b2bd7458931ba942dba2b4bf28cbc187c');
assert.equal(testStream.digest, 'c1efbfbdc9a49979a6e02b4009003de533fcda48');
done();
});

Expand All @@ -136,7 +136,7 @@ describe('archiver', function() {
var testStream = new WriteHashStream('tmp/zerolength.tar');

testStream.on('close', function() {
assert.equal(testStream.digest, '4f9f572817a1f34d0d33b949c959739965e50fa6');
assert.equal(testStream.digest, 'f4f7b53f8ee4c7124298695bffbacfa9e9c0a99f');
done();
});

Expand Down
Binary file modified test/fixtures/headers/tar-file.bin
Binary file not shown.
Binary file modified test/fixtures/headers/tar-fileprefix.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe('headers', function() {
assert.equal(actual.size, 23);
assert.deepEqual(actual.date, testDate);
assert.equal(actual.mtime, testDateEpoch);
assert.equal(actual.checksum, 5730);
assert.equal(actual.checksum, 5490);
assert.equal(actual.type, '0');
assert.equal(actual.linkName, '');
});
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('headers', function() {

describe('#_prepNumeric(num, len)', function() {
it('should convert numeric values to octal strings, padding when needed', function() {
assert.equal(thing._prepNumeric(17, 7), '0000021');
assert.equal(thing._prepNumeric(17, 7), '000021\0');
});
});

Expand Down

0 comments on commit 4f88aae

Please sign in to comment.