tramp-cache.el 18 KB

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