diff --git a/spec/pathwatcher-spec.coffee b/spec/pathwatcher-spec.coffee index df4ef7f..9beccdd 100644 --- a/spec/pathwatcher-spec.coffee +++ b/spec/pathwatcher-spec.coffee @@ -11,7 +11,10 @@ describe 'PathWatcher', -> beforeEach -> fs.writeFileSync(tempFile, '') - + @addMatchers { + toBeAnyOf: (expectedValues) -> + @actual in expectedValues + } afterEach -> pathWatcher.closeAllWatchers() @@ -36,7 +39,7 @@ describe 'PathWatcher', -> expect(pathWatcher.getWatchedPaths()).toEqual [] describe 'when a watched path is changed', -> - it 'fires the callback with the event type and empty path', -> + it 'fires the callback with the event type and path', -> eventType = null eventPath = null watcher = pathWatcher.watch tempFile, (type, path) -> @@ -47,7 +50,7 @@ describe 'PathWatcher', -> waitsFor -> eventType? runs -> expect(eventType).toBe 'change' - expect(eventPath).toBe '' + expect(eventPath).toBe tempFile describe 'when a watched path is renamed #darwin #win32', -> it 'fires the callback with the event type and new path and watches the new path', -> @@ -75,32 +78,32 @@ describe 'PathWatcher', -> waitsFor -> deleted describe 'when a file under watched directory is deleted', -> - it 'fires the callback with the change event and empty path', (done) -> + it 'fires the callback with the change event and path', (done) -> fileUnderDir = path.join(tempDir, 'file') fs.writeFileSync(fileUnderDir, '') watcher = pathWatcher.watch tempDir, (type, path) -> expect(type).toBe 'change' - expect(path).toBe '' + expect(path).toBe fileUnderDir done() fs.unlinkSync(fileUnderDir) describe 'when a new file is created under watched directory', -> - it 'fires the callback with the change event and empty path', -> + it 'fires the callback with the change event and path', -> newFile = path.join(tempDir, 'file') watcher = pathWatcher.watch tempDir, (type, path) -> fs.unlinkSync(newFile) expect(type).toBe 'change' - expect(path).toBe '' + expect(path).toBe newFile done() fs.writeFileSync(newFile, '') describe 'when a file under watched directory is moved', -> - it 'fires the callback with the change event and empty path', (done) -> + it 'fires the callback with the change event and both paths', (done) -> newName = path.join(tempDir, 'file2') watcher = pathWatcher.watch tempDir, (type, path) -> expect(type).toBe 'change' - expect(path).toBe '' + expect(path).toBeAnyOf [tempFile, newName] done() fs.renameSync(tempFile, newName) diff --git a/src/pathwatcher_linux.cc b/src/pathwatcher_linux.cc index 0f6d087..c0c8f4c 100644 --- a/src/pathwatcher_linux.cc +++ b/src/pathwatcher_linux.cc @@ -8,10 +8,15 @@ #include +#include +#include + #include "common.h" static int g_inotify; +static std::map handlemap; + void PlatformInit() { g_inotify = inotify_init(); if (g_inotify == -1) { @@ -43,7 +48,14 @@ void PlatformThread() { int fd = e->wd; EVENT_TYPE type; - std::vector path; + std::string fullpath = handlemap[fd]; + + if (e->len > 0) { + fullpath += "/"; + fullpath += e->name; + } + + std::vector path(fullpath.begin(), fullpath.end()); // Note that inotify won't tell us where the file or directory has been // moved to, so we just treat IN_MOVE_SELF as file being deleted. @@ -63,11 +75,15 @@ void PlatformThread() { WatcherHandle PlatformWatch(const char* path) { int fd = inotify_add_watch(g_inotify, path, IN_ATTRIB | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE | IN_MOVE_SELF | IN_DELETE_SELF); + + handlemap[fd] = std::string(path); + return fd; } void PlatformUnwatch(WatcherHandle fd) { inotify_rm_watch(g_inotify, fd); + handlemap.erase(fd); } bool PlatformIsHandleValid(WatcherHandle handle) {