File: FXDirWatch.cpp

package info (click to toggle)
gogglesmm 1.2.5-6
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 16,812 kB
  • sloc: cpp: 231,960; ansic: 893; xml: 222; makefile: 33
file content (460 lines) | stat: -rw-r--r-- 15,020 bytes parent folder | download | duplicates (2)
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
/********************************************************************************
*                                                                               *
*           W a t c h   D i r e c t o r i e s   f o r   C h a n g e s           *
*                                                                               *
*********************************************************************************
* Copyright (C) 2006,2022 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This library is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU Lesser General Public License as published by   *
* the Free Software Foundation; either version 3 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* This library is distributed in the hope that it will be useful,               *
* but WITHOUT ANY WARRANTY; without even the implied warranty of                *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
* GNU Lesser General Public License for more details.                           *
*                                                                               *
* You should have received a copy of the GNU Lesser General Public License      *
* along with this program.  If not, see <http://www.gnu.org/licenses/>          *
********************************************************************************/
#include "xincs.h"
#include "fxver.h"
#include "fxdefs.h"
#include "fxchar.h"
#include "fxmath.h"
#include "FXException.h"
#include "FXArray.h"
#include "FXHash.h"
#include "FXMutex.h"
#include "FXStream.h"
#include "FXString.h"
#include "FXSize.h"
#include "FXPoint.h"
#include "FXRectangle.h"
#include "FXEvent.h"
#include "FXStringDictionary.h"
#include "FXDictionary.h"
#include "FXReverseDictionary.h"
#include "FXSettings.h"
#include "FXRegistry.h"
#include "FXPath.h"
#include "FXWindow.h"
#include "FXApp.h"
#include "FXIO.h"
#include "FXStat.h"
#include "FXDirWatch.h"

/*
  Notes:
  - Make this more elegant.
  - See also inotify(7) and fanotify(7).
  - Perhaps only create & add handle to FXApp when first watch is created,
    and remove it when last watch is removed.
  - For inotify_event, the following may be of some importance:

      "The name field is only present when an event is returned for a file
      inside a watched directory; it identifies the file pathname relative
      to the watched directory. This pathname is null-terminated, and may
      include further null bytes to align subsequent reads to a suitable
      address boundary."

  - In other words, if watching a file then there is no name string in the
    inotify_event!!

  - We need a fallback version that uses timers.
*/


// Maximum message size
#define MAXMESSAGE 8192

// Bad handle value
#ifdef WIN32
#define BadHandle  INVALID_HANDLE_VALUE
#else
#define BadHandle  -1
#endif


// Test
#undef HAVE_INOTIFY_INIT1


using namespace FX;

/*******************************************************************************/

namespace FX {


// Map
FXDEFMAP(FXDirWatch) FXDirWatchMap[]={
  FXMAPFUNC(SEL_IO_READ,FXDirWatch::ID_CHANGE,FXDirWatch::onMessage),
  FXMAPFUNC(SEL_TIMEOUT,FXDirWatch::ID_CHANGE,FXDirWatch::onMessage)
  };


// Object implementation
FXIMPLEMENT(FXDirWatch,FXObject,FXDirWatchMap,ARRAYNUMBER(FXDirWatchMap));


// Add handler to application
FXDirWatch::FXDirWatch(FXApp* a,FXObject* tgt,FXSelector sel):app(a),hnd(BadHandle),timestamp(0),target(tgt),message(sel){
  FXTRACE((1,"FXDirWatch::FXDirWatch(%p,%p,%d)\n",a,tgt,sel));
  }


// Remove handler from application
FXDirWatch::~FXDirWatch(){
  FXTRACE((1,"FXDirWatch::~FXDirWatch\n"));
  clearAll();
  app=(FXApp*)-1L;
  target=(FXObject*)-1L;
  }


#if defined(WIN32)  /////////////////////////////////////////////////////////////


// Event filter flags
const FXuint FILTER=FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE;


// FIXME important note: watches contents of directory, not a file.
// FIXME also, does not watch directory itself.
// FIXME thus, to watch directory /path/dir, you may need to watch /path AND /path/dir,
// FIXME or watch /path with recursive option....

// Add path to watch; return true if added
FXbool FXDirWatch::addWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::addWatch(%s)\n",path.text()));
  if(!pathToHandle.has(path)){
#if defined(UNICODE)
    FXnchar unifile[MAXPATHLEN];
    utf2ncs(unifile,path.text(),MAXPATHLEN);
    FXuint attrs=::GetFileAttributesW(unifile);
    if((attrs!=INVALID_FILE_ATTRIBUTES) && (attrs&FILE_ATTRIBUTE_DIRECTORY)){
      HANDLE h=::FindFirstChangeNotificationW(unifile,false,FILTER);
#else
    FXuint attrs=::GetFileAttributesA(path.text());
    if((attrs!=INVALID_FILE_ATTRIBUTES) && (attrs&FILE_ATTRIBUTE_DIRECTORY)){
      HANDLE h=::FindFirstChangeNotificationA(path.text(),false,FILTER);
#endif
      if(h!=BadHandle){
        app->addInput(this,ID_CHANGE,h,INPUT_READ,(FXptr)h);
        pathToHandle[path]=(FXptr)h;
        handleToPath[(FXptr)h]=path;
        FXTRACE((1,"%s -> %d\n",path.text(),h));
        return true;
        }
      }
    }
  return false;
  }


// Remove path to watch; return true if removed
FXbool FXDirWatch::remWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::remWatch(%s)\n",path.text()));
  if(pathToHandle.has(path)){
    HANDLE h=(HANDLE)pathToHandle[path];
    pathToHandle.remove(path);
    handleToPath.remove((FXptr)h);
    app->removeInput(h,INPUT_READ);
    if(FindCloseChangeNotification(h)!=0){
      FXTRACE((1,"%s -> %d\n",path.text(),h));
      return true;
      }
    }
  return false;
  }


