summaryrefslogtreecommitdiff
path: root/lib/common/EventWatchFilesystemObject.cpp
blob: 4282f1e32a93a6c63c8dd1e7b2fa31e8a45637a3 (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
// --------------------------------------------------------------------------
//
// File
//		Name:    EventWatchFilesystemObject.cpp
//		Purpose: WaitForEvent compatible object for watching directories
//		Created: 12/3/04
//
// --------------------------------------------------------------------------

#include "Box.h"

#include <fcntl.h>

#ifdef HAVE_UNISTD_H
	#include <unistd.h>
#endif

#include "EventWatchFilesystemObject.h"
#include "autogen_CommonException.h"
#include "Logging.h"

#include "MemLeakFindOn.h"


// --------------------------------------------------------------------------
//
// Function
//		Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const char *)
//		Purpose: Constructor -- opens the file object
//		Created: 12/3/04
//
// --------------------------------------------------------------------------
EventWatchFilesystemObject::EventWatchFilesystemObject(const char *Filename)
#ifdef HAVE_KQUEUE
	: mDescriptor(::open(Filename, O_RDONLY /*O_EVTONLY*/, 0))
#endif
{
#ifdef HAVE_KQUEUE
	if(mDescriptor == -1)
	{
		BOX_ERROR("EventWatchFilesystemObject: "
			"Failed to open file '" << Filename << "': " <<
			strerror(errno));
		THROW_EXCEPTION(CommonException, OSFileOpenError)
	}
#else
	THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform)
#endif
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    EventWatchFilesystemObject::~EventWatchFilesystemObject()
//		Purpose: Destructor
//		Created: 12/3/04
//
// --------------------------------------------------------------------------
EventWatchFilesystemObject::~EventWatchFilesystemObject()
{
	if(mDescriptor != -1)
	{
		::close(mDescriptor);
	}
}


// --------------------------------------------------------------------------
//
// Function
//		Name:    EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &)
//		Purpose: Copy constructor
//		Created: 12/3/04
//
// --------------------------------------------------------------------------
EventWatchFilesystemObject::EventWatchFilesystemObject(const EventWatchFilesystemObject &rToCopy)
	: mDescriptor(::dup(rToCopy.mDescriptor))
{
	if(mDescriptor == -1)
	{
		THROW_EXCEPTION(CommonException, OSFileError)
	}
}


#ifdef HAVE_KQUEUE
// --------------------------------------------------------------------------
//
// Function
//		Name:    EventWatchFilesystemObject::FillInKEvent(struct kevent &, int)
//		Purpose: For WaitForEvent
//		Created: 12/3/04
//
// --------------------------------------------------------------------------
void EventWatchFilesystemObject::FillInKEvent(struct kevent &rEvent, int Flags) const
{
	EV_SET(&rEvent, mDescriptor, EVFILT_VNODE, EV_CLEAR, NOTE_DELETE | NOTE_WRITE, 0, (void*)this);
}
#else
void EventWatchFilesystemObject::FillInPoll(int &fd, short &events, int Flags) const
{
	THROW_EXCEPTION(CommonException, KQueueNotSupportedOnThisPlatform)
}
#endif