Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit c5dcd6f

Browse files
committed
return path in linux, fixes #25
1 parent 6b16a8a commit c5dcd6f

2 files changed

Lines changed: 29 additions & 10 deletions

File tree

spec/pathwatcher-spec.coffee

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ describe 'PathWatcher', ->
1111

1212
beforeEach ->
1313
fs.writeFileSync(tempFile, '')
14-
14+
@addMatchers {
15+
toBeAnyOf: (expectedValues) ->
16+
@actual in expectedValues
17+
}
1518
afterEach ->
1619
pathWatcher.closeAllWatchers()
1720

@@ -36,7 +39,7 @@ describe 'PathWatcher', ->
3639
expect(pathWatcher.getWatchedPaths()).toEqual []
3740

3841
describe 'when a watched path is changed', ->
39-
it 'fires the callback with the event type and empty path', ->
42+
it 'fires the callback with the event type and path', ->
4043
eventType = null
4144
eventPath = null
4245
watcher = pathWatcher.watch tempFile, (type, path) ->
@@ -47,7 +50,7 @@ describe 'PathWatcher', ->
4750
waitsFor -> eventType?
4851
runs ->
4952
expect(eventType).toBe 'change'
50-
expect(eventPath).toBe ''
53+
expect(eventPath).toBe tempFile
5154

5255
describe 'when a watched path is renamed #darwin #win32', ->
5356
it 'fires the callback with the event type and new path and watches the new path', ->
@@ -75,32 +78,32 @@ describe 'PathWatcher', ->
7578
waitsFor -> deleted
7679

7780
describe 'when a file under watched directory is deleted', ->
78-
it 'fires the callback with the change event and empty path', (done) ->
81+
it 'fires the callback with the change event and path', (done) ->
7982
fileUnderDir = path.join(tempDir, 'file')
8083
fs.writeFileSync(fileUnderDir, '')
8184
watcher = pathWatcher.watch tempDir, (type, path) ->
8285
expect(type).toBe 'change'
83-
expect(path).toBe ''
86+
expect(path).toBe fileUnderDir
8487
done()
8588
fs.unlinkSync(fileUnderDir)
8689

8790
describe 'when a new file is created under watched directory', ->
88-
it 'fires the callback with the change event and empty path', ->
91+
it 'fires the callback with the change event and path', ->
8992
newFile = path.join(tempDir, 'file')
9093
watcher = pathWatcher.watch tempDir, (type, path) ->
9194
fs.unlinkSync(newFile)
9295

9396
expect(type).toBe 'change'
94-
expect(path).toBe ''
97+
expect(path).toBe newFile
9598
done()
9699
fs.writeFileSync(newFile, '')
97100

98101
describe 'when a file under watched directory is moved', ->
99-
it 'fires the callback with the change event and empty path', (done) ->
102+
it 'fires the callback with the change event and both paths', (done) ->
100103
newName = path.join(tempDir, 'file2')
101104
watcher = pathWatcher.watch tempDir, (type, path) ->
102105
expect(type).toBe 'change'
103-
expect(path).toBe ''
106+
expect(path).toBeAnyOf [tempFile, newName]
104107
done()
105108
fs.renameSync(tempFile, newName)
106109

src/pathwatcher_linux.cc

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@
88

99
#include <algorithm>
1010

11+
#include <map>
12+
#include <string>
13+
1114
#include "common.h"
1215

1316
static int g_inotify;
1417

18+
static std::map<int, std::string> handlemap;
19+
1520
void PlatformInit() {
1621
g_inotify = inotify_init();
1722
if (g_inotify == -1) {
@@ -43,7 +48,14 @@ void PlatformThread() {
4348

4449
int fd = e->wd;
4550
EVENT_TYPE type;
46-
std::vector<char> path;
51+
std::string fullpath = handlemap[fd];
52+
53+
if (e->len > 0) {
54+
fullpath += "/";
55+
fullpath += e->name;
56+
}
57+
58+
std::vector<char> path(fullpath.begin(), fullpath.end());
4759

4860
// Note that inotify won't tell us where the file or directory has been
4961
// moved to, so we just treat IN_MOVE_SELF as file being deleted.
@@ -63,11 +75,15 @@ void PlatformThread() {
6375
WatcherHandle PlatformWatch(const char* path) {
6476
int fd = inotify_add_watch(g_inotify, path, IN_ATTRIB | IN_CREATE |
6577
IN_DELETE | IN_MODIFY | IN_MOVE | IN_MOVE_SELF | IN_DELETE_SELF);
78+
79+
handlemap[fd] = std::string(path);
80+
6681
return fd;
6782
}
6883

6984
void PlatformUnwatch(WatcherHandle fd) {
7085
inotify_rm_watch(g_inotify, fd);
86+
handlemap.erase(fd);
7187
}
7288

7389
bool PlatformIsHandleValid(WatcherHandle handle) {

0 commit comments

Comments
 (0)