// Clear all watches
FXbool FXDirWatch::clearAll(){
  FXTRACE((1,"FXDirWatch::clearAll\n"));
  if(pathToHandle.used()!=0){
    for(FXint i=0; i<pathToHandle.no(); ++i){
      if(!pathToHandle.empty(i)){
        HANDLE h=(HANDLE)pathToHandle.data(i);
        app->removeInput(h,INPUT_READ);
        FindCloseChangeNotification(h);
        }
      }
    pathToHandle.clear();
    handleToPath.clear();
    return true;
    }
  return false;
  }


// Fire signal message to target
long FXDirWatch::onMessage(FXObject*,FXSelector,void* ptr){
  FXTRACE((1,"FXDirWatch::onMessage()\n"));
  FXString pathname;
  HANDLE h=(HANDLE)ptr;
  FindNextChangeNotification(h);
  pathname=handleToPath[(FXptr)h];
  FXTRACE((2,"pathname=\"%s\"\n",pathname.text()));
#if 0
BOOL ReadDirectoryChangesW(
  HANDLE                          hDirectory,
  LPVOID                          lpBuffer,
  DWORD                           nBufferLength,
  BOOL                            bWatchSubtree,
  DWORD                           dwNotifyFilter,
  LPDWORD                         lpBytesReturned,
  LPOVERLAPPED                    lpOverlapped,
  LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine
  );

struct FILE_NOTIFY_INFORMATION {
  DWORD NextEntryOffset;
  DWORD Action;
  DWORD FileNameLength;
  WCHAR FileName[1];
  };

  USES_CONVERSION;
  HANDLE hDir = CreateFile("c:\\Folder", // pointer to the file name
    FILE_LIST_DIRECTORY,                // access (read-write) mode
    FILE_SHARE_READ|FILE_SHARE_DELETE,  // share mode
    NULL, // security descriptor
    OPEN_EXISTING, // how to create
    FILE_FLAG_BACKUP_SEMANTICS, // file attributes
    NULL // file with attributes to copy
  );

  FILE_NOTIFY_INFORMATION Buffer[1024];
  DWORD BytesReturned;
  while( ReadDirectoryChangesW(hDir, // handle to directory
     &Buffer, // read results buffer
     sizeof(Buffer), // length of buffer
     TRUE, // monitoring option
     FILE_NOTIFY_CHANGE_SECURITY|FILE_NOTIFY_CHANGE_CREATION|FILE_NOTIFY_CHANGE_LAST_ACCESS|FILE_NOTIFY_CHANGE_LAST_WRITE|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_FILE_NAME, // filter conditions
     &BytesReturned, // bytes returned
     NULL, // overlapped buffer
     NULL)){ // completion routine
       ...
   }
  ::CloseHandle(hDir);
#endif

  return 1;
  }


