filenotify.el 18 KB

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