1 ;;; semantic/db-file.el --- Save a semanticdb to a cache file.
3 ;;; Copyright (C) 2000-2005, 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
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 <http://www.gnu.org/licenses/>.
25 ;; A set of semanticdb classes for persistently saving caches on disk.
28 (require 'semantic
/db
)
29 (require 'cedet-files
)
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.")
39 (defcustom semanticdb-default-file-name
"semantic.cache"
40 "File name of the semantic tag cache."
44 (defcustom semanticdb-default-save-directory
45 (locate-user-emacs-file "semanticdb" ".semanticdb")
46 "Directory name where semantic cache files are stored.
47 If this value is nil, files are saved in the current directory. If the value
48 is a valid directory, then it overrides `semanticdb-default-file-name' and
49 stores caches in a coded file name in this directory."
51 :type
'(choice :tag
"Default-Directory"
52 :menu-tag
"Default-Directory"
53 (const :tag
"Use current directory" :value nil
)
56 (defcustom semanticdb-persistent-path
'(always)
57 "List of valid paths that semanticdb will cache tags to.
58 When `global-semanticdb-minor-mode' is active, tag lists will
59 be saved to disk when Emacs exits. Not all directories will have
60 tags that should be saved.
61 The value should be a list of valid paths. A path can be a string,
62 indicating a directory in which to save a variable. An element in the
63 list can also be a symbol. Valid symbols are `never', which will
64 disable any saving anywhere, `always', which enables saving
65 everywhere, or `project', which enables saving in any directory that
66 passes a list of predicates in `semanticdb-project-predicate-functions'."
70 (define-obsolete-variable-alias 'semanticdb-save-database-hooks
71 'semanticdb-save-database-functions
"24.3")
72 (defcustom semanticdb-save-database-functions nil
73 "Abnormal hook run after a database is saved.
74 Each function is called with one argument, the object representing
75 the database recently written."
79 (defvar semanticdb-dir-sep-char
(if (boundp 'directory-sep-char
)
80 (symbol-value 'directory-sep-char
)
82 "Character used for directory separation.
83 Obsoleted in some versions of Emacs. Needed in others.
84 NOTE: This should get deleted from semantic soon.")
86 (defun semanticdb-fix-pathname (dir)
87 "If DIR is broken, fix it.
88 Force DIR to end with a /.
89 Note: Same as `file-name-as-directory'.
90 NOTE: This should get deleted from semantic soon."
91 (file-name-as-directory dir
))
92 ;; I didn't initially know about the above fcn. Keep the below as a
93 ;; reference. Delete it someday once I've proven everything is the same.
94 ;; (if (not (= semanticdb-dir-sep-char (aref path (1- (length path)))))
95 ;; (concat path (list semanticdb-dir-sep-char))
101 (defclass semanticdb-project-database-file
(semanticdb-project-database
103 ((file-header-line :initform
";; SEMANTICDB Tags save file")
104 (do-backups :initform nil
)
105 (semantic-tag-version :initarg
:semantic-tag-version
108 "The version of the tags saved.
109 The default value is 1.4. In semantic 1.4 there was no versioning, so
110 when those files are loaded, this becomes the version number.
111 To save the version number, we must hand-set this version string.")
112 (semanticdb-version :initarg
:semanticdb-version
115 "The version of the object system saved.
116 The default value is 1.4. In semantic 1.4, there was no versioning,
117 so when those files are loaded, this becomes the version number.
118 To save the version number, we must hand-set this version string.")
120 "Database of file tables saved to disk.")
124 (defmethod semanticdb-create-database :STATIC
((dbc semanticdb-project-database-file
)
126 "Create a new semantic database for DIRECTORY and return it.
127 If a database for DIRECTORY has already been loaded, return it.
128 If a database for DIRECTORY exists, then load that database, and return it.
129 If DIRECTORY doesn't exist, create a new one."
130 ;; Make sure this is fully expanded so we don't get duplicates.
131 (setq directory
(file-truename directory
))
132 (let* ((fn (semanticdb-cache-filename dbc directory
))
133 (db (or (semanticdb-file-loaded-p fn
)
134 (if (file-exists-p fn
)
136 (semanticdb-load-database fn
))))))
138 (setq db
(make-instance
139 dbc
; Create the database requested. Perhaps
140 (concat (file-name-nondirectory
145 :semantic-tag-version semantic-tag-version
146 :semanticdb-version semanticdb-file-version
)))
147 ;; Set this up here. We can't put it in the constructor because it
148 ;; would be saved, and we want DB files to be portable.
149 (oset db reference-directory directory
)
154 (declare-function inversion-test
"inversion")
156 (defun semanticdb-load-database (filename)
157 "Load the database FILENAME."
159 (let* ((r (eieio-persistent-read filename semanticdb-project-database-file
))
160 (c (semanticdb-get-database-tables r
))
161 (tv (oref r semantic-tag-version
))
162 (fv (oref r semanticdb-version
))
164 ;; Restore the parent-db connection
166 (oset (car c
) parent-db r
)
168 (unless (and (equal semanticdb-file-version fv
)
169 (equal semantic-tag-version tv
))
170 ;; Try not to load inversion unless we need it:
172 (if (not (inversion-test 'semanticdb-file fv
))
173 (when (inversion-test 'semantic-tag tv
)
174 ;; Incompatible version. Flush tables.
175 (semanticdb-flush-database-tables r
)
176 ;; Reset the version to new version.
177 (oset r semantic-tag-version semantic-tag-version
)
179 (message "Semanticdb file is old. Starting over for %s"
181 ;; Version is not ok. Flush whole system
182 (message "semanticdb file is old. Starting over for %s"
184 ;; This database is so old, we need to replace it.
185 ;; We also need to delete it from the instance tracker.
189 (error (message "Cache Error: [%s] %s, Restart"
193 (defun semanticdb-file-loaded-p (filename)
194 "Return the project belonging to FILENAME if it was already loaded."
195 (eieio-instance-tracker-find filename
'file
'semanticdb-database-list
))
197 (defmethod semanticdb-file-directory-exists-p ((DB semanticdb-project-database-file
)
198 &optional suppress-questions
)
199 "Does the directory the database DB needs to write to exist?
200 If SUPPRESS-QUESTIONS, then do not ask to create the directory."
201 (let ((dest (file-name-directory (oref DB file
)))
204 ;; @TODO - If it was never set up... what should we do ?
206 ((file-exists-p dest
) t
)
207 ((or suppress-questions
208 (and (boundp 'semanticdb--inhibit-make-directory
)
209 semanticdb--inhibit-make-directory
))
211 ((y-or-n-p (format "Create directory %s for SemanticDB? " dest
))
212 (make-directory dest t
)
215 (if (boundp 'semanticdb--inhibit-make-directory
)
216 (setq semanticdb--inhibit-make-directory t
))
219 (defmethod semanticdb-save-db ((DB semanticdb-project-database-file
)
222 "Write out the database DB to its file.
223 If DB is not specified, then use the current database."
224 (let ((objname (oref DB file
)))
225 (when (and (semanticdb-dirty-p DB
)
226 (semanticdb-live-p DB
)
227 (semanticdb-file-directory-exists-p DB suppress-questions
)
228 (semanticdb-write-directory-p DB
)
230 ;;(message "Saving tag summary for %s..." objname)
232 (eieio-persistent-save (or DB semanticdb-current-database
))
233 (file-error ; System error saving? Ignore it.
234 (message "%S: %s" foo objname
))
238 (stringp (nth 1 foo
))
239 (string-match "write[- ]protected" (nth 1 foo
)))
240 (message (nth 1 foo
)))
242 (stringp (nth 1 foo
))
243 (string-match "no such directory" (nth 1 foo
)))
244 (message (nth 1 foo
)))
246 ;; @todo - It should ask if we are not called from a hook.
248 (if (or suppress-questions
249 (y-or-n-p (format "Skip Error: %s ?" (car (cdr foo
)))))
250 (message "Save Error: %S: %s" (car (cdr foo
))
252 (error "%S" (car (cdr foo
))))))))
253 (run-hook-with-args 'semanticdb-save-database-functions
254 (or DB semanticdb-current-database
))
255 ;;(message "Saving tag summary for %s...done" objname)
259 (defmethod semanticdb-live-p ((obj semanticdb-project-database
))
260 "Return non-nil if the file associated with OBJ is live.
261 Live databases are objects associated with existing directories."
262 (and (slot-boundp obj
'reference-directory
)
263 (file-exists-p (oref obj reference-directory
))))
265 (defmethod semanticdb-live-p ((obj semanticdb-table
))
266 "Return non-nil if the file associated with OBJ is live.
267 Live files are either buffers in Emacs, or files existing on the filesystem."
268 (let ((full-filename (semanticdb-full-filename obj
)))
269 (or (find-buffer-visiting full-filename
)
270 (file-exists-p full-filename
))))
272 (defvar semanticdb-data-debug-on-write-error nil
273 "Run the data debugger on tables that issue errors.
274 This variable is set to nil after the first error is encountered
275 to prevent overload.")
277 (declare-function data-debug-insert-thing
"data-debug")
279 (defmethod object-write ((obj semanticdb-table
))
280 "When writing a table, we have to make sure we deoverlay it first.
281 Restore the overlays after writing.
282 Argument OBJ is the object to write."
283 (when (semanticdb-live-p obj
)
284 (when (semanticdb-in-buffer-p obj
)
285 (with-current-buffer (semanticdb-in-buffer-p obj
)
287 ;; Make sure all our tag lists are up to date.
288 (semantic-fetch-tags)
290 ;; Try to get an accurate unmatched syntax table.
291 (when (and (boundp semantic-show-unmatched-syntax-mode
)
292 semantic-show-unmatched-syntax-mode
)
293 ;; Only do this if the user runs unmatched syntax
294 ;; mode display entries.
295 (oset obj unmatched-syntax
296 (semantic-show-unmatched-lex-tokens-fetch))
299 ;; Make sure pointmax is up to date
300 (oset obj pointmax
(point-max))
303 ;; Make sure that the file size and other attributes are
305 (let ((fattr (file-attributes (semanticdb-full-filename obj
))))
306 (oset obj fsize
(nth 7 fattr
))
307 (oset obj lastmodtime
(nth 5 fattr
))
311 (condition-case tableerror
314 (when semanticdb-data-debug-on-write-error
315 (require 'data-debug
)
316 (data-debug-new-buffer (concat "*SEMANTICDB ERROR*"))
317 (data-debug-insert-thing obj
"*" "")
318 (setq semanticdb-data-debug-on-write-error nil
))
319 (message "Error Writing Table: %s" (object-name obj
))
320 (error "%S" (car (cdr tableerror
)))))
322 ;; Clear the dirty bit.
328 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database-file
))
329 "Return non-nil if OBJ should be written to disk.
330 Uses `semanticdb-persistent-path' to determine the return value."
331 (let ((path semanticdb-persistent-path
))
334 (cond ((stringp (car path
))
335 (if (string= (oref obj reference-directory
) (car path
))
337 ((eq (car path
) 'project
)
338 ;; @TODO - EDE causes us to go in here and disable
339 ;; the old default 'always save' setting.
341 ;; With new default 'always' should I care?
342 (if semanticdb-project-predicate-functions
343 (if (run-hook-with-args-until-success
344 'semanticdb-project-predicate-functions
345 (oref obj reference-directory
))
347 ;; If the mode is 'project, and there are no project
348 ;; modes, then just always save the file. If users
349 ;; wish to restrict the search, modify
350 ;; `semanticdb-persistent-path' to include desired paths.
351 (if (= (length semanticdb-persistent-path
) 1)
354 ((eq (car path
) 'never
)
356 ((eq (car path
) 'always
)
358 (t (error "Invalid path %S" (car path
))))
359 (setq path
(cdr path
)))
363 ;;; Filename manipulation
365 (defmethod semanticdb-file-table ((obj semanticdb-project-database-file
) filename
)
366 "From OBJ, return FILENAME's associated table object."
367 ;; Cheater option. In this case, we always have files directly
368 ;; under ourselves. The main project type may not.
369 (object-assoc (file-name-nondirectory filename
) 'file
(oref obj tables
)))
371 (defmethod semanticdb-file-name-non-directory :STATIC
372 ((dbclass semanticdb-project-database-file
))
373 "Return the file name DBCLASS will use.
374 File name excludes any directory part."
375 semanticdb-default-file-name
)
377 (defmethod semanticdb-file-name-directory :STATIC
378 ((dbclass semanticdb-project-database-file
) directory
)
379 "Return the relative directory to where DBCLASS will save its cache file.
380 The returned path is related to DIRECTORY."
381 (if semanticdb-default-save-directory
382 (let ((file (cedet-directory-name-to-file-name directory
)))
383 ;; Now create a filename for the cache file in
384 ;; ;`semanticdb-default-save-directory'.
386 file
(file-name-as-directory semanticdb-default-save-directory
)))
389 (defmethod semanticdb-cache-filename :STATIC
390 ((dbclass semanticdb-project-database-file
) path
)
391 "For DBCLASS, return a file to a cache file belonging to PATH.
392 This could be a cache file in the current directory, or an encoded file
393 name in a secondary directory."
394 ;; Use concat and not expand-file-name, because the dir part
395 ;; may include some of the file name.
396 (concat (semanticdb-file-name-directory dbclass path
)
397 (semanticdb-file-name-non-directory dbclass
)))
399 (defmethod semanticdb-full-filename ((obj semanticdb-project-database-file
))
400 "Fetch the full filename that OBJ refers to."
405 (defun semanticdb-cleanup-cache-files (&optional noerror
)
406 "Cleanup any cache files associated with directories that no longer exist.
407 Optional NOERROR prevents errors from being displayed."
409 (when (and (not semanticdb-default-save-directory
)
411 (error "No default save directory for semantic-save files"))
413 (when semanticdb-default-save-directory
415 ;; Calculate all the cache files we have.
416 (let* ((regexp (regexp-quote semanticdb-default-file-name
))
417 (files (directory-files semanticdb-default-save-directory
422 (setq orig
(cedet-file-name-to-directory-name
423 (file-name-nondirectory F
)))
424 (when (not (file-exists-p (file-name-directory orig
)))
425 (setq to-delete
(cons F to-delete
))
428 (save-window-excursion
429 (let ((buff (get-buffer-create "*Semanticdb Delete*")))
430 (with-current-buffer buff
432 (insert "The following Cache files appear to be obsolete.\n\n")
433 (dolist (F to-delete
)
435 (pop-to-buffer buff t t
)
436 (fit-window-to-buffer (get-buffer-window buff
) nil
1)
437 (when (y-or-n-p "Delete Old Cache Files? ")
439 (message "Deleting to %s..." F
)
444 ;; No files to delete
446 (message "No obsolete semanticdb.cache files."))
449 (provide 'semantic
/db-file
)
452 ;; generated-autoload-file: "loaddefs.el"
453 ;; generated-autoload-load-name: "semantic/db-file"
456 ;;; semantic/db-file.el ends here