#elif defined(HAVE_INOTIFY_INIT1)  //////////////////////////////////////////////


// Event filter flags
const FXuint FILTER_DIRS=IN_ATTRIB|IN_DELETE_SELF|IN_MOVE|IN_CREATE|IN_DELETE;
const FXuint FILTER_FILE=IN_ATTRIB|IN_DELETE_SELF|IN_MOVE|IN_MODIFY|IN_MOVE_SELF;


// Add path to watch; return true if added
FXbool FXDirWatch::addWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::addWatch(%s)\n",path.text()));
  if(!pathToHandle.has(path)){
    FXStat stat;
    if(FXStat::statFile(path,stat)){
      if(pathToHandle.used()==0){
        hnd=::inotify_init1(IN_CLOEXEC);  //hnd=::inotify_init1(IN_CLOEXEC|O_NONBLOCK);
        if(hnd!=BadHandle){
          getApp()->addInput(this,ID_CHANGE,hnd,INPUT_READ);
          }
        }
      if(hnd!=BadHandle){
        FXuint mask=stat.isDirectory()?FILTER_DIRS:FILTER_FILE;
        FXInputHandle h=::inotify_add_watch(hnd,path.text(),mask);
        if(h!=BadHandle){
          pathToHandle[path]=(FXptr)(FXival)h;
          handleToPath[(FXptr)(FXival)h]=path;
          FXTRACE((1,"FXDirWatch::addWatch(%s) -> %d\n",path.text(),h));
          return true;
          }
        }
      }
    }
  return false;
  }


// Remove path to watch; return true if removed
FXbool FXDirWatch::remWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::remWatch(%s)\n",path.text()));
  if(pathToHandle.has(path)){
    if(hnd!=BadHandle){
      FXInputHandle h=(FXival)pathToHandle[path];
      pathToHandle.remove(path);
      handleToPath.remove((FXptr)(FXival)h);
      if(::inotify_rm_watch(hnd,h)!=BadHandle){
        FXTRACE((1,"FXDirWatch::remWatch(%s) -> %d\n",path.text(),h));
        if(pathToHandle.used()==0){
          app->removeInput(hnd,INPUT_READ);
          ::close(hnd);
          hnd=BadHandle;
          }
        return true;
        }
      }
    }
  return false;
  }


// Clear all watches
FXbool FXDirWatch::clearAll(){
  FXTRACE((1,"FXDirWatch::clearAll\n"));
  if(pathToHandle.used()!=0){
    for(FXint i=0; i<pathToHandle.no(); ++i){
      if(!pathToHandle.empty(i)){
        ::inotify_rm_watch(hnd,(FXival)pathToHandle.data(i));
        }
      }
    pathToHandle.clear();
    handleToPath.clear();
    app->removeInput(hnd,INPUT_READ);
    ::close(hnd);
    hnd=BadHandle;
    return true;
    }
  return false;
  }


