]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from trunk
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007, 2008, 2009, 2010 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 GNU Emacs 24. This
27 ;; package provides compatibility functions for GNU Emacs 22, GNU
28 ;; Emacs 23 and XEmacs 21.4+.
29
30 ;;; Code:
31
32 (eval-when-compile
33
34 (require 'tramp-loaddefs))
35
36 (eval-when-compile
37
38 ;; Pacify byte-compiler.
39 (require 'cl))
40
41 (eval-and-compile
42
43 (require 'custom)
44
45 ;; Load the appropriate timer package.
46 (if (featurep 'xemacs)
47 (require 'timer-funcs)
48 (require 'timer))
49
50 ;; We check whether `start-file-process' is bound.
51 (unless (fboundp 'start-file-process)
52
53 ;; tramp-util offers integration into other (X)Emacs packages like
54 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
55 (eval-after-load "tramp"
56 '(require 'tramp-util))
57
58 ;; Make sure that we get integration with the VC package. When it
59 ;; is loaded, we need to pull in the integration module. Not
60 ;; necessary in Emacs 23.
61 (eval-after-load "vc"
62 (eval-after-load "tramp"
63 '(require 'tramp-vc))))
64
65 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
66 ;; Currently, XEmacs supports this.
67 (when (featurep 'xemacs)
68 (unless (boundp 'byte-compile-default-warnings)
69 (defvar byte-compile-default-warnings nil))
70 (delq 'unused-vars byte-compile-default-warnings))
71
72 ;; `last-coding-system-used' is unknown in XEmacs.
73 (unless (boundp 'last-coding-system-used)
74 (defvar last-coding-system-used nil))
75
76 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
77 ;; used in XEmacs, so we set it here and there. The following is
78 ;; needed to pacify Emacs byte-compiler.
79 (unless (boundp 'byte-compile-not-obsolete-var)
80 (defvar byte-compile-not-obsolete-var nil))
81 (setq byte-compile-not-obsolete-var 'directory-sep-char)
82 ;; Emacs 23.2.
83 (unless (boundp 'byte-compile-not-obsolete-vars)
84 (defvar byte-compile-not-obsolete-vars nil))
85 (setq byte-compile-not-obsolete-vars '(directory-sep-char))
86
87 ;; `with-temp-message' does not exists in XEmacs.
88 (condition-case nil
89 (with-temp-message (current-message) nil)
90 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
91
92 ;; For not existing functions, or functions with a changed argument
93 ;; list, there are compiler warnings. We want to avoid them in
94 ;; cases we know what we do.
95 (defmacro tramp-compat-funcall (function &rest arguments)
96 (if (featurep 'xemacs)
97 `(funcall (symbol-function ,function) ,@arguments)
98 `(when (or (subrp ,function) (functionp ,function))
99 (with-no-warnings (funcall ,function ,@arguments)))))
100
101 ;; `set-buffer-multibyte' comes from Emacs Leim.
102 (unless (fboundp 'set-buffer-multibyte)
103 (defalias 'set-buffer-multibyte 'ignore))
104
105 ;; `font-lock-add-keywords' does not exist in XEmacs.
106 (unless (fboundp 'font-lock-add-keywords)
107 (defalias 'font-lock-add-keywords 'ignore))
108
109 ;; The following functions cannot be aliases of the corresponding
110 ;; `tramp-handle-*' functions, because this would bypass the locking
111 ;; mechanism.
112
113 ;; `file-remote-p' has been introduced with Emacs 22. The version
114 ;; of XEmacs is not a magic file name function (yet); this is
115 ;; corrected in tramp-util.el. Here it is sufficient if the
116 ;; function exists.
117 (unless (fboundp 'file-remote-p)
118 (defalias 'file-remote-p
119 (lambda (file &optional identification connected)
120 (when (tramp-tramp-file-p file)
121 (tramp-file-name-handler
122 'file-remote-p file identification connected)))))
123
124 ;; `process-file' does not exist in XEmacs.
125 (unless (fboundp 'process-file)
126 (defalias 'process-file
127 (lambda (program &optional infile buffer display &rest args)
128 (when (tramp-tramp-file-p default-directory)
129 (apply
130 'tramp-file-name-handler
131 'process-file program infile buffer display args)))))
132
133 ;; `start-file-process' is new in Emacs 23.
134 (unless (fboundp 'start-file-process)
135 (defalias 'start-file-process
136 (lambda (name buffer program &rest program-args)
137 (when (tramp-tramp-file-p default-directory)
138 (apply
139 'tramp-file-name-handler
140 'start-file-process name buffer program program-args)))))
141
142 ;; `set-file-times' is also new in Emacs 23.
143 (unless (fboundp 'set-file-times)
144 (defalias 'set-file-times
145 (lambda (filename &optional time)
146 (when (tramp-tramp-file-p filename)
147 (tramp-file-name-handler
148 'set-file-times filename time)))))
149
150 ;; We currently use "[" and "]" in the filename format for IPv6
151 ;; hosts of GNU Emacs. This means, that Emacs wants to expand
152 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
153 ;; because no expansion could be found. We detect this situation
154 ;; and do something really awful: we have `file-expand-wildcards'
155 ;; return the original filename if it can't expand anything. Let's
156 ;; just hope that this doesn't break anything else.
157 ;; It is not needed anymore since GNU Emacs 23.2.
158 (unless (or (featurep 'xemacs)
159 ;; `featurep' has only one argument in XEmacs.
160 (funcall 'featurep 'files 'remote-wildcards))
161 (defadvice file-expand-wildcards
162 (around tramp-advice-file-expand-wildcards activate)
163 (let ((name (ad-get-arg 0)))
164 ;; If it's a Tramp file, look if wildcards need to be expanded
165 ;; at all.
166 (if (and
167 (tramp-tramp-file-p name)
168 (not (string-match
169 "[[*?]" (tramp-compat-funcall
170 'file-remote-p name 'localname))))
171 (setq ad-return-value (list name))
172 ;; Otherwise, just run the original function.
173 ad-do-it)))
174 (add-hook
175 'tramp-unload-hook
176 (lambda ()
177 (ad-remove-advice
178 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
179 (ad-activate 'file-expand-wildcards)))))
180
181 (defsubst tramp-compat-line-beginning-position ()
182 "Return point at beginning of line (compat function).
183 Calls `line-beginning-position' or `point-at-bol' if defined, else
184 own implementation."
185 (cond
186 ((fboundp 'line-beginning-position)
187 (tramp-compat-funcall 'line-beginning-position))
188 ((fboundp 'point-at-bol) (tramp-compat-funcall 'point-at-bol))
189 (t (save-excursion (beginning-of-line) (point)))))
190
191 (defsubst tramp-compat-line-end-position ()
192 "Return point at end of line (compat function).
193 Calls `line-end-position' or `point-at-eol' if defined, else
194 own implementation."
195 (cond
196 ((fboundp 'line-end-position) (tramp-compat-funcall 'line-end-position))
197 ((fboundp 'point-at-eol) (tramp-compat-funcall 'point-at-eol))
198 (t (save-excursion (end-of-line) (point)))))
199
200 (defsubst tramp-compat-temporary-file-directory ()
201 "Return name of directory for temporary files (compat function).
202 For Emacs, this is the variable `temporary-file-directory', for XEmacs
203 this is the function `temp-directory'."
204 (cond
205 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
206 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
207 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
208 (file-name-as-directory (getenv "TEMP")))
209 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
210 (file-name-as-directory (getenv "TMP")))
211 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
212 (file-name-as-directory (getenv "TMPDIR")))
213 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
214 (t (message (concat "Neither `temporary-file-directory' nor "
215 "`temp-directory' is defined -- using /tmp."))
216 (file-name-as-directory "/tmp"))))
217
218 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
219 ;; implementation with `make-temp-name', creating the temporary file
220 ;; immediately in order to avoid a security hole.
221 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
222 "Create a temporary file (compat function).
223 Add the extension of FILENAME, if existing."
224 (let* (file-name-handler-alist
225 (prefix (expand-file-name
226 (symbol-value 'tramp-temp-name-prefix)
227 (tramp-compat-temporary-file-directory)))
228 (extension (file-name-extension filename t))
229 result)
230 (condition-case nil
231 (setq result
232 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
233 (error
234 ;; We use our own implementation, taken from files.el.
235 (while
236 (condition-case ()
237 (progn
238 (setq result (concat (make-temp-name prefix) extension))
239 (if dir-flag
240 (make-directory result)
241 (write-region "" nil result nil 'silent))
242 nil)
243 (file-already-exists t))
244 ;; The file was somehow created by someone else between
245 ;; `make-temp-name' and `write-region', let's try again.
246 nil)))
247 result))
248
249 ;; `most-positive-fixnum' does not exist in XEmacs.
250 (defsubst tramp-compat-most-positive-fixnum ()
251 "Return largest positive integer value (compat function)."
252 (cond
253 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
254 ;; Default value in XEmacs.
255 (t 134217727)))
256
257 (defun tramp-compat-decimal-to-octal (i)
258 "Return a string consisting of the octal digits of I.
259 Not actually used. Use `(format \"%o\" i)' instead?"
260 (cond ((< i 0) (error "Cannot convert negative number to octal"))
261 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
262 ((zerop i) "0")
263 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
264 (number-to-string (% i 8))))))
265
266 ;; Kudos to Gerd Moellmann for this suggestion.
267 (defun tramp-compat-octal-to-decimal (ostr)
268 "Given a string of octal digits, return a decimal number."
269 (let ((x (or ostr "")))
270 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
271 (unless (string-match "\\`[0-7]*\\'" x)
272 (error "Non-octal junk in string `%s'" x))
273 (string-to-number ostr 8)))
274
275 ;; ID-FORMAT does not exists in XEmacs.
276 (defun tramp-compat-file-attributes (filename &optional id-format)
277 "Like `file-attributes' for Tramp files (compat function)."
278 (cond
279 ((or (null id-format) (eq id-format 'integer))
280 (file-attributes filename))
281 ((tramp-tramp-file-p filename)
282 (tramp-file-name-handler 'file-attributes filename id-format))
283 (t (condition-case nil
284 (tramp-compat-funcall 'file-attributes filename id-format)
285 (wrong-number-of-arguments (file-attributes filename))))))
286
287 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
288 ;; hurt to ignore it for other (X)Emacs versions.
289 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.
290 (defun tramp-compat-copy-file
291 (filename newname &optional ok-if-already-exists keep-date
292 preserve-uid-gid preserve-selinux-context)
293 "Like `copy-file' for Tramp files (compat function)."
294 (cond
295 (preserve-selinux-context
296 (tramp-compat-funcall
297 'copy-file filename newname ok-if-already-exists keep-date
298 preserve-uid-gid preserve-selinux-context))
299 (preserve-uid-gid
300 (tramp-compat-funcall
301 'copy-file filename newname ok-if-already-exists keep-date
302 preserve-uid-gid))
303 (t
304 (copy-file filename newname ok-if-already-exists keep-date))))
305
306 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
307 ;; is taken from there.
308 (defun tramp-compat-copy-directory
309 (directory newname &optional keep-time parents)
310 "Make a copy of DIRECTORY (compat function)."
311 (if (fboundp 'copy-directory)
312 (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
313
314 ;; If `default-directory' is a remote directory, make sure we find
315 ;; its `copy-directory' handler.
316 (let ((handler (or (find-file-name-handler directory 'copy-directory)
317 (find-file-name-handler newname 'copy-directory))))
318 (if handler
319 (funcall handler 'copy-directory directory newname keep-time parents)
320
321 ;; Compute target name.
322 (setq directory (directory-file-name (expand-file-name directory))
323 newname (directory-file-name (expand-file-name newname)))
324 (if (and (file-directory-p newname)
325 (not (string-equal (file-name-nondirectory directory)
326 (file-name-nondirectory newname))))
327 (setq newname
328 (expand-file-name
329 (file-name-nondirectory directory) newname)))
330 (if (not (file-directory-p newname)) (make-directory newname parents))
331
332 ;; Copy recursively.
333 (mapc
334 (lambda (file)
335 (if (file-directory-p file)
336 (tramp-compat-copy-directory file newname keep-time parents)
337 (copy-file file newname t keep-time)))
338 ;; We do not want to delete "." and "..".
339 (directory-files
340 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
341
342 ;; Set directory attributes.
343 (set-file-modes newname (file-modes directory))
344 (if keep-time
345 (set-file-times newname (nth 5 (file-attributes directory))))))))
346
347 ;; TRASH has been introduced with Emacs 24.1.
348 (defun tramp-compat-delete-file (filename &optional trash)
349 "Like `delete-file' for Tramp files (compat function)."
350 (condition-case nil
351 (tramp-compat-funcall 'delete-file filename trash)
352 ;; This Emacs version does not support the TRASH flag.
353 (wrong-number-of-arguments
354 (let ((delete-by-moving-to-trash
355 (and (boundp 'delete-by-moving-to-trash)
356 (symbol-value 'delete-by-moving-to-trash)
357 trash)))
358 (delete-file filename)))))
359
360 ;; RECURSIVE has been introduced with Emacs 23.2.
361 (defun tramp-compat-delete-directory (directory &optional recursive)
362 "Like `delete-directory' for Tramp files (compat function)."
363 (if (null recursive)
364 (delete-directory directory)
365 (condition-case nil
366 (tramp-compat-funcall 'delete-directory directory recursive)
367 ;; This Emacs version does not support the RECURSIVE flag. We
368 ;; use the implementation from Emacs 23.2.
369 (wrong-number-of-arguments
370 (setq directory (directory-file-name (expand-file-name directory)))
371 (if (not (file-symlink-p directory))
372 (mapc (lambda (file)
373 (if (eq t (car (file-attributes file)))
374 (tramp-compat-delete-directory file recursive)
375 (delete-file file)))
376 (directory-files
377 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
378 (delete-directory directory)))))
379
380 ;; `number-sequence' does not exist in XEmacs. Implementation is
381 ;; taken from Emacs 23.
382 (defun tramp-compat-number-sequence (from &optional to inc)
383 "Return a sequence of numbers from FROM to TO as a list (compat function)."
384 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
385 (tramp-compat-funcall 'number-sequence from to inc)
386 (if (or (not to) (= from to))
387 (list from)
388 (or inc (setq inc 1))
389 (when (zerop inc) (error "The increment can not be zero"))
390 (let (seq (n 0) (next from))
391 (if (> inc 0)
392 (while (<= next to)
393 (setq seq (cons next seq)
394 n (1+ n)
395 next (+ from (* n inc))))
396 (while (>= next to)
397 (setq seq (cons next seq)
398 n (1+ n)
399 next (+ from (* n inc)))))
400 (nreverse seq)))))
401
402 (defun tramp-compat-split-string (string pattern)
403 "Like `split-string' but omit empty strings.
404 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
405 This is, the first, empty, element is omitted. In XEmacs, the first
406 element is not omitted."
407 (delete "" (split-string string pattern)))
408
409 (defun tramp-compat-call-process
410 (program &optional infile destination display &rest args)
411 "Calls `call-process' on the local host.
412 This is needed because for some Emacs flavors Tramp has
413 defadviced `call-process' to behave like `process-file'. The
414 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
415 (let ((default-directory
416 (if (file-remote-p default-directory)
417 (tramp-compat-temporary-file-directory)
418 default-directory)))
419 (if (executable-find program)
420 (apply 'call-process program infile destination display args)
421 1)))
422
423 (defun tramp-compat-process-running-p (process-name)
424 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
425 (when (stringp process-name)
426 (cond
427 ;; GNU Emacs 22 on w32.
428 ((fboundp 'w32-window-exists-p)
429 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
430
431 ;; GNU Emacs 23.
432 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
433 (let (result)
434 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
435 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
436 (when (and (string-equal
437 (cdr (assoc 'user attributes)) (user-login-name))
438 (let ((comm (cdr (assoc 'comm attributes))))
439 ;; The returned command name could be truncated
440 ;; to 15 characters. Therefore, we cannot check
441 ;; for `string-equal'.
442 (and comm (string-match
443 (concat "^" (regexp-quote comm))
444 process-name))))
445 (setq result t))))))
446
447 ;; Fallback, if there is no Lisp support yet.
448 (t (let ((default-directory
449 (if (file-remote-p default-directory)
450 (tramp-compat-temporary-file-directory)
451 default-directory))
452 (unix95 (getenv "UNIX95"))
453 result)
454 (setenv "UNIX95" "1")
455 (when (member
456 (user-login-name)
457 (tramp-compat-split-string
458 (shell-command-to-string
459 (format "ps -C %s -o user=" process-name))
460 "[ \f\t\n\r\v]+"))
461 (setq result t))
462 (setenv "UNIX95" unix95)
463 result)))))
464
465 (add-hook 'tramp-unload-hook
466 (lambda ()
467 (unload-feature 'tramp-compat 'force)))
468
469 (provide 'tramp-compat)
470
471 ;;; TODO:
472
473 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
474 ;;; tramp-compat.el ends here