From cd5eac2cbb22966631d6dc98f788be0cf9178c02 Mon Sep 17 00:00:00 2001 From: dceejay Date: Mon, 16 Mar 2015 20:21:05 +0000 Subject: [PATCH] Add type and size reporting to the file watch node. --- nodes/core/io/23-watch.html | 4 +++- nodes/core/io/23-watch.js | 16 +++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/nodes/core/io/23-watch.html b/nodes/core/io/23-watch.html index a980c9dd2..1af6b4e64 100644 --- a/nodes/core/io/23-watch.html +++ b/nodes/core/io/23-watch.html @@ -33,7 +33,9 @@

On Windows you must use double back-slashes \\ in any directory names.

The full filename of the file that actually changed is put into msg.payload, while a stringified version of the watch list is returned in msg.topic.

-

msg.file contains just the short filename of the file that changed.

+

msg.file contains just the short filename of the file that changed. + msg.type has the type of thing changed, usually file or directory, + while msg.size holds the file size in bytes.

Of course in Linux, everything is a file and thus can be watched...

Note: The directory or file must exist in order to be watched. If the file or directory gets deleted it may no longer be monitored even if it gets re-created.

diff --git a/nodes/core/io/23-watch.js b/nodes/core/io/23-watch.js index 8a17f5ac2..6434b42c1 100644 --- a/nodes/core/io/23-watch.js +++ b/nodes/core/io/23-watch.js @@ -24,7 +24,7 @@ module.exports = function(RED) { RED.nodes.createNode(this,n); this.files = n.files.split(","); - for (var f =0; f < this.files.length; f++) { + for (var f=0; f < this.files.length; f++) { this.files[f] = this.files[f].trim(); } this.p = (this.files.length == 1) ? this.files[0] : JSON.stringify(this.files); @@ -32,15 +32,25 @@ module.exports = function(RED) { var notifications = new Notify(node.files); notifications.on('change', function (file, event, path) { + var stat; try { if (fs.statSync(path).isDirectory()) { path = path + sep + file; } + stat = fs.statSync(path); } catch(e) { } - var msg = { payload: path, topic: node.p, file: file }; + var type = "other"; + if (stat.isFile()) { type = "file"; } + if (stat.isDirectory()) { type = "directory"; } + if (stat.isBlockDevice()) { type = "blockdevice"; } + if (stat.isCharacterDevice()) { type = "characterdevice"; } + if (stat.isSocket()) { type = "socket"; } + if (stat.isFIFO()) { type = "fifo"; } + var msg = { payload:path, topic:node.p, file:file, type:type, size:stat.size }; node.send(msg); }); notifications.on('error', function (error, path) { - node.warn(error); + var msg = { payload:path }; + node.error(error,msg); }); this.close = function() {