-
Notifications
You must be signed in to change notification settings - Fork 5
/
x-watcher.h
838 lines (717 loc) · 20.5 KB
/
x-watcher.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
#ifndef __X_WATCHER_H
#define __X_WATCHER_H
// necessary includes for the public stuff
#include <pthread.h>
#include <stdbool.h>
#if defined(__linux__)
// noop
#elif defined(__WIN32__)
#include <windows.h>
#include <stdint.h>
#else
#error "Unsupported"
#endif
// PUBLIC STUFF
typedef enum event {
XWATCHER_FILE_UNSPECIFIED,
XWATCHER_FILE_REMOVED,
XWATCHER_FILE_CREATED,
XWATCHER_FILE_MODIFIED,
XWATCHER_FILE_OPENED,
XWATCHER_FILE_ATTRIBUTES_CHANGED,
XWATCHER_FILE_NONE,
XWATCHER_FILE_RENAMED,
// probs more but i couldn't care much
} XWATCHER_FILE_EVENT;
typedef struct xWatcher_reference {
char *path;
void (*callback_func)(
XWATCHER_FILE_EVENT event,
const char *path,
int context,
void *additional_data);
int context;
void *additional_data;
} xWatcher_reference;
struct file {
// just the file name alone
char *name;
// used for adding (additional) context in the handler (if needed)
int context;
// in case you'd like to avoid global variables
void *additional_data;
void (*callback_func)(
XWATCHER_FILE_EVENT event,
const char *path,
int context,
void *additional_data);
} file;
struct directory {
// list of files
struct file *files;
char *path;
// used for adding (additional) context in the handler (if needed)
int context;
// in case you'd like to avoid global variables
void *additional_data;
void (*callback_func)(
XWATCHER_FILE_EVENT event,
const char *path,
int context,
void *additional_data);
#if defined(__linux__)
// we need additional file descriptors (per directory basis)
int inotify_watch_fd;
#elif defined(__WIN32__)
HANDLE handle;
OVERLAPPED overlapped;
uint8_t *event_buffer;
#else
#error "Unsupported"
#endif
} directory;
typedef struct x_watcher {
struct directory *directories;
pthread_t thread;
int thread_id;
bool alive;
#if defined(__linux__)
int inotify_fd; // fd == file descriptor (a common UNIX thing)
#elif defined(__WIN32__)
// literal noop
#else
#error "Unsupported"
#endif
} x_watcher;
// PRIVATE STUFF
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "array.h"
#ifdef __linux__
#include <sys/types.h>
#include <sys/inotify.h>
#include <errno.h>
#include <unistd.h>
#include <poll.h> // for POLLIN
#include <fcntl.h> // for O_NONBLOCK
#define EVENT_SIZE (sizeof(struct inotify_event))
#define BUF_LEN (1024 * (EVENT_SIZE + 16))
#define DIRBRK '/'
static inline void *__internal_xWatcherProcess(void *argument) {
x_watcher *watcher = (x_watcher*) argument;
char buffer[BUF_LEN];
ssize_t lenght;
struct directory *directories = watcher->directories;
while(watcher->alive) {
// poll for events
struct pollfd pfd = { watcher->inotify_fd, POLLIN, 0 };
int ret = poll(&pfd, 1, 50); // timeout of 50ms
if (ret < 0) {
// oops
fprintf(stderr, "poll failed: %s\n", strerror(errno));
break;
} else if (ret == 0) {
// Timeout with no events, move on.
continue;
}
// wait for the kernel to do it's thing
lenght = read(watcher->inotify_fd, buffer, BUF_LEN);
if(lenght < 0) {
// something messed up clearly
perror("read");
return NULL;
}
// pointer to the event structure
ssize_t i = 0;
while(i < lenght) {
// the event list itself
struct inotify_event *event = (struct inotify_event *)
&buffer[i];
// find directory for which this even matches via the descriptor
struct directory *directory = NULL;
for(size_t j = 0; j < arr_count(directories); j++) {
if(directories[j].inotify_watch_fd == event->wd) {
directory = &directories[j];
}
}
if(directory == NULL) {
fprintf(stderr,
"MATCHING FILE DESCRIPTOR NOT FOUND! ERROR!\n");
// BAIL????
}
// find matching file (if any)
struct file *file = NULL;
for(size_t j = 0; j < arr_count(directory->files); j++) {
if(strcmp(directory->files[j].name, event->name) == 0) {
file = &directory->files[j];
}
}
XWATCHER_FILE_EVENT send_event = XWATCHER_FILE_NONE;
if(event->mask & IN_CREATE)
send_event = XWATCHER_FILE_CREATED;
if(event->mask & IN_MODIFY)
send_event = XWATCHER_FILE_MODIFIED;
if(event->mask & IN_DELETE)
send_event = XWATCHER_FILE_REMOVED;
if(event->mask & IN_CLOSE_WRITE ||
event->mask & IN_CLOSE_NOWRITE)
send_event = XWATCHER_FILE_REMOVED;
if(event->mask & IN_ATTRIB)
send_event = XWATCHER_FILE_ATTRIBUTES_CHANGED;
if(event->mask & IN_OPEN)
send_event = XWATCHER_FILE_OPENED;
// file found(?)
if(file != NULL) {
if(send_event != XWATCHER_FILE_NONE) {
// figure out the file path size
size_t filepath_size = strlen(directory->path);
filepath_size += strlen(file->name);
filepath_size += 2;
// create file path string
char *filepath = (char*)malloc(filepath_size);
snprintf(filepath, filepath_size, "%s/%s",
directory->path, file->name);
// callback
file->callback_func(send_event,
filepath,
file->context,
file->additional_data);
// free that garbage
free(filepath);
}
} else {
// Cannot find file, lets try directory
if(directory->callback_func != NULL &&
send_event != XWATCHER_FILE_NONE) {
directory->callback_func(send_event,
directory->path,
directory->context,
directory->additional_data);
}
}
i += EVENT_SIZE + event->len;
}
}
// cleanup time
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
struct directory *directory = &watcher->directories[i];
for(size_t j = 0; j < arr_count(directory->files); j++) {
struct file *file = &directory->files[j];
free(file->name);
}
arr_free(directory->files);
free(directory->path);
inotify_rm_watch(watcher->inotify_fd, directory->inotify_watch_fd);
}
close(watcher->inotify_fd);
arr_free(watcher->directories);
// should we signify that the thread is dead?
return NULL;
}
#elif defined(__WIN32__)
#include <tchar.h>
#define BUF_LEN 1024
#define DIRBRK '\\'
static inline void *__internal_xWatcherProcess(void *argument) {
x_watcher *watcher = (x_watcher*) argument;
struct directory *directories = watcher->directories;
// create an event list so we can still make use of the Windows API
HANDLE events[arr_count(directories)];
for(int i = 0; i < arr_count(directories); i++) {
events[i] = directories[i].overlapped.hEvent;
}
// obv first check if we need to stay alive
while(watcher->alive) {
// wait for any of the objects to respond
DWORD result = WaitForMultipleObjects(arr_count(directories),
events, FALSE, 50 /** timeout of 50ms **/);
// test which object was it
int object_index = -1;
for(int i = 0; i < arr_count(directories); i++) {
if(result == (WAIT_OBJECT_0 + i)) {
object_index = i;
break;
}
}
if(object_index == -1) {
if(result == WAIT_TIMEOUT) {
// it just timed out, let's continue
continue;
} else {
// RUNTIME ERROR! Let's bail
ExitProcess(GetLastError());
}
}
// shorhand for convenience
struct directory *dir = &directories[object_index];
// retrieve event data
DWORD bytes_transferred;
GetOverlappedResult(dir->handle,
&dir->overlapped,
&bytes_transferred, FALSE);
// assign the data's pointer to a proper format for convenience
FILE_NOTIFY_INFORMATION *event = (FILE_NOTIFY_INFORMATION*)
dir->event_buffer;
// loop through the data
for (;;) {
// figure out the wchar string size and allocate as needed
DWORD name_len = event->FileNameLength / sizeof(wchar_t);
char *name_char = malloc(sizeof(char)*(name_len+1));
size_t converted_chars;
// convert wchar* filename to char*
wcstombs_s(&converted_chars, name_char,
name_len+1, event->FileName, name_len);
// convert to proper event type
XWATCHER_FILE_EVENT send_event = XWATCHER_FILE_NONE;
switch (event->Action) {
case FILE_ACTION_ADDED:
send_event = XWATCHER_FILE_CREATED;
break;
case FILE_ACTION_REMOVED:
send_event = XWATCHER_FILE_REMOVED;
break;
case FILE_ACTION_MODIFIED:
send_event = XWATCHER_FILE_MODIFIED;
break;
case FILE_ACTION_RENAMED_OLD_NAME:
case FILE_ACTION_RENAMED_NEW_NAME:
send_event = XWATCHER_FILE_RENAMED;
break;
default:
send_event = XWATCHER_FILE_UNSPECIFIED;
break;
}
// find matching file (if any)
struct file *file = NULL;
for(size_t j = 0; j < arr_count(dir->files); j++) {
if(strcmp(dir->files[j].name, name_char) == 0) {
file = &dir->files[j];
}
}
// file found(?)
if(file != NULL) {
if(send_event != XWATCHER_FILE_NONE) {
// figure out the file path size
size_t filepath_size = strlen(dir->path);
filepath_size += strlen(file->name);
filepath_size += 2;
// create file path string
char *filepath = (char*)malloc(filepath_size);
snprintf(filepath, filepath_size, "%s%c%s",
dir->path, DIRBRK, file->name);
// callback
file->callback_func(send_event,
filepath,
file->context,
file->additional_data);
// free that garbage
free(filepath);
}
} else {
// Cannot find file, lets try directory
if(dir->callback_func != NULL &&
send_event != XWATCHER_FILE_NONE) {
dir->callback_func(send_event,
dir->path,
dir->context,
dir->additional_data);
}
}
// free up the converted string
free(name_char);
// Are there more events to handle?
if (event->NextEntryOffset) {
*((uint8_t**)&event) += event->NextEntryOffset;
} else {
break;
}
}
DWORD dwNotifyFilter =
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_LAST_ACCESS |
FILE_NOTIFY_CHANGE_CREATION |
FILE_NOTIFY_CHANGE_SECURITY;
BOOL recursive = FALSE;
// Queue the next event
BOOL success = ReadDirectoryChangesW(
dir->handle,
dir->event_buffer,
BUF_LEN,
recursive,
dwNotifyFilter,
NULL,
&dir->overlapped,
NULL);
if(!success) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "ReadDirectoryChangesW failed: %s\n",
message);
ExitProcess(error);
}
}
// cleanup time
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
struct directory *directory = &watcher->directories[i];
for(size_t j = 0; j < arr_count(directory->files); j++) {
struct file *file = &directory->files[j];
free(file->name);
}
arr_free(directory->files);
free(directory->path);
free(directory->event_buffer);
CloseHandle(directory->handle);
}
arr_free(watcher->directories);
return NULL;
}
#else
#error "Unsupported"
#endif
static inline x_watcher *xWatcher_create(void) {
x_watcher *watcher = (x_watcher*)malloc(sizeof(x_watcher));
arr_init(watcher->directories);
#if defined(__WIN32__)
// literally noop
#elif defined(__linux__)
watcher->inotify_fd = inotify_init1(O_NONBLOCK);
if(watcher->inotify_fd < 0) {
perror("inotify_init");
return NULL;
}
#else
#error "Unsupported"
#endif
return watcher;
}
static inline bool xWatcher_appendFile(
x_watcher *watcher,
xWatcher_reference *reference) {
char *path = strdup(reference->path);
// the file MUST NOT contain slashed at the end
if(path[strlen(path)-1] == DIRBRK)
return false;
char *filename = NULL;
// we need to split the filename and path
for(size_t i = strlen(path)-1; i > 0; i--) {
if(path[i] == DIRBRK) {
path[i] = '\0'; // break the string, so it splits into two
filename = &path[i+1]; // set the rest of it as the filename
break;
}
}
// If the directory is specifically local, treat it as such.
if(filename == NULL) {
filename = path;
}
struct directory *dir = NULL;
// check against the database of (pre-existing) directories
for(size_t i = 0; i < arr_count(watcher->directories); i++) {
// paths match
if(strcmp(watcher->directories[i].path, path) == 0) {
dir = &watcher->directories[i];
}
}
// directory exists, check if an callback has been already added
if(dir == NULL) {
struct directory new_dir;
new_dir.callback_func = NULL; // DO NOT add callbacks if it's a file
new_dir.context = 0; // context should be invalid as well
new_dir.additional_data = NULL; // so should the data
new_dir.path = path; // add a path to the directory
#if defined(__linux__)
new_dir.inotify_watch_fd = -1; // invalidate inotify
#elif defined(__WIN32__)
new_dir.handle = NULL;
#else
#error "Unsupported"
#endif
// initialize file arrays
arr_init(new_dir.files);
// add the directory to the masses
arr_add(watcher->directories, new_dir);
// move the pointer to the newly added element
dir = &watcher->directories[arr_count(watcher->directories)-1];
}
// search for the file
struct file *file = NULL;
for(size_t i = 0; i < arr_count(dir->files); i++) {
if(strcmp(dir->files[i].name, filename) == 0) {
file = &dir->files[i];
}
}
if(file != NULL) {
return false; // file already exists, that's an ERROR
}
struct file new_file;
// avoid an invalid free because this shares the memory space
// of the full path string
new_file.name = strdup(filename);
new_file.context = reference->context;
new_file.additional_data = reference->additional_data;
new_file.callback_func = reference->callback_func;
// the the element
arr_add(dir->files, new_file);
// and move the pointer to the newly added element
file = &dir->files[arr_count(watcher->directories)-1];
// add the file watcher
#if defined(__linux__)
if(dir->inotify_watch_fd == -1) {
dir->inotify_watch_fd = inotify_add_watch(
watcher->inotify_fd,
path,
IN_ALL_EVENTS);
if(dir->inotify_watch_fd == -1) {
perror("inotify_watch_fd");
return false;
}
}
#elif defined(__WIN32__)
// add directory path
dir->handle = CreateFile(dir->path,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL);
if(dir->handle == INVALID_HANDLE_VALUE) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "CreateFile failed: %s\n",
message);
return false;
}
// create event structure
dir->overlapped.hEvent = CreateEvent(NULL, FALSE, 0, NULL);
if(dir->overlapped.hEvent == NULL) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "CreateEvent failed: %s\n",
message);
return false;
}
// allocate the event buffer
dir->event_buffer = malloc(BUF_LEN);
if(dir->event_buffer == NULL) {
fprintf(stderr, "malloc failed at __FILE__:__LINE__!\n");
return false;
}
// set reading params
BOOL success = ReadDirectoryChangesW(
dir->handle, dir->event_buffer, BUF_LEN, TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_LAST_WRITE,
NULL, &dir->overlapped, NULL);
if(!success) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "ReadDirectoryChangesW failed: %s\n",
message);
return false;
}
#else
#error "Unsupported"
#endif
return true;
}
static inline bool xWatcher_appendDir(
x_watcher *watcher,
xWatcher_reference *reference) {
char *path = strdup(reference->path);
// inotify only works with directories that do NOT have a front-slash
// at the end, so we have to make sure to cut that out
if(path[strlen(path)-1] == DIRBRK)
path[strlen(path)-1] = '\0';
struct directory *dir = NULL;
// check against the database of (pre-existing) directories
for(size_t i=0; i < arr_count(watcher->directories); i++) {
// paths match
if(strcmp(watcher->directories[i].path, path) == 0) {
dir = &watcher->directories[i];
}
}
// directory exists, check if an callback has been already added
if(dir) {
// ERROR, CALLBACK EXISTS
if(dir->callback_func) {
return false;
}
dir->callback_func = reference->callback_func;
dir->context = reference->context;
dir->additional_data = reference->additional_data;
} else {
// keep an eye for this one as it's on the stack
struct directory dir;
dir.path = path;
dir.callback_func = reference->callback_func;
dir.context = reference->context;
dir.additional_data = reference->additional_data;
// initialize file arrays
arr_init(dir.files);
#if defined(__linux__)
dir.inotify_watch_fd = inotify_add_watch(
watcher->inotify_fd,
dir.path,
IN_ALL_EVENTS);
if(dir.inotify_watch_fd == -1) {
perror("inotify_watch_fd");
return false;
}
#elif defined(__WIN32__)
// add directory path
dir.handle = CreateFile(dir.path,
FILE_LIST_DIRECTORY,
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL);
if(dir.handle == INVALID_HANDLE_VALUE) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "CreateFile failed: %s\n",
message);
return false;
}
// create event structure
dir.overlapped.hEvent = CreateEvent(NULL, FALSE, 0, NULL);
if(dir.overlapped.hEvent == NULL) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "CreateEvent failed: %s\n",
message);
return false;
}
// allocate the event buffer
dir.event_buffer = malloc(BUF_LEN);
if(dir.event_buffer == NULL) {
fprintf(stderr, "malloc failed at __FILE__:__LINE__!\n");
return false;
}
// set reading params
BOOL success = ReadDirectoryChangesW(
dir.handle, dir.event_buffer, BUF_LEN, TRUE,
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_LAST_WRITE,
NULL, &dir.overlapped, NULL);
if(!success) {
// get error code
DWORD error = GetLastError();
char *message;
// get error message
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &message,
0, NULL);
fprintf(stderr, "ReadDirectoryChangesW failed: %s\n",
message);
return false;
}
#else
#error "Unsupported"
#endif
arr_add(watcher->directories, dir);
}
return true;
}
static inline bool xWatcher_start(x_watcher *watcher) {
watcher->alive = true;
// create watcher thread
watcher->thread_id = pthread_create(
&watcher->thread,
NULL,
__internal_xWatcherProcess,
watcher);
if(watcher->thread_id != 0) {
perror("pthread_create");
watcher->alive = false;
return false;
}
return true;
}
static inline void xWatcher_destroy(x_watcher *watcher) {
void *ret;
watcher->alive = false;
pthread_join(watcher->thread, &ret);
free(watcher);
}
#endif