From a311be903fb14732c8c722fb7bd9907d0ea0a0f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sun, 11 Oct 2015 18:24:53 +0200 Subject: [PATCH 1/4] Minor clean-up --- headers.js | 6 ++---- pack.js | 12 +++++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/headers.js b/headers.js index 19c3da3..cab8803 100644 --- a/headers.js +++ b/headers.js @@ -190,15 +190,13 @@ exports.encode = function(opts) { } exports.decode = function(buf) { - var typeflag = buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET - var type = toType(typeflag) - var name = decodeStr(buf, 0, 100) var mode = decodeOct(buf, 100) var uid = decodeOct(buf, 108) var gid = decodeOct(buf, 116) var size = decodeOct(buf, 124) var mtime = decodeOct(buf, 136) + var type = toType(buf[156] === 0 ? 0 : buf[156] - ZERO_OFFSET) var linkname = buf[157] === 0 ? null : decodeStr(buf, 157, 100) var uname = decodeStr(buf, 265, 32) var gname = decodeStr(buf, 297, 32) @@ -222,7 +220,7 @@ exports.decode = function(buf) { gid: gid, size: size, mtime: new Date(1000 * mtime), - type: toType(typeflag), + type: type, linkname: linkname, uname: uname, gname: gname, diff --git a/pack.js b/pack.js index eec3496..1de304c 100644 --- a/pack.js +++ b/pack.js @@ -1,10 +1,12 @@ -var util = require('util') -var eos = require('end-of-stream') +var eos = require('end-of-stream') +var util = require('util') + +var Readable = require('readable-stream').Readable +var PassThrough = require('readable-stream').PassThrough +var Writable = require('readable-stream').Writable + var headers = require('./headers') -var Readable = require('readable-stream').Readable -var Writable = require('readable-stream').Writable -var PassThrough = require('readable-stream').PassThrough var END_OF_TAR = new Buffer(1024) END_OF_TAR.fill(0) From b2f57d1b248895d64d19c847fbe68854d9344d56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sun, 11 Oct 2015 18:27:35 +0200 Subject: [PATCH 2/4] Use header 'mode' to detect file type if 'type' not explicitly set --- pack.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pack.js b/pack.js index 1de304c..c9aade5 100644 --- a/pack.js +++ b/pack.js @@ -1,3 +1,4 @@ +var constants = require('constants') var eos = require('end-of-stream') var util = require('util') @@ -18,6 +19,19 @@ var overflow = function(self, size) { if (size) self.push(END_OF_TAR.slice(0, 512 - size)) } +function mode2type(mode) +{ + switch (mode & constants.S_IFMT) { + case constants.S_IFBLK: return 'block-device' + case constants.S_IFCHR: return 'character-device' + case constants.S_IFDIR: return 'directory' + case constants.S_IFIFO: return 'fifo' + case constants.S_IFLNK: return 'symlink' + } + + return 'file' +} + var Sink = function(to) { Writable.call(this) this.written = 0 @@ -83,7 +97,7 @@ Pack.prototype.entry = function(header, buffer, callback) { var self = this if (!header.size) header.size = 0 - if (!header.type) header.type = 'file' + if (!header.type) header.type = mode2type(header.mode) if (!header.mode) header.mode = header.type === 'directory' ? 0755 : 0644 if (!header.uid) header.uid = 0 if (!header.gid) header.gid = 0 From b32e9b6b39c15889d31d4d328e1b66cdf944ed27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Sun, 11 Oct 2015 19:28:28 +0200 Subject: [PATCH 3/4] Allow to stream symlink location if 'linkname' header field is not defined --- pack.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/pack.js b/pack.js index c9aade5..9b46127 100644 --- a/pack.js +++ b/pack.js @@ -112,15 +112,40 @@ Pack.prototype.entry = function(header, buffer, callback) { process.nextTick(callback) return new Void() } + + if (header.type === 'symlink' && !header.linkname) { + var stream = new Writable + var linkname = '' + + stream._write = function(data, enc, cb) + { + linkname += data + cb() + } + eos(stream, function(err) + { + if (err) { // stream was closed + self.destroy() + return callback(err) + } + + header.linkname = linkname + self._encode(header) + callback() + }) + + return stream + } + + this._encode(header) + if (header.type !== 'file' && header.type !== 'contigious-file') { - this._encode(header) process.nextTick(callback) return new Void() } var sink = new Sink(this) - this._encode(header) this._stream = sink eos(sink, function(err) { From 683a518448028c09d0a89fdc655c439edc318273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s=20Combarro=20=22piranna?= Date: Thu, 15 Oct 2015 23:20:00 +0200 Subject: [PATCH 4/4] Removed require() alignment and added Egyptian casing ;-) --- pack.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pack.js b/pack.js index 9b46127..463448f 100644 --- a/pack.js +++ b/pack.js @@ -1,10 +1,10 @@ var constants = require('constants') -var eos = require('end-of-stream') -var util = require('util') +var eos = require('end-of-stream') +var util = require('util') -var Readable = require('readable-stream').Readable +var Readable = require('readable-stream').Readable var PassThrough = require('readable-stream').PassThrough -var Writable = require('readable-stream').Writable +var Writable = require('readable-stream').Writable var headers = require('./headers') @@ -19,8 +19,7 @@ var overflow = function(self, size) { if (size) self.push(END_OF_TAR.slice(0, 512 - size)) } -function mode2type(mode) -{ +function mode2type(mode) { switch (mode & constants.S_IFMT) { case constants.S_IFBLK: return 'block-device' case constants.S_IFCHR: return 'character-device' @@ -117,13 +116,11 @@ Pack.prototype.entry = function(header, buffer, callback) { var stream = new Writable var linkname = '' - stream._write = function(data, enc, cb) - { + stream._write = function(data, enc, cb) { linkname += data cb() } - eos(stream, function(err) - { + eos(stream, function(err) { if (err) { // stream was closed self.destroy() return callback(err)