Belle II Software  release-05-01-25
Inotify.cc
1 #include "daq/slc/system/Inotify.h"
2 
3 #include <daq/slc/base/IOException.h>
4 
5 #include <sys/inotify.h>
6 #include <unistd.h>
7 
8 using namespace Belle2;
9 
10 const unsigned long Inotify::FILE_CREATE(IN_CREATE);
11 const unsigned long Inotify::FILE_OPEN(IN_OPEN);
12 const unsigned long Inotify::FILE_CLOSE_WRITE(IN_CLOSE_WRITE);
13 const unsigned long Inotify::FILE_CLOSE_NOWRITE(IN_CLOSE_NOWRITE);
14 const unsigned long Inotify::FILE_DELETE(IN_DELETE);
15 const unsigned long Inotify::FILE_MODIFY(IN_MODIFY);
16 const unsigned long Inotify::FILE_ACCESS(IN_ACCESS);
17 const unsigned long Inotify::FILE_ATTRIB(IN_ATTRIB);
18 
19 void Inotify::open()
20 {
21  if ((m_fd = ::inotify_init()) < 0) {
22  throw (IOException("Failed to initialize inotify."));
23  }
24 }
25 
26 int Inotify::add(const std::string& path, unsigned long mask)
27 {
28  int wd = inotify_add_watch(m_fd, path.c_str(), mask);
29  if (wd < 0) {
30  throw (IOException("Failed to add a path"));
31  }
32  return wd;
33 }
34 
35 void Inotify::remove(int wd)
36 {
37  if (::inotify_rm_watch(m_fd, wd) < 0) {
38  throw (IOException("Failed to remove a path"));
39  }
40 }
41 
42 InotifyEventList Inotify::wait(int sec)
43 {
44  InotifyEventList ievent_v;
45  //try {
46  if (select(sec, 0)) {
47  char buf[16384];
48  int r = read(m_fd, buf, 16384);
49  if (r <= 0) return ievent_v;
50  int index = 0;
51  while (index < r) {
52  inotify_event* ev = (inotify_event*)&buf[index];
53  int event_size = sizeof(inotify_event) + ev->len;
54  index += event_size;
55  ievent_v.push_back(InotifyEvent(ev->wd, ev->mask, ev->name));
56  }
57  }
58  //} catch (const IOException& e) {
59  //std::cerr << "Failed to select" << std::endl;
60  //}
61  return ievent_v;
62 }
Belle2::IOException
Definition: IOException.h:12
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::InotifyEvent
Definition: Inotify.h:15