| 1 |
// Copyright 2010-2011 Mikeal Rogers |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
var sys = require('util') |
| 16 |
, fs = require('fs') |
| 17 |
, path = require('path') |
| 18 |
, events = require('events') |
| 19 |
; |
| 20 |
|
| 21 |
function walk (dir, options, callback) { |
| 22 |
if (!callback) {callback = options; options = {}} |
| 23 |
if (!callback.files) callback.files = {}; |
| 24 |
if (!callback.pending) callback.pending = 0; |
| 25 |
callback.pending += 1; |
| 26 |
fs.stat(dir, function (err, stat) { |
| 27 |
if (err) return callback(err); |
| 28 |
callback.files[dir] = stat; |
| 29 |
fs.readdir(dir, function (err, files) { |
| 30 |
if (err) { |
| 31 |
if(err.code === 'EACCES' && options.ignoreUnreadableDir) return callback(); |
| 32 |
return callback(err); |
| 33 |
} |
| 34 |
callback.pending -= 1; |
| 35 |
files.forEach(function (f, index) { |
| 36 |
f = path.join(dir, f); |
| 37 |
callback.pending += 1; |
| 38 |
fs.stat(f, function (err, stat) { |
| 39 |
var enoent = false |
| 40 |
, done = false; |
| 41 |
|
| 42 |
if (err) { |
| 43 |
if (err.code !== 'ENOENT' && (err.code !== 'EPERM' && options.ignoreNotPermitted)) { |
| 44 |
return callback(err); |
| 45 |
} else { |
| 46 |
enoent = true; |
| 47 |
} |
| 48 |
} |
| 49 |
callback.pending -= 1; |
| 50 |
done = callback.pending === 0; |
| 51 |
if (!enoent) { |
| 52 |
if (options.ignoreDotFiles && path.basename(f)[0] === '.') return done && callback(null, callback.files); |
| 53 |
if (options.filter && !options.filter(f, stat)) return done && callback(null, callback.files); |
| 54 |
callback.files[f] = stat; |
| 55 |
if (stat.isDirectory() && !(options.ignoreDirectoryPattern && options.ignoreDirectoryPattern.test(f))) walk(f, options, callback); |
| 56 |
done = callback.pending === 0; |
| 57 |
if (done) callback(null, callback.files); |
| 58 |
} |
| 59 |
}) |
| 60 |
}) |
| 61 |
if (callback.pending === 0) callback(null, callback.files); |
| 62 |
}) |
| 63 |
if (callback.pending === 0) callback(null, callback.files); |
| 64 |
}) |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
var watchedFiles = Object.create(null); |
| 69 |
|
| 70 |
exports.watchTree = function ( root, options, callback ) { |
| 71 |
if (!callback) {callback = options; options = {}} |
| 72 |
walk(root, options, function (err, files) { |
| 73 |
if (err) throw err; |
| 74 |
var fileWatcher = function (f) { |
| 75 |
var fsOptions = {}; |
| 76 |
if (options.interval) { |
| 77 |
fsOptions.interval = options.interval * 1000; |
| 78 |
} |
| 79 |
fs.watchFile(f, fsOptions, function (c, p) { |
| 80 |
// Check if anything actually changed in stat |
| 81 |
if (files[f] && !files[f].isDirectory() && c.nlink !== 0 && files[f].mtime.getTime() == c.mtime.getTime()) return; |
| 82 |
files[f] = c; |
| 83 |
if (!files[f].isDirectory()) callback(f, c, p); |
| 84 |
else { |
| 85 |
fs.readdir(f, function (err, nfiles) { |
| 86 |
if (err) return; |
| 87 |
nfiles.forEach(function (b) { |
| 88 |
var file = path.join(f, b); |
| 89 |
if (!files[file] && (options.ignoreDotFiles !== true || b[0] != '.')) { |
| 90 |
fs.stat(file, function (err, stat) { |
| 91 |
if (options.filter && !options.filter(file, stat)) return; |
| 92 |
callback(file, stat, null); |
| 93 |
files[file] = stat; |
| 94 |
fileWatcher(file); |
| 95 |
}) |
| 96 |
} |
| 97 |
}) |
| 98 |
}) |
| 99 |
} |
| 100 |
if (c.nlink === 0) { |
| 101 |
// unwatch removed files. |
| 102 |
delete files[f] |
| 103 |
fs.unwatchFile(f); |
| 104 |
} |
| 105 |
}) |
| 106 |
} |
| 107 |
fileWatcher(root); |
| 108 |
for (var i in files) { |
| 109 |
fileWatcher(i); |
| 110 |
} |
| 111 |
watchedFiles[root] = files; |
| 112 |
callback(files, null, null); |
| 113 |
}) |
| 114 |
} |
| 115 |
|
| 116 |
exports.unwatchTree = function (root) { |
| 117 |
if (!watchedFiles[root]) return; |
| 118 |
Object.keys(watchedFiles[root]).forEach(fs.unwatchFile); |
| 119 |
watchedFiles[root] = false; |
| 120 |
}; |
| 121 |
|
| 122 |
exports.createMonitor = function (root, options, cb) { |
| 123 |
if (!cb) {cb = options; options = {}} |
| 124 |
var monitor = new events.EventEmitter(); |
| 125 |
monitor.stop = exports.unwatchTree.bind(null, root); |
| 126 |
|
| 127 |
var prevFile = {file: null,action: null,stat: null}; |
| 128 |
exports.watchTree(root, options, function (f, curr, prev) { |
| 129 |
// if not curr, prev, but f is an object |
| 130 |
if (typeof f == "object" && prev == null && curr === null) { |
| 131 |
monitor.files = f; |
| 132 |
return cb(monitor); |
| 133 |
} |
| 134 |
|
| 135 |
// if not prev and either prevFile.file is not f or prevFile.action is not created |
| 136 |
if (!prev) { |
| 137 |
if (prevFile.file != f || prevFile.action != "created") { |
| 138 |
prevFile = { file: f, action: "created", stat: curr }; |
| 139 |
return monitor.emit("created", f, curr); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
// if curr.nlink is 0 and either prevFile.file is not f or prevFile.action is not removed |
| 144 |
if (curr) { |
| 145 |
if (curr.nlink === 0) { |
| 146 |
if (prevFile.file != f || prevFile.action != "removed") { |
| 147 |
prevFile = { file: f, action: "removed", stat: curr }; |
| 148 |
return monitor.emit("removed", f, curr); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// if prevFile.file is null or prevFile.stat.mtime is not the same as curr.mtime |
| 154 |
if (prevFile.file === null) { |
| 155 |
return monitor.emit("changed", f, curr, prev); |
| 156 |
} |
| 157 |
// stat might return null, so catch errors |
| 158 |
try { |
| 159 |
if (prevFile.stat.mtime.getTime() !== curr.mtime.getTime()) { |
| 160 |
return monitor.emit("changed", f, curr, prev); |
| 161 |
} |
| 162 |
} catch(e) { |
| 163 |
return monitor.emit("changed", f, curr, prev); |
| 164 |
} |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
exports.walk = walk; |