file_monitor_linux.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/platform.h"
  6. #if CROWN_PLATFORM_LINUX
  7. #include "core/containers/hash_map.h"
  8. #include "core/filesystem/file_monitor.h"
  9. #include "core/filesystem/path.h"
  10. #include "core/memory/temp_allocator.h"
  11. #include "core/strings/dynamic_string.h"
  12. #include "core/thread/thread.h"
  13. #include <dirent.h> // opendir, readdir
  14. #include <errno.h>
  15. #include <limits.h> // NAME_MAX
  16. #include <sys/inotify.h>
  17. #include <sys/select.h>
  18. #include <unistd.h> // read
  19. namespace crown
  20. {
  21. template <>
  22. struct hash<DynamicString>
  23. {
  24. u32 operator()(const DynamicString& str) const
  25. {
  26. return str.to_string_id()._id;
  27. }
  28. };
  29. struct FileMonitorImpl
  30. {
  31. Allocator* _allocator;
  32. int _fd;
  33. HashMap<int, DynamicString> _watches;
  34. HashMap<DynamicString, int> _watches_reverse;
  35. Thread _thread;
  36. bool _exit;
  37. bool _recursive;
  38. FileMonitorFunction _function;
  39. void* _user_data;
  40. FileMonitorImpl(Allocator& a)
  41. : _allocator(&a)
  42. , _fd(0)
  43. , _watches(a)
  44. , _watches_reverse(a)
  45. , _exit(false)
  46. , _recursive(false)
  47. , _function(NULL)
  48. , _user_data(NULL)
  49. {
  50. }
  51. void add_watch(const char* path, bool recursive)
  52. {
  53. CE_ENSURE(path != NULL);
  54. CE_ASSERT(!path::has_trailing_separator(path), "Malformed path");
  55. int wd = inotify_add_watch(_fd
  56. , path
  57. , IN_CREATE
  58. | IN_DELETE // File/directory deleted from watched directory.
  59. | IN_MODIFY
  60. | IN_ATTRIB
  61. | IN_MOVE
  62. | IN_DELETE_SELF // Watched file/directory was itself deleted.
  63. | IN_DONT_FOLLOW // Don't dereference pathname if it is a symbolic link.
  64. | IN_ONLYDIR // Only watch pathname if it is a directory.
  65. );
  66. CE_ASSERT(wd != -1, "inotify_add_watch: errno: %d", errno);
  67. TempAllocator512 ta;
  68. DynamicString str(ta);
  69. str = path;
  70. hash_map::set(_watches, wd, str);
  71. hash_map::set(_watches_reverse, str, wd);
  72. // Add all sub-dirs recursively
  73. DIR *dir;
  74. struct dirent *entry;
  75. if (!(dir = opendir(path)))
  76. return;
  77. while ((entry = readdir(dir)))
  78. {
  79. const char* dname = entry->d_name;
  80. if (!strcmp(dname, ".") || !strcmp(dname, ".."))
  81. continue;
  82. // FIXME: some filesystems do not support DT_DIR.
  83. if (entry->d_type != DT_DIR)
  84. continue;
  85. TempAllocator512 ta;
  86. DynamicString str(ta);
  87. path::join(str, path, dname);
  88. add_watch(str.c_str(), recursive);
  89. }
  90. closedir(dir);
  91. }
  92. void start(const char* path, bool recursive, FileMonitorFunction fmf, void* user_data)
  93. {
  94. CE_ENSURE(NULL != path);
  95. CE_ENSURE(NULL != fmf);
  96. _recursive = recursive;
  97. _function = fmf;
  98. _user_data = user_data;
  99. _fd = inotify_init();
  100. CE_ASSERT(_fd != -1, "inotify_init: errno: %d", errno);
  101. add_watch(path, recursive);
  102. _thread.start(run, this);
  103. }
  104. void stop()
  105. {
  106. _exit = true;
  107. _thread.stop();
  108. close(_fd);
  109. }
  110. int watch()
  111. {
  112. while (!_exit)
  113. {
  114. TempAllocator512 ta;
  115. fd_set set;
  116. struct timeval timeout;
  117. u32 cookie = 0;
  118. u32 cookie_mask = 0;
  119. DynamicString cookie_path(ta);
  120. FD_ZERO(&set);
  121. FD_SET(_fd, &set);
  122. timeout.tv_sec = 0;
  123. timeout.tv_usec = 32*1000;
  124. // select returns 0 if timeout, 1 if input available, -1 if error.
  125. if (select(FD_SETSIZE, &set, NULL, NULL, &timeout) == 0)
  126. continue;
  127. #define BUF_LEN sizeof(struct inotify_event) + NAME_MAX + 1
  128. char buf[BUF_LEN*16];
  129. #undef BUF_LEN
  130. ssize_t len = read(_fd, buf, sizeof(buf));
  131. if (len == 0)
  132. return 0;
  133. if (len == -1)
  134. return -1;
  135. for (char* p = buf; p < buf + len;)
  136. {
  137. inotify_event* ev = (inotify_event*)p;
  138. if (ev->mask & IN_IGNORED)
  139. {
  140. // Watch was removed explicitly (inotify_rm_watch(2)) or
  141. // automatically (file was deleted, or filesystem was
  142. // unmounted).
  143. hash_map::remove(_watches, ev->wd);
  144. }
  145. if (ev->mask & IN_CREATE)
  146. {
  147. DynamicString path(ta);
  148. full_path(path, ev->wd, ev->name);
  149. _function(_user_data
  150. , FileMonitorEvent::CREATED
  151. , ev->mask & IN_ISDIR
  152. , path.c_str()
  153. , NULL
  154. );
  155. if (ev->mask & IN_ISDIR)
  156. add_watch(path.c_str(), _recursive);
  157. }
  158. if (ev->mask & IN_DELETE)
  159. {
  160. DynamicString path(ta);
  161. full_path(path, ev->wd, ev->name);
  162. _function(_user_data
  163. , FileMonitorEvent::DELETED
  164. , ev->mask & IN_ISDIR
  165. , path.c_str()
  166. , NULL
  167. );
  168. }
  169. if (ev->mask & IN_MODIFY || ev->mask & IN_ATTRIB)
  170. {
  171. DynamicString path(ta);
  172. full_path(path, ev->wd, ev->name);
  173. _function(_user_data
  174. , FileMonitorEvent::CHANGED
  175. , ev->mask & IN_ISDIR
  176. , path.c_str()
  177. , NULL
  178. );
  179. }
  180. if (ev->mask & IN_MOVED_FROM)
  181. {
  182. // Two consecutive IN_MOVED_FROM
  183. if (cookie != 0)
  184. {
  185. _function(_user_data
  186. , FileMonitorEvent::DELETED
  187. , cookie_mask & IN_ISDIR
  188. , cookie_path.c_str()
  189. , NULL
  190. );
  191. u32 wd = hash_map::get(_watches_reverse, cookie_path, INT32_MAX);
  192. hash_map::remove(_watches_reverse, cookie_path);
  193. inotify_rm_watch(_fd, wd);
  194. cookie = 0;
  195. cookie_mask = 0;
  196. }
  197. else
  198. {
  199. DynamicString path(ta);
  200. full_path(path, ev->wd, ev->name);
  201. cookie = ev->cookie;
  202. cookie_mask = ev->mask;
  203. cookie_path = path;
  204. }
  205. }
  206. if (ev->mask & IN_MOVED_TO)
  207. {
  208. if (cookie == ev->cookie)
  209. {
  210. // File or directory has been renamed
  211. DynamicString path(ta);
  212. full_path(path, ev->wd, ev->name);
  213. _function(_user_data
  214. , FileMonitorEvent::RENAMED
  215. , ev->mask & IN_ISDIR
  216. , cookie_path.c_str()
  217. , path.c_str()
  218. );
  219. cookie = 0;
  220. cookie_mask = 0;
  221. if (ev->mask & IN_ISDIR)
  222. {
  223. u32 wd = hash_map::get(_watches_reverse, cookie_path, INT32_MAX);
  224. hash_map::remove(_watches_reverse, cookie_path);
  225. inotify_rm_watch(_fd, wd);
  226. add_watch(path.c_str(), _recursive);
  227. }
  228. }
  229. else
  230. {
  231. // File or directory was moved to this folder
  232. DynamicString path(ta);
  233. full_path(path, ev->wd, ev->name);
  234. _function(_user_data
  235. , FileMonitorEvent::CREATED
  236. , ev->mask & IN_ISDIR
  237. , path.c_str()
  238. , NULL
  239. );
  240. cookie = 0;
  241. cookie_mask = 0;
  242. if (ev->mask & IN_ISDIR)
  243. add_watch(path.c_str(), _recursive);
  244. }
  245. }
  246. p += sizeof(inotify_event) + ev->len;
  247. }
  248. // Unpaired IN_MOVE_TO
  249. if (cookie != 0)
  250. {
  251. _function(_user_data
  252. , FileMonitorEvent::DELETED
  253. , cookie_mask & IN_ISDIR
  254. , cookie_path.c_str()
  255. , NULL
  256. );
  257. u32 wd = hash_map::get(_watches_reverse, cookie_path, INT32_MAX);
  258. hash_map::remove(_watches_reverse, cookie_path);
  259. inotify_rm_watch(_fd, wd);
  260. cookie = 0;
  261. cookie_mask = 0;
  262. }
  263. }
  264. return 0;
  265. }
  266. void full_path(DynamicString& path, int wd, const char* name)
  267. {
  268. TempAllocator512 ta;
  269. DynamicString path_base(ta);
  270. path_base = hash_map::get(_watches, wd, path_base);
  271. path::join(path, path_base.c_str(), name);
  272. }
  273. static int run(void* thiz)
  274. {
  275. return ((FileMonitorImpl*)thiz)->watch();
  276. }
  277. };
  278. FileMonitor::FileMonitor(Allocator& a)
  279. {
  280. _impl = CE_NEW(a, FileMonitorImpl)(a);
  281. }
  282. FileMonitor::~FileMonitor()
  283. {
  284. CE_DELETE(*_impl->_allocator, _impl);
  285. }
  286. void FileMonitor::start(const char* path, bool recursive, FileMonitorFunction fmf, void* user_data)
  287. {
  288. _impl->start(path, recursive, fmf, user_data);
  289. }
  290. void FileMonitor::stop()
  291. {
  292. _impl->stop();
  293. }
  294. } // namespace crown
  295. #endif // CROWN_PLATFORM_LINUX