Belle II Software development
Inotify Class Reference
Inheritance diagram for Inotify:
FileDescriptor

Public Member Functions

void open ()
 
int add (const std::string &path, unsigned long mask)
 
void remove (int wd)
 
InotifyEventList wait (int sec)
 
int get_fd () const
 
bool select (int sec=-1, int usec=-1)
 
bool select2 (int sec=-1, int usec=-1)
 
bool close ()
 

Static Public Attributes

static const unsigned long FILE_CREATE
 
static const unsigned long FILE_OPEN
 
static const unsigned long FILE_CLOSE_WRITE
 
static const unsigned long FILE_CLOSE_NOWRITE
 
static const unsigned long FILE_DELETE
 
static const unsigned long FILE_MODIFY
 
static const unsigned long FILE_ACCESS
 
static const unsigned long FILE_ATTRIB
 

Protected Attributes

int m_fd
 

Detailed Description

Definition at line 49 of file Inotify.h.

Constructor & Destructor Documentation

◆ Inotify()

Inotify ( )
inline

Definition at line 62 of file Inotify.h.

62{}

◆ ~Inotify()

virtual ~Inotify ( )
inlinevirtual

Definition at line 63 of file Inotify.h.

63{}

Member Function Documentation

◆ add()

int add ( const std::string &  path,
unsigned long  mask 
)

Definition at line 33 of file Inotify.cc.

34{
35 int wd = inotify_add_watch(m_fd, path.c_str(), mask);
36 if (wd < 0) {
37 throw (IOException("Failed to add a path"));
38 }
39 return wd;
40}

◆ close()

bool close ( )
inherited

Definition at line 90 of file FileDescriptor.cc.

91{
92 if (m_fd > 0) {
93 if (::close(m_fd) != 0) {
94 return false;
95 }
96 }
97 m_fd = -1;
98 return true;
99}

◆ get_fd()

int get_fd ( ) const
inherited

Definition at line 33 of file FileDescriptor.cc.

34{
35 return m_fd;
36}

◆ open()

void open ( )

Definition at line 26 of file Inotify.cc.

27{
28 if ((m_fd = ::inotify_init()) < 0) {
29 throw (IOException("Failed to initialize inotify."));
30 }
31}

◆ remove()

void remove ( int  wd)

Definition at line 42 of file Inotify.cc.

43{
44 if (::inotify_rm_watch(m_fd, wd) < 0) {
45 throw (IOException("Failed to remove a path"));
46 }
47}

◆ select()

bool select ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 38 of file FileDescriptor.cc.

39{
40 if (m_fd <= 0) {
41 return false;
42 }
43 fd_set fds;
44 FD_ZERO(&fds);
45 FD_SET(m_fd, &fds);
46 int ret;
47 if (sec >= 0 && usec >= 0) {
48 timeval t = {sec, usec};
49 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
50 } else {
51 ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
52 }
53 if (ret < 0) {
54 perror("select");
55 throw (IOException("Failed to select"));
56 }
57 if (FD_ISSET(m_fd, &fds)) {
58 return true;
59 } else {
60 return false;
61 }
62}

◆ select2()

bool select2 ( int  sec = -1,
int  usec = -1 
)
inherited

Definition at line 64 of file FileDescriptor.cc.

65{
66 if (m_fd <= 0) {
67 return false;
68 }
69 fd_set fds;
70 FD_ZERO(&fds);
71 FD_SET(m_fd, &fds);
72 int ret;
73 if (sec >= 0 && usec >= 0) {
74 timeval t = {sec, usec};
75 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
76 } else {
77 ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
78 }
79 if (ret < 0) {
80 perror("select");
81 throw (IOException("Failed to select"));
82 }
83 if (FD_ISSET(m_fd, &fds)) {
84 return true;
85 } else {
86 return false;
87 }
88}

◆ wait()

InotifyEventList wait ( int  sec)

Definition at line 49 of file Inotify.cc.

50{
51 InotifyEventList ievent_v;
52 //try {
53 if (select(sec, 0)) {
54 char buf[16384];
55 int r = read(m_fd, buf, 16384);
56 if (r <= 0) return ievent_v;
57 int index = 0;
58 while (index < r) {
59 inotify_event* ev = (inotify_event*)&buf[index];
60 int event_size = sizeof(inotify_event) + ev->len;
61 index += event_size;
62 ievent_v.push_back(InotifyEvent(ev->wd, ev->mask, ev->name));
63 }
64 }
65 //} catch (const IOException& e) {
66 //std::cerr << "Failed to select" << std::endl;
67 //}
68 return ievent_v;
69}

Member Data Documentation

◆ FILE_ACCESS

const unsigned long FILE_ACCESS
static

Definition at line 58 of file Inotify.h.

◆ FILE_ATTRIB

const unsigned long FILE_ATTRIB
static

Definition at line 59 of file Inotify.h.

◆ FILE_CLOSE_NOWRITE

const unsigned long FILE_CLOSE_NOWRITE
static

Definition at line 55 of file Inotify.h.

◆ FILE_CLOSE_WRITE

const unsigned long FILE_CLOSE_WRITE
static

Definition at line 54 of file Inotify.h.

◆ FILE_CREATE

const unsigned long FILE_CREATE
static

Definition at line 52 of file Inotify.h.

◆ FILE_DELETE

const unsigned long FILE_DELETE
static

Definition at line 56 of file Inotify.h.

◆ FILE_MODIFY

const unsigned long FILE_MODIFY
static

Definition at line 57 of file Inotify.h.

◆ FILE_OPEN

const unsigned long FILE_OPEN
static

Definition at line 53 of file Inotify.h.

◆ m_fd

int m_fd
protectedinherited

Definition at line 31 of file FileDescriptor.h.


The documentation for this class was generated from the following files: