This repository has been archived by the owner on Jun 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
fs-tools.js
732 lines (626 loc) · 17.7 KB
/
fs-tools.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
* FsTools
*
* Collection of FS related tools, that stdlib lack of.
**/
'use strict';
// stdlib
var fs = require('fs');
var crypto = require('crypto');
var os = require('os');
var _pth = require('path');
// Node < 0.8 shims
fs.exists = fs.exists || require('path').exists;
fs.existsSync = fs.existsSync || require('path').existsSync;
_pth.sep = _pth.sep || (_pth.resolve('.')[0] === '/' ? '/' : '\\');
var path_join = _pth.join;
var path_normalize = _pth.normalize;
var dirname = _pth.dirname;
// 3rd-party
var async = require('async');
var _ = require('lodash');
// shortcuts
var path_join = _pth.join;
var path_normalize = _pth.normalize;
var dirname = _pth.dirname;
// epxorts: walk, mkdir, copy, remove
var fstools = module.exports = {};
// INTERNAL HELPERS
////////////////////////////////////////////////////////////////////////////////
// walk_flat(path, iterator, callback) -> void
// - path (String): Path to iterate through
// - iterator (Function): Will be fired on each element within `path`
// - callback (Function): Will be fired once all files were processed
//
// Walks through given `path` and calls `iterator(path, callback)` for
// each found entry (regardless to it's type) and waits for all `callback`s
// (passed to iterator) to be fired. After all callbacks were fired, fires
// `callback` (given to walk_flat itself).
//
// NOTICE: It walks through single dimension of file system - it won't go into
// found sub-directories. See `walk_deep` for this puprpose.
//
// Example:
//
// walk_flat('/home/nodeca', function (path, callback) {
// if ('/home/nodeca/secrets.yml' === path) {
// callback(Error("There is secrets file."));
// return;
// }
//
// if ('/home/nodeca/xxx' === path) {
// callback(Error("Path contains some porno?"));
// return;
// }
//
// callback();
// }, function (err) {
// if (err) console.error(err);
// console.log('Done');
// });
//
function walk_flat(path, iterator, callback) {
fs.readdir(path, function (err, files) {
if (err) {
if ('ENOENT' === err.code) {
callback(null);
return;
}
callback(err);
return;
}
async.forEach(files, function (file, next) {
iterator(path_join(path, file), next);
}, callback);
});
}
// walk_deep(path, match, iterator, callback) -> void
// - path (String): Path to iterate through
// - match (Function): Path test whenever iterator should be executed or not
// - iterator (Function): Will be fired on each element within `path`
// - callback (Function): Will be fired once all files were processed
//
// Walks through given `path` with all it's nested childs calling
// `iterator(path, callback)` for each found file.
//
function walk_deep(path, match, iterator, callback) {
walk_flat(path_normalize(path), function (path, next) {
fs.lstat(path, function (err, stats) {
if (err) {
next(err);
return;
}
if (stats.isDirectory()) {
walk_deep(path, match, iterator, next);
return;
}
if (match(path)) {
iterator(path, stats, next);
return;
}
next();
});
}, callback);
}
function copy_file(src, dst, callback) {
var ifd, ofd
, done = _.once(callback);
// create streams
ifd = fs.createReadStream(src, { bufferSize: 64 * 1024 }).on('error', done);
ofd = fs.createWriteStream(dst).on('error', done).on('close', done);
// pipe src to dst
ifd.pipe(ofd);
}
// PUBLIC API
////////////////////////////////////////////////////////////////////////////////
/**
* FsTools.walk(path, pattern, iterator, callback) -> void
* FsTools.walk(path, iterator, callback) -> void
*
* Walks throught all files withing `path` (including sub-dirs) and calls
* `iterator` on each found file (or block device etc.) matching `pattern`.
* If no `pattern` was given - will fire call `iterator` for every single
* path found. After all iterations will call `callback` (if it was specified)
* with passing `error` as first arguemtn if there was an error.
*
* If `path` points a file, iterator will be called against it (respecting
* pattern if it was given).
*
*
* ##### Iterator
*
* All iterations are running within promise. So `callback` given to the `walk`
* will fire only after all `iterator` callbacks willnotify they finished their
* work:
*
* var iterator = function (path, stats, callback) {
* // ... do something
* if (err) {
* // ... if error occured we can "stop" walker
* callback(err);
* return;
* }
* // ... if everything is good and finished notify walker we're done
* callback();
* };
*
* Iterator is called with following arguments:
*
* - `path` (String): Full path of the found element (e.g. `/foo/bar.txt`)
* - `stats` (fs.Stats): Stats object of found path
* - `callback` (Function): Callback function to call after path processing
*
*
* ##### Example
*
* fstools.walk('/home/nodeca', function (path, stats, callback) {
* if (stats.isBlockDevice()) {
* callback(Error("WTF? Block devices are not expetcted in my room"));
* return;
* }
*
* if (stats.isSocket()) {
* console.log("Finally I found my socket");
* }
*
* callback();
* }, function (err) {
* if (err) {
* // shit happens!
* console.error(err);
* process.exit(1);
* }
*
* console.log("Hooray! We're done!");
* });
*
*
* ##### Example (using pattern matching)
*
* fstools.walk('/home/nodeca', '\.yml$', function (path, stats, callback) {
* fs.readFile(path, 'utf-8', funtion (err, str) {
* if (err) {
* callback(err);
* return;
* }
*
* console.log(str);
* callback();
* });
* }, function (err) {
* if (err) {
* console.error(err);
* }
*
* console.log('Done!');
* });
**/
fstools.walk = function (path, pattern, iterator, callback) {
var match;
if (!callback) {
pattern = null;
iterator = arguments[1];
callback = arguments[2];
}
if (!pattern) {
match = function () { return true; };
} else if (_.isFunction(pattern) && !_.isRegExp(pattern)) {
match = pattern;
} else {
pattern = new RegExp(pattern);
match = function (path) { return pattern.test(path); };
}
path = path_normalize(path);
fs.lstat(path, function (err, stats) {
if (err) {
callback('ENOENT' === err.code ? null : err);
return;
}
if (stats.isDirectory()) {
walk_deep(path, match, iterator, callback);
return;
}
if (match(path)) {
iterator(path, stats, callback);
return;
}
callback();
});
};
/**
* FsTools.walkSync(path, pattern, iterator) -> void
* FsTools.walkSync(path, iterator) -> void
*
* Synchronous version of [[FsTools.walk]].
**/
fstools.walkSync = function (path, pattern, iterator) {
var match, stat;
if (!iterator) {
pattern = null;
iterator = arguments[1];
}
if (!pattern) {
match = function () { return true; };
} else if (_.isFunction(pattern) && !_.isRegExp(pattern)) {
match = pattern;
} else {
pattern = new RegExp(pattern);
match = function (path) { return pattern.test(path); };
}
path = path_normalize(path);
try {
stat = fs.lstatSync(path);
} catch (err) {
if ('ENOENT' === err.code) {
return;
}
// rethrow
throw err;
}
if (stat.isDirectory()) {
fs.readdirSync(path).forEach(function (file) {
fstools.walkSync(path_join(path, file), match, iterator);
});
return;
}
if (match(path)) {
iterator(path, stat);
return;
}
};
/**
* FsTools.remove(path, callback) -> void
* - path (String): Path to remove
* - callback (Function): Fired after path was removed
*
* Removes given `path`. If it was a directory will remove it recursively,
* similar to UNIX' `rm -rf <path>`. After all will fire `callback(err)` with
* an error if there were any.
*
* If given `path` was file - will proxy call to `fs.unlink`.
*
*
* ##### Example
*
* fstools.remove('/home/nodeca/trash', function (err) {
* if (err) {
* console.log("U can't touch that");
* console.err(err);
* process.exit(1);
* } else {
* console.log("It's Hammer time");
* process.exit(0);
* }
* });
**/
fstools.remove = function (path, callback) {
path = path_normalize(path);
fs.lstat(path, function (err, stats) {
if (err) {
// file/dir not exists - no need to do anything
if ('ENOENT' === err.code) {
callback(null);
return;
}
// unknown error - can't continue
callback(err);
return;
}
if (!stats.isDirectory()) {
fs.unlink(path, callback);
return;
}
async.series([
async.apply(walk_flat, path, fstools.remove),
async.apply(fs.rmdir, path)
], function (err/*, results */) {
callback(err);
});
});
};
/**
* FsTools.removeSync(path) -> void
* - path (String): Path to remove
*
* Removes given `path`. If it was a directory will remove it recursively,
* similar to UNIX' `rm -rf <path>`.
*
* If given `path` was file - will proxy call to `fs.unlinkSync`.
*
*
* ##### Example
*
* try {
* fstools.remove('/home/nodeca/trash');
* console.log("It's Hammer time");
* process.exit(0);
* } catch (err) {
* console.log("U can't touch that");
* console.err(err);
* process.exit(1);
* }
**/
fstools.removeSync = function removeSync(path) {
var nested_err, lstat;
path = path_normalize(path);
lstat = fs.lstatSync(path);
if (!lstat.isDirectory()) {
fs.unlinkSync(path);
return;
}
fs.readdirSync(path).forEach(function (file) {
try {
fstools.removeSync(path_join(path, file));
} catch (err) {
nested_err = err;
}
});
if (!nested_err) {
fs.rmdirSync(path);
return;
}
throw nested_err;
};
/**
* FsTools.mkdir(path, mode, callback) -> void
* FsTools.mkdir(path, callback) -> void
* - path (String): Path to create
* - mode (String|Number): Permission mode of new directory. See stdlib
* fs.mkdir for details. Default: '0755'.
* - callback (Function): Fired after path was created
*
* Creates given path, creating parents recursively if needed.
* Similar to UNIX' `mkdir -pf <path>`. After all will fire `callback(err)` with
* an error if there were any.
*
*
* ##### Example
*
* fstools.mkdir('/home/nodeca/media/xxx', function (err) {
* if (err) {
* console.log("Can't' create directory");
* console.err(err);
* process.exit(1);
* } else {
* console.log("We can now store some romantic movies here");
* process.exit(0);
* }
* });
**/
fstools.mkdir = function (path, mode, callback) {
if (undefined === callback && _.isFunction(mode)) {
callback = mode;
mode = '0755';
}
path = path_normalize(path);
fs.exists(path, function (exists) {
var parent;
if (exists) {
callback(null);
return;
}
parent = dirname(path);
fstools.mkdir(parent, mode, function (err) {
if (err) {
callback(err);
return;
}
fs.mkdir(path, mode, function (err) {
// EEXIST is not error in our case
// but a race condition :((
if (err && 'EEXIST' === err.code) {
callback(null);
return;
}
// fallback to default behavior
callback(err);
});
});
});
};
/**
* FsTools.mkdirSync(path[, mode = '0755']) -> void
*
* Sync version of [[FsTools.mkdir]].
**/
fstools.mkdirSync = function (path, mode) {
mode = mode || '0755';
path = path_normalize(path);
if (fs.existsSync(path)) {
return;
}
fstools.mkdirSync(dirname(path), mode);
try {
fs.mkdirSync(path, mode);
} catch (err) {
if ('EEXIST' !== err.code) {
throw err;
}
}
};
/**
* FsTools.copy(src, dst, callback) -> void
* - src (String): Source file
* - dst (String): Destination file
* - callback (Function): Fired after path has been copied
*
* Copies `src` to `dst`, creates directory for given `dst` with
* [[FsTools.mkdir]] if needed. Fires `callback(err)` upon
* completion.
*
*
* ##### Example
*
* var src = '/home/nodeca/secrets.yml',
* dst = '/home/nodeca/very/deep/secrets/main.yml';
*
* fstools.copy(src, dst, function (err) {
* if (err) {
* console.log("Failed copy " + src + " into " + dst);
* console.err(err);
* process.exit(1);
* } else {
* console.log("Done!");
* process.exit(0);
* }
* });
**/
fstools.copy = function (src, dst, callback) {
src = path_normalize(src);
dst = path_normalize(dst);
// sad but true - people make mistakes...
if (src === dst) {
callback(null);
return;
}
fs.lstat(src, function (err, stats) {
if (err) {
callback(err);
return;
}
fstools.mkdir(dirname(dst), function (err) {
var chmod, done;
if (err) {
callback(err);
return;
}
// chmod dst
chmod = async.apply(fs.chmod, dst, stats.mode.toString(8).slice(-4));
// reject async.series' results
done = function (err/*, results */) { callback(err); };
// *** file
if (stats.isFile()) {
async.series([async.apply(copy_file, src, dst), chmod], done);
return;
}
// *** symbolic link
if (stats.isSymbolicLink()) {
async.waterfall([
function (next) {
fs.exists(dst, function (exists) {
if (exists) {
fstools.remove(dst, next);
return;
}
next();
});
},
async.apply(fs.readlink, src),
function (linkpath, next) {
fs.symlink(linkpath, dst, next);
},
chmod
], done);
return;
}
// *** directory
if (stats.isDirectory()) {
async.series([
function (next) {
fs.mkdir(dst, '0755', function (err) {
if (err && 'EEXIST' === err.code) {
next(null);
return;
}
next(err);
});
},
async.apply(walk_flat, src, function (path, next) {
fstools.copy(path, dst + path.replace(src, ''), next);
}),
chmod
], done);
return;
}
// *** unsupported src
callback(new Error('Unsupported type of the source'));
});
});
};
/**
* FsTools.move(source, destination, callback) -> Void
* - source (String): Source filename
* - destination (String): Destination filename
*
* Moves file from `source` to `destination`.
**/
fstools.move = function move(source, destination, callback) {
fs.rename(source, destination, function (err) {
if (!err) {
callback();
return;
}
// TODO: Needs testing coverage
// normally err.code can be:
// - EXDEV (different partitions/devices)
// - ENOTEMPTY (source and destination are not-empty dirs)
// - EISDIR (destionation is dir, source is file)
async.series([
async.apply(fstools.copy, source, destination),
async.apply(fstools.remove, source)
], function (err/*, results*/) {
callback(err);
});
});
};
/**
* FsTools.tmpdir([template = '/tmp/fstools.XXXXXX']) -> String
* - template (String): Temporary directory pattern.
*
* Returns non-existing (at the moment of request) temporary directory path.
* `template` must contain a substring with at least 3 consecutive `X`, that
* will be replaced with pseudo-random string of the same length.
*
*
* ##### Example
*
* fstools.tmpdir('/tmp/fooXXX'); // -> '/tmp/fooa2f'
* fstools.tmpdir('/tmp/fooXXX'); // -> '/tmp/foocb1'
* fstools.tmpdir('/tmp/foo-XXXXXX'); // -> '/tmp/foo-ad25e0'
**/
fstools.tmpdir = function tmpdir(template) {
// Default location - in system tmp dir
var tmp_default = path_join(
// Just fallback to '/tmp' for node v0.6. Dirty, but better than nothing :)
os.tmpdir ? os.tmpdir() : os.tmpDir ? os.tmpDir() : '/tmp',
'fstools.XXXXXX'
);
var match = (template || tmp_default).match(/^(.*?)(X{3,})(.*?)$/),
attempts, length, random, pathname;
if (!match) {
throw new Error('Invalid tmpdir template: ' + template);
}
attempts = 5;
length = match[2].length;
// Do not try more than attempts of times
while (attempts--) {
random = crypto.randomBytes(Math.ceil(length / 2)).toString('hex').substring(0, length);
pathname = (match[1] || '') + random + (match[3] || '');
if (!fs.existsSync(pathname)) {
// Generated pathname is uniq - return it
return pathname;
}
}
throw new Error('Failed to generate uniq tmpdir with template: ' + template);
};
/**
* FsTools.findSorted(pathname, pattern, callback(err, files)) -> Void
* FsTools.findSorted(pathname, callback(err, files)) -> Void
*
* Finds all files matching `pattern` withing `pathname` (including sub-dirs),
* and passes list of found files (sorted alphabethically) to the `callback`.
* If no `pattern` was given - will collect all found files.
**/
fstools.findSorted = function findSorted(pathname, pattern, callback) {
var files = [];
if (!callback) {
callback = pattern;
pattern = null;
}
fstools.walk(pathname, pattern, function (file, stats, next) {
files.push(file);
next();
}, function (err) {
callback(err, files.sort());
});
};