Search code examples
node.jsfilesystemwatcher

In Node.js, is it possible to tell where a watched file was moved to?


fs.watch provides only two possible event types: 'rename' and 'change'. Both renaming a file (e.g. using mv) and deleting it (e.g. using rm) cause fs.watch to report a 'rename' event.

After a file is moved, fs.watch will continue to report events from it (unless you explicitly close the FSWatcher). When that happens, I'd like to know where the file has moved to.

Is there any way to do this, short of touching every file on the system in turn to see when a 'change' event is fired?


Solution

  • This looks impossible until node either implements an interface to get file names from a file descriptor (so you could just keep a fd and get the name from there on rename), or gives back the path/filename reliably along the FSWatcher events.

    A workaround to this would be to create a temporary hard link to the target file via fs.link , then you can get at the file even after changes, though you won't be able to retrieve it's new name.

    It's interesting that EventMachine has the same issue: http://eventmachine.rubyforge.org/EventMachine/FileWatch.html#M000291