source: branches/alilink/WINDOW/AW_inotify.cxx

Last change on this file was 18126, checked in by westram, 5 years ago
File size: 24.0 KB
Line 
1// =============================================================== //
2//                                                                 //
3//   File      : AW_inotify.cxx                                    //
4//   Purpose   : watch file/directory changes                      //
5//                                                                 //
6//   Coded by Ralf Westram (coder@reallysoft.de) in October 2017   //
7//   http://www.arb-home.de/                                       //
8//                                                                 //
9// =============================================================== //
10
11#include "aw_inotify.hxx"
12
13#include "aw_root.hxx"
14#include <arbdbt.h>
15#include <arb_file.h>
16
17#include <unistd.h>
18
19#include <string>
20#include <list>
21#include <set>
22
23using namespace std;
24
25#if defined(LINUX)
26# define USE_INOTIFY
27#endif
28
29#if !defined(USE_INOTIFY)
30# define USE_STATPOLL
31#endif
32
33#if defined(USE_STATPOLL)
34#include <sys/stat.h>
35#endif
36
37#if defined(USE_INOTIFY)
38#include <sys/inotify.h>
39#endif
40
41#if defined(DEBUG)
42// #define TRACE_INOTIFY
43// #define TRACE_INOTIFY_BASIC
44#endif
45
46#if defined(TRACE_INOTIFY)
47#define IF_TRACE_INOTIFY(x) x
48#else // !TRACE_INOTIFY
49#define IF_TRACE_INOTIFY(x)
50#endif
51
52
53typedef set<FileChangedCallback> CallbackList;
54class TrackedFiles;
55
56class TrackedFile {
57    string       file;
58    CallbackList callbacks;
59
60#if defined(USE_INOTIFY)
61    int watch_descriptor;
62#else // USE_STATPOLL
63
64    mutable int lastModtime; // last known modification time of 'file'
65    int getModtime() const {
66        struct stat st;
67        if (stat(file.c_str(), &st) == 0) return st.st_mtime;
68        return 0;
69    }
70    bool was_changed() const {
71        int  currModtime = getModtime();
72        bool changed     = currModtime>lastModtime;
73        lastModtime      = currModtime;
74        return changed;
75    }
76#endif
77
78public:
79    TrackedFile(const string& filename) :
80        file(filename)
81    {
82#if defined(USE_INOTIFY)
83        watch_descriptor = -1;
84#else // USE_STATPOLL
85        lastModtime      = 0;
86#endif
87    }
88#if defined(USE_INOTIFY)
89    ~TrackedFile() {
90        aw_assert(watch_descriptor == -1); // watch has to be removed before destruction
91    }
92#endif
93
94    const string& get_name() const { return file; }
95
96    void add_callback   (const FileChangedCallback& cb) { callbacks.insert(cb); }
97    void remove_callback(const FileChangedCallback& cb) { callbacks.erase(cb); }
98
99    bool empty() const { return callbacks.empty(); }
100
101    void callAll(ChangeReason reason) const {
102        bool deleted_still_exists = reason == CR_DELETED && GB_is_regularfile(file.c_str());
103        if (!deleted_still_exists) {
104            CallbackList copy = callbacks;
105            for (CallbackList::const_iterator cb = copy.begin(); cb != copy.end(); ++cb) {
106                if (callbacks.find(*cb) != callbacks.end()) {
107                    (*cb)(file.c_str(), reason);
108                }
109            }
110        }
111    }
112
113#if defined(USE_INOTIFY)
114    int get_watch_descriptor() const {
115        return watch_descriptor;
116    }
117    GB_ERROR add_watch(int inotifier) {
118        aw_assert(watch_descriptor == -1);
119        watch_descriptor = inotify_add_watch(inotifier, file.c_str(), IN_DELETE_SELF|IN_MOVE_SELF|IN_CLOSE_WRITE|IN_DELETE);
120        return watch_descriptor<0 ? GB_IO_error("watching", file.c_str()) : NULp;
121    }
122    GB_ERROR remove_watch(int inotifier) {
123        GB_ERROR err = NULp;
124        if (watch_descriptor != -1) {
125            if (inotify_rm_watch(inotifier, watch_descriptor)<0) {
126                err = GB_IO_error("un-watching", file.c_str());
127            }
128            watch_descriptor = -1;
129        }
130        return err;
131    }
132    void mark_as_disabled() {
133        watch_descriptor = -1;
134    }
135    void track_creation(TrackedFiles *tracked);
136
137#else // USE_STATPOLL
138    void callback_if_changed() const { if (was_changed()) callAll(CR_MODIFIED); }
139#endif
140};
141
142class TrackedFiles : virtual Noncopyable {
143    typedef list<TrackedFile> FileList;
144
145    FileList files;
146
147#if defined(USE_INOTIFY)
148    int inotifier;
149
150    RefPtr<const char> error;
151
152    SmartCharPtr ievent_buffer;
153    int          oversize;
154
155    bool reactivate_stale;
156
157    size_t get_ievent_buffer_size() {
158        return sizeof(inotify_event)+oversize;
159    }
160    void realloc_buffer(int new_oversize) {
161        aw_assert(new_oversize>oversize);
162        oversize      = new_oversize;
163        ievent_buffer = ARB_alloc<char>(get_ievent_buffer_size());
164    }
165
166    inotify_event *get_ievent_buffer() {
167        if (oversize<0) realloc_buffer(9*sizeof(inotify_event)); // reserve memory for 10 events
168        return reinterpret_cast<inotify_event*>(&*ievent_buffer);
169    }
170    void increase_buffer_oversize() {
171        aw_assert(oversize>0);
172        realloc_buffer(oversize*3/2);
173    }
174
175
176#endif
177
178    FileList::iterator find(const string& file) {
179        for (FileList::iterator f = files.begin(); f != files.end(); ++f) {
180            if (file == f->get_name()) return f;
181        }
182        return files.end();
183    }
184#if defined(USE_INOTIFY)
185    FileList::iterator find_watch_descriptor(int wd) {
186        for (FileList::iterator f = files.begin(); f != files.end(); ++f) {
187            if (wd == f->get_watch_descriptor()) {
188                return f;
189            }
190        }
191        return files.end();
192    }
193
194    void check_for_created_files() {
195        aw_assert(reactivate_stale);
196        for (FileList::iterator f = files.begin(); f != files.end(); ++f) {
197            if (f->get_watch_descriptor() == -1) {
198                GB_ERROR err = f->add_watch(inotifier);
199                if (!err) { // successfully reactivated
200                    f->callAll(CR_CREATED);
201                }
202#if defined(TRACE_INOTIFY)
203                fprintf(stderr, "check_for_created_files: add_watch for '%s': %s wd=%i\n", f->get_name().c_str(), err, f->get_watch_descriptor());
204#endif
205            }
206        }
207        // @@@ if no "stale" watch descriptor left => uninstall all watches of parent_modified_cb
208    }
209#endif
210
211public:
212#if defined(USE_INOTIFY)
213    TrackedFiles() :
214        error(NULp),
215        oversize(-1),
216        reactivate_stale(false)
217    {
218        inotifier = inotify_init();
219        if (inotifier<0) {
220            error = GB_IO_error("creating", "<inotify-instance>");
221        }
222    }
223    ~TrackedFiles() {
224        for (FileList::iterator f = files.begin(); f != files.end(); ++f) {
225            if (f->get_watch_descriptor() >= 0) {
226                GB_ERROR  ferr  = f->remove_watch(inotifier);
227                if (ferr) fprintf(stderr, "Error in ~TrackedFiles: %s\n", ferr);
228            }
229            aw_assert(f->get_watch_descriptor() == -1);
230        }
231        close(inotifier);
232    }
233#endif
234
235    GB_ERROR get_error() {
236#if defined(USE_INOTIFY)
237        GB_ERROR err = error;
238        error        = NULp;
239        return err;
240#else // USE_STATPOLL
241        return NULp;
242#endif
243    }
244
245    bool empty() const {
246        return files.empty();
247    }
248    void insert(string file, const FileChangedCallback& fccb) {
249        // @@@ use canonical path of file!
250
251#if defined(TRACE_INOTIFY_BASIC)
252        fprintf(stderr, "[inotifier] + %s\n", file.c_str());
253#endif
254
255#if defined(USE_INOTIFY)
256        aw_assert(!error);
257#endif
258        FileList::iterator found = find(file);
259        if (found == files.end()) {
260            files.push_back(TrackedFile(file)); // new tracked file
261            found = find(file);
262#if defined(USE_INOTIFY)
263            GB_ERROR werr = found->add_watch(inotifier);
264            if (werr) found->track_creation(this);
265#if defined(TRACE_INOTIFY)
266            fprintf(stderr, "insert: add_watch for '%s': %s wd=%i\n", found->get_name().c_str(), werr, found->get_watch_descriptor());
267#endif
268#endif
269        }
270        found->add_callback(fccb);
271    }
272    void erase(string file, const FileChangedCallback& fccb) {
273        // @@@ use canonical path of file!
274
275#if defined(TRACE_INOTIFY_BASIC)
276        fprintf(stderr, "[inotifier] - %s\n", file.c_str());
277#endif
278
279#if defined(USE_INOTIFY)
280        aw_assert(!error);
281#endif
282        FileList::iterator found = find(file);
283        if (found != files.end()) {
284            found->remove_callback(fccb);
285            if (found->empty()) {
286#if defined(USE_INOTIFY)
287                if (found->get_watch_descriptor() != -1) {
288                    error = found->remove_watch(inotifier);
289                }
290#endif
291                files.erase(found);
292            }
293        }
294    }
295
296    void check_changes();
297#if defined(USE_INOTIFY)
298    void request_reactivate_stale() { reactivate_stale = true; }
299#endif
300};
301
302#if defined(USE_INOTIFY)
303static void parent_modified_cb(const char *IF_TRACE_INOTIFY(parent_dir), ChangeReason IF_TRACE_INOTIFY(reason), TrackedFiles *tracked) {
304#if defined(TRACE_INOTIFY)
305    fprintf(stderr, "parent_modified_cb(dir='%s', reason=%i) called\n", parent_dir, int(reason));
306#endif
307    tracked->request_reactivate_stale();
308}
309
310void TrackedFile::track_creation(TrackedFiles *tracked) { // @@@ creation not tracked if !USE_INOTIFY
311    aw_assert(get_watch_descriptor() == -1); // otherwise it exists
312
313    char *parent_dir;
314    GB_split_full_path(file.c_str(), &parent_dir, NULp, NULp, NULp);
315
316    aw_assert(parent_dir);
317    if (parent_dir) {
318        AW_add_inotification(parent_dir, makeFileChangedCallback(parent_modified_cb, tracked));
319        free(parent_dir);
320    }
321}
322#endif
323
324void TrackedFiles::check_changes() {
325    // called manually from unittest below
326    // called via timer callback otherwise
327
328#if defined(USE_INOTIFY)
329    aw_assert(!error);
330
331    timespec timeout = { 0, 0 }; // don't wait
332    fd_set readfds;
333    FD_ZERO(&readfds);
334    FD_SET(inotifier, &readfds);
335
336    int sel;
337    while ((sel = pselect(inotifier+1, &readfds, NULp, NULp, &timeout, NULp))) { // only poll if events are waiting
338        aw_assert(sel>0);
339
340        inotify_event *ievent = get_ievent_buffer();
341        ssize_t        got    = read(inotifier, ievent, get_ievent_buffer_size());
342
343        if (got >= ssize_t(sizeof(inotify_event))) {
344            while (got>0) {
345                FileList::iterator found = find_watch_descriptor(ievent->wd);
346                if (found != files.end()) {
347                    bool watch_removed = (ievent->mask & IN_IGNORED);
348#if defined(TRACE_INOTIFY)
349                    const char *name = (ievent->len>1) ? ievent->name : "";
350
351                    fprintf(stderr,
352                            "got inotify event for '%s' (wd=%i, mask=%X, watch_removed=%i) '%s'\n",
353                            found->get_name().c_str(), ievent->wd, ievent->mask, int(watch_removed), name);
354#endif
355                    if (watch_removed) {
356                        found->mark_as_disabled();
357                        found->track_creation(this);
358                    }
359                    else if (ievent->mask & IN_DELETE_SELF) {
360                        found->callAll(CR_DELETED);
361                    }
362                    else if (ievent->mask & IN_MOVE_SELF) {
363                        found->callAll(CR_DELETED);
364                        found->remove_watch(inotifier); // invalidate watch descriptor of found
365                        found->track_creation(this);    // ensure re-creation gets detected
366                        reactivate_stale = true;        // causes check whether new target has a "stale" watch
367                    }
368                    else if (ievent->mask & (IN_DELETE|IN_CLOSE_WRITE)) { // item deleted from directory or file/dir modified
369                        found->callAll(CR_MODIFIED);
370                    }
371                    else {
372                        aw_assert(0); // unhandled event from inotify
373                    }
374                }
375                else {
376#if defined(TRACE_INOTIFY)
377                    fprintf(stderr, "ignoring event for unknown watch_descriptor=%i\n", ievent->wd);
378#endif
379                }
380
381                int event_len  = sizeof(inotify_event)+ievent->len;
382                got           -= event_len;
383                ievent         = reinterpret_cast<inotify_event*>(reinterpret_cast<char*>(ievent)+event_len);
384            }
385        }
386        else {
387            bool     buffer_too_small = false;
388            GB_ERROR read_err         = NULp;
389
390            if (got == -1) {
391                if (errno == EINVAL) buffer_too_small = true;
392                else read_err = GB_IO_error("reading", "<inotify-descriptor>");
393            }
394            else if (got == 0) buffer_too_small = true;
395            else {
396                aw_assert(0);
397            }
398
399            if (read_err) {
400                fprintf(stderr, "inotifier broken: %s\n", read_err);
401                aw_assert(0);
402                break; // while loop
403            }
404
405            if (!buffer_too_small) {
406                aw_assert(buffer_too_small);
407                GBK_terminate("inotify event queue broken? (neither buffer_too_small nor read_err)");
408            }
409            increase_buffer_oversize();
410        }
411
412        aw_assert(FD_ISSET(inotifier, &readfds));
413    }
414
415    if (reactivate_stale) {
416        check_for_created_files();
417        reactivate_stale = false; // do once if any dir-content changes
418    }
419
420#else // USE_STATPOLL
421    for (FileList::iterator f = files.begin(); f != files.end(); ++f) {
422        f->callback_if_changed();
423    }
424#endif
425}
426
427// --------------------------------------------------------------------------------
428
429static SmartPtr<TrackedFiles> allTrackers(new TrackedFiles);
430
431static bool    maintain_timer_callback = true;
432const unsigned AW_INOTIFY_TIMER        = 700; // ms
433
434static unsigned timed_inotifications_check_cb() {
435    allTrackers->check_changes();
436    if (allTrackers->empty()) {
437#if defined(TRACE_INOTIFY_BASIC)
438        fputs("[inotifier] - timer callback\n", stderr);
439#endif
440        return 0;
441    }
442    return AW_INOTIFY_TIMER;
443}
444
445static void show_tracked_error() {
446    GB_ERROR err = allTrackers->get_error();
447    if (err) {
448        fprintf(stderr, "Error in TrackedFiles: %s\n", err);
449    }
450}
451
452void AW_add_inotification(const char *file, const FileChangedCallback& fccb) {
453    /*! callback after a file changes
454     * @param file the file to watch
455     * @param fccb callback to call
456     */
457    show_tracked_error();
458    if (allTrackers->empty() & maintain_timer_callback) {
459#if defined(TRACE_INOTIFY_BASIC)
460        fputs("[inotifier] + timer callback\n", stderr);
461#endif
462        AW_root::SINGLETON->add_timed_callback(AW_INOTIFY_TIMER, makeTimedCallback(timed_inotifications_check_cb));
463    }
464    allTrackers->insert(file, fccb);
465    show_tracked_error();
466}
467
468void AW_remove_inotification(const char *file, const FileChangedCallback& fccb) {
469    allTrackers->erase(file, fccb);
470    show_tracked_error();
471}
472
473// --------------------------------------------------------------------------------
474
475#ifdef UNIT_TESTS
476#ifndef TEST_UNIT_H
477#include <test_unit.h>
478#endif
479#include <arb_cs.h>
480#include <arb_str.h>
481
482const int FLAGS = 4;
483static int change_flag[FLAGS][CHANGE_REASONS];
484
485static void trace_file_changed_cb(const char *IF_TRACE_INOTIFY(file), ChangeReason reason, int flag) {
486    aw_assert(flag<FLAGS);
487    change_flag[flag][reason]++;
488
489#if defined(TRACE_INOTIFY)
490    fprintf(stderr, "trace_file_changed_cb: flag=%i reason=%i file='%s'\n", flag, int(reason), file);
491#endif
492}
493
494// buf layout is "0123-0123-0123" (digits here are flag indices)
495#define BUFIDX(f,r) ((r)*(FLAGS+1)+(f))
496
497static const char *update_change_counts() {
498    for (int f = 0; f<FLAGS; ++f) {
499        for (int r = 0; r<CHANGE_REASONS; ++r) {
500            change_flag[f][r] = 0;
501        }
502    }
503
504    allTrackers->check_changes();
505
506    static char buf[BUFIDX(0,CHANGE_REASONS)];
507    for (int r = 0; r<CHANGE_REASONS; ++r) {
508        for (int f = 0; f<FLAGS; ++f) {
509            buf[BUFIDX(f,r)] = '0'+change_flag[f][r];
510        }
511        buf[BUFIDX(FLAGS,r)] = '-';
512    }
513    buf[BUFIDX(0,CHANGE_REASONS)-1] = 0;
514    return buf;
515}
516
517static arb_test::match_expectation change_counts_are(const char *expected_counts) {
518    using namespace   arb_test;
519    expectation_group expected;
520
521    const char *detected_counts = update_change_counts();
522
523    expected.add(that(detected_counts).is_equal_to(expected_counts));
524
525    return all().ofgroup(expected);
526}
527
528#define TEST_EXPECT_CHANGE_COUNTS(expected) TEST_EXPECTATION(change_counts_are(expected))
529
530static void update_file(const char *filename) {
531    FILE *out = fopen(filename, "wt");
532    aw_assert(out);
533    fputs("hi", out);
534    fclose(out);
535}
536
537inline void touch_files(const char *f1, const char *f2 = NULp) {
538#if defined(USE_STATPOLL)
539    static time_t last = 0;
540    {
541        time_t now;
542        do time(&now); while (now == last); // wait for new second (to ensure timestamps differ)
543    }
544#endif
545
546    if (f1) update_file(f1);
547    if (f2) update_file(f2);
548
549#if defined(USE_STATPOLL)
550    time(&last);
551#endif
552}
553
554inline void erase_files(const char *f1, const char *f2 = NULp) {
555    if (f1) TEST_EXPECT_MORE_EQUAL(GB_unlink(f1), 0);
556    if (f2) TEST_EXPECT_MORE_EQUAL(GB_unlink(f2), 0);
557}
558
559inline void move_file(const char *src, const char *dst) {
560    TEST_EXPECT_NO_ERROR(GB_move_file(src, dst));
561}
562inline void double_move_file(const char *src, const char *tmp, const char *dst) {
563    move_file(src, tmp);
564    move_file(tmp, dst);
565}
566
567#define INOTIFY_TESTDIR "inotify"
568
569// #define RETRY_INOTIFICATIONS_TEST
570
571void TEST_inotifications() {
572#if !defined(USE_INOTIFY)
573# warning inotifications broken in poll mode
574    MISSING_TEST(TEST_inotifications);
575    // @@@ fix behavior in poll mode (tests below fail)
576    // functionality needed in arb seems to work nevertheless (maybe just test less if !USE_INOTIFY?)
577    return;
578#endif
579
580    // for some unknown reason this test randomly fails on one build host
581    //
582    // Update: the previous changes (log:trunk@16775:16780) do not fix the problem :(
583    // * if the test fails initially, repeated calls always fail!
584    // * but initial failure only happens with 5-10% probability (of calls of test-executable)
585    //
586    static bool inotify_tests_failing_randomly = ARB_stricmp(arb_gethostname(), "build-jessie") == 0;
587    if (inotify_tests_failing_randomly) {
588#if defined(RETRY_INOTIFICATIONS_TEST)
589        TEST_ALLOW_RETRY(5); // tell test-suite to try this test up to 5 times
590#else
591        MISSING_TEST("TEST_inotifications fails randomly on build-jessie -> skipped!");
592        return; // disable test on this host
593#endif
594    }
595
596    const char *inotify_testdir = INOTIFY_TESTDIR;
597
598    const char *testfile1 = INOTIFY_TESTDIR "/inotify1.testfile";
599    const char *testfile2 = INOTIFY_TESTDIR "/inotify2.testfile";
600    const char *nofile    = INOTIFY_TESTDIR "/no.testfile";
601
602    TEST_EXPECT_NO_ERROR(GB_create_directory(INOTIFY_TESTDIR));
603
604#if defined(RETRY_INOTIFICATIONS_TEST)
605    static bool retrying = false;
606    if (retrying) {
607        // remove any leftovers from last try
608        erase_files(testfile1,testfile2);
609        erase_files(nofile);
610        TEST_EXPECT_ZERO(rmdir(inotify_testdir));
611        allTrackers = new TrackedFiles;
612        TEST_EXPECT_NO_ERROR(GB_create_directory(INOTIFY_TESTDIR));
613    }
614    retrying = true;
615#endif
616
617    FileChangedCallback fccb1 = makeFileChangedCallback(trace_file_changed_cb, 0);
618    FileChangedCallback fccb2 = makeFileChangedCallback(trace_file_changed_cb, 1);
619    FileChangedCallback fccb3 = makeFileChangedCallback(trace_file_changed_cb, 2);
620    FileChangedCallback dccb4 = makeFileChangedCallback(trace_file_changed_cb, 3); // tracks directory
621
622    maintain_timer_callback = false;
623    erase_files(nofile); // make sure file does not exist
624
625    // event order  (= order of ChangeReason) is "MODIFIED-DELETED-UNKNOWN"
626    TEST_EXPECT_CHANGE_COUNTS("0000-0000-0000");
627
628    AW_add_inotification(inotify_testdir, dccb4);
629
630    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0002-0000-0000"); // two cbs for two changes in directory
631
632    AW_add_inotification(testfile1, fccb1);
633
634    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("1002-0000-0000"); // two cbs for dir-changes; one cb for testfile1
635
636    AW_add_inotification(testfile2, fccb2);
637
638    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("1102-0000-0000");
639
640    erase_files(testfile1); TEST_EXPECT_CHANGE_COUNTS("0001-1000-0000"); // dir changed + testfile1 deleted
641    touch_files(testfile1); TEST_EXPECT_CHANGE_COUNTS("0001-0000-1000"); // dir changed + testfile1 (re-)created
642    touch_files(testfile1); TEST_EXPECT_CHANGE_COUNTS("1001-0000-0000"); // dir changed + testfile1 modified
643
644    erase_files(testfile2); TEST_EXPECT_CHANGE_COUNTS("0001-0100-0000"); // dir changed + testfile2 deleted
645
646    AW_add_inotification(nofile, fccb3); // add a tracker on non-existing file
647
648    // touch non-yet-existing 'nofile' -> should detect creation
649    touch_files(nofile);                      TEST_EXPECT_CHANGE_COUNTS("0001-0000-0010"); // detects creation of non-existing file
650    erase_files(nofile); touch_files(nofile); TEST_EXPECT_CHANGE_COUNTS("0002-0000-0010"); // does detect quick delete+recreate as "create" (delete gets supressed)
651    erase_files(nofile);                      TEST_EXPECT_CHANGE_COUNTS("0001-0010-0000"); // detects erase
652    touch_files(nofile); erase_files(nofile); TEST_EXPECT_CHANGE_COUNTS("0002-0000-0000"); // does NOT detect quick create+delete (just 2 directory changes)
653
654    // test moving files
655    move_file(testfile1, nofile);                   TEST_EXPECT_CHANGE_COUNTS("0000-1000-0010"); // detects delete 'testfile1' and create 'nofile'
656    touch_files(nofile);                            TEST_EXPECT_CHANGE_COUNTS("0011-0000-0000"); // detects modify 'nofile'
657    touch_files(testfile1);                         TEST_EXPECT_CHANGE_COUNTS("0001-0000-1000"); // detects create 'testfile1'
658    move_file(nofile, testfile1);                   TEST_EXPECT_CHANGE_COUNTS("0000-0010-1000"); // detects delete 'nofile' and create 'testfile1' (delete of overwritten 'testfile1' gets supressed)
659    double_move_file(testfile1, nofile, testfile1); TEST_EXPECT_CHANGE_COUNTS("0000-0000-1000"); // detects creation of 'testfile1' ('nofile' does not trigger)
660    double_move_file(testfile1, nofile, testfile2); TEST_EXPECT_CHANGE_COUNTS("0000-1000-0100"); // delete 'testfile1' + create 'testfile2' ('nofile' does not trigger)
661    touch_files(testfile1, nofile);                 TEST_EXPECT_CHANGE_COUNTS("0002-0000-1010"); // create 'testfile1'+'nofile'
662    double_move_file(testfile2, nofile, testfile1); TEST_EXPECT_CHANGE_COUNTS("0000-0110-1000"); // delete 'testfile2' and 'nofile' + create 'testfile1'
663
664    AW_add_inotification(testfile1, fccb3);
665    AW_add_inotification(testfile2, fccb3);
666
667    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("1012-0000-0110"); // dir changed + testfile1 modified + testfile2 created
668
669    touch_files(testfile1);            TEST_EXPECT_CHANGE_COUNTS("1011-0000-0000"); // 2 cbs for file + 1 cb for dir triggered by one touch
670    touch_files(testfile2);            TEST_EXPECT_CHANGE_COUNTS("0111-0000-0000"); // same for other testfile
671
672    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("1122-0000-0000"); // 6 callbacks triggered by 2 touches
673    AW_add_inotification(testfile2, fccb3); // (try to) add a tracker twice
674    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("1122-0000-0000"); // still 6 callbacks triggered by 2 touches
675
676    AW_remove_inotification(testfile1, fccb1);
677
678    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0122-0000-0000");
679
680    // changing cd is ok for triggers @@@ might fail for delete+re-create
681    touch_files(testfile1, testfile2);
682    {
683        TEST_EXPECT_ZERO(chdir(inotify_testdir));
684        TEST_EXPECT_CHANGE_COUNTS("0122-0000-0000");
685        TEST_EXPECT_ZERO(chdir(".."));
686    }
687
688    AW_remove_inotification(testfile2, fccb2);
689
690    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0022-0000-0000");
691
692    AW_remove_inotification(testfile1, fccb3);
693
694    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0012-0000-0000");
695
696    AW_remove_inotification(testfile2, fccb3);
697
698    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0002-0000-0000");
699
700    AW_remove_inotification(inotify_testdir, dccb4);
701
702    touch_files(testfile1, testfile2); TEST_EXPECT_CHANGE_COUNTS("0000-0000-0000");
703
704    TEST_EXPECT_ZERO(GB_unlink(testfile1));
705    TEST_EXPECT_ZERO(GB_unlink(testfile2));
706    TEST_EXPECT_ZERO(rmdir(inotify_testdir));
707
708    allTrackers = new TrackedFiles; // test cleanup (normally occurs at program exit)
709}
710
711#endif // UNIT_TESTS
712
713// --------------------------------------------------------------------------------
Note: See TracBrowser for help on using the repository browser.