filenotify.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. ;;; filenotify.el --- watch files for changes on disk -*- lexical-binding:t -*-
  2. ;; Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. ;; Author: Michael Albinus <michael.albinus@gmx.de>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs 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. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary
  16. ;; This package is an abstraction layer from the different low-level
  17. ;; file notification packages `gfilenotify', `inotify' and
  18. ;; `w32notify'.
  19. ;;; Code:
  20. (defconst file-notify--library
  21. (cond
  22. ((featurep 'gfilenotify) 'gfilenotify)
  23. ((featurep 'inotify) 'inotify)
  24. ((featurep 'w32notify) 'w32notify))
  25. "Non-nil when Emacs has been compiled with file notification support.
  26. The value is the name of the low-level file notification package
  27. to be used for local file systems. Remote file notifications
  28. could use another implementation.")
  29. (defvar file-notify-descriptors (make-hash-table :test 'equal)
  30. "Hash table for registered file notification descriptors.
  31. A key in this hash table is the descriptor as returned from
  32. `gfilenotify', `inotify', `w32notify' or a file name handler.
  33. The value in the hash table is a list
  34. (DIR (FILE . CALLBACK) (FILE . CALLBACK) ...)
  35. Several values for a given DIR happen only for `inotify', when
  36. different files from the same directory are watched.")
  37. (defun file-notify--rm-descriptor (descriptor &optional what)
  38. "Remove DESCRIPTOR from `file-notify-descriptors'.
  39. DESCRIPTOR should be an object returned by `file-notify-add-watch'.
  40. If it is registered in `file-notify-descriptors', a stopped event is sent.
  41. WHAT is a file or directory name to be removed, needed just for `inotify'."
  42. (let* ((desc (if (consp descriptor) (car descriptor) descriptor))
  43. (file (if (consp descriptor) (cdr descriptor)))
  44. (registered (gethash desc file-notify-descriptors))
  45. (dir (car registered)))
  46. (when (and (consp registered) (or (null what) (string-equal dir what)))
  47. ;; Send `stopped' event.
  48. (dolist (entry (cdr registered))
  49. (funcall (cdr entry)
  50. `(,descriptor stopped
  51. ,(or (and (stringp (car entry))
  52. (expand-file-name (car entry) dir))
  53. dir))))
  54. ;; Modify `file-notify-descriptors'.
  55. (if (not file)
  56. (remhash desc file-notify-descriptors)
  57. (setcdr registered
  58. (delete (assoc file (cdr registered)) (cdr registered)))
  59. (if (null (cdr registered))
  60. (remhash desc file-notify-descriptors)
  61. (puthash desc registered file-notify-descriptors))))))
  62. ;; This function is used by `gfilenotify', `inotify' and `w32notify' events.
  63. ;;;###autoload
  64. (defun file-notify-handle-event (event)
  65. "Handle file system monitoring event.
  66. If EVENT is a filewatch event, call its callback. It has the format
  67. (file-notify (DESCRIPTOR ACTIONS FILE [FILE1-OR-COOKIE]) CALLBACK)
  68. Otherwise, signal a `file-notify-error'."
  69. (interactive "e")
  70. ;;(message "file-notify-handle-event %S" event)
  71. (if (and (eq (car event) 'file-notify)
  72. (>= (length event) 3))
  73. (funcall (nth 2 event) (nth 1 event))
  74. (signal 'file-notify-error
  75. (cons "Not a valid file-notify event" event))))
  76. ;; Needed for `inotify' and `w32notify'. In the latter case, COOKIE is nil.
  77. (defvar file-notify--pending-event nil
  78. "A pending file notification events for a future `renamed' action.
  79. It is a form ((DESCRIPTOR ACTION FILE [FILE1-OR-COOKIE]) CALLBACK).")
  80. (defun file-notify--event-file-name (event)
  81. "Return file name of file notification event, or nil."
  82. (directory-file-name
  83. (expand-file-name
  84. (or (and (stringp (nth 2 event)) (nth 2 event)) "")
  85. (car (gethash (car event) file-notify-descriptors)))))
  86. ;; Only `gfilenotify' could return two file names.
  87. (defun file-notify--event-file1-name (event)
  88. "Return second file name of file notification event, or nil.
  89. This is available in case a file has been moved."
  90. (and (stringp (nth 3 event))
  91. (directory-file-name
  92. (expand-file-name
  93. (nth 3 event) (car (gethash (car event) file-notify-descriptors))))))
  94. ;; Cookies are offered by `inotify' only.
  95. (defun file-notify--event-cookie (event)
  96. "Return cookie of file notification event, or nil.
  97. This is available in case a file has been moved."
  98. (nth 3 event))
  99. ;; `inotify' returns the same descriptor when the file (directory)
  100. ;; uses the same inode. We want to distinguish, and apply a virtual
  101. ;; descriptor which make the difference.
  102. (defun file-notify--descriptor (desc file)
  103. "Return the descriptor to be used in `file-notify-*-watch'.
  104. For `gfilenotify' and `w32notify' it is the same descriptor as
  105. used in the low-level file notification package."
  106. (if (and (natnump desc) (eq file-notify--library 'inotify))
  107. (cons desc
  108. (and (stringp file)
  109. (car (assoc
  110. (file-name-nondirectory file)
  111. (gethash desc file-notify-descriptors)))))
  112. desc))
  113. ;; The callback function used to map between specific flags of the
  114. ;; respective file notifications, and the ones we return.
  115. (defun file-notify-callback (event)
  116. "Handle an EVENT returned from file notification.
  117. EVENT is the cadr of the event in `file-notify-handle-event'
  118. \(DESCRIPTOR ACTIONS FILE [FILE1-OR-COOKIE])."
  119. (let* ((desc (car event))
  120. (registered (gethash desc file-notify-descriptors))
  121. (actions (nth 1 event))
  122. (file (file-notify--event-file-name event))
  123. file1 callback pending-event stopped)
  124. ;; Make actions a list.
  125. (unless (consp actions) (setq actions (cons actions nil)))
  126. ;; Loop over registered entries. In fact, more than one entry
  127. ;; happens only for `inotify'.
  128. (dolist (entry (cdr registered))
  129. ;; Check, that event is meant for us.
  130. (unless (setq callback (cdr entry))
  131. (setq actions nil))
  132. ;; Loop over actions. In fact, more than one action happens only
  133. ;; for `inotify'.
  134. (dolist (action actions)
  135. ;; Send pending event, if it doesn't match.
  136. (when (and file-notify--pending-event
  137. ;; The cookie doesn't match.
  138. (not (eq (file-notify--event-cookie
  139. (car file-notify--pending-event))
  140. (file-notify--event-cookie event)))
  141. (or
  142. ;; inotify.
  143. (and (eq (nth 1 (car file-notify--pending-event))
  144. 'moved-from)
  145. (not (eq action 'moved-to)))
  146. ;; w32notify.
  147. (and (eq (nth 1 (car file-notify--pending-event))
  148. 'renamed-from)
  149. (not (eq action 'renamed-to)))))
  150. (setq pending-event file-notify--pending-event
  151. file-notify--pending-event nil)
  152. (setcar (cdar pending-event) 'deleted))
  153. ;; Map action. We ignore all events which cannot be mapped.
  154. (setq action
  155. (cond
  156. ;; gfilenotify.
  157. ((memq action '(attribute-changed changed created deleted))
  158. action)
  159. ((eq action 'moved)
  160. (setq file1 (file-notify--event-file1-name event))
  161. 'renamed)
  162. ;; inotify, w32notify.
  163. ((eq action 'ignored)
  164. (setq stopped t actions nil))
  165. ((eq action 'attrib) 'attribute-changed)
  166. ((memq action '(create added)) 'created)
  167. ((memq action '(modify modified)) 'changed)
  168. ((memq action '(delete delete-self move-self removed)) 'deleted)
  169. ;; Make the event pending.
  170. ((memq action '(moved-from renamed-from))
  171. (setq file-notify--pending-event
  172. `((,desc ,action ,file ,(file-notify--event-cookie event))
  173. ,callback))
  174. nil)
  175. ;; Look for pending event.
  176. ((memq action '(moved-to renamed-to))
  177. (if (null file-notify--pending-event)
  178. 'created
  179. (setq file1 file
  180. file (file-notify--event-file-name
  181. (car file-notify--pending-event)))
  182. ;; If the source is handled by another watch, we
  183. ;; must fire the rename event there as well.
  184. (when (not (equal (file-notify--descriptor desc file1)
  185. (file-notify--descriptor
  186. (caar file-notify--pending-event)
  187. (file-notify--event-file-name
  188. file-notify--pending-event))))
  189. (setq pending-event
  190. `((,(caar file-notify--pending-event)
  191. renamed ,file ,file1)
  192. ,(cadr file-notify--pending-event))))
  193. (setq file-notify--pending-event nil)
  194. 'renamed))))
  195. ;; Apply pending callback.
  196. (when pending-event
  197. (setcar
  198. (car pending-event)
  199. (file-notify--descriptor
  200. (caar pending-event)
  201. (file-notify--event-file-name file-notify--pending-event)))
  202. (funcall (cadr pending-event) (car pending-event))
  203. (setq pending-event nil))
  204. ;; Check for stopped.
  205. ;;(message "file-notify-callback %S %S" file registered)
  206. (setq
  207. stopped
  208. (or
  209. stopped
  210. (and
  211. (memq action '(deleted renamed))
  212. (= (length (cdr registered)) 1)
  213. (string-equal
  214. (file-name-nondirectory file)
  215. (or (file-name-nondirectory (car registered))
  216. (car (cadr registered)))))))
  217. ;; Apply callback.
  218. (when (and action
  219. (or
  220. ;; If there is no relative file name for that watch,
  221. ;; we watch the whole directory.
  222. (null (nth 0 entry))
  223. ;; File matches.
  224. (string-equal
  225. (nth 0 entry) (file-name-nondirectory file))
  226. ;; File1 matches.
  227. (and (stringp file1)
  228. (string-equal
  229. (nth 0 entry) (file-name-nondirectory file1)))))
  230. (if file1
  231. (funcall
  232. callback
  233. `(,(file-notify--descriptor desc file) ,action ,file ,file1))
  234. (funcall
  235. callback
  236. `(,(file-notify--descriptor desc file) ,action ,file)))))
  237. ;; Modify `file-notify-descriptors'.
  238. (when stopped
  239. (file-notify--rm-descriptor
  240. (file-notify--descriptor desc file) file)))))
  241. ;; `gfilenotify' and `w32notify' return a unique descriptor for every
  242. ;; `file-notify-add-watch', while `inotify' returns a unique
  243. ;; descriptor per inode only.
  244. (defun file-notify-add-watch (file flags callback)
  245. "Add a watch for filesystem events pertaining to FILE.
  246. This arranges for filesystem events pertaining to FILE to be reported
  247. to Emacs. Use `file-notify-rm-watch' to cancel the watch.
  248. The returned value is a descriptor for the added watch. If the
  249. file cannot be watched for some reason, this function signals a
  250. `file-notify-error' error.
  251. FLAGS is a list of conditions to set what will be watched for. It can
  252. include the following symbols:
  253. `change' -- watch for file changes
  254. `attribute-change' -- watch for file attributes changes, like
  255. permissions or modification time
  256. If FILE is a directory, `change' watches for file creation or
  257. deletion in that directory. This does not work recursively.
  258. When any event happens, Emacs will call the CALLBACK function passing
  259. it a single argument EVENT, which is of the form
  260. (DESCRIPTOR ACTION FILE [FILE1])
  261. DESCRIPTOR is the same object as the one returned by this function.
  262. ACTION is the description of the event. It could be any one of the
  263. following:
  264. `created' -- FILE was created
  265. `deleted' -- FILE was deleted
  266. `changed' -- FILE has changed
  267. `renamed' -- FILE has been renamed to FILE1
  268. `attribute-changed' -- a FILE attribute was changed
  269. `stopped' -- watching FILE has been stopped
  270. FILE is the name of the file whose event is being reported."
  271. ;; Check arguments.
  272. (unless (stringp file)
  273. (signal 'wrong-type-argument `(,file)))
  274. (setq file (expand-file-name file))
  275. (unless (and (consp flags)
  276. (null (delq 'change (delq 'attribute-change (copy-tree flags)))))
  277. (signal 'wrong-type-argument `(,flags)))
  278. (unless (functionp callback)
  279. (signal 'wrong-type-argument `(,callback)))
  280. (let* ((handler (find-file-name-handler file 'file-notify-add-watch))
  281. (dir (directory-file-name
  282. (if (file-directory-p file)
  283. file
  284. (file-name-directory file))))
  285. desc func l-flags registered)
  286. (unless (file-directory-p dir)
  287. (signal 'file-notify-error `("Directory does not exist" ,dir)))
  288. (if handler
  289. ;; A file name handler could exist even if there is no local
  290. ;; file notification support.
  291. (setq desc (funcall
  292. handler 'file-notify-add-watch dir flags callback))
  293. ;; Check, whether Emacs has been compiled with file notification
  294. ;; support.
  295. (unless file-notify--library
  296. (signal 'file-notify-error
  297. '("No file notification package available")))
  298. ;; Determine low-level function to be called.
  299. (setq func
  300. (cond
  301. ((eq file-notify--library 'gfilenotify) 'gfile-add-watch)
  302. ((eq file-notify--library 'inotify) 'inotify-add-watch)
  303. ((eq file-notify--library 'w32notify) 'w32notify-add-watch)))
  304. ;; Determine respective flags.
  305. (if (eq file-notify--library 'gfilenotify)
  306. (setq l-flags (append '(watch-mounts send-moved) flags))
  307. (when (memq 'change flags)
  308. (setq
  309. l-flags
  310. (cond
  311. ((eq file-notify--library 'inotify)
  312. '(create delete delete-self modify move-self move))
  313. ((eq file-notify--library 'w32notify)
  314. '(file-name directory-name size last-write-time)))))
  315. (when (memq 'attribute-change flags)
  316. (push (cond
  317. ((eq file-notify--library 'inotify) 'attrib)
  318. ((eq file-notify--library 'w32notify) 'attributes))
  319. l-flags)))
  320. ;; Call low-level function.
  321. (setq desc (funcall func dir l-flags 'file-notify-callback)))
  322. ;; Modify `file-notify-descriptors'.
  323. (setq registered (gethash desc file-notify-descriptors))
  324. (puthash
  325. desc
  326. `(,dir
  327. (,(unless (file-directory-p file) (file-name-nondirectory file))
  328. . ,callback)
  329. . ,(cdr registered))
  330. file-notify-descriptors)
  331. ;; Return descriptor.
  332. (file-notify--descriptor
  333. desc (unless (file-directory-p file) (file-name-nondirectory file)))))
  334. (defun file-notify-rm-watch (descriptor)
  335. "Remove an existing watch specified by its DESCRIPTOR.
  336. DESCRIPTOR should be an object returned by `file-notify-add-watch'."
  337. (let* ((desc (if (consp descriptor) (car descriptor) descriptor))
  338. (file (if (consp descriptor) (cdr descriptor)))
  339. (registered (gethash desc file-notify-descriptors))
  340. (dir (car registered))
  341. (handler (and (stringp dir)
  342. (find-file-name-handler dir 'file-notify-rm-watch))))
  343. (when (stringp dir)
  344. ;; Call low-level function.
  345. (when (or (not file)
  346. (and (= (length (cdr registered)) 1)
  347. (assoc file (cdr registered))))
  348. (condition-case nil
  349. (if handler
  350. ;; A file name handler could exist even if there is no local
  351. ;; file notification support.
  352. (funcall handler 'file-notify-rm-watch descriptor)
  353. (funcall
  354. (cond
  355. ((eq file-notify--library 'gfilenotify) 'gfile-rm-watch)
  356. ((eq file-notify--library 'inotify) 'inotify-rm-watch)
  357. ((eq file-notify--library 'w32notify) 'w32notify-rm-watch))
  358. desc))
  359. (file-notify-error nil)))
  360. ;; Modify `file-notify-descriptors'.
  361. (file-notify--rm-descriptor descriptor))))
  362. (defun file-notify-valid-p (descriptor)
  363. "Check a watch specified by its DESCRIPTOR.
  364. DESCRIPTOR should be an object returned by `file-notify-add-watch'."
  365. (let* ((desc (if (consp descriptor) (car descriptor) descriptor))
  366. (file (if (consp descriptor) (cdr descriptor)))
  367. (registered (gethash desc file-notify-descriptors))
  368. (dir (car registered))
  369. handler)
  370. (when (stringp dir)
  371. (setq handler (find-file-name-handler dir 'file-notify-valid-p))
  372. (and (or ;; It is a directory.
  373. (not file)
  374. ;; The file is registered.
  375. (assoc file (cdr registered)))
  376. (if handler
  377. ;; A file name handler could exist even if there is no
  378. ;; local file notification support.
  379. (funcall handler 'file-notify-valid-p descriptor)
  380. (funcall
  381. (cond
  382. ((eq file-notify--library 'gfilenotify) 'gfile-valid-p)
  383. ((eq file-notify--library 'inotify) 'inotify-valid-p)
  384. ((eq file-notify--library 'w32notify) 'w32notify-valid-p))
  385. desc))
  386. t))))
  387. ;; The end:
  388. (provide 'filenotify)
  389. ;;; filenotify.el ends here