Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
Implement recursive fs watch on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostoy committed Sep 13, 2014
1 parent 49cb40c commit 5e83804
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/uv-win.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,7 @@ RB_HEAD(uv_timer_tree_s, uv_timer_s);
HANDLE dir_handle; \
int req_pending; \
uv_fs_event_cb cb; \
unsigned int win_flags; \
WCHAR* filew; \
WCHAR* short_filew; \
WCHAR* dirw; \
Expand Down
23 changes: 20 additions & 3 deletions src/win/fs-event.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static void uv_fs_event_queue_readdirchanges(uv_loop_t* loop,
if (!ReadDirectoryChangesW(handle->dir_handle,
handle->buffer,
uv_directory_watcher_buffer_size,
FALSE,
(handle->win_flags & UV_FS_EVENT_RECURSIVE) ? TRUE: FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
Expand All @@ -63,6 +63,21 @@ static void uv_fs_event_queue_readdirchanges(uv_loop_t* loop,
handle->req_pending = 1;
}

static int uv_relative_path(const WCHAR* filename, const WCHAR* dir,
WCHAR** relpath) {
int dirlen = wcslen(dir);
int filelen = wcslen(filename);
if (dir[dirlen - 1] == '\\') {
dirlen--;
}
*relpath = (WCHAR*)malloc((MAX_PATH + 1) * sizeof(WCHAR));
if (!*relpath) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
}
wcsncpy(*relpath, filename + dirlen + 1, filelen - dirlen - 1);
(*relpath)[filelen - dirlen - 1] = L'\0';
return 0;
}

static int uv_split_path(const WCHAR* filename, WCHAR** dir,
WCHAR** file) {
Expand Down Expand Up @@ -137,6 +152,8 @@ int uv_fs_event_start(uv_fs_event_t* handle,
return UV_EINVAL;

handle->cb = cb;
// Added win_flags for recursive watcher
handle->win_flags = flags;
handle->path = strdup(path);
if (!handle->path) {
uv_fatal_error(ERROR_OUTOFMEMORY, "malloc");
Expand Down Expand Up @@ -237,7 +254,7 @@ int uv_fs_event_start(uv_fs_event_t* handle,
if (!ReadDirectoryChangesW(handle->dir_handle,
handle->buffer,
uv_directory_watcher_buffer_size,
FALSE,
(handle->win_flags & UV_FS_EVENT_RECURSIVE) ? TRUE: FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
Expand Down Expand Up @@ -410,7 +427,7 @@ void uv_process_fs_event_req(uv_loop_t* loop, uv_req_t* req,

if (long_filenamew) {
/* Get the file name out of the long path. */
result = uv_split_path(long_filenamew, NULL, &filenamew);
result = uv_relative_path(long_filenamew, handle->dirw, &filenamew);
free(long_filenamew);

if (result == 0) {
Expand Down

0 comments on commit 5e83804

Please sign in to comment.