db-file.el 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. ;;; semantic/db-file.el --- Save a semanticdb to a cache file.
  2. ;;; Copyright (C) 2000-2005, 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: tags
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; A set of semanticdb classes for persistently saving caches on disk.
  19. ;;
  20. (require 'semantic)
  21. (require 'semantic/db)
  22. (require 'cedet-files)
  23. (defvar semanticdb-file-version semantic-version
  24. "Version of semanticdb we are writing files to disk with.")
  25. (defvar semanticdb-file-incompatible-version "1.4"
  26. "Version of semanticdb we are not reverse compatible with.")
  27. ;;; Settings
  28. ;;
  29. (defcustom semanticdb-default-file-name "semantic.cache"
  30. "File name of the semantic tag cache."
  31. :group 'semanticdb
  32. :type 'string)
  33. (defcustom semanticdb-default-save-directory
  34. (locate-user-emacs-file "semanticdb" ".semanticdb")
  35. "Directory name where semantic cache files are stored.
  36. If this value is nil, files are saved in the current directory. If the value
  37. is a valid directory, then it overrides `semanticdb-default-file-name' and
  38. stores caches in a coded file name in this directory."
  39. :group 'semanticdb
  40. :type '(choice :tag "Default-Directory"
  41. :menu-tag "Default-Directory"
  42. (const :tag "Use current directory" :value nil)
  43. (directory)))
  44. (defcustom semanticdb-persistent-path '(always)
  45. "List of valid paths that semanticdb will cache tags to.
  46. When `global-semanticdb-minor-mode' is active, tag lists will
  47. be saved to disk when Emacs exits. Not all directories will have
  48. tags that should be saved.
  49. The value should be a list of valid paths. A path can be a string,
  50. indicating a directory in which to save a variable. An element in the
  51. list can also be a symbol. Valid symbols are `never', which will
  52. disable any saving anywhere, `always', which enables saving
  53. everywhere, or `project', which enables saving in any directory that
  54. passes a list of predicates in `semanticdb-project-predicate-functions'."
  55. :group 'semanticdb
  56. :type nil)
  57. (defcustom semanticdb-save-database-hooks nil
  58. "Abnormal hook run after a database is saved.
  59. Each function is called with one argument, the object representing
  60. the database recently written."
  61. :group 'semanticdb
  62. :type 'hook)
  63. (defvar semanticdb-dir-sep-char (if (boundp 'directory-sep-char)
  64. (symbol-value 'directory-sep-char)
  65. ?/)
  66. "Character used for directory separation.
  67. Obsoleted in some versions of Emacs. Needed in others.
  68. NOTE: This should get deleted from semantic soon.")
  69. (defun semanticdb-fix-pathname (dir)
  70. "If DIR is broken, fix it.
  71. Force DIR to end with a /.
  72. Note: Same as `file-name-as-directory'.
  73. NOTE: This should get deleted from semantic soon."
  74. (file-name-as-directory dir))
  75. ;; I didn't initially know about the above fcn. Keep the below as a
  76. ;; reference. Delete it someday once I've proven everything is the same.
  77. ;; (if (not (= semanticdb-dir-sep-char (aref path (1- (length path)))))
  78. ;; (concat path (list semanticdb-dir-sep-char))
  79. ;; path))
  80. ;;; Classes
  81. ;;
  82. ;;;###autoload
  83. (defclass semanticdb-project-database-file (semanticdb-project-database
  84. eieio-persistent)
  85. ((file-header-line :initform ";; SEMANTICDB Tags save file")
  86. (do-backups :initform nil)
  87. (semantic-tag-version :initarg :semantic-tag-version
  88. :initform "1.4"
  89. :documentation
  90. "The version of the tags saved.
  91. The default value is 1.4. In semantic 1.4 there was no versioning, so
  92. when those files are loaded, this becomes the version number.
  93. To save the version number, we must hand-set this version string.")
  94. (semanticdb-version :initarg :semanticdb-version
  95. :initform "1.4"
  96. :documentation
  97. "The version of the object system saved.
  98. The default value is 1.4. In semantic 1.4, there was no versioning,
  99. so when those files are loaded, this becomes the version number.
  100. To save the version number, we must hand-set this version string.")
  101. )
  102. "Database of file tables saved to disk.")
  103. ;;; Code:
  104. ;;
  105. (defmethod semanticdb-create-database :STATIC ((dbc semanticdb-project-database-file)
  106. directory)
  107. "Create a new semantic database for DIRECTORY and return it.
  108. If a database for DIRECTORY has already been loaded, return it.
  109. If a database for DIRECTORY exists, then load that database, and return it.
  110. If DIRECTORY doesn't exist, create a new one."
  111. ;; Make sure this is fully expanded so we don't get duplicates.
  112. (setq directory (file-truename directory))
  113. (let* ((fn (semanticdb-cache-filename dbc directory))
  114. (db (or (semanticdb-file-loaded-p fn)
  115. (if (file-exists-p fn)
  116. (progn
  117. (semanticdb-load-database fn))))))
  118. (unless db
  119. (setq db (make-instance
  120. dbc ; Create the database requested. Perhaps
  121. (concat (file-name-nondirectory
  122. (directory-file-name
  123. directory))
  124. "/")
  125. :file fn :tables nil
  126. :semantic-tag-version semantic-version
  127. :semanticdb-version semanticdb-file-version)))
  128. ;; Set this up here. We can't put it in the constructor because it
  129. ;; would be saved, and we want DB files to be portable.
  130. (oset db reference-directory directory)
  131. db))
  132. ;;; File IO
  133. (declare-function inversion-test "inversion")
  134. (defun semanticdb-load-database (filename)
  135. "Load the database FILENAME."
  136. (condition-case foo
  137. (let* ((r (eieio-persistent-read filename))
  138. (c (semanticdb-get-database-tables r))
  139. (tv (oref r semantic-tag-version))
  140. (fv (oref r semanticdb-version))
  141. )
  142. ;; Restore the parent-db connection
  143. (while c
  144. (oset (car c) parent-db r)
  145. (setq c (cdr c)))
  146. (unless (and (equal semanticdb-file-version fv)
  147. (equal semantic-tag-version tv))
  148. ;; Try not to load inversion unless we need it:
  149. (require 'inversion)
  150. (if (not (inversion-test 'semanticdb-file fv))
  151. (when (inversion-test 'semantic-tag tv)
  152. ;; Incompatible version. Flush tables.
  153. (semanticdb-flush-database-tables r)
  154. ;; Reset the version to new version.
  155. (oset r semantic-tag-version semantic-tag-version)
  156. ;; Warn user
  157. (message "Semanticdb file is old. Starting over for %s"
  158. filename))
  159. ;; Version is not ok. Flush whole system
  160. (message "semanticdb file is old. Starting over for %s"
  161. filename)
  162. ;; This database is so old, we need to replace it.
  163. ;; We also need to delete it from the instance tracker.
  164. (delete-instance r)
  165. (setq r nil)))
  166. r)
  167. (error (message "Cache Error: [%s] %s, Restart"
  168. filename foo)
  169. nil)))
  170. (defun semanticdb-file-loaded-p (filename)
  171. "Return the project belonging to FILENAME if it was already loaded."
  172. (eieio-instance-tracker-find filename 'file 'semanticdb-database-list))
  173. (defmethod semanticdb-file-directory-exists-p ((DB semanticdb-project-database-file)
  174. &optional suppress-questions)
  175. "Does the directory the database DB needs to write to exist?
  176. If SUPPRESS-QUESTIONS, then do not ask to create the directory."
  177. (let ((dest (file-name-directory (oref DB file)))
  178. )
  179. (cond ((null dest)
  180. ;; @TODO - If it was never set up... what should we do ?
  181. nil)
  182. ((file-exists-p dest) t)
  183. ((or suppress-questions
  184. (and (boundp 'semanticdb--inhibit-make-directory)
  185. semanticdb--inhibit-make-directory))
  186. nil)
  187. ((y-or-n-p (format "Create directory %s for SemanticDB? " dest))
  188. (make-directory dest t)
  189. t)
  190. (t
  191. (if (boundp 'semanticdb--inhibit-make-directory)
  192. (setq semanticdb--inhibit-make-directory t))
  193. nil))))
  194. (defmethod semanticdb-save-db ((DB semanticdb-project-database-file)
  195. &optional
  196. suppress-questions)
  197. "Write out the database DB to its file.
  198. If DB is not specified, then use the current database."
  199. (let ((objname (oref DB file)))
  200. (when (and (semanticdb-dirty-p DB)
  201. (semanticdb-live-p DB)
  202. (semanticdb-file-directory-exists-p DB suppress-questions)
  203. (semanticdb-write-directory-p DB)
  204. )
  205. ;;(message "Saving tag summary for %s..." objname)
  206. (condition-case foo
  207. (eieio-persistent-save (or DB semanticdb-current-database))
  208. (file-error ; System error saving? Ignore it.
  209. (message "%S: %s" foo objname))
  210. (error
  211. (cond
  212. ((and (listp foo)
  213. (stringp (nth 1 foo))
  214. (string-match "write[- ]protected" (nth 1 foo)))
  215. (message (nth 1 foo)))
  216. ((and (listp foo)
  217. (stringp (nth 1 foo))
  218. (string-match "no such directory" (nth 1 foo)))
  219. (message (nth 1 foo)))
  220. (t
  221. ;; @todo - It should ask if we are not called from a hook.
  222. ;; How?
  223. (if (or suppress-questions
  224. (y-or-n-p (format "Skip Error: %s ?" (car (cdr foo)))))
  225. (message "Save Error: %S: %s" (car (cdr foo))
  226. objname)
  227. (error "%S" (car (cdr foo))))))))
  228. (run-hook-with-args 'semanticdb-save-database-hooks
  229. (or DB semanticdb-current-database))
  230. ;;(message "Saving tag summary for %s...done" objname)
  231. )
  232. ))
  233. (defmethod semanticdb-live-p ((obj semanticdb-project-database))
  234. "Return non-nil if the file associated with OBJ is live.
  235. Live databases are objects associated with existing directories."
  236. (and (slot-boundp obj 'reference-directory)
  237. (file-exists-p (oref obj reference-directory))))
  238. (defmethod semanticdb-live-p ((obj semanticdb-table))
  239. "Return non-nil if the file associated with OBJ is live.
  240. Live files are either buffers in Emacs, or files existing on the filesystem."
  241. (let ((full-filename (semanticdb-full-filename obj)))
  242. (or (find-buffer-visiting full-filename)
  243. (file-exists-p full-filename))))
  244. (defvar semanticdb-data-debug-on-write-error nil
  245. "Run the data debugger on tables that issue errors.
  246. This variable is set to nil after the first error is encountered
  247. to prevent overload.")
  248. (declare-function data-debug-insert-thing "data-debug")
  249. (defmethod object-write ((obj semanticdb-table))
  250. "When writing a table, we have to make sure we deoverlay it first.
  251. Restore the overlays after writing.
  252. Argument OBJ is the object to write."
  253. (when (semanticdb-live-p obj)
  254. (when (semanticdb-in-buffer-p obj)
  255. (with-current-buffer (semanticdb-in-buffer-p obj)
  256. ;; Make sure all our tag lists are up to date.
  257. (semantic-fetch-tags)
  258. ;; Try to get an accurate unmatched syntax table.
  259. (when (and (boundp semantic-show-unmatched-syntax-mode)
  260. semantic-show-unmatched-syntax-mode)
  261. ;; Only do this if the user runs unmatched syntax
  262. ;; mode display entries.
  263. (oset obj unmatched-syntax
  264. (semantic-show-unmatched-lex-tokens-fetch))
  265. )
  266. ;; Make sure pointmax is up to date
  267. (oset obj pointmax (point-max))
  268. ))
  269. ;; Make sure that the file size and other attributes are
  270. ;; up to date.
  271. (let ((fattr (file-attributes (semanticdb-full-filename obj))))
  272. (oset obj fsize (nth 7 fattr))
  273. (oset obj lastmodtime (nth 5 fattr))
  274. )
  275. ;; Do it!
  276. (condition-case tableerror
  277. (call-next-method)
  278. (error
  279. (when semanticdb-data-debug-on-write-error
  280. (require 'data-debug)
  281. (data-debug-new-buffer (concat "*SEMANTICDB ERROR*"))
  282. (data-debug-insert-thing obj "*" "")
  283. (setq semanticdb-data-debug-on-write-error nil))
  284. (message "Error Writing Table: %s" (object-name obj))
  285. (error "%S" (car (cdr tableerror)))))
  286. ;; Clear the dirty bit.
  287. (oset obj dirty nil)
  288. ))
  289. ;;; State queries
  290. ;;
  291. (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database-file))
  292. "Return non-nil if OBJ should be written to disk.
  293. Uses `semanticdb-persistent-path' to determine the return value."
  294. (let ((path semanticdb-persistent-path))
  295. (catch 'found
  296. (while path
  297. (cond ((stringp (car path))
  298. (if (string= (oref obj reference-directory) (car path))
  299. (throw 'found t)))
  300. ((eq (car path) 'project)
  301. ;; @TODO - EDE causes us to go in here and disable
  302. ;; the old default 'always save' setting.
  303. ;;
  304. ;; With new default 'always' should I care?
  305. (if semanticdb-project-predicate-functions
  306. (if (run-hook-with-args-until-success
  307. 'semanticdb-project-predicate-functions
  308. (oref obj reference-directory))
  309. (throw 'found t))
  310. ;; If the mode is 'project, and there are no project
  311. ;; modes, then just always save the file. If users
  312. ;; wish to restrict the search, modify
  313. ;; `semanticdb-persistent-path' to include desired paths.
  314. (if (= (length semanticdb-persistent-path) 1)
  315. (throw 'found t))
  316. ))
  317. ((eq (car path) 'never)
  318. (throw 'found nil))
  319. ((eq (car path) 'always)
  320. (throw 'found t))
  321. (t (error "Invalid path %S" (car path))))
  322. (setq path (cdr path)))
  323. (call-next-method))
  324. ))
  325. ;;; Filename manipulation
  326. ;;
  327. (defmethod semanticdb-file-table ((obj semanticdb-project-database-file) filename)
  328. "From OBJ, return FILENAME's associated table object."
  329. ;; Cheater option. In this case, we always have files directly
  330. ;; under ourselves. The main project type may not.
  331. (object-assoc (file-name-nondirectory filename) 'file (oref obj tables)))
  332. (defmethod semanticdb-file-name-non-directory :STATIC
  333. ((dbclass semanticdb-project-database-file))
  334. "Return the file name DBCLASS will use.
  335. File name excludes any directory part."
  336. semanticdb-default-file-name)
  337. (defmethod semanticdb-file-name-directory :STATIC
  338. ((dbclass semanticdb-project-database-file) directory)
  339. "Return the relative directory to where DBCLASS will save its cache file.
  340. The returned path is related to DIRECTORY."
  341. (if semanticdb-default-save-directory
  342. (let ((file (cedet-directory-name-to-file-name directory)))
  343. ;; Now create a filename for the cache file in
  344. ;; ;`semanticdb-default-save-directory'.
  345. (expand-file-name
  346. file (file-name-as-directory semanticdb-default-save-directory)))
  347. directory))
  348. (defmethod semanticdb-cache-filename :STATIC
  349. ((dbclass semanticdb-project-database-file) path)
  350. "For DBCLASS, return a file to a cache file belonging to PATH.
  351. This could be a cache file in the current directory, or an encoded file
  352. name in a secondary directory."
  353. ;; Use concat and not expand-file-name, because the dir part
  354. ;; may include some of the file name.
  355. (concat (semanticdb-file-name-directory dbclass path)
  356. (semanticdb-file-name-non-directory dbclass)))
  357. (defmethod semanticdb-full-filename ((obj semanticdb-project-database-file))
  358. "Fetch the full filename that OBJ refers to."
  359. (oref obj file))
  360. ;;; FLUSH OLD FILES
  361. ;;
  362. (defun semanticdb-cleanup-cache-files (&optional noerror)
  363. "Cleanup any cache files associated with directories that no longer exist.
  364. Optional NOERROR prevents errors from being displayed."
  365. (interactive)
  366. (when (and (not semanticdb-default-save-directory)
  367. (not noerror))
  368. (error "No default save directory for semantic-save files"))
  369. (when semanticdb-default-save-directory
  370. ;; Calculate all the cache files we have.
  371. (let* ((regexp (regexp-quote semanticdb-default-file-name))
  372. (files (directory-files semanticdb-default-save-directory
  373. t regexp))
  374. (orig nil)
  375. (to-delete nil))
  376. (dolist (F files)
  377. (setq orig (cedet-file-name-to-directory-name
  378. (file-name-nondirectory F)))
  379. (when (not (file-exists-p (file-name-directory orig)))
  380. (setq to-delete (cons F to-delete))
  381. ))
  382. (if to-delete
  383. (save-window-excursion
  384. (let ((buff (get-buffer-create "*Semanticdb Delete*")))
  385. (with-current-buffer buff
  386. (erase-buffer)
  387. (insert "The following Cache files appear to be obsolete.\n\n")
  388. (dolist (F to-delete)
  389. (insert F "\n")))
  390. (pop-to-buffer buff t t)
  391. (fit-window-to-buffer (get-buffer-window buff) nil 1)
  392. (when (y-or-n-p "Delete Old Cache Files? ")
  393. (mapc (lambda (F)
  394. (message "Deleting to %s..." F)
  395. (delete-file F))
  396. to-delete)
  397. (message "done."))
  398. ))
  399. ;; No files to delete
  400. (when (not noerror)
  401. (message "No obsolete semanticdb.cache files."))
  402. ))))
  403. (provide 'semantic/db-file)
  404. ;; Local variables:
  405. ;; generated-autoload-file: "loaddefs.el"
  406. ;; generated-autoload-load-name: "semantic/db-file"
  407. ;; End:
  408. ;;; semantic/db-file.el ends here