summaryrefslogtreecommitdiff
path: root/source/fswatcher/fswatcher_windows.cpp
blob: 9c660f418661b834ea78aea5a8f00bcd64528664 (plain)
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
/*
   A small drop-in library for watching the filesystem for changes.

   version 0.1, february, 2015

   Copyright (C) 2015- Fredrik Kihlander

   This software is provided 'as-is', without any express or implied
   warranty.  In no event will the authors be held liable for any damages
   arising from the use of this software.

   Permission is granted to anyone to use this software for any purpose,
   including commercial applications, and to alter it and redistribute it
   freely, subject to the following restrictions:

   1. The origin of this software must not be misrepresented; you must not
      claim that you wrote the original software. If you use this software
      in a product, an acknowledgment in the product documentation would be
      appreciated but is not required.
   2. Altered source versions must be plainly marked as such, and must not be
      misrepresented as being the original software.
   3. This notice may not be removed or altered from any source distribution.

   Fredrik Kihlander
*/
#include <fswatcher/fswatcher.h>

#include <stdlib.h> // malloc
#include <windows.h>

#include <stdio.h> // remove

struct fswatcher
{
    fswatcher_allocator* allocator;
    bool recursive;
    bool blocking;

    const char* watch_dir;
    size_t watch_dir_len;

    HANDLE     directory;
    OVERLAPPED overlapped;

    DWORD read_buffer[2048]; // hmmmm.
};

static void* fswatcher_default_realloc( fswatcher_allocator*, void* ptr, size_t, size_t new_size )
{
	return realloc( ptr, new_size );
}

static void fswatcher_default_free( fswatcher_allocator*, void* ptr )
{
	free( ptr );
}

static fswatcher_allocator g_fswatcher_default_alloc = { fswatcher_default_realloc, fswatcher_default_free };

static void* fswatcher_realloc( fswatcher_allocator* allocator, void* ptr, size_t old_size, size_t new_size )
{
	return allocator->realloc( allocator, ptr, old_size, new_size );
}

static void fswatcher_free( fswatcher_allocator* allocator, void* ptr )
{
	if( allocator->free )
		allocator->free( allocator, ptr );
}

#define FS_MAKE_CALLBACK( type, src, dst ) handler->callback( handler, (type), (src), (dst) );

static char* fswatcher_build_full_path( fswatcher_t watcher, fswatcher_allocator* allocator, FILE_NOTIFY_INFORMATION* ev )
{
	size_t path_len = (size_t)( ev->FileNameLength / 2 );

	int utf8_len = WideCharToMultiByte( CP_UTF8,       // CodePage
										0,             // dwFlags
										ev->FileName,  // lpWideCharStr
										(int)path_len, // cchWideChar
										nullptr,       // lpMultiByteStr
										0,             // cbMultiByte
										nullptr,       // lpDefaultChar
										nullptr );     // lpUsedDefaultChar
	size_t res_len = watcher->watch_dir_len + utf8_len;

	char* res = (char*)fswatcher_realloc( allocator, 0x0, 0, res_len + 1 );
	strcpy( res, watcher->watch_dir );

	char* file_part = res + watcher->watch_dir_len;

	WideCharToMultiByte( CP_UTF8,       // CodePage
						 0,             // dwFlags
						 ev->FileName,  // lpWideCharStr
						 (int)path_len, // cchWideChar
						 file_part,     // lpMultiByteStr
						 utf8_len,      // cbMultiByte
						 nullptr,       // lpDefaultChar
						 nullptr );     // lpUsedDefaultChar
	res[res_len] = '\0';
	return res;
}

static void fswatcher_begin_read( fswatcher_t watcher )
{
    ::ZeroMemory( &watcher->overlapped, sizeof( watcher->overlapped ) );

    BOOL success = ::ReadDirectoryChangesW( watcher->directory,
                                            watcher->read_buffer,
                                            sizeof( watcher->read_buffer ),
                                            watcher->recursive ? TRUE : FALSE,
                                            FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME,
                                            0x0,
                                            &watcher->overlapped,
                                            0x0 );
    if( !success )
        printf("NOOOOOOOOOOOOOO!");
}

