2010-11-21 05:02:53 +00:00
|
|
|
(function () {
|
|
|
|
var fs = require('fs'),
|
|
|
|
Futures = require('futures'),
|
|
|
|
joinPath = require('path').join,
|
|
|
|
util = require('util'),
|
2010-12-02 10:09:01 +00:00
|
|
|
ev = require("events"),
|
|
|
|
emitter = new ev.EventEmitter(),
|
|
|
|
oneNodeEvent = [
|
|
|
|
"file",
|
|
|
|
"directory",
|
|
|
|
"blockDevice",
|
|
|
|
"characterDevice",
|
|
|
|
"symbolicLink",
|
|
|
|
"fifo",
|
|
|
|
"socket"
|
|
|
|
],
|
|
|
|
multiNodeEvents = [
|
|
|
|
// multiple
|
|
|
|
"files",
|
|
|
|
"directories",
|
|
|
|
"blockDevices",
|
|
|
|
"characterDevices",
|
|
|
|
"symbolicLinks",
|
|
|
|
"fifos",
|
|
|
|
"sockets"
|
|
|
|
],
|
|
|
|
eventsTpl = {
|
|
|
|
listeners: function () { return []; },
|
|
|
|
next: function () { return; }
|
|
|
|
},
|
|
|
|
events = {},
|
|
|
|
nexts = {};
|
|
|
|
|
|
|
|
function newVersion() {
|
|
|
|
throw new Error("see README.md at http://github.com/coolaj86/node-walk");
|
|
|
|
}
|
|
|
|
|
|
|
|
function noop() {
|
|
|
|
}
|
|
|
|
|
|
|
|
function remove(arr, obj) {
|
|
|
|
return arr.splice(arr.indexOf(obj), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
oneNodeEvent.forEach(function (key) {
|
|
|
|
var e = events[key] = {}, next;
|
|
|
|
|
|
|
|
Object.keys(eventsTpl).forEach(function (k) {
|
|
|
|
e[k] = eventsTpl[k]();
|
|
|
|
});
|
|
|
|
|
|
|
|
emitter.on("newListener", function (ev, listener) {
|
|
|
|
var count = 0,
|
|
|
|
num = e.listeners.length + 1;
|
|
|
|
|
|
|
|
e.listeners.push(listener);
|
|
|
|
e.next = function (cb) {
|
|
|
|
cb = noop;
|
|
|
|
return function () {
|
|
|
|
if (count === num) { cb(); }
|
|
|
|
count += 1;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
});
|
2010-11-21 05:02:53 +00:00
|
|
|
|
2010-12-02 10:09:01 +00:00
|
|
|
// TODO
|
|
|
|
next = function () {
|
|
|
|
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
function sortNodesByType(path, stats, o, cb) {
|
2010-11-21 05:02:53 +00:00
|
|
|
if (stats.isFile()) {
|
|
|
|
o.files.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("file", path, stats, (nexts["file"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isDirectory()) {
|
|
|
|
o.dirs.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("directory", path, stats, function () {
|
|
|
|
remove(o.dirs, stats);
|
|
|
|
}, (nexts["directory"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isBlockDevice()) {
|
|
|
|
o.blocks.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("blockDevice", path, stats, (nexts["blockDevice"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isCharacterDevice()) {
|
|
|
|
o.chars.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("characterDevice", path, stats, (nexts["characterDevice"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isSymbolicLink()) {
|
|
|
|
o.links.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("symbolicLink", path, stats, (nexts["symbolicLink"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isFIFO()) {
|
|
|
|
o.fifos.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("fifo", path, stats, (nexts["fifo"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else if (stats.isSocket()) {
|
|
|
|
o.sockets.push(stats);
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.emit("socket", path, stats, (nexts["socket"]||noop)(cb));
|
2010-11-21 05:02:53 +00:00
|
|
|
} else {
|
2010-12-02 10:09:01 +00:00
|
|
|
// emitter.emit("error", stats);
|
|
|
|
util.debug(stats.name + 'is not of any node type');
|
2010-11-21 05:02:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
import os
|
|
|
|
from os.path import join, getsize
|
|
|
|
for root, dirs, files in os.walk('python/Lib/email'):
|
|
|
|
print root, "consumes",
|
|
|
|
print sum(getsize(join(root, name)) for name in files),
|
|
|
|
print "bytes in", len(files), "non-directory files"
|
|
|
|
if 'CVS' in dirs:
|
|
|
|
dirs.remove('CVS') # don't visit CVS directories
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
fs.walk(path, function ({ err, root, dirs, files }) {}, {
|
|
|
|
// currently ignored
|
|
|
|
topdown: boolean,
|
|
|
|
onerror: boolean, // ignored
|
2010-12-02 10:09:01 +00:00
|
|
|
followLinks: boolean // lstat or stat
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
|
|
|
*/
|
|
|
|
|
|
|
|
function walk(firstPath, options, callback) {
|
|
|
|
options = options || {};
|
2010-12-02 10:09:01 +00:00
|
|
|
var fstat = options.followLinks ? fs.stat : fs.lstat,
|
2010-11-21 05:02:53 +00:00
|
|
|
subscription = Futures.subscription();
|
|
|
|
|
|
|
|
if (callback) { subscription.subscribe(callback); }
|
|
|
|
|
|
|
|
function readDir(path) {
|
|
|
|
var p = Futures.promise();
|
|
|
|
|
|
|
|
fs.readdir(path, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
err.path = path;
|
2010-12-02 10:09:01 +00:00
|
|
|
subscription.deliver(err, path);
|
2010-11-21 05:02:53 +00:00
|
|
|
// Signal the completion of this readdir attempt
|
|
|
|
p.fulfill();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO fix futures sequence to not require a first function like this
|
|
|
|
var s = Futures.sequence(function(n){n();}),
|
|
|
|
nodes = [],
|
|
|
|
o = {
|
|
|
|
errors: [],
|
|
|
|
dirs: [],
|
2010-12-02 10:09:01 +00:00
|
|
|
files: [],
|
|
|
|
links: [],
|
2010-11-21 05:02:53 +00:00
|
|
|
blocks: [],
|
|
|
|
chars: [],
|
|
|
|
fifos: [],
|
|
|
|
sockets: []
|
|
|
|
};
|
|
|
|
|
|
|
|
files.forEach(function (file) {
|
2010-12-02 10:09:01 +00:00
|
|
|
// pushes onto the sequence stack without recursion
|
2010-11-21 05:02:53 +00:00
|
|
|
s.then(function (next) {
|
|
|
|
fstat(joinPath(path, file), function (err, stats) {
|
|
|
|
stats = stats || {};
|
|
|
|
stats.name = file;
|
|
|
|
nodes.push(stats);
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
stats.err = err;
|
|
|
|
o.errors.push(stats);
|
|
|
|
} else {
|
2010-12-02 10:09:01 +00:00
|
|
|
sortNodesByType(path, stats, o);
|
2010-11-21 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
s.then(function (next) {
|
|
|
|
var s2 = Futures.sequence(function(n){n();});
|
2010-12-02 10:09:01 +00:00
|
|
|
if (nodes.length > 0) {
|
|
|
|
subscription.deliver(undefined, path, o.errors, o.dirs, o.files, o.links, o.blocks, o.chars, o.fifos, o.sockets);
|
|
|
|
if (o.errors.length > 0) {
|
|
|
|
emitter.emit("errors", path, o.errors);
|
|
|
|
}
|
|
|
|
if (o.dirs.length > 0) {
|
|
|
|
emitter.emit("directories", path, o.dirs);
|
|
|
|
}
|
|
|
|
if (o.files.length > 0) {
|
|
|
|
emitter.emit("files", path, o.files);
|
|
|
|
}
|
|
|
|
if (o.links.length > 0) {
|
|
|
|
emitter.emit("symbolicLinks", path, o.links);
|
|
|
|
}
|
|
|
|
if (o.blocks.length > 0) {
|
|
|
|
emitter.emit("blockDevices", path, o.blocks);
|
|
|
|
}
|
|
|
|
if (o.chars.length > 0) {
|
|
|
|
emitter.emit("characterDevices", path, o.chars);
|
|
|
|
}
|
|
|
|
if (o.fifos.length > 0) {
|
|
|
|
emitter.emit("fifos", path, o.fifos);
|
|
|
|
}
|
|
|
|
if (o.sockets.length > 0) {
|
|
|
|
emitter.emit("sockets", path, o.fifos);
|
|
|
|
}
|
|
|
|
p.fulfill();
|
|
|
|
|
|
|
|
o.dirs.forEach(function (dir) {
|
|
|
|
s2.then(function (next2) {
|
|
|
|
readDir(joinPath(path, dir.name))
|
|
|
|
.when(function () { next2(); });
|
|
|
|
});
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
|
|
|
|
2010-12-02 10:09:01 +00:00
|
|
|
next();
|
|
|
|
}
|
2010-11-21 05:02:53 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return p.passable();
|
|
|
|
}
|
|
|
|
|
|
|
|
readDir(firstPath) //.whenever(callback);
|
|
|
|
|
2010-12-02 10:09:01 +00:00
|
|
|
emitter.whenever = subscription.subscribe;
|
|
|
|
return emitter;
|
2010-11-21 05:02:53 +00:00
|
|
|
}
|
|
|
|
|
2010-12-02 10:09:01 +00:00
|
|
|
newVersion.walk = walk;
|
|
|
|
newVersion.remove = remove;
|
|
|
|
module.exports = newVersion;
|
2010-11-21 05:02:53 +00:00
|
|
|
}());
|