Provide 'epg-file.
[elisp/epg.git] / epg-file.el
1 (require 'epg)
2
3 (defcustom epg-file-name-regexp "\\.gpg\\'"
4   "Regexp that matches filenames that are assumed to be encrypted
5 with GnuPG."
6   :type 'regexp
7   :group 'epg-file)
8
9 (defun epg-file-handler (operation &rest args)
10   (let ((epg-file-operation (get operation 'epg-file)))
11     (if epg-file-operation
12         (apply epg-file-operation args)
13       (epg-file-run-real-handler operation args))))
14
15 (defun epg-file-run-real-handler (operation args)
16   (let ((inhibit-file-name-handlers
17          (cons 'epg-file-handler
18                (and (eq inhibit-file-name-operation operation)
19                     inhibit-file-name-handlers)))
20         (inhibit-file-name-operation operation))
21     (apply operation args)))
22
23 (defvar buffer-file-type)
24 (defvar last-coding-system-used)
25 (defun epg-file-write-region (start end filename &optional append visit
26                                     lockname mustbenew)
27   (let* ((visit-file (if (stringp visit)
28                          (expand-file-name visit)
29                        (expand-file-name filename)))
30          ;; XXX: Obtain the value returned by choose_write_coding_system
31          (coding-system (condition-case nil
32                             (epg-file-run-real-handler
33                              'write-region (list start end "/"))
34                           (file-error (if (boundp 'last-coding-system-used)
35                                           last-coding-system-used
36                                         buffer-file-coding-system))))
37          ;; start and end are normally buffer positions
38          ;; specifying the part of the buffer to write.
39          ;; If start is nil, that means to use the entire buffer contents.
40          ;; If start is a string, then output that string to the file
41          ;; instead of any buffer contents; end is ignored.
42          (string (encode-coding-string (cond
43                                         ((stringp start)
44                                          start)
45                                         ((null start)
46                                          (buffer-string))
47                                         (t
48                                          (buffer-substring start end)))
49                                        coding-system)))
50     (with-temp-buffer
51       (if (fboundp 'set-buffer-multibyte)
52           (set-buffer-multibyte nil))
53       ;; Optional fourth argument append if non-nil means
54       ;;   append to existing file contents (if any).  If it is an integer,
55       ;;   seek to that offset in the file before writing.
56       (if (and append (file-exists-p filename))
57           ;; Enable passphrase cache on this temp buffer
58           (let ((coding-system-for-read 'binary))
59             ;; set visit to t so that passphrase is cached
60             (insert-file-contents filename t)
61             (setq buffer-file-name nil)))
62       ;; Insert data to encrypt
63       (goto-char (if (integerp append) (1+ append) (point-max)))
64       (delete-region (point) (min (+ (point) (length string)) (point-max)))
65       (insert string)
66
67       (let ((coding-system-for-write 'binary)
68             (coding-system-for-read 'binary)
69             (context (epg-make-context))
70             cipher)
71         (when (setq cipher (epg-encrypt-string context (buffer-string) nil))
72           (if (and (memq system-type '(ms-dos windows-nt))
73                    (boundp 'buffer-file-type))
74               (setq buffer-file-type t))
75           (epg-file-run-real-handler
76            'write-region
77            (list cipher nil filename nil 'not-visit lockname mustbenew)))))
78     ;; Optional fifth argument visit, if t or a string, means
79     ;;   set the last-save-file-modtime of buffer to this file's modtime
80     ;;   and mark buffer not modified.
81     ;; If visit is a string, it is a second file name;
82     ;;   the output goes to filename, but the buffer is marked as visiting visit.
83     ;;   visit is also the file name to lock and unlock for clash detection.
84     ;; If visit is neither t nor nil nor a string,
85     ;;   that means do not display the "Wrote file" message.
86     (when (or (eq visit t) (stringp visit))
87       (setq buffer-file-name filename)
88       (set-visited-file-modtime))
89     (if (stringp visit)
90         (setq buffer-file-name visit))
91     (when (or (eq visit t) (eq visit nil) (stringp visit))
92       (message "Wrote %s" visit-file))
93     (if (boundp 'last-coding-system-used)
94         (setq last-coding-system-used coding-system))
95     nil))
96
97 (defun epg-file-insert-file-contents (filename &optional visit beg end replace)
98   (barf-if-buffer-read-only)
99   (setq filename (expand-file-name filename))
100   (let ((filename (expand-file-name filename))
101         (length 0))
102     (if (file-exists-p filename)
103         (let ((local-file
104                (let ((inhibit-file-name-operation
105                       (when (eq inhibit-file-name-operation
106                                 'insert-file-contents)
107                         'file-local-copy)))
108                  (file-local-copy filename)))
109               (coding-system-for-read 'binary)
110               (context (epg-make-context))
111               string)
112           (unwind-protect
113               (progn
114                 (setq string (epg-decrypt-file context (or local-file
115                                                            filename))
116                       length (length string))
117                 (if replace
118                     (goto-char (point-min)))
119                 (save-excursion
120                   (let ((buffer-file-name (if visit nil buffer-file-name)))
121                     (save-restriction
122                       (narrow-to-region (point) (point))
123                       (insert (decode-coding-string string 'undecided)))
124                     (if replace
125                         (delete-region (point) (point-max))))))
126             (when (and local-file (file-exists-p local-file))
127               (delete-file local-file)))))
128     ;; If second argument visit is non-nil, the buffer's visited filename
129     ;; and last save file modtime are set, and it is marked unmodified.
130     (when visit
131       (unlock-buffer)
132       (setq buffer-file-name filename)
133       (set-visited-file-modtime))
134
135     ;; If visiting and the file does not exist, visiting is completed
136     ;; before the error is signaled.
137     (if (and visit (not (file-exists-p filename)))
138         (signal 'file-error (list "Opening input file" filename)))
139
140     ;; Returns list of absolute file name and number of characters inserted.
141     (list filename length)))
142
143 (put 'write-region 'epg-file 'epg-file-write-region)
144 (put 'insert-file-contents 'epg-file 'epg-file-insert-file-contents)
145
146 (unless (assoc epg-file-name-regexp file-name-handler-alist)
147   (setq file-name-handler-alist
148         (cons (cons epg-file-name-regexp 'epg-file-handler)
149               file-name-handler-alist)))
150
151 (unless (assoc epg-file-name-regexp auto-mode-alist)
152   (setq auto-mode-alist
153         (cons (list epg-file-name-regexp nil 'strip-suffix)
154               auto-mode-alist)))
155
156 (provide 'epg-file)
157
158 ;;; epg-file.el ends here