fswatcher_t fswatcher_create( fswatcher_create_flags flags, fswatcher_event_type types, const char* watch_dir, fswatcher_allocator* allocator )
{
    if( allocator == 0x0 )
		allocator = &g_fswatcher_default_alloc;

	fswatcher_t w = (fswatcher_t)fswatcher_realloc( allocator, 0x0, 0, sizeof( fswatcher ) );
    w->allocator = allocator;
    w->watch_dir_len = strlen( watch_dir );
    w->watch_dir = (char*)fswatcher_realloc( w->allocator, 0x0, 0, w->watch_dir_len + 1 );
    strcpy( (char*)w->watch_dir, watch_dir );
    w->recursive = ( flags & FSWATCHER_CREATE_RECURSIVE ) > 0;
    w->blocking  = ( flags & FSWATCHER_CREATE_BLOCKING ) > 0;
    w->directory = ::CreateFile( watch_dir,
                                 FILE_LIST_DIRECTORY,
                                 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                                 NULL, // security descriptor
                                 OPEN_EXISTING, // how to create
                                 FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, // file attributes
                                 NULL ); // file with attributes to copy
    // handle error ...

    fswatcher_begin_read( w );
	return w;
}

void fswatcher_destroy( fswatcher_t watcher )
{
    ::CloseHandle( watcher->directory );
	fswatcher_free( watcher->allocator, watcher );
}

void fswatcher_poll( fswatcher_t watcher, fswatcher_event_handler* handler, fswatcher_allocator* allocator )
{
    DWORD bytes;
    BOOL res = ::GetOverlappedResult( watcher->directory,
                                      &watcher->overlapped,
                                      &bytes,
                                      watcher->blocking );
    if( res != TRUE )
    	return;

    char* move_src = 0x0;

	FILE_NOTIFY_INFORMATION* ev = (FILE_NOTIFY_INFORMATION*)watcher->read_buffer;
	do
	{
		switch(ev->Action)
		{
			case FILE_ACTION_ADDED:
			{
				char* src = fswatcher_build_full_path( watcher, watcher->allocator, ev );
				FS_MAKE_CALLBACK( FSWATCHER_EVENT_CREATE, src, 0x0 );
				fswatcher_free( watcher->allocator, src );
			}
			break;
			case FILE_ACTION_REMOVED:
			{
				char* src = fswatcher_build_full_path( watcher, watcher->allocator, ev );
				FS_MAKE_CALLBACK( FSWATCHER_EVENT_REMOVE, src, 0x0 );
				fswatcher_free( watcher->allocator, src );
			}
			break;
			case FILE_ACTION_MODIFIED:
			{
				char* src = fswatcher_build_full_path( watcher, watcher->allocator, ev );
				FS_MAKE_CALLBACK( FSWATCHER_EVENT_MODIFY, src, 0x0 );
				fswatcher_free( watcher->allocator, src );
			}
			break;
			case FILE_ACTION_RENAMED_OLD_NAME:
			{
				move_src = fswatcher_build_full_path( watcher, watcher->allocator, ev );
			}
			break;
			case FILE_ACTION_RENAMED_NEW_NAME:
			{
				char* dst = fswatcher_build_full_path( watcher, watcher->allocator, ev );
				FS_MAKE_CALLBACK( FSWATCHER_EVENT_MOVE, move_src, dst );
				fswatcher_free( watcher->allocator, move_src );
				fswatcher_free( watcher->allocator, dst );
				move_src = 0x0;
			}
			break;
			default:
				printf("unhandled action %d\n", ev->Action);
		}

		if( ev->NextEntryOffset == 0 )
			break;
		ev = (FILE_NOTIFY_INFORMATION*)((char*)ev + ev->NextEntryOffset);
	}
	while( true );

	if( move_src != 0x0 )
	{
		// how to handle this?
		fswatcher_free( watcher->allocator, move_src );
	}

	fswatcher_begin_read( watcher );
}