]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Make all Tramp tests pass for "gdrive" method
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007-2016 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; Tramp's main Emacs version for development is Emacs 25. This
27 ;; package provides compatibility functions for Emacs 23 and Emacs 24.
28
29 ;;; Code:
30
31 ;; Pacify byte-compiler.
32 (eval-when-compile
33 (require 'cl))
34
35 (require 'auth-source)
36 (require 'advice)
37 (require 'custom)
38 (require 'format-spec)
39 (require 'password-cache)
40 (require 'shell)
41 (require 'timer)
42 (require 'ucs-normalize)
43
44 (require 'trampver)
45 (require 'tramp-loaddefs)
46
47 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs
48 ;; 24.1. Besides t, nil, and integer, we use also timestamps (as
49 ;; returned by `current-time') internally.
50 (unless (boundp 'remote-file-name-inhibit-cache)
51 (defvar remote-file-name-inhibit-cache nil))
52
53 ;; For not existing functions, obsolete functions, or functions with a
54 ;; changed argument list, there are compiler warnings. We want to
55 ;; avoid them in cases we know what we do.
56 (defmacro tramp-compat-funcall (function &rest arguments)
57 `(when (or (subrp ,function) (functionp ,function))
58 (with-no-warnings (funcall ,function ,@arguments))))
59
60 ;; We currently use "[" and "]" in the filename format for IPv6 hosts
61 ;; of GNU Emacs. This means that Emacs wants to expand wildcards if
62 ;; `find-file-wildcards' is non-nil, and then barfs because no
63 ;; expansion could be found. We detect this situation and do
64 ;; something really awful: we have `file-expand-wildcards' return the
65 ;; original filename if it can't expand anything. Let's just hope
66 ;; that this doesn't break anything else. It is not needed anymore
67 ;; since GNU Emacs 23.2.
68 (unless (featurep 'files 'remote-wildcards)
69 (defadvice file-expand-wildcards
70 (around tramp-advice-file-expand-wildcards activate)
71 (let ((name (ad-get-arg 0)))
72 ;; If it's a Tramp file, look if wildcards need to be expanded
73 ;; at all.
74 (if (and
75 (tramp-tramp-file-p name)
76 (not (string-match "[[*?]" (file-remote-p name 'localname))))
77 (setq ad-return-value (list name))
78 ;; Otherwise, just run the original function.
79 ad-do-it)))
80 (add-hook
81 'tramp-unload-hook
82 (lambda ()
83 (ad-remove-advice
84 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
85 (ad-activate 'file-expand-wildcards))))
86
87 ;; `condition-case-unless-debug' is introduced with Emacs 24.
88 (if (fboundp 'condition-case-unless-debug)
89 (defalias 'tramp-compat-condition-case-unless-debug
90 'condition-case-unless-debug)
91 (defmacro tramp-compat-condition-case-unless-debug
92 (var bodyform &rest handlers)
93 "Like `condition-case' except that it does not catch anything when debugging."
94 (declare (debug condition-case) (indent 2))
95 (let ((bodysym (make-symbol "body")))
96 `(let ((,bodysym (lambda () ,bodyform)))
97 (if debug-on-error
98 (funcall ,bodysym)
99 (condition-case ,var
100 (funcall ,bodysym)
101 ,@handlers))))))
102
103 (defsubst tramp-compat-temporary-file-directory ()
104 "Return name of directory for temporary files.
105 It is the default value of `temporary-file-directory'."
106 ;; We must return a local directory. If it is remote, we could run
107 ;; into an infloop.
108 (eval (car (get 'temporary-file-directory 'standard-value))))
109
110 (defsubst tramp-compat-make-temp-file (f &optional dir-flag)
111 "Create a local temporary file (compat function).
112 Add the extension of F, if existing."
113 (let* (file-name-handler-alist
114 (prefix (expand-file-name
115 (symbol-value 'tramp-temp-name-prefix)
116 (tramp-compat-temporary-file-directory)))
117 (extension (file-name-extension f t)))
118 (make-temp-file prefix dir-flag extension)))
119
120 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
121 ;; (as PRESERVE-SELINUX-CONTEXT), and renamed in Emacs 24.3.
122 (defun tramp-compat-copy-file
123 (filename newname &optional ok-if-already-exists keep-date
124 preserve-uid-gid preserve-extended-attributes)
125 "Like `copy-file' for Tramp files (compat function)."
126 (cond
127 (preserve-extended-attributes
128 (condition-case nil
129 (tramp-compat-funcall
130 'copy-file filename newname ok-if-already-exists keep-date
131 preserve-uid-gid preserve-extended-attributes)
132 (wrong-number-of-arguments
133 (copy-file
134 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
135 (t
136 (copy-file
137 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
138
139 ;; COPY-CONTENTS has been introduced with Emacs 24.1.
140 (defun tramp-compat-copy-directory
141 (directory newname &optional keep-time parents copy-contents)
142 "Make a copy of DIRECTORY (compat function)."
143 (condition-case nil
144 (tramp-compat-funcall
145 'copy-directory directory newname keep-time parents copy-contents)
146
147 ;; `copy-directory' is either not implemented, or it does not
148 ;; support the the COPY-CONTENTS flag. For the time being, we
149 ;; ignore COPY-CONTENTS as well.
150
151 (error
152 ;; If `default-directory' is a remote directory, make sure we
153 ;; find its `copy-directory' handler.
154 (let ((handler (or (find-file-name-handler directory 'copy-directory)
155 (find-file-name-handler newname 'copy-directory))))
156 (if handler
157 (funcall handler 'copy-directory directory newname keep-time parents)
158
159 ;; Compute target name.
160 (setq directory (directory-file-name (expand-file-name directory))
161 newname (directory-file-name (expand-file-name newname)))
162 (if (and (file-directory-p newname)
163 (not (string-equal (file-name-nondirectory directory)
164 (file-name-nondirectory newname))))
165 (setq newname
166 (expand-file-name
167 (file-name-nondirectory directory) newname)))
168 (if (not (file-directory-p newname)) (make-directory newname parents))
169
170 ;; Copy recursively.
171 (mapc
172 (lambda (file)
173 (if (file-directory-p file)
174 (tramp-compat-copy-directory file newname keep-time parents)
175 (copy-file file newname t keep-time)))
176 ;; We do not want to delete "." and "..".
177 (directory-files directory 'full directory-files-no-dot-files-regexp))
178
179 ;; Set directory attributes.
180 (set-file-modes newname (file-modes directory))
181 (if keep-time
182 (set-file-times newname (nth 5 (file-attributes directory)))))))))
183
184 ;; TRASH has been introduced with Emacs 24.1.
185 (defun tramp-compat-delete-file (filename &optional trash)
186 "Like `delete-file' for Tramp files (compat function)."
187 (condition-case nil
188 (tramp-compat-funcall 'delete-file filename trash)
189 ;; This Emacs version does not support the TRASH flag.
190 (wrong-number-of-arguments
191 (let ((delete-by-moving-to-trash
192 (and (boundp 'delete-by-moving-to-trash)
193 (symbol-value 'delete-by-moving-to-trash)
194 trash)))
195 (delete-file filename)))))
196
197 ;; RECURSIVE has been introduced with Emacs 23.2. TRASH has been
198 ;; introduced with Emacs 24.1.
199 (defun tramp-compat-delete-directory (directory &optional recursive trash)
200 "Like `delete-directory' for Tramp files (compat function)."
201 (condition-case nil
202 (cond
203 (trash
204 (tramp-compat-funcall 'delete-directory directory recursive trash))
205 (t
206 (delete-directory directory recursive)))
207 ;; This Emacs version does not support the TRASH flag. We use the
208 ;; implementation from Emacs 23.2.
209 (wrong-number-of-arguments
210 (setq directory (directory-file-name (expand-file-name directory)))
211 (when (not (file-symlink-p directory))
212 (mapc (lambda (file)
213 (if (eq t (car (file-attributes file)))
214 (tramp-compat-delete-directory file recursive trash)
215 (tramp-compat-delete-file file trash)))
216 (directory-files
217 directory 'full directory-files-no-dot-files-regexp)))
218 (delete-directory directory))))
219
220 (defun tramp-compat-process-running-p (process-name)
221 "Returns t if system process PROCESS-NAME is running for `user-login-name'."
222 (when (stringp process-name)
223 (cond
224 ;; GNU Emacs 22 on w32.
225 ((fboundp 'w32-window-exists-p)
226 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
227
228 ;; GNU Emacs 23.
229 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
230 (let (result)
231 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
232 (let ((attributes (process-attributes pid)))
233 (when (and (string-equal
234 (cdr (assoc 'user attributes)) (user-login-name))
235 (let ((comm (cdr (assoc 'comm attributes))))
236 ;; The returned command name could be truncated
237 ;; to 15 characters. Therefore, we cannot check
238 ;; for `string-equal'.
239 (and comm (string-match
240 (concat "^" (regexp-quote comm))
241 process-name))))
242 (setq result t)))))))))
243
244 ;; `default-toplevel-value' has been declared in Emacs 24.
245 (unless (fboundp 'default-toplevel-value)
246 (defalias 'default-toplevel-value 'symbol-value))
247
248 ;; `format-message' is new in Emacs 25.
249 (unless (fboundp 'format-message)
250 (defalias 'format-message 'format))
251
252 (add-hook 'tramp-unload-hook
253 (lambda ()
254 (unload-feature 'tramp-loaddefs 'force)
255 (unload-feature 'tramp-compat 'force)))
256
257 (provide 'tramp-compat)
258
259 ;;; TODO:
260
261 ;;; tramp-compat.el ends here