1 ;;; semantic/db-file.el --- Save a semanticdb to a cache file.
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
4 ;;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; A set of semanticdb classes for persistently saving caches on disk.
30 (require 'semantic
/db
)
31 (require 'cedet-files
)
33 (defvar semanticdb-file-version semantic-version
34 "Version of semanticdb we are writing files to disk with.")
35 (defvar semanticdb-file-incompatible-version
"1.4"
36 "Version of semanticdb we are not reverse compatible with.")
40 (defcustom semanticdb-default-file-name
"semantic.cache"
41 "File name of the semantic tag cache."
45 (defcustom semanticdb-default-save-directory
46 (expand-file-name "semanticdb" user-emacs-directory
)
47 "Directory name where semantic cache files are stored.
48 If this value is nil, files are saved in the current directory. If the value
49 is a valid directory, then it overrides `semanticdb-default-file-name' and
50 stores caches in a coded file name in this directory."
52 :type
'(choice :tag
"Default-Directory"
53 :menu-tag
"Default-Directory"
54 (const :tag
"Use current directory" :value nil
)
57 (defcustom semanticdb-persistent-path
'(always)
58 "List of valid paths that semanticdb will cache tags to.
59 When `global-semanticdb-minor-mode' is active, tag lists will
60 be saved to disk when Emacs exits. Not all directories will have
61 tags that should be saved.
62 The value should be a list of valid paths. A path can be a string,
63 indicating a directory in which to save a variable. An element in the
64 list can also be a symbol. Valid symbols are `never', which will
65 disable any saving anywhere, `always', which enables saving
66 everywhere, or `project', which enables saving in any directory that
67 passes a list of predicates in `semanticdb-project-predicate-functions'."
71 (defcustom semanticdb-save-database-hook nil
72 "Normal hook run after a database is saved.
73 Each function is called with one argument, the object representing
74 the database recently written."
78 (define-obsolete-variable-alias
79 'semanticdb-save-database-hooks
80 'semanticdb-save-database-hook
"23.2")
82 (defvar semanticdb-dir-sep-char
(if (boundp 'directory-sep-char
)
83 (symbol-value 'directory-sep-char
)
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))
104 (defclass semanticdb-project-database-file
(semanticdb-project-database
106 ((file-header-line :initform
";; SEMANTICDB Tags save file")
107 (do-backups :initform nil
)
108 (semantic-tag-version :initarg
:semantic-tag-version
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
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.")
127 (defmethod semanticdb-create-database :STATIC
((dbc semanticdb-project-database-file
)
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
)
139 (semanticdb-load-database fn
))))))
141 (setq db
(make-instance
142 dbc
; Create the database requested. Perhaps
143 (concat (file-name-nondirectory
148 :semantic-tag-version semantic-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
)
157 (declare-function inversion-test
"inversion")
159 (defun semanticdb-load-database (filename)
160 "Load the database FILENAME."
162 (let* ((r (eieio-persistent-read filename
))
163 (c (semanticdb-get-database-tables r
))
164 (tv (oref r semantic-tag-version
))
165 (fv (oref r semanticdb-version
))
167 ;; Restore the parent-db connection
169 (oset (car c
) parent-db r
)
171 (unless (and (equal semanticdb-file-version fv
)
172 (equal semantic-tag-version tv
))
173 ;; Try not to load inversion unless we need it:
175 (if (not (inversion-test 'semanticdb-file fv
))
176 (when (inversion-test 'semantic-tag tv
)
177 ;; Incompatible version. Flush tables.
178 (semanticdb-flush-database-tables r
)
179 ;; Reset the version to new version.
180 (oset r semantic-tag-version semantic-tag-version
)
182 (message "Semanticdb file is old. Starting over for %s"
184 ;; Version is not ok. Flush whole system
185 (message "semanticdb file is old. Starting over for %s"
187 ;; This database is so old, we need to replace it.
188 ;; We also need to delete it from the instance tracker.
192 (error (message "Cache Error: [%s] %s, Restart"
196 (defun semanticdb-file-loaded-p (filename)
197 "Return the project belonging to FILENAME if it was already loaded."
198 (eieio-instance-tracker-find filename
'file
'semanticdb-database-list
))
200 (defmethod semanticdb-file-directory-exists-p ((DB semanticdb-project-database-file
)
201 &optional supress-questions
)
202 "Does the directory the database DB needs to write to exist?
203 If SUPRESS-QUESTIONS, then do not ask to create the directory."
204 (let ((dest (file-name-directory (oref DB file
)))
207 ;; @TODO - If it was never set up... what should we do ?
209 ((file-exists-p dest
) t
)
210 ((or supress-questions
211 (and (boundp 'semanticdb--inhibit-make-directory
)
212 semanticdb--inhibit-make-directory
))
214 ((y-or-n-p (format "Create directory %s for SemanticDB? " dest
))
215 (make-directory dest t
)
218 (if (boundp 'semanticdb--inhibit-make-directory
)
219 (setq semanticdb--inhibit-make-directory t
))
222 (defmethod semanticdb-save-db ((DB semanticdb-project-database-file
)
225 "Write out the database DB to its file.
226 If DB is not specified, then use the current database."
227 (let ((objname (oref DB file
)))
228 (when (and (semanticdb-dirty-p DB
)
229 (semanticdb-live-p DB
)
230 (semanticdb-file-directory-exists-p DB supress-questions
)
231 (semanticdb-write-directory-p DB
)
233 ;;(message "Saving tag summary for %s..." objname)
235 (eieio-persistent-save (or DB semanticdb-current-database
))
236 (file-error ; System error saving? Ignore it.
237 (message "%S: %s" foo objname
))
241 (stringp (nth 1 foo
))
242 (string-match "write[- ]protected" (nth 1 foo
)))
243 (message (nth 1 foo
)))
245 (stringp (nth 1 foo
))
246 (string-match "no such directory" (nth 1 foo
)))
247 (message (nth 1 foo
)))
249 ;; @todo - It should ask if we are not called from a hook.
251 (if (or supress-questions
252 (y-or-n-p (format "Skip Error: %S ?" (car (cdr foo
)))))
253 (message "Save Error: %S: %s" (car (cdr foo
))
255 (error "%S" (car (cdr foo
))))))))
256 (run-hook-with-args 'semanticdb-save-database-hooks
257 (or DB semanticdb-current-database
))
258 ;;(message "Saving tag summary for %s...done" objname)
262 (defmethod semanticdb-live-p ((obj semanticdb-project-database
))
263 "Return non-nil if the file associated with OBJ is live.
264 Live databases are objects associated with existing directories."
265 (and (slot-boundp obj
'reference-directory
)
266 (file-exists-p (oref obj reference-directory
))))
268 (defmethod semanticdb-live-p ((obj semanticdb-table
))
269 "Return non-nil if the file associated with OBJ is live.
270 Live files are either buffers in Emacs, or files existing on the filesystem."
271 (let ((full-filename (semanticdb-full-filename obj
)))
272 (or (find-buffer-visiting full-filename
)
273 (file-exists-p full-filename
))))
275 (defvar semanticdb-data-debug-on-write-error nil
276 "Run the data debugger on tables that issue errors.
277 This variable is set to nil after the first error is encountered
278 to prevent overload.")
280 (declare-function data-debug-insert-thing
"data-debug")
282 (defmethod object-write ((obj semanticdb-table
))
283 "When writing a table, we have to make sure we deoverlay it first.
284 Restore the overlays after writting.
285 Argument OBJ is the object to write."
286 (when (semanticdb-live-p obj
)
287 (when (semanticdb-in-buffer-p obj
)
289 (set-buffer (semanticdb-in-buffer-p obj
))
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 enties.
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
309 (let ((fattr (file-attributes (semanticdb-full-filename obj
))))
310 (oset obj fsize
(nth 7 fattr
))
311 (oset obj lastmodtime
(nth 5 fattr
))
315 (condition-case tableerror
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" (object-name obj
))
324 (error "%S" (car (cdr tableerror
)))))
326 ;; Clear the dirty bit.
332 (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
))
338 (cond ((stringp (car path
))
339 (if (string= (oref obj reference-directory
) (car path
))
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
))
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)
358 ((eq (car path
) 'never
)
360 ((eq (car path
) 'always
)
362 (t (error "Invalid path %S" (car path
))))
363 (setq path
(cdr path
)))
367 ;;; Filename manipulation
369 (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 (defmethod semanticdb-file-name-non-directory :STATIC
376 ((dbclass 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 (defmethod semanticdb-file-name-directory :STATIC
382 ((dbclass 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'.
390 file
(file-name-as-directory semanticdb-default-save-directory
)))
393 (defmethod semanticdb-cache-filename :STATIC
394 ((dbclass 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 (defmethod semanticdb-full-filename ((obj semanticdb-project-database-file
))
404 "Fetch the full filename that OBJ refers to."
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."
413 (when (and (not semanticdb-default-save-directory
)
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
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
))
432 (save-window-excursion
433 (let ((buff (get-buffer-create "*Semanticdb Delete*")))
434 (with-current-buffer buff
436 (insert "The following Cache files appear to be obsolete.\n\n")
437 (dolist (F to-delete
)
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? ")
443 (message "Deleting to %s..." F
)
448 ;; No files to delete
450 (message "No obsolete semanticdb.cache files."))
453 (provide 'semantic
/db-file
)
456 ;; generated-autoload-file: "loaddefs.el"
457 ;; generated-autoload-feature: semantic/loaddefs
458 ;; generated-autoload-load-name: "semantic/db-file"
461 ;;; semantic/db-file.el ends here