| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: EventWatchFilesystemObject.cpp |
|---|
| 5 | // Purpose: WaitForEvent compatible object for watching directories |
|---|
| 6 | // Created: 12/3/04 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #include "Box.h" |
|---|
| 11 | |
|---|
| 12 | #include <errno.h> |
|---|
| 13 | #include <fcntl.h> |
|---|
| 14 | |
|---|
| 15 | #ifdef HAVE_UNISTD_H |
|---|
| 16 | #include <unistd.h> |
|---|
| 17 | #endif |
|---|
| 18 | |
|---|
| 19 | #include "EventWatchFilesystemObject.h" |
|---|
| 20 | #include "autogen_CommonException.h" |
|---|
| 21 | #include "Logging.h" |
|---|
| 22 | |
|---|
| 23 | #include "MemLeakFindOn.h" |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | // -------------------------------------------------------------------------- |
|---|
| 27 | // |
|---|
| 28 | // Function |
|---|
| 29 | // Name: EventWatchFilesystemObject::EventWatchFilesystemObject |
|---|
| 30 | // (const char *) |
|---|
| 31 | // Purpose: Constructor -- opens the file object |
|---|
| 32 | // Created: 12/3/04 |
|---|
| 33 | // |
|---|
| 34 | // -------------------------------------------------------------------------- |
|---|
| 35 | EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename) |
|---|
| 36 | #ifdef HAVE_KQUEUE |
|---|
| 37 | : mDescriptor(::open(Filename, O_RDONLY /*O_EVTONLY*/, 0)) |
|---|
| 38 | #endif |
|---|
| 39 | { |
|---|
| 40 | #ifdef HAVE_KQUEUE |
|---|
| 41 | if(mDescriptor == -1) |
|---|
| 42 | { |
|---|
| 43 | BOX_LOG_SYS_ERROR("EventWatchFilesystemObject: " |
|---|
| 44 | "Failed to open file '" << Filename << "'"); |
|---|
| 45 | THROW_EXCEPTION(CommonException, OSFileOpenError) |
|---|
| 46 | } |
|---|
| 47 | #else |
|---|
| 48 | THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform) |
|---|
| 49 | #endif |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | // -------------------------------------------------------------------------- |
|---|
| 54 | // |
|---|
| 55 | // Function |
|---|
| 56 | // Name: EventWatchFilesystemObject::~EventWatchFilesystemObject() |
|---|
| 57 | // Purpose: Destructor |
|---|
| 58 | // Created: 12/3/04 |
|---|
| 59 | // |
|---|
| 60 | // -------------------------------------------------------------------------- |
|---|
| 61 | EventWatchFilesystemObject::~EventWatchFilesystemObject() |
|---|
| 62 | { |
|---|
| 63 | if(mDescriptor != -1) |
|---|
| 64 | { |
|---|
| 65 | ::close(mDescriptor); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | // -------------------------------------------------------------------------- |
|---|
| 71 | // |
|---|
| 72 | // Function |
|---|
| 73 | // Name: EventWatchFilesystemObject::EventWatchFilesystemObject |
|---|
| 74 | // (const EventWatchFilesystemObject &) |
|---|
| 75 | // Purpose: Copy constructor |
|---|
| 76 | // Created: 12/3/04 |
|---|
| 77 | // |
|---|
| 78 | // -------------------------------------------------------------------------- |
|---|
| 79 | EventWatchFilesystemObject::EventWatchFilesystemObject( |
|---|
| 80 | const EventWatchFilesystemObject &rToCopy) |
|---|
| 81 | : mDescriptor(::dup(rToCopy.mDescriptor)) |
|---|
| 82 | { |
|---|
| 83 | if(mDescriptor == -1) |
|---|
| 84 | { |
|---|
| 85 | THROW_EXCEPTION(CommonException, OSFileError) |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | #ifdef HAVE_KQUEUE |
|---|
| 91 | // -------------------------------------------------------------------------- |
|---|
| 92 | // |
|---|
| 93 | // Function |
|---|
| 94 | // Name: EventWatchFilesystemObject::FillInKEvent(struct kevent &, int) |
|---|
| 95 | // Purpose: For WaitForEvent |
|---|
| 96 | // Created: 12/3/04 |
|---|
| 97 | // |
|---|
| 98 | // -------------------------------------------------------------------------- |
|---|
| 99 | void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, |
|---|
| 100 | int Flags) const |
|---|
| 101 | { |
|---|
| 102 | EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, |
|---|
| 103 | NOTE_DELETE | NOTE_WRITE, 0, (void*)this); |
|---|
| 104 | } |
|---|
| 105 | #else |
|---|
| 106 | void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, |
|---|
| 107 | int Flags) const |
|---|
| 108 | { |
|---|
| 109 | THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform) |
|---|
| 110 | } |
|---|
| 111 | #endif |
|---|
| 112 | |
|---|