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
(expand-file-name "~/.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 (defcustom semanticdb-save-database-hooks nil
71 "*Hooks run after a database is saved.
72 Each function is called with one argument, the object representing
73 the database recently written."
77 (defvar semanticdb-dir-sep-char
(if (boundp 'directory-sep-char
)
78 (symbol-value 'directory-sep-char
)
80 "Character used for directory separation.
81 Obsoleted in some versions of Emacs. Needed in others.
82 NOTE: This should get deleted from semantic soon.")
84 (defun semanticdb-fix-pathname (dir)
85 "If DIR is broken, fix it.
86 Force DIR to end with a /.
87 Note: Same as `file-name-as-directory'.
88 NOTE: This should get deleted from semantic soon."
89 (file-name-as-directory dir
))
90 ;; I didn't initially know about the above fcn. Keep the below as a
91 ;; reference. Delete it someday once I've proven everything is the same.
92 ;; (if (not (= semanticdb-dir-sep-char (aref path (1- (length path)))))
93 ;; (concat path (list semanticdb-dir-sep-char))
98 (defclass semanticdb-project-database-file
(semanticdb-project-database
100 ((file-header-line :initform
";; SEMANTICDB Tags save file")
101 (do-backups :initform nil
)
102 (semantic-tag-version :initarg
:semantic-tag-version
105 "The version of the tags saved.
106 The default value is 1.4. In semantic 1.4 there was no versioning, so
107 when those files are loaded, this becomes the version number.
108 To save the version number, we must hand-set this version string.")
109 (semanticdb-version :initarg
:semanticdb-version
112 "The version of the object system saved.
113 The default value is 1.4. In semantic 1.4, there was no versioning,
114 so when those files are loaded, this becomes the version number.
115 To save the version number, we must hand-set this version string.")
117 "Database of file tables saved to disk.")
121 (defmethod semanticdb-create-database :STATIC
((dbc semanticdb-project-database-file
)
123 "Create a new semantic database for DIRECTORY and return it.
124 If a database for DIRECTORY has already been loaded, return it.
125 If a database for DIRECTORY exists, then load that database, and return it.
126 If DIRECTORY doesn't exist, create a new one."
127 ;; Make sure this is fully expanded so we don't get duplicates.
128 (setq directory
(file-truename directory
))
129 (let* ((fn (semanticdb-cache-filename dbc directory
))
130 (db (or (semanticdb-file-loaded-p fn
)
131 (if (file-exists-p fn
)
133 (semanticdb-load-database fn
))))))
135 (setq db
(make-instance
136 dbc
; Create the database requested. Perhaps
137 (concat (file-name-nondirectory
142 :semantic-tag-version semantic-version
143 :semanticdb-version semanticdb-file-version
)))
144 ;; Set this up here. We can't put it in the constructor because it
145 ;; would be saved, and we want DB files to be portable.
146 (oset db reference-directory directory
)
151 (declare-function inversion-test
"inversion")
153 (defun semanticdb-load-database (filename)
154 "Load the database FILENAME."
156 (let* ((r (eieio-persistent-read filename
))
157 (c (semanticdb-get-database-tables r
))
158 (tv (oref r semantic-tag-version
))
159 (fv (oref r semanticdb-version
))
161 ;; Restore the parent-db connection
163 (oset (car c
) parent-db r
)
165 (unless (and (equal semanticdb-file-version fv
)
166 (equal semantic-tag-version tv
))
167 ;; Try not to load inversion unless we need it:
169 (if (not (inversion-test 'semanticdb-file fv
))
170 (when (inversion-test 'semantic-tag tv
)
171 ;; Incompatible version. Flush tables.
172 (semanticdb-flush-database-tables r
)
173 ;; Reset the version to new version.
174 (oset r semantic-tag-version semantic-tag-version
)
176 (message "Semanticdb file is old. Starting over for %s"
178 ;; Version is not ok. Flush whole system
179 (message "semanticdb file is old. Starting over for %s"
181 ;; This database is so old, we need to replace it.
182 ;; We also need to delete it from the instance tracker.
186 (error (message "Cache Error: [%s] %s, Restart"
190 (defun semanticdb-file-loaded-p (filename)
191 "Return the project belonging to FILENAME if it was already loaded."
192 (eieio-instance-tracker-find filename
'file
'semanticdb-database-list
))
194 (defmethod semanticdb-file-directory-exists-p ((DB semanticdb-project-database-file
)
195 &optional supress-questions
)
196 "Does the directory the database DB needs to write to exist?
197 If SUPRESS-QUESTIONS, then do not ask to create the directory."
198 (let ((dest (file-name-directory (oref DB file
)))
201 ;; @TODO - If it was never set up... what should we do ?
203 ((file-exists-p dest
) t
)
204 (supress-questions nil
)
205 ((y-or-n-p (format "Create directory %s for SemanticDB? "
207 (make-directory dest t
)
212 (defmethod semanticdb-save-db ((DB semanticdb-project-database-file
)
215 "Write out the database DB to its file.
216 If DB is not specified, then use the current database."
217 (let ((objname (oref DB file
)))
218 (when (and (semanticdb-dirty-p DB
)
219 (semanticdb-live-p DB
)
220 (semanticdb-file-directory-exists-p DB supress-questions
)
221 (semanticdb-write-directory-p DB
)
223 ;;(message "Saving tag summary for %s..." objname)
225 (eieio-persistent-save (or DB semanticdb-current-database
))
226 (file-error ; System error saving? Ignore it.
227 (message "%S: %s" foo objname
))
231 (stringp (nth 1 foo
))
232 (string-match "write[- ]protected" (nth 1 foo
)))
233 (message (nth 1 foo
)))
235 (stringp (nth 1 foo
))
236 (string-match "no such directory" (nth 1 foo
)))
237 (message (nth 1 foo
)))
239 ;; @todo - It should ask if we are not called from a hook.
241 (if (or supress-questions
242 (y-or-n-p (format "Skip Error: %S ?" (car (cdr foo
)))))
243 (message "Save Error: %S: %s" (car (cdr foo
))
245 (error "%S" (car (cdr foo
))))))))
246 (run-hook-with-args 'semanticdb-save-database-hooks
247 (or DB semanticdb-current-database
))
248 ;;(message "Saving tag summary for %s...done" objname)
252 (defmethod semanticdb-live-p ((obj semanticdb-project-database
))
253 "Return non-nil if the file associated with OBJ is live.
254 Live databases are objects associated with existing directories."
255 (and (slot-boundp obj
'reference-directory
)
256 (file-exists-p (oref obj reference-directory
))))
258 (defmethod semanticdb-live-p ((obj semanticdb-table
))
259 "Return non-nil if the file associated with OBJ is live.
260 Live files are either buffers in Emacs, or files existing on the filesystem."
261 (let ((full-filename (semanticdb-full-filename obj
)))
262 (or (find-buffer-visiting full-filename
)
263 (file-exists-p full-filename
))))
265 (defvar semanticdb-data-debug-on-write-error nil
266 "Run the data debugger on tables that issue errors.
267 This variable is set to nil after the first error is encountered
268 to prevent overload.")
270 (declare-function data-debug-insert-thing
"data-debug")
272 (defmethod object-write ((obj semanticdb-table
))
273 "When writing a table, we have to make sure we deoverlay it first.
274 Restore the overlays after writting.
275 Argument OBJ is the object to write."
276 (when (semanticdb-live-p obj
)
277 (when (semanticdb-in-buffer-p obj
)
279 (set-buffer (semanticdb-in-buffer-p obj
))
281 ;; Make sure all our tag lists are up to date.
282 (semantic-fetch-tags)
284 ;; Try to get an accurate unmatched syntax table.
285 (when (and (boundp semantic-show-unmatched-syntax-mode
)
286 semantic-show-unmatched-syntax-mode
)
287 ;; Only do this if the user runs unmatched syntax
288 ;; mode display enties.
289 (oset obj unmatched-syntax
290 (semantic-show-unmatched-lex-tokens-fetch))
293 ;; Make sure pointmax is up to date
294 (oset obj pointmax
(point-max))
297 ;; Make sure that the file size and other attributes are
299 (let ((fattr (file-attributes (semanticdb-full-filename obj
))))
300 (oset obj fsize
(nth 7 fattr
))
301 (oset obj lastmodtime
(nth 5 fattr
))
305 (condition-case tableerror
308 (when semanticdb-data-debug-on-write-error
309 (require 'data-debug
)
310 (data-debug-new-buffer (concat "*SEMANTICDB ERROR*"))
311 (data-debug-insert-thing obj
"*" "")
312 (setq semanticdb-data-debug-on-write-error nil
))
313 (message "Error Writing Table: %s" (object-name obj
))
314 (error "%S" (car (cdr tableerror
)))))
316 ;; Clear the dirty bit.
322 (defmethod semanticdb-write-directory-p ((obj semanticdb-project-database-file
))
323 "Return non-nil if OBJ should be written to disk.
324 Uses `semanticdb-persistent-path' to determine the return value."
325 (let ((path semanticdb-persistent-path
))
328 (cond ((stringp (car path
))
329 (if (string= (oref obj reference-directory
) (car path
))
331 ((eq (car path
) 'project
)
332 ;; @TODO - EDE causes us to go in here and disable
333 ;; the old default 'always save' setting.
335 ;; With new default 'always' should I care?
336 (if semanticdb-project-predicate-functions
337 (if (run-hook-with-args-until-success
338 'semanticdb-project-predicate-functions
339 (oref obj reference-directory
))
341 ;; If the mode is 'project, and there are no project
342 ;; modes, then just always save the file. If users
343 ;; wish to restrict the search, modify
344 ;; `semanticdb-persistent-path' to include desired paths.
345 (if (= (length semanticdb-persistent-path
) 1)
348 ((eq (car path
) 'never
)
350 ((eq (car path
) 'always
)
352 (t (error "Invalid path %S" (car path
))))
353 (setq path
(cdr path
)))
357 ;;; Filename manipulation
359 (defmethod semanticdb-file-table ((obj semanticdb-project-database-file
) filename
)
360 "From OBJ, return FILENAME's associated table object."
361 ;; Cheater option. In this case, we always have files directly
362 ;; under ourselves. The main project type may not.
363 (object-assoc (file-name-nondirectory filename
) 'file
(oref obj tables
)))
365 (defmethod semanticdb-file-name-non-directory :STATIC
366 ((dbclass semanticdb-project-database-file
))
367 "Return the file name DBCLASS will use.
368 File name excludes any directory part."
369 semanticdb-default-file-name
)
371 (defmethod semanticdb-file-name-directory :STATIC
372 ((dbclass semanticdb-project-database-file
) directory
)
373 "Return the relative directory to where DBCLASS will save its cache file.
374 The returned path is related to DIRECTORY."
375 (if semanticdb-default-save-directory
376 (let ((file (cedet-directory-name-to-file-name directory
)))
377 ;; Now create a filename for the cache file in
378 ;; ;`semanticdb-default-save-directory'.
380 file
(file-name-as-directory semanticdb-default-save-directory
)))
383 (defmethod semanticdb-cache-filename :STATIC
384 ((dbclass semanticdb-project-database-file
) path
)
385 "For DBCLASS, return a file to a cache file belonging to PATH.
386 This could be a cache file in the current directory, or an encoded file
387 name in a secondary directory."
388 ;; Use concat and not expand-file-name, because the dir part
389 ;; may include some of the file name.
390 (concat (semanticdb-file-name-directory dbclass path
)
391 (semanticdb-file-name-non-directory dbclass
)))
393 (defmethod semanticdb-full-filename ((obj semanticdb-project-database-file
))
394 "Fetch the full filename that OBJ refers to."
399 (defun semanticdb-cleanup-cache-files (&optional noerror
)
400 "Cleanup any cache files associated with directories that no longer exist.
401 Optional NOERROR prevents errors from being displayed."
403 (when (and (not semanticdb-default-save-directory
)
405 (error "No default save directory for semantic-save files"))
407 (when semanticdb-default-save-directory
409 ;; Calculate all the cache files we have.
410 (let* ((regexp (regexp-quote semanticdb-default-file-name
))
411 (files (directory-files semanticdb-default-save-directory
416 (setq orig
(cedet-file-name-to-directory-name
417 (file-name-nondirectory F
)))
418 (when (not (file-exists-p (file-name-directory orig
)))
419 (setq to-delete
(cons F to-delete
))
422 (save-window-excursion
423 (let ((buff (get-buffer-create "*Semanticdb Delete*")))
424 (with-current-buffer buff
426 (insert "The following Cache files appear to be obsolete.\n\n")
427 (dolist (F to-delete
)
429 (pop-to-buffer buff t t
)
430 (fit-window-to-buffer (get-buffer-window buff
) nil
1)
431 (when (y-or-n-p "Delete Old Cache Files? ")
433 (message "Deleting to %s..." F
)
438 ;; No files to delete
440 (message "No obsolete semanticdb.cache files."))
443 (provide 'semantic
/db-file
)
445 ;;; semantic/db-file.el ends here