-
Notifications
You must be signed in to change notification settings - Fork 21
/
dmon.h
1748 lines (1504 loc) · 59.2 KB
/
dmon.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
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __DMON_H__
#define __DMON_H__
//
// Copyright 2023 Sepehr Taghdisian (septag@github). All rights reserved.
// License: https://github.com/septag/dmon#license-bsd-2-clause
//
// Portable directory monitoring library
// watches directories for file or directory changes.
//
// Usage:
// define DMON_IMPL and include this file to use it:
// #define DMON_IMPL
// #include "dmon.h"
//
// dmon_init():
// Call this once at the start of your program.
// This will start a low-priority monitoring thread
// dmon_deinit():
// Call this when your work with dmon is finished, usually on program terminate
// This will free resources and stop the monitoring thread
// dmon_watch:
// Watch for directories
// You can watch multiple directories by calling this function multiple times
// rootdir: root directory to monitor
// watch_cb: callback function to receive events.
// NOTE that this function is called from another thread, so you should
// beware of data races in your application when accessing data within this
// callback
// flags: watch flags, see dmon_watch_flags_t
// user_data: user pointer that is passed to callback function
// Returns the Id of the watched directory after successful call, or returns Id=0 if error
// dmon_unwatch:
// Remove the directory from watch list
//
// see test.c for the basic example
//
// Configuration:
// You can customize some low-level functionality like malloc and logging by overriding macros:
//
// DMON_MALLOC, DMON_FREE, DMON_REALLOC:
// define these macros to override memory allocations
// default is 'malloc', 'free' and 'realloc'
// DMON_ASSERT:
// define this to provide your own assert
// default is 'assert'
// DMON_LOG_ERROR:
// define this to provide your own logging mechanism
// default implementation logs to stdout and breaks the program
// DMON_LOG_DEBUG
// define this to provide your own extra debug logging mechanism
// default implementation logs to stdout in DEBUG and does nothing in other builds
// DMON_API_DECL, DMON_API_IMPL
// define these to provide your own API declarations. (for example: static)
// default is nothing (which is extern in C language )
// DMON_MAX_PATH
// Maximum size of path characters
// default is 260 characters
// DMON_MAX_WATCHES
// Maximum number of watch directories
// default is 64
// DMON_SLEEP_INTERVAL
// Number of milliseconds to pause between polling for file changes
// default is 10 ms
//
// TODO:
// - Use FSEventStreamSetDispatchQueue instead of FSEventStreamScheduleWithRunLoop on MacOS
// - DMON_WATCHFLAGS_FOLLOW_SYMLINKS does not resolve files
// - implement DMON_WATCHFLAGS_OUTOFSCOPE_LINKS
// - implement DMON_WATCHFLAGS_IGNORE_DIRECTORIES
//
// History:
// 1.0.0 First version. working Win32/Linux backends
// 1.1.0 MacOS backend
// 1.1.1 Minor fixes, eliminate gcc/clang warnings with -Wall
// 1.1.2 Eliminate some win32 dead code
// 1.1.3 Fixed select not resetting causing high cpu usage on linux
// 1.2.1 inotify (linux) fixes and improvements, added extra functionality header for linux
// to manually add/remove directories manually to the watch handle, in case of large file sets
// 1.2.2 Name refactoring
// 1.3.0 Fixing bugs and proper watch/unwatch handles with freelists. Lower memory consumption, especially on Windows backend
// 1.3.1 Fix in MacOS event grouping
#include <stdbool.h>
#include <stdint.h>
#ifndef DMON_API_DECL
# define DMON_API_DECL
#endif
#ifndef DMON_API_IMPL
# define DMON_API_IMPL
#endif
typedef struct { uint32_t id; } dmon_watch_id;
// Pass these flags to `dmon_watch`
typedef enum dmon_watch_flags_t {
DMON_WATCHFLAGS_RECURSIVE = 0x1, // monitor all child directories
DMON_WATCHFLAGS_FOLLOW_SYMLINKS = 0x2, // resolve symlinks (linux only)
DMON_WATCHFLAGS_OUTOFSCOPE_LINKS = 0x4, // TODO: not implemented yet
DMON_WATCHFLAGS_IGNORE_DIRECTORIES = 0x8 // TODO: not implemented yet
} dmon_watch_flags;
// Action is what operation performed on the file. this value is provided by watch callback
typedef enum dmon_action_t {
DMON_ACTION_CREATE = 1,
DMON_ACTION_DELETE,
DMON_ACTION_MODIFY,
DMON_ACTION_MOVE
} dmon_action;
#ifdef __cplusplus
extern "C" {
#endif
DMON_API_DECL void dmon_init(void);
DMON_API_DECL void dmon_deinit(void);
DMON_API_DECL dmon_watch_id dmon_watch(const char* rootdir,
void (*watch_cb)(dmon_watch_id watch_id, dmon_action action,
const char* rootdir, const char* filepath,
const char* oldfilepath, void* user),
uint32_t flags, void* user_data);
DMON_API_DECL void dmon_unwatch(dmon_watch_id id);
#ifdef __cplusplus
}
#endif
#ifdef DMON_IMPL
#define DMON_OS_WINDOWS 0
#define DMON_OS_MACOS 0
#define DMON_OS_LINUX 0
#if defined(_WIN32) || defined(_WIN64)
# undef DMON_OS_WINDOWS
# define DMON_OS_WINDOWS 1
#elif defined(__linux__)
# undef DMON_OS_LINUX
# define DMON_OS_LINUX 1
#elif defined(__ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__)
# undef DMON_OS_MACOS
# define DMON_OS_MACOS __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__
#else
# define DMON_OS 0
# error "unsupported platform"
#endif
#if DMON_OS_WINDOWS
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# ifndef NOMINMAX
# define NOMINMAX
# endif
# include <windows.h>
# include <intrin.h>
# ifdef _MSC_VER
# pragma intrinsic(_InterlockedExchange)
# endif
#elif DMON_OS_LINUX
# ifndef __USE_MISC
# define __USE_MISC
# endif
# include <dirent.h>
# include <errno.h>
# include <fcntl.h>
# include <linux/limits.h>
# include <pthread.h>
# include <sys/inotify.h>
# include <sys/stat.h>
# include <sys/time.h>
# include <time.h>
# include <unistd.h>
# include <stdlib.h>
#elif DMON_OS_MACOS
# include <pthread.h>
# include <CoreServices/CoreServices.h>
# include <sys/time.h>
# include <sys/stat.h>
# include <dispatch/dispatch.h>
#endif
#ifndef DMON_MALLOC
# include <stdlib.h>
# define DMON_MALLOC(size) malloc(size)
# define DMON_FREE(ptr) free(ptr)
# define DMON_REALLOC(ptr, size) realloc(ptr, size)
#endif
#ifndef DMON_ASSERT
# include <assert.h>
# define DMON_ASSERT(e) assert(e)
#endif
#ifndef DMON_LOG_ERROR
# include <stdio.h>
# define DMON_LOG_ERROR(s) do { puts(s); DMON_ASSERT(0); } while(0)
#endif
#ifndef DMON_LOG_DEBUG
# ifndef NDEBUG
# include <stdio.h>
# define DMON_LOG_DEBUG(s) do { puts(s); } while(0)
# else
# define DMON_LOG_DEBUG(s)
# endif
#endif
#ifndef DMON_MAX_WATCHES
# define DMON_MAX_WATCHES 64
#endif
#ifndef DMON_MAX_PATH
# define DMON_MAX_PATH 260
#endif
#define _DMON_UNUSED(x) (void)(x)
#ifndef _DMON_PRIVATE
# if defined(__GNUC__) || defined(__clang__)
# define _DMON_PRIVATE __attribute__((unused)) static
# else
# define _DMON_PRIVATE static
# endif
#endif
#ifndef DMON_SLEEP_INTERVAL
# define DMON_SLEEP_INTERVAL 10
#endif
#include <string.h>
#ifndef _DMON_LOG_ERRORF
# define _DMON_LOG_ERRORF(str, ...) do { char msg[512]; snprintf(msg, sizeof(msg), str, __VA_ARGS__); DMON_LOG_ERROR(msg); } while(0);
#endif
#ifndef _DMON_LOG_DEBUGF
# define _DMON_LOG_DEBUGF(str, ...) do { char msg[512]; snprintf(msg, sizeof(msg), str, __VA_ARGS__); DMON_LOG_DEBUG(msg); } while(0);
#endif
#ifndef _dmon_min
# define _dmon_min(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef _dmon_max
# define _dmon_max(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef _dmon_swap
# define _dmon_swap(a, b, _type) \
do { \
_type tmp = a; \
a = b; \
b = tmp; \
} while (0)
#endif
#ifndef _dmon_make_id
# ifdef __cplusplus
# define _dmon_make_id(id) {id}
# else
# define _dmon_make_id(id) (dmon_watch_id) {id}
# endif
#endif // _dmon_make_id
_DMON_PRIVATE bool _dmon_isrange(char ch, char from, char to)
{
return (uint8_t)(ch - from) <= (uint8_t)(to - from);
}
_DMON_PRIVATE bool _dmon_isupperchar(char ch)
{
return _dmon_isrange(ch, 'A', 'Z');
}
_DMON_PRIVATE char _dmon_tolowerchar(char ch)
{
return ch + (_dmon_isupperchar(ch) ? 0x20 : 0);
}
_DMON_PRIVATE char* _dmon_tolower(char* dst, int dst_sz, const char* str)
{
int offset = 0;
int dst_max = dst_sz - 1;
while (*str && offset < dst_max) {
dst[offset++] = _dmon_tolowerchar(*str);
++str;
}
dst[offset] = '\0';
return dst;
}
_DMON_PRIVATE char* _dmon_strcpy(char* dst, int dst_sz, const char* src)
{
DMON_ASSERT(dst);
DMON_ASSERT(src);
const int32_t len = (int32_t)strlen(src);
const int32_t _max = dst_sz - 1;
const int32_t num = (len < _max ? len : _max);
memcpy(dst, src, num);
dst[num] = '\0';
return dst;
}
_DMON_PRIVATE char* _dmon_unixpath(char* dst, int size, const char* path)
{
size_t len = strlen(path), i;
len = _dmon_min(len, (size_t)size - 1);
for (i = 0; i < len; i++) {
if (path[i] != '\\')
dst[i] = path[i];
else
dst[i] = '/';
}
dst[len] = '\0';
return dst;
}
#if DMON_OS_LINUX || DMON_OS_MACOS
_DMON_PRIVATE char* _dmon_strcat(char* dst, int dst_sz, const char* src)
{
int len = (int)strlen(dst);
return _dmon_strcpy(dst + len, dst_sz - len, src);
}
#endif // DMON_OS_LINUX || DMON_OS_MACOS
// stretchy buffer: https://github.com/nothings/stb/blob/master/stretchy_buffer.h
#define stb_sb_free(a) ((a) ? DMON_FREE(stb__sbraw(a)),0 : 0)
#define stb_sb_push(a,v) (stb__sbmaybegrow(a,1), (a)[stb__sbn(a)++] = (v))
#define stb_sb_pop(a) (stb__sbn(a)--)
#define stb_sb_count(a) ((a) ? stb__sbn(a) : 0)
#define stb_sb_add(a,n) (stb__sbmaybegrow(a,n), stb__sbn(a)+=(n), &(a)[stb__sbn(a)-(n)])
#define stb_sb_last(a) ((a)[stb__sbn(a)-1])
#define stb_sb_reset(a) ((a) ? (stb__sbn(a) = 0) : 0)
#define stb__sbraw(a) ((int *) (a) - 2)
#define stb__sbm(a) stb__sbraw(a)[0]
#define stb__sbn(a) stb__sbraw(a)[1]
#define stb__sbneedgrow(a,n) ((a)==0 || stb__sbn(a)+(n) >= stb__sbm(a))
#define stb__sbmaybegrow(a,n) (stb__sbneedgrow(a,(n)) ? stb__sbgrow(a,n) : 0)
#define stb__sbgrow(a,n) (*((void **)&(a)) = stb__sbgrowf((a), (n), sizeof(*(a))))
static void * stb__sbgrowf(void *arr, int increment, int itemsize)
{
int dbl_cur = arr ? 2*stb__sbm(arr) : 0;
int min_needed = stb_sb_count(arr) + increment;
int m = dbl_cur > min_needed ? dbl_cur : min_needed;
int *p = (int *) DMON_REALLOC(arr ? stb__sbraw(arr) : 0, itemsize * m + sizeof(int)*2);
if (p) {
if (!arr)
p[1] = 0;
p[0] = m;
return p+2;
} else {
return (void *) (2*sizeof(int)); // try to force a NULL pointer exception later
}
}
// watcher callback (same as dmon.h's declaration)
typedef void (_dmon_watch_cb)(dmon_watch_id, dmon_action, const char*, const char*, const char*, void*);
#if DMON_OS_WINDOWS
// ---------------------------------------------------------------------------------------------------------------------
// @Windows
// IOCP
#ifdef UNICODE
# define _DMON_WINAPI_STR(name, size) wchar_t _##name[size]; MultiByteToWideChar(CP_UTF8, 0, name, -1, _##name, size)
#else
# define _DMON_WINAPI_STR(name, size) const char* _##name = name
#endif
typedef struct dmon__win32_event {
char filepath[DMON_MAX_PATH];
DWORD action;
dmon_watch_id watch_id;
bool skip;
} dmon__win32_event;
typedef struct dmon__watch_state {
dmon_watch_id id;
OVERLAPPED overlapped;
HANDLE dir_handle;
uint8_t buffer[64512]; // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365465(v=vs.85).aspx
DWORD notify_filter;
_dmon_watch_cb* watch_cb;
uint32_t watch_flags;
void* user_data;
char rootdir[DMON_MAX_PATH];
char old_filepath[DMON_MAX_PATH];
} dmon__watch_state;
typedef struct dmon__state {
int num_watches;
dmon__watch_state* watches[DMON_MAX_WATCHES];
int freelist[DMON_MAX_WATCHES];
HANDLE thread_handle;
CRITICAL_SECTION mutex;
volatile LONG modify_watches;
dmon__win32_event* events;
bool quit;
} dmon__state;
static bool _dmon_init;
static dmon__state _dmon;
_DMON_PRIVATE bool _dmon_refresh_watch(dmon__watch_state* watch)
{
return ReadDirectoryChangesW(watch->dir_handle, watch->buffer, sizeof(watch->buffer),
(watch->watch_flags & DMON_WATCHFLAGS_RECURSIVE) ? TRUE : FALSE,
watch->notify_filter, NULL, &watch->overlapped, NULL) != 0;
}
_DMON_PRIVATE void _dmon_unwatch(dmon__watch_state* watch)
{
CancelIo(watch->dir_handle);
CloseHandle(watch->overlapped.hEvent);
CloseHandle(watch->dir_handle);
}
_DMON_PRIVATE void _dmon_win32_process_events(void)
{
int i, c;
for (i = 0, c = stb_sb_count(_dmon.events); i < c; i++) {
dmon__win32_event* ev = &_dmon.events[i];
if (ev->skip) {
continue;
}
if (ev->action == FILE_ACTION_MODIFIED || ev->action == FILE_ACTION_ADDED) {
// remove duplicate modifies on a single file
int j;
for (j = i + 1; j < c; j++) {
dmon__win32_event* check_ev = &_dmon.events[j];
if (check_ev->action == FILE_ACTION_MODIFIED &&
strcmp(ev->filepath, check_ev->filepath) == 0) {
check_ev->skip = true;
}
}
}
}
// trigger user callbacks
for (i = 0, c = stb_sb_count(_dmon.events); i < c; i++) {
dmon__win32_event* ev = &_dmon.events[i];
if (ev->skip) {
continue;
}
dmon__watch_state* watch = _dmon.watches[ev->watch_id.id - 1];
if(watch == NULL || watch->watch_cb == NULL) {
continue;
}
switch (ev->action) {
case FILE_ACTION_ADDED:
watch->watch_cb(ev->watch_id, DMON_ACTION_CREATE, watch->rootdir, ev->filepath, NULL,
watch->user_data);
break;
case FILE_ACTION_MODIFIED:
watch->watch_cb(ev->watch_id, DMON_ACTION_MODIFY, watch->rootdir, ev->filepath, NULL,
watch->user_data);
break;
case FILE_ACTION_RENAMED_OLD_NAME: {
// find the first occurrence of the NEW_NAME
// this is somewhat API flaw that we have no reference for relating old and new files
int j;
for (j = i + 1; j < c; j++) {
dmon__win32_event* check_ev = &_dmon.events[j];
if (check_ev->action == FILE_ACTION_RENAMED_NEW_NAME) {
watch->watch_cb(check_ev->watch_id, DMON_ACTION_MOVE, watch->rootdir,
check_ev->filepath, ev->filepath, watch->user_data);
break;
}
}
} break;
case FILE_ACTION_REMOVED:
watch->watch_cb(ev->watch_id, DMON_ACTION_DELETE, watch->rootdir, ev->filepath, NULL,
watch->user_data);
break;
}
}
stb_sb_reset(_dmon.events);
}
_DMON_PRIVATE DWORD WINAPI _dmon_thread(LPVOID arg)
{
_DMON_UNUSED(arg);
HANDLE wait_handles[DMON_MAX_WATCHES];
dmon__watch_state* watch_states[DMON_MAX_WATCHES];
SYSTEMTIME starttm;
GetSystemTime(&starttm);
uint64_t msecs_elapsed = 0;
while (!_dmon.quit) {
int i;
if (_dmon.modify_watches || !TryEnterCriticalSection(&_dmon.mutex)) {
Sleep(DMON_SLEEP_INTERVAL);
continue;
}
if (_dmon.num_watches == 0) {
Sleep(DMON_SLEEP_INTERVAL);
LeaveCriticalSection(&_dmon.mutex);
continue;
}
for (i = 0; i < DMON_MAX_WATCHES; i++) {
if (_dmon.watches[i]) {
dmon__watch_state* watch = _dmon.watches[i];
watch_states[i] = watch;
wait_handles[i] = watch->overlapped.hEvent;
}
}
DWORD wait_result = WaitForMultipleObjects(_dmon.num_watches, wait_handles, FALSE, 10);
DMON_ASSERT(wait_result != WAIT_FAILED);
if (wait_result != WAIT_TIMEOUT) {
dmon__watch_state* watch = watch_states[wait_result - WAIT_OBJECT_0];
DMON_ASSERT(HasOverlappedIoCompleted(&watch->overlapped));
DWORD bytes;
if (GetOverlappedResult(watch->dir_handle, &watch->overlapped, &bytes, FALSE)) {
char filepath[DMON_MAX_PATH];
PFILE_NOTIFY_INFORMATION notify;
size_t offset = 0;
if (bytes == 0) {
_dmon_refresh_watch(watch);
LeaveCriticalSection(&_dmon.mutex);
continue;
}
do {
notify = (PFILE_NOTIFY_INFORMATION)&watch->buffer[offset];
int count = WideCharToMultiByte(CP_UTF8, 0, notify->FileName,
notify->FileNameLength / sizeof(WCHAR),
filepath, DMON_MAX_PATH - 1, NULL, NULL);
filepath[count] = TEXT('\0');
_dmon_unixpath(filepath, sizeof(filepath), filepath);
// TODO: ignore directories if flag is set
if (stb_sb_count(_dmon.events) == 0) {
msecs_elapsed = 0;
}
dmon__win32_event wev = { { 0 }, notify->Action, watch->id, false };
_dmon_strcpy(wev.filepath, sizeof(wev.filepath), filepath);
stb_sb_push(_dmon.events, wev);
offset += notify->NextEntryOffset;
} while (notify->NextEntryOffset > 0);
if (!_dmon.quit) {
_dmon_refresh_watch(watch);
}
}
} // if (WaitForMultipleObjects)
SYSTEMTIME tm;
GetSystemTime(&tm);
LONG dt =(tm.wSecond - starttm.wSecond) * 1000 + (tm.wMilliseconds - starttm.wMilliseconds);
starttm = tm;
msecs_elapsed += dt;
if (msecs_elapsed > 100 && stb_sb_count(_dmon.events) > 0) {
_dmon_win32_process_events();
msecs_elapsed = 0;
}
LeaveCriticalSection(&_dmon.mutex);
}
return 0;
}
DMON_API_IMPL void dmon_init(void)
{
DMON_ASSERT(!_dmon_init);
InitializeCriticalSection(&_dmon.mutex);
_dmon.thread_handle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_dmon_thread, NULL, 0, NULL);
DMON_ASSERT(_dmon.thread_handle);
for (int i = 0; i < DMON_MAX_WATCHES; i++)
_dmon.freelist[i] = DMON_MAX_WATCHES - i - 1;
_dmon_init = true;
}
DMON_API_IMPL void dmon_deinit(void)
{
DMON_ASSERT(_dmon_init);
_dmon.quit = true;
if (_dmon.thread_handle != INVALID_HANDLE_VALUE) {
WaitForSingleObject(_dmon.thread_handle, INFINITE);
CloseHandle(_dmon.thread_handle);
}
{
int i;
for (i = 0; i < DMON_MAX_WATCHES; i++) {
if (_dmon.watches[i]) {
_dmon_unwatch(_dmon.watches[i]);
DMON_FREE(_dmon.watches[i]);
}
}
}
DeleteCriticalSection(&_dmon.mutex);
stb_sb_free(_dmon.events);
memset(&_dmon, 0x0, sizeof(_dmon));
_dmon_init = false;
}
DMON_API_IMPL dmon_watch_id dmon_watch(const char* rootdir,
void (*watch_cb)(dmon_watch_id watch_id, dmon_action action,
const char* dirname, const char* filename,
const char* oldname, void* user),
uint32_t flags, void* user_data)
{
DMON_ASSERT(_dmon_init);
DMON_ASSERT(watch_cb);
DMON_ASSERT(rootdir && rootdir[0]);
_InterlockedExchange(&_dmon.modify_watches, 1);
EnterCriticalSection(&_dmon.mutex);
DMON_ASSERT(_dmon.num_watches < DMON_MAX_WATCHES);
if (_dmon.num_watches >= DMON_MAX_WATCHES) {
DMON_LOG_ERROR("Exceeding maximum number of watches");
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
return _dmon_make_id(0);
}
int num_freelist = DMON_MAX_WATCHES - _dmon.num_watches;
int index = _dmon.freelist[num_freelist - 1];
uint32_t id = (uint32_t)(index + 1);
if (_dmon.watches[index] == NULL) {
dmon__watch_state* state = (dmon__watch_state*)DMON_MALLOC(sizeof(dmon__watch_state));
DMON_ASSERT(state);
if (state == NULL) {
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
return _dmon_make_id(0);
}
memset(state, 0x0, sizeof(dmon__watch_state));
_dmon.watches[index] = state;
}
++_dmon.num_watches;
dmon__watch_state* watch = _dmon.watches[index];
watch->id = _dmon_make_id(id);
watch->watch_flags = flags;
watch->watch_cb = watch_cb;
watch->user_data = user_data;
_dmon_strcpy(watch->rootdir, sizeof(watch->rootdir) - 1, rootdir);
_dmon_unixpath(watch->rootdir, sizeof(watch->rootdir), rootdir);
size_t rootdir_len = strlen(watch->rootdir);
if (watch->rootdir[rootdir_len - 1] != '/') {
watch->rootdir[rootdir_len] = '/';
watch->rootdir[rootdir_len + 1] = '\0';
}
_DMON_WINAPI_STR(rootdir, DMON_MAX_PATH);
watch->dir_handle =
CreateFile(_rootdir, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
if (watch->dir_handle != INVALID_HANDLE_VALUE) {
watch->notify_filter = FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_SIZE;
watch->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
DMON_ASSERT(watch->overlapped.hEvent != INVALID_HANDLE_VALUE);
if (!_dmon_refresh_watch(watch)) {
_dmon_unwatch(watch);
DMON_LOG_ERROR("ReadDirectoryChanges failed");
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
return _dmon_make_id(0);
}
} else {
_DMON_LOG_ERRORF("Could not open: %s", rootdir);
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
return _dmon_make_id(0);
}
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
return _dmon_make_id(id);
}
DMON_API_IMPL void dmon_unwatch(dmon_watch_id id)
{
DMON_ASSERT(_dmon_init);
DMON_ASSERT(id.id > 0);
int index = id.id - 1;
DMON_ASSERT(index < DMON_MAX_WATCHES);
DMON_ASSERT(_dmon.watches[index]);
DMON_ASSERT(_dmon.num_watches > 0);
if (_dmon.watches[index]) {
_InterlockedExchange(&_dmon.modify_watches, 1);
EnterCriticalSection(&_dmon.mutex);
_dmon_unwatch(_dmon.watches[index]);
DMON_FREE(_dmon.watches[index]);
_dmon.watches[index] = NULL;
--_dmon.num_watches;
int num_freelist = DMON_MAX_WATCHES - _dmon.num_watches;
_dmon.freelist[num_freelist - 1] = index;
LeaveCriticalSection(&_dmon.mutex);
_InterlockedExchange(&_dmon.modify_watches, 0);
}
}
#elif DMON_OS_LINUX
// ---------------------------------------------------------------------------------------------------------------------
// @Linux
// inotify linux backend
#define _DMON_TEMP_BUFFSIZE ((sizeof(struct inotify_event) + PATH_MAX) * 1024)
typedef struct dmon__watch_subdir {
char rootdir[DMON_MAX_PATH];
} dmon__watch_subdir;
typedef struct dmon__inotify_event {
char filepath[DMON_MAX_PATH];
uint32_t mask;
uint32_t cookie;
dmon_watch_id watch_id;
bool skip;
} dmon__inotify_event;
typedef struct dmon__watch_state {
dmon_watch_id id;
int fd;
uint32_t watch_flags;
_dmon_watch_cb* watch_cb;
void* user_data;
char rootdir[DMON_MAX_PATH];
dmon__watch_subdir* subdirs;
int* wds;
} dmon__watch_state;
typedef struct dmon__state {
dmon__watch_state* watches[DMON_MAX_WATCHES];
int freelist[DMON_MAX_WATCHES];
dmon__inotify_event* events;
int num_watches;
pthread_t thread_handle;
pthread_mutex_t mutex;
bool quit;
} dmon__state;
static bool _dmon_init;
static dmon__state _dmon;
_DMON_PRIVATE void _dmon_watch_recursive(const char* dirname, int fd, uint32_t mask,
bool followlinks, dmon__watch_state* watch)
{
struct dirent* entry;
DIR* dir = opendir(dirname);
DMON_ASSERT(dir);
char watchdir[DMON_MAX_PATH];
while ((entry = readdir(dir)) != NULL) {
bool entry_valid = false;
if (entry->d_type == DT_DIR) {
if (strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) {
_dmon_strcpy(watchdir, sizeof(watchdir), dirname);
_dmon_strcat(watchdir, sizeof(watchdir), entry->d_name);
entry_valid = true;
}
} else if (followlinks && entry->d_type == DT_LNK) {
char linkpath[PATH_MAX];
_dmon_strcpy(watchdir, sizeof(watchdir), dirname);
_dmon_strcat(watchdir, sizeof(watchdir), entry->d_name);
char* r = realpath(watchdir, linkpath);
_DMON_UNUSED(r);
DMON_ASSERT(r);
_dmon_strcpy(watchdir, sizeof(watchdir), linkpath);
entry_valid = true;
}
// add sub-directory to watch dirs
if (entry_valid) {
int watchdir_len = (int)strlen(watchdir);
if (watchdir[watchdir_len - 1] != '/') {
watchdir[watchdir_len] = '/';
watchdir[watchdir_len + 1] = '\0';
}
int wd = inotify_add_watch(fd, watchdir, mask);
_DMON_UNUSED(wd);
DMON_ASSERT(wd != -1);
dmon__watch_subdir subdir;
_dmon_strcpy(subdir.rootdir, sizeof(subdir.rootdir), watchdir);
if (strstr(subdir.rootdir, watch->rootdir) == subdir.rootdir) {
_dmon_strcpy(subdir.rootdir, sizeof(subdir.rootdir), watchdir + strlen(watch->rootdir));
}
stb_sb_push(watch->subdirs, subdir);
stb_sb_push(watch->wds, wd);
// recurse
_dmon_watch_recursive(watchdir, fd, mask, followlinks, watch);
}
}
closedir(dir);
}
_DMON_PRIVATE const char* _dmon_find_subdir(const dmon__watch_state* watch, int wd)
{
const int* wds = watch->wds;
int i, c;
for (i = 0, c = stb_sb_count(wds); i < c; i++) {
if (wd == wds[i]) {
return watch->subdirs[i].rootdir;
}
}
return NULL;
}
_DMON_PRIVATE void _dmon_gather_recursive(dmon__watch_state* watch, const char* dirname)
{
struct dirent* entry;
DIR* dir = opendir(dirname);
DMON_ASSERT(dir);
char newdir[DMON_MAX_PATH];
while ((entry = readdir(dir)) != NULL) {
bool entry_valid = false;
bool is_dir = false;
if (strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name, ".") != 0) {
_dmon_strcpy(newdir, sizeof(newdir), dirname);
_dmon_strcat(newdir, sizeof(newdir), entry->d_name);
is_dir = (entry->d_type == DT_DIR);
entry_valid = true;
}
// add sub-directory to watch dirs
if (entry_valid) {
dmon__watch_subdir subdir;
_dmon_strcpy(subdir.rootdir, sizeof(subdir.rootdir), newdir);
if (strstr(subdir.rootdir, watch->rootdir) == subdir.rootdir) {
_dmon_strcpy(subdir.rootdir, sizeof(subdir.rootdir), newdir + strlen(watch->rootdir));
}
dmon__inotify_event dev = { { 0 }, IN_CREATE|(is_dir ? IN_ISDIR : 0U), 0, watch->id, false };
_dmon_strcpy(dev.filepath, sizeof(dev.filepath), subdir.rootdir);
stb_sb_push(_dmon.events, dev);
}
}
closedir(dir);
}
_DMON_PRIVATE void _dmon_inotify_process_events(void)
{
int i, c;
for (i = 0, c = stb_sb_count(_dmon.events); i < c; i++) {
dmon__inotify_event* ev = &_dmon.events[i];
if (ev->skip) {
continue;
}
// remove redundant modify events on a single file
if (ev->mask & IN_MODIFY) {
int j;
for (j = i + 1; j < c; j++) {
dmon__inotify_event* check_ev = &_dmon.events[j];
if ((check_ev->mask & IN_MODIFY) && strcmp(ev->filepath, check_ev->filepath) == 0) {
ev->skip = true;
break;
} else if ((ev->mask & IN_ISDIR) && (check_ev->mask & (IN_ISDIR|IN_MODIFY))) {
// in some cases, particularly when created files under sub directories
// there can be two modify events for a single subdir one with trailing slash and one without
// remove trailing slash from both cases and test
int l1 = (int)strlen(ev->filepath);
int l2 = (int)strlen(check_ev->filepath);
if (ev->filepath[l1-1] == '/') ev->filepath[l1-1] = '\0';
if (check_ev->filepath[l2-1] == '/') check_ev->filepath[l2-1] = '\0';
if (strcmp(ev->filepath, check_ev->filepath) == 0) {
ev->skip = true;
break;
}
}
}
} else if (ev->mask & IN_CREATE) {
int j;
bool loop_break = false;
for (j = i + 1; j < c && !loop_break; j++) {
dmon__inotify_event* check_ev = &_dmon.events[j];
if ((check_ev->mask & IN_MOVED_FROM) && strcmp(ev->filepath, check_ev->filepath) == 0) {
// there is a case where some programs (like gedit):
// when we save, it creates a temp file, and moves it to the file being modified
// search for these cases and remove all of them
int k;
for (k = j + 1; k < c; k++) {
dmon__inotify_event* third_ev = &_dmon.events[k];
if (third_ev->mask & IN_MOVED_TO && check_ev->cookie == third_ev->cookie) {
third_ev->mask = IN_MODIFY; // change to modified
ev->skip = check_ev->skip = true;
loop_break = true;
break;
}
}
} else if ((check_ev->mask & IN_MODIFY) && strcmp(ev->filepath, check_ev->filepath) == 0) {
// Another case is that file is copied. CREATE and MODIFY happens sequentially
// so we ignore MODIFY event
check_ev->skip = true;
}
}
} else if (ev->mask & IN_MOVED_FROM) {
bool move_valid = false;
int j;
for (j = i + 1; j < c; j++) {
dmon__inotify_event* check_ev = &_dmon.events[j];
if (check_ev->mask & IN_MOVED_TO && ev->cookie == check_ev->cookie) {
move_valid = true;
break;
}
}
// in some environments like nautilus file explorer:
// when a file is deleted, it is moved to recycle bin
// so if the destination of the move is not valid, it's probably DELETE
if (!move_valid) {
ev->mask = IN_DELETE;
}
} else if (ev->mask & IN_MOVED_TO) {
bool move_valid = false;
int j;
for (j = 0; j < i; j++) {
dmon__inotify_event* check_ev = &_dmon.events[j];
if (check_ev->mask & IN_MOVED_FROM && ev->cookie == check_ev->cookie) {
move_valid = true;
break;
}
}
// in some environments like nautilus file explorer:
// when a file is deleted, it is moved to recycle bin, on undo it is moved back it
// so if the destination of the move is not valid, it's probably CREATE
if (!move_valid) {
ev->mask = IN_CREATE;
}
} else if (ev->mask & IN_DELETE) {
int j;
for (j = i + 1; j < c; j++) {
dmon__inotify_event* check_ev = &_dmon.events[j];
// if the file is DELETED and then MODIFIED after, just ignore the modify event
if ((check_ev->mask & IN_MODIFY) && strcmp(ev->filepath, check_ev->filepath) == 0) {
check_ev->skip = true;
break;
}
}
}
}
// trigger user callbacks
for (i = 0; i < stb_sb_count(_dmon.events); i++) {
dmon__inotify_event* ev = &_dmon.events[i];
if (ev->skip) {
continue;
}
dmon__watch_state* watch = _dmon.watches[ev->watch_id.id - 1];
if(watch == NULL || watch->watch_cb == NULL) {
continue;
}
if (ev->mask & IN_CREATE) {
if (ev->mask & IN_ISDIR) {
if (watch->watch_flags & DMON_WATCHFLAGS_RECURSIVE) {
char watchdir[DMON_MAX_PATH];
_dmon_strcpy(watchdir, sizeof(watchdir), watch->rootdir);
_dmon_strcat(watchdir, sizeof(watchdir), ev->filepath);
_dmon_strcat(watchdir, sizeof(watchdir), "/");
uint32_t mask = IN_MOVED_TO | IN_CREATE | IN_MOVED_FROM | IN_DELETE | IN_MODIFY;
int wd = inotify_add_watch(watch->fd, watchdir, mask);
_DMON_UNUSED(wd);