tar-async icon indicating copy to clipboard operation
tar-async copied to clipboard

binary stability

Open paleozogt opened this issue 4 years ago • 2 comments

With command-line tar, multiple invocations produce exactly the same file, byte-for-byte. For example:

➜  echo 'Woohoo!! Tar me up Scotty!' > test.txt
➜  tar cvf test1.tar test.txt
test.txt
➜  tar cvf test2.tar test.txt
test.txt
➜  openssl sha256 test1.tar
SHA256(test1.tar)= 559da93465d3c535728b9b4311d012a3e9267e70b08b184d0e83fdb9d947edc2
➜  openssl sha256 test2.tar
SHA256(test2.tar)= 559da93465d3c535728b9b4311d012a3e9267e70b08b184d0e83fdb9d947edc2

With tar-async, this isn't true. Pasting the example from the main page into index.js,

// index.js
var Tar = require('tar-async'), // or require('tar-async').Tar or require('tar-async/tar')
	tape = new Tar({output: require('fs').createWriteStream('out.tar')});

tape.append('test.txt', 'Woohoo!! Tar me up Scotty!', function () {
	tape.close();
});

we then invoke it twice:

➜  node index.js
➜  mv out.tar out1.tar

➜  node index.js
➜  mv out.tar out2.tar

➜  openssl sha256 out1.tar
SHA256(out1.tar)= 9f54914b2e317b6a9762581217bafe9ebbbb7ba2818362731730da560153b3d6
➜  openssl sha256 out2.tar
SHA256(out2.tar)= c3e2eac2e83611ad1c5d809d0ec1e305a2cc9cab063bd3041bfad3252d1f50fd

Note that the generated files are subtly different. The difference isn't in the file contents but something about the tar header.

What is tar-async doing that makes it produce different output each time?

paleozogt avatar Oct 07 '20 20:10 paleozogt

It's using the current timestamp rather than the file timestamp?

https://github.com/beatgammit/tar-async/blob/master/lib/tar.js#L173

coolaj86 avatar Oct 08 '20 16:10 coolaj86

Ah, I didn't realize it worked that way. Looks like we can inject our own mtime to keep the output constant:

var Tar = require('tar-async'), // or require('tar-async').Tar or require('tar-async/tar')
        tape = new Tar({output: require('fs').createWriteStream('out.tar')});

tape.append('test.txt', 'Woohoo!! Tar me up Scotty!', {mtime:0}, function () {
        tape.close();
});

Sorry for the false-alarm!

paleozogt avatar Oct 08 '20 17:10 paleozogt