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