gtkIOStream  1.7.0
GTK+ << C++ IOStream operators for GTK+. Now with ORBing, numerical computation, audio client and more ...
FileWatchThreaded.H
Go to the documentation of this file.
1 /* Copyright 2000-2018 Matt Flax <flatmax@flatmax.org>
2  This file is part of GTK+ IOStream class set
3 
4  GTK+ IOStream is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  GTK+ IOStream is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You have received a copy of the GNU General Public License
15  along with GTK+ IOStream
16  */
17 #ifndef FILEWATCHTHREAD_H_
18 #define FILEWATCHTHREAD_H_
19 
20 #include <Thread.H>
21 #include <sys/inotify.h>
22 #include <unistd.h>
23 #include <limits.h>
24 #include <assert.h>
25 
48  int fd;
49  #define MAX_EVENTS 10
50  #define BUF_SIZE MAX_EVENTS*(sizeof(struct inotify_event) + NAME_MAX + 1)
51  char eventBuf[BUF_SIZE] __attribute__ ((aligned(__alignof__(struct inotify_event))));
52 
53  std::map<std::string, int> pathFds;
54 
55  virtual void *threadMain(void){
56  while (int numRead = read(fd, eventBuf, BUF_SIZE)) {
57  if (numRead == 0 | numRead == -1){
58  printf("FileWatchThreaded::threadMain : read() from inotify fd returned %d !\n",numRead);
59  Debug().evaluateError(numRead); // print strerror
60  break;
61  }
62  for (char *p = (char*)eventBuf; p < eventBuf + numRead; p += sizeof(struct inotify_event) + reinterpret_cast<struct inotify_event *>(p)->len) {
63  if (reinterpret_cast<struct inotify_event *>(p)->mask & IN_MODIFY)
64  modified(reinterpret_cast<struct inotify_event *>(p)->name); // call the modified function
65  if (reinterpret_cast<struct inotify_event *>(p)->mask & IN_CLOSE_WRITE)
66  closeWrite(reinterpret_cast<struct inotify_event *>(p)->name); // call the modified function
67  // Other unhandled events which can be added ...
68  // IN_ACCESS, IN_ATTRIB, IN_CLOSE_NOWRITE, IN_CREATE, IN_DELETE, IN_DELETE_SELF,IN_IGNORED
69  // IN_ISDIR, IN_MOVE_SELF, IN_MOVED_FROM, IN_MOVED_TO, IN_OPEN, IN_Q_OVERFLOW, IN_UNMOUNT
70  }
71  }
72  return NULL;
73  }
74 
78  virtual void modified(char *name){
79  }
80 
84  virtual void closeWrite(char *name){
85  // printf("FileWatchThreaded::close write\n");
86  }
87 public:
90  fd = inotify_init();
91  if (fd == -1){
92  Debug().evaluateError(fd, " FileWatchThreaded : When calling inotify_init.");
93  assert(0); // exit on failure
94  }
95  }
96 
98  virtual ~FileWatchThreaded(){
99  }
100 
105  virtual int add(std::string path){
106  int ret = inotify_add_watch(fd, path.c_str(), IN_ALL_EVENTS);
107  if (ret == -1)
108  return Debug().evaluateError(fd, " FileWatchThreaded : When calling inotify_add_watch.");
109  pathFds[path]=ret; // store the path fd
110  return 0;
111  }
112 
117  virtual int addFile(std::string path){
118  int ret=add(path);
119  if (ret==ESRCH) { // file doesn't exist, attempt to create
120  FILE *fid=fopen(path.c_str(), "a");
121  if (fid==NULL) // check for failure
122  ret=-EIO;
123  else {
124  fclose(fid);
125  ret=add(path); // now try to add the file a second time
126  }
127  }
128  return ret;
129  }
130 };
131 
132 #endif // FILEWATCHTHREAD_H_
virtual int addFile(std::string path)
FileWatchThreaded()
Constructor.
int fd
The inotify file descriptor.
virtual int evaluateError(int errorNum)
Definition: Debug.H:132
char eventBuf [BUF_SIZE] __attribute__((aligned(__alignof__(struct inotify_event))))
The buffered events.
float p
virtual void closeWrite(char *name)
virtual ~FileWatchThreaded()
Destructor.
virtual void modified(char *name)
#define BUF_SIZE
Definition: Debug.H:112
virtual int add(std::string path)
std::map< std::string, int > pathFds
This will contain a map between watch file descriptors and file/dir names.
virtual void * threadMain(void)
gtkIOStream: /tmp/gtkiostream/include/FileWatchThreaded.H Source File
GTK+ IOStream  Beta