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