forked from cthackers/adm-zip
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
199 lines (170 loc) · 6.97 KB
/
utils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
var fs = require("fs"),
pth = require('path');
fs.existsSync = fs.existsSync || pth.existsSync;
module.exports = (function() {
var crcTable = [],
Constants = require('./constants'),
Errors = require('./errors'),
PATH_SEPARATOR = pth.normalize("/");
function mkdirSync(/*String*/path) {
var resolvedPath = path.split(PATH_SEPARATOR)[0];
path.split(PATH_SEPARATOR).forEach(function(name) {
if (!name || name.substr(-1,1) == ":") return;
resolvedPath += PATH_SEPARATOR + name;
var stat;
try {
stat = fs.statSync(resolvedPath);
} catch (e) {
fs.mkdirSync(resolvedPath);
}
if (stat && stat.isFile())
throw Errors.FILE_IN_THE_WAY.replace("%s", resolvedPath);
});
}
function findSync(/*String*/root, /*RegExp*/pattern, /*Boolean*/recoursive) {
if (typeof pattern === 'boolean') {
recoursive = pattern;
pattern = undefined;
}
var files = [];
fs.readdirSync(root).forEach(function(file) {
var path = pth.join(root, file);
if (fs.statSync(path).isDirectory() && recoursive)
files = files.concat(findSync(path, pattern, recoursive));
if (!pattern || pattern.test(path)) {
files.push(pth.normalize(path) + (fs.statSync(path).isDirectory() ? PATH_SEPARATOR : ""));
}
});
return files;
}
return {
makeDir : function(/*String*/path) {
mkdirSync(path);
},
crc32 : function(buf) {
var b = new Buffer(4);
if (!crcTable.length) {
for (var n = 0; n < 256; n++) {
var c = n;
for (var k = 8; --k >= 0;) //
if ((c & 1) != 0) { c = 0xedb88320 ^ (c >>> 1); } else { c = c >>> 1; }
if (c < 0) {
b.writeInt32LE(c, 0);
c = b.readUInt32LE(0);
}
crcTable[n] = c;
}
}
var crc = 0, off = 0, len = buf.length, c1 = ~crc;
while(--len >= 0) c1 = crcTable[(c1 ^ buf[off++].charCodeAt()) & 0xff] ^ (c1 >>> 8);
crc = ~c1;
b.writeInt32LE(crc & 0xffffffff, 0);
return b.readUInt32LE(0);
},
methodToString : function(/*Number*/method) {
switch (method) {
case Constants.STORED:
return 'STORED (' + method + ')';
case Constants.DEFLATED:
return 'DEFLATED (' + method + ')';
default:
return 'UNSUPPORTED (' + method + ')';
}
},
writeFileTo : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr) {
if (fs.existsSync(path)) {
if (!overwrite)
return false; // cannot overwite
var stat = fs.statSync(path);
if (stat.isDirectory()) {
return false;
}
}
var folder = pth.dirname(path);
if (!fs.existsSync(folder)) {
mkdirSync(folder);
}
var fd;
try {
fd = fs.openSync(path, 'w', 438); // 0666
} catch(e) {
fs.chmodSync(path, 438);
fd = fs.openSync(path, 'w', 438);
}
if (fd) {
fs.writeSync(fd, content, 0, content.length, 0);
fs.closeSync(fd);
}
fs.chmodSync(path, attr || 438);
return true;
},
writeFileToAsync : function(/*String*/path, /*Buffer*/content, /*Boolean*/overwrite, /*Number*/attr, /*Function*/callback) {
if(typeof attr === 'function') {
callback = attr;
attr = undefined;
}
fs.exists(path, function(exists) {
if(exists && !overwrite)
return callback(false);
fs.stat(path, function(err, stat) {
if(exists &&stat.isDirectory()) {
return callback(false);
}
var folder = pth.dirname(path);
fs.exists(folder, function(exists) {
if(!exists)
mkdirSync(folder);
fs.open(path, 'w', 438, function(err, fd) {
if(err) {
fs.chmod(path, 438, function(err) {
fs.open(path, 'w', 438, function(err, fd) {
fs.write(fd, content, 0, content.length, 0, function(err, written, buffer) {
fs.close(fd, function(err) {
fs.chmod(path, attr || 438, function() {
callback(true);
})
});
});
});
})
} else {
if(fd) {
fs.write(fd, content, 0, content.length, 0, function(err, written, buffer) {
fs.close(fd, function(err) {
fs.chmod(path, attr || 438, function() {
callback(true);
})
});
});
} else {
fs.chmod(path, attr || 438, function() {
callback(true);
})
}
}
});
})
})
})
},
findFiles : function(/*String*/path) {
return findSync(path, true);
},
getAttributes : function(/*String*/path) {
},
setAttributes : function(/*String*/path) {
},
toBuffer : function(input) {
if (Buffer.isBuffer(input)) {
return input;
} else {
if (input.length == 0) {
return new Buffer(0)
}
return new Buffer(input, 'utf8');
}
},
Constants : Constants,
Errors : Errors
}
})();