This repository has been archived by the owner on Aug 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 105
/
tar.js
170 lines (146 loc) · 4.67 KB
/
tar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
var logger = require('./logger'),
env = require('./env'),
Packer = require('./fstream-jam'),
fstream = require('fstream'),
tar = require('tar'),
zlib = require('zlib'),
path = require('path'),
fs = require('fs');
var umask = parseInt(022, 8);
var modes = {
exec: 0777 & (~umask),
file: 0666 & (~umask),
umask: umask
};
var myUid = process.getuid && process.getuid(),
myGid = process.getgid && process.getgid();
if (process.env.SUDO_UID && myUid === 0) {
if (!isNaN(process.env.SUDO_UID)) {
myUid = +process.env.SUDO_UID;
}
if (!isNaN(process.env.SUDO_GID)) {
myGid = +process.env.SUDO_GID;
}
}
/**
*/
exports.create = function(cfg, source, target, callback) {
logger.info('creating', target);
function returnError(err) {
// don't call the callback multiple times, just return the first error
var _callback = callback;
callback = function () {};
return _callback(err);
}
var fwriter = fstream.Writer({ type: 'File', path: target });
fwriter.on('error', function (err) {
logger.error('error writing ' + target);
//logger.error(err);
return returnError(err);
});
fwriter.on('close', function () {
callback(null, target);
});
var istream = Packer({
packageInfo: cfg,
path: source,
type: "Directory",
isDirectory: true
});
istream.on('error', function (err) {
logger.error('error reading ' + source);
//logger.error(err);
return returnError(err);
});
istream.on("child", function (c) {
//var root = path.resolve(c.root.path, '../package');
//logger.info('adding', c.path.substr(root.length + 1));
});
var packer = tar.Pack({ noProprietary: true });
packer.on('error', function (err) {
logger.error('tar creation error ' + target);
//logger.error(err);
return returnError(err);
});
var zipper = zlib.Gzip();
zipper.on('error', function (err) {
logger.error('gzip error ' + target);
//logger.error(err);
return returnError(err);
});
istream.pipe(packer).pipe(zipper).pipe(fwriter);
};
/**
*/
exports.extract = function (source, target, callback) {
logger.info('extracting', source);
var umask = modes.umask;
var dmode = modes.dmode;
var fmode = modes.fmode;
function returnError(err) {
// don't call the callback multiple times, just return the first error
var _callback = callback;
callback = function () {};
return _callback(err);
}
var freader = fs.createReadStream(source);
freader.on('error', function (err) {
logger.error('error reading ' + source);
//logger.error(err);
return returnError(err);
});
var extract_opts = {
type: 'Directory',
path: target,
strip: 1,
filter: function () {
// symbolic links are not allowed in packages
if (this.type.match(/^.*Link$/)) {
logger.warning(
'excluding symbolic link',
this.path.substr(target.length + 1) + ' -> ' + this.linkpath
);
return false;
}
return true;
}
};
if (!env.isWindows && typeof myUid === "number" && typeof myGid === "number") {
extract_opts.uid = myUid;
extract_opts.gid = myGid;
}
var extractor = tar.Extract(extract_opts);
extractor.on('error', function (err) {
logger.error('untar error ' + source);
//logger.error(err);
return returnError(err);
});
extractor.on('entry', function (entry) {
//logger.info('extracting', entry.path);
entry.mode = entry.mode || entry.props.mode;
var original_mode = entry.mode;
entry.mode = entry.mode | (entry.type === "Directory" ? dmode: fmode);
entry.mode = entry.mode & (~umask);
if (original_mode !== entry.mode) {
logger.info(
'modified mode',
original_mode + ' => ' + entry.mode + ' ' + entry.path
);
}
if (process.platform !== "win32" &&
typeof myUid === "number" && typeof myGid === "number") {
entry.props.uid = entry.uid = myUid;
entry.props.gid = entry.gid = myGid;
}
});
extractor.on('end', function () {
return callback(null, target);
});
var unzipper = zlib.Unzip();
unzipper.on('error', function (err) {
logger.error('unzip error ' + source);
//logger.error(err);
return returnError(err);
});
freader.pipe(unzipper).pipe(extractor);
};