tramp-cache.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. ;;; tramp-cache.el --- file information caching for Tramp
  2. ;; Copyright (C) 2000, 2005-2012 Free Software Foundation, Inc.
  3. ;; Author: Daniel Pittman <daniel@inanna.danann.net>
  4. ;; Michael Albinus <michael.albinus@gmx.de>
  5. ;; Keywords: comm, processes
  6. ;; Package: tramp
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; An implementation of information caching for remote files.
  20. ;; Each connection, identified by a vector [method user host
  21. ;; localname] or by a process, has a unique cache. We distinguish 3
  22. ;; kind of caches, depending on the key:
  23. ;;
  24. ;; - localname is NIL. This are reusable properties. Examples:
  25. ;; "remote-shell" identifies the POSIX shell to be called on the
  26. ;; remote host, or "perl" is the command to be called on the remote
  27. ;; host when starting a Perl script. These properties are saved in
  28. ;; the file `tramp-persistency-file-name'.
  29. ;;
  30. ;; - localname is a string. This are temporary properties, which are
  31. ;; related to the file localname is referring to. Examples:
  32. ;; "file-exists-p" is t or nile, depending on the file existence, or
  33. ;; "file-attributes" caches the result of the function
  34. ;; `file-attributes'.
  35. ;;
  36. ;; - The key is a process. This are temporary properties related to
  37. ;; an open connection. Examples: "scripts" keeps shell script
  38. ;; definitions already sent to the remote shell, "last-cmd-time" is
  39. ;; the time stamp a command has been sent to the remote process.
  40. ;;; Code:
  41. (require 'tramp)
  42. (autoload 'time-stamp-string "time-stamp")
  43. ;;; -- Cache --
  44. ;;;###tramp-autoload
  45. (defvar tramp-cache-data (make-hash-table :test 'equal)
  46. "Hash table for remote files properties.")
  47. (defcustom tramp-persistency-file-name
  48. (cond
  49. ;; GNU Emacs.
  50. ((and (fboundp 'locate-user-emacs-file))
  51. (expand-file-name (tramp-compat-funcall 'locate-user-emacs-file "tramp")))
  52. ((and (boundp 'user-emacs-directory)
  53. (stringp (symbol-value 'user-emacs-directory))
  54. (file-directory-p (symbol-value 'user-emacs-directory)))
  55. (expand-file-name "tramp" (symbol-value 'user-emacs-directory)))
  56. ((and (not (featurep 'xemacs)) (file-directory-p "~/.emacs.d/"))
  57. "~/.emacs.d/tramp")
  58. ;; XEmacs.
  59. ((and (boundp 'user-init-directory)
  60. (stringp (symbol-value 'user-init-directory))
  61. (file-directory-p (symbol-value 'user-init-directory)))
  62. (expand-file-name "tramp" (symbol-value 'user-init-directory)))
  63. ((and (featurep 'xemacs) (file-directory-p "~/.xemacs/"))
  64. "~/.xemacs/tramp")
  65. ;; For users without `~/.emacs.d/' or `~/.xemacs/'.
  66. (t "~/.tramp"))
  67. "File which keeps connection history for Tramp connections."
  68. :group 'tramp
  69. :type 'file)
  70. (defvar tramp-cache-data-changed nil
  71. "Whether persistent cache data have been changed.")
  72. ;;;###tramp-autoload
  73. (defun tramp-get-file-property (vec file property default)
  74. "Get the PROPERTY of FILE from the cache context of VEC.
  75. Returns DEFAULT if not set."
  76. ;; Unify localname.
  77. (setq vec (copy-sequence vec))
  78. (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
  79. (let* ((hash (or (gethash vec tramp-cache-data)
  80. (puthash vec (make-hash-table :test 'equal)
  81. tramp-cache-data)))
  82. (value (when (hash-table-p hash) (gethash property hash))))
  83. (if
  84. ;; We take the value only if there is any, and
  85. ;; `remote-file-name-inhibit-cache' indicates that it is still
  86. ;; valid. Otherwise, DEFAULT is set.
  87. (and (consp value)
  88. (or (null remote-file-name-inhibit-cache)
  89. (and (integerp remote-file-name-inhibit-cache)
  90. (<=
  91. (tramp-time-diff (current-time) (car value))
  92. remote-file-name-inhibit-cache))
  93. (and (consp remote-file-name-inhibit-cache)
  94. (tramp-time-less-p
  95. remote-file-name-inhibit-cache (car value)))))
  96. (setq value (cdr value))
  97. (setq value default))
  98. (tramp-message vec 8 "%s %s %s" file property value)
  99. (when (>= tramp-verbose 10)
  100. (let* ((var (intern (concat "tramp-cache-get-count-" property)))
  101. (val (or (ignore-errors (symbol-value var)) 0)))
  102. (set var (1+ val))))
  103. value))
  104. ;;;###tramp-autoload
  105. (defun tramp-set-file-property (vec file property value)
  106. "Set the PROPERTY of FILE to VALUE, in the cache context of VEC.
  107. Returns VALUE."
  108. ;; Unify localname.
  109. (setq vec (copy-sequence vec))
  110. (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
  111. (let ((hash (or (gethash vec tramp-cache-data)
  112. (puthash vec (make-hash-table :test 'equal)
  113. tramp-cache-data))))
  114. ;; We put the timestamp there.
  115. (puthash property (cons (current-time) value) hash)
  116. (tramp-message vec 8 "%s %s %s" file property value)
  117. (when (>= tramp-verbose 10)
  118. (let* ((var (intern (concat "tramp-cache-set-count-" property)))
  119. (val (or (ignore-errors (symbol-value var)) 0)))
  120. (set var (1+ val))))
  121. value))
  122. ;;;###tramp-autoload
  123. (defmacro with-file-property (vec file property &rest body)
  124. "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
  125. FILE must be a local file name on a connection identified via VEC."
  126. `(if (file-name-absolute-p ,file)
  127. (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
  128. (when (eq value 'undef)
  129. ;; We cannot pass @body as parameter to
  130. ;; `tramp-set-file-property' because it mangles our
  131. ;; debug messages.
  132. (setq value (progn ,@body))
  133. (tramp-set-file-property ,vec ,file ,property value))
  134. value)
  135. ,@body))
  136. ;;;###tramp-autoload
  137. (put 'with-file-property 'lisp-indent-function 3)
  138. (put 'with-file-property 'edebug-form-spec t)
  139. (tramp-compat-font-lock-add-keywords
  140. 'emacs-lisp-mode '("\\<with-file-property\\>"))
  141. ;;;###tramp-autoload
  142. (defun tramp-flush-file-property (vec file)
  143. "Remove all properties of FILE in the cache context of VEC."
  144. ;; Remove file property of symlinks.
  145. (let ((truename (tramp-get-file-property vec file "file-truename" nil)))
  146. (when (and (stringp truename)
  147. (not (string-equal file truename)))
  148. (tramp-flush-file-property vec truename)))
  149. ;; Unify localname.
  150. (setq vec (copy-sequence vec))
  151. (aset vec 3 (tramp-run-real-handler 'directory-file-name (list file)))
  152. (tramp-message vec 8 "%s" file)
  153. (remhash vec tramp-cache-data))
  154. ;;;###tramp-autoload
  155. (defun tramp-flush-directory-property (vec directory)
  156. "Remove all properties of DIRECTORY in the cache context of VEC.
  157. Remove also properties of all files in subdirectories."
  158. (let ((directory (tramp-run-real-handler
  159. 'directory-file-name (list directory))))
  160. (tramp-message vec 8 "%s" directory)
  161. (maphash
  162. (lambda (key value)
  163. (when (and (stringp (tramp-file-name-localname key))
  164. (string-match directory (tramp-file-name-localname key)))
  165. (remhash key tramp-cache-data)))
  166. tramp-cache-data)))
  167. ;; Reverting or killing a buffer should also flush file properties.
  168. ;; They could have been changed outside Tramp. In eshell, "ls" would
  169. ;; not show proper directory contents when a file has been copied or
  170. ;; deleted before.
  171. (defun tramp-flush-file-function ()
  172. "Flush all Tramp cache properties from `buffer-file-name'."
  173. (let ((bfn (if (stringp (buffer-file-name))
  174. (buffer-file-name)
  175. default-directory)))
  176. (when (tramp-tramp-file-p bfn)
  177. (with-parsed-tramp-file-name bfn nil
  178. (tramp-flush-file-property v localname)))))
  179. (add-hook 'before-revert-hook 'tramp-flush-file-function)
  180. (add-hook 'eshell-pre-command-hook 'tramp-flush-file-function)
  181. (add-hook 'kill-buffer-hook 'tramp-flush-file-function)
  182. (add-hook 'tramp-cache-unload-hook
  183. (lambda ()
  184. (remove-hook 'before-revert-hook
  185. 'tramp-flush-file-function)
  186. (remove-hook 'eshell-pre-command-hook
  187. 'tramp-flush-file-function)
  188. (remove-hook 'kill-buffer-hook
  189. 'tramp-flush-file-function)))
  190. ;;; -- Properties --
  191. ;;;###tramp-autoload
  192. (defun tramp-get-connection-property (key property default)
  193. "Get the named PROPERTY for the connection.
  194. KEY identifies the connection, it is either a process or a vector.
  195. If the value is not set for the connection, returns DEFAULT."
  196. ;; Unify key by removing localname from vector. Work with a copy in
  197. ;; order to avoid side effects.
  198. (when (vectorp key)
  199. (setq key (copy-sequence key))
  200. (aset key 3 nil))
  201. (let* ((hash (gethash key tramp-cache-data))
  202. (value (if (hash-table-p hash)
  203. (gethash property hash default)
  204. default)))
  205. (tramp-message key 7 "%s %s" property value)
  206. value))
  207. ;;;###tramp-autoload
  208. (defun tramp-set-connection-property (key property value)
  209. "Set the named PROPERTY of a connection to VALUE.
  210. KEY identifies the connection, it is either a process or a vector.
  211. PROPERTY is set persistent when KEY is a vector."
  212. ;; Unify key by removing localname from vector. Work with a copy in
  213. ;; order to avoid side effects.
  214. (when (vectorp key)
  215. (setq key (copy-sequence key))
  216. (aset key 3 nil))
  217. (let ((hash (or (gethash key tramp-cache-data)
  218. (puthash key (make-hash-table :test 'equal)
  219. tramp-cache-data))))
  220. (puthash property value hash)
  221. (setq tramp-cache-data-changed t)
  222. (tramp-message key 7 "%s %s" property value)
  223. value))
  224. ;;;###tramp-autoload
  225. (defmacro with-connection-property (key property &rest body)
  226. "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
  227. `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
  228. (when (eq value 'undef)
  229. ;; We cannot pass ,@body as parameter to
  230. ;; `tramp-set-connection-property' because it mangles our debug
  231. ;; messages.
  232. (setq value (progn ,@body))
  233. (tramp-set-connection-property ,key ,property value))
  234. value))
  235. ;;;###tramp-autoload
  236. (put 'with-connection-property 'lisp-indent-function 2)
  237. (put 'with-connection-property 'edebug-form-spec t)
  238. (tramp-compat-font-lock-add-keywords
  239. 'emacs-lisp-mode '("\\<with-connection-property\\>"))
  240. ;;;###tramp-autoload
  241. (defun tramp-flush-connection-property (key)
  242. "Remove all properties identified by KEY.
  243. KEY identifies the connection, it is either a process or a vector."
  244. ;; Unify key by removing localname from vector. Work with a copy in
  245. ;; order to avoid side effects.
  246. (when (vectorp key)
  247. (setq key (copy-sequence key))
  248. (aset key 3 nil))
  249. (tramp-message
  250. key 7 "%s %s" key
  251. (let ((hash (gethash key tramp-cache-data))
  252. properties)
  253. (if (hash-table-p hash)
  254. (maphash
  255. (lambda (x y) (add-to-list 'properties x 'append))
  256. (gethash key tramp-cache-data)))
  257. properties))
  258. (setq tramp-cache-data-changed t)
  259. (remhash key tramp-cache-data))
  260. ;;;###tramp-autoload
  261. (defun tramp-cache-print (table)
  262. "Print hash table TABLE."
  263. (when (hash-table-p table)
  264. (let (result)
  265. (maphash
  266. (lambda (key value)
  267. (let ((tmp (format
  268. "(%s %s)"
  269. (if (processp key)
  270. (prin1-to-string (prin1-to-string key))
  271. (prin1-to-string key))
  272. (if (hash-table-p value)
  273. (tramp-cache-print value)
  274. (if (bufferp value)
  275. (prin1-to-string (prin1-to-string value))
  276. (prin1-to-string value))))))
  277. (setq result (if result (concat result " " tmp) tmp))))
  278. table)
  279. result)))
  280. ;;;###tramp-autoload
  281. (defun tramp-list-connections ()
  282. "Return a list of all known connection vectors according to `tramp-cache'."
  283. (let (result)
  284. (maphash
  285. (lambda (key value)
  286. (when (and (vectorp key) (null (aref key 3)))
  287. (add-to-list 'result key)))
  288. tramp-cache-data)
  289. result))
  290. (defun tramp-dump-connection-properties ()
  291. "Write persistent connection properties into file `tramp-persistency-file-name'."
  292. ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
  293. (ignore-errors
  294. (when (and (hash-table-p tramp-cache-data)
  295. (not (zerop (hash-table-count tramp-cache-data)))
  296. tramp-cache-data-changed
  297. (stringp tramp-persistency-file-name))
  298. (let ((cache (copy-hash-table tramp-cache-data)))
  299. ;; Remove temporary data. If there is the key "login-as", we
  300. ;; don't save either, because all other properties might
  301. ;; depend on the login name, and we want to give the
  302. ;; possibility to use another login name later on.
  303. (maphash
  304. (lambda (key value)
  305. (if (and (vectorp key)
  306. (not (tramp-file-name-localname key))
  307. (not (gethash "login-as" value)))
  308. (progn
  309. (remhash "process-name" value)
  310. (remhash "process-buffer" value)
  311. (remhash "first-password-request" value))
  312. (remhash key cache)))
  313. cache)
  314. ;; Dump it.
  315. (with-temp-buffer
  316. (insert
  317. ";; -*- emacs-lisp -*-"
  318. ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
  319. (condition-case nil
  320. (progn
  321. (format
  322. " <%s %s>\n"
  323. (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
  324. tramp-persistency-file-name))
  325. (error "\n"))
  326. ";; Tramp connection history. Don't change this file.\n"
  327. ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
  328. (with-output-to-string
  329. (pp (read (format "(%s)" (tramp-cache-print cache))))))
  330. (write-region
  331. (point-min) (point-max) tramp-persistency-file-name))))))
  332. (unless noninteractive
  333. (add-hook 'kill-emacs-hook 'tramp-dump-connection-properties))
  334. (add-hook 'tramp-cache-unload-hook
  335. (lambda ()
  336. (remove-hook 'kill-emacs-hook
  337. 'tramp-dump-connection-properties)))
  338. ;;;###tramp-autoload
  339. (defun tramp-parse-connection-properties (method)
  340. "Return a list of (user host) tuples allowed to access for METHOD.
  341. This function is added always in `tramp-get-completion-function'
  342. for all methods. Resulting data are derived from connection history."
  343. (let (res)
  344. (maphash
  345. (lambda (key value)
  346. (if (and (vectorp key)
  347. (string-equal method (tramp-file-name-method key))
  348. (not (tramp-file-name-localname key)))
  349. (push (list (tramp-file-name-user key)
  350. (tramp-file-name-host key))
  351. res)))
  352. tramp-cache-data)
  353. res))
  354. ;; Read persistent connection history.
  355. (when (and (stringp tramp-persistency-file-name)
  356. (zerop (hash-table-count tramp-cache-data))
  357. ;; When "emacs -Q" has been called, both variables are nil.
  358. ;; We do not load the persistency file then, in order to
  359. ;; have a clean test environment.
  360. (or (and (boundp 'init-file-user) (symbol-value 'init-file-user))
  361. (and (boundp 'site-run-file) (symbol-value 'site-run-file))))
  362. (condition-case err
  363. (with-temp-buffer
  364. (insert-file-contents tramp-persistency-file-name)
  365. (let ((list (read (current-buffer)))
  366. element key item)
  367. (while (setq element (pop list))
  368. (setq key (pop element))
  369. (while (setq item (pop element))
  370. (tramp-set-connection-property key (pop item) (car item)))))
  371. (setq tramp-cache-data-changed nil))
  372. (file-error
  373. ;; Most likely because the file doesn't exist yet. No message.
  374. (clrhash tramp-cache-data))
  375. (error
  376. ;; File is corrupted.
  377. (message "Tramp persistency file '%s' is corrupted: %s"
  378. tramp-persistency-file-name (error-message-string err))
  379. (clrhash tramp-cache-data))))
  380. (add-hook 'tramp-unload-hook
  381. (lambda ()
  382. (unload-feature 'tramp-cache 'force)))
  383. (provide 'tramp-cache)
  384. ;;; tramp-cache.el ends here