OSDN Git Service

* epa-file.el (epa-file-insert-file-contents): Set progress-callback.
[epg/epg.git] / epa-file.el
1 ;;; epa-file.el --- the EasyPG Assistant, transparent file encryption
2 ;; Copyright (C) 2006 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: PGP, GnuPG
6
7 ;; This file is part of EasyPG.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Code:
25
26 (require 'epa)
27
28 (defgroup epa-file nil
29   "The EasyPG Assistant hooks for transparent file encryption"
30   :group 'epa)
31
32 (defcustom epa-file-name-regexp "\\.gpg\\'"
33   "Regexp which matches filenames to be encrypted with GnuPG."
34   :type 'regexp
35   :group 'epa-file)
36
37 (defvar epa-file-handler
38   (cons epa-file-name-regexp 'epa-file-handler))
39   
40 (defvar epa-file-passphrase-alist nil)
41
42 (defun epa-file-passphrase-callback-function (context key-id file)
43   (if (eq key-id 'SYM)
44       (let ((entry (assoc file epa-file-passphrase-alist))
45             passphrase)
46         (or (copy-sequence (cdr entry))
47             (progn
48               (unless entry
49                 (setq entry (list file)
50                       epa-file-passphrase-alist (cons entry
51                                                  epa-file-passphrase-alist)))
52               (setq passphrase (epa-passphrase-callback-function context
53                                                                  key-id nil))
54               (setcdr entry (copy-sequence passphrase))
55               passphrase)))
56     (epa-passphrase-callback-function context key-id nil)))
57
58 (defun epa-file-handler (operation &rest args)
59   (save-match-data
60     (let ((op (get operation 'epa-file)))
61       (if op
62           (apply op args)
63         (epa-file-run-real-handler operation args)))))
64
65 (defun epa-file-run-real-handler (operation args)
66   (let ((inhibit-file-name-handlers
67          (cons 'epa-file-handler
68                (and (eq inhibit-file-name-operation operation)
69                     inhibit-file-name-handlers)))
70         (inhibit-file-name-operation operation))
71     (apply operation args)))
72
73 (defun epa-file-decode-and-insert (string file visit beg end replace)
74   (if (fboundp 'decode-coding-inserted-region)
75       (save-restriction
76         (narrow-to-region (point) (point))
77         (let ((multibyte enable-multibyte-characters))
78           (set-buffer-multibyte nil)
79           (insert string)
80           (set-buffer-multibyte multibyte)
81           (decode-coding-inserted-region
82            (point-min) (point-max)
83            (substring file 0 (string-match epa-file-name-regexp file))
84            visit beg end replace)))
85     (insert (decode-coding-string string (or coding-system-for-read
86                                              'undecided)))))
87
88 (defvar last-coding-system-used)
89 (defun epa-file-insert-file-contents (file &optional visit beg end replace)
90   (barf-if-buffer-read-only)
91   (if (and visit (or beg end))
92       (error "Attempt to visit less than an entire file"))
93   (setq file (expand-file-name file))
94   (let ((local-copy (epa-file-run-real-handler #'file-local-copy (list file)))
95         (context (epg-make-context))
96         string length entry)
97     (if visit
98         (setq buffer-file-name file))
99     (epg-context-set-passphrase-callback
100      context
101      (cons #'epa-file-passphrase-callback-function
102            file))
103     (epg-context-set-progress-callback context
104                                        #'epa-progress-callback-function)
105     (unwind-protect
106         (progn
107           (if replace
108               (goto-char (point-min)))
109           (condition-case error
110               (setq string (epg-decrypt-file context file nil))
111             (error
112              (if (setq entry (assoc file epa-file-passphrase-alist))
113                  (setcdr entry nil))
114              (signal 'file-error
115                      (cons "Opening input file" (cdr error)))))
116           (if (or beg end)
117               (setq string (substring string (or beg 0) end)))
118           (save-excursion
119             (save-restriction
120               (narrow-to-region (point) (point))
121               (epa-file-decode-and-insert string file visit beg end replace)
122               (setq length (- (point-max) (point-min))))
123             (if replace
124                 (delete-region (point) (point-max)))))
125       (if (and local-copy
126                (file-exists-p local-copy))
127           (delete-file local-copy)))
128     (list file length)))
129 (put 'insert-file-contents 'epa-file 'epa-file-insert-file-contents)
130
131 (defun epa-file-write-region (start end file &optional append visit lockname
132                                     mustbenew)
133   (if append
134       (error "Can't append to the file."))
135   (setq file (expand-file-name file))
136   (let* ((coding-system (or coding-system-for-write
137                             (if (boundp 'last-coding-system-used)
138                                 (condition-case nil
139                                     (write-region (point-min) (point-max) "/")
140                                   (error last-coding-system-used))
141                               buffer-file-coding-system)))
142          (context (epg-make-context))
143          (coding-system-for-write 'binary)
144          string entry)
145     (epg-context-set-passphrase-callback
146      context
147      (cons #'epa-file-passphrase-callback-function
148            file))
149     (epg-context-set-progress-callback context
150                                        #'epa-progress-callback-function)
151     (condition-case error
152         (setq string
153               (epg-encrypt-string
154                context
155                (if (stringp start)
156                    (encode-coding-string start coding-system)
157                  (encode-coding-string (buffer-substring start end)
158                                        coding-system))
159                (unless (assoc file epa-file-passphrase-alist)
160                  (epa-select-keys
161                   context
162                   "Select recipents for encryption.
163 If no one is selected, symmetric encryption will be performed.  "))))
164       (error
165        (if (setq entry (assoc file epa-file-passphrase-alist))
166            (setcdr entry nil))
167        (signal 'file-error (cons "Opening output file" (cdr error)))))
168     (epa-file-run-real-handler
169      #'write-region
170      (list string nil file append visit lockname mustbenew))
171     (if (boundp 'last-coding-system-used)
172         (setq last-coding-system-used coding-system))
173     (if (eq visit t)
174         (progn
175           (setq buffer-file-name file)
176           (set-visited-file-modtime))
177       (if (stringp visit)
178           (progn
179             (set-visited-file-modtime)
180             (setq buffer-file-name visit))))
181     (if (or (eq visit t)
182             (eq visit nil)
183             (stringp visit))
184         (message "Wrote %s" buffer-file-name))))
185 (put 'write-region 'epa-file 'epa-file-write-region)
186
187 ;;;###autoload
188 (defun epa-file-enable ()
189   (interactive)
190   (if (memq epa-file-handler file-name-handler-alist)
191       (message "`epa-file' already enabled")
192     (setq file-name-handler-alist
193           (cons epa-file-handler file-name-handler-alist))
194     (message "`epa-file' enabled")))
195
196 ;;;###autoload
197 (defun epa-file-disable ()
198   (interactive)
199   (if (memq epa-file-handler file-name-handler-alist)
200       (progn
201         (setq file-name-handler-alist
202               (delq epa-file-handler file-name-handler-alist))
203         (message "`epa-file' disabled"))
204     (message "`epa-file' already disabled")))
205
206 (provide 'epa-file)
207
208 ;;; epa-file.el ends here