From 106bd60d9bf54570da3e102c0ca44efd612bb816 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 3 Dec 2010 01:58:29 -0700 Subject: [PATCH] started on much much better version of walk --- lib/walk2.js | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 lib/walk2.js diff --git a/lib/walk2.js b/lib/walk2.js new file mode 100644 index 0000000..5373b39 --- /dev/null +++ b/lib/walk2.js @@ -0,0 +1,138 @@ +(function () { + var fs = require('fs'), + upath = require('path'), + util = require('util'), + Futures = require('futures'), + events = require('events'), + emitter = new events.EventEmitter(), + oneNodeEvents = [ + "file", + "directory", + "blockDevice", + "characterDevice", + "symbolicLink", + "fifo", + "socket" + ], + multiNodeEvents = [ + // multiple + "files", + "directories", + "blockDevices", + "characterDevices", + "symbolicLinks", + "fifos", + "sockets" + ], + fstat; + + function sortNodesByType(path, stats, o, next) { + var type, listeners, num, count; + + if (stats.isFile()) { + type = "file"; + o.files.push(stats); + } else if (stats.isDirectory()) { + type = "directory"; + o.dirs.push(stats); + } else if (stats.isBlockDevice()) { + type = "blockDevice"; + o.blocks.push(stats); + } else if (stats.isCharacterDevice()) { + type = "characterDevice"; + o.chars.push(stats); + } else if (stats.isSymbolicLink()) { + type = "symbolicLink"; + o.links.push(stats); + } else if (stats.isFIFO()) { + type = "fifo"; + o.fifos.push(stats); + } else if (stats.isSocket()) { + type = "socket"; + o.sockets.push(stats); + } else { + throw new Error(upath.join(path,stats.name) + 'is not of any tested node type'); + } + + listeners = emitter.listeners(type); + // get the current number of listeners (may change) + num = listeners.length; + if (!num) { + next(); + return; + } + + // join all; once all listeners have responded, continue + count = 0; + emitter.emit(type, path, stats, function () { + count += 1; + if (num === count) { + next(); + } + }); + } + + function handleFiles(path, files) { + var s = Futures.sequence(), + nodes = [], + o = { + errors: [], + dirs: [], + files: [], + links: [], + blocks: [], + chars: [], + fifos: [], + sockets: [] + }; + + files.forEach(function (file) { + s.then(function (next) { + fstat(upath.join(path, file), function (err, stats) { + if (err) { + util.debug("[Error] " + util.inspect(err)); + next(); + return; + } + stats.name = file; + sortNodesByType(path, stats, o, next); + }); + }); + }); + s.then(function (next) { + // TODO copycat the emitters above + next(); + }); + return s; + } + + function handlePath(path) { + + } + + function walk(fullpath, options) { + fstat = (options||{}).followLinks ? fs.stat : fs.lstat; + + var path, file, len, s; + + upath.normalize(fullpath); + + len = fullpath.length - 1; + if (len > 1 && '/' === fullpath[len]) { + fullpath = fullpath.substr(0, len); + } + + path = upath.dirname(fullpath); + file = upath.basename(fullpath); + + s = handleFiles(path, [file]); + s(function (next) { next(); }); + return emitter; + } + + var walker = walk("/System"); + walker.on("directory", function (path, dir, next) { + console.log(path, dir.name); + next(); + }); +}());