Skip to content

Commit

Permalink
gral_directory_monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
eyelash committed Feb 12, 2024
1 parent 838ce82 commit f4d406b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions gral.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ void gral_file_remove(char const *path);
void gral_directory_create(char const *path);
void gral_directory_iterate(char const *path, void (*callback)(char const *name, void *user_data), void *user_data);
void gral_directory_remove(char const *path);
void gral_directory_monitor(char const *path, void (*callback)(void *user_data), void *user_data);


/*=========
Expand Down
4 changes: 4 additions & 0 deletions gral_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,10 @@ void gral_directory_remove(char const *path) {
rmdir(path);
}

void gral_directory_monitor(char const *path, void (*callback)(void *user_data), void *user_data) {
// TODO: implement
}


/*=========
TIME
Expand Down
4 changes: 4 additions & 0 deletions gral_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,10 @@ void gral_directory_remove(char const *path) {
rmdir(path);
}

void gral_directory_monitor(char const *path, void (*callback)(void *user_data), void *user_data) {
// TODO: implement
}


/*=========
TIME
Expand Down
28 changes: 28 additions & 0 deletions gral_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,34 @@ void gral_directory_remove(char const *path) {
RemoveDirectory(utf8_to_utf16(path));
}

struct DirectoryMonitor {
HANDLE directory;
OVERLAPPED overlapped;
char buffer[4096];
};

static void directory_monitor_callback(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped) {
DirectoryMonitor *monitor = lpOverlapped.hEvent;
DWORD offset = 0;
while (TRUE) {
FILE_NOTIFY_INFORMATION *information = (FILE_NOTIFY_INFORMATION *)(monitor->buffer + offset);
if (information->NextEntryOffset == 0) {
break;
}
offset += information->NextEntryOffset;
}
ReadDirectoryChangesW(monitor->directory, monitor->buffer, sizeof(monitor->buffer), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, NULL, &monitor->overlapped, directory_monitor_callback);
}

void gral_directory_monitor(char const *path, void (*callback)(void *user_data), void *user_data) {
DirectoryMonitor *monitor = new DirectoryMonitor();
monitor->directory = CreateFile(utf8_to_utf16(path), FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
ZeroMemory(&monitor->overlapped, sizeof(monitor->overlapped));
monitor->overlapped.hEvent = monitor;
ReadDirectoryChangesW(monitor->directory, monitor->buffer, sizeof(monitor->buffer), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, NULL, &monitor->overlapped, directory_monitor_callback);
//CloseHandle(monitor->directory);
}


/*=========
TIME
Expand Down

0 comments on commit f4d406b

Please sign in to comment.