// Fire signal message to target
long FXDirWatch::onMessage(FXObject*,FXSelector,void*){
  FXTRACE((1,"FXDirWatch::onMessage()\n"));
  FXival expect=0;
  if(0<=::ioctl(hnd,FIONREAD,&expect) && 0<expect){
    FXTRACE((1,"expect=%ld\n",expect));
    FXchar *ptr;
    if(allocElms(ptr,expect)){
      FXival actual=::read(hnd,ptr,expect);
      FXTRACE((1,"actual=%ld\n",actual));
      if(0<actual){
        FXchar *end=ptr+actual;
        FXchar *pne=ptr;
        while(pne<end){
          inotify_event* ne=(inotify_event*)pne;
          FXString pathname=FXPath::absolute(handleToPath[(FXptr)(FXival)ne->wd],ne->name);
          FXTRACE((2,"wd=%d mask=%x cookie=%u len=%u name=\"%s\" pathname=\"%s\"\n",ne->wd,ne->mask,ne->cookie,ne->len,ne->name,pathname.text()));
          if(ne->mask&IN_ACCESS)        FXTRACE((2,"IN_ACCESS "));
          if(ne->mask&IN_ATTRIB)        FXTRACE((2,"IN_ATTRIB "));
          if(ne->mask&IN_CLOSE_NOWRITE) FXTRACE((2,"IN_CLOSE_NOWRITE "));
          if(ne->mask&IN_CLOSE_WRITE)   FXTRACE((2,"IN_CLOSE_WRITE "));
          if(ne->mask&IN_CREATE)        FXTRACE((2,"IN_CREATE "));
          if(ne->mask&IN_DELETE)        FXTRACE((2,"IN_DELETE "));
          if(ne->mask&IN_DELETE_SELF)   FXTRACE((2,"IN_DELETE_SELF "));
          if(ne->mask&IN_IGNORED)       FXTRACE((2,"IN_IGNORED "));
          if(ne->mask&IN_ISDIR)         FXTRACE((2,"IN_ISDIR "));
          if(ne->mask&IN_MODIFY)        FXTRACE((2,"IN_MODIFY "));
          if(ne->mask&IN_MOVE_SELF)     FXTRACE((2,"IN_MOVE_SELF "));
          if(ne->mask&IN_MOVED_FROM)    FXTRACE((2,"IN_MOVED_FROM "));
          if(ne->mask&IN_MOVED_TO)      FXTRACE((2,"IN_MOVED_TO "));
          if(ne->mask&IN_OPEN)          FXTRACE((2,"IN_OPEN "));
          if(ne->mask&IN_Q_OVERFLOW)    FXTRACE((2,"IN_Q_OVERFLOW "));
          if(ne->mask&IN_UNMOUNT)       FXTRACE((2,"IN_UNMOUNT "));
          FXTRACE((2,"\n"));
          if(ne->mask&(IN_MOVED_TO|IN_CREATE)){
            FXTRACE((1,"SEL_INSERTED \"%s\"\n",pathname.text()));
            }
          else if(ne->mask&(IN_DELETE|IN_MOVED_FROM)){
            FXTRACE((1,"SEL_DELETED \"%s\"\n",pathname.text()));
            }
          else if(ne->mask&(IN_ATTRIB)){
            FXTRACE((1,"SEL_CHANGED \"%s\"\n",pathname.text()));
            }
          pne+=sizeof(inotify_event)+ne->len;
          }
        }
      freeElms(ptr);
      }
    }
  return 1;
  }


#else  //////////////////////////////////////////////////////////////////////////


// Interval between refreshes
const FXTime REFRESHINTERVAL=1000000000;


// Add path to watch; return true if added
FXbool FXDirWatch::addWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::addWatch(%s)\n",path.text()));
  if(!pathToHandle.has(path)){
    FXStat stat;
    if(FXStat::statFile(path,stat)){
      if(pathToHandle.used()==0){
        getApp()->addTimeout(this,ID_CHANGE,REFRESHINTERVAL);
        }
      pathToHandle[path]=(FXptr)(stat.isFile()?1L:2L);
      return true;
      }
    }
  return false;
  }


// Remove path to watch; return true if removed
FXbool FXDirWatch::remWatch(const FXString& path){
  FXTRACE((1,"FXDirWatch::remWatch(%s)\n",path.text()));
  if(pathToHandle.has(path)){
    pathToHandle.remove(path);
    if(pathToHandle.used()==0){
      getApp()->removeTimeout(this,ID_CHANGE);
      }
    return true;
    }
  return false;
  }


// Clear all watches
FXbool FXDirWatch::clearAll(){
  FXTRACE((1,"FXDirWatch::clearAll()\n"));
  if(pathToHandle.used()!=0){
    pathToHandle.clear();
    getApp()->removeTimeout(this,ID_CHANGE);
    return true;
    }
  return false;
  }


// Fire signal message to target
long FXDirWatch::onMessage(FXObject*,FXSelector,void*){
  FXTRACE((1,"FXDirWatch::onMessage()\n"));
  if(pathToHandle.used()!=0){
    FXTime newstamp=0;
    for(FXint i=0; i<pathToHandle.no(); ++i){
      if(!pathToHandle.empty(i)){
        FXStat stat;
        if(FXStat::statFile(pathToHandle.key(i),stat)){
          FXTime time=stat.modified();
          if(newstamp<time) newstamp=time;
          if(timestamp<time){
            FXTRACE((1,"SEL_CHANGED \"%s\"\n",pathToHandle.key(i).text()));
            }
          }
        else{
          FXTRACE((1,"SEL_DELETED \"%s\"\n",pathToHandle.key(i).text()));
          }
        }
      }
    timestamp=newstamp;
    getApp()->addTimeout(this,ID_CHANGE,REFRESHINTERVAL);
    }
  return 1;
  }


#endif  /////////////////////////////////////////////////////////////////////////

}