Skip to content

Commit

Permalink
return path in linux, fixes atom#25
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovacs committed Sep 19, 2014
1 parent 6b16a8a commit c5dcd6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
21 changes: 12 additions & 9 deletions spec/pathwatcher-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ describe 'PathWatcher', ->

beforeEach ->
fs.writeFileSync(tempFile, '')

@addMatchers {
toBeAnyOf: (expectedValues) ->
@actual in expectedValues
}
afterEach ->
pathWatcher.closeAllWatchers()

Expand All @@ -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) ->
Expand All @@ -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', ->
Expand Down Expand Up @@ -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)

Expand Down
18 changes: 17 additions & 1 deletion src/pathwatcher_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

#include <algorithm>

#include <map>
#include <string>

#include "common.h"

static int g_inotify;

static std::map<int, std::string> handlemap;

void PlatformInit() {
g_inotify = inotify_init();
if (g_inotify == -1) {
Expand Down Expand Up @@ -43,7 +48,14 @@ void PlatformThread() {

int fd = e->wd;
EVENT_TYPE type;
std::vector<char> path;
std::string fullpath = handlemap[fd];

if (e->len > 0) {
fullpath += "/";
fullpath += e->name;
}

std::vector<char> 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.
Expand All @@ -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) {
Expand Down

0 comments on commit c5dcd6f

Please sign in to comment.