aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Albinus2010-09-13 17:17:01 +0200
committerMichael Albinus2010-09-13 17:17:01 +0200
commit03c1ad4351c10ec998410e8d34f52ac4a1a0decd (patch)
tree346a89ab10838632537671c6dde3fdc0cb9abe3d
parentc7a1fb9be8e736296bfb5520f1b74b2809e46e5f (diff)
downloademacs-03c1ad4351c10ec998410e8d34f52ac4a1a0decd.tar.gz
emacs-03c1ad4351c10ec998410e8d34f52ac4a1a0decd.zip
Major code cleanup. Split tramp.el into tramp.el and tramp-sh.el.
* Makefile.in (TRAMP_SRC): Remove tramp-fish.el. Add tramp-sh.el. * net/tramp.el (top): Don't show loading message. Require just 'tramp-compat, everything else is required there. Use `ignore-errors' where appropriate. (tramp-inline-compress-start-size, tramp-copy-size-limit) (tramp-terminal-type, tramp-end-of-output) (tramp-initial-end-of-output, tramp-completion-function-alist-rsh) (tramp-completion-function-alist-ssh) (tramp-completion-function-alist-telnet) (tramp-completion-function-alist-su) (tramp-completion-function-alist-putty, tramp-remote-path) (tramp-remote-process-environment, tramp-sh-extra-args) (tramp-actions-before-shell, tramp-uudecode) (tramp-perl-file-truename, tramp-perl-file-name-all-completions) (tramp-perl-file-attributes) (tramp-perl-directory-files-and-attributes) (tramp-perl-encode-with-module, tramp-perl-decode-with-module) (tramp-perl-encode, tramp-perl-decode) (tramp-vc-registered-read-file-names, tramp-file-mode-type-map) (tramp-file-name-handler-alist, tramp-make-tramp-temp-file) (tramp-handle-make-symbolic-link, tramp-handle-load) (tramp-handle-file-name-as-directory) (tramp-handle-file-name-directory) (tramp-handle-file-name-nondirectory, tramp-handle-file-truename) (tramp-handle-file-exists-p, tramp-handle-file-attributes) (tramp-do-file-attributes-with-ls) (tramp-do-file-attributes-with-perl) (tramp-do-file-attributes-with-stat) (tramp-handle-set-visited-file-modtime) (tramp-handle-verify-visited-file-modtime) (tramp-handle-set-file-modes, tramp-handle-set-file-times) (tramp-set-file-uid-gid, tramp-remote-selinux-p) (tramp-handle-file-selinux-context) (tramp-handle-set-file-selinux-context) (tramp-handle-file-executable-p, tramp-handle-file-readable-p) (tramp-handle-file-newer-than-file-p, tramp-handle-file-modes) (tramp-handle-file-directory-p, tramp-handle-file-regular-p) (tramp-handle-file-symlink-p, tramp-handle-file-writable-p) (tramp-handle-file-ownership-preserved-p) (tramp-handle-directory-file-name, tramp-handle-directory-files) (tramp-handle-directory-files-and-attributes) (tramp-do-directory-files-and-attributes-with-perl) (tramp-do-directory-files-and-attributes-with-stat) (tramp-handle-file-name-all-completions) (tramp-handle-file-name-completion, tramp-handle-add-name-to-file) (tramp-handle-copy-file, tramp-handle-copy-directory) (tramp-handle-rename-file, tramp-do-copy-or-rename-file) (tramp-do-copy-or-rename-file-via-buffer) (tramp-do-copy-or-rename-file-directly) (tramp-do-copy-or-rename-file-out-of-band) (tramp-handle-make-directory, tramp-handle-delete-directory) (tramp-handle-delete-file) (tramp-handle-dired-recursive-delete-directory) (tramp-handle-dired-compress-file, tramp-handle-dired-uncache) (tramp-handle-insert-directory) (tramp-handle-unhandled-file-name-directory) (tramp-handle-expand-file-name) (tramp-handle-substitute-in-file-name) (tramp-handle-executable-find, tramp-process-sentinel) (tramp-handle-start-file-process, tramp-handle-process-file) (tramp-handle-call-process-region, tramp-handle-shell-command) (tramp-handle-file-local-copy, tramp-handle-file-remote-p) (tramp-handle-insert-file-contents) (tramp-handle-insert-file-contents-literally) (tramp-handle-find-backup-file-name) (tramp-handle-make-auto-save-file-name, tramp-handle-write-region) (tramp-vc-registered-file-names, tramp-handle-vc-registered) (tramp-sh-file-name-handler, tramp-vc-file-name-handler) (tramp-maybe-send-script, tramp-set-auto-save, tramp-run-test) (tramp-run-test2, tramp-find-executable, tramp-set-remote-path) (tramp-find-file-exists-command, tramp-open-shell) (tramp-find-shell, tramp-barf-if-no-shell-prompt) (tramp-open-connection-setup-interactive-shell) (tramp-local-coding-commands, tramp-remote-coding-commands) (tramp-find-inline-encoding, tramp-call-local-coding-command) (tramp-inline-compress-commands, tramp-find-inline-compress) (tramp-compute-multi-hops, tramp-maybe-open-connection) (tramp-send-command , tramp-wait-for-output) (tramp-send-command-and-check, tramp-barf-unless-okay) (tramp-send-command-and-read, tramp-mode-string-to-int) (tramp-convert-file-attributes, tramp-check-cached-permissions) (tramp-file-mode-from-int, tramp-file-mode-permissions) (tramp-shell-case-fold, tramp-make-copy-program-file-name) (tramp-method-out-of-band-p, tramp-local-host-p) (tramp-get-remote-path, tramp-get-remote-tmpdir) (tramp-get-ls-command, tramp-get-ls-command-with-dired) (tramp-get-test-command, tramp-get-test-nt-command) (tramp-get-file-exists-command, tramp-get-remote-ln) (tramp-get-remote-perl, tramp-get-remote-stat) (tramp-get-remote-readlink, tramp-get-remote-trash) (tramp-get-remote-id, tramp-get-remote-uid, tramp-get-remote-gid) (tramp-get-local-uid, tramp-get-local-gid) (tramp-get-inline-compress, tramp-get-inline-coding, ): Move to tramp-sh.el. (tramp-methods, tramp-default-method-alist) (tramp-default-user-alist, tramp-foreign-file-name-handler-alist): Move initialization to tramp-sh.el. (tramp-temp-name-prefix): Make it a defconst. (tramp-dissect-file-name): Don't check anymore for multi-hop methods. (tramp-debug-outline-regexp): Add a docstring. (tramp-debug-outline-level): Renamed from `tramp-outline-level'. (tramp-get-debug-buffer): Use it. * net/tramp-cache.el (top): Set tramp-autoload cookie for initialization forms. (tramp-set-connection-property): Don't protect `tramp-message' call, it isn't necessary any longer. (tramp-dump-connection-properties): Use `ignore-errors'. * net/tramp-compat.el (top): Require 'advice, 'format-spec, 'password-cache and 'auth-source. * net/tramp-gvfs.el (top): * net/tramp-smb.el (top): Require 'tramp-sh. * net/tramp-gw.el (tramp-gw-open-network-stream): Use `ignore-errors'. * net/tramp-sh.el: New file, derived from tramp.el. (top): Initialize `tramp-methods', `tramp-default-method-alist', `tramp-default-user-alist', tramp-foreign-file-name-handler-alist. Remove "scp1_old", "scp2_old", "ssh1_old", "ssh2_old". Use `ignore-errors' where appropriate. (tramp-sh-file-name-handler-alist): Renamed from `tramp-file-name-handler-alist'. (tramp-send-command-and-check): Return t or nil. Remove all `zerop' checks, where called. (tramp-handle-set-file-modes) (tramp-do-copy-or-rename-file-directly) (tramp-handle-delete-directory, tramp-handle-delete-file) (tramp-maybe-send-script, ): Use `tramp-barf-unless-okay'. (tramp-sh-file-name-handler, tramp-send-command-and-check) (tramp-get-remote-ln): Set tramp-autoload cookie. * net/tramp-fish.el: Remove file.
-rw-r--r--lisp/ChangeLog141
-rw-r--r--lisp/Makefile.in6
-rw-r--r--lisp/net/tramp-cache.el80
-rw-r--r--lisp/net/tramp-compat.el14
-rw-r--r--lisp/net/tramp-fish.el1186
-rw-r--r--lisp/net/tramp-ftp.el6
-rw-r--r--lisp/net/tramp-gvfs.el5
-rw-r--r--lisp/net/tramp-gw.el7
-rw-r--r--lisp/net/tramp-sh.el5506
-rw-r--r--lisp/net/tramp-smb.el4
-rw-r--r--lisp/net/tramp.el5830
11 files changed, 5803 insertions, 6982 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index f0e59a6c6a6..c216119fd0f 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,144 @@
12010-09-13 Michael Albinus <michael.albinus@gmx.de>
2
3 Major code cleanup. Split tramp.el into tramp.el and tramp-sh.el.
4
5 * Makefile.in (TRAMP_SRC): Remove tramp-fish.el. Add tramp-sh.el.
6
7 * net/tramp.el (top): Don't show loading message. Require just
8 'tramp-compat, everything else is required there. Use
9 `ignore-errors' where appropriate.
10 (tramp-inline-compress-start-size, tramp-copy-size-limit)
11 (tramp-terminal-type, tramp-end-of-output)
12 (tramp-initial-end-of-output, tramp-completion-function-alist-rsh)
13 (tramp-completion-function-alist-ssh)
14 (tramp-completion-function-alist-telnet)
15 (tramp-completion-function-alist-su)
16 (tramp-completion-function-alist-putty, tramp-remote-path)
17 (tramp-remote-process-environment, tramp-sh-extra-args)
18 (tramp-actions-before-shell, tramp-uudecode)
19 (tramp-perl-file-truename, tramp-perl-file-name-all-completions)
20 (tramp-perl-file-attributes)
21 (tramp-perl-directory-files-and-attributes)
22 (tramp-perl-encode-with-module, tramp-perl-decode-with-module)
23 (tramp-perl-encode, tramp-perl-decode)
24 (tramp-vc-registered-read-file-names, tramp-file-mode-type-map)
25 (tramp-file-name-handler-alist, tramp-make-tramp-temp-file)
26 (tramp-handle-make-symbolic-link, tramp-handle-load)
27 (tramp-handle-file-name-as-directory)
28 (tramp-handle-file-name-directory)
29 (tramp-handle-file-name-nondirectory, tramp-handle-file-truename)
30 (tramp-handle-file-exists-p, tramp-handle-file-attributes)
31 (tramp-do-file-attributes-with-ls)
32 (tramp-do-file-attributes-with-perl)
33 (tramp-do-file-attributes-with-stat)
34 (tramp-handle-set-visited-file-modtime)
35 (tramp-handle-verify-visited-file-modtime)
36 (tramp-handle-set-file-modes, tramp-handle-set-file-times)
37 (tramp-set-file-uid-gid, tramp-remote-selinux-p)
38 (tramp-handle-file-selinux-context)
39 (tramp-handle-set-file-selinux-context)
40 (tramp-handle-file-executable-p, tramp-handle-file-readable-p)
41 (tramp-handle-file-newer-than-file-p, tramp-handle-file-modes)
42 (tramp-handle-file-directory-p, tramp-handle-file-regular-p)
43 (tramp-handle-file-symlink-p, tramp-handle-file-writable-p)
44 (tramp-handle-file-ownership-preserved-p)
45 (tramp-handle-directory-file-name, tramp-handle-directory-files)
46 (tramp-handle-directory-files-and-attributes)
47 (tramp-do-directory-files-and-attributes-with-perl)
48 (tramp-do-directory-files-and-attributes-with-stat)
49 (tramp-handle-file-name-all-completions)
50 (tramp-handle-file-name-completion, tramp-handle-add-name-to-file)
51 (tramp-handle-copy-file, tramp-handle-copy-directory)
52 (tramp-handle-rename-file, tramp-do-copy-or-rename-file)
53 (tramp-do-copy-or-rename-file-via-buffer)
54 (tramp-do-copy-or-rename-file-directly)
55 (tramp-do-copy-or-rename-file-out-of-band)
56 (tramp-handle-make-directory, tramp-handle-delete-directory)
57 (tramp-handle-delete-file)
58 (tramp-handle-dired-recursive-delete-directory)
59 (tramp-handle-dired-compress-file, tramp-handle-dired-uncache)
60 (tramp-handle-insert-directory)
61 (tramp-handle-unhandled-file-name-directory)
62 (tramp-handle-expand-file-name)
63 (tramp-handle-substitute-in-file-name)
64 (tramp-handle-executable-find, tramp-process-sentinel)
65 (tramp-handle-start-file-process, tramp-handle-process-file)
66 (tramp-handle-call-process-region, tramp-handle-shell-command)
67 (tramp-handle-file-local-copy, tramp-handle-file-remote-p)
68 (tramp-handle-insert-file-contents)
69 (tramp-handle-insert-file-contents-literally)
70 (tramp-handle-find-backup-file-name)
71 (tramp-handle-make-auto-save-file-name, tramp-handle-write-region)
72 (tramp-vc-registered-file-names, tramp-handle-vc-registered)
73 (tramp-sh-file-name-handler, tramp-vc-file-name-handler)
74 (tramp-maybe-send-script, tramp-set-auto-save, tramp-run-test)
75 (tramp-run-test2, tramp-find-executable, tramp-set-remote-path)
76 (tramp-find-file-exists-command, tramp-open-shell)
77 (tramp-find-shell, tramp-barf-if-no-shell-prompt)
78 (tramp-open-connection-setup-interactive-shell)
79 (tramp-local-coding-commands, tramp-remote-coding-commands)
80 (tramp-find-inline-encoding, tramp-call-local-coding-command)
81 (tramp-inline-compress-commands, tramp-find-inline-compress)
82 (tramp-compute-multi-hops, tramp-maybe-open-connection)
83 (tramp-send-command , tramp-wait-for-output)
84 (tramp-send-command-and-check, tramp-barf-unless-okay)
85 (tramp-send-command-and-read, tramp-mode-string-to-int)
86 (tramp-convert-file-attributes, tramp-check-cached-permissions)
87 (tramp-file-mode-from-int, tramp-file-mode-permissions)
88 (tramp-shell-case-fold, tramp-make-copy-program-file-name)
89 (tramp-method-out-of-band-p, tramp-local-host-p)
90 (tramp-get-remote-path, tramp-get-remote-tmpdir)
91 (tramp-get-ls-command, tramp-get-ls-command-with-dired)
92 (tramp-get-test-command, tramp-get-test-nt-command)
93 (tramp-get-file-exists-command, tramp-get-remote-ln)
94 (tramp-get-remote-perl, tramp-get-remote-stat)
95 (tramp-get-remote-readlink, tramp-get-remote-trash)
96 (tramp-get-remote-id, tramp-get-remote-uid, tramp-get-remote-gid)
97 (tramp-get-local-uid, tramp-get-local-gid)
98 (tramp-get-inline-compress, tramp-get-inline-coding, ): Move to
99 tramp-sh.el.
100 (tramp-methods, tramp-default-method-alist)
101 (tramp-default-user-alist, tramp-foreign-file-name-handler-alist):
102 Move initialization to tramp-sh.el.
103 (tramp-temp-name-prefix): Make it a defconst.
104 (tramp-dissect-file-name): Don't check anymore for multi-hop
105 methods.
106 (tramp-debug-outline-regexp): Add a docstring.
107 (tramp-debug-outline-level): Renamed from `tramp-outline-level'.
108 (tramp-get-debug-buffer): Use it.
109
110 * net/tramp-cache.el (top): Set tramp-autoload cookie for
111 initialization forms.
112 (tramp-set-connection-property): Don't protect `tramp-message'
113 call, it isn't necessary any longer.
114 (tramp-dump-connection-properties): Use `ignore-errors'.
115
116 * net/tramp-compat.el (top): Require 'advice, 'format-spec,
117 'password-cache and 'auth-source.
118
119 * net/tramp-gvfs.el (top):
120 * net/tramp-smb.el (top): Require 'tramp-sh.
121
122 * net/tramp-gw.el (tramp-gw-open-network-stream): Use `ignore-errors'.
123
124 * net/tramp-sh.el: New file, derived from tramp.el.
125 (top): Initialize `tramp-methods', `tramp-default-method-alist',
126 `tramp-default-user-alist', tramp-foreign-file-name-handler-alist.
127 Remove "scp1_old", "scp2_old", "ssh1_old", "ssh2_old". Use
128 `ignore-errors' where appropriate.
129 (tramp-sh-file-name-handler-alist): Renamed from
130 `tramp-file-name-handler-alist'.
131 (tramp-send-command-and-check): Return t or nil. Remove all
132 `zerop' checks, where called.
133 (tramp-handle-set-file-modes)
134 (tramp-do-copy-or-rename-file-directly)
135 (tramp-handle-delete-directory, tramp-handle-delete-file)
136 (tramp-maybe-send-script, ): Use `tramp-barf-unless-okay'.
137 (tramp-sh-file-name-handler, tramp-send-command-and-check)
138 (tramp-get-remote-ln): Set tramp-autoload cookie.
139
140 * net/tramp-fish.el: Remove file.
141
12010-09-13 Daiki Ueno <ueno@unixuser.org> 1422010-09-13 Daiki Ueno <ueno@unixuser.org>
2 143
3 * epa-file.el (epa-file-insert-file-contents): If visiting, bind 144 * epa-file.el (epa-file-insert-file-contents): If visiting, bind
diff --git a/lisp/Makefile.in b/lisp/Makefile.in
index 918ce0ecc64..1e2a7c4d48b 100644
--- a/lisp/Makefile.in
+++ b/lisp/Makefile.in
@@ -336,9 +336,9 @@ $(MH_E_DIR)/mh-loaddefs.el: $(MH_E_SRC)
336TRAMP_DIR = $(lisp)/net 336TRAMP_DIR = $(lisp)/net
337TRAMP_SRC = $(TRAMP_DIR)/tramp.el $(TRAMP_DIR)/tramp-cache.el \ 337TRAMP_SRC = $(TRAMP_DIR)/tramp.el $(TRAMP_DIR)/tramp-cache.el \
338 $(TRAMP_DIR)/tramp-cmds.el $(TRAMP_DIR)/tramp-compat.el \ 338 $(TRAMP_DIR)/tramp-cmds.el $(TRAMP_DIR)/tramp-compat.el \
339 $(TRAMP_DIR)/tramp-fish.el $(TRAMP_DIR)/tramp-ftp.el \ 339 $(TRAMP_DIR)/tramp-ftp.el $(TRAMP_DIR)/tramp-gvfs.el \
340 $(TRAMP_DIR)/tramp-gvfs.el $(TRAMP_DIR)/tramp-gw.el \ 340 $(TRAMP_DIR)/tramp-gw.el $(TRAMP_DIR)/tramp-imap.el \
341 $(TRAMP_DIR)/tramp-imap.el $(TRAMP_DIR)/tramp-smb.el \ 341 $(TRAMP_DIR)/tramp-sh.el $(TRAMP_DIR)/tramp-smb.el \
342 $(TRAMP_DIR)/tramp-uu.el $(TRAMP_DIR)/trampver.el 342 $(TRAMP_DIR)/tramp-uu.el $(TRAMP_DIR)/trampver.el
343 343
344$(TRAMP_DIR)/tramp-loaddefs.el: $(TRAMP_SRC) 344$(TRAMP_DIR)/tramp-loaddefs.el: $(TRAMP_SRC)
diff --git a/lisp/net/tramp-cache.el b/lisp/net/tramp-cache.el
index a2864a6328f..7a55a4978d1 100644
--- a/lisp/net/tramp-cache.el
+++ b/lisp/net/tramp-cache.el
@@ -149,8 +149,10 @@ FILE must be a local file name on a connection identified via VEC."
149 value) 149 value)
150 ,@body)) 150 ,@body))
151 151
152;;;###tramp-autoload
152(put 'with-file-property 'lisp-indent-function 3) 153(put 'with-file-property 'lisp-indent-function 3)
153(put 'with-file-property 'edebug-form-spec t) 154(put 'with-file-property 'edebug-form-spec t)
155;;;###tramp-autoload
154(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>")) 156(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
155 157
156;;;###tramp-autoload 158;;;###tramp-autoload
@@ -235,12 +237,7 @@ PROPERTY is set persistent when KEY is a vector."
235 tramp-cache-data)))) 237 tramp-cache-data))))
236 (puthash property value hash) 238 (puthash property value hash)
237 (setq tramp-cache-data-changed t) 239 (setq tramp-cache-data-changed t)
238 ;; This function is called also during initialization of 240 (tramp-message key 7 "%s %s" property value)
239 ;; tramp-cache.el. `tramp-message´ is not defined yet at this
240 ;; time, so we ignore the corresponding error.
241 (condition-case nil
242 (tramp-message key 7 "%s %s" property value)
243 (error nil))
244 value)) 241 value))
245 242
246;;;###tramp-autoload 243;;;###tramp-autoload
@@ -255,8 +252,10 @@ PROPERTY is set persistent when KEY is a vector."
255 (tramp-set-connection-property ,key ,property value)) 252 (tramp-set-connection-property ,key ,property value))
256 value)) 253 value))
257 254
255;;;###tramp-autoload
258(put 'with-connection-property 'lisp-indent-function 2) 256(put 'with-connection-property 'lisp-indent-function 2)
259(put 'with-connection-property 'edebug-form-spec t) 257(put 'with-connection-property 'edebug-form-spec t)
258;;;###tramp-autoload
260(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>")) 259(font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
261 260
262;;;###tramp-autoload 261;;;###tramp-autoload
@@ -315,41 +314,40 @@ KEY identifies the connection, it is either a process or a vector."
315(defun tramp-dump-connection-properties () 314(defun tramp-dump-connection-properties ()
316 "Write persistent connection properties into file `tramp-persistency-file-name'." 315 "Write persistent connection properties into file `tramp-persistency-file-name'."
317 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed. 316 ;; We shouldn't fail, otherwise (X)Emacs might not be able to be closed.
318 (condition-case nil 317 (ignore-errors
319 (when (and (hash-table-p tramp-cache-data) 318 (when (and (hash-table-p tramp-cache-data)
320 (not (zerop (hash-table-count tramp-cache-data))) 319 (not (zerop (hash-table-count tramp-cache-data)))
321 tramp-cache-data-changed 320 tramp-cache-data-changed
322 (stringp tramp-persistency-file-name)) 321 (stringp tramp-persistency-file-name))
323 (let ((cache (copy-hash-table tramp-cache-data))) 322 (let ((cache (copy-hash-table tramp-cache-data)))
324 ;; Remove temporary data. 323 ;; Remove temporary data.
325 (maphash 324 (maphash
326 '(lambda (key value) 325 '(lambda (key value)
327 (if (and (vectorp key) (not (tramp-file-name-localname key))) 326 (if (and (vectorp key) (not (tramp-file-name-localname key)))
328 (progn 327 (progn
329 (remhash "process-name" value) 328 (remhash "process-name" value)
330 (remhash "process-buffer" value) 329 (remhash "process-buffer" value)
331 (remhash "first-password-request" value)) 330 (remhash "first-password-request" value))
332 (remhash key cache))) 331 (remhash key cache)))
333 cache) 332 cache)
334 ;; Dump it. 333 ;; Dump it.
335 (with-temp-buffer 334 (with-temp-buffer
336 (insert 335 (insert
337 ";; -*- emacs-lisp -*-" 336 ";; -*- emacs-lisp -*-"
338 ;; `time-stamp-string' might not exist in all (X)Emacs flavors. 337 ;; `time-stamp-string' might not exist in all (X)Emacs flavors.
339 (condition-case nil 338 (condition-case nil
340 (progn 339 (progn
341 (format 340 (format
342 " <%s %s>\n" 341 " <%s %s>\n"
343 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S") 342 (time-stamp-string "%02y/%02m/%02d %02H:%02M:%02S")
344 tramp-persistency-file-name)) 343 tramp-persistency-file-name))
345 (error "\n")) 344 (error "\n"))
346 ";; Tramp connection history. Don't change this file.\n" 345 ";; Tramp connection history. Don't change this file.\n"
347 ";; You can delete it, forcing Tramp to reapply the checks.\n\n" 346 ";; You can delete it, forcing Tramp to reapply the checks.\n\n"
348 (with-output-to-string 347 (with-output-to-string
349 (pp (read (format "(%s)" (tramp-cache-print cache)))))) 348 (pp (read (format "(%s)" (tramp-cache-print cache))))))
350 (write-region 349 (write-region
351 (point-min) (point-max) tramp-persistency-file-name)))) 350 (point-min) (point-max) tramp-persistency-file-name))))))
352 (error nil)))
353 351
354(add-hook 'kill-emacs-hook 'tramp-dump-connection-properties) 352(add-hook 'kill-emacs-hook 'tramp-dump-connection-properties)
355(add-hook 'tramp-cache-unload-hook 353(add-hook 'tramp-cache-unload-hook
diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el
index cd2f0b0ddf5..d17ad8d09ce 100644
--- a/lisp/net/tramp-compat.el
+++ b/lisp/net/tramp-compat.el
@@ -38,7 +38,21 @@
38 38
39(eval-and-compile 39(eval-and-compile
40 40
41 (require 'advice)
41 (require 'custom) 42 (require 'custom)
43 (require 'format-spec)
44
45 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
46 ;; mandatory.
47 (if (featurep 'xemacs)
48 (load "password" 'noerror)
49 (or (require 'password-cache nil 'noerror)
50 (require 'password nil 'noerror))) ; Part of contrib.
51
52 ;; auth-source is relatively new.
53 (if (featurep 'xemacs)
54 (load "auth-source" 'noerror)
55 (require 'auth-source nil 'noerror))
42 56
43 ;; Load the appropriate timer package. 57 ;; Load the appropriate timer package.
44 (if (featurep 'xemacs) 58 (if (featurep 'xemacs)
diff --git a/lisp/net/tramp-fish.el b/lisp/net/tramp-fish.el
deleted file mode 100644
index e5d0ffd3366..00000000000
--- a/lisp/net/tramp-fish.el
+++ /dev/null
@@ -1,1186 +0,0 @@
1;;; tramp-fish.el --- Tramp access functions for FISH protocol
2
3;; Copyright (C) 2006, 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;; Access functions for FIles transferred over SHell protocol from Tramp.
27
28;; FISH is a protocol developped for the GNU Midnight Commander
29;; <https://savannah.gnu.org/projects/mc>. A client connects to a
30;; remote host via ssh (or rsh, shall be configurable), and starts
31;; there a fish server via the command "start_fish_server". All
32;; commands from the client have the form "#FISH_COMMAND\n" (always
33;; one line), followed by equivalent shell commands in case there is
34;; no fish server running.
35
36;; The fish server (or the equivalent shell commands) must return the
37;; response, which is finished by a line "### xxx <optional text>\n".
38;; "xxx" stands for 3 digits, representing a return code. Return
39;; codes "# 000" and "# 001" are reserved for fallback implementation
40;; with native shell commands; they are not used inside the server. See
41;; <http://cvs.savannah.gnu.org/viewcvs/mc/vfs/README.fish?root=mc&view=markup>
42;; for details of original specification.
43
44;; The GNU Midnight Commander implements the original fish protocol
45;; version 0.0.2. The KDE Konqueror has its own implementation, which
46;; can be found at
47;; <http://websvn.kde.org/branches/KDE/3.5/kdebase/kioslave/fish>. It
48;; implements an extended protocol version 0.0.3. Additionally, it
49;; provides a fish server implementation in Perl (which is the only
50;; implementation I've heard of). The following command reference is
51;; based on that implementation.
52
53;; All commands return either "### 2xx\n" (OK) or "### 5xx <optional text>\n"
54;; (NOK). Return codes are mentioned only if they are different from this.
55;; Spaces in any parameter must be escaped by "\ ".
56
57;; Command/Return Code Comment
58;;
59;; #FISH initial connection, not used
60;; in .fishsrv.pl
61;; ### 100 transfer fish server missing server, or wrong checksum
62;; version 0.0.3 only
63
64;; #VER a.b.c <commands requested>
65;; VER x.y.z <commands offered> .fishsrv.pl response is not uptodate
66
67;; #PWD
68;; /path/to/file
69
70;; #CWD /some/path
71
72;; #COPY /path/a /path/b version 0.0.3 only
73
74;; #RENAME /path/a /path/b
75
76;; #SYMLINK /path/a /path/b
77
78;; #LINK /path/a /path/b
79
80;; #DELE /some/path
81
82;; #MKD /some/path
83
84;; #RMD /some/path
85
86;; #CHOWN user /file/name
87
88;; #CHGRP group /file/name
89
90;; #CHMOD 1234 file
91
92;; #READ <offset> <size> /path/and/filename
93;; ### 291 successful exit when reading
94;; ended at eof
95;; ### 292 successful exit when reading
96;; did not end at eof
97
98;; #WRITE <offset> <size> /path/and/filename
99
100;; #APPEND <size> /path/and/filename version 0.0.3 only
101
102;; #LIST /directory
103;; <number of entries> version 0.0.3 only
104;; ### 100 version 0.0.3 only
105;; P<unix permissions> <owner>.<group>
106;; S<size>
107;; d<3-letters month name> <day> <year or HH:MM>
108;; D<year> <month> <day> <hour> <minute> <second>[.1234]
109;; E<major-of-device>,<minor>
110;; :<filename>
111;; L<filename symlink points to>
112;; M<mimetype> version 0.0.3 only
113;; <blank line to separate items>
114
115;; #STAT /file version 0.0.3 only
116;; like #LIST except for directories
117;; <number of entries>
118;; ### 100
119;; P<unix permissions> <owner>.<group>
120;; S<size>
121;; d<3-letters month name> <day> <year or HH:MM>
122;; D<year> <month> <day> <hour> <minute> <second>[.1234]
123;; E<major-of-device>,<minor>
124;; :<filename>
125;; L<filename symlink points to>
126;; <blank line to separate items>
127
128;; #RETR /some/name
129;; <filesize>
130;; ### 100
131;; <binary data> exactly filesize bytes
132;; ### 200 with no preceding newline
133
134;; #STOR <size> /file/name
135;; ### 100
136;; <data> exactly size bytes
137;; ### 001 partial success
138
139;; #EXEC <command> <tmpfile> version 0.0.3 only
140;; <tmpfile> must not exists. It contains the output of <command>.
141;; It can be retrieved afterwards. Last line is
142;; ###RESULT: <returncode>
143
144;; This implementation is meant as proof of the concept, whether there
145;; is a better performance compared with the native ssh method. It
146;; looks like the file information retrieval is slower, especially the
147;; #LIST command. On the other hand, the file contents transmission
148;; seems to perform better than other inline methods, because there is
149;; no need for data encoding/decoding, and it supports the APPEND
150;; parameter of `write-region'. Transfer of binary data fails due to
151;; Emacs' process input/output handling.
152
153;;; Code:
154
155(eval-when-compile
156 ;; Pacify byte-compiler.
157 (require 'cl))
158
159(require 'tramp)
160
161;; Define FISH method ...
162;;;###tramp-autoload
163(defconst tramp-fish-method "fish"
164 "*Method to connect via FISH protocol.")
165
166;; ... and add it to the method list.
167;;;###tramp-autoload
168(add-to-list 'tramp-methods (cons tramp-fish-method nil))
169
170;; Add a default for `tramp-default-user-alist'. Default is the local user.
171(add-to-list 'tramp-default-user-alist
172 `(,tramp-fish-method nil ,(user-login-name)))
173
174;; Add completion function for FISH method.
175(tramp-set-completion-function
176 tramp-fish-method tramp-completion-function-alist-ssh)
177
178(defconst tramp-fish-continue-prompt-regexp "^### 100.*\n"
179 "FISH return code OK.")
180
181;; It cannot be a defconst, occasionally we bind it locally.
182(defvar tramp-fish-ok-prompt-regexp "^### 200\n"
183 "FISH return code OK.")
184
185(defconst tramp-fish-error-prompt-regexp "^### \\(4\\|5\\)[0-9]+.*\n"
186 "Regexp for possible error strings of FISH servers.
187Used instead of analyzing error codes of commands.")
188
189(defcustom tramp-fish-start-fish-server-command
190 (concat "stty intr \"\" quit \"\" erase \"\" kill \"\" eof \"\" eol \"\" eol2 \"\" swtch \"\" start \"\" stop \"\" susp \"\" rprnt \"\" werase \"\" lnext \"\" flush \"\"; "
191 "perl .fishsrv.pl "
192 "`grep 'ARGV\\[0\\]' .fishsrv.pl | "
193 "sed -e 's/^[^\"]*\"//' -e 's/\"[^\"]*$//'`; "
194 "exit")
195 "*Command to connect via FISH protocol."
196 :group 'tramp
197 :type 'string)
198
199;; New handlers should be added here.
200(defconst tramp-fish-file-name-handler-alist
201 '(
202 ;; `access-file' performed by default handler
203 (add-name-to-file . tramp-fish-handle-add-name-to-file)
204 ;; `byte-compiler-base-file-name' performed by default handler
205 (copy-file . tramp-fish-handle-copy-file)
206 (delete-directory . tramp-fish-handle-delete-directory)
207 (delete-file . tramp-fish-handle-delete-file)
208 ;; `diff-latest-backup-file' performed by default handler
209 (directory-file-name . tramp-handle-directory-file-name)
210 (directory-files . tramp-handle-directory-files)
211 (directory-files-and-attributes . tramp-fish-handle-directory-files-and-attributes)
212 ;; `dired-call-process' performed by default handler
213 ;; `dired-compress-file' performed by default handler
214 (dired-uncache . tramp-handle-dired-uncache)
215 (expand-file-name . tramp-fish-handle-expand-file-name)
216 ;; `file-accessible-directory-p' performed by default handler
217 (file-attributes . tramp-fish-handle-file-attributes)
218 (file-directory-p . tramp-fish-handle-file-directory-p)
219 (file-executable-p . tramp-fish-handle-file-executable-p)
220 (file-exists-p . tramp-fish-handle-file-exists-p)
221 (file-local-copy . tramp-fish-handle-file-local-copy)
222 (file-modes . tramp-handle-file-modes)
223 (file-name-all-completions . tramp-fish-handle-file-name-all-completions)
224 (file-name-as-directory . tramp-handle-file-name-as-directory)
225 (file-name-completion . tramp-handle-file-name-completion)
226 (file-name-directory . tramp-handle-file-name-directory)
227 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
228 ;; `file-name-sans-versions' performed by default handler
229 (file-newer-than-file-p . tramp-fish-handle-file-newer-than-file-p)
230 (file-ownership-preserved-p . ignore)
231 (file-readable-p . tramp-fish-handle-file-readable-p)
232 (file-regular-p . tramp-handle-file-regular-p)
233 (file-remote-p . tramp-handle-file-remote-p)
234 ;; `file-selinux-context' performed by default handler.
235 (file-symlink-p . tramp-handle-file-symlink-p)
236 ;; `file-truename' performed by default handler
237 (file-writable-p . tramp-fish-handle-file-writable-p)
238 (find-backup-file-name . tramp-handle-find-backup-file-name)
239 ;; `find-file-noselect' performed by default handler
240 ;; `get-file-buffer' performed by default handler
241 (insert-directory . tramp-fish-handle-insert-directory)
242 (insert-file-contents . tramp-fish-handle-insert-file-contents)
243 (load . tramp-handle-load)
244 (make-directory . tramp-fish-handle-make-directory)
245 (make-directory-internal . tramp-fish-handle-make-directory-internal)
246 (make-symbolic-link . tramp-fish-handle-make-symbolic-link)
247 (rename-file . tramp-fish-handle-rename-file)
248 (set-file-modes . tramp-fish-handle-set-file-modes)
249 ;; `set-file-selinux-context' performed by default handler.
250 (set-file-times . tramp-fish-handle-set-file-times)
251 (set-visited-file-modtime . ignore)
252 (shell-command . tramp-handle-shell-command)
253 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
254 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
255 (vc-registered . ignore)
256 (verify-visited-file-modtime . ignore)
257 (write-region . tramp-fish-handle-write-region)
258 (executable-find . tramp-fish-handle-executable-find)
259 (start-file-process . ignore)
260 (process-file . tramp-fish-handle-process-file)
261)
262 "Alist of handler functions for Tramp FISH method.
263Operations not mentioned here will be handled by the default Emacs primitives.")
264
265;;;###tramp-autoload
266(defsubst tramp-fish-file-name-p (filename)
267 "Check if it's a filename for FISH protocol."
268 (let ((v (tramp-dissect-file-name filename)))
269 (string= (tramp-file-name-method v) tramp-fish-method)))
270
271;;;###tramp-autoload
272(defun tramp-fish-file-name-handler (operation &rest args)
273 "Invoke the FISH related OPERATION.
274First arg specifies the OPERATION, second arg is a list of arguments to
275pass to the OPERATION."
276 (let ((fn (assoc operation tramp-fish-file-name-handler-alist)))
277 (if fn
278 (save-match-data (apply (cdr fn) args))
279 (tramp-run-real-handler operation args))))
280
281;;;###tramp-autoload
282(add-to-list 'tramp-foreign-file-name-handler-alist
283 (cons 'tramp-fish-file-name-p 'tramp-fish-file-name-handler))
284
285
286;; File name primitives
287
288(defun tramp-fish-handle-add-name-to-file
289 (filename newname &optional ok-if-already-exists)
290 "Like `add-name-to-file' for Tramp files."
291 (unless (tramp-equal-remote filename newname)
292 (with-parsed-tramp-file-name
293 (if (tramp-tramp-file-p filename) filename newname) nil
294 (tramp-error
295 v 'file-error
296 "add-name-to-file: %s"
297 "only implemented for same method, same user, same host")))
298 (with-parsed-tramp-file-name filename v1
299 (with-parsed-tramp-file-name newname v2
300 (when (and (not ok-if-already-exists)
301 (file-exists-p newname)
302 (not (numberp ok-if-already-exists))
303 (y-or-n-p
304 (format
305 "File %s already exists; make it a new name anyway? "
306 newname)))
307 (tramp-error
308 v2 'file-error
309 "add-name-to-file: file %s already exists" newname))
310 (tramp-flush-file-property v2 v2-localname)
311 (unless (tramp-fish-send-command-and-check
312 v1 (format "#LINK %s %s" v1-localname v2-localname))
313 (tramp-error
314 v1 'file-error "Error with add-name-to-file %s" newname)))))
315
316(defun tramp-fish-handle-copy-file
317 (filename newname &optional ok-if-already-exists keep-date
318 preserve-uid-gid preserve-selinux-context)
319 "Like `copy-file' for Tramp files."
320 (tramp-fish-do-copy-or-rename-file
321 'copy filename newname ok-if-already-exists keep-date preserve-uid-gid))
322
323(defun tramp-fish-handle-delete-directory (directory &optional recursive)
324 "Like `delete-directory' for Tramp files."
325 (when (file-exists-p directory)
326 (if recursive
327 (mapc
328 (lambda (file)
329 (if (file-directory-p file)
330 (tramp-compat-delete-directory file recursive)
331 (delete-file file)))
332 ;; We do not want to delete "." and "..".
333 (directory-files
334 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
335 (with-parsed-tramp-file-name
336 (directory-file-name (expand-file-name directory)) nil
337 (tramp-flush-directory-property v localname)
338 (tramp-fish-send-command-and-check v (format "#RMD %s" localname)))))
339
340(defun tramp-fish-handle-delete-file (filename &optional trash)
341 "Like `delete-file' for Tramp files."
342 (when (file-exists-p filename)
343 (with-parsed-tramp-file-name (expand-file-name filename) nil
344 (tramp-flush-file-property v localname)
345 (tramp-fish-send-command-and-check v (format "#DELE %s" localname)))))
346
347(defun tramp-fish-handle-directory-files-and-attributes
348 (directory &optional full match nosort id-format)
349 "Like `directory-files-and-attributes' for Tramp files."
350 (mapcar
351 (lambda (x)
352 (cons x
353 (tramp-compat-file-attributes
354 (if full x (expand-file-name x directory))
355 id-format)))
356 (directory-files directory full match nosort)))
357
358(defun tramp-fish-handle-expand-file-name (name &optional dir)
359 "Like `expand-file-name' for Tramp files."
360 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
361 (setq dir (or dir default-directory "/"))
362 ;; Unless NAME is absolute, concat DIR and NAME.
363 (unless (file-name-absolute-p name)
364 (setq name (concat (file-name-as-directory dir) name)))
365 ;; If NAME is not a Tramp file, run the real handler,
366 (if (or (tramp-completion-mode-p) (not (tramp-tramp-file-p name)))
367 (tramp-drop-volume-letter
368 (tramp-run-real-handler 'expand-file-name (list name nil)))
369 ;; Dissect NAME.
370 (with-parsed-tramp-file-name name nil
371 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
372 (setq localname (concat "~/" localname)))
373 ;; Tilde expansion if necessary.
374 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
375 (let ((uname (match-string 1 localname))
376 (fname (match-string 2 localname)))
377 ;; We cannot apply "~user/", because this is not supported
378 ;; by the FISH protocol.
379 (unless (string-equal uname "~")
380 (tramp-error
381 v 'file-error "Tilde expansion not supported for %s" name))
382 (setq uname
383 (with-connection-property v uname
384 (tramp-fish-send-command-and-check v "#PWD")
385 (with-current-buffer (tramp-get-buffer v)
386 (goto-char (point-min))
387 (buffer-substring (point) (tramp-compat-line-end-position)))))
388 (setq localname (concat uname fname))))
389 ;; There might be a double slash, for example when "~/"
390 ;; expands to "/". Remove this.
391 (while (string-match "//" localname)
392 (setq localname (replace-match "/" t t localname)))
393 ;; No tilde characters in file name, do normal
394 ;; expand-file-name (this does "/./" and "/../"). We bind
395 ;; `directory-sep-char' here for XEmacs on Windows, which
396 ;; would otherwise use backslash. `default-directory' is
397 ;; bound, because on Windows there would be problems with UNC
398 ;; shares or Cygwin mounts.
399 (let ((directory-sep-char ?/)
400 (default-directory (tramp-compat-temporary-file-directory)))
401 (tramp-make-tramp-file-name
402 method user host
403 (tramp-drop-volume-letter
404 (tramp-run-real-handler
405 'expand-file-name (list localname))))))))
406
407(defun tramp-fish-handle-file-attributes (filename &optional id-format)
408 "Like `file-attributes' for Tramp files."
409 (with-parsed-tramp-file-name (expand-file-name filename) nil
410 (with-file-property v localname (format "file-attributes-%s" id-format)
411 (cdr (car (tramp-fish-get-file-entries v localname nil))))))
412
413(defun tramp-fish-handle-file-directory-p (filename)
414 "Like `file-directory-p' for Tramp files."
415 (let ((attributes (file-attributes filename)))
416 (and attributes
417 (or (string-match "d" (nth 8 attributes))
418 (and (file-symlink-p filename)
419 (with-parsed-tramp-file-name filename nil
420 (file-directory-p
421 (tramp-make-tramp-file-name
422 method user host (nth 0 attributes))))))
423 t)))
424
425(defun tramp-fish-handle-file-exists-p (filename)
426 "Like `file-exists-p' for Tramp files."
427 (and (file-attributes filename) t))
428
429(defun tramp-fish-handle-file-executable-p (filename)
430 "Like `file-executable-p' for Tramp files."
431 (with-parsed-tramp-file-name (expand-file-name filename) nil
432 (with-file-property v localname "file-executable-p"
433 (when (file-exists-p filename)
434 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename))))
435 (home-directory
436 (tramp-make-tramp-file-name
437 method user host
438 (tramp-get-connection-property v "home-directory" nil))))
439 (or (and (char-equal (aref mode-chars 3) ?x)
440 (equal (nth 2 (file-attributes filename))
441 (nth 2 (file-attributes home-directory))))
442 (and (char-equal (aref mode-chars 6) ?x)
443 (equal (nth 3 (file-attributes filename))
444 (nth 3 (file-attributes home-directory))))
445 (char-equal (aref mode-chars 9) ?x)))))))
446
447(defun tramp-fish-handle-file-readable-p (filename)
448 "Like `file-readable-p' for Tramp files."
449 (with-parsed-tramp-file-name (expand-file-name filename) nil
450 (with-file-property v localname "file-readable-p"
451 (when (file-exists-p filename)
452 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename))))
453 (home-directory
454 (tramp-make-tramp-file-name
455 method user host
456 (tramp-get-connection-property v "home-directory" nil))))
457 (or (and (char-equal (aref mode-chars 1) ?r)
458 (equal (nth 2 (file-attributes filename))
459 (nth 2 (file-attributes home-directory))))
460 (and (char-equal (aref mode-chars 4) ?r)
461 (equal (nth 3 (file-attributes filename))
462 (nth 3 (file-attributes home-directory))))
463 (char-equal (aref mode-chars 7) ?r)))))))
464
465(defun tramp-fish-handle-file-writable-p (filename)
466 "Like `file-writable-p' for Tramp files."
467 (with-parsed-tramp-file-name (expand-file-name filename) nil
468 (with-file-property v localname "file-writable-p"
469 (if (not (file-exists-p filename))
470 ;; If file doesn't exist, check if directory is writable.
471 (and (file-directory-p (file-name-directory filename))
472 (file-writable-p (file-name-directory filename)))
473 ;; Existing files must be writable.
474 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename))))
475 (home-directory
476 (tramp-make-tramp-file-name
477 method user host
478 (tramp-get-connection-property v "home-directory" nil))))
479 (or (and (char-equal (aref mode-chars 2) ?w)
480 (equal (nth 2 (file-attributes filename))
481 (nth 2 (file-attributes home-directory))))
482 (and (char-equal (aref mode-chars 5) ?w)
483 (equal (nth 3 (file-attributes filename))
484 (nth 3 (file-attributes home-directory))))
485 (char-equal (aref mode-chars 8) ?w)))))))
486
487(defun tramp-fish-handle-file-local-copy (filename)
488 "Like `file-local-copy' for Tramp files."
489 (with-parsed-tramp-file-name (expand-file-name filename) nil
490 (unless (file-exists-p filename)
491 (tramp-error
492 v 'file-error
493 "Cannot make local copy of non-existing file `%s'" filename))
494 (let ((tmpfile (tramp-compat-make-temp-file filename)))
495 (with-progress-reporter
496 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
497 (when (tramp-fish-retrieve-data v)
498 ;; Save file
499 (with-current-buffer (tramp-get-buffer v)
500 (write-region (point-min) (point-max) tmpfile))
501 tmpfile)))))
502
503;; This function should return "foo/" for directories and "bar" for
504;; files.
505(defun tramp-fish-handle-file-name-all-completions (filename directory)
506 "Like `file-name-all-completions' for Tramp files."
507 (all-completions
508 filename
509 (with-parsed-tramp-file-name (expand-file-name directory) nil
510 (with-file-property v localname "file-name-all-completions"
511 (save-match-data
512 (let ((entries
513 (with-file-property v localname "file-entries"
514 (tramp-fish-get-file-entries v localname t))))
515 (mapcar
516 (lambda (x)
517 (list
518 (if (string-match "d" (nth 9 x))
519 (file-name-as-directory (nth 0 x))
520 (nth 0 x))))
521 entries)))))))
522
523(defun tramp-fish-handle-file-newer-than-file-p (file1 file2)
524 "Like `file-newer-than-file-p' for Tramp files."
525 (cond
526 ((not (file-exists-p file1)) nil)
527 ((not (file-exists-p file2)) t)
528 (t (tramp-time-less-p (nth 5 (file-attributes file2))
529 (nth 5 (file-attributes file1))))))
530
531(defun tramp-fish-handle-insert-directory
532 (filename switches &optional wildcard full-directory-p)
533 "Like `insert-directory' for Tramp files.
534WILDCARD and FULL-DIRECTORY-P are not handled."
535 (setq filename (expand-file-name filename))
536 (when (file-directory-p filename)
537 ;; This check is a little bit strange, but in `dired-add-entry'
538 ;; this function is called with a non-directory ...
539 (setq filename (file-name-as-directory filename)))
540
541 (with-parsed-tramp-file-name filename nil
542 (tramp-flush-file-property v localname)
543 (save-match-data
544 (let ((entries
545 (with-file-property v localname "file-entries"
546 (tramp-fish-get-file-entries v localname t))))
547
548 ;; Sort entries
549 (setq entries
550 (sort
551 entries
552 (lambda (x y)
553 (if (string-match "t" switches)
554 ;; Sort by date.
555 (tramp-time-less-p (nth 6 y) (nth 6 x))
556 ;; Sort by name.
557 (string-lessp (nth 0 x) (nth 0 y))))))
558
559 ;; Print entries.
560 (mapcar
561 (lambda (x)
562 (insert
563 (format
564 "%10s %3d %-8s %-8s %8s %s %s%s\n"
565 (nth 9 x) ; mode
566 1 ; hardlinks
567 (nth 3 x) ; uid
568 (nth 4 x) ; gid
569 (nth 8 x) ; size
570 (format-time-string
571 (if (tramp-time-less-p
572 (tramp-time-subtract (current-time) (nth 6 x))
573 tramp-half-a-year)
574 "%b %e %R"
575 "%b %e %Y")
576 (nth 6 x)) ; date
577 (nth 0 x) ; file name
578 (if (stringp (nth 1 x)) (format " -> %s" (nth 1 x)) "")))
579 (forward-line)
580 (beginning-of-line))
581 entries)))))
582
583(defun tramp-fish-handle-insert-file-contents
584 (filename &optional visit beg end replace)
585 "Like `insert-file-contents' for Tramp files."
586 (barf-if-buffer-read-only)
587 (when visit
588 (setq buffer-file-name (expand-file-name filename))
589 (set-visited-file-modtime)
590 (set-buffer-modified-p nil))
591
592 (with-parsed-tramp-file-name filename nil
593 (if (not (file-exists-p filename))
594 (tramp-error
595 v 'file-error "File %s not found on remote host" filename)
596
597 (let ((point (point))
598 size)
599 (with-progress-reporter v 3 (format "Fetching file %s" filename)
600 (when (tramp-fish-retrieve-data v)
601 ;; Insert file
602 (insert
603 (with-current-buffer (tramp-get-buffer v)
604 (let ((beg (or beg (point-min)))
605 (end (min (or end (point-max)) (point-max))))
606 (setq size (- end beg))
607 (buffer-substring beg end))))
608 (goto-char point)))
609
610 (list (expand-file-name filename) size)))))
611
612(defun tramp-fish-handle-make-directory (dir &optional parents)
613 "Like `make-directory' for Tramp files."
614 (setq dir (directory-file-name (expand-file-name dir)))
615 (unless (file-name-absolute-p dir)
616 (setq dir (expand-file-name dir default-directory)))
617 (with-parsed-tramp-file-name dir nil
618 (save-match-data
619 (let ((ldir (file-name-directory dir)))
620 ;; Make missing directory parts
621 (when (and parents (not (file-directory-p ldir)))
622 (make-directory ldir parents))
623 ;; Just do it
624 (when (file-directory-p ldir)
625 (make-directory-internal dir))
626 (unless (file-directory-p dir)
627 (tramp-error v 'file-error "Couldn't make directory %s" dir))))))
628
629(defun tramp-fish-handle-make-directory-internal (directory)
630 "Like `make-directory-internal' for Tramp files."
631 (setq directory (directory-file-name (expand-file-name directory)))
632 (unless (file-name-absolute-p directory)
633 (setq directory (expand-file-name directory default-directory)))
634 (when (file-directory-p (file-name-directory directory))
635 (with-parsed-tramp-file-name directory nil
636 (save-match-data
637 (unless
638 (tramp-fish-send-command-and-check v (format "#MKD %s" localname))
639 (tramp-error
640 v 'file-error "Couldn't make directory %s" directory))))))
641
642(defun tramp-fish-handle-make-symbolic-link
643 (filename linkname &optional ok-if-already-exists)
644 "Like `make-symbolic-link' for Tramp files.
645If LINKNAME is a non-Tramp file, it is used verbatim as the target of
646the symlink. If LINKNAME is a Tramp file, only the localname component is
647used as the target of the symlink.
648
649If LINKNAME is a Tramp file and the localname component is relative, then
650it is expanded first, before the localname component is taken. Note that
651this can give surprising results if the user/host for the source and
652target of the symlink differ."
653 (with-parsed-tramp-file-name linkname nil
654 ;; Do the 'confirm if exists' thing.
655 (when (file-exists-p linkname)
656 ;; What to do?
657 (if (or (null ok-if-already-exists) ; not allowed to exist
658 (and (numberp ok-if-already-exists)
659 (not (yes-or-no-p
660 (format
661 "File %s already exists; make it a link anyway? "
662 localname)))))
663 (tramp-error
664 v 'file-already-exists "File %s already exists" localname)
665 (delete-file linkname)))
666
667 ;; If FILENAME is a Tramp name, use just the localname component.
668 (when (tramp-tramp-file-p filename)
669 (setq filename (tramp-file-name-localname
670 (tramp-dissect-file-name (expand-file-name filename)))))
671
672 ;; Right, they are on the same host, regardless of user, method, etc.
673 ;; We now make the link on the remote machine. This will occur as the user
674 ;; that FILENAME belongs to.
675 (unless
676 (tramp-fish-send-command-and-check
677 v (format "#SYMLINK %s %s" filename localname))
678 (tramp-error v 'file-error "Error creating symbolic link %s" linkname))))
679
680(defun tramp-fish-handle-rename-file
681 (filename newname &optional ok-if-already-exists)
682 "Like `rename-file' for Tramp files."
683 (tramp-fish-do-copy-or-rename-file
684 'rename filename newname ok-if-already-exists t))
685
686(defun tramp-fish-handle-set-file-modes (filename mode)
687 "Like `set-file-modes' for Tramp files."
688 (with-parsed-tramp-file-name filename nil
689 (tramp-flush-file-property v localname)
690 (unless (tramp-fish-send-command-and-check
691 v (format "#CHMOD %s %s"
692 (tramp-compat-decimal-to-octal mode)
693 (tramp-shell-quote-argument localname)))
694 (tramp-error
695 v 'file-error "Error while changing file's mode %s" filename))))
696
697(defun tramp-fish-handle-set-file-times (filename &optional time)
698 "Like `set-file-times' for Tramp files."
699 (with-parsed-tramp-file-name filename nil
700 (let ((time (if (or (null time) (equal time '(0 0))) (current-time) time)))
701 (zerop (process-file
702 "touch" nil nil nil "-t"
703 (format-time-string "%Y%m%d%H%M.%S" time)
704 (tramp-shell-quote-argument localname))))))
705
706(defun tramp-fish-handle-write-region
707 (start end filename &optional append visit lockname confirm)
708 "Like `write-region' for Tramp files."
709 (setq filename (expand-file-name filename))
710 (with-parsed-tramp-file-name filename nil
711 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
712 (when (and (not (featurep 'xemacs))
713 confirm (file-exists-p filename))
714 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
715 filename))
716 (tramp-error v 'file-error "File not overwritten")))
717
718 (tramp-flush-file-property v localname)
719
720 ;; Send command
721 (let ((tramp-fish-ok-prompt-regexp
722 (concat
723 tramp-fish-ok-prompt-regexp "\\|"
724 tramp-fish-continue-prompt-regexp)))
725 (tramp-fish-send-command
726 v (format "%s %d %s\n### 100"
727 (if append "#APPEND" "#STOR") (- end start) localname)))
728
729 ;; Send data, if there are any.
730 (when (> end start)
731 (tramp-fish-send-command v (buffer-substring-no-properties start end)))
732
733 (when (eq visit t)
734 (set-visited-file-modtime))))
735
736(defun tramp-fish-handle-executable-find (command)
737 "Like `executable-find' for Tramp files."
738 (with-temp-buffer
739 (if (zerop (process-file "which" nil t nil command))
740 (progn
741 (goto-char (point-min))
742 (buffer-substring (point-min) (tramp-compat-line-end-position))))))
743
744(defun tramp-fish-handle-process-file
745 (program &optional infile destination display &rest args)
746 "Like `process-file' for Tramp files."
747 ;; The implementation is not complete yet.
748 (when (and (numberp destination) (zerop destination))
749 (error "Implementation does not handle immediate return"))
750
751 (with-parsed-tramp-file-name default-directory nil
752 (let (command input tmpinput output tmpoutput stderr tmpstderr
753 outbuf tmpfile ret)
754 ;; Compute command.
755 (setq command (mapconcat 'tramp-shell-quote-argument
756 (cons program args) " "))
757 ;; Determine input.
758 (if (null infile)
759 (setq input "/dev/null")
760 (setq infile (expand-file-name infile))
761 (if (tramp-equal-remote default-directory infile)
762 ;; INFILE is on the same remote host.
763 (setq input (with-parsed-tramp-file-name infile nil localname))
764 ;; INFILE must be copied to remote host.
765 (setq input (tramp-make-tramp-temp-file v)
766 tmpinput (tramp-make-tramp-file-name method user host input))
767 (copy-file infile tmpinput t)))
768 (when input (setq command (format "%s <%s" command input)))
769
770 ;; Determine output.
771 (setq output (tramp-make-tramp-temp-file v)
772 tmpoutput (tramp-make-tramp-file-name method user host output))
773 (cond
774 ;; Just a buffer
775 ((bufferp destination)
776 (setq outbuf destination))
777 ;; A buffer name
778 ((stringp destination)
779 (setq outbuf (get-buffer-create destination)))
780 ;; (REAL-DESTINATION ERROR-DESTINATION)
781 ((consp destination)
782 ;; output
783 (cond
784 ((bufferp (car destination))
785 (setq outbuf (car destination)))
786 ((stringp (car destination))
787 (setq outbuf (get-buffer-create (car destination)))))
788 ;; stderr
789 (cond
790 ((stringp (cadr destination))
791 (setcar (cdr destination) (expand-file-name (cadr destination)))
792 (if (tramp-equal-remote default-directory (cadr destination))
793 ;; stderr is on the same remote host.
794 (setq stderr (with-parsed-tramp-file-name
795 (cadr destination) nil localname))
796 ;; stderr must be copied to remote host. The temporary
797 ;; file must be deleted after execution.
798 (setq stderr (tramp-make-tramp-temp-file v)
799 tmpstderr (tramp-make-tramp-file-name
800 method user host stderr))))
801 ;; stderr to be discarded
802 ((null (cadr destination))
803 (setq stderr "/dev/null"))))
804 ;; 't
805 (destination
806 (setq outbuf (current-buffer))))
807 (when stderr (setq command (format "%s 2>%s" command stderr)))
808
809 ;; Goto working directory.
810 (unless
811 (tramp-fish-send-command-and-check
812 v (format "#CWD %s" (tramp-shell-quote-argument localname)))
813 (tramp-error v 'file-error "No such directory: %s" default-directory))
814 ;; Send the command. It might not return in time, so we protect it.
815 (condition-case nil
816 (unwind-protect
817 (unless (tramp-fish-send-command-and-check
818 v (format
819 "#EXEC %s %s"
820 (tramp-shell-quote-argument command) output))
821 (error nil))
822 ;; Check return code.
823 (setq tmpfile
824 (file-local-copy
825 (tramp-make-tramp-file-name method user host output)))
826 (with-temp-buffer
827 (insert-file-contents tmpfile)
828 (goto-char (point-max))
829 (forward-line -1)
830 (looking-at "^###RESULT: \\([0-9]+\\)")
831 (setq ret (string-to-number (match-string 1)))
832 (delete-region (point) (point-max))
833 (write-region (point-min) (point-max) tmpfile))
834 ;; We should show the output anyway.
835 (when outbuf
836 (with-current-buffer outbuf (insert-file-contents tmpfile))
837 (when display (display-buffer outbuf))))
838 ;; When the user did interrupt, we should do it also.
839 (error (setq ret 1)))
840
841 ;; Provide error file.
842 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
843 ;; Cleanup.
844 (when tmpinput (delete-file tmpinput))
845 (when tmpoutput (delete-file tmpoutput))
846 ;; Return exit status.
847 ret)))
848
849
850;; Internal file name functions
851
852(defun tramp-fish-do-copy-or-rename-file
853 (op filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
854 "Copy or rename a remote file.
855OP must be `copy' or `rename' and indicates the operation to
856perform. FILENAME specifies the file to copy or rename, NEWNAME
857is the name of the new file (for copy) or the new name of the
858file (for rename). OK-IF-ALREADY-EXISTS means don't barf if
859NEWNAME exists already. KEEP-DATE means to make sure that
860NEWNAME has the same timestamp as FILENAME.
861
862This function is invoked by `tramp-fish-handle-copy-file' and
863`tramp-fish-handle-rename-file'. It is an error if OP is neither
864of `copy' and `rename'. FILENAME and NEWNAME must be absolute
865file names."
866 (unless (memq op '(copy rename))
867 (error "Unknown operation `%s', must be `copy' or `rename'" op))
868 (let ((t1 (tramp-tramp-file-p filename))
869 (t2 (tramp-tramp-file-p newname)))
870
871 (unless ok-if-already-exists
872 (when (and t2 (file-exists-p newname))
873 (with-parsed-tramp-file-name newname nil
874 (tramp-error
875 v 'file-already-exists "File %s already exists" newname))))
876
877 (prog1
878 (cond
879 ;; Both are Tramp files.
880 ((and t1 t2)
881 (cond
882 ;; Shortcut: if method, host, user are the same for both
883 ;; files, we invoke `cp' or `mv' on the remote host
884 ;; directly.
885 ((tramp-equal-remote filename newname)
886 (tramp-fish-do-copy-or-rename-file-directly
887 op filename newname keep-date preserve-uid-gid))
888 ;; No shortcut was possible. So we copy the
889 ;; file first. If the operation was `rename', we go
890 ;; back and delete the original file (if the copy was
891 ;; successful). The approach is simple-minded: we
892 ;; create a new buffer, insert the contents of the
893 ;; source file into it, then write out the buffer to
894 ;; the target file. The advantage is that it doesn't
895 ;; matter which filename handlers are used for the
896 ;; source and target file.
897 (t
898 (tramp-do-copy-or-rename-file-via-buffer
899 op filename newname keep-date))))
900
901 ;; One file is a Tramp file, the other one is local.
902 ((or t1 t2)
903 ;; Use the generic method via a Tramp buffer.
904 (tramp-do-copy-or-rename-file-via-buffer
905 op filename newname keep-date))
906
907 (t
908 ;; One of them must be a Tramp file.
909 (error "Tramp implementation says this cannot happen")))
910 ;; When newname did exist, we have wrong cached values.
911 (when t2
912 (with-parsed-tramp-file-name newname nil
913 (tramp-flush-file-property v localname)
914 (tramp-flush-file-property v (file-name-directory localname)))))))
915
916(defun tramp-fish-do-copy-or-rename-file-directly
917 (op filename newname keep-date preserve-uid-gid)
918 "Invokes `COPY' or `RENAME' on the remote system.
919OP must be one of `copy' or `rename', indicating `cp' or `mv',
920respectively. VEC specifies the connection. LOCALNAME1 and
921LOCALNAME2 specify the two arguments of `cp' or `mv'. If
922KEEP-DATE is non-nil, preserve the time stamp when copying.
923PRESERVE-UID-GID is completely ignored."
924 (with-parsed-tramp-file-name filename v1
925 (with-parsed-tramp-file-name newname v2
926 (tramp-fish-send-command
927 v1
928 (format "%s %s %s"
929 (if (eq op 'copy) "#COPY" "#RENAME")
930 (tramp-shell-quote-argument v1-localname)
931 (tramp-shell-quote-argument v2-localname)))))
932 ;; KEEP-DATE handling.
933 (when (and keep-date (functionp 'set-file-times))
934 (set-file-times newname (nth 5 (file-attributes filename))))
935 ;; Set the mode.
936 (set-file-modes newname (tramp-default-file-modes filename)))
937
938(defun tramp-fish-get-file-entries (vec localname list)
939 "Read entries returned by FISH server.
940When LIST is true, a #LIST command will be sent, including all entries
941of a directory. Otherwise, #STAT is sent for just one entry.
942Result is a list of (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
943SIZE MODE WEIRD INODE DEVICE)."
944 (block nil
945 (with-current-buffer (tramp-get-buffer vec)
946 ;; #LIST does not work properly with trailing "/", at least in
947 ;; .fishsrv.pl.
948 (when (string-match "/$" localname)
949 (setq localname (concat localname ".")))
950
951 (let ((command (format "%s %s" (if list "#LIST" "#STAT") localname))
952 buffer-read-only num res)
953
954 ;; Send command
955 (tramp-fish-send-command vec command)
956
957 ;; Read number of entries
958 (goto-char (point-min))
959 (condition-case nil
960 (unless (integerp (setq num (read (current-buffer)))) (error nil))
961 (error (return nil)))
962 (forward-line)
963 (delete-region (point-min) (point))
964
965 ;; Read return code
966 (goto-char (point-min))
967 (condition-case nil
968 (unless (looking-at tramp-fish-continue-prompt-regexp) (error nil))
969 (error (return nil)))
970 (forward-line)
971 (delete-region (point-min) (point))
972
973 ;; Loop the listing
974 (dotimes (i num)
975 (let ((item (tramp-fish-read-file-entry)))
976 ;; Add inode and device.
977 (add-to-list
978 'res (append item
979 (list (tramp-get-inode vec)
980 (tramp-get-device vec))))))
981
982 ;; Read return code
983 (goto-char (point-min))
984 (condition-case nil
985 (unless (looking-at tramp-fish-ok-prompt-regexp) (error nil))
986 (error (tramp-error
987 vec 'file-error
988 "`%s' does not return a valid Lisp expression: `%s'"
989 command (buffer-string))))
990 (forward-line)
991 (delete-region (point-min) (point))
992
993 res))))
994
995(defun tramp-fish-read-file-entry ()
996 "Parse entry in output buffer.
997Result is the list (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
998SIZE MODE WEIRD)."
999 ;; We are called from `tramp-fish-get-file-entries', which sets the
1000 ;; current buffer.
1001 (let (buffer-read-only localname link uid gid mtime size mode)
1002 (block nil
1003 (while t
1004 (cond
1005 ;; P<unix permissions> <owner>.<group>
1006 ((looking-at "^P\\(.+\\)\\s-\\(.+\\)\\.\\(.+\\)$")
1007 (setq mode (match-string 1))
1008 (setq uid (match-string 2))
1009 (setq gid (match-string 3))
1010 (when (string-match "^d" mode) (setq link t)))
1011 ;; S<size>
1012 ((looking-at "^S\\([0-9]+\\)$")
1013 (setq size (string-to-number (match-string 1))))
1014 ;; D<year> <month> <day> <hour> <minute> <second>[.1234]
1015 ((looking-at
1016 "^D\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\(\\S-+\\)$")
1017 (setq mtime
1018 (encode-time
1019 (string-to-number (match-string 6))
1020 (string-to-number (match-string 5))
1021 (string-to-number (match-string 4))
1022 (string-to-number (match-string 3))
1023 (string-to-number (match-string 2))
1024 (string-to-number (match-string 1)))))
1025 ;; d<3-letters month name> <day> <year or HH:MM>
1026 ((looking-at "^d") nil)
1027 ;; E<major-of-device>,<minor>
1028 ((looking-at "^E") nil)
1029 ;; :<filename>
1030 ((looking-at "^:\\(.+\\)$")
1031 (setq localname (match-string 1)))
1032 ;; L<filename symlink points to>
1033 ((looking-at "^L\\(.+\\)$")
1034 (setq link (match-string 1)))
1035 ;; M<mimetype>
1036 ((looking-at "^M\\(.+\\)$") nil)
1037 ;; last line
1038 ((looking-at "^$")
1039 (return)))
1040 ;; Delete line.
1041 (forward-line)
1042 (delete-region (point-min) (point))))
1043
1044 ;; Delete trailing empty line.
1045 (forward-line)
1046 (delete-region (point-min) (point))
1047
1048 ;; Return entry in `file-attributes' format.
1049 (list localname link -1 uid gid '(0 0) mtime '(0 0) size mode nil)))
1050
1051(defun tramp-fish-retrieve-data (vec)
1052 "Reads remote data for FISH protocol.
1053The data are left in the connection buffer of VEC for further processing.
1054Returns the size of the data."
1055 (block nil
1056 (with-current-buffer (tramp-get-buffer vec)
1057 ;; The retrieved data might be in binary format, without
1058 ;; trailing newline. Therefore, the OK prompt might not start
1059 ;; at the beginning of a line.
1060 (let ((tramp-fish-ok-prompt-regexp "### 200\n")
1061 size)
1062
1063 ;; Send command
1064 (tramp-fish-send-command
1065 vec (format "#RETR %s" (tramp-file-name-localname vec)))
1066
1067 ;; Read filesize
1068 (goto-char (point-min))
1069 (condition-case nil
1070 (unless (integerp (setq size (read (current-buffer)))) (error nil))
1071 (error (return nil)))
1072 (forward-line)
1073 (delete-region (point-min) (point))
1074
1075 ;; Read return code
1076 (goto-char (point-min))
1077 (condition-case nil
1078 (unless (looking-at tramp-fish-continue-prompt-regexp) (error nil))
1079 (error (return nil)))
1080 (forward-line)
1081 (delete-region (point-min) (point))
1082
1083 ;; The received data might contain the OK prompt already, so
1084 ;; there might be outstanding data.
1085 (while (/= (+ size (length tramp-fish-ok-prompt-regexp))
1086 (- (point-max) (point-min)))
1087 (tramp-wait-for-regexp
1088 (tramp-get-connection-process vec) nil
1089 (concat tramp-fish-ok-prompt-regexp "$")))
1090
1091 ;; Read return code
1092 (goto-char (+ (point-min) size))
1093 (condition-case nil
1094 (unless (looking-at tramp-fish-ok-prompt-regexp) (error nil))
1095 (error (return nil)))
1096 (delete-region (+ (point-min) size) (point-max))
1097 size))))
1098
1099
1100;; Connection functions
1101
1102(defun tramp-fish-maybe-open-connection (vec)
1103 "Maybe open a connection VEC.
1104Does not do anything if a connection is already open, but re-opens the
1105connection if a previous connection has died for some reason."
1106 (let ((process-connection-type tramp-process-connection-type)
1107 (p (get-buffer-process (tramp-get-buffer vec))))
1108
1109 ;; New connection must be opened.
1110 (unless (and p (processp p) (memq (process-status p) '(run open)))
1111
1112 ;; Set variables for computing the prompt for reading password.
1113 (setq tramp-current-method (tramp-file-name-method vec)
1114 tramp-current-user (tramp-file-name-user vec)
1115 tramp-current-host (tramp-file-name-host vec))
1116
1117 ;; Start new process.
1118 (when (and p (processp p))
1119 (delete-process p))
1120 (setenv "TERM" tramp-terminal-type)
1121 (setenv "PS1" tramp-initial-end-of-output)
1122 (with-progress-reporter
1123 vec 3
1124 (format "Opening connection for %s@%s using %s"
1125 tramp-current-user tramp-current-host tramp-current-method)
1126
1127 (let* ((process-connection-type tramp-process-connection-type)
1128 (inhibit-eol-conversion nil)
1129 (coding-system-for-read 'binary)
1130 (coding-system-for-write 'binary)
1131 ;; This must be done in order to avoid our file name handler.
1132 (p (let ((default-directory
1133 (tramp-compat-temporary-file-directory)))
1134 (start-process
1135 (or (tramp-get-connection-property vec "process-name" nil)
1136 (tramp-buffer-name vec))
1137 (tramp-get-connection-buffer vec)
1138 "ssh" "-l"
1139 (tramp-file-name-user vec)
1140 (tramp-file-name-host vec)))))
1141 (tramp-message
1142 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1143
1144 ;; Check whether process is alive.
1145 (tramp-set-process-query-on-exit-flag p nil)
1146
1147 (tramp-process-actions p vec tramp-actions-before-shell 60)
1148 (tramp-fish-send-command vec tramp-fish-start-fish-server-command)
1149 (tramp-message
1150 vec 3
1151 "Found remote shell prompt on `%s'" (tramp-file-name-host vec)))))))
1152
1153(defun tramp-fish-send-command (vec command)
1154 "Send the COMMAND to connection VEC."
1155 (tramp-fish-maybe-open-connection vec)
1156 (tramp-message vec 6 "%s" command)
1157 (tramp-send-string vec command)
1158 (tramp-wait-for-regexp
1159 (tramp-get-connection-process vec) nil
1160 (concat tramp-fish-ok-prompt-regexp "\\|" tramp-fish-error-prompt-regexp)))
1161
1162(defun tramp-fish-send-command-and-check (vec command)
1163 "Send the COMMAND to connection VEC.
1164Returns nil if there has been an error message."
1165
1166 ;; Send command.
1167 (tramp-fish-send-command vec command)
1168
1169 ;; Read return code.
1170 (with-current-buffer (tramp-get-buffer vec)
1171 (goto-char (point-min))
1172 (looking-at tramp-fish-ok-prompt-regexp)))
1173
1174(add-hook 'tramp-unload-hook
1175 (lambda ()
1176 (unload-feature 'tramp-fish 'force)))
1177
1178(provide 'tramp-fish)
1179;
1180;;;; TODO:
1181;
1182;; * Evaluate the MIME information with #LIST or #STAT.
1183;
1184
1185;; arch-tag: a66df7df-5f29-42a7-a921-643ceb29db49
1186;;;; tramp-fish.el ends here
diff --git a/lisp/net/tramp-ftp.el b/lisp/net/tramp-ftp.el
index 799b974bd04..7f8b7454caf 100644
--- a/lisp/net/tramp-ftp.el
+++ b/lisp/net/tramp-ftp.el
@@ -219,11 +219,7 @@ pass to the OPERATION."
219 219
220;;; TODO: 220;;; TODO:
221 221
222;; * In case of "/ftp:host:file" this works only for functions which 222;; * There are no backup files on FTP hosts.
223;; are defined in `tramp-file-name-handler-alist'. Call has to be
224;; pretended in `tramp-file-name-handler' otherwise.
225;; Furthermore, there are no backup files on FTP hosts.
226;; Worth further investigations.
227 223
228;; arch-tag: 759fb338-5c63-4b99-bd36-b4d59db91cff 224;; arch-tag: 759fb338-5c63-4b99-bd36-b4d59db91cff
229;;; tramp-ftp.el ends here 225;;; tramp-ftp.el ends here
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index 6e07ec19021..adcda5ebb80 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -103,6 +103,11 @@
103 (require 'custom)) 103 (require 'custom))
104 104
105(require 'tramp) 105(require 'tramp)
106
107;; We call several `tramp-handle-*' functions directly. So we must
108;; reqire that package as well.
109(require 'tramp-sh)
110
106(require 'dbus) 111(require 'dbus)
107(require 'url-parse) 112(require 'url-parse)
108(require 'url-util) 113(require 'url-util)
diff --git a/lisp/net/tramp-gw.el b/lisp/net/tramp-gw.el
index 63dfd105f1c..a550d46b9b5 100644
--- a/lisp/net/tramp-gw.el
+++ b/lisp/net/tramp-gw.el
@@ -243,10 +243,9 @@ authentication is requested from proxy server, provide it."
243 ;; Trap errors to be traced in the right trace buffer. Often, 243 ;; Trap errors to be traced in the right trace buffer. Often,
244 ;; proxies have a timeout of 60". We wait 65" in order to 244 ;; proxies have a timeout of 60". We wait 65" in order to
245 ;; receive an answer this case. 245 ;; receive an answer this case.
246 (condition-case nil 246 (ignore-errors
247 (let (tramp-verbose) 247 (let (tramp-verbose)
248 (tramp-wait-for-regexp proc 65 "\r?\n\r?\n")) 248 (tramp-wait-for-regexp proc 65 "\r?\n\r?\n")))
249 (error nil))
250 ;; Check return code. 249 ;; Check return code.
251 (goto-char (point-min)) 250 (goto-char (point-min))
252 (narrow-to-region 251 (narrow-to-region
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
new file mode 100644
index 00000000000..6d51da3bf97
--- /dev/null
+++ b/lisp/net/tramp-sh.el
@@ -0,0 +1,5506 @@
1;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
2
3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6;; (copyright statements below in code to be updated with the above notice)
7
8;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9;; Michael Albinus <michael.albinus@gmx.de>
10;; Keywords: comm, processes
11;; Package: tramp
12
13;; This file is part of GNU Emacs.
14
15;; GNU Emacs is free software: you can redistribute it and/or modify
16;; it under the terms of the GNU General Public License as published by
17;; the Free Software Foundation, either version 3 of the License, or
18;; (at your option) any later version.
19
20;; GNU Emacs is distributed in the hope that it will be useful,
21;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23;; GNU General Public License for more details.
24
25;; You should have received a copy of the GNU General Public License
26;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28;;; Code:
29
30(require 'tramp)
31(require 'shell)
32
33;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
34;; not sure at all that this is the right way to do it, but let's hope
35;; it works for now, and wait for a guru to point out the Right Way to
36;; achieve this.
37;;(eval-when-compile
38;; (unless (fboundp 'dired-insert-set-properties)
39;; (fset 'dired-insert-set-properties 'ignore)))
40;; Gerd suggests this:
41(eval-when-compile (require 'dired))
42;; Note that dired is required at run-time, too, when it is needed.
43;; It is only needed on XEmacs for the function
44;; `dired-insert-set-properties'.
45
46(defcustom tramp-inline-compress-start-size 4096
47 "*The minimum size of compressing where inline transfer.
48When inline transfer, compress transfered data of file
49whose size is this value or above (up to `tramp-copy-size-limit').
50If it is nil, no compression at all will be applied."
51 :group 'tramp
52 :type '(choice (const nil) integer))
53
54(defcustom tramp-copy-size-limit 10240
55 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
56If it is nil, inline out-of-the-band copy will be used without a check."
57 :group 'tramp
58 :type '(choice (const nil) integer))
59
60;;;###tramp-autoload
61(defcustom tramp-terminal-type "dumb"
62 "*Value of TERM environment variable for logging in to remote host.
63Because Tramp wants to parse the output of the remote shell, it is easily
64confused by ANSI color escape sequences and suchlike. Often, shell init
65files conditionalize this setup based on the TERM environment variable."
66 :group 'tramp
67 :type 'string)
68
69;; ksh on OpenBSD 4.5 requires, that PS1 contains a `#' character for
70;; root users. It uses the `$' character for other users. In order
71;; to guarantee a proper prompt, we use "#$" for the prompt.
72
73(defvar tramp-end-of-output
74 (format
75 "///%s#$"
76 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
77 "String used to recognize end of output.
78The '$' character at the end is quoted; the string cannot be
79detected as prompt when being sent on echoing hosts, therefore.")
80
81;;;###tramp-autoload
82(defconst tramp-initial-end-of-output "#$ "
83 "Prompt when establishing a connection.")
84
85;; Initialize `tramp-methods' with the supported methods.
86;;;###tramp-autoload
87(add-to-list 'tramp-methods
88 '("rcp"
89 (tramp-login-program "rsh")
90 (tramp-login-args (("%h") ("-l" "%u")))
91 (tramp-remote-sh "/bin/sh")
92 (tramp-copy-program "rcp")
93 (tramp-copy-args (("-p" "%k") ("-r")))
94 (tramp-copy-keep-date t)
95 (tramp-copy-recursive t)))
96;;;###tramp-autoload
97(add-to-list 'tramp-methods
98 '("remcp"
99 (tramp-login-program "remsh")
100 (tramp-login-args (("%h") ("-l" "%u")))
101 (tramp-remote-sh "/bin/sh")
102 (tramp-copy-program "rcp")
103 (tramp-copy-args (("-p" "%k")))
104 (tramp-copy-keep-date t)))
105;;;###tramp-autoload
106(add-to-list
107 'tramp-methods
108 '("scp" (tramp-login-program "ssh")
109 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
110 (tramp-async-args (("-q")))
111 (tramp-remote-sh "/bin/sh")
112 (tramp-copy-program "scp")
113 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
114 (tramp-copy-keep-date t)
115 (tramp-copy-recursive t)
116 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
117 ("-o" "UserKnownHostsFile=/dev/null")
118 ("-o" "StrictHostKeyChecking=no")))
119 (tramp-default-port 22)))
120;;;###tramp-autoload
121(add-to-list 'tramp-methods
122 '("scp1"
123 (tramp-login-program "ssh")
124 (tramp-login-args (("-l" "%u") ("-p" "%p")
125 ("-1") ("-e" "none") ("%h")))
126 (tramp-async-args (("-q")))
127 (tramp-remote-sh "/bin/sh")
128 (tramp-copy-program "scp")
129 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
130 (tramp-copy-keep-date t)
131 (tramp-copy-recursive t)
132 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
133 ("-o" "UserKnownHostsFile=/dev/null")
134 ("-o" "StrictHostKeyChecking=no")))
135 (tramp-default-port 22)))
136;;;###tramp-autoload
137(add-to-list 'tramp-methods
138 '("scp2"
139 (tramp-login-program "ssh")
140 (tramp-login-args (("-l" "%u") ("-p" "%p")
141 ("-2") ("-e" "none") ("%h")))
142 (tramp-async-args (("-q")))
143 (tramp-remote-sh "/bin/sh")
144 (tramp-copy-program "scp")
145 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
146 (tramp-copy-keep-date t)
147 (tramp-copy-recursive t)
148 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
149 ("-o" "UserKnownHostsFile=/dev/null")
150 ("-o" "StrictHostKeyChecking=no")))
151 (tramp-default-port 22)))
152;;;###tramp-autoload
153(add-to-list 'tramp-methods
154 '("scpc"
155 (tramp-login-program "ssh")
156 (tramp-login-args (("-l" "%u") ("-p" "%p")
157 ("-o" "ControlPath=%t.%%r@%%h:%%p")
158 ("-o" "ControlMaster=yes")
159 ("-e" "none") ("%h")))
160 (tramp-async-args (("-q")))
161 (tramp-remote-sh "/bin/sh")
162 (tramp-copy-program "scp")
163 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
164 ("-o" "ControlPath=%t.%%r@%%h:%%p")
165 ("-o" "ControlMaster=auto")))
166 (tramp-copy-keep-date t)
167 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
168 ("-o" "UserKnownHostsFile=/dev/null")
169 ("-o" "StrictHostKeyChecking=no")))
170 (tramp-default-port 22)))
171;;;###tramp-autoload
172(add-to-list 'tramp-methods
173 '("scpx"
174 (tramp-login-program "ssh")
175 (tramp-login-args (("-l" "%u") ("-p" "%p")
176 ("-e" "none") ("-t" "-t")
177 ("%h") ("/bin/sh")))
178 (tramp-async-args (("-q")))
179 (tramp-remote-sh "/bin/sh")
180 (tramp-copy-program "scp")
181 (tramp-copy-args (("-p" "%k")))
182 (tramp-copy-keep-date t)
183 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
184 ("-o" "UserKnownHostsFile=/dev/null")
185 ("-o" "StrictHostKeyChecking=no")))
186 (tramp-default-port 22)))
187;;;###tramp-autoload
188(add-to-list 'tramp-methods
189 '("sftp"
190 (tramp-login-program "ssh")
191 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
192 (tramp-async-args (("-q")))
193 (tramp-remote-sh "/bin/sh")
194 (tramp-copy-program "sftp")))
195;;;###tramp-autoload
196(add-to-list 'tramp-methods
197 '("rsync"
198 (tramp-login-program "ssh")
199 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
200 (tramp-async-args (("-q")))
201 (tramp-remote-sh "/bin/sh")
202 (tramp-copy-program "rsync")
203 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
204 (tramp-copy-keep-date t)
205 (tramp-copy-keep-tmpfile t)
206 (tramp-copy-recursive t)))
207;;;###tramp-autoload
208(add-to-list 'tramp-methods
209 `("rsyncc"
210 (tramp-login-program "ssh")
211 (tramp-login-args (("-l" "%u") ("-p" "%p")
212 ("-o" "ControlPath=%t.%%r@%%h:%%p")
213 ("-o" "ControlMaster=yes")
214 ("-e" "none") ("%h")))
215 (tramp-async-args (("-q")))
216 (tramp-remote-sh "/bin/sh")
217 (tramp-copy-program "rsync")
218 (tramp-copy-args (("-t" "%k") ("-r")))
219 (tramp-copy-env (("RSYNC_RSH")
220 (,(concat
221 "ssh"
222 " -o ControlPath=%t.%%r@%%h:%%p"
223 " -o ControlMaster=auto"))))
224 (tramp-copy-keep-date t)
225 (tramp-copy-keep-tmpfile t)
226 (tramp-copy-recursive t)))
227;;;###tramp-autoload
228(add-to-list 'tramp-methods
229 '("rsh"
230 (tramp-login-program "rsh")
231 (tramp-login-args (("%h") ("-l" "%u")))
232 (tramp-remote-sh "/bin/sh")))
233;;;###tramp-autoload
234(add-to-list 'tramp-methods
235 '("remsh"
236 (tramp-login-program "remsh")
237 (tramp-login-args (("%h") ("-l" "%u")))
238 (tramp-remote-sh "/bin/sh")))
239;;;###tramp-autoload
240(add-to-list 'tramp-methods
241 '("ssh"
242 (tramp-login-program "ssh")
243 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
244 (tramp-async-args (("-q")))
245 (tramp-remote-sh "/bin/sh")
246 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
247 ("-o" "UserKnownHostsFile=/dev/null")
248 ("-o" "StrictHostKeyChecking=no")))
249 (tramp-default-port 22)))
250;;;###tramp-autoload
251(add-to-list 'tramp-methods
252 '("ssh1"
253 (tramp-login-program "ssh")
254 (tramp-login-args (("-l" "%u") ("-p" "%p")
255 ("-1") ("-e" "none") ("%h")))
256 (tramp-async-args (("-q")))
257 (tramp-remote-sh "/bin/sh")
258 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
259 ("-o" "UserKnownHostsFile=/dev/null")
260 ("-o" "StrictHostKeyChecking=no")))
261 (tramp-default-port 22)))
262;;;###tramp-autoload
263(add-to-list 'tramp-methods
264 '("ssh2"
265 (tramp-login-program "ssh")
266 (tramp-login-args (("-l" "%u") ("-p" "%p")
267 ("-2") ("-e" "none") ("%h")))
268 (tramp-async-args (("-q")))
269 (tramp-remote-sh "/bin/sh")
270 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
271 ("-o" "UserKnownHostsFile=/dev/null")
272 ("-o" "StrictHostKeyChecking=no")))
273 (tramp-default-port 22)))
274;;;###tramp-autoload
275(add-to-list 'tramp-methods
276 '("sshx"
277 (tramp-login-program "ssh")
278 (tramp-login-args (("-l" "%u") ("-p" "%p")
279 ("-e" "none") ("-t" "-t")
280 ("%h") ("/bin/sh")))
281 (tramp-async-args (("-q")))
282 (tramp-remote-sh "/bin/sh")
283 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
284 ("-o" "UserKnownHostsFile=/dev/null")
285 ("-o" "StrictHostKeyChecking=no")))
286 (tramp-default-port 22)))
287;;;###tramp-autoload
288(add-to-list 'tramp-methods
289 '("telnet"
290 (tramp-login-program "telnet")
291 (tramp-login-args (("%h") ("%p")))
292 (tramp-remote-sh "/bin/sh")
293 (tramp-default-port 23)))
294;;;###tramp-autoload
295(add-to-list 'tramp-methods
296 '("su"
297 (tramp-login-program "su")
298 (tramp-login-args (("-") ("%u")))
299 (tramp-remote-sh "/bin/sh")))
300;;;###tramp-autoload
301(add-to-list 'tramp-methods
302 '("sudo"
303 (tramp-login-program "sudo")
304 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
305 (tramp-remote-sh "/bin/sh")))
306;;;###tramp-autoload
307(add-to-list 'tramp-methods
308 '("krlogin"
309 (tramp-login-program "krlogin")
310 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
311 (tramp-remote-sh "/bin/sh")))
312;;;###tramp-autoload
313(add-to-list 'tramp-methods
314 '("plink"
315 (tramp-login-program "plink")
316 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
317 (tramp-remote-sh "/bin/sh")
318 (tramp-password-end-of-line "xy") ;see docstring for "xy"
319 (tramp-default-port 22)))
320;;;###tramp-autoload
321(add-to-list 'tramp-methods
322 '("plink1"
323 (tramp-login-program "plink")
324 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
325 (tramp-remote-sh "/bin/sh")
326 (tramp-password-end-of-line "xy") ;see docstring for "xy"
327 (tramp-default-port 22)))
328;;;###tramp-autoload
329(add-to-list 'tramp-methods
330 `("plinkx"
331 (tramp-login-program "plink")
332 ;; ("%h") must be a single element, see
333 ;; `tramp-compute-multi-hops'.
334 (tramp-login-args (("-load") ("%h") ("-t")
335 (,(format
336 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
337 tramp-terminal-type
338 tramp-initial-end-of-output))
339 ("/bin/sh")))
340 (tramp-remote-sh "/bin/sh")))
341;;;###tramp-autoload
342(add-to-list 'tramp-methods
343 '("pscp"
344 (tramp-login-program "plink")
345 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
346 (tramp-remote-sh "/bin/sh")
347 (tramp-copy-program "pscp")
348 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
349 (tramp-copy-keep-date t)
350 (tramp-password-end-of-line "xy") ;see docstring for "xy"
351 (tramp-default-port 22)))
352;;;###tramp-autoload
353(add-to-list 'tramp-methods
354 '("psftp"
355 (tramp-login-program "plink")
356 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
357 (tramp-remote-sh "/bin/sh")
358 (tramp-copy-program "pscp")
359 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
360 (tramp-copy-keep-date t)
361 (tramp-password-end-of-line "xy"))) ;see docstring for "xy"
362;;;###tramp-autoload
363(add-to-list 'tramp-methods
364 '("fcp"
365 (tramp-login-program "fsh")
366 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
367 (tramp-remote-sh "/bin/sh -i")
368 (tramp-copy-program "fcp")
369 (tramp-copy-args (("-p" "%k")))
370 (tramp-copy-keep-date t)))
371
372(add-to-list 'tramp-default-method-alist
373 `(,tramp-local-host-regexp "\\`root\\'" "su"))
374
375(add-to-list 'tramp-default-user-alist
376 '("\\`su\\(do\\)?\\'" nil "root"))
377(add-to-list 'tramp-default-user-alist
378 `("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
379 nil ,(user-login-name)))
380
381(defconst tramp-completion-function-alist-rsh
382 '((tramp-parse-rhosts "/etc/hosts.equiv")
383 (tramp-parse-rhosts "~/.rhosts"))
384 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
385
386(defconst tramp-completion-function-alist-ssh
387 '((tramp-parse-rhosts "/etc/hosts.equiv")
388 (tramp-parse-rhosts "/etc/shosts.equiv")
389 (tramp-parse-shosts "/etc/ssh_known_hosts")
390 (tramp-parse-sconfig "/etc/ssh_config")
391 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
392 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
393 (tramp-parse-rhosts "~/.rhosts")
394 (tramp-parse-rhosts "~/.shosts")
395 (tramp-parse-shosts "~/.ssh/known_hosts")
396 (tramp-parse-sconfig "~/.ssh/config")
397 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
398 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
399 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
400
401(defconst tramp-completion-function-alist-telnet
402 '((tramp-parse-hosts "/etc/hosts"))
403 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
404
405(defconst tramp-completion-function-alist-su
406 '((tramp-parse-passwd "/etc/passwd"))
407 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
408
409(defconst tramp-completion-function-alist-putty
410 '((tramp-parse-putty
411 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
412 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
413
414(tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
415(tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
416(tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
417(tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh)
418(tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh)
419(tramp-set-completion-function "scpc" tramp-completion-function-alist-ssh)
420(tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
421(tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh)
422(tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
423(tramp-set-completion-function "rsyncc" tramp-completion-function-alist-ssh)
424(tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
425(tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
426(tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
427(tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh)
428(tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh)
429(tramp-set-completion-function "ssh1_old" tramp-completion-function-alist-ssh)
430(tramp-set-completion-function "ssh2_old" tramp-completion-function-alist-ssh)
431(tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
432(tramp-set-completion-function "telnet" tramp-completion-function-alist-telnet)
433(tramp-set-completion-function "su" tramp-completion-function-alist-su)
434(tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
435(tramp-set-completion-function "krlogin" tramp-completion-function-alist-rsh)
436(tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
437(tramp-set-completion-function "plink1" tramp-completion-function-alist-ssh)
438(tramp-set-completion-function "plinkx" tramp-completion-function-alist-putty)
439(tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
440(tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)
441
442;; "getconf PATH" yields:
443;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
444;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
445;; GNU/Linux (Debian, Suse): /bin:/usr/bin
446;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
447;; IRIX64: /usr/bin
448(defcustom tramp-remote-path
449 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
450 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
451 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
452 "*List of directories to search for executables on remote host.
453For every remote host, this variable will be set buffer local,
454keeping the list of existing directories on that host.
455
456You can use `~' in this list, but when searching for a shell which groks
457tilde expansion, all directory names starting with `~' will be ignored.
458
459`Default Directories' represent the list of directories given by
460the command \"getconf PATH\". It is recommended to use this
461entry on top of this list, because these are the default
462directories for POSIX compatible commands.
463
464`Private Directories' are the settings of the $PATH environment,
465as given in your `~/.profile'."
466 :group 'tramp
467 :type '(repeat (choice
468 (const :tag "Default Directories" tramp-default-remote-path)
469 (const :tag "Private Directories" tramp-own-remote-path)
470 (string :tag "Directory"))))
471
472(defcustom tramp-remote-process-environment
473 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
474 ,(format "TERM=%s" tramp-terminal-type)
475 "EMACS=t" ;; Deprecated.
476 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
477 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
478 "autocorrect=" "correct=")
479
480 "*List of environment variables to be set on the remote host.
481
482Each element should be a string of the form ENVVARNAME=VALUE. An
483entry ENVVARNAME= diables the corresponding environment variable,
484which might have been set in the init files like ~/.profile.
485
486Special handling is applied to the PATH environment, which should
487not be set here. Instead of, it should be set via `tramp-remote-path'."
488 :group 'tramp
489 :type '(repeat string))
490
491(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
492 "*Alist specifying extra arguments to pass to the remote shell.
493Entries are (REGEXP . ARGS) where REGEXP is a regular expression
494matching the shell file name and ARGS is a string specifying the
495arguments.
496
497This variable is only used when Tramp needs to start up another shell
498for tilde expansion. The extra arguments should typically prevent the
499shell from reading its init file."
500 :group 'tramp
501 ;; This might be the wrong way to test whether the widget type
502 ;; `alist' is available. Who knows the right way to test it?
503 :type (if (get 'alist 'widget-type)
504 '(alist :key-type string :value-type string)
505 '(repeat (cons string string))))
506
507(defconst tramp-actions-before-shell
508 '((tramp-login-prompt-regexp tramp-action-login)
509 (tramp-password-prompt-regexp tramp-action-password)
510 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
511 (shell-prompt-pattern tramp-action-succeed)
512 (tramp-shell-prompt-pattern tramp-action-succeed)
513 (tramp-yesno-prompt-regexp tramp-action-yesno)
514 (tramp-yn-prompt-regexp tramp-action-yn)
515 (tramp-terminal-prompt-regexp tramp-action-terminal)
516 (tramp-process-alive-regexp tramp-action-process-alive))
517 "List of pattern/action pairs.
518Whenever a pattern matches, the corresponding action is performed.
519Each item looks like (PATTERN ACTION).
520
521The PATTERN should be a symbol, a variable. The value of this
522variable gives the regular expression to search for. Note that the
523regexp must match at the end of the buffer, \"\\'\" is implicitly
524appended to it.
525
526The ACTION should also be a symbol, but a function. When the
527corresponding PATTERN matches, the ACTION function is called.")
528
529(defconst tramp-actions-copy-out-of-band
530 '((tramp-password-prompt-regexp tramp-action-password)
531 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
532 (tramp-copy-failed-regexp tramp-action-permission-denied)
533 (tramp-process-alive-regexp tramp-action-out-of-band))
534 "List of pattern/action pairs.
535This list is used for copying/renaming with out-of-band methods.
536
537See `tramp-actions-before-shell' for more info.")
538
539(defconst tramp-uudecode
540 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
541cat /tmp/tramp.$$
542rm -f /tmp/tramp.$$"
543 "Shell function to implement `uudecode' to standard output.
544Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
545for this or `uudecode -p', but some systems don't, and for them
546we have this shell function.")
547
548(defconst tramp-perl-file-truename
549 "%s -e '
550use File::Spec;
551use Cwd \"realpath\";
552
553sub recursive {
554 my ($volume, @dirs) = @_;
555 my $real = realpath(File::Spec->catpath(
556 $volume, File::Spec->catdir(@dirs), \"\"));
557 if ($real) {
558 my ($vol, $dir) = File::Spec->splitpath($real, 1);
559 return ($vol, File::Spec->splitdir($dir));
560 }
561 else {
562 my $last = pop(@dirs);
563 ($volume, @dirs) = recursive($volume, @dirs);
564 push(@dirs, $last);
565 return ($volume, @dirs);
566 }
567}
568
569$result = realpath($ARGV[0]);
570if (!$result) {
571 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
572 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
573
574 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
575}
576
577if ($ARGV[0] =~ /\\/$/) {
578 $result = $result . \"/\";
579}
580
581print \"\\\"$result\\\"\\n\";
582' \"$1\" 2>/dev/null"
583 "Perl script to produce output suitable for use with `file-truename'
584on the remote file system.
585Escape sequence %s is replaced with name of Perl binary.
586This string is passed to `format', so percent characters need to be doubled.")
587
588(defconst tramp-perl-file-name-all-completions
589 "%s -e 'sub case {
590 my $str = shift;
591 if ($ARGV[2]) {
592 return lc($str);
593 }
594 else {
595 return $str;
596 }
597}
598opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
599@files = readdir(d); closedir(d);
600foreach $f (@files) {
601 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
602 if (-d \"$ARGV[0]/$f\") {
603 print \"$f/\\n\";
604 }
605 else {
606 print \"$f\\n\";
607 }
608 }
609}
610print \"ok\\n\"
611' \"$1\" \"$2\" \"$3\" 2>/dev/null"
612 "Perl script to produce output suitable for use with
613`file-name-all-completions' on the remote file system. Escape
614sequence %s is replaced with name of Perl binary. This string is
615passed to `format', so percent characters need to be doubled.")
616
617;; Perl script to implement `file-attributes' in a Lisp `read'able
618;; output. If you are hacking on this, note that you get *no* output
619;; unless this spits out a complete line, including the '\n' at the
620;; end.
621;; The device number is returned as "-1", because there will be a virtual
622;; device number set in `tramp-handle-file-attributes'.
623(defconst tramp-perl-file-attributes
624 "%s -e '
625@stat = lstat($ARGV[0]);
626if (!@stat) {
627 print \"nil\\n\";
628 exit 0;
629}
630if (($stat[2] & 0170000) == 0120000)
631{
632 $type = readlink($ARGV[0]);
633 $type = \"\\\"$type\\\"\";
634}
635elsif (($stat[2] & 0170000) == 040000)
636{
637 $type = \"t\";
638}
639else
640{
641 $type = \"nil\"
642};
643$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
644$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
645printf(
646 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
647 $type,
648 $stat[3],
649 $uid,
650 $gid,
651 $stat[8] >> 16 & 0xffff,
652 $stat[8] & 0xffff,
653 $stat[9] >> 16 & 0xffff,
654 $stat[9] & 0xffff,
655 $stat[10] >> 16 & 0xffff,
656 $stat[10] & 0xffff,
657 $stat[7],
658 $stat[2],
659 $stat[1] >> 16 & 0xffff,
660 $stat[1] & 0xffff
661);' \"$1\" \"$2\" 2>/dev/null"
662 "Perl script to produce output suitable for use with `file-attributes'
663on the remote file system.
664Escape sequence %s is replaced with name of Perl binary.
665This string is passed to `format', so percent characters need to be doubled.")
666
667(defconst tramp-perl-directory-files-and-attributes
668 "%s -e '
669chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
670opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
671@list = readdir(DIR);
672closedir(DIR);
673$n = scalar(@list);
674printf(\"(\\n\");
675for($i = 0; $i < $n; $i++)
676{
677 $filename = $list[$i];
678 @stat = lstat($filename);
679 if (($stat[2] & 0170000) == 0120000)
680 {
681 $type = readlink($filename);
682 $type = \"\\\"$type\\\"\";
683 }
684 elsif (($stat[2] & 0170000) == 040000)
685 {
686 $type = \"t\";
687 }
688 else
689 {
690 $type = \"nil\"
691 };
692 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
693 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
694 printf(
695 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
696 $filename,
697 $type,
698 $stat[3],
699 $uid,
700 $gid,
701 $stat[8] >> 16 & 0xffff,
702 $stat[8] & 0xffff,
703 $stat[9] >> 16 & 0xffff,
704 $stat[9] & 0xffff,
705 $stat[10] >> 16 & 0xffff,
706 $stat[10] & 0xffff,
707 $stat[7],
708 $stat[2],
709 $stat[1] >> 16 & 0xffff,
710 $stat[1] & 0xffff,
711 $stat[0] >> 16 & 0xffff,
712 $stat[0] & 0xffff);
713}
714printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
715 "Perl script implementing `directory-files-attributes' as Lisp `read'able
716output.
717Escape sequence %s is replaced with name of Perl binary.
718This string is passed to `format', so percent characters need to be doubled.")
719
720;; These two use base64 encoding.
721(defconst tramp-perl-encode-with-module
722 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
723 "Perl program to use for encoding a file.
724Escape sequence %s is replaced with name of Perl binary.
725This string is passed to `format', so percent characters need to be doubled.
726This implementation requires the MIME::Base64 Perl module to be installed
727on the remote host.")
728
729(defconst tramp-perl-decode-with-module
730 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
731 "Perl program to use for decoding a file.
732Escape sequence %s is replaced with name of Perl binary.
733This string is passed to `format', so percent characters need to be doubled.
734This implementation requires the MIME::Base64 Perl module to be installed
735on the remote host.")
736
737(defconst tramp-perl-encode
738 "%s -e '
739# This script contributed by Juanma Barranquero <lektu@terra.es>.
740# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
741# Free Software Foundation, Inc.
742use strict;
743
744my %%trans = do {
745 my $i = 0;
746 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
747 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
748};
749
750binmode(\\*STDIN);
751
752# We read in chunks of 54 bytes, to generate output lines
753# of 72 chars (plus end of line)
754$/ = \\54;
755
756while (my $data = <STDIN>) {
757 my $pad = q();
758
759 # Only for the last chunk, and only if did not fill the last three-byte packet
760 if (eof) {
761 my $mod = length($data) %% 3;
762 $pad = q(=) x (3 - $mod) if $mod;
763 }
764
765 # Not the fastest method, but it is simple: unpack to binary string, split
766 # by groups of 6 bits and convert back from binary to byte; then map into
767 # the translation table
768 print
769 join q(),
770 map($trans{$_},
771 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
772 $pad,
773 qq(\\n);
774}' 2>/dev/null"
775 "Perl program to use for encoding a file.
776Escape sequence %s is replaced with name of Perl binary.
777This string is passed to `format', so percent characters need to be doubled.")
778
779(defconst tramp-perl-decode
780 "%s -e '
781# This script contributed by Juanma Barranquero <lektu@terra.es>.
782# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
783# Free Software Foundation, Inc.
784use strict;
785
786my %%trans = do {
787 my $i = 0;
788 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
789 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
790};
791
792my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
793
794binmode(\\*STDOUT);
795
796# We are going to accumulate into $pending to accept any line length
797# (we do not check they are <= 76 chars as the RFC says)
798my $pending = q();
799
800while (my $data = <STDIN>) {
801 chomp $data;
802
803 # If we find one or two =, we have reached the end and
804 # any following data is to be discarded
805 my $finished = $data =~ s/(==?).*/$1/;
806 $pending .= $data;
807
808 my $len = length($pending);
809 my $chunk = substr($pending, 0, $len & ~3);
810 $pending = substr($pending, $len & ~3 + 1);
811
812 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
813 # split in 8-bit chunks and convert back to char.
814 print join q(),
815 map $bytes{$_},
816 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
817
818 last if $finished;
819}' 2>/dev/null"
820 "Perl program to use for decoding a file.
821Escape sequence %s is replaced with name of Perl binary.
822This string is passed to `format', so percent characters need to be doubled.")
823
824(defconst tramp-vc-registered-read-file-names
825 "echo \"(\"
826while read file; do
827 if %s \"$file\"; then
828 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
829 else
830 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
831 fi
832 if %s \"$file\"; then
833 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
834 else
835 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
836 fi
837done
838echo \")\""
839 "Script to check existence of VC related files.
840It must be send formatted with two strings; the tests for file
841existence, and file readability. Input shall be read via
842here-document, otherwise the command could exceed maximum length
843of command line.")
844
845(defconst tramp-file-mode-type-map
846 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
847 (1 . "p") ; fifo
848 (2 . "c") ; character device
849 (3 . "m") ; multiplexed character device (v7)
850 (4 . "d") ; directory
851 (5 . "?") ; Named special file (XENIX)
852 (6 . "b") ; block device
853 (7 . "?") ; multiplexed block device (v7)
854 (8 . "-") ; regular file
855 (9 . "n") ; network special file (HP-UX)
856 (10 . "l") ; symlink
857 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
858 (12 . "s") ; socket
859 (13 . "D") ; door special (Solaris)
860 (14 . "w")) ; whiteout (BSD)
861 "A list of file types returned from the `stat' system call.
862This is used to map a mode number to a permission string.")
863
864;; New handlers should be added here. The following operations can be
865;; handled using the normal primitives: file-name-sans-versions,
866;; get-file-buffer.
867(defconst tramp-sh-file-name-handler-alist
868 '((load . tramp-handle-load)
869 (make-symbolic-link . tramp-handle-make-symbolic-link)
870 (file-name-as-directory . tramp-handle-file-name-as-directory)
871 (file-name-directory . tramp-handle-file-name-directory)
872 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
873 (file-truename . tramp-handle-file-truename)
874 (file-exists-p . tramp-handle-file-exists-p)
875 (file-directory-p . tramp-handle-file-directory-p)
876 (file-executable-p . tramp-handle-file-executable-p)
877 (file-readable-p . tramp-handle-file-readable-p)
878 (file-regular-p . tramp-handle-file-regular-p)
879 (file-symlink-p . tramp-handle-file-symlink-p)
880 (file-writable-p . tramp-handle-file-writable-p)
881 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
882 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
883 (file-attributes . tramp-handle-file-attributes)
884 (file-modes . tramp-handle-file-modes)
885 (directory-files . tramp-handle-directory-files)
886 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
887 (file-name-all-completions . tramp-handle-file-name-all-completions)
888 (file-name-completion . tramp-handle-file-name-completion)
889 (add-name-to-file . tramp-handle-add-name-to-file)
890 (copy-file . tramp-handle-copy-file)
891 (copy-directory . tramp-handle-copy-directory)
892 (rename-file . tramp-handle-rename-file)
893 (set-file-modes . tramp-handle-set-file-modes)
894 (set-file-times . tramp-handle-set-file-times)
895 (make-directory . tramp-handle-make-directory)
896 (delete-directory . tramp-handle-delete-directory)
897 (delete-file . tramp-handle-delete-file)
898 (directory-file-name . tramp-handle-directory-file-name)
899 ;; `executable-find' is not official yet.
900 (executable-find . tramp-handle-executable-find)
901 (start-file-process . tramp-handle-start-file-process)
902 (process-file . tramp-handle-process-file)
903 (shell-command . tramp-handle-shell-command)
904 (insert-directory . tramp-handle-insert-directory)
905 (expand-file-name . tramp-handle-expand-file-name)
906 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
907 (file-local-copy . tramp-handle-file-local-copy)
908 (file-remote-p . tramp-handle-file-remote-p)
909 (insert-file-contents . tramp-handle-insert-file-contents)
910 (insert-file-contents-literally
911 . tramp-handle-insert-file-contents-literally)
912 (write-region . tramp-handle-write-region)
913 (find-backup-file-name . tramp-handle-find-backup-file-name)
914 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
915 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
916 (dired-compress-file . tramp-handle-dired-compress-file)
917 (dired-recursive-delete-directory
918 . tramp-handle-dired-recursive-delete-directory)
919 (dired-uncache . tramp-handle-dired-uncache)
920 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
921 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
922 (file-selinux-context . tramp-handle-file-selinux-context)
923 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
924 (vc-registered . tramp-handle-vc-registered))
925 "Alist of handler functions.
926Operations not mentioned here will be handled by the normal Emacs functions.")
927
928;; This must be the last entry, because `identity' always matches.
929;;;###tramp-autoload
930(add-to-list 'tramp-foreign-file-name-handler-alist
931 '(identity . tramp-sh-file-name-handler) 'append)
932
933;;; File Name Handler Functions:
934
935(defun tramp-handle-make-symbolic-link
936 (filename linkname &optional ok-if-already-exists)
937 "Like `make-symbolic-link' for Tramp files.
938If LINKNAME is a non-Tramp file, it is used verbatim as the target of
939the symlink. If LINKNAME is a Tramp file, only the localname component is
940used as the target of the symlink.
941
942If LINKNAME is a Tramp file and the localname component is relative, then
943it is expanded first, before the localname component is taken. Note that
944this can give surprising results if the user/host for the source and
945target of the symlink differ."
946 (with-parsed-tramp-file-name linkname l
947 (let ((ln (tramp-get-remote-ln l))
948 (cwd (tramp-run-real-handler
949 'file-name-directory (list l-localname))))
950 (unless ln
951 (tramp-error
952 l 'file-error
953 "Making a symbolic link. ln(1) does not exist on the remote host."))
954
955 ;; Do the 'confirm if exists' thing.
956 (when (file-exists-p linkname)
957 ;; What to do?
958 (if (or (null ok-if-already-exists) ; not allowed to exist
959 (and (numberp ok-if-already-exists)
960 (not (yes-or-no-p
961 (format
962 "File %s already exists; make it a link anyway? "
963 l-localname)))))
964 (tramp-error
965 l 'file-already-exists "File %s already exists" l-localname)
966 (delete-file linkname)))
967
968 ;; If FILENAME is a Tramp name, use just the localname component.
969 (when (tramp-tramp-file-p filename)
970 (setq filename
971 (tramp-file-name-localname
972 (tramp-dissect-file-name (expand-file-name filename)))))
973
974 (tramp-flush-file-property l (file-name-directory l-localname))
975 (tramp-flush-file-property l l-localname)
976
977 ;; Right, they are on the same host, regardless of user, method, etc.
978 ;; We now make the link on the remote machine. This will occur as the user
979 ;; that FILENAME belongs to.
980 (tramp-send-command-and-check
981 l
982 (format
983 "cd %s && %s -sf %s %s"
984 (tramp-shell-quote-argument cwd)
985 ln
986 (tramp-shell-quote-argument filename)
987 (tramp-shell-quote-argument l-localname))
988 t))))
989
990(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
991 "Like `load' for Tramp files."
992 (with-parsed-tramp-file-name (expand-file-name file) nil
993 (unless nosuffix
994 (cond ((file-exists-p (concat file ".elc"))
995 (setq file (concat file ".elc")))
996 ((file-exists-p (concat file ".el"))
997 (setq file (concat file ".el")))))
998 (when must-suffix
999 ;; The first condition is always true for absolute file names.
1000 ;; Included for safety's sake.
1001 (unless (or (file-name-directory file)
1002 (string-match "\\.elc?\\'" file))
1003 (tramp-error
1004 v 'file-error
1005 "File `%s' does not include a `.el' or `.elc' suffix" file)))
1006 (unless noerror
1007 (when (not (file-exists-p file))
1008 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
1009 (if (not (file-exists-p file))
1010 nil
1011 (let ((tramp-message-show-message (not nomessage)))
1012 (with-progress-reporter v 0 (format "Loading %s" file)
1013 (let ((local-copy (file-local-copy file)))
1014 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
1015 (unwind-protect
1016 (load local-copy noerror t t)
1017 (delete-file local-copy)))))
1018 t)))
1019
1020;; Localname manipulation functions that grok Tramp localnames...
1021(defun tramp-handle-file-name-as-directory (file)
1022 "Like `file-name-as-directory' but aware of Tramp files."
1023 ;; `file-name-as-directory' would be sufficient except localname is
1024 ;; the empty string.
1025 (let ((v (tramp-dissect-file-name file t)))
1026 ;; Run the command on the localname portion only.
1027 (tramp-make-tramp-file-name
1028 (tramp-file-name-method v)
1029 (tramp-file-name-user v)
1030 (tramp-file-name-host v)
1031 (tramp-run-real-handler
1032 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
1033
1034(defun tramp-handle-file-name-directory (file)
1035 "Like `file-name-directory' but aware of Tramp files."
1036 ;; Everything except the last filename thing is the directory. We
1037 ;; cannot apply `with-parsed-tramp-file-name', because this expands
1038 ;; the remote file name parts. This is a problem when we are in
1039 ;; file name completion.
1040 (let ((v (tramp-dissect-file-name file t)))
1041 ;; Run the command on the localname portion only.
1042 (tramp-make-tramp-file-name
1043 (tramp-file-name-method v)
1044 (tramp-file-name-user v)
1045 (tramp-file-name-host v)
1046 (tramp-run-real-handler
1047 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
1048
1049(defun tramp-handle-file-name-nondirectory (file)
1050 "Like `file-name-nondirectory' but aware of Tramp files."
1051 (with-parsed-tramp-file-name file nil
1052 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
1053
1054(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
1055 "Like `file-truename' for Tramp files."
1056 (with-parsed-tramp-file-name (expand-file-name filename) nil
1057 (with-file-property v localname "file-truename"
1058 (let ((result nil)) ; result steps in reverse order
1059 (tramp-message v 4 "Finding true name for `%s'" filename)
1060 (cond
1061 ;; Use GNU readlink --canonicalize-missing where available.
1062 ((tramp-get-remote-readlink v)
1063 (setq result
1064 (tramp-send-command-and-read
1065 v
1066 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1067 (tramp-get-remote-readlink v)
1068 (tramp-shell-quote-argument localname)))))
1069
1070 ;; Use Perl implementation.
1071 ((and (tramp-get-remote-perl v)
1072 (tramp-get-connection-property v "perl-file-spec" nil)
1073 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1074 (tramp-maybe-send-script
1075 v tramp-perl-file-truename "tramp_perl_file_truename")
1076 (setq result
1077 (tramp-send-command-and-read
1078 v
1079 (format "tramp_perl_file_truename %s"
1080 (tramp-shell-quote-argument localname)))))
1081
1082 ;; Do it yourself. We bind `directory-sep-char' here for
1083 ;; XEmacs on Windows, which would otherwise use backslash.
1084 (t (let* ((directory-sep-char ?/)
1085 (steps (tramp-compat-split-string localname "/"))
1086 (localnamedir (tramp-run-real-handler
1087 'file-name-as-directory (list localname)))
1088 (is-dir (string= localname localnamedir))
1089 (thisstep nil)
1090 (numchase 0)
1091 ;; Don't make the following value larger than
1092 ;; necessary. People expect an error message in a
1093 ;; timely fashion when something is wrong;
1094 ;; otherwise they might think that Emacs is hung.
1095 ;; Of course, correctness has to come first.
1096 (numchase-limit 20)
1097 symlink-target)
1098 (while (and steps (< numchase numchase-limit))
1099 (setq thisstep (pop steps))
1100 (tramp-message
1101 v 5 "Check %s"
1102 (mapconcat 'identity
1103 (append '("") (reverse result) (list thisstep))
1104 "/"))
1105 (setq symlink-target
1106 (nth 0 (file-attributes
1107 (tramp-make-tramp-file-name
1108 method user host
1109 (mapconcat 'identity
1110 (append '("")
1111 (reverse result)
1112 (list thisstep))
1113 "/")))))
1114 (cond ((string= "." thisstep)
1115 (tramp-message v 5 "Ignoring step `.'"))
1116 ((string= ".." thisstep)
1117 (tramp-message v 5 "Processing step `..'")
1118 (pop result))
1119 ((stringp symlink-target)
1120 ;; It's a symlink, follow it.
1121 (tramp-message v 5 "Follow symlink to %s" symlink-target)
1122 (setq numchase (1+ numchase))
1123 (when (file-name-absolute-p symlink-target)
1124 (setq result nil))
1125 ;; If the symlink was absolute, we'll get a string like
1126 ;; "/user@host:/some/target"; extract the
1127 ;; "/some/target" part from it.
1128 (when (tramp-tramp-file-p symlink-target)
1129 (unless (tramp-equal-remote filename symlink-target)
1130 (tramp-error
1131 v 'file-error
1132 "Symlink target `%s' on wrong host" symlink-target))
1133 (setq symlink-target localname))
1134 (setq steps
1135 (append (tramp-compat-split-string
1136 symlink-target "/")
1137 steps)))
1138 (t
1139 ;; It's a file.
1140 (setq result (cons thisstep result)))))
1141 (when (>= numchase numchase-limit)
1142 (tramp-error
1143 v 'file-error
1144 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1145 (setq result (reverse result))
1146 ;; Combine list to form string.
1147 (setq result
1148 (if result
1149 (mapconcat 'identity (cons "" result) "/")
1150 "/"))
1151 (when (and is-dir (or (string= "" result)
1152 (not (string= (substring result -1) "/"))))
1153 (setq result (concat result "/"))))))
1154
1155 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
1156 (tramp-make-tramp-file-name method user host result)))))
1157
1158;; Basic functions.
1159
1160(defun tramp-handle-file-exists-p (filename)
1161 "Like `file-exists-p' for Tramp files."
1162 (with-parsed-tramp-file-name filename nil
1163 (with-file-property v localname "file-exists-p"
1164 (or (not (null (tramp-get-file-property
1165 v localname "file-attributes-integer" nil)))
1166 (not (null (tramp-get-file-property
1167 v localname "file-attributes-string" nil)))
1168 (tramp-send-command-and-check
1169 v
1170 (format
1171 "%s %s"
1172 (tramp-get-file-exists-command v)
1173 (tramp-shell-quote-argument localname)))))))
1174
1175;; CCC: This should check for an error condition and signal failure
1176;; when something goes wrong.
1177;; Daniel Pittman <daniel@danann.net>
1178(defun tramp-handle-file-attributes (filename &optional id-format)
1179 "Like `file-attributes' for Tramp files."
1180 (unless id-format (setq id-format 'integer))
1181 ;; Don't modify `last-coding-system-used' by accident.
1182 (let ((last-coding-system-used last-coding-system-used))
1183 (with-parsed-tramp-file-name (expand-file-name filename) nil
1184 (with-file-property v localname (format "file-attributes-%s" id-format)
1185 (save-excursion
1186 (tramp-convert-file-attributes
1187 v
1188 (cond
1189 ((tramp-get-remote-stat v)
1190 (tramp-do-file-attributes-with-stat v localname id-format))
1191 ((tramp-get-remote-perl v)
1192 (tramp-do-file-attributes-with-perl v localname id-format))
1193 (t
1194 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
1195
1196(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1197 "Implement `file-attributes' for Tramp files using the ls(1) command."
1198 (let (symlinkp dirp
1199 res-inode res-filemodes res-numlinks
1200 res-uid res-gid res-size res-symlink-target)
1201 (tramp-message vec 5 "file attributes with ls: %s" localname)
1202 (tramp-send-command
1203 vec
1204 (format "(%s %s || %s -h %s) && %s %s %s"
1205 (tramp-get-file-exists-command vec)
1206 (tramp-shell-quote-argument localname)
1207 (tramp-get-test-command vec)
1208 (tramp-shell-quote-argument localname)
1209 (tramp-get-ls-command vec)
1210 (if (eq id-format 'integer) "-ildn" "-ild")
1211 (tramp-shell-quote-argument localname)))
1212 ;; parse `ls -l' output ...
1213 (with-current-buffer (tramp-get-buffer vec)
1214 (when (> (buffer-size) 0)
1215 (goto-char (point-min))
1216 ;; ... inode
1217 (setq res-inode
1218 (condition-case err
1219 (read (current-buffer))
1220 (invalid-read-syntax
1221 (when (and (equal (cadr err)
1222 "Integer constant overflow in reader")
1223 (string-match
1224 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1225 (car (cddr err))))
1226 (let* ((big (read (substring (car (cddr err)) 0
1227 (match-beginning 1))))
1228 (small (read (match-string 1 (car (cddr err)))))
1229 (twiddle (/ small 65536)))
1230 (cons (+ big twiddle)
1231 (- small (* twiddle 65536))))))))
1232 ;; ... file mode flags
1233 (setq res-filemodes (symbol-name (read (current-buffer))))
1234 ;; ... number links
1235 (setq res-numlinks (read (current-buffer)))
1236 ;; ... uid and gid
1237 (setq res-uid (read (current-buffer)))
1238 (setq res-gid (read (current-buffer)))
1239 (if (eq id-format 'integer)
1240 (progn
1241 (unless (numberp res-uid) (setq res-uid -1))
1242 (unless (numberp res-gid) (setq res-gid -1)))
1243 (progn
1244 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1245 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1246 ;; ... size
1247 (setq res-size (read (current-buffer)))
1248 ;; From the file modes, figure out other stuff.
1249 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1250 (setq dirp (eq ?d (aref res-filemodes 0)))
1251 ;; if symlink, find out file name pointed to
1252 (when symlinkp
1253 (search-forward "-> ")
1254 (setq res-symlink-target
1255 (buffer-substring (point) (tramp-compat-line-end-position))))
1256 ;; return data gathered
1257 (list
1258 ;; 0. t for directory, string (name linked to) for symbolic
1259 ;; link, or nil.
1260 (or dirp res-symlink-target)
1261 ;; 1. Number of links to file.
1262 res-numlinks
1263 ;; 2. File uid.
1264 res-uid
1265 ;; 3. File gid.
1266 res-gid
1267 ;; 4. Last access time, as a list of two integers. First
1268 ;; integer has high-order 16 bits of time, second has low 16
1269 ;; bits.
1270 ;; 5. Last modification time, likewise.
1271 ;; 6. Last status change time, likewise.
1272 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1273 ;; 7. Size in bytes (-1, if number is out of range).
1274 res-size
1275 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1276 res-filemodes
1277 ;; 9. t if file's gid would change if file were deleted and
1278 ;; recreated. Will be set in `tramp-convert-file-attributes'
1279 t
1280 ;; 10. inode number.
1281 res-inode
1282 ;; 11. Device number. Will be replaced by a virtual device number.
1283 -1
1284 )))))
1285
1286(defun tramp-do-file-attributes-with-perl
1287 (vec localname &optional id-format)
1288 "Implement `file-attributes' for Tramp files using a Perl script."
1289 (tramp-message vec 5 "file attributes with perl: %s" localname)
1290 (tramp-maybe-send-script
1291 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1292 (tramp-send-command-and-read
1293 vec
1294 (format "tramp_perl_file_attributes %s %s"
1295 (tramp-shell-quote-argument localname) id-format)))
1296
1297(defun tramp-do-file-attributes-with-stat
1298 (vec localname &optional id-format)
1299 "Implement `file-attributes' for Tramp files using stat(1) command."
1300 (tramp-message vec 5 "file attributes with stat: %s" localname)
1301 (tramp-send-command-and-read
1302 vec
1303 (format
1304 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1305 ;; parse correctly the sequence "((". Therefore, we add a space.
1306 "( (%s %s || %s -h %s) && %s -c '( (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s || echo nil)"
1307 (tramp-get-file-exists-command vec)
1308 (tramp-shell-quote-argument localname)
1309 (tramp-get-test-command vec)
1310 (tramp-shell-quote-argument localname)
1311 (tramp-get-remote-stat vec)
1312 (if (eq id-format 'integer) "%u" "\"%U\"")
1313 (if (eq id-format 'integer) "%g" "\"%G\"")
1314 (tramp-shell-quote-argument localname))))
1315
1316(defun tramp-handle-set-visited-file-modtime (&optional time-list)
1317 "Like `set-visited-file-modtime' for Tramp files."
1318 (unless (buffer-file-name)
1319 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1320 (buffer-name)))
1321 (if time-list
1322 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1323 (let ((f (buffer-file-name))
1324 coding-system-used)
1325 (with-parsed-tramp-file-name f nil
1326 (let* ((attr (file-attributes f))
1327 ;; '(-1 65535) means file doesn't exists yet.
1328 (modtime (or (nth 5 attr) '(-1 65535))))
1329 (when (boundp 'last-coding-system-used)
1330 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1331 ;; We use '(0 0) as a don't-know value. See also
1332 ;; `tramp-do-file-attributes-with-ls'.
1333 (if (not (equal modtime '(0 0)))
1334 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1335 (progn
1336 (tramp-send-command
1337 v
1338 (format "%s -ild %s"
1339 (tramp-get-ls-command v)
1340 (tramp-shell-quote-argument localname)))
1341 (setq attr (buffer-substring (point)
1342 (progn (end-of-line) (point)))))
1343 (tramp-set-file-property
1344 v localname "visited-file-modtime-ild" attr))
1345 (when (boundp 'last-coding-system-used)
1346 (set 'last-coding-system-used coding-system-used))
1347 nil)))))
1348
1349;; This function makes the same assumption as
1350;; `tramp-handle-set-visited-file-modtime'.
1351(defun tramp-handle-verify-visited-file-modtime (buf)
1352 "Like `verify-visited-file-modtime' for Tramp files.
1353At the time `verify-visited-file-modtime' calls this function, we
1354already know that the buffer is visiting a file and that
1355`visited-file-modtime' does not return 0. Do not call this
1356function directly, unless those two cases are already taken care
1357of."
1358 (with-current-buffer buf
1359 (let ((f (buffer-file-name)))
1360 ;; There is no file visiting the buffer, or the buffer has no
1361 ;; recorded last modification time, or there is no established
1362 ;; connection.
1363 (if (or (not f)
1364 (eq (visited-file-modtime) 0)
1365 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1366 t
1367 (with-parsed-tramp-file-name f nil
1368 (tramp-flush-file-property v localname)
1369 (let* ((attr (file-attributes f))
1370 (modtime (nth 5 attr))
1371 (mt (visited-file-modtime)))
1372
1373 (cond
1374 ;; File exists, and has a known modtime.
1375 ((and attr (not (equal modtime '(0 0))))
1376 (< (abs (tramp-time-diff
1377 modtime
1378 ;; For compatibility, deal with both the old
1379 ;; (HIGH . LOW) and the new (HIGH LOW) return
1380 ;; values of `visited-file-modtime'.
1381 (if (atom (cdr mt))
1382 (list (car mt) (cdr mt))
1383 mt)))
1384 2))
1385 ;; Modtime has the don't know value.
1386 (attr
1387 (tramp-send-command
1388 v
1389 (format "%s -ild %s"
1390 (tramp-get-ls-command v)
1391 (tramp-shell-quote-argument localname)))
1392 (with-current-buffer (tramp-get-buffer v)
1393 (setq attr (buffer-substring
1394 (point) (progn (end-of-line) (point)))))
1395 (equal
1396 attr
1397 (tramp-get-file-property
1398 v localname "visited-file-modtime-ild" "")))
1399 ;; If file does not exist, say it is not modified if and
1400 ;; only if that agrees with the buffer's record.
1401 (t (equal mt '(-1 65535))))))))))
1402
1403(defun tramp-handle-set-file-modes (filename mode)
1404 "Like `set-file-modes' for Tramp files."
1405 (with-parsed-tramp-file-name filename nil
1406 (tramp-flush-file-property v localname)
1407 ;; FIXME: extract the proper text from chmod's stderr.
1408 (tramp-barf-unless-okay
1409 v
1410 (format "chmod %s %s"
1411 (tramp-compat-decimal-to-octal mode)
1412 (tramp-shell-quote-argument localname))
1413 "Error while changing file's mode %s" filename)))
1414
1415(defun tramp-handle-set-file-times (filename &optional time)
1416 "Like `set-file-times' for Tramp files."
1417 (if (file-remote-p filename)
1418 (with-parsed-tramp-file-name filename nil
1419 (tramp-flush-file-property v localname)
1420 (let ((time (if (or (null time) (equal time '(0 0)))
1421 (current-time)
1422 time))
1423 ;; With GNU Emacs, `format-time-string' has an optional
1424 ;; parameter UNIVERSAL. This is preferred, because we
1425 ;; could handle the case when the remote host is located
1426 ;; in a different time zone as the local host.
1427 (utc (not (featurep 'xemacs))))
1428 (tramp-send-command-and-check
1429 v (format "%s touch -t %s %s"
1430 (if utc "TZ=UTC; export TZ;" "")
1431 (if utc
1432 (format-time-string "%Y%m%d%H%M.%S" time t)
1433 (format-time-string "%Y%m%d%H%M.%S" time))
1434 (tramp-shell-quote-argument localname)))))
1435
1436 ;; We handle also the local part, because in older Emacsen,
1437 ;; without `set-file-times', this function is an alias for this.
1438 ;; We are local, so we don't need the UTC settings.
1439 (zerop
1440 (tramp-compat-call-process
1441 "touch" nil nil nil "-t"
1442 (format-time-string "%Y%m%d%H%M.%S" time)
1443 (tramp-shell-quote-argument filename)))))
1444
1445(defun tramp-set-file-uid-gid (filename &optional uid gid)
1446 "Set the ownership for FILENAME.
1447If UID and GID are provided, these values are used; otherwise uid
1448and gid of the corresponding user is taken. Both parameters must be integers."
1449 ;; Modern Unices allow chown only for root. So we might need
1450 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1451 ;; working with su(do)? when it is needed, so it shall succeed in
1452 ;; the majority of cases.
1453 ;; Don't modify `last-coding-system-used' by accident.
1454 (let ((last-coding-system-used last-coding-system-used))
1455 (if (file-remote-p filename)
1456 (with-parsed-tramp-file-name filename nil
1457 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1458 ;; If we are root on the local host, we can do it directly.
1459 (tramp-set-file-uid-gid localname uid gid)
1460 (let ((uid (or (and (integerp uid) uid)
1461 (tramp-get-remote-uid v 'integer)))
1462 (gid (or (and (integerp gid) gid)
1463 (tramp-get-remote-gid v 'integer))))
1464 (tramp-send-command
1465 v (format
1466 "chown %d:%d %s" uid gid
1467 (tramp-shell-quote-argument localname))))))
1468
1469 ;; We handle also the local part, because there doesn't exist
1470 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1471 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
1472 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
1473 (tramp-compat-call-process
1474 "chown" nil nil nil
1475 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1476
1477(defun tramp-remote-selinux-p (vec)
1478 "Check, whether SELINUX is enabled on the remote host."
1479 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
1480 (let ((result (tramp-find-executable
1481 vec "getenforce" (tramp-get-remote-path vec) t t)))
1482 (and result
1483 (string-equal
1484 (tramp-send-command-and-read
1485 vec (format "echo \\\"`%S`\\\"" result))
1486 "Enforcing")))))
1487
1488(defun tramp-handle-file-selinux-context (filename)
1489 "Like `file-selinux-context' for Tramp files."
1490 (with-parsed-tramp-file-name filename nil
1491 (with-file-property v localname "file-selinux-context"
1492 (let ((context '(nil nil nil nil))
1493 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1494 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1495 (when (and (tramp-remote-selinux-p v)
1496 (tramp-send-command-and-check
1497 v (format
1498 "%s -d -Z %s"
1499 (tramp-get-ls-command v)
1500 (tramp-shell-quote-argument localname))))
1501 (with-current-buffer (tramp-get-connection-buffer v)
1502 (goto-char (point-min))
1503 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
1504 (setq context (list (match-string 1) (match-string 2)
1505 (match-string 3) (match-string 4))))))
1506 ;; Return the context.
1507 context))))
1508
1509(defun tramp-handle-set-file-selinux-context (filename context)
1510 "Like `set-file-selinux-context' for Tramp files."
1511 (with-parsed-tramp-file-name filename nil
1512 (if (and (consp context)
1513 (tramp-remote-selinux-p v)
1514 (tramp-send-command-and-check
1515 v (format "chcon %s %s %s %s %s"
1516 (if (stringp (nth 0 context))
1517 (format "--user=%s" (nth 0 context)) "")
1518 (if (stringp (nth 1 context))
1519 (format "--role=%s" (nth 1 context)) "")
1520 (if (stringp (nth 2 context))
1521 (format "--type=%s" (nth 2 context)) "")
1522 (if (stringp (nth 3 context))
1523 (format "--range=%s" (nth 3 context)) "")
1524 (tramp-shell-quote-argument localname))))
1525 (tramp-set-file-property v localname "file-selinux-context" context)
1526 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
1527 ;; We always return nil.
1528 nil)
1529
1530;; Simple functions using the `test' command.
1531
1532(defun tramp-handle-file-executable-p (filename)
1533 "Like `file-executable-p' for Tramp files."
1534 (with-parsed-tramp-file-name filename nil
1535 (with-file-property v localname "file-executable-p"
1536 ;; Examine `file-attributes' cache to see if request can be
1537 ;; satisfied without remote operation.
1538 (or (tramp-check-cached-permissions v ?x)
1539 (tramp-run-test "-x" filename)))))
1540
1541(defun tramp-handle-file-readable-p (filename)
1542 "Like `file-readable-p' for Tramp files."
1543 (with-parsed-tramp-file-name filename nil
1544 (with-file-property v localname "file-readable-p"
1545 ;; Examine `file-attributes' cache to see if request can be
1546 ;; satisfied without remote operation.
1547 (or (tramp-check-cached-permissions v ?r)
1548 (tramp-run-test "-r" filename)))))
1549
1550;; When the remote shell is started, it looks for a shell which groks
1551;; tilde expansion. Here, we assume that all shells which grok tilde
1552;; expansion will also provide a `test' command which groks `-nt' (for
1553;; newer than). If this breaks, tell me about it and I'll try to do
1554;; something smarter about it.
1555(defun tramp-handle-file-newer-than-file-p (file1 file2)
1556 "Like `file-newer-than-file-p' for Tramp files."
1557 (cond ((not (file-exists-p file1))
1558 nil)
1559 ((not (file-exists-p file2))
1560 t)
1561 ;; We are sure both files exist at this point.
1562 (t
1563 (save-excursion
1564 ;; We try to get the mtime of both files. If they are not
1565 ;; equal to the "dont-know" value, then we subtract the times
1566 ;; and obtain the result.
1567 (let ((fa1 (file-attributes file1))
1568 (fa2 (file-attributes file2)))
1569 (if (and (not (equal (nth 5 fa1) '(0 0)))
1570 (not (equal (nth 5 fa2) '(0 0))))
1571 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1572 ;; If one of them is the dont-know value, then we can
1573 ;; still try to run a shell command on the remote host.
1574 ;; However, this only works if both files are Tramp
1575 ;; files and both have the same method, same user, same
1576 ;; host.
1577 (unless (tramp-equal-remote file1 file2)
1578 (with-parsed-tramp-file-name
1579 (if (tramp-tramp-file-p file1) file1 file2) nil
1580 (tramp-error
1581 v 'file-error
1582 "Files %s and %s must have same method, user, host"
1583 file1 file2)))
1584 (with-parsed-tramp-file-name file1 nil
1585 (tramp-run-test2
1586 (tramp-get-test-nt-command v) file1 file2))))))))
1587
1588;; Functions implemented using the basic functions above.
1589
1590(defun tramp-handle-file-modes (filename)
1591 "Like `file-modes' for Tramp files."
1592 (let ((truename (or (file-truename filename) filename)))
1593 (when (file-exists-p truename)
1594 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
1595
1596(defun tramp-handle-file-directory-p (filename)
1597 "Like `file-directory-p' for Tramp files."
1598 ;; Care must be taken that this function returns `t' for symlinks
1599 ;; pointing to directories. Surely the most obvious implementation
1600 ;; would be `test -d', but that returns false for such symlinks.
1601 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
1602 ;; I now think he's right. So we could be using `test -d', couldn't
1603 ;; we?
1604 ;;
1605 ;; Alternatives: `cd %s', `test -d %s'
1606 (with-parsed-tramp-file-name filename nil
1607 (with-file-property v localname "file-directory-p"
1608 (tramp-run-test "-d" filename))))
1609
1610(defun tramp-handle-file-regular-p (filename)
1611 "Like `file-regular-p' for Tramp files."
1612 (and (file-exists-p filename)
1613 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
1614
1615(defun tramp-handle-file-symlink-p (filename)
1616 "Like `file-symlink-p' for Tramp files."
1617 (with-parsed-tramp-file-name filename nil
1618 (let ((x (car (file-attributes filename))))
1619 (when (stringp x)
1620 ;; When Tramp is running on VMS, then `file-name-absolute-p'
1621 ;; might do weird things.
1622 (if (file-name-absolute-p x)
1623 (tramp-make-tramp-file-name method user host x)
1624 x)))))
1625
1626(defun tramp-handle-file-writable-p (filename)
1627 "Like `file-writable-p' for Tramp files."
1628 (with-parsed-tramp-file-name filename nil
1629 (with-file-property v localname "file-writable-p"
1630 (if (file-exists-p filename)
1631 ;; Examine `file-attributes' cache to see if request can be
1632 ;; satisfied without remote operation.
1633 (or (tramp-check-cached-permissions v ?w)
1634 (tramp-run-test "-w" filename))
1635 ;; If file doesn't exist, check if directory is writable.
1636 (and (tramp-run-test "-d" (file-name-directory filename))
1637 (tramp-run-test "-w" (file-name-directory filename)))))))
1638
1639(defun tramp-handle-file-ownership-preserved-p (filename)
1640 "Like `file-ownership-preserved-p' for Tramp files."
1641 (with-parsed-tramp-file-name filename nil
1642 (with-file-property v localname "file-ownership-preserved-p"
1643 (let ((attributes (file-attributes filename)))
1644 ;; Return t if the file doesn't exist, since it's true that no
1645 ;; information would be lost by an (attempted) delete and create.
1646 (or (null attributes)
1647 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
1648
1649;; Other file name ops.
1650
1651(defun tramp-handle-directory-file-name (directory)
1652 "Like `directory-file-name' for Tramp files."
1653 ;; If localname component of filename is "/", leave it unchanged.
1654 ;; Otherwise, remove any trailing slash from localname component.
1655 ;; Method, host, etc, are unchanged. Does it make sense to try
1656 ;; to avoid parsing the filename?
1657 (with-parsed-tramp-file-name directory nil
1658 (if (and (not (zerop (length localname)))
1659 (eq (aref localname (1- (length localname))) ?/)
1660 (not (string= localname "/")))
1661 (substring directory 0 -1)
1662 directory)))
1663
1664;; Directory listings.
1665
1666(defun tramp-handle-directory-files
1667 (directory &optional full match nosort files-only)
1668 "Like `directory-files' for Tramp files."
1669 ;; FILES-ONLY is valid for XEmacs only.
1670 (when (file-directory-p directory)
1671 (setq directory (file-name-as-directory (expand-file-name directory)))
1672 (let ((temp (nreverse (file-name-all-completions "" directory)))
1673 result item)
1674
1675 (while temp
1676 (setq item (directory-file-name (pop temp)))
1677 (when (and (or (null match) (string-match match item))
1678 (or (null files-only)
1679 ;; Files only.
1680 (and (equal files-only t) (file-regular-p item))
1681 ;; Directories only.
1682 (file-directory-p item)))
1683 (push (if full (concat directory item) item)
1684 result)))
1685 (if nosort result (sort result 'string<)))))
1686
1687(defun tramp-handle-directory-files-and-attributes
1688 (directory &optional full match nosort id-format)
1689 "Like `directory-files-and-attributes' for Tramp files."
1690 (unless id-format (setq id-format 'integer))
1691 (when (file-directory-p directory)
1692 (setq directory (expand-file-name directory))
1693 (let* ((temp
1694 (copy-tree
1695 (with-parsed-tramp-file-name directory nil
1696 (with-file-property
1697 v localname
1698 (format "directory-files-and-attributes-%s" id-format)
1699 (save-excursion
1700 (mapcar
1701 (lambda (x)
1702 (cons (car x)
1703 (tramp-convert-file-attributes v (cdr x))))
1704 (cond
1705 ((tramp-get-remote-stat v)
1706 (tramp-do-directory-files-and-attributes-with-stat
1707 v localname id-format))
1708 ((tramp-get-remote-perl v)
1709 (tramp-do-directory-files-and-attributes-with-perl
1710 v localname id-format)))))))))
1711 result item)
1712
1713 (while temp
1714 (setq item (pop temp))
1715 (when (or (null match) (string-match match (car item)))
1716 (when full
1717 (setcar item (expand-file-name (car item) directory)))
1718 (push item result)))
1719
1720 (if nosort
1721 result
1722 (sort result (lambda (x y) (string< (car x) (car y))))))))
1723
1724(defun tramp-do-directory-files-and-attributes-with-perl
1725 (vec localname &optional id-format)
1726 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1727 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1728 (tramp-maybe-send-script
1729 vec tramp-perl-directory-files-and-attributes
1730 "tramp_perl_directory_files_and_attributes")
1731 (let ((object
1732 (tramp-send-command-and-read
1733 vec
1734 (format "tramp_perl_directory_files_and_attributes %s %s"
1735 (tramp-shell-quote-argument localname) id-format))))
1736 (when (stringp object) (tramp-error vec 'file-error object))
1737 object))
1738
1739(defun tramp-do-directory-files-and-attributes-with-stat
1740 (vec localname &optional id-format)
1741 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1742 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1743 (tramp-send-command-and-read
1744 vec
1745 (format
1746 (concat
1747 ;; We must care about filenames with spaces, or starting with
1748 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1749 ;; but it does not work on all remote systems. Therefore, we
1750 ;; quote the filenames via sed.
1751 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
1752 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
1753 "echo \")\"")
1754 (tramp-shell-quote-argument localname)
1755 (tramp-get-ls-command vec)
1756 (tramp-get-remote-stat vec)
1757 (if (eq id-format 'integer) "%u" "\"%U\"")
1758 (if (eq id-format 'integer) "%g" "\"%G\""))))
1759
1760;; This function should return "foo/" for directories and "bar" for
1761;; files.
1762(defun tramp-handle-file-name-all-completions (filename directory)
1763 "Like `file-name-all-completions' for Tramp files."
1764 (unless (save-match-data (string-match "/" filename))
1765 (with-parsed-tramp-file-name (expand-file-name directory) nil
1766
1767 (all-completions
1768 filename
1769 (mapcar
1770 'list
1771 (or
1772 ;; Try cache first
1773 (and
1774 ;; Ignore if expired
1775 (or (not (integerp tramp-completion-reread-directory-timeout))
1776 (<= (tramp-time-diff
1777 (current-time)
1778 (tramp-get-file-property
1779 v localname "last-completion" '(0 0 0)))
1780 tramp-completion-reread-directory-timeout))
1781
1782 ;; Try cache entries for filename, filename with last
1783 ;; character removed, filename with last two characters
1784 ;; removed, ..., and finally the empty string - all
1785 ;; concatenated to the local directory name
1786
1787 ;; This is inefficient for very long filenames, pity
1788 ;; `reduce' is not available...
1789 (car
1790 (apply
1791 'append
1792 (mapcar
1793 (lambda (x)
1794 (let ((cache-hit
1795 (tramp-get-file-property
1796 v
1797 (concat localname (substring filename 0 x))
1798 "file-name-all-completions"
1799 nil)))
1800 (when cache-hit (list cache-hit))))
1801 (tramp-compat-number-sequence (length filename) 0 -1)))))
1802
1803 ;; Cache expired or no matching cache entry found so we need
1804 ;; to perform a remote operation
1805 (let (result)
1806 ;; Get a list of directories and files, including reliably
1807 ;; tagging the directories with a trailing '/'. Because I
1808 ;; rock. --daniel@danann.net
1809
1810 ;; Changed to perform `cd' in the same remote op and only
1811 ;; get entries starting with `filename'. Capture any `cd'
1812 ;; error messages. Ensure any `cd' and `echo' aliases are
1813 ;; ignored.
1814 (tramp-send-command
1815 v
1816 (if (tramp-get-remote-perl v)
1817 (progn
1818 (tramp-maybe-send-script
1819 v tramp-perl-file-name-all-completions
1820 "tramp_perl_file_name_all_completions")
1821 (format "tramp_perl_file_name_all_completions %s %s %d"
1822 (tramp-shell-quote-argument localname)
1823 (tramp-shell-quote-argument filename)
1824 (if (symbol-value
1825 ;; `read-file-name-completion-ignore-case'
1826 ;; is introduced with Emacs 22.1.
1827 (if (boundp
1828 'read-file-name-completion-ignore-case)
1829 'read-file-name-completion-ignore-case
1830 'completion-ignore-case))
1831 1 0)))
1832
1833 (format (concat
1834 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1835 ;; `ls' with wildcard might fail with `Argument
1836 ;; list too long' error in some corner cases; if
1837 ;; `ls' fails after `cd' succeeded, chances are
1838 ;; that's the case, so let's retry without
1839 ;; wildcard. This will return "too many" entries
1840 ;; but that isn't harmful.
1841 " || %s -a 2>/dev/null)"
1842 " | while read f; do"
1843 " if %s -d \"$f\" 2>/dev/null;"
1844 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1845 " && \\echo ok) || \\echo fail")
1846 (tramp-shell-quote-argument localname)
1847 (tramp-get-ls-command v)
1848 ;; When `filename' is empty, just `ls' without
1849 ;; filename argument is more efficient than `ls *'
1850 ;; for very large directories and might avoid the
1851 ;; `Argument list too long' error.
1852 ;;
1853 ;; With and only with wildcard, we need to add
1854 ;; `-d' to prevent `ls' from descending into
1855 ;; sub-directories.
1856 (if (zerop (length filename))
1857 "."
1858 (concat (tramp-shell-quote-argument filename) "* -d"))
1859 (tramp-get-ls-command v)
1860 (tramp-get-test-command v))))
1861
1862 ;; Now grab the output.
1863 (with-current-buffer (tramp-get-buffer v)
1864 (goto-char (point-max))
1865
1866 ;; Check result code, found in last line of output
1867 (forward-line -1)
1868 (if (looking-at "^fail$")
1869 (progn
1870 ;; Grab error message from line before last line
1871 ;; (it was put there by `cd 2>&1')
1872 (forward-line -1)
1873 (tramp-error
1874 v 'file-error
1875 "tramp-handle-file-name-all-completions: %s"
1876 (buffer-substring
1877 (point) (tramp-compat-line-end-position))))
1878 ;; For peace of mind, if buffer doesn't end in `fail'
1879 ;; then it should end in `ok'. If neither are in the
1880 ;; buffer something went seriously wrong on the remote
1881 ;; side.
1882 (unless (looking-at "^ok$")
1883 (tramp-error
1884 v 'file-error
1885 "\
1886tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1887 (tramp-shell-quote-argument localname) (buffer-string))))
1888
1889 (while (zerop (forward-line -1))
1890 (push (buffer-substring
1891 (point) (tramp-compat-line-end-position))
1892 result)))
1893
1894 ;; Because the remote op went through OK we know the
1895 ;; directory we `cd'-ed to exists
1896 (tramp-set-file-property
1897 v localname "file-exists-p" t)
1898
1899 ;; Because the remote op went through OK we know every
1900 ;; file listed by `ls' exists.
1901 (mapc (lambda (entry)
1902 (tramp-set-file-property
1903 v (concat localname entry) "file-exists-p" t))
1904 result)
1905
1906 (tramp-set-file-property
1907 v localname "last-completion" (current-time))
1908
1909 ;; Store result in the cache
1910 (tramp-set-file-property
1911 v (concat localname filename)
1912 "file-name-all-completions"
1913 result))))))))
1914
1915(defun tramp-handle-file-name-completion
1916 (filename directory &optional predicate)
1917 "Like `file-name-completion' for Tramp files."
1918 (unless (tramp-tramp-file-p directory)
1919 (error
1920 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
1921 directory))
1922 (try-completion
1923 filename
1924 (mapcar 'list (file-name-all-completions filename directory))
1925 (when predicate
1926 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
1927
1928;; cp, mv and ln
1929
1930(defun tramp-handle-add-name-to-file
1931 (filename newname &optional ok-if-already-exists)
1932 "Like `add-name-to-file' for Tramp files."
1933 (unless (tramp-equal-remote filename newname)
1934 (with-parsed-tramp-file-name
1935 (if (tramp-tramp-file-p filename) filename newname) nil
1936 (tramp-error
1937 v 'file-error
1938 "add-name-to-file: %s"
1939 "only implemented for same method, same user, same host")))
1940 (with-parsed-tramp-file-name filename v1
1941 (with-parsed-tramp-file-name newname v2
1942 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1943 (when (and (not ok-if-already-exists)
1944 (file-exists-p newname)
1945 (not (numberp ok-if-already-exists))
1946 (y-or-n-p
1947 (format
1948 "File %s already exists; make it a new name anyway? "
1949 newname)))
1950 (tramp-error
1951 v2 'file-error
1952 "add-name-to-file: file %s already exists" newname))
1953 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1954 (tramp-flush-file-property v2 v2-localname)
1955 (tramp-barf-unless-okay
1956 v1
1957 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1958 (tramp-shell-quote-argument v2-localname))
1959 "error with add-name-to-file, see buffer `%s' for details"
1960 (buffer-name))))))
1961
1962(defun tramp-handle-copy-file
1963 (filename newname &optional ok-if-already-exists keep-date
1964 preserve-uid-gid preserve-selinux-context)
1965 "Like `copy-file' for Tramp files."
1966 (setq filename (expand-file-name filename))
1967 (setq newname (expand-file-name newname))
1968 (cond
1969 ;; At least one file a Tramp file?
1970 ((or (tramp-tramp-file-p filename)
1971 (tramp-tramp-file-p newname))
1972 (tramp-do-copy-or-rename-file
1973 'copy filename newname ok-if-already-exists keep-date
1974 preserve-uid-gid preserve-selinux-context))
1975 ;; Compat section.
1976 (preserve-selinux-context
1977 (tramp-run-real-handler
1978 'copy-file
1979 (list filename newname ok-if-already-exists keep-date
1980 preserve-uid-gid preserve-selinux-context)))
1981 (preserve-uid-gid
1982 (tramp-run-real-handler
1983 'copy-file
1984 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1985 (t
1986 (tramp-run-real-handler
1987 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1988
1989(defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
1990 "Like `copy-directory' for Tramp files."
1991 (let ((t1 (tramp-tramp-file-p dirname))
1992 (t2 (tramp-tramp-file-p newname)))
1993 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1994 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1995 ;; When DIRNAME and NEWNAME are remote, they must have
1996 ;; the same method.
1997 (or (null t1) (null t2)
1998 (string-equal
1999 (tramp-file-name-method (tramp-dissect-file-name dirname))
2000 (tramp-file-name-method (tramp-dissect-file-name newname)))))
2001 ;; scp or rsync DTRT.
2002 (progn
2003 (setq dirname (directory-file-name (expand-file-name dirname))
2004 newname (directory-file-name (expand-file-name newname)))
2005 (if (and (file-directory-p newname)
2006 (not (string-equal (file-name-nondirectory dirname)
2007 (file-name-nondirectory newname))))
2008 (setq newname
2009 (expand-file-name
2010 (file-name-nondirectory dirname) newname)))
2011 (if (not (file-directory-p (file-name-directory newname)))
2012 (make-directory (file-name-directory newname) parents))
2013 (tramp-do-copy-or-rename-file-out-of-band
2014 'copy dirname newname keep-date))
2015 ;; We must do it file-wise.
2016 (tramp-run-real-handler
2017 'copy-directory (list dirname newname keep-date parents)))
2018
2019 ;; When newname did exist, we have wrong cached values.
2020 (when t2
2021 (with-parsed-tramp-file-name newname nil
2022 (tramp-flush-file-property v (file-name-directory localname))
2023 (tramp-flush-file-property v localname))))))
2024
2025(defun tramp-handle-rename-file
2026 (filename newname &optional ok-if-already-exists)
2027 "Like `rename-file' for Tramp files."
2028 ;; Check if both files are local -- invoke normal rename-file.
2029 ;; Otherwise, use Tramp from local system.
2030 (setq filename (expand-file-name filename))
2031 (setq newname (expand-file-name newname))
2032 ;; At least one file a Tramp file?
2033 (if (or (tramp-tramp-file-p filename)
2034 (tramp-tramp-file-p newname))
2035 (tramp-do-copy-or-rename-file
2036 'rename filename newname ok-if-already-exists t t)
2037 (tramp-run-real-handler
2038 'rename-file (list filename newname ok-if-already-exists))))
2039
2040(defun tramp-do-copy-or-rename-file
2041 (op filename newname &optional ok-if-already-exists keep-date
2042 preserve-uid-gid preserve-selinux-context)
2043 "Copy or rename a remote file.
2044OP must be `copy' or `rename' and indicates the operation to perform.
2045FILENAME specifies the file to copy or rename, NEWNAME is the name of
2046the new file (for copy) or the new name of the file (for rename).
2047OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2048KEEP-DATE means to make sure that NEWNAME has the same timestamp
2049as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2050the uid and gid if both files are on the same host.
2051PRESERVE-SELINUX-CONTEXT activates selinux commands.
2052
2053This function is invoked by `tramp-handle-copy-file' and
2054`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2055and `rename'. FILENAME and NEWNAME must be absolute file names."
2056 (unless (memq op '(copy rename))
2057 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2058 (let ((t1 (tramp-tramp-file-p filename))
2059 (t2 (tramp-tramp-file-p newname))
2060 (context (and preserve-selinux-context
2061 (apply 'file-selinux-context (list filename))))
2062 pr tm)
2063
2064 (with-parsed-tramp-file-name (if t1 filename newname) nil
2065 (when (and (not ok-if-already-exists) (file-exists-p newname))
2066 (tramp-error
2067 v 'file-already-exists "File %s already exists" newname))
2068
2069 (with-progress-reporter
2070 v 0 (format "%s %s to %s"
2071 (if (eq op 'copy) "Copying" "Renaming")
2072 filename newname)
2073
2074 (cond
2075 ;; Both are Tramp files.
2076 ((and t1 t2)
2077 (with-parsed-tramp-file-name filename v1
2078 (with-parsed-tramp-file-name newname v2
2079 (cond
2080 ;; Shortcut: if method, host, user are the same for
2081 ;; both files, we invoke `cp' or `mv' on the remote
2082 ;; host directly.
2083 ((tramp-equal-remote filename newname)
2084 (tramp-do-copy-or-rename-file-directly
2085 op filename newname
2086 ok-if-already-exists keep-date preserve-uid-gid))
2087
2088 ;; Try out-of-band operation.
2089 ((tramp-method-out-of-band-p
2090 v1 (nth 7 (file-attributes filename)))
2091 (tramp-do-copy-or-rename-file-out-of-band
2092 op filename newname keep-date))
2093
2094 ;; No shortcut was possible. So we copy the file
2095 ;; first. If the operation was `rename', we go back
2096 ;; and delete the original file (if the copy was
2097 ;; successful). The approach is simple-minded: we
2098 ;; create a new buffer, insert the contents of the
2099 ;; source file into it, then write out the buffer to
2100 ;; the target file. The advantage is that it doesn't
2101 ;; matter which filename handlers are used for the
2102 ;; source and target file.
2103 (t
2104 (tramp-do-copy-or-rename-file-via-buffer
2105 op filename newname keep-date))))))
2106
2107 ;; One file is a Tramp file, the other one is local.
2108 ((or t1 t2)
2109 (cond
2110 ;; Fast track on local machine.
2111 ((tramp-local-host-p v)
2112 (tramp-do-copy-or-rename-file-directly
2113 op filename newname
2114 ok-if-already-exists keep-date preserve-uid-gid))
2115
2116 ;; If the Tramp file has an out-of-band method, the
2117 ;; corresponding copy-program can be invoked.
2118 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
2119 (tramp-do-copy-or-rename-file-out-of-band
2120 op filename newname keep-date))
2121
2122 ;; Use the inline method via a Tramp buffer.
2123 (t (tramp-do-copy-or-rename-file-via-buffer
2124 op filename newname keep-date))))
2125
2126 (t
2127 ;; One of them must be a Tramp file.
2128 (error "Tramp implementation says this cannot happen")))
2129
2130 ;; Handle `preserve-selinux-context'.
2131 (when context (apply 'set-file-selinux-context (list newname context)))
2132
2133 ;; In case of `rename', we must flush the cache of the source file.
2134 (when (and t1 (eq op 'rename))
2135 (with-parsed-tramp-file-name filename v1
2136 (tramp-flush-file-property v1 (file-name-directory localname))
2137 (tramp-flush-file-property v1 localname)))
2138
2139 ;; When newname did exist, we have wrong cached values.
2140 (when t2
2141 (with-parsed-tramp-file-name newname v2
2142 (tramp-flush-file-property v2 (file-name-directory localname))
2143 (tramp-flush-file-property v2 localname)))))))
2144
2145(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2146 "Use an Emacs buffer to copy or rename a file.
2147First arg OP is either `copy' or `rename' and indicates the operation.
2148FILENAME is the source file, NEWNAME the target file.
2149KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2150 (with-temp-buffer
2151 ;; We must disable multibyte, because binary data shall not be
2152 ;; converted.
2153 (set-buffer-multibyte nil)
2154 (let ((coding-system-for-read 'binary)
2155 (jka-compr-inhibit t))
2156 (insert-file-contents-literally filename))
2157 ;; We don't want the target file to be compressed, so we let-bind
2158 ;; `jka-compr-inhibit' to t.
2159 (let ((coding-system-for-write 'binary)
2160 (jka-compr-inhibit t))
2161 (write-region (point-min) (point-max) newname)))
2162 ;; KEEP-DATE handling.
2163 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2164 ;; Set the mode.
2165 (set-file-modes newname (tramp-default-file-modes filename))
2166 ;; If the operation was `rename', delete the original file.
2167 (unless (eq op 'copy) (delete-file filename)))
2168
2169(defun tramp-do-copy-or-rename-file-directly
2170 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2171 "Invokes `cp' or `mv' on the remote system.
2172OP must be one of `copy' or `rename', indicating `cp' or `mv',
2173respectively. FILENAME specifies the file to copy or rename,
2174NEWNAME is the name of the new file (for copy) or the new name of
2175the file (for rename). Both files must reside on the same host.
2176KEEP-DATE means to make sure that NEWNAME has the same timestamp
2177as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2178the uid and gid from FILENAME."
2179 (let ((t1 (tramp-tramp-file-p filename))
2180 (t2 (tramp-tramp-file-p newname))
2181 (file-times (nth 5 (file-attributes filename)))
2182 (file-modes (tramp-default-file-modes filename)))
2183 (with-parsed-tramp-file-name (if t1 filename newname) nil
2184 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2185 ((eq op 'copy) "cp -f")
2186 ((eq op 'rename) "mv -f")
2187 (t (tramp-error
2188 v 'file-error
2189 "Unknown operation `%s', must be `copy' or `rename'"
2190 op))))
2191 (localname1
2192 (if t1
2193 (tramp-file-name-handler 'file-remote-p filename 'localname)
2194 filename))
2195 (localname2
2196 (if t2
2197 (tramp-file-name-handler 'file-remote-p newname 'localname)
2198 newname))
2199 (prefix (file-remote-p (if t1 filename newname)))
2200 cmd-result)
2201
2202 (cond
2203 ;; Both files are on a remote host, with same user.
2204 ((and t1 t2)
2205 (setq cmd-result
2206 (tramp-send-command-and-check
2207 v (format "%s %s %s" cmd
2208 (tramp-shell-quote-argument localname1)
2209 (tramp-shell-quote-argument localname2))))
2210 (with-current-buffer (tramp-get-buffer v)
2211 (goto-char (point-min))
2212 (unless
2213 (or
2214 (and keep-date
2215 ;; Mask cp -f error.
2216 (re-search-forward
2217 tramp-operation-not-permitted-regexp nil t))
2218 cmd-result)
2219 (tramp-error-with-buffer
2220 nil v 'file-error
2221 "Copying directly failed, see buffer `%s' for details."
2222 (buffer-name)))))
2223
2224 ;; We are on the local host.
2225 ((or t1 t2)
2226 (cond
2227 ;; We can do it directly.
2228 ((let (file-name-handler-alist)
2229 (and (file-readable-p localname1)
2230 (file-writable-p (file-name-directory localname2))
2231 (or (file-directory-p localname2)
2232 (file-writable-p localname2))))
2233 (if (eq op 'copy)
2234 (tramp-compat-copy-file
2235 localname1 localname2 ok-if-already-exists
2236 keep-date preserve-uid-gid)
2237 (tramp-run-real-handler
2238 'rename-file (list localname1 localname2 ok-if-already-exists))))
2239
2240 ;; We can do it directly with `tramp-send-command'
2241 ((and (file-readable-p (concat prefix localname1))
2242 (file-writable-p
2243 (file-name-directory (concat prefix localname2)))
2244 (or (file-directory-p (concat prefix localname2))
2245 (file-writable-p (concat prefix localname2))))
2246 (tramp-do-copy-or-rename-file-directly
2247 op (concat prefix localname1) (concat prefix localname2)
2248 ok-if-already-exists keep-date t)
2249 ;; We must change the ownership to the local user.
2250 (tramp-set-file-uid-gid
2251 (concat prefix localname2)
2252 (tramp-get-local-uid 'integer)
2253 (tramp-get-local-gid 'integer)))
2254
2255 ;; We need a temporary file in between.
2256 (t
2257 ;; Create the temporary file.
2258 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2259 (unwind-protect
2260 (progn
2261 (cond
2262 (t1
2263 (tramp-barf-unless-okay
2264 v (format
2265 "%s %s %s" cmd
2266 (tramp-shell-quote-argument localname1)
2267 (tramp-shell-quote-argument tmpfile))
2268 "Copying directly failed, see buffer `%s' for details."
2269 (tramp-get-buffer v))
2270 ;; We must change the ownership as remote user.
2271 ;; Since this does not work reliable, we also
2272 ;; give read permissions.
2273 (set-file-modes
2274 (concat prefix tmpfile)
2275 (tramp-compat-octal-to-decimal "0777"))
2276 (tramp-set-file-uid-gid
2277 (concat prefix tmpfile)
2278 (tramp-get-local-uid 'integer)
2279 (tramp-get-local-gid 'integer)))
2280 (t2
2281 (if (eq op 'copy)
2282 (tramp-compat-copy-file
2283 localname1 tmpfile t
2284 keep-date preserve-uid-gid)
2285 (tramp-run-real-handler
2286 'rename-file
2287 (list localname1 tmpfile t)))
2288 ;; We must change the ownership as local user.
2289 ;; Since this does not work reliable, we also
2290 ;; give read permissions.
2291 (set-file-modes
2292 tmpfile (tramp-compat-octal-to-decimal "0777"))
2293 (tramp-set-file-uid-gid
2294 tmpfile
2295 (tramp-get-remote-uid v 'integer)
2296 (tramp-get-remote-gid v 'integer))))
2297
2298 ;; Move the temporary file to its destination.
2299 (cond
2300 (t2
2301 (tramp-barf-unless-okay
2302 v (format
2303 "cp -f -p %s %s"
2304 (tramp-shell-quote-argument tmpfile)
2305 (tramp-shell-quote-argument localname2))
2306 "Copying directly failed, see buffer `%s' for details."
2307 (tramp-get-buffer v)))
2308 (t1
2309 (tramp-run-real-handler
2310 'rename-file
2311 (list tmpfile localname2 ok-if-already-exists)))))
2312
2313 ;; Save exit.
2314 (condition-case nil
2315 (delete-file tmpfile)
2316 (error)))))))))
2317
2318 ;; Set the time and mode. Mask possible errors.
2319 (condition-case nil
2320 (when keep-date
2321 (set-file-times newname file-times)
2322 (set-file-modes newname file-modes))
2323 (error)))))
2324
2325(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2326 "Invoke rcp program to copy.
2327The method used must be an out-of-band method."
2328 (let ((t1 (tramp-tramp-file-p filename))
2329 (t2 (tramp-tramp-file-p newname))
2330 copy-program copy-args copy-env copy-keep-date port spec
2331 source target)
2332
2333 (with-parsed-tramp-file-name (if t1 filename newname) nil
2334 (if (and t1 t2)
2335
2336 ;; Both are Tramp files. We shall optimize it, when the
2337 ;; methods for filename and newname are the same.
2338 (let* ((dir-flag (file-directory-p filename))
2339 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2340 (if dir-flag
2341 (setq tmpfile
2342 (expand-file-name
2343 (file-name-nondirectory newname) tmpfile)))
2344 (unwind-protect
2345 (progn
2346 (tramp-do-copy-or-rename-file-out-of-band
2347 op filename tmpfile keep-date)
2348 (tramp-do-copy-or-rename-file-out-of-band
2349 'rename tmpfile newname keep-date))
2350 ;; Save exit.
2351 (condition-case nil
2352 (if dir-flag
2353 (tramp-compat-delete-directory
2354 (expand-file-name ".." tmpfile) 'recursive)
2355 (delete-file tmpfile))
2356 (error))))
2357
2358 ;; Expand hops. Might be necessary for gateway methods.
2359 (setq v (car (tramp-compute-multi-hops v)))
2360 (aset v 3 localname)
2361
2362 ;; Check which ones of source and target are Tramp files.
2363 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2364 target (funcall
2365 (if (and (file-directory-p filename)
2366 (string-equal
2367 (file-name-nondirectory filename)
2368 (file-name-nondirectory newname)))
2369 'file-name-directory
2370 'identity)
2371 (if t2 (tramp-make-copy-program-file-name v) newname)))
2372
2373 ;; Check for port number. Until now, there's no need for handling
2374 ;; like method, user, host.
2375 (setq host (tramp-file-name-real-host v)
2376 port (tramp-file-name-port v)
2377 port (or (and port (number-to-string port)) ""))
2378
2379 ;; Compose copy command.
2380 (setq spec (format-spec-make
2381 ?h host ?u user ?p port
2382 ?t (tramp-get-connection-property
2383 (tramp-get-connection-process v) "temp-file" "")
2384 ?k (if keep-date " " ""))
2385 copy-program (tramp-get-method-parameter
2386 method 'tramp-copy-program)
2387 copy-keep-date (tramp-get-method-parameter
2388 method 'tramp-copy-keep-date)
2389 copy-args
2390 (delq
2391 nil
2392 (mapcar
2393 (lambda (x)
2394 (setq
2395 x
2396 ;; " " is indication for keep-date argument.
2397 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
2398 (unless (member "" x) (mapconcat 'identity x " ")))
2399 (tramp-get-method-parameter method 'tramp-copy-args)))
2400 copy-env
2401 (delq
2402 nil
2403 (mapcar
2404 (lambda (x)
2405 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2406 (unless (member "" x) (mapconcat 'identity x " ")))
2407 (tramp-get-method-parameter method 'tramp-copy-env))))
2408
2409 ;; Check for program.
2410 (when (and (fboundp 'executable-find)
2411 (not (let ((default-directory
2412 (tramp-compat-temporary-file-directory)))
2413 (executable-find copy-program))))
2414 (tramp-error
2415 v 'file-error "Cannot find copy program: %s" copy-program))
2416
2417 ;; Set variables for computing the prompt for reading
2418 ;; password.
2419 (setq tramp-current-method (tramp-file-name-method v)
2420 tramp-current-user (tramp-file-name-user v)
2421 tramp-current-host (tramp-file-name-host v))
2422
2423 (unwind-protect
2424 (with-temp-buffer
2425 ;; The default directory must be remote.
2426 (let ((default-directory
2427 (file-name-directory (if t1 filename newname)))
2428 (process-environment (copy-sequence process-environment)))
2429 ;; Set the transfer process properties.
2430 (tramp-set-connection-property
2431 v "process-name" (buffer-name (current-buffer)))
2432 (tramp-set-connection-property
2433 v "process-buffer" (current-buffer))
2434 (while copy-env
2435 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2436 (setenv (pop copy-env) (pop copy-env)))
2437
2438 ;; Use an asynchronous process. By this, password can
2439 ;; be handled. The default directory must be local, in
2440 ;; order to apply the correct `copy-program'. We don't
2441 ;; set a timeout, because the copying of large files can
2442 ;; last longer than 60 secs.
2443 (let ((p (let ((default-directory
2444 (tramp-compat-temporary-file-directory)))
2445 (apply 'start-process
2446 (tramp-get-connection-property
2447 v "process-name" nil)
2448 (tramp-get-connection-property
2449 v "process-buffer" nil)
2450 copy-program
2451 (append copy-args (list source target))))))
2452 (tramp-message
2453 v 6 "%s" (mapconcat 'identity (process-command p) " "))
2454 (tramp-set-process-query-on-exit-flag p nil)
2455 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
2456
2457 ;; Reset the transfer process properties.
2458 (tramp-set-connection-property v "process-name" nil)
2459 (tramp-set-connection-property v "process-buffer" nil))
2460
2461 ;; Handle KEEP-DATE argument.
2462 (when (and keep-date (not copy-keep-date))
2463 (set-file-times newname (nth 5 (file-attributes filename))))
2464
2465 ;; Set the mode.
2466 (unless (and keep-date copy-keep-date)
2467 (ignore-errors
2468 (set-file-modes newname (tramp-default-file-modes filename)))))
2469
2470 ;; If the operation was `rename', delete the original file.
2471 (unless (eq op 'copy)
2472 (if (file-regular-p filename)
2473 (delete-file filename)
2474 (tramp-compat-delete-directory filename 'recursive))))))
2475
2476(defun tramp-handle-make-directory (dir &optional parents)
2477 "Like `make-directory' for Tramp files."
2478 (setq dir (expand-file-name dir))
2479 (with-parsed-tramp-file-name dir nil
2480 (tramp-flush-directory-property v (file-name-directory localname))
2481 (save-excursion
2482 (tramp-barf-unless-okay
2483 v (format "%s %s"
2484 (if parents "mkdir -p" "mkdir")
2485 (tramp-shell-quote-argument localname))
2486 "Couldn't make directory %s" dir))))
2487
2488(defun tramp-handle-delete-directory (directory &optional recursive)
2489 "Like `delete-directory' for Tramp files."
2490 (setq directory (expand-file-name directory))
2491 (with-parsed-tramp-file-name directory nil
2492 (tramp-flush-file-property v (file-name-directory localname))
2493 (tramp-flush-directory-property v localname)
2494 (tramp-barf-unless-okay
2495 v (format "%s %s"
2496 (if recursive "rm -rf" "rmdir")
2497 (tramp-shell-quote-argument localname))
2498 "Couldn't delete %s" directory)))
2499
2500(defun tramp-handle-delete-file (filename &optional trash)
2501 "Like `delete-file' for Tramp files."
2502 (setq filename (expand-file-name filename))
2503 (with-parsed-tramp-file-name filename nil
2504 (tramp-flush-file-property v (file-name-directory localname))
2505 (tramp-flush-file-property v localname)
2506 (tramp-barf-unless-okay
2507 v (format "%s %s"
2508 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2509 (tramp-shell-quote-argument localname))
2510 "Couldn't delete %s" filename)))
2511
2512;; Dired.
2513
2514;; CCC: This does not seem to be enough. Something dies when
2515;; we try and delete two directories under Tramp :/
2516(defun tramp-handle-dired-recursive-delete-directory (filename)
2517 "Recursively delete the directory given.
2518This is like `dired-recursive-delete-directory' for Tramp files."
2519 (with-parsed-tramp-file-name filename nil
2520 ;; Run a shell command 'rm -r <localname>'
2521 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2522 (unless (file-exists-p filename)
2523 (tramp-error v 'file-error "No such directory: %s" filename))
2524 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
2525 (tramp-send-command
2526 v
2527 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2528 ;; Don't read the output, do it explicitely.
2529 nil t)
2530 ;; Wait for the remote system to return to us...
2531 ;; This might take a while, allow it plenty of time.
2532 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2533 ;; Make sure that it worked...
2534 (tramp-flush-file-property v (file-name-directory localname))
2535 (tramp-flush-directory-property v localname)
2536 (and (file-exists-p filename)
2537 (tramp-error
2538 v 'file-error "Failed to recursively delete %s" filename))))
2539
2540(defun tramp-handle-dired-compress-file (file &rest ok-flag)
2541 "Like `dired-compress-file' for Tramp files."
2542 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2543 ;; Code stolen mainly from dired-aux.el.
2544 (with-parsed-tramp-file-name file nil
2545 (tramp-flush-file-property v localname)
2546 (save-excursion
2547 (let ((suffixes
2548 (if (not (featurep 'xemacs))
2549 ;; Emacs case
2550 (symbol-value 'dired-compress-file-suffixes)
2551 ;; XEmacs has `dired-compression-method-alist', which is
2552 ;; transformed into `dired-compress-file-suffixes' structure.
2553 (mapcar
2554 (lambda (x)
2555 (list (concat (regexp-quote (nth 1 x)) "\\'")
2556 nil
2557 (mapconcat 'identity (nth 3 x) " ")))
2558 (symbol-value 'dired-compression-method-alist))))
2559 suffix)
2560 ;; See if any suffix rule matches this file name.
2561 (while suffixes
2562 (let (case-fold-search)
2563 (if (string-match (car (car suffixes)) localname)
2564 (setq suffix (car suffixes) suffixes nil))
2565 (setq suffixes (cdr suffixes))))
2566
2567 (cond ((file-symlink-p file)
2568 nil)
2569 ((and suffix (nth 2 suffix))
2570 ;; We found an uncompression rule.
2571 (with-progress-reporter v 0 (format "Uncompressing %s" file)
2572 (when (tramp-send-command-and-check
2573 v (concat (nth 2 suffix) " "
2574 (tramp-shell-quote-argument localname)))
2575 ;; `dired-remove-file' is not defined in XEmacs.
2576 (tramp-compat-funcall 'dired-remove-file file)
2577 (string-match (car suffix) file)
2578 (concat (substring file 0 (match-beginning 0))))))
2579 (t
2580 ;; We don't recognize the file as compressed, so compress it.
2581 ;; Try gzip.
2582 (with-progress-reporter v 0 (format "Compressing %s" file)
2583 (when (tramp-send-command-and-check
2584 v (concat "gzip -f "
2585 (tramp-shell-quote-argument localname)))
2586 ;; `dired-remove-file' is not defined in XEmacs.
2587 (tramp-compat-funcall 'dired-remove-file file)
2588 (cond ((file-exists-p (concat file ".gz"))
2589 (concat file ".gz"))
2590 ((file-exists-p (concat file ".z"))
2591 (concat file ".z"))
2592 (t nil))))))))))
2593
2594(defun tramp-handle-dired-uncache (dir &optional dir-p)
2595 "Like `dired-uncache' for Tramp files."
2596 ;; DIR-P is valid for XEmacs only.
2597 (with-parsed-tramp-file-name
2598 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
2599 (tramp-flush-directory-property v localname)))
2600
2601(defun tramp-handle-insert-directory
2602 (filename switches &optional wildcard full-directory-p)
2603 "Like `insert-directory' for Tramp files."
2604 (setq filename (expand-file-name filename))
2605 (with-parsed-tramp-file-name filename nil
2606 (if (and (featurep 'ls-lisp)
2607 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2608 (tramp-run-real-handler
2609 'insert-directory (list filename switches wildcard full-directory-p))
2610 (when (stringp switches)
2611 (setq switches (split-string switches)))
2612 (when (and (member "--dired" switches)
2613 (not (tramp-get-ls-command-with-dired v)))
2614 (setq switches (delete "--dired" switches)))
2615 (when wildcard
2616 (setq wildcard (tramp-run-real-handler
2617 'file-name-nondirectory (list localname)))
2618 (setq localname (tramp-run-real-handler
2619 'file-name-directory (list localname))))
2620 (unless full-directory-p
2621 (setq switches (add-to-list 'switches "-d" 'append)))
2622 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2623 (when wildcard
2624 (setq switches (concat switches " " wildcard)))
2625 (tramp-message
2626 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2627 switches filename (if wildcard "yes" "no")
2628 (if full-directory-p "yes" "no"))
2629 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2630 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2631 (if full-directory-p
2632 (tramp-send-command
2633 v
2634 (format "%s %s %s 2>/dev/null"
2635 (tramp-get-ls-command v)
2636 switches
2637 (if wildcard
2638 localname
2639 (tramp-shell-quote-argument (concat localname ".")))))
2640 (tramp-barf-unless-okay
2641 v
2642 (format "cd %s" (tramp-shell-quote-argument
2643 (tramp-run-real-handler
2644 'file-name-directory (list localname))))
2645 "Couldn't `cd %s'"
2646 (tramp-shell-quote-argument
2647 (tramp-run-real-handler 'file-name-directory (list localname))))
2648 (tramp-send-command
2649 v
2650 (format "%s %s %s"
2651 (tramp-get-ls-command v)
2652 switches
2653 (if (or wildcard
2654 (zerop (length
2655 (tramp-run-real-handler
2656 'file-name-nondirectory (list localname)))))
2657 ""
2658 (tramp-shell-quote-argument
2659 (tramp-run-real-handler
2660 'file-name-nondirectory (list localname)))))))
2661 (let ((beg (point)))
2662 ;; We cannot use `insert-buffer-substring' because the Tramp
2663 ;; buffer changes its contents before insertion due to calling
2664 ;; `expand-file' and alike.
2665 (insert
2666 (with-current-buffer (tramp-get-buffer v)
2667 (buffer-string)))
2668
2669 ;; Check for "--dired" output.
2670 (forward-line -2)
2671 (when (looking-at "//SUBDIRED//")
2672 (forward-line -1))
2673 (when (looking-at "//DIRED//\\s-+")
2674 (let ((databeg (match-end 0))
2675 (end (tramp-compat-line-end-position)))
2676 ;; Now read the numeric positions of file names.
2677 (goto-char databeg)
2678 (while (< (point) end)
2679 (let ((start (+ beg (read (current-buffer))))
2680 (end (+ beg (read (current-buffer)))))
2681 (if (memq (char-after end) '(?\n ?\ ))
2682 ;; End is followed by \n or by " -> ".
2683 (put-text-property start end 'dired-filename t))))))
2684 ;; Remove trailing lines.
2685 (goto-char (tramp-compat-line-beginning-position))
2686 (while (looking-at "//")
2687 (forward-line 1)
2688 (delete-region (match-beginning 0) (point)))
2689
2690 ;; The inserted file could be from somewhere else.
2691 (when (and (not wildcard) (not full-directory-p))
2692 (goto-char (point-max))
2693 (when (file-symlink-p filename)
2694 (goto-char (search-backward "->" beg 'noerror)))
2695 (search-backward
2696 (if (zerop (length (file-name-nondirectory filename)))
2697 "."
2698 (file-name-nondirectory filename))
2699 beg 'noerror)
2700 (replace-match (file-relative-name filename) t))
2701
2702 (goto-char (point-max))))))
2703
2704(defun tramp-handle-unhandled-file-name-directory (filename)
2705 "Like `unhandled-file-name-directory' for Tramp files."
2706 ;; With Emacs 23, we could simply return `nil'. But we must keep it
2707 ;; for backward compatibility.
2708 (expand-file-name "~/"))
2709
2710;; Canonicalization of file names.
2711
2712(defun tramp-handle-expand-file-name (name &optional dir)
2713 "Like `expand-file-name' for Tramp files.
2714If the localname part of the given filename starts with \"/../\" then
2715the result will be a local, non-Tramp, filename."
2716 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2717 (setq dir (or dir default-directory "/"))
2718 ;; Unless NAME is absolute, concat DIR and NAME.
2719 (unless (file-name-absolute-p name)
2720 (setq name (concat (file-name-as-directory dir) name)))
2721 ;; If NAME is not a Tramp file, run the real handler.
2722 (if (not (tramp-connectable-p name))
2723 (tramp-run-real-handler 'expand-file-name (list name nil))
2724 ;; Dissect NAME.
2725 (with-parsed-tramp-file-name name nil
2726 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2727 (setq localname (concat "~/" localname)))
2728 ;; Tilde expansion if necessary. This needs a shell which
2729 ;; groks tilde expansion! The function `tramp-find-shell' is
2730 ;; supposed to find such a shell on the remote host. Please
2731 ;; tell me about it when this doesn't work on your system.
2732 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2733 (let ((uname (match-string 1 localname))
2734 (fname (match-string 2 localname)))
2735 ;; We cannot simply apply "~/", because under sudo "~/" is
2736 ;; expanded to the local user home directory but to the
2737 ;; root home directory. On the other hand, using always
2738 ;; the default user name for tilde expansion is not
2739 ;; appropriate either, because ssh and companions might
2740 ;; use a user name from the config file.
2741 (when (and (string-equal uname "~")
2742 (string-match "\\`su\\(do\\)?\\'" method))
2743 (setq uname (concat uname user)))
2744 (setq uname
2745 (with-connection-property v uname
2746 (tramp-send-command
2747 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2748 (with-current-buffer (tramp-get-buffer v)
2749 (goto-char (point-min))
2750 (buffer-substring
2751 (point) (tramp-compat-line-end-position)))))
2752 (setq localname (concat uname fname))))
2753 ;; There might be a double slash, for example when "~/"
2754 ;; expands to "/". Remove this.
2755 (while (string-match "//" localname)
2756 (setq localname (replace-match "/" t t localname)))
2757 ;; No tilde characters in file name, do normal
2758 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2759 ;; `directory-sep-char' here for XEmacs on Windows, which would
2760 ;; otherwise use backslash. `default-directory' is bound,
2761 ;; because on Windows there would be problems with UNC shares or
2762 ;; Cygwin mounts.
2763 (let ((directory-sep-char ?/)
2764 (default-directory (tramp-compat-temporary-file-directory)))
2765 (tramp-make-tramp-file-name
2766 method user host
2767 (tramp-drop-volume-letter
2768 (tramp-run-real-handler
2769 'expand-file-name (list localname))))))))
2770
2771(defun tramp-handle-substitute-in-file-name (filename)
2772 "Like `substitute-in-file-name' for Tramp files.
2773\"//\" and \"/~\" substitute only in the local filename part.
2774If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
2775beginning of local filename are not substituted."
2776 ;; First, we must replace environment variables.
2777 (setq filename (tramp-replace-environment-variables filename))
2778 (with-parsed-tramp-file-name filename nil
2779 (if (equal tramp-syntax 'url)
2780 ;; We need to check localname only. The other parts cannot contain
2781 ;; "//" or "/~".
2782 (if (and (> (length localname) 1)
2783 (or (string-match "//" localname)
2784 (string-match "/~" localname 1)))
2785 (tramp-run-real-handler 'substitute-in-file-name (list filename))
2786 (tramp-make-tramp-file-name
2787 (when method (substitute-in-file-name method))
2788 (when user (substitute-in-file-name user))
2789 (when host (substitute-in-file-name host))
2790 (when localname
2791 (tramp-run-real-handler
2792 'substitute-in-file-name (list localname)))))
2793 ;; Ignore in LOCALNAME everything before "//" or "/~".
2794 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
2795 (setq filename
2796 (concat (file-remote-p filename)
2797 (replace-match "\\1" nil nil localname)))
2798 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
2799 (when (string-match "~$" filename)
2800 (setq filename (concat filename "/"))))
2801 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
2802
2803;;; Remote commands:
2804
2805(defun tramp-handle-executable-find (command)
2806 "Like `executable-find' for Tramp files."
2807 (with-parsed-tramp-file-name default-directory nil
2808 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2809
2810(defun tramp-process-sentinel (proc event)
2811 "Flush file caches."
2812 (unless (memq (process-status proc) '(run open))
2813 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2814 (when vec
2815 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2816 (tramp-flush-directory-property vec "")))))
2817
2818;; We use BUFFER also as connection buffer during setup. Because of
2819;; this, its original contents must be saved, and restored once
2820;; connection has been setup.
2821(defun tramp-handle-start-file-process (name buffer program &rest args)
2822 "Like `start-file-process' for Tramp files."
2823 (with-parsed-tramp-file-name default-directory nil
2824 (unwind-protect
2825 ;; When PROGRAM is nil, we just provide a tty.
2826 (let ((command
2827 (when (stringp program)
2828 (format "cd %s; exec %s"
2829 (tramp-shell-quote-argument localname)
2830 (mapconcat 'tramp-shell-quote-argument
2831 (cons program args) " "))))
2832 (tramp-process-connection-type
2833 (or (null program) tramp-process-connection-type))
2834 (name1 name)
2835 (i 0))
2836 (unless buffer
2837 ;; BUFFER can be nil. We use a temporary buffer.
2838 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2839 (while (get-process name1)
2840 ;; NAME must be unique as process name.
2841 (setq i (1+ i)
2842 name1 (format "%s<%d>" name i)))
2843 (setq name name1)
2844 ;; Set the new process properties.
2845 (tramp-set-connection-property v "process-name" name)
2846 (tramp-set-connection-property v "process-buffer" buffer)
2847 ;; Activate narrowing in order to save BUFFER contents.
2848 ;; Clear also the modification time; otherwise we might be
2849 ;; interrupted by `verify-visited-file-modtime'.
2850 (with-current-buffer (tramp-get-connection-buffer v)
2851 (clear-visited-file-modtime)
2852 (narrow-to-region (point-max) (point-max)))
2853 (if command
2854 ;; Send the command.
2855 (tramp-send-command v command nil t) ; nooutput
2856 ;; Check, whether a pty is associated.
2857 (tramp-maybe-open-connection v)
2858 (unless (process-get (tramp-get-connection-process v) 'remote-tty)
2859 (tramp-error
2860 v 'file-error "pty association is not supported for `%s'" name)))
2861 (let ((p (tramp-get-connection-process v)))
2862 ;; Set sentinel and query flag for this process.
2863 (tramp-set-connection-property p "vector" v)
2864 (set-process-sentinel p 'tramp-process-sentinel)
2865 (tramp-set-process-query-on-exit-flag p t)
2866 ;; Return process.
2867 p))
2868 ;; Save exit.
2869 (with-current-buffer (tramp-get-connection-buffer v)
2870 (if (string-match tramp-temp-buffer-name (buffer-name))
2871 (progn
2872 (set-process-buffer (tramp-get-connection-process v) nil)
2873 (kill-buffer (current-buffer)))
2874 (widen)
2875 (goto-char (point-max))))
2876 (tramp-set-connection-property v "process-name" nil)
2877 (tramp-set-connection-property v "process-buffer" nil))))
2878
2879(defun tramp-handle-process-file
2880 (program &optional infile destination display &rest args)
2881 "Like `process-file' for Tramp files."
2882 ;; The implementation is not complete yet.
2883 (when (and (numberp destination) (zerop destination))
2884 (error "Implementation does not handle immediate return"))
2885
2886 (with-parsed-tramp-file-name default-directory nil
2887 (let (command input tmpinput stderr tmpstderr outbuf ret)
2888 ;; Compute command.
2889 (setq command (mapconcat 'tramp-shell-quote-argument
2890 (cons program args) " "))
2891 ;; Determine input.
2892 (if (null infile)
2893 (setq input "/dev/null")
2894 (setq infile (expand-file-name infile))
2895 (if (tramp-equal-remote default-directory infile)
2896 ;; INFILE is on the same remote host.
2897 (setq input (with-parsed-tramp-file-name infile nil localname))
2898 ;; INFILE must be copied to remote host.
2899 (setq input (tramp-make-tramp-temp-file v)
2900 tmpinput (tramp-make-tramp-file-name method user host input))
2901 (copy-file infile tmpinput t)))
2902 (when input (setq command (format "%s <%s" command input)))
2903
2904 ;; Determine output.
2905 (cond
2906 ;; Just a buffer.
2907 ((bufferp destination)
2908 (setq outbuf destination))
2909 ;; A buffer name.
2910 ((stringp destination)
2911 (setq outbuf (get-buffer-create destination)))
2912 ;; (REAL-DESTINATION ERROR-DESTINATION)
2913 ((consp destination)
2914 ;; output.
2915 (cond
2916 ((bufferp (car destination))
2917 (setq outbuf (car destination)))
2918 ((stringp (car destination))
2919 (setq outbuf (get-buffer-create (car destination))))
2920 ((car destination)
2921 (setq outbuf (current-buffer))))
2922 ;; stderr.
2923 (cond
2924 ((stringp (cadr destination))
2925 (setcar (cdr destination) (expand-file-name (cadr destination)))
2926 (if (tramp-equal-remote default-directory (cadr destination))
2927 ;; stderr is on the same remote host.
2928 (setq stderr (with-parsed-tramp-file-name
2929 (cadr destination) nil localname))
2930 ;; stderr must be copied to remote host. The temporary
2931 ;; file must be deleted after execution.
2932 (setq stderr (tramp-make-tramp-temp-file v)
2933 tmpstderr (tramp-make-tramp-file-name
2934 method user host stderr))))
2935 ;; stderr to be discarded.
2936 ((null (cadr destination))
2937 (setq stderr "/dev/null"))))
2938 ;; 't
2939 (destination
2940 (setq outbuf (current-buffer))))
2941 (when stderr (setq command (format "%s 2>%s" command stderr)))
2942
2943 ;; Send the command. It might not return in time, so we protect
2944 ;; it. Call it in a subshell, in order to preserve working
2945 ;; directory.
2946 (condition-case nil
2947 (unwind-protect
2948 (setq ret
2949 (if (tramp-send-command-and-check
2950 v (format "\\cd %s; %s"
2951 (tramp-shell-quote-argument localname)
2952 command)
2953 t t)
2954 0 1))
2955 ;; We should show the output anyway.
2956 (when outbuf
2957 (with-current-buffer outbuf
2958 (insert
2959 (with-current-buffer (tramp-get-connection-buffer v)
2960 (buffer-string))))
2961 (when display (display-buffer outbuf))))
2962 ;; When the user did interrupt, we should do it also. We use
2963 ;; return code -1 as marker.
2964 (quit
2965 (kill-buffer (tramp-get-connection-buffer v))
2966 (setq ret -1))
2967 ;; Handle errors.
2968 (error
2969 (kill-buffer (tramp-get-connection-buffer v))
2970 (setq ret 1)))
2971
2972 ;; Provide error file.
2973 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2974
2975 ;; Cleanup. We remove all file cache values for the connection,
2976 ;; because the remote process could have changed them.
2977 (when tmpinput (delete-file tmpinput))
2978
2979 ;; `process-file-side-effects' has been introduced with GNU
2980 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2981 ;; by `program'. If it doesn't exist, we assume its default
2982 ;; value 't'.
2983 (unless (and (boundp 'process-file-side-effects)
2984 (not (symbol-value 'process-file-side-effects)))
2985 (tramp-flush-directory-property v ""))
2986
2987 ;; Return exit status.
2988 (if (equal ret -1)
2989 (keyboard-quit)
2990 ret))))
2991
2992(defun tramp-handle-call-process-region
2993 (start end program &optional delete buffer display &rest args)
2994 "Like `call-process-region' for Tramp files."
2995 (let ((tmpfile (tramp-compat-make-temp-file "")))
2996 (write-region start end tmpfile)
2997 (when delete (delete-region start end))
2998 (unwind-protect
2999 (apply 'call-process program tmpfile buffer display args)
3000 (delete-file tmpfile))))
3001
3002(defun tramp-handle-shell-command
3003 (command &optional output-buffer error-buffer)
3004 "Like `shell-command' for Tramp files."
3005 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3006 ;; We cannot use `shell-file-name' and `shell-command-switch',
3007 ;; they are variables of the local host.
3008 (args (list
3009 (tramp-get-method-parameter
3010 (tramp-file-name-method
3011 (tramp-dissect-file-name default-directory))
3012 'tramp-remote-sh)
3013 "-c" (substring command 0 asynchronous)))
3014 current-buffer-p
3015 (output-buffer
3016 (cond
3017 ((bufferp output-buffer) output-buffer)
3018 ((stringp output-buffer) (get-buffer-create output-buffer))
3019 (output-buffer
3020 (setq current-buffer-p t)
3021 (current-buffer))
3022 (t (get-buffer-create
3023 (if asynchronous
3024 "*Async Shell Command*"
3025 "*Shell Command Output*")))))
3026 (error-buffer
3027 (cond
3028 ((bufferp error-buffer) error-buffer)
3029 ((stringp error-buffer) (get-buffer-create error-buffer))))
3030 (buffer
3031 (if (and (not asynchronous) error-buffer)
3032 (with-parsed-tramp-file-name default-directory nil
3033 (list output-buffer (tramp-make-tramp-temp-file v)))
3034 output-buffer))
3035 (p (get-buffer-process output-buffer)))
3036
3037 ;; Check whether there is another process running. Tramp does not
3038 ;; support 2 (asynchronous) processes in parallel.
3039 (when p
3040 (if (yes-or-no-p "A command is running. Kill it? ")
3041 (ignore-errors (kill-process p))
3042 (error "Shell command in progress")))
3043
3044 (if current-buffer-p
3045 (progn
3046 (barf-if-buffer-read-only)
3047 (push-mark nil t))
3048 (with-current-buffer output-buffer
3049 (setq buffer-read-only nil)
3050 (erase-buffer)))
3051
3052 (if (and (not current-buffer-p) (integerp asynchronous))
3053 (prog1
3054 ;; Run the process.
3055 (apply 'start-file-process "*Async Shell*" buffer args)
3056 ;; Display output.
3057 (pop-to-buffer output-buffer)
3058 (setq mode-line-process '(":%s"))
3059 (shell-mode))
3060
3061 (prog1
3062 ;; Run the process.
3063 (apply 'process-file (car args) nil buffer nil (cdr args))
3064 ;; Insert error messages if they were separated.
3065 (when (listp buffer)
3066 (with-current-buffer error-buffer
3067 (insert-file-contents (cadr buffer)))
3068 (delete-file (cadr buffer)))
3069 (if current-buffer-p
3070 ;; This is like exchange-point-and-mark, but doesn't
3071 ;; activate the mark. It is cleaner to avoid activation,
3072 ;; even though the command loop would deactivate the mark
3073 ;; because we inserted text.
3074 (goto-char (prog1 (mark t)
3075 (set-marker (mark-marker) (point)
3076 (current-buffer))))
3077 ;; There's some output, display it.
3078 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
3079 (if (functionp 'display-message-or-buffer)
3080 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
3081 (pop-to-buffer output-buffer))))))))
3082
3083(defun tramp-handle-file-local-copy (filename)
3084 "Like `file-local-copy' for Tramp files."
3085
3086 (with-parsed-tramp-file-name filename nil
3087 (unless (file-exists-p filename)
3088 (tramp-error
3089 v 'file-error
3090 "Cannot make local copy of non-existing file `%s'" filename))
3091
3092 (let* ((size (nth 7 (file-attributes filename)))
3093 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
3094 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
3095 (tmpfile (tramp-compat-make-temp-file filename)))
3096
3097 (condition-case err
3098 (cond
3099 ;; `copy-file' handles direct copy and out-of-band methods.
3100 ((or (tramp-local-host-p v)
3101 (tramp-method-out-of-band-p v size))
3102 (copy-file filename tmpfile t t))
3103
3104 ;; Use inline encoding for file transfer.
3105 (rem-enc
3106 (save-excursion
3107 (with-progress-reporter
3108 v 3 (format "Encoding remote file %s" filename)
3109 (tramp-barf-unless-okay
3110 v (format rem-enc (tramp-shell-quote-argument localname))
3111 "Encoding remote file failed"))
3112
3113 (if (functionp loc-dec)
3114 ;; If local decoding is a function, we call it. We
3115 ;; must disable multibyte, because
3116 ;; `uudecode-decode-region' doesn't handle it
3117 ;; correctly.
3118 (with-temp-buffer
3119 (set-buffer-multibyte nil)
3120 (insert-buffer-substring (tramp-get-buffer v))
3121 (with-progress-reporter
3122 v 3 (format "Decoding remote file %s with function %s"
3123 filename loc-dec)
3124 (funcall loc-dec (point-min) (point-max))
3125 ;; Unset `file-name-handler-alist'. Otherwise,
3126 ;; epa-file gets confused.
3127 (let (file-name-handler-alist
3128 (coding-system-for-write 'binary))
3129 (write-region (point-min) (point-max) tmpfile))))
3130
3131 ;; If tramp-decoding-function is not defined for this
3132 ;; method, we invoke tramp-decoding-command instead.
3133 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
3134 ;; Unset `file-name-handler-alist'. Otherwise,
3135 ;; epa-file gets confused.
3136 (let (file-name-handler-alist
3137 (coding-system-for-write 'binary))
3138 (write-region (point-min) (point-max) tmpfile2))
3139 (with-progress-reporter
3140 v 3 (format "Decoding remote file %s with command %s"
3141 filename loc-dec)
3142 (unwind-protect
3143 (tramp-call-local-coding-command
3144 loc-dec tmpfile2 tmpfile)
3145 (delete-file tmpfile2)))))
3146
3147 ;; Set proper permissions.
3148 (set-file-modes tmpfile (tramp-default-file-modes filename))
3149 ;; Set local user ownership.
3150 (tramp-set-file-uid-gid tmpfile)))
3151
3152 ;; Oops, I don't know what to do.
3153 (t (tramp-error
3154 v 'file-error "Wrong method specification for `%s'" method)))
3155
3156 ;; Error handling.
3157 ((error quit)
3158 (delete-file tmpfile)
3159 (signal (car err) (cdr err))))
3160
3161 (run-hooks 'tramp-handle-file-local-copy-hook)
3162 tmpfile)))
3163
3164(defun tramp-handle-file-remote-p (filename &optional identification connected)
3165 "Like `file-remote-p' for Tramp files."
3166 (let ((tramp-verbose 3))
3167 (when (tramp-tramp-file-p filename)
3168 (let* ((v (tramp-dissect-file-name filename))
3169 (p (tramp-get-connection-process v))
3170 (c (and p (processp p) (memq (process-status p) '(run open)))))
3171 ;; We expand the file name only, if there is already a connection.
3172 (with-parsed-tramp-file-name
3173 (if c (expand-file-name filename) filename) nil
3174 (and (or (not connected) c)
3175 (cond
3176 ((eq identification 'method) method)
3177 ((eq identification 'user) user)
3178 ((eq identification 'host) host)
3179 ((eq identification 'localname) localname)
3180 (t (tramp-make-tramp-file-name method user host "")))))))))
3181
3182(defun tramp-handle-insert-file-contents
3183 (filename &optional visit beg end replace)
3184 "Like `insert-file-contents' for Tramp files."
3185 (barf-if-buffer-read-only)
3186 (setq filename (expand-file-name filename))
3187 (let (result local-copy remote-copy)
3188 (with-parsed-tramp-file-name filename nil
3189 (unwind-protect
3190 (if (not (file-exists-p filename))
3191 ;; We don't raise a Tramp error, because it might be
3192 ;; suppressed, like in `find-file-noselect-1'.
3193 (signal 'file-error
3194 (list "File not found on remote host" filename))
3195
3196 (if (and (tramp-local-host-p v)
3197 (let (file-name-handler-alist)
3198 (file-readable-p localname)))
3199 ;; Short track: if we are on the local host, we can
3200 ;; run directly.
3201 (setq result
3202 (tramp-run-real-handler
3203 'insert-file-contents
3204 (list localname visit beg end replace)))
3205
3206 ;; When we shall insert only a part of the file, we copy
3207 ;; this part.
3208 (when (or beg end)
3209 (setq remote-copy (tramp-make-tramp-temp-file v))
3210 (tramp-send-command
3211 v
3212 (cond
3213 ((and beg end)
3214 (format "tail -c +%d %s | head -c +%d >%s"
3215 (1+ beg) (tramp-shell-quote-argument localname)
3216 (- end beg) remote-copy))
3217 (beg
3218 (format "tail -c +%d %s >%s"
3219 (1+ beg) (tramp-shell-quote-argument localname)
3220 remote-copy))
3221 (end
3222 (format "head -c +%d %s >%s"
3223 (1+ end) (tramp-shell-quote-argument localname)
3224 remote-copy)))))
3225
3226 ;; `insert-file-contents-literally' takes care to avoid
3227 ;; calling jka-compr. By let-binding
3228 ;; `inhibit-file-name-operation', we propagate that care
3229 ;; to the `file-local-copy' operation.
3230 (setq local-copy
3231 (let ((inhibit-file-name-operation
3232 (when (eq inhibit-file-name-operation
3233 'insert-file-contents)
3234 'file-local-copy)))
3235 (cond
3236 ((stringp remote-copy)
3237 (file-local-copy
3238 (tramp-make-tramp-file-name
3239 method user host remote-copy)))
3240 ((stringp tramp-temp-buffer-file-name)
3241 (copy-file filename tramp-temp-buffer-file-name 'ok)
3242 tramp-temp-buffer-file-name)
3243 (t (file-local-copy filename)))))
3244
3245 ;; When the file is not readable for the owner, it
3246 ;; cannot be inserted, even it is redable for the group
3247 ;; or for everybody.
3248 (set-file-modes local-copy (tramp-compat-octal-to-decimal "0600"))
3249
3250 (when (and (null remote-copy)
3251 (tramp-get-method-parameter
3252 method 'tramp-copy-keep-tmpfile))
3253 ;; We keep the local file for performance reasons,
3254 ;; useful for "rsync".
3255 (setq tramp-temp-buffer-file-name local-copy)
3256 (put 'tramp-temp-buffer-file-name 'permanent-local t))
3257
3258 (with-progress-reporter
3259 v 3 (format "Inserting local temp file `%s'" local-copy)
3260 ;; We must ensure that `file-coding-system-alist'
3261 ;; matches `local-copy'.
3262 (let ((file-coding-system-alist
3263 (tramp-find-file-name-coding-system-alist
3264 filename local-copy)))
3265 (setq result
3266 (insert-file-contents
3267 local-copy nil nil nil replace))))))
3268
3269 ;; Save exit.
3270 (progn
3271 (when visit
3272 (setq buffer-file-name filename)
3273 (setq buffer-read-only (not (file-writable-p filename)))
3274 (set-visited-file-modtime)
3275 (set-buffer-modified-p nil))
3276 (when (and (stringp local-copy)
3277 (or remote-copy (null tramp-temp-buffer-file-name)))
3278 (delete-file local-copy))
3279 (when (stringp remote-copy)
3280 (delete-file
3281 (tramp-make-tramp-file-name method user host remote-copy))))))
3282
3283 ;; Result.
3284 (list (expand-file-name filename)
3285 (cadr result))))
3286
3287;; This is needed for XEmacs only. Code stolen from files.el.
3288(defun tramp-handle-insert-file-contents-literally
3289 (filename &optional visit beg end replace)
3290 "Like `insert-file-contents-literally' for Tramp files."
3291 (let ((format-alist nil)
3292 (after-insert-file-functions nil)
3293 (coding-system-for-read 'no-conversion)
3294 (coding-system-for-write 'no-conversion)
3295 (find-buffer-file-type-function
3296 (if (fboundp 'find-buffer-file-type)
3297 (symbol-function 'find-buffer-file-type)
3298 nil))
3299 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3300 (inhibit-file-name-operation 'insert-file-contents))
3301 (unwind-protect
3302 (progn
3303 (fset 'find-buffer-file-type (lambda (filename) t))
3304 (insert-file-contents filename visit beg end replace))
3305 ;; Save exit.
3306 (if find-buffer-file-type-function
3307 (fset 'find-buffer-file-type find-buffer-file-type-function)
3308 (fmakunbound 'find-buffer-file-type)))))
3309
3310(defun tramp-handle-find-backup-file-name (filename)
3311 "Like `find-backup-file-name' for Tramp files."
3312 (with-parsed-tramp-file-name filename nil
3313 ;; We set both variables. It doesn't matter whether it is
3314 ;; Emacs or XEmacs.
3315 (let ((backup-directory-alist
3316 ;; Emacs case.
3317 (when (boundp 'backup-directory-alist)
3318 (if (symbol-value 'tramp-backup-directory-alist)
3319 (mapcar
3320 (lambda (x)
3321 (cons
3322 (car x)
3323 (if (and (stringp (cdr x))
3324 (file-name-absolute-p (cdr x))
3325 (not (tramp-file-name-p (cdr x))))
3326 (tramp-make-tramp-file-name method user host (cdr x))
3327 (cdr x))))
3328 (symbol-value 'tramp-backup-directory-alist))
3329 (symbol-value 'backup-directory-alist))))
3330
3331 (bkup-backup-directory-info
3332 ;; XEmacs case.
3333 (when (boundp 'bkup-backup-directory-info)
3334 (if (symbol-value 'tramp-bkup-backup-directory-info)
3335 (mapcar
3336 (lambda (x)
3337 (nconc
3338 (list (car x))
3339 (list
3340 (if (and (stringp (car (cdr x)))
3341 (file-name-absolute-p (car (cdr x)))
3342 (not (tramp-file-name-p (car (cdr x)))))
3343 (tramp-make-tramp-file-name
3344 method user host (car (cdr x)))
3345 (car (cdr x))))
3346 (cdr (cdr x))))
3347 (symbol-value 'tramp-bkup-backup-directory-info))
3348 (symbol-value 'bkup-backup-directory-info)))))
3349
3350 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3351
3352(defun tramp-handle-make-auto-save-file-name ()
3353 "Like `make-auto-save-file-name' for Tramp files.
3354Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3355 (let ((tramp-auto-save-directory tramp-auto-save-directory)
3356 (buffer-file-name
3357 (tramp-subst-strs-in-string
3358 '(("_" . "|")
3359 ("/" . "_a")
3360 (":" . "_b")
3361 ("|" . "__")
3362 ("[" . "_l")
3363 ("]" . "_r"))
3364 (buffer-file-name))))
3365 ;; File name must be unique. This is ensured with Emacs 22 (see
3366 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3367 ;; all other cases we must do it ourselves.
3368 (when (boundp 'auto-save-file-name-transforms)
3369 (mapc
3370 (lambda (x)
3371 (when (and (string-match (car x) buffer-file-name)
3372 (not (car (cddr x))))
3373 (setq tramp-auto-save-directory
3374 (or tramp-auto-save-directory
3375 (tramp-compat-temporary-file-directory)))))
3376 (symbol-value 'auto-save-file-name-transforms)))
3377 ;; Create directory.
3378 (when tramp-auto-save-directory
3379 (setq buffer-file-name
3380 (expand-file-name buffer-file-name tramp-auto-save-directory))
3381 (unless (file-exists-p tramp-auto-save-directory)
3382 (make-directory tramp-auto-save-directory t)))
3383 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3384 ;; it is not a magic file name operation (since Emacs 22).
3385 ;; We must deactivate it temporarily.
3386 (if (not (ad-is-active 'make-auto-save-file-name))
3387 (tramp-run-real-handler 'make-auto-save-file-name nil)
3388 ;; else
3389 (ad-deactivate 'make-auto-save-file-name)
3390 (prog1
3391 (tramp-run-real-handler 'make-auto-save-file-name nil)
3392 (ad-activate 'make-auto-save-file-name)))))
3393
3394(defvar tramp-handle-write-region-hook nil
3395 "Normal hook to be run at the end of `tramp-handle-write-region'.")
3396
3397;; CCC grok LOCKNAME
3398(defun tramp-handle-write-region
3399 (start end filename &optional append visit lockname confirm)
3400 "Like `write-region' for Tramp files."
3401 (setq filename (expand-file-name filename))
3402 (with-parsed-tramp-file-name filename nil
3403 ;; Following part commented out because we don't know what to do about
3404 ;; file locking, and it does not appear to be a problem to ignore it.
3405 ;; Ange-ftp ignores it, too.
3406 ;; (when (and lockname (stringp lockname))
3407 ;; (setq lockname (expand-file-name lockname)))
3408 ;; (unless (or (eq lockname nil)
3409 ;; (string= lockname filename))
3410 ;; (error
3411 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3412
3413 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3414 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3415 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3416 (tramp-error v 'file-error "File not overwritten")))
3417
3418 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3419 (tramp-get-remote-uid v 'integer)))
3420 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3421 (tramp-get-remote-gid v 'integer))))
3422
3423 (if (and (tramp-local-host-p v)
3424 ;; `file-writable-p' calls `file-expand-file-name'. We
3425 ;; cannot use `tramp-run-real-handler' therefore.
3426 (let (file-name-handler-alist)
3427 (and
3428 (file-writable-p (file-name-directory localname))
3429 (or (file-directory-p localname)
3430 (file-writable-p localname)))))
3431 ;; Short track: if we are on the local host, we can run directly.
3432 (tramp-run-real-handler
3433 'write-region
3434 (list start end localname append 'no-message lockname confirm))
3435
3436 (let ((modes (save-excursion (tramp-default-file-modes filename)))
3437 ;; We use this to save the value of
3438 ;; `last-coding-system-used' after writing the tmp
3439 ;; file. At the end of the function, we set
3440 ;; `last-coding-system-used' to this saved value. This
3441 ;; way, any intermediary coding systems used while
3442 ;; talking to the remote shell or suchlike won't hose
3443 ;; this variable. This approach was snarfed from
3444 ;; ange-ftp.el.
3445 coding-system-used
3446 ;; Write region into a tmp file. This isn't really
3447 ;; needed if we use an encoding function, but currently
3448 ;; we use it always because this makes the logic
3449 ;; simpler.
3450 (tmpfile (or tramp-temp-buffer-file-name
3451 (tramp-compat-make-temp-file filename))))
3452
3453 ;; If `append' is non-nil, we copy the file locally, and let
3454 ;; the native `write-region' implementation do the job.
3455 (when append (copy-file filename tmpfile 'ok))
3456
3457 ;; We say `no-message' here because we don't want the
3458 ;; visited file modtime data to be clobbered from the temp
3459 ;; file. We call `set-visited-file-modtime' ourselves later
3460 ;; on. We must ensure that `file-coding-system-alist'
3461 ;; matches `tmpfile'.
3462 (let (file-name-handler-alist
3463 (file-coding-system-alist
3464 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3465 (condition-case err
3466 (tramp-run-real-handler
3467 'write-region
3468 (list start end tmpfile append 'no-message lockname confirm))
3469 ((error quit)
3470 (setq tramp-temp-buffer-file-name nil)
3471 (delete-file tmpfile)
3472 (signal (car err) (cdr err))))
3473
3474 ;; Now, `last-coding-system-used' has the right value. Remember it.
3475 (when (boundp 'last-coding-system-used)
3476 (setq coding-system-used
3477 (symbol-value 'last-coding-system-used))))
3478
3479 ;; The permissions of the temporary file should be set. If
3480 ;; filename does not exist (eq modes nil) it has been
3481 ;; renamed to the backup file. This case `save-buffer'
3482 ;; handles permissions.
3483 ;; Ensure, that it is still readable.
3484 (when modes
3485 (set-file-modes
3486 tmpfile
3487 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3488
3489 ;; This is a bit lengthy due to the different methods
3490 ;; possible for file transfer. First, we check whether the
3491 ;; method uses an rcp program. If so, we call it.
3492 ;; Otherwise, both encoding and decoding command must be
3493 ;; specified. However, if the method _also_ specifies an
3494 ;; encoding function, then that is used for encoding the
3495 ;; contents of the tmp file.
3496 (let* ((size (nth 7 (file-attributes tmpfile)))
3497 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3498 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3499 (cond
3500 ;; `copy-file' handles direct copy and out-of-band methods.
3501 ((or (tramp-local-host-p v)
3502 (tramp-method-out-of-band-p v size))
3503 (if (and (not (stringp start))
3504 (= (or end (point-max)) (point-max))
3505 (= (or start (point-min)) (point-min))
3506 (tramp-get-method-parameter
3507 method 'tramp-copy-keep-tmpfile))
3508 (progn
3509 (setq tramp-temp-buffer-file-name tmpfile)
3510 (condition-case err
3511 ;; We keep the local file for performance
3512 ;; reasons, useful for "rsync".
3513 (copy-file tmpfile filename t)
3514 ((error quit)
3515 (setq tramp-temp-buffer-file-name nil)
3516 (delete-file tmpfile)
3517 (signal (car err) (cdr err)))))
3518 (setq tramp-temp-buffer-file-name nil)
3519 ;; Don't rename, in order to keep context in SELinux.
3520 (unwind-protect
3521 (copy-file tmpfile filename t)
3522 (delete-file tmpfile))))
3523
3524 ;; Use inline file transfer.
3525 (rem-dec
3526 ;; Encode tmpfile.
3527 (unwind-protect
3528 (with-temp-buffer
3529 (set-buffer-multibyte nil)
3530 ;; Use encoding function or command.
3531 (if (functionp loc-enc)
3532 (with-progress-reporter
3533 v 3 (format "Encoding region using function `%s'"
3534 loc-enc)
3535 (let ((coding-system-for-read 'binary))
3536 (insert-file-contents-literally tmpfile))
3537 ;; The following `let' is a workaround for the
3538 ;; base64.el that comes with pgnus-0.84. If
3539 ;; both of the following conditions are
3540 ;; satisfied, it tries to write to a local
3541 ;; file in default-directory, but at this
3542 ;; point, default-directory is remote.
3543 ;; (`call-process-region' can't write to
3544 ;; remote files, it seems.) The file in
3545 ;; question is a tmp file anyway.
3546 (let ((default-directory
3547 (tramp-compat-temporary-file-directory)))
3548 (funcall loc-enc (point-min) (point-max))))
3549
3550 (with-progress-reporter
3551 v 3 (format "Encoding region using command `%s'"
3552 loc-enc)
3553 (unless (zerop (tramp-call-local-coding-command
3554 loc-enc tmpfile t))
3555 (tramp-error
3556 v 'file-error
3557 (concat "Cannot write to `%s', "
3558 "local encoding command `%s' failed")
3559 filename loc-enc))))
3560
3561 ;; Send buffer into remote decoding command which
3562 ;; writes to remote file. Because this happens on
3563 ;; the remote host, we cannot use the function.
3564 (with-progress-reporter
3565 v 3
3566 (format "Decoding region into remote file %s" filename)
3567 (goto-char (point-max))
3568 (unless (bolp) (newline))
3569 (tramp-send-command
3570 v
3571 (format
3572 (concat rem-dec " <<'EOF'\n%sEOF")
3573 (tramp-shell-quote-argument localname)
3574 (buffer-string)))
3575 (tramp-barf-unless-okay
3576 v nil
3577 "Couldn't write region to `%s', decode using `%s' failed"
3578 filename rem-dec)
3579 ;; When `file-precious-flag' is set, the region is
3580 ;; written to a temporary file. Check that the
3581 ;; checksum is equal to that from the local tmpfile.
3582 (when file-precious-flag
3583 (erase-buffer)
3584 (and
3585 ;; cksum runs locally, if possible.
3586 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3587 ;; cksum runs remotely.
3588 (tramp-send-command-and-check
3589 v
3590 (format
3591 "cksum <%s" (tramp-shell-quote-argument localname)))
3592 ;; ... they are different.
3593 (not
3594 (string-equal
3595 (buffer-string)
3596 (with-current-buffer (tramp-get-buffer v)
3597 (buffer-string))))
3598 (tramp-error
3599 v 'file-error
3600 (concat "Couldn't write region to `%s',"
3601 " decode using `%s' failed")
3602 filename rem-dec)))))
3603
3604 ;; Save exit.
3605 (delete-file tmpfile)))
3606
3607 ;; That's not expected.
3608 (t
3609 (tramp-error
3610 v 'file-error
3611 (concat "Method `%s' should specify both encoding and "
3612 "decoding command or an rcp program")
3613 method))))
3614
3615 ;; Make `last-coding-system-used' have the right value.
3616 (when coding-system-used
3617 (set 'last-coding-system-used coding-system-used))))
3618
3619 (tramp-flush-file-property v (file-name-directory localname))
3620 (tramp-flush-file-property v localname)
3621
3622 ;; We must protect `last-coding-system-used', now we have set it
3623 ;; to its correct value.
3624 (let (last-coding-system-used (need-chown t))
3625 ;; Set file modification time.
3626 (when (or (eq visit t) (stringp visit))
3627 (let ((file-attr (file-attributes filename)))
3628 (set-visited-file-modtime
3629 ;; We must pass modtime explicitely, because filename can
3630 ;; be different from (buffer-file-name), f.e. if
3631 ;; `file-precious-flag' is set.
3632 (nth 5 file-attr))
3633 (when (and (eq (nth 2 file-attr) uid)
3634 (eq (nth 3 file-attr) gid))
3635 (setq need-chown nil))))
3636
3637 ;; Set the ownership.
3638 (when need-chown
3639 (tramp-set-file-uid-gid filename uid gid))
3640 (when (or (eq visit t) (null visit) (stringp visit))
3641 (tramp-message v 0 "Wrote %s" filename))
3642 (run-hooks 'tramp-handle-write-region-hook)))))
3643
3644(defvar tramp-vc-registered-file-names nil
3645 "List used to collect file names, which are checked during `vc-registered'.")
3646
3647;; VC backends check for the existence of various different special
3648;; files. This is very time consuming, because every single check
3649;; requires a remote command (the file cache must be invalidated).
3650;; Therefore, we apply a kind of optimization. We install the file
3651;; name handler `tramp-vc-file-name-handler', which does nothing but
3652;; remembers all file names for which `file-exists-p' or
3653;; `file-readable-p' has been applied. A first run of `vc-registered'
3654;; is performed. Afterwards, a script is applied for all collected
3655;; file names, using just one remote command. The result of this
3656;; script is used to fill the file cache with actual values. Now we
3657;; can reset the file name handlers, and we make a second run of
3658;; `vc-registered', which returns the expected result without sending
3659;; any other remote command.
3660(defun tramp-handle-vc-registered (file)
3661 "Like `vc-registered' for Tramp files."
3662 (with-temp-message ""
3663 (with-parsed-tramp-file-name file nil
3664 (with-progress-reporter
3665 v 3 (format "Checking `vc-registered' for %s" file)
3666
3667 ;; There could be new files, created by the vc backend. We
3668 ;; cannot reuse the old cache entries, therefore.
3669 (let (tramp-vc-registered-file-names
3670 (tramp-cache-inhibit-cache (current-time))
3671 (file-name-handler-alist
3672 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3673
3674 ;; Here we collect only file names, which need an operation.
3675 (tramp-run-real-handler 'vc-registered (list file))
3676 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3677
3678 ;; Send just one command, in order to fill the cache.
3679 (when tramp-vc-registered-file-names
3680 (tramp-maybe-send-script
3681 v
3682 (format tramp-vc-registered-read-file-names
3683 (tramp-get-file-exists-command v)
3684 (format "%s -r" (tramp-get-test-command v)))
3685 "tramp_vc_registered_read_file_names")
3686
3687 (dolist
3688 (elt
3689 (tramp-send-command-and-read
3690 v
3691 (format
3692 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3693 (mapconcat 'tramp-shell-quote-argument
3694 tramp-vc-registered-file-names
3695 "\n"))))
3696
3697 (tramp-set-file-property
3698 v (car elt) (cadr elt) (cadr (cdr elt))))))
3699
3700 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3701 ;; calls shall be answered from the file cache. We unset
3702 ;; `process-file-side-effects' in order to keep the cache when
3703 ;; `process-file' calls appear.
3704 (let (process-file-side-effects)
3705 (tramp-run-real-handler 'vc-registered (list file)))))))
3706
3707;;;###tramp-autoload
3708(defun tramp-sh-file-name-handler (operation &rest args)
3709 "Invoke remote-shell Tramp file name handler.
3710Fall back to normal file name handler if no Tramp handler exists."
3711 (when (and tramp-locked (not tramp-locker))
3712 (setq tramp-locked nil)
3713 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3714 (let ((tl tramp-locked))
3715 (unwind-protect
3716 (progn
3717 (setq tramp-locked t)
3718 (let ((tramp-locker t))
3719 (save-match-data
3720 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3721 (if fn
3722 (apply (cdr fn) args)
3723 (tramp-run-real-handler operation args))))))
3724 (setq tramp-locked tl))))
3725
3726(defun tramp-vc-file-name-handler (operation &rest args)
3727 "Invoke special file name handler, which collects files to be handled."
3728 (save-match-data
3729 (let ((filename
3730 (tramp-replace-environment-variables
3731 (apply 'tramp-file-name-for-operation operation args)))
3732 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3733 (with-parsed-tramp-file-name filename nil
3734 (cond
3735 ;; That's what we want: file names, for which checks are
3736 ;; applied. We assume, that VC uses only `file-exists-p' and
3737 ;; `file-readable-p' checks; otherwise we must extend the
3738 ;; list. We do not perform any action, but return nil, in
3739 ;; order to keep `vc-registered' running.
3740 ((and fn (memq operation '(file-exists-p file-readable-p)))
3741 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3742 nil)
3743 ;; Tramp file name handlers like `expand-file-name'. They
3744 ;; must still work.
3745 (fn
3746 (save-match-data (apply (cdr fn) args)))
3747 ;; Default file name handlers, we don't care.
3748 (t (tramp-run-real-handler operation args)))))))
3749
3750;;; Internal Functions:
3751
3752(defun tramp-maybe-send-script (vec script name)
3753 "Define in remote shell function NAME implemented as SCRIPT.
3754Only send the definition if it has not already been done."
3755 (let* ((p (tramp-get-connection-process vec))
3756 (scripts (tramp-get-connection-property p "scripts" nil)))
3757 (unless (member name scripts)
3758 (with-progress-reporter vec 5 (format "Sending script `%s'" name)
3759 ;; The script could contain a call of Perl. This is masked with `%s'.
3760 (tramp-barf-unless-okay
3761 vec
3762 (format "%s () {\n%s\n}" name
3763 (format script (tramp-get-remote-perl vec)))
3764 "Script %s sending failed" name)
3765 (tramp-set-connection-property p "scripts" (cons name scripts))))))
3766
3767(defun tramp-set-auto-save ()
3768 (when (and ;; ange-ftp has its own auto-save mechanism
3769 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3770 'tramp-sh-file-name-handler)
3771 auto-save-default)
3772 (auto-save-mode 1)))
3773(add-hook 'find-file-hooks 'tramp-set-auto-save t)
3774(add-hook 'tramp-unload-hook
3775 (lambda ()
3776 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3777
3778(defun tramp-run-test (switch filename)
3779 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3780Returns the exit code of the `test' program."
3781 (with-parsed-tramp-file-name filename nil
3782 (tramp-send-command-and-check
3783 v
3784 (format
3785 "%s %s %s"
3786 (tramp-get-test-command v)
3787 switch
3788 (tramp-shell-quote-argument localname)))))
3789
3790(defun tramp-run-test2 (format-string file1 file2)
3791 "Run `test'-like program on the remote system, given FILE1, FILE2.
3792FORMAT-STRING contains the program name, switches, and place holders.
3793Returns the exit code of the `test' program. Barfs if the methods,
3794hosts, or files, disagree."
3795 (unless (tramp-equal-remote file1 file2)
3796 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3797 (tramp-error
3798 v 'file-error
3799 "tramp-run-test2 only implemented for same method, user, host")))
3800 (with-parsed-tramp-file-name file1 v1
3801 (with-parsed-tramp-file-name file1 v2
3802 (tramp-send-command-and-check
3803 v1
3804 (format format-string
3805 (tramp-shell-quote-argument v1-localname)
3806 (tramp-shell-quote-argument v2-localname))))))
3807
3808(defun tramp-find-executable
3809 (vec progname dirlist &optional ignore-tilde ignore-path)
3810 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3811First arg VEC specifies the connection, PROGNAME is the program
3812to search for, and DIRLIST gives the list of directories to
3813search. If IGNORE-TILDE is non-nil, directory names starting
3814with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3815only in DIRLIST.
3816
3817Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3818
3819This function expects to be in the right *tramp* buffer."
3820 (with-current-buffer (tramp-get-connection-buffer vec)
3821 (let (result)
3822 ;; Check whether the executable is in $PATH. "which(1)" does not
3823 ;; report always a correct error code; therefore we check the
3824 ;; number of words it returns.
3825 (unless ignore-path
3826 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3827 (goto-char (point-min))
3828 (if (looking-at "^\\s-*1$")
3829 (setq result (concat "\\" progname))))
3830 (unless result
3831 (when ignore-tilde
3832 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3833 ;; `remove' is in CL, and we want to avoid CL dependencies.
3834 (let (newdl d)
3835 (while dirlist
3836 (setq d (car dirlist))
3837 (setq dirlist (cdr dirlist))
3838 (unless (char-equal ?~ (aref d 0))
3839 (setq newdl (cons d newdl))))
3840 (setq dirlist (nreverse newdl))))
3841 (tramp-send-command
3842 vec
3843 (format (concat "while read d; "
3844 "do if test -x $d/%s -a -f $d/%s; "
3845 "then echo tramp_executable $d/%s; "
3846 "break; fi; done <<'EOF'\n"
3847 "%s\nEOF")
3848 progname progname progname (mapconcat 'identity dirlist "\n")))
3849 (goto-char (point-max))
3850 (when (search-backward "tramp_executable " nil t)
3851 (skip-chars-forward "^ ")
3852 (skip-chars-forward " ")
3853 (setq result (buffer-substring
3854 (point) (tramp-compat-line-end-position)))))
3855 result)))
3856
3857(defun tramp-set-remote-path (vec)
3858 "Sets the remote environment PATH to existing directories.
3859I.e., for each directory in `tramp-remote-path', it is tested
3860whether it exists and if so, it is added to the environment
3861variable PATH."
3862 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3863 (tramp-send-command
3864 vec (format "PATH=%s; export PATH"
3865 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3866
3867;; ------------------------------------------------------------
3868;; -- Communication with external shell --
3869;; ------------------------------------------------------------
3870
3871(defun tramp-find-file-exists-command (vec)
3872 "Find a command on the remote host for checking if a file exists.
3873Here, we are looking for a command which has zero exit status if the
3874file exists and nonzero exit status otherwise."
3875 (let ((existing "/")
3876 (nonexisting
3877 (tramp-shell-quote-argument "/ this file does not exist "))
3878 result)
3879 ;; The algorithm is as follows: we try a list of several commands.
3880 ;; For each command, we first run `$cmd /' -- this should return
3881 ;; true, as the root directory always exists. And then we run
3882 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3883 ;; does not exist. This should return false. We use the first
3884 ;; command we find that seems to work.
3885 ;; The list of commands to try is as follows:
3886 ;; `ls -d' This works on most systems, but NetBSD 1.4
3887 ;; has a bug: `ls' always returns zero exit
3888 ;; status, even for files which don't exist.
3889 ;; `test -e' Some Bourne shells have a `test' builtin
3890 ;; which does not know the `-e' option.
3891 ;; `/bin/test -e' For those, the `test' binary on disk normally
3892 ;; provides the option. Alas, the binary
3893 ;; is sometimes `/bin/test' and sometimes it's
3894 ;; `/usr/bin/test'.
3895 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3896 (unless (or
3897 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3898 (tramp-send-command-and-check
3899 vec (format "%s %s" result existing))
3900 (not (tramp-send-command-and-check
3901 vec (format "%s %s" result nonexisting))))
3902 (and (setq result "/bin/test -e")
3903 (tramp-send-command-and-check
3904 vec (format "%s %s" result existing))
3905 (not (tramp-send-command-and-check
3906 vec (format "%s %s" result nonexisting))))
3907 (and (setq result "/usr/bin/test -e")
3908 (tramp-send-command-and-check
3909 vec (format "%s %s" result existing))
3910 (not (tramp-send-command-and-check
3911 vec (format "%s %s" result nonexisting))))
3912 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3913 (tramp-send-command-and-check
3914 vec (format "%s %s" result existing))
3915 (not (tramp-send-command-and-check
3916 vec (format "%s %s" result nonexisting)))))
3917 (tramp-error
3918 vec 'file-error "Couldn't find command to check if file exists"))
3919 result))
3920
3921(defun tramp-open-shell (vec shell)
3922 "Opens shell SHELL."
3923 (with-progress-reporter vec 5 (format "Opening remote shell `%s'" shell)
3924 ;; Find arguments for this shell.
3925 (let ((tramp-end-of-output tramp-initial-end-of-output)
3926 (alist tramp-sh-extra-args)
3927 item extra-args)
3928 (while (and alist (null extra-args))
3929 (setq item (pop alist))
3930 (when (string-match (car item) shell)
3931 (setq extra-args (cdr item))))
3932 (when extra-args (setq shell (concat shell " " extra-args)))
3933 (tramp-send-command
3934 vec (format "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
3935 (shell-quote-argument tramp-end-of-output) shell)
3936 t))
3937 ;; Setting prompts.
3938 (tramp-send-command
3939 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
3940 (tramp-send-command vec "PS2=''" t)
3941 (tramp-send-command vec "PS3=''" t)
3942 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3943
3944(defun tramp-find-shell (vec)
3945 "Opens a shell on the remote host which groks tilde expansion."
3946 (unless (tramp-get-connection-property vec "remote-shell" nil)
3947 (let (shell)
3948 (with-current-buffer (tramp-get-buffer vec)
3949 (tramp-send-command vec "echo ~root" t)
3950 (cond
3951 ((or (string-match "^~root$" (buffer-string))
3952 ;; The default shell (ksh93) of OpenSolaris is buggy.
3953 (string-equal (tramp-get-connection-property vec "uname" "")
3954 "SunOS 5.11"))
3955 (setq shell
3956 (or (tramp-find-executable
3957 vec "bash" (tramp-get-remote-path vec) t t)
3958 (tramp-find-executable
3959 vec "ksh" (tramp-get-remote-path vec) t t)))
3960 (unless shell
3961 (tramp-error
3962 vec 'file-error
3963 "Couldn't find a shell which groks tilde expansion"))
3964 (tramp-message
3965 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3966 (tramp-open-shell vec shell))
3967
3968 (t (tramp-message
3969 vec 5 "Remote `%s' groks tilde expansion, good"
3970 (tramp-set-connection-property
3971 vec "remote-shell"
3972 (tramp-get-method-parameter
3973 (tramp-file-name-method vec) 'tramp-remote-sh)))))))))
3974
3975;; Utility functions.
3976
3977(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3978 "Wait for shell prompt and barf if none appears.
3979Looks at process PROC to see if a shell prompt appears in TIMEOUT
3980seconds. If not, it produces an error message with the given ERROR-ARGS."
3981 (unless
3982 (tramp-wait-for-regexp
3983 proc timeout
3984 (format
3985 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3986 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3987
3988(defun tramp-open-connection-setup-interactive-shell (proc vec)
3989 "Set up an interactive shell.
3990Mainly sets the prompt and the echo correctly. PROC is the shell
3991process to set up. VEC specifies the connection."
3992 (let ((tramp-end-of-output tramp-initial-end-of-output))
3993 ;; It is useful to set the prompt in the following command because
3994 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3995 ;; about and thus /bin/sh will display a strange prompt. For
3996 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3997 ;; display the current working directory but /bin/sh will display
3998 ;; a dollar sign. The following command line sets $PS1 to a sane
3999 ;; value, and works under Bourne-ish shells as well as csh-like
4000 ;; shells. Daniel Pittman reports that the unusual positioning of
4001 ;; the single quotes makes it work under `rc', too. We also unset
4002 ;; the variable $ENV because that is read by some sh
4003 ;; implementations (eg, bash when called as sh) on startup; this
4004 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
4005 ;; is another way to set the prompt in /bin/bash, it must be
4006 ;; discarded as well.
4007 (tramp-open-shell
4008 vec
4009 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-remote-sh))
4010
4011 ;; Disable echo.
4012 (tramp-message vec 5 "Setting up remote shell environment")
4013 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
4014 ;; Check whether the echo has really been disabled. Some
4015 ;; implementations, like busybox of embedded GNU/Linux, don't
4016 ;; support disabling.
4017 (tramp-send-command vec "echo foo" t)
4018 (with-current-buffer (process-buffer proc)
4019 (goto-char (point-min))
4020 (when (looking-at "echo foo")
4021 (tramp-set-connection-property proc "remote-echo" t)
4022 (tramp-message vec 5 "Remote echo still on. Ok.")
4023 ;; Make sure backspaces and their echo are enabled and no line
4024 ;; width magic interferes with them.
4025 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
4026
4027 (tramp-message vec 5 "Setting shell prompt")
4028 (tramp-send-command
4029 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
4030 (tramp-send-command vec "PS2=''" t)
4031 (tramp-send-command vec "PS3=''" t)
4032 (tramp-send-command vec "PROMPT_COMMAND=''" t)
4033
4034 ;; Try to set up the coding system correctly.
4035 ;; CCC this can't be the right way to do it. Hm.
4036 (tramp-message vec 5 "Determining coding system")
4037 (tramp-send-command vec "echo foo ; echo bar" t)
4038 (with-current-buffer (process-buffer proc)
4039 (goto-char (point-min))
4040 (if (featurep 'mule)
4041 ;; Use MULE to select the right EOL convention for communicating
4042 ;; with the process.
4043 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
4044 (cons 'undecided 'undecided)))
4045 cs-decode cs-encode)
4046 (when (symbolp cs) (setq cs (cons cs cs)))
4047 (setq cs-decode (car cs))
4048 (setq cs-encode (cdr cs))
4049 (unless cs-decode (setq cs-decode 'undecided))
4050 (unless cs-encode (setq cs-encode 'undecided))
4051 (setq cs-encode (tramp-coding-system-change-eol-conversion
4052 cs-encode 'unix))
4053 (when (search-forward "\r" nil t)
4054 (setq cs-decode (tramp-coding-system-change-eol-conversion
4055 cs-decode 'dos)))
4056 (tramp-compat-funcall
4057 'set-buffer-process-coding-system cs-decode cs-encode)
4058 (tramp-message
4059 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
4060 ;; Look for ^M and do something useful if found.
4061 (when (search-forward "\r" nil t)
4062 ;; We have found a ^M but cannot frob the process coding system
4063 ;; because we're running on a non-MULE Emacs. Let's try
4064 ;; stty, instead.
4065 (tramp-send-command vec "stty -onlcr" t))))
4066 ;; Dump stty settings in the traces.
4067 (when (>= tramp-verbose 9)
4068 (tramp-send-command vec "stty -a" t))
4069 (tramp-send-command vec "set +o vi +o emacs" t)
4070
4071 ;; Check whether the output of "uname -sr" has been changed. If
4072 ;; yes, this is a strong indication that we must expire all
4073 ;; connection properties. We start again with
4074 ;; `tramp-maybe-open-connection', it will be catched there.
4075 (tramp-message vec 5 "Checking system information")
4076 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
4077 (new-uname
4078 (tramp-set-connection-property
4079 vec "uname"
4080 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
4081 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
4082 (with-current-buffer (tramp-get-debug-buffer vec)
4083 ;; Keep the debug buffer.
4084 (rename-buffer
4085 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
4086 (tramp-compat-funcall 'tramp-cleanup-connection vec)
4087 (if (= (point-min) (point-max))
4088 (kill-buffer nil)
4089 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
4090 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
4091 (tramp-get-buffer vec)
4092 (tramp-message
4093 vec 3
4094 "Connection reset, because remote host changed from `%s' to `%s'"
4095 old-uname new-uname)
4096 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
4097
4098 ;; Check whether the remote host suffers from buggy
4099 ;; `send-process-string'. This is known for FreeBSD (see comment in
4100 ;; `send_process', file process.c). I've tested sending 624 bytes
4101 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
4102 ;; this host type is detected locally. It cannot handle remote
4103 ;; hosts, though.
4104 (with-connection-property proc "chunksize"
4105 (cond
4106 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
4107 tramp-chunksize)
4108 (t
4109 (tramp-message
4110 vec 5 "Checking remote host type for `send-process-string' bug")
4111 (if (string-match
4112 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
4113 500 0))))
4114
4115 ;; Set remote PATH variable.
4116 (tramp-set-remote-path vec)
4117
4118 ;; Search for a good shell before searching for a command which
4119 ;; checks if a file exists. This is done because Tramp wants to use
4120 ;; "test foo; echo $?" to check if various conditions hold, and
4121 ;; there are buggy /bin/sh implementations which don't execute the
4122 ;; "echo $?" part if the "test" part has an error. In particular,
4123 ;; the OpenSolaris /bin/sh is a problem. There are also other
4124 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
4125 ;; in function declarations, or changing HISTFILE in place.
4126 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
4127 ;; detected.
4128 (tramp-find-shell vec)
4129
4130 ;; Disable unexpected output.
4131 (tramp-send-command vec "mesg n; biff n" t)
4132
4133 ;; IRIX64 bash expands "!" even when in single quotes. This
4134 ;; destroys our shell functions, we must disable it. See
4135 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4136 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
4137 (tramp-send-command vec "set +H" t))
4138
4139 ;; Set `remote-tty' process property.
4140 (ignore-errors
4141 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"")))
4142 (unless (zerop (length tty)) (process-put proc 'remote-tty tty))))
4143
4144 ;; Set the environment.
4145 (tramp-message vec 5 "Setting default environment")
4146
4147 (let ((env (copy-sequence tramp-remote-process-environment))
4148 unset item)
4149 (while env
4150 (setq item (tramp-compat-split-string (car env) "="))
4151 (setcdr item (mapconcat 'identity (cdr item) "="))
4152 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
4153 (tramp-send-command
4154 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
4155 (push (car item) unset))
4156 (setq env (cdr env)))
4157 (when unset
4158 (tramp-send-command
4159 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
4160
4161;; CCC: We should either implement a Perl version of base64 encoding
4162;; and decoding. Then we just use that in the last item. The other
4163;; alternative is to use the Perl version of UU encoding. But then
4164;; we need a Lisp version of uuencode.
4165;;
4166;; Old text from documentation of tramp-methods:
4167;; Using a uuencode/uudecode inline method is discouraged, please use one
4168;; of the base64 methods instead since base64 encoding is much more
4169;; reliable and the commands are more standardized between the different
4170;; Unix versions. But if you can't use base64 for some reason, please
4171;; note that the default uudecode command does not work well for some
4172;; Unices, in particular AIX and Irix. For AIX, you might want to use
4173;; the following command for uudecode:
4174;;
4175;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4176;;
4177;; For Irix, no solution is known yet.
4178
4179(autoload 'uudecode-decode-region "uudecode")
4180
4181(defconst tramp-local-coding-commands
4182 '((b64 base64-encode-region base64-decode-region)
4183 (uu tramp-uuencode-region uudecode-decode-region)
4184 (pack
4185 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
4186 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
4187 "List of local coding commands for inline transfer.
4188Each item is a list that looks like this:
4189
4190\(FORMAT ENCODING DECODING\)
4191
4192FORMAT is symbol describing the encoding/decoding format. It can be
4193`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4194
4195ENCODING and DECODING can be strings, giving commands, or symbols,
4196giving functions. If they are strings, then they can contain
4197the \"%s\" format specifier. If that specifier is present, the input
4198filename will be put into the command line at that spot. If the
4199specifier is not present, the input should be read from standard
4200input.
4201
4202If they are functions, they will be called with two arguments, start
4203and end of region, and are expected to replace the region contents
4204with the encoded or decoded results, respectively.")
4205
4206(defconst tramp-remote-coding-commands
4207 '((b64 "base64" "base64 -d")
4208 (b64 "mimencode -b" "mimencode -u -b")
4209 (b64 "mmencode -b" "mmencode -u -b")
4210 (b64 "recode data..base64" "recode base64..data")
4211 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
4212 (b64 tramp-perl-encode tramp-perl-decode)
4213 (uu "uuencode xxx" "uudecode -o /dev/stdout")
4214 (uu "uuencode xxx" "uudecode -o -")
4215 (uu "uuencode xxx" "uudecode -p")
4216 (uu "uuencode xxx" tramp-uudecode)
4217 (pack
4218 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
4219 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
4220 "List of remote coding commands for inline transfer.
4221Each item is a list that looks like this:
4222
4223\(FORMAT ENCODING DECODING\)
4224
4225FORMAT is symbol describing the encoding/decoding format. It can be
4226`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4227
4228ENCODING and DECODING can be strings, giving commands, or symbols,
4229giving variables. If they are strings, then they can contain
4230the \"%s\" format specifier. If that specifier is present, the input
4231filename will be put into the command line at that spot. If the
4232specifier is not present, the input should be read from standard
4233input.
4234
4235If they are variables, this variable is a string containing a Perl
4236implementation for this functionality. This Perl program will be transferred
4237to the remote host, and it is available as shell function with the same name.")
4238
4239(defun tramp-find-inline-encoding (vec)
4240 "Find an inline transfer encoding that works.
4241Goes through the list `tramp-local-coding-commands' and
4242`tramp-remote-coding-commands'."
4243 (save-excursion
4244 (let ((local-commands tramp-local-coding-commands)
4245 (magic "xyzzy")
4246 loc-enc loc-dec rem-enc rem-dec litem ritem found)
4247 (while (and local-commands (not found))
4248 (setq litem (pop local-commands))
4249 (catch 'wont-work-local
4250 (let ((format (nth 0 litem))
4251 (remote-commands tramp-remote-coding-commands))
4252 (setq loc-enc (nth 1 litem))
4253 (setq loc-dec (nth 2 litem))
4254 ;; If the local encoder or decoder is a string, the
4255 ;; corresponding command has to work locally.
4256 (if (not (stringp loc-enc))
4257 (tramp-message
4258 vec 5 "Checking local encoding function `%s'" loc-enc)
4259 (tramp-message
4260 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4261 (unless (zerop (tramp-call-local-coding-command
4262 loc-enc nil nil))
4263 (throw 'wont-work-local nil)))
4264 (if (not (stringp loc-dec))
4265 (tramp-message
4266 vec 5 "Checking local decoding function `%s'" loc-dec)
4267 (tramp-message
4268 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4269 (unless (zerop (tramp-call-local-coding-command
4270 loc-dec nil nil))
4271 (throw 'wont-work-local nil)))
4272 ;; Search for remote coding commands with the same format
4273 (while (and remote-commands (not found))
4274 (setq ritem (pop remote-commands))
4275 (catch 'wont-work-remote
4276 (when (equal format (nth 0 ritem))
4277 (setq rem-enc (nth 1 ritem))
4278 (setq rem-dec (nth 2 ritem))
4279 ;; Check if remote encoding and decoding commands can be
4280 ;; called remotely with null input and output. This makes
4281 ;; sure there are no syntax errors and the command is really
4282 ;; found. Note that we do not redirect stdout to /dev/null,
4283 ;; for two reasons: when checking the decoding command, we
4284 ;; actually check the output it gives. And also, when
4285 ;; redirecting "mimencode" output to /dev/null, then as root
4286 ;; it might change the permissions of /dev/null!
4287 (when (not (stringp rem-enc))
4288 (let ((name (symbol-name rem-enc)))
4289 (while (string-match (regexp-quote "-") name)
4290 (setq name (replace-match "_" nil t name)))
4291 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4292 (setq rem-enc name)))
4293 (tramp-message
4294 vec 5
4295 "Checking remote encoding command `%s' for sanity" rem-enc)
4296 (unless (tramp-send-command-and-check
4297 vec (format "%s </dev/null" rem-enc) t)
4298 (throw 'wont-work-remote nil))
4299
4300 (when (not (stringp rem-dec))
4301 (let ((name (symbol-name rem-dec)))
4302 (while (string-match (regexp-quote "-") name)
4303 (setq name (replace-match "_" nil t name)))
4304 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
4305 (setq rem-dec name)))
4306 (tramp-message
4307 vec 5
4308 "Checking remote decoding command `%s' for sanity" rem-dec)
4309 (unless (tramp-send-command-and-check
4310 vec
4311 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4312 t)
4313 (throw 'wont-work-remote nil))
4314
4315 (with-current-buffer (tramp-get-buffer vec)
4316 (goto-char (point-min))
4317 (unless (looking-at (regexp-quote magic))
4318 (throw 'wont-work-remote nil)))
4319
4320 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4321 (setq rem-enc (nth 1 ritem))
4322 (setq rem-dec (nth 2 ritem))
4323 (setq found t)))))))
4324
4325 ;; Did we find something?
4326 (unless found
4327 (tramp-error
4328 vec 'file-error "Couldn't find an inline transfer encoding"))
4329
4330 ;; Set connection properties.
4331 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4332 (tramp-set-connection-property vec "local-encoding" loc-enc)
4333 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4334 (tramp-set-connection-property vec "local-decoding" loc-dec)
4335 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4336 (tramp-set-connection-property vec "remote-encoding" rem-enc)
4337 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4338 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
4339
4340(defun tramp-call-local-coding-command (cmd input output)
4341 "Call the local encoding or decoding command.
4342If CMD contains \"%s\", provide input file INPUT there in command.
4343Otherwise, INPUT is passed via standard input.
4344INPUT can also be nil which means `/dev/null'.
4345OUTPUT can be a string (which specifies a filename), or t (which
4346means standard output and thus the current buffer), or nil (which
4347means discard it)."
4348 (tramp-compat-call-process
4349 tramp-encoding-shell
4350 (when (and input (not (string-match "%s" cmd))) input)
4351 (if (eq output t) t nil)
4352 nil
4353 tramp-encoding-command-switch
4354 (concat
4355 (if (string-match "%s" cmd) (format cmd input) cmd)
4356 (if (stringp output) (concat "> " output) ""))))
4357
4358(defconst tramp-inline-compress-commands
4359 '(("gzip" "gzip -d")
4360 ("bzip2" "bzip2 -d")
4361 ("compress" "compress -d"))
4362 "List of compress and decompress commands for inline transfer.
4363Each item is a list that looks like this:
4364
4365\(COMPRESS DECOMPRESS\)
4366
4367COMPRESS or DECOMPRESS are strings with the respective commands.")
4368
4369(defun tramp-find-inline-compress (vec)
4370 "Find an inline transfer compress command that works.
4371Goes through the list `tramp-inline-compress-commands'."
4372 (save-excursion
4373 (let ((commands tramp-inline-compress-commands)
4374 (magic "xyzzy")
4375 item compress decompress
4376 found)
4377 (while (and commands (not found))
4378 (catch 'next
4379 (setq item (pop commands)
4380 compress (nth 0 item)
4381 decompress (nth 1 item))
4382 (tramp-message
4383 vec 5
4384 "Checking local compress command `%s', `%s' for sanity"
4385 compress decompress)
4386 (unless (zerop (tramp-call-local-coding-command
4387 (format "echo %s | %s | %s"
4388 magic compress decompress) nil nil))
4389 (throw 'next nil))
4390 (tramp-message
4391 vec 5
4392 "Checking remote compress command `%s', `%s' for sanity"
4393 compress decompress)
4394 (unless (tramp-send-command-and-check
4395 vec (format "echo %s | %s | %s" magic compress decompress) t)
4396 (throw 'next nil))
4397 (setq found t)))
4398
4399 ;; Did we find something?
4400 (if found
4401 (progn
4402 ;; Set connection properties.
4403 (tramp-message
4404 vec 5 "Using inline transfer compress command `%s'" compress)
4405 (tramp-set-connection-property vec "inline-compress" compress)
4406 (tramp-message
4407 vec 5 "Using inline transfer decompress command `%s'" decompress)
4408 (tramp-set-connection-property vec "inline-decompress" decompress))
4409
4410 (tramp-set-connection-property vec "inline-compress" nil)
4411 (tramp-set-connection-property vec "inline-decompress" nil)
4412 (tramp-message
4413 vec 2 "Couldn't find an inline transfer compress command")))))
4414
4415(defun tramp-compute-multi-hops (vec)
4416 "Expands VEC according to `tramp-default-proxies-alist'.
4417Gateway hops are already opened."
4418 (let ((target-alist `(,vec))
4419 (choices tramp-default-proxies-alist)
4420 item proxy)
4421
4422 ;; Look for proxy hosts to be passed.
4423 (while choices
4424 (setq item (pop choices)
4425 proxy (eval (nth 2 item)))
4426 (when (and
4427 ;; host
4428 (string-match (or (eval (nth 0 item)) "")
4429 (or (tramp-file-name-host (car target-alist)) ""))
4430 ;; user
4431 (string-match (or (eval (nth 1 item)) "")
4432 (or (tramp-file-name-user (car target-alist)) "")))
4433 (if (null proxy)
4434 ;; No more hops needed.
4435 (setq choices nil)
4436 ;; Replace placeholders.
4437 (setq proxy
4438 (format-spec
4439 proxy
4440 (format-spec-make
4441 ?u (or (tramp-file-name-user (car target-alist)) "")
4442 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4443 (with-parsed-tramp-file-name proxy l
4444 ;; Add the hop.
4445 (add-to-list 'target-alist l)
4446 ;; Start next search.
4447 (setq choices tramp-default-proxies-alist)))))
4448
4449 ;; Handle gateways.
4450 (when (string-match
4451 (format
4452 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4453 (tramp-file-name-method (car target-alist)))
4454 (let ((gw (pop target-alist))
4455 (hop (pop target-alist)))
4456 ;; Is the method prepared for gateways?
4457 (unless (tramp-get-method-parameter
4458 (tramp-file-name-method hop) 'tramp-default-port)
4459 (tramp-error
4460 vec 'file-error
4461 "Method `%s' is not supported for gateway access."
4462 (tramp-file-name-method hop)))
4463 ;; Add default port if needed.
4464 (unless
4465 (string-match
4466 tramp-host-with-port-regexp (tramp-file-name-host hop))
4467 (aset hop 2
4468 (concat
4469 (tramp-file-name-host hop) tramp-prefix-port-format
4470 (number-to-string
4471 (tramp-get-method-parameter
4472 (tramp-file-name-method hop) 'tramp-default-port)))))
4473 ;; Open the gateway connection.
4474 (add-to-list
4475 'target-alist
4476 (vector
4477 (tramp-file-name-method hop) (tramp-file-name-user hop)
4478 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
4479 ;; For the password prompt, we need the correct values.
4480 ;; Therefore, we must remember the gateway vector. But we
4481 ;; cannot do it as connection property, because it shouldn't
4482 ;; be persistent. And we have no started process yet either.
4483 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4484
4485 ;; Foreign and out-of-band methods are not supported for multi-hops.
4486 (when (cdr target-alist)
4487 (setq choices target-alist)
4488 (while choices
4489 (setq item (pop choices))
4490 (when
4491 (or
4492 (not
4493 (tramp-get-method-parameter
4494 (tramp-file-name-method item) 'tramp-login-program))
4495 (tramp-get-method-parameter
4496 (tramp-file-name-method item) 'tramp-copy-program))
4497 (tramp-error
4498 vec 'file-error
4499 "Method `%s' is not supported for multi-hops."
4500 (tramp-file-name-method item)))))
4501
4502 ;; In case the host name is not used for the remote shell
4503 ;; command, the user could be misguided by applying a random
4504 ;; hostname.
4505 (let* ((v (car target-alist))
4506 (method (tramp-file-name-method v))
4507 (host (tramp-file-name-host v)))
4508 (unless
4509 (or
4510 ;; There are multi-hops.
4511 (cdr target-alist)
4512 ;; The host name is used for the remote shell command.
4513 (member
4514 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4515 ;; The host is local. We cannot use `tramp-local-host-p'
4516 ;; here, because it opens a connection as well.
4517 (string-match tramp-local-host-regexp host))
4518 (tramp-error
4519 v 'file-error
4520 "Host `%s' looks like a remote host, `%s' can only use the local host"
4521 host method)))
4522
4523 ;; Result.
4524 target-alist))
4525
4526(defun tramp-maybe-open-connection (vec)
4527 "Maybe open a connection VEC.
4528Does not do anything if a connection is already open, but re-opens the
4529connection if a previous connection has died for some reason."
4530 (catch 'uname-changed
4531 (let ((p (tramp-get-connection-process vec))
4532 (process-name (tramp-get-connection-property vec "process-name" nil))
4533 (process-environment (copy-sequence process-environment)))
4534
4535 ;; If too much time has passed since last command was sent, look
4536 ;; whether process is still alive. If it isn't, kill it. When
4537 ;; using ssh, it can sometimes happen that the remote end has
4538 ;; hung up but the local ssh client doesn't recognize this until
4539 ;; it tries to send some data to the remote end. So that's why
4540 ;; we try to send a command from time to time, then look again
4541 ;; whether the process is really alive.
4542 (condition-case nil
4543 (when (and (> (tramp-time-diff
4544 (current-time)
4545 (tramp-get-connection-property
4546 p "last-cmd-time" '(0 0 0)))
4547 60)
4548 p (processp p) (memq (process-status p) '(run open)))
4549 (tramp-send-command vec "echo are you awake" t t)
4550 (unless (and (memq (process-status p) '(run open))
4551 (tramp-wait-for-output p 10))
4552 ;; The error will be catched locally.
4553 (tramp-error vec 'file-error "Awake did fail")))
4554 (file-error
4555 (tramp-flush-connection-property vec)
4556 (tramp-flush-connection-property p)
4557 (delete-process p)
4558 (setq p nil)))
4559
4560 ;; New connection must be opened.
4561 (unless (and p (processp p) (memq (process-status p) '(run open)))
4562
4563 ;; We call `tramp-get-buffer' in order to get a debug buffer for
4564 ;; messages from the beginning.
4565 (tramp-get-buffer vec)
4566 (with-progress-reporter
4567 vec 3
4568 (if (zerop (length (tramp-file-name-user vec)))
4569 (format "Opening connection for %s using %s"
4570 (tramp-file-name-host vec)
4571 (tramp-file-name-method vec))
4572 (format "Opening connection for %s@%s using %s"
4573 (tramp-file-name-user vec)
4574 (tramp-file-name-host vec)
4575 (tramp-file-name-method vec)))
4576
4577 ;; Start new process.
4578 (when (and p (processp p))
4579 (delete-process p))
4580 (setenv "TERM" tramp-terminal-type)
4581 (setenv "LC_ALL" "C")
4582 (setenv "PROMPT_COMMAND")
4583 (setenv "PS1" tramp-initial-end-of-output)
4584 (let* ((target-alist (tramp-compute-multi-hops vec))
4585 (process-connection-type tramp-process-connection-type)
4586 (process-adaptive-read-buffering nil)
4587 (coding-system-for-read nil)
4588 ;; This must be done in order to avoid our file name handler.
4589 (p (let ((default-directory
4590 (tramp-compat-temporary-file-directory)))
4591 (start-process
4592 (or process-name (tramp-buffer-name vec))
4593 (tramp-get-connection-buffer vec)
4594 tramp-encoding-shell))))
4595
4596 (tramp-message
4597 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4598
4599 ;; Check whether process is alive.
4600 (tramp-set-process-query-on-exit-flag p nil)
4601 (tramp-barf-if-no-shell-prompt
4602 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4603
4604 ;; Now do all the connections as specified.
4605 (while target-alist
4606 (let* ((hop (car target-alist))
4607 (l-method (tramp-file-name-method hop))
4608 (l-user (tramp-file-name-user hop))
4609 (l-host (tramp-file-name-host hop))
4610 (l-port nil)
4611 (login-program
4612 (tramp-get-method-parameter
4613 l-method 'tramp-login-program))
4614 (login-args
4615 (tramp-get-method-parameter l-method 'tramp-login-args))
4616 (async-args
4617 (tramp-get-method-parameter l-method 'tramp-async-args))
4618 (gw-args
4619 (tramp-get-method-parameter l-method 'tramp-gw-args))
4620 (gw (tramp-get-file-property hop "" "gateway" nil))
4621 (g-method (and gw (tramp-file-name-method gw)))
4622 (g-user (and gw (tramp-file-name-user gw)))
4623 (g-host (and gw (tramp-file-name-host gw)))
4624 (command login-program)
4625 ;; We don't create the temporary file. In fact,
4626 ;; it is just a prefix for the ControlPath option
4627 ;; of ssh; the real temporary file has another
4628 ;; name, and it is created and protected by ssh.
4629 ;; It is also removed by ssh, when the connection
4630 ;; is closed.
4631 (tmpfile
4632 (tramp-set-connection-property
4633 p "temp-file"
4634 (make-temp-name
4635 (expand-file-name
4636 tramp-temp-name-prefix
4637 (tramp-compat-temporary-file-directory)))))
4638 spec)
4639
4640 ;; Add arguments for asynchrononous processes.
4641 (when (and process-name async-args)
4642 (setq login-args (append async-args login-args)))
4643
4644 ;; Add gateway arguments if necessary.
4645 (when (and gw gw-args)
4646 (setq login-args (append gw-args login-args)))
4647
4648 ;; Check for port number. Until now, there's no need
4649 ;; for handling like method, user, host.
4650 (when (string-match tramp-host-with-port-regexp l-host)
4651 (setq l-port (match-string 2 l-host)
4652 l-host (match-string 1 l-host)))
4653
4654 ;; Set variables for computing the prompt for reading
4655 ;; password. They can also be derived from a gateway.
4656 (setq tramp-current-method (or g-method l-method)
4657 tramp-current-user (or g-user l-user)
4658 tramp-current-host (or g-host l-host))
4659
4660 ;; Replace login-args place holders.
4661 (setq
4662 l-host (or l-host "")
4663 l-user (or l-user "")
4664 l-port (or l-port "")
4665 spec (format-spec-make
4666 ?h l-host ?u l-user ?p l-port ?t tmpfile)
4667 command
4668 (concat
4669 ;; We do not want to see the trailing local prompt in
4670 ;; `start-file-process'.
4671 (unless (memq system-type '(windows-nt)) "exec ")
4672 command " "
4673 (mapconcat
4674 (lambda (x)
4675 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4676 (unless (member "" x) (mapconcat 'identity x " ")))
4677 login-args " ")
4678 ;; Local shell could be a Windows COMSPEC. It
4679 ;; doesn't know the ";" syntax, but we must exit
4680 ;; always for `start-file-process'. "exec" does not
4681 ;; work either.
4682 (if (memq system-type '(windows-nt)) " && exit || exit")))
4683
4684 ;; Send the command.
4685 (tramp-message vec 3 "Sending command `%s'" command)
4686 (tramp-send-command vec command t t)
4687 (tramp-process-actions p vec tramp-actions-before-shell 60)
4688 (tramp-message
4689 vec 3 "Found remote shell prompt on `%s'" l-host))
4690 ;; Next hop.
4691 (setq target-alist (cdr target-alist)))
4692
4693 ;; Make initial shell settings.
4694 (tramp-open-connection-setup-interactive-shell p vec)))))))
4695
4696(defun tramp-send-command (vec command &optional neveropen nooutput)
4697 "Send the COMMAND to connection VEC.
4698Erases temporary buffer before sending the command. If optional
4699arg NEVEROPEN is non-nil, never try to open the connection. This
4700is meant to be used from `tramp-maybe-open-connection' only. The
4701function waits for output unless NOOUTPUT is set."
4702 (unless neveropen (tramp-maybe-open-connection vec))
4703 (let ((p (tramp-get-connection-process vec)))
4704 (when (tramp-get-connection-property p "remote-echo" nil)
4705 ;; We mark the command string that it can be erased in the output buffer.
4706 (tramp-set-connection-property p "check-remote-echo" t)
4707 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4708 (tramp-message vec 6 "%s" command)
4709 (tramp-send-string vec command)
4710 (unless nooutput (tramp-wait-for-output p))))
4711
4712(defun tramp-wait-for-output (proc &optional timeout)
4713 "Wait for output from remote command."
4714 (unless (buffer-live-p (process-buffer proc))
4715 (delete-process proc)
4716 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4717 (with-current-buffer (process-buffer proc)
4718 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4719 ;; be leading escape sequences, which must be ignored.
4720 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4721 ;; Sometimes, the commands do not return a newline but a
4722 ;; null byte before the shell prompt, for example "git
4723 ;; ls-files -c -z ...".
4724 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4725 (found (tramp-wait-for-regexp proc timeout regexp1)))
4726 (if found
4727 (let (buffer-read-only)
4728 ;; A simple-minded busybox has sent " ^H" sequences.
4729 ;; Delete them.
4730 (goto-char (point-min))
4731 (when (re-search-forward
4732 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
4733 (forward-line 1)
4734 (delete-region (point-min) (point)))
4735 ;; Delete the prompt.
4736 (goto-char (point-max))
4737 (re-search-backward regexp nil t)
4738 (delete-region (point) (point-max)))
4739 (if timeout
4740 (tramp-error
4741 proc 'file-error
4742 "[[Remote prompt `%s' not found in %d secs]]"
4743 tramp-end-of-output timeout)
4744 (tramp-error
4745 proc 'file-error
4746 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4747 ;; Return value is whether end-of-output sentinel was found.
4748 found)))
4749
4750(defun tramp-send-command-and-check
4751 (vec command &optional subshell dont-suppress-err)
4752 "Run COMMAND and check its exit status.
4753Sends `echo $?' along with the COMMAND for checking the exit status. If
4754COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4755
4756If the optional argument SUBSHELL is non-nil, the command is
4757executed in a subshell, ie surrounded by parentheses. If
4758DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4759 (tramp-send-command
4760 vec
4761 (concat (if subshell "( " "")
4762 command
4763 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4764 "echo tramp_exit_status $?"
4765 (if subshell " )" "")))
4766 (with-current-buffer (tramp-get-connection-buffer vec)
4767 (goto-char (point-max))
4768 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4769 (tramp-error
4770 vec 'file-error "Couldn't find exit status of `%s'" command))
4771 (skip-chars-forward "^ ")
4772 (prog1
4773 (zerop (read (current-buffer)))
4774 (let (buffer-read-only)
4775 (delete-region (match-beginning 0) (point-max))))))
4776
4777(defun tramp-barf-unless-okay (vec command fmt &rest args)
4778 "Run COMMAND, check exit status, throw error if exit status not okay.
4779Similar to `tramp-send-command-and-check' but accepts two more arguments
4780FMT and ARGS which are passed to `error'."
4781 (unless (tramp-send-command-and-check vec command)
4782 (apply 'tramp-error vec 'file-error fmt args)))
4783
4784(defun tramp-send-command-and-read (vec command)
4785 "Run COMMAND and return the output, which must be a Lisp expression.
4786In case there is no valid Lisp expression, it raises an error"
4787 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
4788 (with-current-buffer (tramp-get-connection-buffer vec)
4789 ;; Read the expression.
4790 (goto-char (point-min))
4791 (condition-case nil
4792 (prog1 (read (current-buffer))
4793 ;; Error handling.
4794 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
4795 (error nil)))
4796 (error (tramp-error
4797 vec 'file-error
4798 "`%s' does not return a valid Lisp expression: `%s'"
4799 command (buffer-string))))))
4800
4801(defun tramp-mode-string-to-int (mode-string)
4802 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
4803 (let* (case-fold-search
4804 (mode-chars (string-to-vector mode-string))
4805 (owner-read (aref mode-chars 1))
4806 (owner-write (aref mode-chars 2))
4807 (owner-execute-or-setid (aref mode-chars 3))
4808 (group-read (aref mode-chars 4))
4809 (group-write (aref mode-chars 5))
4810 (group-execute-or-setid (aref mode-chars 6))
4811 (other-read (aref mode-chars 7))
4812 (other-write (aref mode-chars 8))
4813 (other-execute-or-sticky (aref mode-chars 9)))
4814 (save-match-data
4815 (logior
4816 (cond
4817 ((char-equal owner-read ?r) (tramp-compat-octal-to-decimal "00400"))
4818 ((char-equal owner-read ?-) 0)
4819 (t (error "Second char `%c' must be one of `r-'" owner-read)))
4820 (cond
4821 ((char-equal owner-write ?w) (tramp-compat-octal-to-decimal "00200"))
4822 ((char-equal owner-write ?-) 0)
4823 (t (error "Third char `%c' must be one of `w-'" owner-write)))
4824 (cond
4825 ((char-equal owner-execute-or-setid ?x)
4826 (tramp-compat-octal-to-decimal "00100"))
4827 ((char-equal owner-execute-or-setid ?S)
4828 (tramp-compat-octal-to-decimal "04000"))
4829 ((char-equal owner-execute-or-setid ?s)
4830 (tramp-compat-octal-to-decimal "04100"))
4831 ((char-equal owner-execute-or-setid ?-) 0)
4832 (t (error "Fourth char `%c' must be one of `xsS-'"
4833 owner-execute-or-setid)))
4834 (cond
4835 ((char-equal group-read ?r) (tramp-compat-octal-to-decimal "00040"))
4836 ((char-equal group-read ?-) 0)
4837 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
4838 (cond
4839 ((char-equal group-write ?w) (tramp-compat-octal-to-decimal "00020"))
4840 ((char-equal group-write ?-) 0)
4841 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
4842 (cond
4843 ((char-equal group-execute-or-setid ?x)
4844 (tramp-compat-octal-to-decimal "00010"))
4845 ((char-equal group-execute-or-setid ?S)
4846 (tramp-compat-octal-to-decimal "02000"))
4847 ((char-equal group-execute-or-setid ?s)
4848 (tramp-compat-octal-to-decimal "02010"))
4849 ((char-equal group-execute-or-setid ?-) 0)
4850 (t (error "Seventh char `%c' must be one of `xsS-'"
4851 group-execute-or-setid)))
4852 (cond
4853 ((char-equal other-read ?r)
4854 (tramp-compat-octal-to-decimal "00004"))
4855 ((char-equal other-read ?-) 0)
4856 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
4857 (cond
4858 ((char-equal other-write ?w) (tramp-compat-octal-to-decimal "00002"))
4859 ((char-equal other-write ?-) 0)
4860 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
4861 (cond
4862 ((char-equal other-execute-or-sticky ?x)
4863 (tramp-compat-octal-to-decimal "00001"))
4864 ((char-equal other-execute-or-sticky ?T)
4865 (tramp-compat-octal-to-decimal "01000"))
4866 ((char-equal other-execute-or-sticky ?t)
4867 (tramp-compat-octal-to-decimal "01001"))
4868 ((char-equal other-execute-or-sticky ?-) 0)
4869 (t (error "Tenth char `%c' must be one of `xtT-'"
4870 other-execute-or-sticky)))))))
4871
4872(defun tramp-convert-file-attributes (vec attr)
4873 "Convert file-attributes ATTR generated by perl script, stat or ls.
4874Convert file mode bits to string and set virtual device number.
4875Return ATTR."
4876 (when attr
4877 ;; Convert last access time.
4878 (unless (listp (nth 4 attr))
4879 (setcar (nthcdr 4 attr)
4880 (list (floor (nth 4 attr) 65536)
4881 (floor (mod (nth 4 attr) 65536)))))
4882 ;; Convert last modification time.
4883 (unless (listp (nth 5 attr))
4884 (setcar (nthcdr 5 attr)
4885 (list (floor (nth 5 attr) 65536)
4886 (floor (mod (nth 5 attr) 65536)))))
4887 ;; Convert last status change time.
4888 (unless (listp (nth 6 attr))
4889 (setcar (nthcdr 6 attr)
4890 (list (floor (nth 6 attr) 65536)
4891 (floor (mod (nth 6 attr) 65536)))))
4892 ;; Convert file size.
4893 (when (< (nth 7 attr) 0)
4894 (setcar (nthcdr 7 attr) -1))
4895 (when (and (floatp (nth 7 attr))
4896 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4897 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4898 ;; Convert file mode bits to string.
4899 (unless (stringp (nth 8 attr))
4900 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4901 (when (stringp (car attr))
4902 (aset (nth 8 attr) 0 ?l)))
4903 ;; Convert directory indication bit.
4904 (when (string-match "^d" (nth 8 attr))
4905 (setcar attr t))
4906 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4907 (when (consp (car attr))
4908 (if (and (stringp (caar attr))
4909 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4910 (setcar attr (match-string 1 (caar attr)))
4911 (setcar attr nil)))
4912 ;; Set file's gid change bit.
4913 (setcar (nthcdr 9 attr)
4914 (if (numberp (nth 3 attr))
4915 (not (= (nth 3 attr)
4916 (tramp-get-remote-gid vec 'integer)))
4917 (not (string-equal
4918 (nth 3 attr)
4919 (tramp-get-remote-gid vec 'string)))))
4920 ;; Convert inode.
4921 (unless (listp (nth 10 attr))
4922 (setcar (nthcdr 10 attr)
4923 (condition-case nil
4924 (cons (floor (nth 10 attr) 65536)
4925 (floor (mod (nth 10 attr) 65536)))
4926 ;; Inodes can be incredible huge. We must hide this.
4927 (error (tramp-get-inode vec)))))
4928 ;; Set virtual device number.
4929 (setcar (nthcdr 11 attr)
4930 (tramp-get-device vec))
4931 attr))
4932
4933(defun tramp-check-cached-permissions (vec access)
4934 "Check `file-attributes' caches for VEC.
4935Return t if according to the cache access type ACCESS is known to
4936be granted."
4937 (let ((result nil)
4938 (offset (cond
4939 ((eq ?r access) 1)
4940 ((eq ?w access) 2)
4941 ((eq ?x access) 3))))
4942 (dolist (suffix '("string" "integer") result)
4943 (setq
4944 result
4945 (or
4946 result
4947 (let ((file-attr
4948 (tramp-get-file-property
4949 vec (tramp-file-name-localname vec)
4950 (concat "file-attributes-" suffix) nil))
4951 (remote-uid
4952 (tramp-get-connection-property
4953 vec (concat "uid-" suffix) nil))
4954 (remote-gid
4955 (tramp-get-connection-property
4956 vec (concat "gid-" suffix) nil)))
4957 (and
4958 file-attr
4959 (or
4960 ;; Not a symlink
4961 (eq t (car file-attr))
4962 (null (car file-attr)))
4963 (or
4964 ;; World accessible.
4965 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4966 ;; User accessible and owned by user.
4967 (and
4968 (eq access (aref (nth 8 file-attr) offset))
4969 (equal remote-uid (nth 2 file-attr)))
4970 ;; Group accessible and owned by user's
4971 ;; principal group.
4972 (and
4973 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4974 (equal remote-gid (nth 3 file-attr)))))))))))
4975
4976(defun tramp-file-mode-from-int (mode)
4977 "Turn an integer representing a file mode into an ls(1)-like string."
4978 (let ((type (cdr
4979 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4980 (user (logand (lsh mode -6) 7))
4981 (group (logand (lsh mode -3) 7))
4982 (other (logand (lsh mode -0) 7))
4983 (suid (> (logand (lsh mode -9) 4) 0))
4984 (sgid (> (logand (lsh mode -9) 2) 0))
4985 (sticky (> (logand (lsh mode -9) 1) 0)))
4986 (setq user (tramp-file-mode-permissions user suid "s"))
4987 (setq group (tramp-file-mode-permissions group sgid "s"))
4988 (setq other (tramp-file-mode-permissions other sticky "t"))
4989 (concat type user group other)))
4990
4991(defun tramp-file-mode-permissions (perm suid suid-text)
4992 "Convert a permission bitset into a string.
4993This is used internally by `tramp-file-mode-from-int'."
4994 (let ((r (> (logand perm 4) 0))
4995 (w (> (logand perm 2) 0))
4996 (x (> (logand perm 1) 0)))
4997 (concat (or (and r "r") "-")
4998 (or (and w "w") "-")
4999 (or (and suid x suid-text) ; suid, execute
5000 (and suid (upcase suid-text)) ; suid, !execute
5001 (and x "x") "-")))) ; !suid
5002
5003(defun tramp-shell-case-fold (string)
5004 "Converts STRING to shell glob pattern which ignores case."
5005 (mapconcat
5006 (lambda (c)
5007 (if (equal (downcase c) (upcase c))
5008 (vector c)
5009 (format "[%c%c]" (downcase c) (upcase c))))
5010 string
5011 ""))
5012
5013(defun tramp-make-copy-program-file-name (vec)
5014 "Create a file name suitable to be passed to `rcp' and workalikes."
5015 (let ((user (tramp-file-name-user vec))
5016 (host (tramp-file-name-real-host vec))
5017 (localname (tramp-shell-quote-argument
5018 (tramp-file-name-localname vec))))
5019 (if (not (zerop (length user)))
5020 (format "%s@%s:%s" user host localname)
5021 (format "%s:%s" host localname))))
5022
5023(defun tramp-method-out-of-band-p (vec size)
5024 "Return t if this is an out-of-band method, nil otherwise."
5025 (and
5026 ;; It shall be an out-of-band method.
5027 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
5028 ;; Either the file size is large enough, or (in rare cases) there
5029 ;; does not exist a remote encoding.
5030 (or (null tramp-copy-size-limit)
5031 (> size tramp-copy-size-limit)
5032 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
5033
5034(defun tramp-local-host-p (vec)
5035 "Return t if this points to the local host, nil otherwise."
5036 ;; We cannot use `tramp-file-name-real-host'. A port is an
5037 ;; indication for an ssh tunnel or alike.
5038 (let ((host (tramp-file-name-host vec)))
5039 (and
5040 (stringp host)
5041 (string-match tramp-local-host-regexp host)
5042 ;; The method shall be applied to one of the shell file name
5043 ;; handler. `tramp-local-host-p' is also called for "smb" and
5044 ;; alike, where it must fail.
5045 (tramp-get-method-parameter
5046 (tramp-file-name-method vec) 'tramp-login-program)
5047 ;; The local temp directory must be writable for the other user.
5048 (file-writable-p
5049 (tramp-make-tramp-file-name
5050 (tramp-file-name-method vec)
5051 (tramp-file-name-user vec)
5052 host
5053 (tramp-compat-temporary-file-directory)))
5054 ;; On some systems, chown runs only for root.
5055 (or (zerop (user-uid))
5056 (zerop (tramp-get-remote-uid vec 'integer))))))
5057
5058;; Variables local to connection.
5059
5060(defun tramp-get-remote-path (vec)
5061 (with-connection-property
5062 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5063 ;; cache the result for the session only. Otherwise, the result
5064 ;; is cached persistently.
5065 (if (memq 'tramp-own-remote-path tramp-remote-path)
5066 (tramp-get-connection-process vec)
5067 vec)
5068 "remote-path"
5069 (let* ((remote-path (copy-tree tramp-remote-path))
5070 (elt1 (memq 'tramp-default-remote-path remote-path))
5071 (elt2 (memq 'tramp-own-remote-path remote-path))
5072 (default-remote-path
5073 (when elt1
5074 (condition-case nil
5075 (tramp-send-command-and-read
5076 vec "echo \\\"`getconf PATH`\\\"")
5077 ;; Default if "getconf" is not available.
5078 (error
5079 (tramp-message
5080 vec 3
5081 "`getconf PATH' not successful, using default value \"%s\"."
5082 "/bin:/usr/bin")
5083 "/bin:/usr/bin"))))
5084 (own-remote-path
5085 (when elt2
5086 (condition-case nil
5087 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
5088 ;; Default if "getconf" is not available.
5089 (error
5090 (tramp-message
5091 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
5092 nil)))))
5093
5094 ;; Replace place holder `tramp-default-remote-path'.
5095 (when elt1
5096 (setcdr elt1
5097 (append
5098 (tramp-compat-split-string default-remote-path ":")
5099 (cdr elt1)))
5100 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
5101
5102 ;; Replace place holder `tramp-own-remote-path'.
5103 (when elt2
5104 (setcdr elt2
5105 (append
5106 (tramp-compat-split-string own-remote-path ":")
5107 (cdr elt2)))
5108 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
5109
5110 ;; Remove double entries.
5111 (setq elt1 remote-path)
5112 (while (consp elt1)
5113 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
5114 (setcar elt2 nil))
5115 (setq elt1 (cdr elt1)))
5116
5117 ;; Remove non-existing directories.
5118 (delq
5119 nil
5120 (mapcar
5121 (lambda (x)
5122 (and
5123 (stringp x)
5124 (file-directory-p
5125 (tramp-make-tramp-file-name
5126 (tramp-file-name-method vec)
5127 (tramp-file-name-user vec)
5128 (tramp-file-name-host vec)
5129 x))
5130 x))
5131 remote-path)))))
5132
5133(defun tramp-get-remote-tmpdir (vec)
5134 (with-connection-property vec "tmp-directory"
5135 (let ((dir (tramp-shell-quote-argument "/tmp")))
5136 (if (and (tramp-send-command-and-check
5137 vec (format "%s -d %s" (tramp-get-test-command vec) dir))
5138 (tramp-send-command-and-check
5139 vec (format "%s -w %s" (tramp-get-test-command vec) dir)))
5140 dir
5141 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
5142
5143(defun tramp-make-tramp-temp-file (vec)
5144 "Create a temporary file on the remote host identified by VEC.
5145Return the local name of the temporary file."
5146 (let ((prefix
5147 (tramp-make-tramp-file-name
5148 (tramp-file-name-method vec)
5149 (tramp-file-name-user vec)
5150 (tramp-file-name-host vec)
5151 (tramp-drop-volume-letter
5152 (expand-file-name
5153 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
5154 result)
5155 (while (not result)
5156 ;; `make-temp-file' would be the natural choice for
5157 ;; implementation. But it calls `write-region' internally,
5158 ;; which also needs a temporary file - we would end in an
5159 ;; infinite loop.
5160 (setq result (make-temp-name prefix))
5161 (if (file-exists-p result)
5162 (setq result nil)
5163 ;; This creates the file by side effect.
5164 (set-file-times result)
5165 (set-file-modes result (tramp-compat-octal-to-decimal "0700"))))
5166
5167 ;; Return the local part.
5168 (with-parsed-tramp-file-name result nil localname)))
5169
5170(defun tramp-get-ls-command (vec)
5171 (with-connection-property vec "ls"
5172 (tramp-message vec 5 "Finding a suitable `ls' command")
5173 (or
5174 (catch 'ls-found
5175 (dolist (cmd '("ls" "gnuls" "gls"))
5176 (let ((dl (tramp-get-remote-path vec))
5177 result)
5178 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5179 ;; Check parameters. On busybox, "ls" output coloring is
5180 ;; enabled by default sometimes. So we try to disable it
5181 ;; when possible. $LS_COLORING is not supported there.
5182 ;; Some "ls" versions are sensible wrt the order of
5183 ;; arguments, they fail when "-al" is after the
5184 ;; "--color=never" argument (for example on FreeBSD).
5185 (when (tramp-send-command-and-check
5186 vec (format "%s -lnd /" result))
5187 (when (tramp-send-command-and-check
5188 vec (format
5189 "%s --color=never -al /dev/null" result))
5190 (setq result (concat result " --color=never")))
5191 (throw 'ls-found result))
5192 (setq dl (cdr dl))))))
5193 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
5194
5195(defun tramp-get-ls-command-with-dired (vec)
5196 (save-match-data
5197 (with-connection-property vec "ls-dired"
5198 (tramp-message vec 5 "Checking, whether `ls --dired' works")
5199 ;; Some "ls" versions are sensible wrt the order of arguments,
5200 ;; they fail when "-al" is after the "--dired" argument (for
5201 ;; example on FreeBSD).
5202 (tramp-send-command-and-check
5203 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
5204
5205(defun tramp-get-test-command (vec)
5206 (with-connection-property vec "test"
5207 (tramp-message vec 5 "Finding a suitable `test' command")
5208 (if (tramp-send-command-and-check vec "test 0")
5209 "test"
5210 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
5211
5212(defun tramp-get-test-nt-command (vec)
5213 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5214 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5215 ;; for otherwise the shell crashes.
5216 (with-connection-property vec "test-nt"
5217 (or
5218 (progn
5219 (tramp-send-command
5220 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
5221 (with-current-buffer (tramp-get-buffer vec)
5222 (goto-char (point-min))
5223 (when (looking-at (regexp-quote tramp-end-of-output))
5224 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
5225 (progn
5226 (tramp-send-command
5227 vec
5228 (format
5229 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5230 (tramp-get-test-command vec)))
5231 "tramp_test_nt %s %s"))))
5232
5233(defun tramp-get-file-exists-command (vec)
5234 (with-connection-property vec "file-exists"
5235 (tramp-message vec 5 "Finding command to check if file exists")
5236 (tramp-find-file-exists-command vec)))
5237
5238(defun tramp-get-remote-ln (vec)
5239 (with-connection-property vec "ln"
5240 (tramp-message vec 5 "Finding a suitable `ln' command")
5241 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
5242
5243(defun tramp-get-remote-perl (vec)
5244 (with-connection-property vec "perl"
5245 (tramp-message vec 5 "Finding a suitable `perl' command")
5246 (let ((result
5247 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
5248 (tramp-find-executable
5249 vec "perl" (tramp-get-remote-path vec)))))
5250 ;; We must check also for some Perl modules.
5251 (when result
5252 (with-connection-property vec "perl-file-spec"
5253 (tramp-send-command-and-check
5254 vec (format "%s -e 'use File::Spec;'" result)))
5255 (with-connection-property vec "perl-cwd-realpath"
5256 (tramp-send-command-and-check
5257 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
5258 result)))
5259
5260(defun tramp-get-remote-stat (vec)
5261 (with-connection-property vec "stat"
5262 (tramp-message vec 5 "Finding a suitable `stat' command")
5263 (let ((result (tramp-find-executable
5264 vec "stat" (tramp-get-remote-path vec)))
5265 tmp)
5266 ;; Check whether stat(1) returns usable syntax. %s does not
5267 ;; work on older AIX systems.
5268 (when result
5269 (setq tmp
5270 ;; We don't want to display an error message.
5271 (with-temp-message (or (current-message) "")
5272 (ignore-errors
5273 (tramp-send-command-and-read
5274 vec (format "%s -c '(\"%%N\" %%s)' /" result)))))
5275 (unless (and (listp tmp) (stringp (car tmp))
5276 (string-match "^./.$" (car tmp))
5277 (integerp (cadr tmp)))
5278 (setq result nil)))
5279 result)))
5280
5281(defun tramp-get-remote-readlink (vec)
5282 (with-connection-property vec "readlink"
5283 (tramp-message vec 5 "Finding a suitable `readlink' command")
5284 (let ((result (tramp-find-executable
5285 vec "readlink" (tramp-get-remote-path vec))))
5286 (when (and result
5287 ;; We don't want to display an error message.
5288 (with-temp-message (or (current-message) "")
5289 (ignore-errors
5290 (tramp-send-command-and-check
5291 vec (format "%s --canonicalize-missing /" result)))))
5292 result))))
5293
5294(defun tramp-get-remote-trash (vec)
5295 (with-connection-property vec "trash"
5296 (tramp-message vec 5 "Finding a suitable `trash' command")
5297 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
5298
5299(defun tramp-get-remote-id (vec)
5300 (with-connection-property vec "id"
5301 (tramp-message vec 5 "Finding POSIX `id' command")
5302 (or
5303 (catch 'id-found
5304 (let ((dl (tramp-get-remote-path vec))
5305 result)
5306 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
5307 ;; Check POSIX parameter.
5308 (when (tramp-send-command-and-check vec (format "%s -u" result))
5309 (throw 'id-found result))
5310 (setq dl (cdr dl)))))
5311 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
5312
5313(defun tramp-get-remote-uid (vec id-format)
5314 (with-connection-property vec (format "uid-%s" id-format)
5315 (let ((res (tramp-send-command-and-read
5316 vec
5317 (format "%s -u%s %s"
5318 (tramp-get-remote-id vec)
5319 (if (equal id-format 'integer) "" "n")
5320 (if (equal id-format 'integer)
5321 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5322 ;; The command might not always return a number.
5323 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5324
5325(defun tramp-get-remote-gid (vec id-format)
5326 (with-connection-property vec (format "gid-%s" id-format)
5327 (let ((res (tramp-send-command-and-read
5328 vec
5329 (format "%s -g%s %s"
5330 (tramp-get-remote-id vec)
5331 (if (equal id-format 'integer) "" "n")
5332 (if (equal id-format 'integer)
5333 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5334 ;; The command might not always return a number.
5335 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5336
5337(defun tramp-get-local-uid (id-format)
5338 (if (equal id-format 'integer) (user-uid) (user-login-name)))
5339
5340(defun tramp-get-local-gid (id-format)
5341 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
5342
5343;; Some predefined connection properties.
5344(defun tramp-get-inline-compress (vec prop size)
5345 "Return the compress command related to PROP.
5346PROP is either `inline-compress' or `inline-decompress'. SIZE is
5347the length of the file to be compressed.
5348
5349If no corresponding command is found, nil is returned."
5350 (when (and (integerp tramp-inline-compress-start-size)
5351 (> size tramp-inline-compress-start-size))
5352 (with-connection-property vec prop
5353 (tramp-find-inline-compress vec)
5354 (tramp-get-connection-property vec prop nil))))
5355
5356(defun tramp-get-inline-coding (vec prop size)
5357 "Return the coding command related to PROP.
5358PROP is either `remote-encoding', `remode-decoding',
5359`local-encoding' or `local-decoding'.
5360
5361SIZE is the length of the file to be coded. Depending on SIZE,
5362compression might be applied.
5363
5364If no corresponding command is found, nil is returned.
5365Otherwise, either a string is returned which contains a `%s' mark
5366to be used for the respective input or output file; or a Lisp
5367function cell is returned to be applied on a buffer."
5368 (let ((coding
5369 (with-connection-property vec prop
5370 (tramp-find-inline-encoding vec)
5371 (tramp-get-connection-property vec prop nil)))
5372 (prop1 (if (string-match "encoding" prop)
5373 "inline-compress" "inline-decompress"))
5374 compress)
5375 ;; The connection property might have been cached. So we must send
5376 ;; the script to the remote side - maybe.
5377 (when (and coding (symbolp coding) (string-match "remote" prop))
5378 (let ((name (symbol-name coding)))
5379 (while (string-match (regexp-quote "-") name)
5380 (setq name (replace-match "_" nil t name)))
5381 (tramp-maybe-send-script vec (symbol-value coding) name)
5382 (setq coding name)))
5383 (when coding
5384 ;; Check for the `compress' command.
5385 (setq compress (tramp-get-inline-compress vec prop1 size))
5386 ;; Return the value.
5387 (cond
5388 ((and compress (symbolp coding))
5389 (if (string-match "decompress" prop1)
5390 `(lambda (beg end)
5391 (,coding beg end)
5392 (let ((coding-system-for-write 'binary)
5393 (coding-system-for-read 'binary))
5394 (apply
5395 'call-process-region (point-min) (point-max)
5396 (car (split-string ,compress)) t t nil
5397 (cdr (split-string ,compress)))))
5398 `(lambda (beg end)
5399 (let ((coding-system-for-write 'binary)
5400 (coding-system-for-read 'binary))
5401 (apply
5402 'call-process-region beg end
5403 (car (split-string ,compress)) t t nil
5404 (cdr (split-string ,compress))))
5405 (,coding (point-min) (point-max)))))
5406 ((symbolp coding)
5407 coding)
5408 ((and compress (string-match "decoding" prop))
5409 (format "(%s | %s >%%s)" coding compress))
5410 (compress
5411 (format "(%s <%%s | %s)" compress coding))
5412 ((string-match "decoding" prop)
5413 (format "%s >%%s" coding))
5414 (t
5415 (format "%s <%%s" coding))))))
5416
5417;;; Integration of eshell.el:
5418
5419(eval-when-compile
5420 (defvar eshell-path-env))
5421
5422;; eshell.el keeps the path in `eshell-path-env'. We must change it
5423;; when `default-directory' points to another host.
5424(defun tramp-eshell-directory-change ()
5425 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
5426 (setq eshell-path-env
5427 (if (file-remote-p default-directory)
5428 (with-parsed-tramp-file-name default-directory nil
5429 (mapconcat
5430 'identity
5431 (tramp-get-remote-path v)
5432 ":"))
5433 (getenv "PATH"))))
5434
5435(eval-after-load "esh-util"
5436 '(progn
5437 (tramp-eshell-directory-change)
5438 (add-hook 'eshell-directory-change-hook
5439 'tramp-eshell-directory-change)
5440 (add-hook 'tramp-unload-hook
5441 (lambda ()
5442 (remove-hook 'eshell-directory-change-hook
5443 'tramp-eshell-directory-change)))))
5444
5445(add-hook 'tramp-unload-hook
5446 (lambda ()
5447 (unload-feature 'tramp-sh 'force)))
5448
5449(provide 'tramp-sh)
5450
5451;;; TODO:
5452
5453;; * Don't use globbing for directories with many files, as this is
5454;; likely to produce long command lines, and some shells choke on
5455;; long command lines.
5456;; * Make it work for different encodings, and for different file name
5457;; encodings, too. (Daniel Pittman)
5458;; * Don't search for perl5 and perl. Instead, only search for perl and
5459;; then look if it's the right version (with `perl -v').
5460;; * When editing a remote CVS controlled file as a different user, VC
5461;; gets confused about the file locking status. Try to find out why
5462;; the workaround doesn't work.
5463;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5464;; until the last but one hop via `start-file-process'. Apply it
5465;; also for ftp and smb.
5466;; * WIBNI if we had a command "trampclient"? If I was editing in
5467;; some shell with root priviledges, it would be nice if I could
5468;; just call
5469;; trampclient filename.c
5470;; as an editor, and the _current_ shell would connect to an Emacs
5471;; server and would be used in an existing non-priviledged Emacs
5472;; session for doing the editing in question.
5473;; That way, I need not tell Emacs my password again and be afraid
5474;; that it makes it into core dumps or other ugly stuff (I had Emacs
5475;; once display a just typed password in the context of a keyboard
5476;; sequence prompt for a question immediately following in a shell
5477;; script run within Emacs -- nasty).
5478;; And if I have some ssh session running to a different computer,
5479;; having the possibility of passing a local file there to a local
5480;; Emacs session (in case I can arrange for a connection back) would
5481;; be nice.
5482;; Likely the corresponding Tramp server should not allow the
5483;; equivalent of the emacsclient -eval option in order to make this
5484;; reasonably unproblematic. And maybe trampclient should have some
5485;; way of passing credentials, like by using an SSL socket or
5486;; something. (David Kastrup)
5487;; * Reconnect directly to a compliant shell without first going
5488;; through the user's default shell. (Pete Forman)
5489;; * How can I interrupt the remote process with a signal
5490;; (interrupt-process seems not to work)? (Markus Triska)
5491;; * Avoid the local shell entirely for starting remote processes. If
5492;; so, I think even a signal, when delivered directly to the local
5493;; SSH instance, would correctly be propagated to the remote process
5494;; automatically; possibly SSH would have to be started with
5495;; "-t". (Markus Triska)
5496;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5497;; isn't on the remote host. (Mark A. Hershberger)
5498;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5499;; * Optimize out-of-band copying, when both methods are scp-like (not
5500;; rsync).
5501;; * Keep a second connection open for out-of-band methods like scp or
5502;; rsync.
5503;; * Try telnet+curl as new method. It might be useful for busybox,
5504;; without built-in uuencode/uudecode.
5505
5506;;; tramp-sh.el ends here
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index 84d11972115..e48a8b321fd 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -31,6 +31,10 @@
31(eval-when-compile (require 'cl)) ; block, return 31(eval-when-compile (require 'cl)) ; block, return
32(require 'tramp) 32(require 'tramp)
33 33
34;; We call several `tramp-handle-*' functions directly. So we must
35;; reqire that package as well.
36(require 'tramp-sh)
37
34;; Define SMB method ... 38;; Define SMB method ...
35;;;###tramp-autoload 39;;;###tramp-autoload
36(defconst tramp-smb-method "smb" 40(defconst tramp-smb-method "smb"
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index cddfd44c13b..d03201a3afb 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -3,8 +3,6 @@
3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 3;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 5
6;; (copyright statements below in code to be updated with the above notice)
7
8;; Author: Kai Großjohann <kai.grossjohann@gmx.net> 6;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9;; Michael Albinus <michael.albinus@gmx.de> 7;; Michael Albinus <michael.albinus@gmx.de>
10;; Keywords: comm, processes 8;; Keywords: comm, processes
@@ -60,33 +58,8 @@
60 58
61;;; Code: 59;;; Code:
62 60
63;; Since Emacs 23.1, loading messages have been disabled during
64;; autoload. However, loading Tramp takes a while, and it could
65;; happen while typing a filename in the minibuffer. Therefore, Tramp
66;; shall inform about.
67(when (and load-in-progress (null (current-message)))
68 (message "Loading tramp..."))
69
70(require 'tramp-compat) 61(require 'tramp-compat)
71 62
72(require 'format-spec)
73;; As long as password.el is not part of (X)Emacs, it shouldn't
74;; be mandatory
75(if (featurep 'xemacs)
76 (load "password" 'noerror)
77 (or (require 'password-cache nil 'noerror)
78 (require 'password nil 'noerror))) ; from No Gnus, also in tar ball
79
80(require 'shell)
81(require 'advice)
82
83(eval-and-compile
84 (if (featurep 'xemacs)
85 (load "auth-source" 'noerror)
86 (require 'auth-source nil 'noerror)))
87
88(autoload 'uudecode-decode-region "uudecode")
89
90;;; User Customizable Internal Variables: 63;;; User Customizable Internal Variables:
91 64
92(defgroup tramp nil 65(defgroup tramp nil
@@ -202,382 +175,8 @@ See the variable `tramp-encoding-shell' for more information."
202 :group 'tramp 175 :group 'tramp
203 :type 'string) 176 :type 'string)
204 177
205(defcustom tramp-inline-compress-start-size 4096
206 "*The minimum size of compressing where inline transfer.
207When inline transfer, compress transfered data of file
208whose size is this value or above (up to `tramp-copy-size-limit').
209If it is nil, no compression at all will be applied."
210 :group 'tramp
211 :type '(choice (const nil) integer))
212
213(defcustom tramp-copy-size-limit 10240
214 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
215If it is nil, inline out-of-the-band copy will be used without a check."
216 :group 'tramp
217 :type '(choice (const nil) integer))
218
219;;;###tramp-autoload
220(defcustom tramp-terminal-type "dumb"
221 "*Value of TERM environment variable for logging in to remote host.
222Because Tramp wants to parse the output of the remote shell, it is easily
223confused by ANSI color escape sequences and suchlike. Often, shell init
224files conditionalize this setup based on the TERM environment variable."
225 :group 'tramp
226 :type 'string)
227
228;; ksh on OpenBSD 4.5 requires, that PS1 contains a `#' character for
229;; root users. It uses the `$' character for other users. In order
230;; to guarantee a proper prompt, we use "#$" for the prompt.
231
232(defvar tramp-end-of-output
233 (format
234 "///%s#$"
235 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
236 "String used to recognize end of output.
237The '$' character at the end is quoted; the string cannot be
238detected as prompt when being sent on echoing hosts, therefore.")
239
240;;;###tramp-autoload
241(defconst tramp-initial-end-of-output "#$ "
242 "Prompt when establishing a connection.")
243
244;;;###tramp-autoload 178;;;###tramp-autoload
245(defvar tramp-methods 179(defvar tramp-methods nil
246 `(("rcp" (tramp-login-program "rsh")
247 (tramp-login-args (("%h") ("-l" "%u")))
248 (tramp-remote-sh "/bin/sh")
249 (tramp-copy-program "rcp")
250 (tramp-copy-args (("-p" "%k") ("-r")))
251 (tramp-copy-keep-date t)
252 (tramp-copy-recursive t)
253 (tramp-password-end-of-line nil))
254 ("scp" (tramp-login-program "ssh")
255 (tramp-login-args (("-l" "%u") ("-p" "%p")
256 ("-e" "none") ("%h")))
257 (tramp-async-args (("-q")))
258 (tramp-remote-sh "/bin/sh")
259 (tramp-copy-program "scp")
260 (tramp-copy-args (("-P" "%p") ("-p" "%k")
261 ("-q") ("-r")))
262 (tramp-copy-keep-date t)
263 (tramp-copy-recursive t)
264 (tramp-password-end-of-line nil)
265 (tramp-gw-args (("-o"
266 "GlobalKnownHostsFile=/dev/null")
267 ("-o" "UserKnownHostsFile=/dev/null")
268 ("-o" "StrictHostKeyChecking=no")))
269 (tramp-default-port 22))
270 ("scp1" (tramp-login-program "ssh")
271 (tramp-login-args (("-l" "%u") ("-p" "%p")
272 ("-1") ("-e" "none") ("%h")))
273 (tramp-async-args (("-q")))
274 (tramp-remote-sh "/bin/sh")
275 (tramp-copy-program "scp")
276 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
277 ("-q") ("-r")))
278 (tramp-copy-keep-date t)
279 (tramp-copy-recursive t)
280 (tramp-password-end-of-line nil)
281 (tramp-gw-args (("-o"
282 "GlobalKnownHostsFile=/dev/null")
283 ("-o" "UserKnownHostsFile=/dev/null")
284 ("-o" "StrictHostKeyChecking=no")))
285 (tramp-default-port 22))
286 ("scp2" (tramp-login-program "ssh")
287 (tramp-login-args (("-l" "%u") ("-p" "%p")
288 ("-2") ("-e" "none") ("%h")))
289 (tramp-async-args (("-q")))
290 (tramp-remote-sh "/bin/sh")
291 (tramp-copy-program "scp")
292 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
293 ("-q") ("-r")))
294 (tramp-copy-keep-date t)
295 (tramp-copy-recursive t)
296 (tramp-password-end-of-line nil)
297 (tramp-gw-args (("-o"
298 "GlobalKnownHostsFile=/dev/null")
299 ("-o" "UserKnownHostsFile=/dev/null")
300 ("-o" "StrictHostKeyChecking=no")))
301 (tramp-default-port 22))
302 ("scp1_old"
303 (tramp-login-program "ssh1")
304 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
305 ("-e" "none")))
306 (tramp-remote-sh "/bin/sh")
307 (tramp-copy-program "scp1")
308 (tramp-copy-args (("-p" "%k") ("-r")))
309 (tramp-copy-keep-date t)
310 (tramp-copy-recursive t)
311 (tramp-password-end-of-line nil))
312 ("scp2_old"
313 (tramp-login-program "ssh2")
314 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
315 ("-e" "none")))
316 (tramp-remote-sh "/bin/sh")
317 (tramp-copy-program "scp2")
318 (tramp-copy-args (("-p" "%k") ("-r")))
319 (tramp-copy-keep-date t)
320 (tramp-copy-recursive t)
321 (tramp-password-end-of-line nil))
322 ("sftp" (tramp-login-program "ssh")
323 (tramp-login-args (("-l" "%u") ("-p" "%p")
324 ("-e" "none") ("%h")))
325 (tramp-async-args (("-q")))
326 (tramp-remote-sh "/bin/sh")
327 (tramp-copy-program "sftp")
328 (tramp-copy-args nil)
329 (tramp-copy-keep-date nil)
330 (tramp-password-end-of-line nil))
331 ("rsync" (tramp-login-program "ssh")
332 (tramp-login-args (("-l" "%u") ("-p" "%p")
333 ("-e" "none") ("%h")))
334 (tramp-async-args (("-q")))
335 (tramp-remote-sh "/bin/sh")
336 (tramp-copy-program "rsync")
337 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
338 (tramp-copy-keep-date t)
339 (tramp-copy-keep-tmpfile t)
340 (tramp-copy-recursive t)
341 (tramp-password-end-of-line nil))
342 ("rsyncc"
343 (tramp-login-program "ssh")
344 (tramp-login-args (("-l" "%u") ("-p" "%p")
345 ("-o" "ControlPath=%t.%%r@%%h:%%p")
346 ("-o" "ControlMaster=yes")
347 ("-e" "none") ("%h")))
348 (tramp-async-args (("-q")))
349 (tramp-remote-sh "/bin/sh")
350 (tramp-copy-program "rsync")
351 (tramp-copy-args (("-t" "%k") ("-r")))
352 (tramp-copy-env (("RSYNC_RSH")
353 (,(concat
354 "ssh"
355 " -o ControlPath=%t.%%r@%%h:%%p"
356 " -o ControlMaster=auto"))))
357 (tramp-copy-keep-date t)
358 (tramp-copy-keep-tmpfile t)
359 (tramp-copy-recursive t)
360 (tramp-password-end-of-line nil))
361 ("remcp" (tramp-login-program "remsh")
362 (tramp-login-args (("%h") ("-l" "%u")))
363 (tramp-remote-sh "/bin/sh")
364 (tramp-copy-program "rcp")
365 (tramp-copy-args (("-p" "%k")))
366 (tramp-copy-keep-date t)
367 (tramp-password-end-of-line nil))
368 ("rsh" (tramp-login-program "rsh")
369 (tramp-login-args (("%h") ("-l" "%u")))
370 (tramp-remote-sh "/bin/sh")
371 (tramp-copy-program nil)
372 (tramp-copy-args nil)
373 (tramp-copy-keep-date nil)
374 (tramp-password-end-of-line nil))
375 ("ssh" (tramp-login-program "ssh")
376 (tramp-login-args (("-l" "%u") ("-p" "%p")
377 ("-e" "none") ("%h")))
378 (tramp-async-args (("-q")))
379 (tramp-remote-sh "/bin/sh")
380 (tramp-copy-program nil)
381 (tramp-copy-args nil)
382 (tramp-copy-keep-date nil)
383 (tramp-password-end-of-line nil)
384 (tramp-gw-args (("-o"
385 "GlobalKnownHostsFile=/dev/null")
386 ("-o" "UserKnownHostsFile=/dev/null")
387 ("-o" "StrictHostKeyChecking=no")))
388 (tramp-default-port 22))
389 ("ssh1" (tramp-login-program "ssh")
390 (tramp-login-args (("-l" "%u") ("-p" "%p")
391 ("-1") ("-e" "none") ("%h")))
392 (tramp-async-args (("-q")))
393 (tramp-remote-sh "/bin/sh")
394 (tramp-copy-program nil)
395 (tramp-copy-args nil)
396 (tramp-copy-keep-date nil)
397 (tramp-password-end-of-line nil)
398 (tramp-gw-args (("-o"
399 "GlobalKnownHostsFile=/dev/null")
400 ("-o" "UserKnownHostsFile=/dev/null")
401 ("-o" "StrictHostKeyChecking=no")))
402 (tramp-default-port 22))
403 ("ssh2" (tramp-login-program "ssh")
404 (tramp-login-args (("-l" "%u") ("-p" "%p")
405 ("-2") ("-e" "none") ("%h")))
406 (tramp-async-args (("-q")))
407 (tramp-remote-sh "/bin/sh")
408 (tramp-copy-program nil)
409 (tramp-copy-args nil)
410 (tramp-copy-keep-date nil)
411 (tramp-password-end-of-line nil)
412 (tramp-gw-args (("-o"
413 "GlobalKnownHostsFile=/dev/null")
414 ("-o" "UserKnownHostsFile=/dev/null")
415 ("-o" "StrictHostKeyChecking=no")))
416 (tramp-default-port 22))
417 ("ssh1_old"
418 (tramp-login-program "ssh1")
419 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
420 ("-e" "none")))
421 (tramp-async-args (("-q")))
422 (tramp-remote-sh "/bin/sh")
423 (tramp-copy-program nil)
424 (tramp-copy-args nil)
425 (tramp-copy-keep-date nil)
426 (tramp-password-end-of-line nil))
427 ("ssh2_old"
428 (tramp-login-program "ssh2")
429 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
430 ("-e" "none")))
431 (tramp-remote-sh "/bin/sh")
432 (tramp-copy-program nil)
433 (tramp-copy-args nil)
434 (tramp-copy-keep-date nil)
435 (tramp-password-end-of-line nil))
436 ("remsh" (tramp-login-program "remsh")
437 (tramp-login-args (("%h") ("-l" "%u")))
438 (tramp-remote-sh "/bin/sh")
439 (tramp-copy-program nil)
440 (tramp-copy-args nil)
441 (tramp-copy-keep-date nil)
442 (tramp-password-end-of-line nil))
443 ("telnet"
444 (tramp-login-program "telnet")
445 (tramp-login-args (("%h") ("%p")))
446 (tramp-remote-sh "/bin/sh")
447 (tramp-copy-program nil)
448 (tramp-copy-args nil)
449 (tramp-copy-keep-date nil)
450 (tramp-password-end-of-line nil)
451 (tramp-default-port 23))
452 ("su" (tramp-login-program "su")
453 (tramp-login-args (("-") ("%u")))
454 (tramp-remote-sh "/bin/sh")
455 (tramp-copy-program nil)
456 (tramp-copy-args nil)
457 (tramp-copy-keep-date nil)
458 (tramp-password-end-of-line nil))
459 ("sudo" (tramp-login-program "sudo")
460 (tramp-login-args (("-u" "%u")
461 ("-s") ("-H") ("-p" "Password:")))
462 (tramp-remote-sh "/bin/sh")
463 (tramp-copy-program nil)
464 (tramp-copy-args nil)
465 (tramp-copy-keep-date nil)
466 (tramp-password-end-of-line nil))
467 ("scpc" (tramp-login-program "ssh")
468 (tramp-login-args (("-l" "%u") ("-p" "%p")
469 ("-o" "ControlPath=%t.%%r@%%h:%%p")
470 ("-o" "ControlMaster=yes")
471 ("-e" "none") ("%h")))
472 (tramp-async-args (("-q")))
473 (tramp-remote-sh "/bin/sh")
474 (tramp-copy-program "scp")
475 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
476 ("-o" "ControlPath=%t.%%r@%%h:%%p")
477 ("-o" "ControlMaster=auto")))
478 (tramp-copy-keep-date t)
479 (tramp-password-end-of-line nil)
480 (tramp-gw-args (("-o"
481 "GlobalKnownHostsFile=/dev/null")
482 ("-o" "UserKnownHostsFile=/dev/null")
483 ("-o" "StrictHostKeyChecking=no")))
484 (tramp-default-port 22))
485 ("scpx" (tramp-login-program "ssh")
486 (tramp-login-args (("-l" "%u") ("-p" "%p")
487 ("-e" "none") ("-t" "-t")
488 ("%h") ("/bin/sh")))
489 (tramp-async-args (("-q")))
490 (tramp-remote-sh "/bin/sh")
491 (tramp-copy-program "scp")
492 (tramp-copy-args (("-p" "%k")))
493 (tramp-copy-keep-date t)
494 (tramp-password-end-of-line nil)
495 (tramp-gw-args (("-o"
496 "GlobalKnownHostsFile=/dev/null")
497 ("-o" "UserKnownHostsFile=/dev/null")
498 ("-o" "StrictHostKeyChecking=no")))
499 (tramp-default-port 22))
500 ("sshx" (tramp-login-program "ssh")
501 (tramp-login-args (("-l" "%u") ("-p" "%p")
502 ("-e" "none") ("-t" "-t")
503 ("%h") ("/bin/sh")))
504 (tramp-async-args (("-q")))
505 (tramp-remote-sh "/bin/sh")
506 (tramp-copy-program nil)
507 (tramp-copy-args nil)
508 (tramp-copy-keep-date nil)
509 (tramp-password-end-of-line nil)
510 (tramp-gw-args (("-o"
511 "GlobalKnownHostsFile=/dev/null")
512 ("-o" "UserKnownHostsFile=/dev/null")
513 ("-o" "StrictHostKeyChecking=no")))
514 (tramp-default-port 22))
515 ("krlogin"
516 (tramp-login-program "krlogin")
517 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
518 (tramp-remote-sh "/bin/sh")
519 (tramp-copy-program nil)
520 (tramp-copy-args nil)
521 (tramp-copy-keep-date nil)
522 (tramp-password-end-of-line nil))
523 ("plink" (tramp-login-program "plink")
524 (tramp-login-args (("-l" "%u") ("-P" "%p")
525 ("-ssh") ("%h")))
526 (tramp-remote-sh "/bin/sh")
527 (tramp-copy-program nil)
528 (tramp-copy-args nil)
529 (tramp-copy-keep-date nil)
530 (tramp-password-end-of-line "xy") ;see docstring for "xy"
531 (tramp-default-port 22))
532 ("plink1"
533 (tramp-login-program "plink")
534 (tramp-login-args (("-l" "%u") ("-P" "%p")
535 ("-1" "-ssh") ("%h")))
536 (tramp-remote-sh "/bin/sh")
537 (tramp-copy-program nil)
538 (tramp-copy-args nil)
539 (tramp-copy-keep-date nil)
540 (tramp-password-end-of-line "xy") ;see docstring for "xy"
541 (tramp-default-port 22))
542 ("plinkx"
543 (tramp-login-program "plink")
544 ;; ("%h") must be a single element, see
545 ;; `tramp-compute-multi-hops'.
546 (tramp-login-args (("-load") ("%h") ("-t")
547 (,(format
548 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
549 tramp-terminal-type
550 tramp-initial-end-of-output))
551 ("/bin/sh")))
552 (tramp-remote-sh "/bin/sh")
553 (tramp-copy-program nil)
554 (tramp-copy-args nil)
555 (tramp-copy-keep-date nil)
556 (tramp-password-end-of-line nil))
557 ("pscp" (tramp-login-program "plink")
558 (tramp-login-args (("-l" "%u") ("-P" "%p")
559 ("-ssh") ("%h")))
560 (tramp-remote-sh "/bin/sh")
561 (tramp-copy-program "pscp")
562 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
563 (tramp-copy-keep-date t)
564 (tramp-password-end-of-line "xy") ;see docstring for "xy"
565 (tramp-default-port 22))
566 ("psftp" (tramp-login-program "plink")
567 (tramp-login-args (("-l" "%u") ("-P" "%p")
568 ("-ssh") ("%h")))
569 (tramp-remote-sh "/bin/sh")
570 (tramp-copy-program "pscp")
571 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
572 (tramp-copy-keep-date t)
573 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
574 ("fcp" (tramp-login-program "fsh")
575 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
576 (tramp-remote-sh "/bin/sh -i")
577 (tramp-copy-program "fcp")
578 (tramp-copy-args (("-p" "%k")))
579 (tramp-copy-keep-date t)
580 (tramp-password-end-of-line nil)))
581 "*Alist of methods for remote files. 180 "*Alist of methods for remote files.
582This is a list of entries of the form (NAME PARAM1 PARAM2 ...). 181This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
583Each NAME stands for a remote access method. Each PARAM is a 182Each NAME stands for a remote access method. Each PARAM is a
@@ -719,8 +318,7 @@ Also see `tramp-default-method-alist'."
719 :group 'tramp 318 :group 'tramp
720 :type 'string) 319 :type 'string)
721 320
722(defcustom tramp-default-method-alist 321(defcustom tramp-default-method-alist nil
723 '(("\\`localhost\\'" "\\`root\\'" "su"))
724 "*Default method to use for specific host/user pairs. 322 "*Default method to use for specific host/user pairs.
725This is an alist of items (HOST USER METHOD). The first matching item 323This is an alist of items (HOST USER METHOD). The first matching item
726specifies the method to use for a file name which does not specify a 324specifies the method to use for a file name which does not specify a
@@ -737,8 +335,7 @@ See `tramp-methods' for a list of possibilities for METHOD."
737 (regexp :tag "User regexp") 335 (regexp :tag "User regexp")
738 (string :tag "Method")))) 336 (string :tag "Method"))))
739 337
740(defcustom tramp-default-user 338(defcustom tramp-default-user nil
741 nil
742 "*Default user to use for transferring files. 339 "*Default user to use for transferring files.
743It is nil by default; otherwise settings in configuration files like 340It is nil by default; otherwise settings in configuration files like
744\"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'. 341\"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
@@ -747,10 +344,7 @@ This variable is regarded as obsolete, and will be removed soon."
747 :group 'tramp 344 :group 'tramp
748 :type '(choice (const nil) string)) 345 :type '(choice (const nil) string))
749 346
750(defcustom tramp-default-user-alist 347(defcustom tramp-default-user-alist nil
751 `(("\\`su\\(do\\)?\\'" nil "root")
752 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
753 nil ,(user-login-name)))
754 "*Default user to use for specific method/host pairs. 348 "*Default user to use for specific method/host pairs.
755This is an alist of items (METHOD HOST USER). The first matching item 349This is an alist of items (METHOD HOST USER). The first matching item
756specifies the user to use for a file name which does not specify a 350specifies the user to use for a file name which does not specify a
@@ -765,8 +359,7 @@ empty string for the method name."
765 (regexp :tag "Host regexp") 359 (regexp :tag "Host regexp")
766 (string :tag "User")))) 360 (string :tag "User"))))
767 361
768(defcustom tramp-default-host 362(defcustom tramp-default-host (system-name)
769 (system-name)
770 "*Default host to use for transferring files. 363 "*Default host to use for transferring files.
771Useful for su and sudo methods mostly." 364Useful for su and sudo methods mostly."
772 :group 'tramp 365 :group 'tramp
@@ -796,39 +389,6 @@ interpreted as a regular expression which always matches."
796 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$") 389 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
797 "*Host names which are regarded as local host.") 390 "*Host names which are regarded as local host.")
798 391
799(defconst tramp-completion-function-alist-rsh
800 '((tramp-parse-rhosts "/etc/hosts.equiv")
801 (tramp-parse-rhosts "~/.rhosts"))
802 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
803
804(defconst tramp-completion-function-alist-ssh
805 '((tramp-parse-rhosts "/etc/hosts.equiv")
806 (tramp-parse-rhosts "/etc/shosts.equiv")
807 (tramp-parse-shosts "/etc/ssh_known_hosts")
808 (tramp-parse-sconfig "/etc/ssh_config")
809 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
810 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
811 (tramp-parse-rhosts "~/.rhosts")
812 (tramp-parse-rhosts "~/.shosts")
813 (tramp-parse-shosts "~/.ssh/known_hosts")
814 (tramp-parse-sconfig "~/.ssh/config")
815 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
816 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
817 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
818
819(defconst tramp-completion-function-alist-telnet
820 '((tramp-parse-hosts "/etc/hosts"))
821 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
822
823(defconst tramp-completion-function-alist-su
824 '((tramp-parse-passwd "/etc/passwd"))
825 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
826
827(defconst tramp-completion-function-alist-putty
828 '((tramp-parse-putty
829 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
830 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
831
832(defvar tramp-completion-function-alist nil 392(defvar tramp-completion-function-alist nil
833 "*Alist of methods for remote files. 393 "*Alist of methods for remote files.
834This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\). 394This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
@@ -849,63 +409,6 @@ names from FILE for completion. The following predefined FUNCTIONs exists:
849FUNCTION can also be a customer defined function. For more details see 409FUNCTION can also be a customer defined function. For more details see
850the info pages.") 410the info pages.")
851 411
852(eval-after-load "tramp"
853 '(progn
854 (tramp-set-completion-function
855 "rcp" tramp-completion-function-alist-rsh)
856 (tramp-set-completion-function
857 "scp" tramp-completion-function-alist-ssh)
858 (tramp-set-completion-function
859 "scp1" tramp-completion-function-alist-ssh)
860 (tramp-set-completion-function
861 "scp2" tramp-completion-function-alist-ssh)
862 (tramp-set-completion-function
863 "scp1_old" tramp-completion-function-alist-ssh)
864 (tramp-set-completion-function
865 "scp2_old" tramp-completion-function-alist-ssh)
866 (tramp-set-completion-function
867 "rsync" tramp-completion-function-alist-ssh)
868 (tramp-set-completion-function
869 "rsyncc" tramp-completion-function-alist-ssh)
870 (tramp-set-completion-function
871 "remcp" tramp-completion-function-alist-rsh)
872 (tramp-set-completion-function
873 "rsh" tramp-completion-function-alist-rsh)
874 (tramp-set-completion-function
875 "ssh" tramp-completion-function-alist-ssh)
876 (tramp-set-completion-function
877 "ssh1" tramp-completion-function-alist-ssh)
878 (tramp-set-completion-function
879 "ssh2" tramp-completion-function-alist-ssh)
880 (tramp-set-completion-function
881 "ssh1_old" tramp-completion-function-alist-ssh)
882 (tramp-set-completion-function
883 "ssh2_old" tramp-completion-function-alist-ssh)
884 (tramp-set-completion-function
885 "remsh" tramp-completion-function-alist-rsh)
886 (tramp-set-completion-function
887 "telnet" tramp-completion-function-alist-telnet)
888 (tramp-set-completion-function
889 "su" tramp-completion-function-alist-su)
890 (tramp-set-completion-function
891 "sudo" tramp-completion-function-alist-su)
892 (tramp-set-completion-function
893 "scpx" tramp-completion-function-alist-ssh)
894 (tramp-set-completion-function
895 "sshx" tramp-completion-function-alist-ssh)
896 (tramp-set-completion-function
897 "krlogin" tramp-completion-function-alist-rsh)
898 (tramp-set-completion-function
899 "plink" tramp-completion-function-alist-ssh)
900 (tramp-set-completion-function
901 "plink1" tramp-completion-function-alist-ssh)
902 (tramp-set-completion-function
903 "plinkx" tramp-completion-function-alist-putty)
904 (tramp-set-completion-function
905 "pscp" tramp-completion-function-alist-ssh)
906 (tramp-set-completion-function
907 "fcp" tramp-completion-function-alist-ssh)))
908
909(defconst tramp-echo-mark-marker "_echo" 412(defconst tramp-echo-mark-marker "_echo"
910 "String marker to surround echoed commands.") 413 "String marker to surround echoed commands.")
911 414
@@ -954,55 +457,6 @@ The default value is to use the same value as `tramp-rsh-end-of-line'."
954 :group 'tramp 457 :group 'tramp
955 :type 'string) 458 :type 'string)
956 459
957;; "getconf PATH" yields:
958;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
959;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
960;; GNU/Linux (Debian, Suse): /bin:/usr/bin
961;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
962;; IRIX64: /usr/bin
963(defcustom tramp-remote-path
964 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
965 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
966 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
967 "*List of directories to search for executables on remote host.
968For every remote host, this variable will be set buffer local,
969keeping the list of existing directories on that host.
970
971You can use `~' in this list, but when searching for a shell which groks
972tilde expansion, all directory names starting with `~' will be ignored.
973
974`Default Directories' represent the list of directories given by
975the command \"getconf PATH\". It is recommended to use this
976entry on top of this list, because these are the default
977directories for POSIX compatible commands.
978
979`Private Directories' are the settings of the $PATH environment,
980as given in your `~/.profile'."
981 :group 'tramp
982 :type '(repeat (choice
983 (const :tag "Default Directories" tramp-default-remote-path)
984 (const :tag "Private Directories" tramp-own-remote-path)
985 (string :tag "Directory"))))
986
987(defcustom tramp-remote-process-environment
988 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
989 ,(format "TERM=%s" tramp-terminal-type)
990 "EMACS=t" ;; Deprecated.
991 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
992 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
993 "autocorrect=" "correct=")
994
995 "*List of environment variables to be set on the remote host.
996
997Each element should be a string of the form ENVVARNAME=VALUE. An
998entry ENVVARNAME= diables the corresponding environment variable,
999which might have been set in the init files like ~/.profile.
1000
1001Special handling is applied to the PATH environment, which should
1002not be set here. Instead of, it should be set via `tramp-remote-path'."
1003 :group 'tramp
1004 :type '(repeat string))
1005
1006(defcustom tramp-login-prompt-regexp 460(defcustom tramp-login-prompt-regexp
1007 ".*ogin\\( .*\\)?: *" 461 ".*ogin\\( .*\\)?: *"
1008 "*Regexp matching login-like prompts. 462 "*Regexp matching login-like prompts.
@@ -1130,15 +584,13 @@ The answer will be provided by `tramp-action-process-alive',
1130 :group 'tramp 584 :group 'tramp
1131 :type 'regexp) 585 :type 'regexp)
1132 586
1133(defcustom tramp-temp-name-prefix "tramp." 587(defconst tramp-temp-name-prefix "tramp."
1134 "*Prefix to use for temporary files. 588 "*Prefix to use for temporary files.
1135If this is a relative file name (such as \"tramp.\"), it is considered 589If this is a relative file name (such as \"tramp.\"), it is considered
1136relative to the directory name returned by the function 590relative to the directory name returned by the function
1137`tramp-compat-temporary-file-directory' (which see). It may also be an 591`tramp-compat-temporary-file-directory' (which see). It may also be an
1138absolute file name; don't forget to include a prefix for the filename 592absolute file name; don't forget to include a prefix for the filename
1139part, though." 593part, though.")
1140 :group 'tramp
1141 :type 'string)
1142 594
1143(defconst tramp-temp-buffer-name " *tramp temp*" 595(defconst tramp-temp-buffer-name " *tramp temp*"
1144 "Buffer name for a temporary buffer. 596 "Buffer name for a temporary buffer.
@@ -1149,22 +601,6 @@ It shall be used in combination with `generate-new-buffer-name'.")
1149Useful for \"rsync\" like methods.") 601Useful for \"rsync\" like methods.")
1150(make-variable-buffer-local 'tramp-temp-buffer-file-name) 602(make-variable-buffer-local 'tramp-temp-buffer-file-name)
1151 603
1152(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1153 "*Alist specifying extra arguments to pass to the remote shell.
1154Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1155matching the shell file name and ARGS is a string specifying the
1156arguments.
1157
1158This variable is only used when Tramp needs to start up another shell
1159for tilde expansion. The extra arguments should typically prevent the
1160shell from reading its init file."
1161 :group 'tramp
1162 ;; This might be the wrong way to test whether the widget type
1163 ;; `alist' is available. Who knows the right way to test it?
1164 :type (if (get 'alist 'widget-type)
1165 '(alist :key-type string :value-type string)
1166 '(repeat (cons string string))))
1167
1168;; XEmacs is distributed with few Lisp packages. Further packages are 604;; XEmacs is distributed with few Lisp packages. Further packages are
1169;; installed using EFS. If we use a unified filename format, then 605;; installed using EFS. If we use a unified filename format, then
1170;; Tramp is required in addition to EFS. (But why can't Tramp just 606;; Tramp is required in addition to EFS. (But why can't Tramp just
@@ -1223,8 +659,7 @@ Used in `tramp-make-tramp-file-name'.")
1223 "*Regexp matching delimeter between method and user or host names. 659 "*Regexp matching delimeter between method and user or host names.
1224Derived from `tramp-postfix-method-format'.") 660Derived from `tramp-postfix-method-format'.")
1225 661
1226(defconst tramp-user-regexp 662(defconst tramp-user-regexp "[^:/ \t]+"
1227 "[^:/ \t]+"
1228 "*Regexp matching user names.") 663 "*Regexp matching user names.")
1229 664
1230(defconst tramp-prefix-domain-format "%" 665(defconst tramp-prefix-domain-format "%"
@@ -1235,8 +670,7 @@ Derived from `tramp-postfix-method-format'.")
1235 "*Regexp matching delimeter between user and domain names. 670 "*Regexp matching delimeter between user and domain names.
1236Derived from `tramp-prefix-domain-format'.") 671Derived from `tramp-prefix-domain-format'.")
1237 672
1238(defconst tramp-domain-regexp 673(defconst tramp-domain-regexp "[-a-zA-Z0-9_.]+"
1239 "[-a-zA-Z0-9_.]+"
1240 "*Regexp matching domain names.") 674 "*Regexp matching domain names.")
1241 675
1242(defconst tramp-user-with-domain-regexp 676(defconst tramp-user-with-domain-regexp
@@ -1245,8 +679,7 @@ Derived from `tramp-prefix-domain-format'.")
1245 "\\(" tramp-domain-regexp "\\)") 679 "\\(" tramp-domain-regexp "\\)")
1246 "*Regexp matching user names with domain names.") 680 "*Regexp matching user names with domain names.")
1247 681
1248(defconst tramp-postfix-user-format 682(defconst tramp-postfix-user-format "@"
1249 "@"
1250 "*String matching delimeter between user and host names. 683 "*String matching delimeter between user and host names.
1251Used in `tramp-make-tramp-file-name'.") 684Used in `tramp-make-tramp-file-name'.")
1252 685
@@ -1255,8 +688,7 @@ Used in `tramp-make-tramp-file-name'.")
1255 "*Regexp matching delimeter between user and host names. 688 "*Regexp matching delimeter between user and host names.
1256Derived from `tramp-postfix-user-format'.") 689Derived from `tramp-postfix-user-format'.")
1257 690
1258(defconst tramp-host-regexp 691(defconst tramp-host-regexp "[a-zA-Z0-9_.-]+"
1259 "[a-zA-Z0-9_.-]+"
1260 "*Regexp matching host names.") 692 "*Regexp matching host names.")
1261 693
1262(defconst tramp-prefix-ipv6-format 694(defconst tramp-prefix-ipv6-format
@@ -1304,8 +736,7 @@ Derived from `tramp-postfix-ipv6-format'.")
1304 "*Regexp matching delimeter between host names and port numbers. 736 "*Regexp matching delimeter between host names and port numbers.
1305Derived from `tramp-prefix-port-format'.") 737Derived from `tramp-prefix-port-format'.")
1306 738
1307(defconst tramp-port-regexp 739(defconst tramp-port-regexp "[0-9]+"
1308 "[0-9]+"
1309 "*Regexp matching port numbers.") 740 "*Regexp matching port numbers.")
1310 741
1311(defconst tramp-host-with-port-regexp 742(defconst tramp-host-with-port-regexp
@@ -1327,8 +758,7 @@ Used in `tramp-make-tramp-file-name'.")
1327 "*Regexp matching delimeter between host names and localnames. 758 "*Regexp matching delimeter between host names and localnames.
1328Derived from `tramp-postfix-host-format'.") 759Derived from `tramp-postfix-host-format'.")
1329 760
1330(defconst tramp-localname-regexp 761(defconst tramp-localname-regexp ".*$"
1331 ".*$"
1332 "*Regexp matching localnames.") 762 "*Regexp matching localnames.")
1333 763
1334;; File name format. 764;; File name format.
@@ -1376,15 +806,13 @@ Tramp. See `tramp-file-name-structure' for more explanations.
1376On W32 systems, the volume letter must be ignored.") 806On W32 systems, the volume letter must be ignored.")
1377 807
1378;;;###autoload 808;;;###autoload
1379(defconst tramp-file-name-regexp-separate 809(defconst tramp-file-name-regexp-separate "\\`/\\[.*\\]"
1380 "\\`/\\[.*\\]"
1381 "Value for `tramp-file-name-regexp' for separate remoting. 810 "Value for `tramp-file-name-regexp' for separate remoting.
1382XEmacs uses a separate filename syntax for Tramp and EFS. 811XEmacs uses a separate filename syntax for Tramp and EFS.
1383See `tramp-file-name-structure' for more explanations.") 812See `tramp-file-name-structure' for more explanations.")
1384 813
1385;;;###autoload 814;;;###autoload
1386(defconst tramp-file-name-regexp-url 815(defconst tramp-file-name-regexp-url "\\`/[^/:]+://"
1387 "\\`/[^/:]+://"
1388 "Value for `tramp-file-name-regexp' for URL-like remoting. 816 "Value for `tramp-file-name-regexp' for URL-like remoting.
1389See `tramp-file-name-structure' for more explanations.") 817See `tramp-file-name-structure' for more explanations.")
1390 818
@@ -1458,38 +886,6 @@ updated after changing this variable.
1458 886
1459Also see `tramp-file-name-structure'.") 887Also see `tramp-file-name-structure'.")
1460 888
1461(defconst tramp-actions-before-shell
1462 '((tramp-login-prompt-regexp tramp-action-login)
1463 (tramp-password-prompt-regexp tramp-action-password)
1464 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1465 (shell-prompt-pattern tramp-action-succeed)
1466 (tramp-shell-prompt-pattern tramp-action-succeed)
1467 (tramp-yesno-prompt-regexp tramp-action-yesno)
1468 (tramp-yn-prompt-regexp tramp-action-yn)
1469 (tramp-terminal-prompt-regexp tramp-action-terminal)
1470 (tramp-process-alive-regexp tramp-action-process-alive))
1471 "List of pattern/action pairs.
1472Whenever a pattern matches, the corresponding action is performed.
1473Each item looks like (PATTERN ACTION).
1474
1475The PATTERN should be a symbol, a variable. The value of this
1476variable gives the regular expression to search for. Note that the
1477regexp must match at the end of the buffer, \"\\'\" is implicitly
1478appended to it.
1479
1480The ACTION should also be a symbol, but a function. When the
1481corresponding PATTERN matches, the ACTION function is called.")
1482
1483(defconst tramp-actions-copy-out-of-band
1484 '((tramp-password-prompt-regexp tramp-action-password)
1485 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1486 (tramp-copy-failed-regexp tramp-action-permission-denied)
1487 (tramp-process-alive-regexp tramp-action-out-of-band))
1488 "List of pattern/action pairs.
1489This list is used for copying/renaming with out-of-band methods.
1490
1491See `tramp-actions-before-shell' for more info.")
1492
1493;; Chunked sending kludge. We set this to 500 for black-listed constellations 889;; Chunked sending kludge. We set this to 500 for black-listed constellations
1494;; known to have a bug in `process-send-string'; some ssh connections appear 890;; known to have a bug in `process-send-string'; some ssh connections appear
1495;; to drop bytes when data is sent too quickly. There is also a connection 891;; to drop bytes when data is sent too quickly. There is also a connection
@@ -1595,432 +991,18 @@ means to use always cached values for the directory contents."
1595(defvar tramp-current-host nil 991(defvar tramp-current-host nil
1596 "Remote host for this *tramp* buffer.") 992 "Remote host for this *tramp* buffer.")
1597 993
1598(defconst tramp-uudecode
1599 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1600cat /tmp/tramp.$$
1601rm -f /tmp/tramp.$$"
1602 "Shell function to implement `uudecode' to standard output.
1603Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1604for this or `uudecode -p', but some systems don't, and for them
1605we have this shell function.")
1606
1607(defconst tramp-perl-file-truename
1608 "%s -e '
1609use File::Spec;
1610use Cwd \"realpath\";
1611
1612sub recursive {
1613 my ($volume, @dirs) = @_;
1614 my $real = realpath(File::Spec->catpath(
1615 $volume, File::Spec->catdir(@dirs), \"\"));
1616 if ($real) {
1617 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1618 return ($vol, File::Spec->splitdir($dir));
1619 }
1620 else {
1621 my $last = pop(@dirs);
1622 ($volume, @dirs) = recursive($volume, @dirs);
1623 push(@dirs, $last);
1624 return ($volume, @dirs);
1625 }
1626}
1627
1628$result = realpath($ARGV[0]);
1629if (!$result) {
1630 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1631 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1632
1633 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1634}
1635
1636if ($ARGV[0] =~ /\\/$/) {
1637 $result = $result . \"/\";
1638}
1639
1640print \"\\\"$result\\\"\\n\";
1641' \"$1\" 2>/dev/null"
1642 "Perl script to produce output suitable for use with `file-truename'
1643on the remote file system.
1644Escape sequence %s is replaced with name of Perl binary.
1645This string is passed to `format', so percent characters need to be doubled.")
1646
1647(defconst tramp-perl-file-name-all-completions
1648 "%s -e 'sub case {
1649 my $str = shift;
1650 if ($ARGV[2]) {
1651 return lc($str);
1652 }
1653 else {
1654 return $str;
1655 }
1656}
1657opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1658@files = readdir(d); closedir(d);
1659foreach $f (@files) {
1660 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1661 if (-d \"$ARGV[0]/$f\") {
1662 print \"$f/\\n\";
1663 }
1664 else {
1665 print \"$f\\n\";
1666 }
1667 }
1668}
1669print \"ok\\n\"
1670' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1671 "Perl script to produce output suitable for use with
1672`file-name-all-completions' on the remote file system. Escape
1673sequence %s is replaced with name of Perl binary. This string is
1674passed to `format', so percent characters need to be doubled.")
1675
1676;; Perl script to implement `file-attributes' in a Lisp `read'able
1677;; output. If you are hacking on this, note that you get *no* output
1678;; unless this spits out a complete line, including the '\n' at the
1679;; end.
1680;; The device number is returned as "-1", because there will be a virtual
1681;; device number set in `tramp-handle-file-attributes'.
1682(defconst tramp-perl-file-attributes
1683 "%s -e '
1684@stat = lstat($ARGV[0]);
1685if (!@stat) {
1686 print \"nil\\n\";
1687 exit 0;
1688}
1689if (($stat[2] & 0170000) == 0120000)
1690{
1691 $type = readlink($ARGV[0]);
1692 $type = \"\\\"$type\\\"\";
1693}
1694elsif (($stat[2] & 0170000) == 040000)
1695{
1696 $type = \"t\";
1697}
1698else
1699{
1700 $type = \"nil\"
1701};
1702$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1703$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1704printf(
1705 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1706 $type,
1707 $stat[3],
1708 $uid,
1709 $gid,
1710 $stat[8] >> 16 & 0xffff,
1711 $stat[8] & 0xffff,
1712 $stat[9] >> 16 & 0xffff,
1713 $stat[9] & 0xffff,
1714 $stat[10] >> 16 & 0xffff,
1715 $stat[10] & 0xffff,
1716 $stat[7],
1717 $stat[2],
1718 $stat[1] >> 16 & 0xffff,
1719 $stat[1] & 0xffff
1720);' \"$1\" \"$2\" 2>/dev/null"
1721 "Perl script to produce output suitable for use with `file-attributes'
1722on the remote file system.
1723Escape sequence %s is replaced with name of Perl binary.
1724This string is passed to `format', so percent characters need to be doubled.")
1725
1726(defconst tramp-perl-directory-files-and-attributes
1727 "%s -e '
1728chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1729opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1730@list = readdir(DIR);
1731closedir(DIR);
1732$n = scalar(@list);
1733printf(\"(\\n\");
1734for($i = 0; $i < $n; $i++)
1735{
1736 $filename = $list[$i];
1737 @stat = lstat($filename);
1738 if (($stat[2] & 0170000) == 0120000)
1739 {
1740 $type = readlink($filename);
1741 $type = \"\\\"$type\\\"\";
1742 }
1743 elsif (($stat[2] & 0170000) == 040000)
1744 {
1745 $type = \"t\";
1746 }
1747 else
1748 {
1749 $type = \"nil\"
1750 };
1751 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1752 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1753 printf(
1754 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1755 $filename,
1756 $type,
1757 $stat[3],
1758 $uid,
1759 $gid,
1760 $stat[8] >> 16 & 0xffff,
1761 $stat[8] & 0xffff,
1762 $stat[9] >> 16 & 0xffff,
1763 $stat[9] & 0xffff,
1764 $stat[10] >> 16 & 0xffff,
1765 $stat[10] & 0xffff,
1766 $stat[7],
1767 $stat[2],
1768 $stat[1] >> 16 & 0xffff,
1769 $stat[1] & 0xffff,
1770 $stat[0] >> 16 & 0xffff,
1771 $stat[0] & 0xffff);
1772}
1773printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
1774 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1775output.
1776Escape sequence %s is replaced with name of Perl binary.
1777This string is passed to `format', so percent characters need to be doubled.")
1778
1779;; ;; These two use uu encoding.
1780;; (defvar tramp-perl-encode "%s -e'\
1781;; print qq(begin 644 xxx\n);
1782;; my $s = q();
1783;; my $res = q();
1784;; while (read(STDIN, $s, 45)) {
1785;; print pack(q(u), $s);
1786;; }
1787;; print qq(`\n);
1788;; print qq(end\n);
1789;; '"
1790;; "Perl program to use for encoding a file.
1791;; Escape sequence %s is replaced with name of Perl binary.")
1792
1793;; (defvar tramp-perl-decode "%s -ne '
1794;; print unpack q(u), $_;
1795;; '"
1796;; "Perl program to use for decoding a file.
1797;; Escape sequence %s is replaced with name of Perl binary.")
1798
1799;; These two use base64 encoding.
1800(defconst tramp-perl-encode-with-module
1801 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1802 "Perl program to use for encoding a file.
1803Escape sequence %s is replaced with name of Perl binary.
1804This string is passed to `format', so percent characters need to be doubled.
1805This implementation requires the MIME::Base64 Perl module to be installed
1806on the remote host.")
1807
1808(defconst tramp-perl-decode-with-module
1809 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1810 "Perl program to use for decoding a file.
1811Escape sequence %s is replaced with name of Perl binary.
1812This string is passed to `format', so percent characters need to be doubled.
1813This implementation requires the MIME::Base64 Perl module to be installed
1814on the remote host.")
1815
1816(defconst tramp-perl-encode
1817 "%s -e '
1818# This script contributed by Juanma Barranquero <lektu@terra.es>.
1819# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1820# Free Software Foundation, Inc.
1821use strict;
1822
1823my %%trans = do {
1824 my $i = 0;
1825 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1826 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1827};
1828
1829binmode(\\*STDIN);
1830
1831# We read in chunks of 54 bytes, to generate output lines
1832# of 72 chars (plus end of line)
1833$/ = \\54;
1834
1835while (my $data = <STDIN>) {
1836 my $pad = q();
1837
1838 # Only for the last chunk, and only if did not fill the last three-byte packet
1839 if (eof) {
1840 my $mod = length($data) %% 3;
1841 $pad = q(=) x (3 - $mod) if $mod;
1842 }
1843
1844 # Not the fastest method, but it is simple: unpack to binary string, split
1845 # by groups of 6 bits and convert back from binary to byte; then map into
1846 # the translation table
1847 print
1848 join q(),
1849 map($trans{$_},
1850 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1851 $pad,
1852 qq(\\n);
1853}' 2>/dev/null"
1854 "Perl program to use for encoding a file.
1855Escape sequence %s is replaced with name of Perl binary.
1856This string is passed to `format', so percent characters need to be doubled.")
1857
1858(defconst tramp-perl-decode
1859 "%s -e '
1860# This script contributed by Juanma Barranquero <lektu@terra.es>.
1861# Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1862# Free Software Foundation, Inc.
1863use strict;
1864
1865my %%trans = do {
1866 my $i = 0;
1867 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1868 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1869};
1870
1871my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1872
1873binmode(\\*STDOUT);
1874
1875# We are going to accumulate into $pending to accept any line length
1876# (we do not check they are <= 76 chars as the RFC says)
1877my $pending = q();
1878
1879while (my $data = <STDIN>) {
1880 chomp $data;
1881
1882 # If we find one or two =, we have reached the end and
1883 # any following data is to be discarded
1884 my $finished = $data =~ s/(==?).*/$1/;
1885 $pending .= $data;
1886
1887 my $len = length($pending);
1888 my $chunk = substr($pending, 0, $len & ~3);
1889 $pending = substr($pending, $len & ~3 + 1);
1890
1891 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1892 # split in 8-bit chunks and convert back to char.
1893 print join q(),
1894 map $bytes{$_},
1895 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1896
1897 last if $finished;
1898}' 2>/dev/null"
1899 "Perl program to use for decoding a file.
1900Escape sequence %s is replaced with name of Perl binary.
1901This string is passed to `format', so percent characters need to be doubled.")
1902
1903(defconst tramp-vc-registered-read-file-names
1904 "echo \"(\"
1905while read file; do
1906 if %s \"$file\"; then
1907 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1908 else
1909 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1910 fi
1911 if %s \"$file\"; then
1912 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1913 else
1914 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1915 fi
1916done
1917echo \")\""
1918 "Script to check existence of VC related files.
1919It must be send formatted with two strings; the tests for file
1920existence, and file readability. Input shall be read via
1921here-document, otherwise the command could exceed maximum length
1922of command line.")
1923
1924(defconst tramp-file-mode-type-map
1925 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1926 (1 . "p") ; fifo
1927 (2 . "c") ; character device
1928 (3 . "m") ; multiplexed character device (v7)
1929 (4 . "d") ; directory
1930 (5 . "?") ; Named special file (XENIX)
1931 (6 . "b") ; block device
1932 (7 . "?") ; multiplexed block device (v7)
1933 (8 . "-") ; regular file
1934 (9 . "n") ; network special file (HP-UX)
1935 (10 . "l") ; symlink
1936 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1937 (12 . "s") ; socket
1938 (13 . "D") ; door special (Solaris)
1939 (14 . "w")) ; whiteout (BSD)
1940 "A list of file types returned from the `stat' system call.
1941This is used to map a mode number to a permission string.")
1942
1943;; New handlers should be added here. The following operations can be
1944;; handled using the normal primitives: file-name-sans-versions,
1945;; get-file-buffer.
1946(defconst tramp-file-name-handler-alist
1947 '((load . tramp-handle-load)
1948 (make-symbolic-link . tramp-handle-make-symbolic-link)
1949 (file-name-as-directory . tramp-handle-file-name-as-directory)
1950 (file-name-directory . tramp-handle-file-name-directory)
1951 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1952 (file-truename . tramp-handle-file-truename)
1953 (file-exists-p . tramp-handle-file-exists-p)
1954 (file-directory-p . tramp-handle-file-directory-p)
1955 (file-executable-p . tramp-handle-file-executable-p)
1956 (file-readable-p . tramp-handle-file-readable-p)
1957 (file-regular-p . tramp-handle-file-regular-p)
1958 (file-symlink-p . tramp-handle-file-symlink-p)
1959 (file-writable-p . tramp-handle-file-writable-p)
1960 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1961 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1962 (file-attributes . tramp-handle-file-attributes)
1963 (file-modes . tramp-handle-file-modes)
1964 (directory-files . tramp-handle-directory-files)
1965 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1966 (file-name-all-completions . tramp-handle-file-name-all-completions)
1967 (file-name-completion . tramp-handle-file-name-completion)
1968 (add-name-to-file . tramp-handle-add-name-to-file)
1969 (copy-file . tramp-handle-copy-file)
1970 (copy-directory . tramp-handle-copy-directory)
1971 (rename-file . tramp-handle-rename-file)
1972 (set-file-modes . tramp-handle-set-file-modes)
1973 (set-file-times . tramp-handle-set-file-times)
1974 (make-directory . tramp-handle-make-directory)
1975 (delete-directory . tramp-handle-delete-directory)
1976 (delete-file . tramp-handle-delete-file)
1977 (directory-file-name . tramp-handle-directory-file-name)
1978 ;; `executable-find' is not official yet.
1979 (executable-find . tramp-handle-executable-find)
1980 (start-file-process . tramp-handle-start-file-process)
1981 (process-file . tramp-handle-process-file)
1982 (shell-command . tramp-handle-shell-command)
1983 (insert-directory . tramp-handle-insert-directory)
1984 (expand-file-name . tramp-handle-expand-file-name)
1985 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
1986 (file-local-copy . tramp-handle-file-local-copy)
1987 (file-remote-p . tramp-handle-file-remote-p)
1988 (insert-file-contents . tramp-handle-insert-file-contents)
1989 (insert-file-contents-literally
1990 . tramp-handle-insert-file-contents-literally)
1991 (write-region . tramp-handle-write-region)
1992 (find-backup-file-name . tramp-handle-find-backup-file-name)
1993 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1994 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1995 (dired-compress-file . tramp-handle-dired-compress-file)
1996 (dired-recursive-delete-directory
1997 . tramp-handle-dired-recursive-delete-directory)
1998 (dired-uncache . tramp-handle-dired-uncache)
1999 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
2000 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
2001 (file-selinux-context . tramp-handle-file-selinux-context)
2002 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
2003 (vc-registered . tramp-handle-vc-registered))
2004 "Alist of handler functions.
2005Operations not mentioned here will be handled by the normal Emacs functions.")
2006
2007;; Handlers for partial Tramp file names. For Emacs just
2008;; `file-name-all-completions' is needed.
2009;;;###autoload 994;;;###autoload
2010(defconst tramp-completion-file-name-handler-alist 995(defconst tramp-completion-file-name-handler-alist
2011 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions) 996 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
2012 (file-name-completion . tramp-completion-handle-file-name-completion)) 997 (file-name-completion . tramp-completion-handle-file-name-completion))
2013 "Alist of completion handler functions. 998 "Alist of completion handler functions.
2014Used for file names matching `tramp-file-name-regexp'. Operations not 999Used for file names matching `tramp-file-name-regexp'. Operations
2015mentioned here will be handled by `tramp-file-name-handler-alist' or the 1000not mentioned here will be handled by Tramp's file name handler
2016normal Emacs functions.") 1001functions, or the normal Emacs functions.")
2017 1002
2018;; Handlers for foreign methods, like FTP or SMB, shall be plugged here. 1003;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
2019;;;###tramp-autoload 1004;;;###tramp-autoload
2020(defvar tramp-foreign-file-name-handler-alist 1005(defvar tramp-foreign-file-name-handler-alist nil
2021 ;; (identity . tramp-sh-file-name-handler) should always be the last
2022 ;; entry, because `identity' always matches.
2023 '((identity . tramp-sh-file-name-handler))
2024 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially. 1006 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2025If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by 1007If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2026calling HANDLER.") 1008calling HANDLER.")
@@ -2150,10 +1132,6 @@ values."
2150 (user (match-string (nth 2 tramp-file-name-structure) name)) 1132 (user (match-string (nth 2 tramp-file-name-structure) name))
2151 (host (match-string (nth 3 tramp-file-name-structure) name)) 1133 (host (match-string (nth 3 tramp-file-name-structure) name))
2152 (localname (match-string (nth 4 tramp-file-name-structure) name))) 1134 (localname (match-string (nth 4 tramp-file-name-structure) name)))
2153 (when (member method '("multi" "multiu"))
2154 (error
2155 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
2156 method))
2157 (when host 1135 (when host
2158 (when (string-match tramp-prefix-ipv6-regexp host) 1136 (when (string-match tramp-prefix-ipv6-regexp host)
2159 (setq host (replace-match "" nil t host))) 1137 (setq host (replace-match "" nil t host)))
@@ -2205,7 +1183,8 @@ necessary only. This function will be used in file name completion."
2205 (when (not (zerop (length host))) 1183 (when (not (zerop (length host)))
2206 (concat 1184 (concat
2207 (if (string-match tramp-ipv6-regexp host) 1185 (if (string-match tramp-ipv6-regexp host)
2208 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format) 1186 (concat
1187 tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
2209 host) 1188 host)
2210 tramp-postfix-host-format)) 1189 tramp-postfix-host-format))
2211 (when localname localname))) 1190 (when localname localname)))
@@ -2251,7 +1230,15 @@ from the default one."
2251 (format "*debug tramp/%s %s*" method host)))) 1230 (format "*debug tramp/%s %s*" method host))))
2252 1231
2253(defconst tramp-debug-outline-regexp 1232(defconst tramp-debug-outline-regexp
2254 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #") 1233 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #"
1234 "Used for highlighting Tramp debug buffers in `outline-mode'.")
1235
1236(defun tramp-debug-outline-level ()
1237 "Return the depth to which a statement is nested in the outline.
1238Point must be at the beginning of a header line.
1239
1240The outline level is equal to the verbosity of the Tramp message."
1241 (1+ (string-to-number (match-string 1))))
2255 1242
2256(defun tramp-get-debug-buffer (vec) 1243(defun tramp-get-debug-buffer (vec)
2257 "Get the debug buffer for VEC." 1244 "Get the debug buffer for VEC."
@@ -2268,16 +1255,9 @@ from the default one."
2268 (outline-regexp tramp-debug-outline-regexp)) 1255 (outline-regexp tramp-debug-outline-regexp))
2269 (outline-mode)) 1256 (outline-mode))
2270 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp) 1257 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
2271 (set (make-local-variable 'outline-level) 'tramp-outline-level)) 1258 (set (make-local-variable 'outline-level) 'tramp-debug-outline-level))
2272 (current-buffer))) 1259 (current-buffer)))
2273 1260
2274(defun tramp-outline-level ()
2275 "Return the depth to which a statement is nested in the outline.
2276Point must be at the beginning of a header line.
2277
2278The outline level is equal to the verbosity of the Tramp message."
2279 (1+ (string-to-number (match-string 1))))
2280
2281(defsubst tramp-debug-message (vec fmt-string &rest args) 1261(defsubst tramp-debug-message (vec fmt-string &rest args)
2282 "Append message to debug buffer. 1262 "Append message to debug buffer.
2283Message is formatted with FMT-STRING as control string and the remaining 1263Message is formatted with FMT-STRING as control string and the remaining
@@ -2344,36 +1324,34 @@ is greater than or equal 4.
2344Calls functions `message' and `tramp-debug-message' with FMT-STRING as 1324Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2345control string and the remaining ARGS to actually emit the message (if 1325control string and the remaining ARGS to actually emit the message (if
2346applicable)." 1326applicable)."
2347 (condition-case nil 1327 (ignore-errors
2348 (when (<= level tramp-verbose) 1328 (when (<= level tramp-verbose)
2349 ;; Match data must be preserved! 1329 ;; Match data must be preserved!
2350 (save-match-data 1330 (save-match-data
2351 ;; Display only when there is a minimum level. 1331 ;; Display only when there is a minimum level.
2352 (when (and tramp-message-show-message (<= level 3)) 1332 (when (and tramp-message-show-message (<= level 3))
2353 (apply 'message 1333 (apply 'message
2354 (concat 1334 (concat
2355 (cond 1335 (cond
2356 ((= level 0) "") 1336 ((= level 0) "")
2357 ((= level 1) "") 1337 ((= level 1) "")
2358 ((= level 2) "Warning: ") 1338 ((= level 2) "Warning: ")
2359 (t "Tramp: ")) 1339 (t "Tramp: "))
2360 fmt-string) 1340 fmt-string)
2361 args)) 1341 args))
2362 ;; Log only when there is a minimum level. 1342 ;; Log only when there is a minimum level.
2363 (when (>= tramp-verbose 4) 1343 (when (>= tramp-verbose 4)
2364 (when (and vec-or-proc 1344 (when (and vec-or-proc
2365 (processp vec-or-proc) 1345 (processp vec-or-proc)
2366 (buffer-name (process-buffer vec-or-proc))) 1346 (buffer-name (process-buffer vec-or-proc)))
2367 (with-current-buffer (process-buffer vec-or-proc) 1347 (with-current-buffer (process-buffer vec-or-proc)
2368 ;; Translate proc to vec. 1348 ;; Translate proc to vec.
2369 (setq vec-or-proc (tramp-dissect-file-name default-directory)))) 1349 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2370 (when (and vec-or-proc (vectorp vec-or-proc)) 1350 (when (and vec-or-proc (vectorp vec-or-proc))
2371 (apply 'tramp-debug-message 1351 (apply 'tramp-debug-message
2372 vec-or-proc 1352 vec-or-proc
2373 (concat (format "(%d) # " level) fmt-string) 1353 (concat (format "(%d) # " level) fmt-string)
2374 args))))) 1354 args)))))))
2375 ;; Suppress all errors.
2376 (error nil)))
2377 1355
2378(defsubst tramp-error (vec-or-proc signal fmt-string &rest args) 1356(defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2379 "Emit an error. 1357 "Emit an error.
@@ -2455,11 +1433,10 @@ progress reporter."
2455 (when (and tramp-message-show-message 1433 (when (and tramp-message-show-message
2456 ;; Display only when there is a minimum level. 1434 ;; Display only when there is a minimum level.
2457 (<= ,level (min tramp-verbose 3))) 1435 (<= ,level (min tramp-verbose 3)))
2458 (condition-case nil 1436 (ignore-errors
2459 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message) 1437 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
2460 tm (when pr 1438 tm (when pr
2461 (run-at-time 3 0.1 'tramp-progress-reporter-update pr))) 1439 (run-at-time 3 0.1 'tramp-progress-reporter-update pr)))))
2462 (error nil)))
2463 (unwind-protect 1440 (unwind-protect
2464 ;; Execute the body. Unset `tramp-message-show-message' when 1441 ;; Execute the body. Unset `tramp-message-show-message' when
2465 ;; the timer object is created, in order to suppress 1442 ;; the timer object is created, in order to suppress
@@ -2490,34 +1467,6 @@ letter into the file name. This function removes it."
2490 1467
2491 (defalias 'tramp-drop-volume-letter 'identity))) 1468 (defalias 'tramp-drop-volume-letter 'identity)))
2492 1469
2493(defsubst tramp-make-tramp-temp-file (vec)
2494 "Create a temporary file on the remote host identified by VEC.
2495Return the local name of the temporary file."
2496 (let ((prefix
2497 (tramp-make-tramp-file-name
2498 (tramp-file-name-method vec)
2499 (tramp-file-name-user vec)
2500 (tramp-file-name-host vec)
2501 (tramp-drop-volume-letter
2502 (expand-file-name
2503 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
2504 result)
2505 (while (not result)
2506 ;; `make-temp-file' would be the natural choice for
2507 ;; implementation. But it calls `write-region' internally,
2508 ;; which also needs a temporary file - we would end in an
2509 ;; infinite loop.
2510 (setq result (make-temp-name prefix))
2511 (if (file-exists-p result)
2512 (setq result nil)
2513 ;; This creates the file by side effect.
2514 (set-file-times result)
2515 (set-file-modes result (tramp-compat-octal-to-decimal "0700"))))
2516
2517 ;; Return the local part.
2518 (with-parsed-tramp-file-name result nil localname)))
2519
2520
2521;;; Config Manipulation Functions: 1470;;; Config Manipulation Functions:
2522 1471
2523(defun tramp-set-completion-function (method function-list) 1472(defun tramp-set-completion-function (method function-list)
@@ -2640,279 +1589,6 @@ been set up by `rfn-eshadow-setup-minibuffer'."
2640 (remove-hook 'rfn-eshadow-update-overlay-hook 1589 (remove-hook 'rfn-eshadow-update-overlay-hook
2641 'tramp-rfn-eshadow-update-overlay)))) 1590 'tramp-rfn-eshadow-update-overlay))))
2642 1591
2643
2644;;; Integration of eshell.el:
2645
2646(eval-when-compile
2647 (defvar eshell-path-env))
2648
2649;; eshell.el keeps the path in `eshell-path-env'. We must change it
2650;; when `default-directory' points to another host.
2651(defun tramp-eshell-directory-change ()
2652 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2653 (setq eshell-path-env
2654 (if (file-remote-p default-directory)
2655 (with-parsed-tramp-file-name default-directory nil
2656 (mapconcat
2657 'identity
2658 (tramp-get-remote-path v)
2659 ":"))
2660 (getenv "PATH"))))
2661
2662(eval-after-load "esh-util"
2663 '(progn
2664 (tramp-eshell-directory-change)
2665 (add-hook 'eshell-directory-change-hook
2666 'tramp-eshell-directory-change)
2667 (add-hook 'tramp-unload-hook
2668 (lambda ()
2669 (remove-hook 'eshell-directory-change-hook
2670 'tramp-eshell-directory-change)))))
2671
2672
2673;;; File Name Handler Functions:
2674
2675(defun tramp-handle-make-symbolic-link
2676 (filename linkname &optional ok-if-already-exists)
2677 "Like `make-symbolic-link' for Tramp files.
2678If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2679the symlink. If LINKNAME is a Tramp file, only the localname component is
2680used as the target of the symlink.
2681
2682If LINKNAME is a Tramp file and the localname component is relative, then
2683it is expanded first, before the localname component is taken. Note that
2684this can give surprising results if the user/host for the source and
2685target of the symlink differ."
2686 (with-parsed-tramp-file-name linkname l
2687 (let ((ln (tramp-get-remote-ln l))
2688 (cwd (tramp-run-real-handler
2689 'file-name-directory (list l-localname))))
2690 (unless ln
2691 (tramp-error
2692 l 'file-error
2693 "Making a symbolic link. ln(1) does not exist on the remote host."))
2694
2695 ;; Do the 'confirm if exists' thing.
2696 (when (file-exists-p linkname)
2697 ;; What to do?
2698 (if (or (null ok-if-already-exists) ; not allowed to exist
2699 (and (numberp ok-if-already-exists)
2700 (not (yes-or-no-p
2701 (format
2702 "File %s already exists; make it a link anyway? "
2703 l-localname)))))
2704 (tramp-error
2705 l 'file-already-exists "File %s already exists" l-localname)
2706 (delete-file linkname)))
2707
2708 ;; If FILENAME is a Tramp name, use just the localname component.
2709 (when (tramp-tramp-file-p filename)
2710 (setq filename
2711 (tramp-file-name-localname
2712 (tramp-dissect-file-name (expand-file-name filename)))))
2713
2714 (tramp-flush-file-property l (file-name-directory l-localname))
2715 (tramp-flush-file-property l l-localname)
2716
2717 ;; Right, they are on the same host, regardless of user, method, etc.
2718 ;; We now make the link on the remote machine. This will occur as the user
2719 ;; that FILENAME belongs to.
2720 (zerop
2721 (tramp-send-command-and-check
2722 l
2723 (format
2724 "cd %s && %s -sf %s %s"
2725 (tramp-shell-quote-argument cwd)
2726 ln
2727 (tramp-shell-quote-argument filename)
2728 (tramp-shell-quote-argument l-localname))
2729 t)))))
2730
2731(defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2732 "Like `load' for Tramp files."
2733 (with-parsed-tramp-file-name (expand-file-name file) nil
2734 (unless nosuffix
2735 (cond ((file-exists-p (concat file ".elc"))
2736 (setq file (concat file ".elc")))
2737 ((file-exists-p (concat file ".el"))
2738 (setq file (concat file ".el")))))
2739 (when must-suffix
2740 ;; The first condition is always true for absolute file names.
2741 ;; Included for safety's sake.
2742 (unless (or (file-name-directory file)
2743 (string-match "\\.elc?\\'" file))
2744 (tramp-error
2745 v 'file-error
2746 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2747 (unless noerror
2748 (when (not (file-exists-p file))
2749 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2750 (if (not (file-exists-p file))
2751 nil
2752 (let ((tramp-message-show-message (not nomessage)))
2753 (with-progress-reporter v 0 (format "Loading %s" file)
2754 (let ((local-copy (file-local-copy file)))
2755 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2756 (unwind-protect
2757 (load local-copy noerror t t)
2758 (delete-file local-copy)))))
2759 t)))
2760
2761;; Localname manipulation functions that grok Tramp localnames...
2762(defun tramp-handle-file-name-as-directory (file)
2763 "Like `file-name-as-directory' but aware of Tramp files."
2764 ;; `file-name-as-directory' would be sufficient except localname is
2765 ;; the empty string.
2766 (let ((v (tramp-dissect-file-name file t)))
2767 ;; Run the command on the localname portion only.
2768 (tramp-make-tramp-file-name
2769 (tramp-file-name-method v)
2770 (tramp-file-name-user v)
2771 (tramp-file-name-host v)
2772 (tramp-run-real-handler
2773 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2774
2775(defun tramp-handle-file-name-directory (file)
2776 "Like `file-name-directory' but aware of Tramp files."
2777 ;; Everything except the last filename thing is the directory. We
2778 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2779 ;; the remote file name parts. This is a problem when we are in
2780 ;; file name completion.
2781 (let ((v (tramp-dissect-file-name file t)))
2782 ;; Run the command on the localname portion only.
2783 (tramp-make-tramp-file-name
2784 (tramp-file-name-method v)
2785 (tramp-file-name-user v)
2786 (tramp-file-name-host v)
2787 (tramp-run-real-handler
2788 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2789
2790(defun tramp-handle-file-name-nondirectory (file)
2791 "Like `file-name-nondirectory' but aware of Tramp files."
2792 (with-parsed-tramp-file-name file nil
2793 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2794
2795(defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2796 "Like `file-truename' for Tramp files."
2797 (with-parsed-tramp-file-name (expand-file-name filename) nil
2798 (with-file-property v localname "file-truename"
2799 (let ((result nil)) ; result steps in reverse order
2800 (tramp-message v 4 "Finding true name for `%s'" filename)
2801 (cond
2802 ;; Use GNU readlink --canonicalize-missing where available.
2803 ((tramp-get-remote-readlink v)
2804 (setq result
2805 (tramp-send-command-and-read
2806 v
2807 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2808 (tramp-get-remote-readlink v)
2809 (tramp-shell-quote-argument localname)))))
2810
2811 ;; Use Perl implementation.
2812 ((and (tramp-get-remote-perl v)
2813 (tramp-get-connection-property v "perl-file-spec" nil)
2814 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2815 (tramp-maybe-send-script
2816 v tramp-perl-file-truename "tramp_perl_file_truename")
2817 (setq result
2818 (tramp-send-command-and-read
2819 v
2820 (format "tramp_perl_file_truename %s"
2821 (tramp-shell-quote-argument localname)))))
2822
2823 ;; Do it yourself. We bind `directory-sep-char' here for
2824 ;; XEmacs on Windows, which would otherwise use backslash.
2825 (t (let* ((directory-sep-char ?/)
2826 (steps (tramp-compat-split-string localname "/"))
2827 (localnamedir (tramp-run-real-handler
2828 'file-name-as-directory (list localname)))
2829 (is-dir (string= localname localnamedir))
2830 (thisstep nil)
2831 (numchase 0)
2832 ;; Don't make the following value larger than
2833 ;; necessary. People expect an error message in a
2834 ;; timely fashion when something is wrong;
2835 ;; otherwise they might think that Emacs is hung.
2836 ;; Of course, correctness has to come first.
2837 (numchase-limit 20)
2838 symlink-target)
2839 (while (and steps (< numchase numchase-limit))
2840 (setq thisstep (pop steps))
2841 (tramp-message
2842 v 5 "Check %s"
2843 (mapconcat 'identity
2844 (append '("") (reverse result) (list thisstep))
2845 "/"))
2846 (setq symlink-target
2847 (nth 0 (file-attributes
2848 (tramp-make-tramp-file-name
2849 method user host
2850 (mapconcat 'identity
2851 (append '("")
2852 (reverse result)
2853 (list thisstep))
2854 "/")))))
2855 (cond ((string= "." thisstep)
2856 (tramp-message v 5 "Ignoring step `.'"))
2857 ((string= ".." thisstep)
2858 (tramp-message v 5 "Processing step `..'")
2859 (pop result))
2860 ((stringp symlink-target)
2861 ;; It's a symlink, follow it.
2862 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2863 (setq numchase (1+ numchase))
2864 (when (file-name-absolute-p symlink-target)
2865 (setq result nil))
2866 ;; If the symlink was absolute, we'll get a string like
2867 ;; "/user@host:/some/target"; extract the
2868 ;; "/some/target" part from it.
2869 (when (tramp-tramp-file-p symlink-target)
2870 (unless (tramp-equal-remote filename symlink-target)
2871 (tramp-error
2872 v 'file-error
2873 "Symlink target `%s' on wrong host" symlink-target))
2874 (setq symlink-target localname))
2875 (setq steps
2876 (append (tramp-compat-split-string
2877 symlink-target "/")
2878 steps)))
2879 (t
2880 ;; It's a file.
2881 (setq result (cons thisstep result)))))
2882 (when (>= numchase numchase-limit)
2883 (tramp-error
2884 v 'file-error
2885 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2886 (setq result (reverse result))
2887 ;; Combine list to form string.
2888 (setq result
2889 (if result
2890 (mapconcat 'identity (cons "" result) "/")
2891 "/"))
2892 (when (and is-dir (or (string= "" result)
2893 (not (string= (substring result -1) "/"))))
2894 (setq result (concat result "/"))))))
2895
2896 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2897 (tramp-make-tramp-file-name method user host result)))))
2898
2899;; Basic functions.
2900
2901(defun tramp-handle-file-exists-p (filename)
2902 "Like `file-exists-p' for Tramp files."
2903 (with-parsed-tramp-file-name filename nil
2904 (with-file-property v localname "file-exists-p"
2905 (or (not (null (tramp-get-file-property
2906 v localname "file-attributes-integer" nil)))
2907 (not (null (tramp-get-file-property
2908 v localname "file-attributes-string" nil)))
2909 (zerop (tramp-send-command-and-check
2910 v
2911 (format
2912 "%s %s"
2913 (tramp-get-file-exists-command v)
2914 (tramp-shell-quote-argument localname))))))))
2915
2916;; Inodes don't exist for some file systems. Therefore we must 1592;; Inodes don't exist for some file systems. Therefore we must
2917;; generate virtual ones. Used in `find-buffer-visiting'. The method 1593;; generate virtual ones. Used in `find-buffer-visiting'. The method
2918;; applied might be not so efficient (Ange-FTP uses hashes). But 1594;; applied might be not so efficient (Ange-FTP uses hashes). But
@@ -2929,428 +1605,6 @@ target of the symlink differ."
2929(defvar tramp-devices nil 1605(defvar tramp-devices nil
2930 "Keeps virtual device numbers.") 1606 "Keeps virtual device numbers.")
2931 1607
2932;; CCC: This should check for an error condition and signal failure
2933;; when something goes wrong.
2934;; Daniel Pittman <daniel@danann.net>
2935(defun tramp-handle-file-attributes (filename &optional id-format)
2936 "Like `file-attributes' for Tramp files."
2937 (unless id-format (setq id-format 'integer))
2938 ;; Don't modify `last-coding-system-used' by accident.
2939 (let ((last-coding-system-used last-coding-system-used))
2940 (with-parsed-tramp-file-name (expand-file-name filename) nil
2941 (with-file-property v localname (format "file-attributes-%s" id-format)
2942 (save-excursion
2943 (tramp-convert-file-attributes
2944 v
2945 (cond
2946 ((tramp-get-remote-stat v)
2947 (tramp-do-file-attributes-with-stat v localname id-format))
2948 ((tramp-get-remote-perl v)
2949 (tramp-do-file-attributes-with-perl v localname id-format))
2950 (t
2951 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2952
2953(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
2954 "Implement `file-attributes' for Tramp files using the ls(1) command."
2955 (let (symlinkp dirp
2956 res-inode res-filemodes res-numlinks
2957 res-uid res-gid res-size res-symlink-target)
2958 (tramp-message vec 5 "file attributes with ls: %s" localname)
2959 (tramp-send-command
2960 vec
2961 (format "(%s %s || %s -h %s) && %s %s %s"
2962 (tramp-get-file-exists-command vec)
2963 (tramp-shell-quote-argument localname)
2964 (tramp-get-test-command vec)
2965 (tramp-shell-quote-argument localname)
2966 (tramp-get-ls-command vec)
2967 (if (eq id-format 'integer) "-ildn" "-ild")
2968 (tramp-shell-quote-argument localname)))
2969 ;; parse `ls -l' output ...
2970 (with-current-buffer (tramp-get-buffer vec)
2971 (when (> (buffer-size) 0)
2972 (goto-char (point-min))
2973 ;; ... inode
2974 (setq res-inode
2975 (condition-case err
2976 (read (current-buffer))
2977 (invalid-read-syntax
2978 (when (and (equal (cadr err)
2979 "Integer constant overflow in reader")
2980 (string-match
2981 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2982 (car (cddr err))))
2983 (let* ((big (read (substring (car (cddr err)) 0
2984 (match-beginning 1))))
2985 (small (read (match-string 1 (car (cddr err)))))
2986 (twiddle (/ small 65536)))
2987 (cons (+ big twiddle)
2988 (- small (* twiddle 65536))))))))
2989 ;; ... file mode flags
2990 (setq res-filemodes (symbol-name (read (current-buffer))))
2991 ;; ... number links
2992 (setq res-numlinks (read (current-buffer)))
2993 ;; ... uid and gid
2994 (setq res-uid (read (current-buffer)))
2995 (setq res-gid (read (current-buffer)))
2996 (if (eq id-format 'integer)
2997 (progn
2998 (unless (numberp res-uid) (setq res-uid -1))
2999 (unless (numberp res-gid) (setq res-gid -1)))
3000 (progn
3001 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
3002 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
3003 ;; ... size
3004 (setq res-size (read (current-buffer)))
3005 ;; From the file modes, figure out other stuff.
3006 (setq symlinkp (eq ?l (aref res-filemodes 0)))
3007 (setq dirp (eq ?d (aref res-filemodes 0)))
3008 ;; if symlink, find out file name pointed to
3009 (when symlinkp
3010 (search-forward "-> ")
3011 (setq res-symlink-target
3012 (buffer-substring (point) (tramp-compat-line-end-position))))
3013 ;; return data gathered
3014 (list
3015 ;; 0. t for directory, string (name linked to) for symbolic
3016 ;; link, or nil.
3017 (or dirp res-symlink-target)
3018 ;; 1. Number of links to file.
3019 res-numlinks
3020 ;; 2. File uid.
3021 res-uid
3022 ;; 3. File gid.
3023 res-gid
3024 ;; 4. Last access time, as a list of two integers. First
3025 ;; integer has high-order 16 bits of time, second has low 16
3026 ;; bits.
3027 ;; 5. Last modification time, likewise.
3028 ;; 6. Last status change time, likewise.
3029 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
3030 ;; 7. Size in bytes (-1, if number is out of range).
3031 res-size
3032 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
3033 res-filemodes
3034 ;; 9. t if file's gid would change if file were deleted and
3035 ;; recreated. Will be set in `tramp-convert-file-attributes'
3036 t
3037 ;; 10. inode number.
3038 res-inode
3039 ;; 11. Device number. Will be replaced by a virtual device number.
3040 -1
3041 )))))
3042
3043(defun tramp-do-file-attributes-with-perl
3044 (vec localname &optional id-format)
3045 "Implement `file-attributes' for Tramp files using a Perl script."
3046 (tramp-message vec 5 "file attributes with perl: %s" localname)
3047 (tramp-maybe-send-script
3048 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
3049 (tramp-send-command-and-read
3050 vec
3051 (format "tramp_perl_file_attributes %s %s"
3052 (tramp-shell-quote-argument localname) id-format)))
3053
3054(defun tramp-do-file-attributes-with-stat
3055 (vec localname &optional id-format)
3056 "Implement `file-attributes' for Tramp files using stat(1) command."
3057 (tramp-message vec 5 "file attributes with stat: %s" localname)
3058 (tramp-send-command-and-read
3059 vec
3060 (format
3061 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
3062 ;; parse correctly the sequence "((". Therefore, we add a space.
3063 "( (%s %s || %s -h %s) && %s -c '( (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s || echo nil)"
3064 (tramp-get-file-exists-command vec)
3065 (tramp-shell-quote-argument localname)
3066 (tramp-get-test-command vec)
3067 (tramp-shell-quote-argument localname)
3068 (tramp-get-remote-stat vec)
3069 (if (eq id-format 'integer) "%u" "\"%U\"")
3070 (if (eq id-format 'integer) "%g" "\"%G\"")
3071 (tramp-shell-quote-argument localname))))
3072
3073(defun tramp-handle-set-visited-file-modtime (&optional time-list)
3074 "Like `set-visited-file-modtime' for Tramp files."
3075 (unless (buffer-file-name)
3076 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
3077 (buffer-name)))
3078 (if time-list
3079 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
3080 (let ((f (buffer-file-name))
3081 coding-system-used)
3082 (with-parsed-tramp-file-name f nil
3083 (let* ((attr (file-attributes f))
3084 ;; '(-1 65535) means file doesn't exists yet.
3085 (modtime (or (nth 5 attr) '(-1 65535))))
3086 (when (boundp 'last-coding-system-used)
3087 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3088 ;; We use '(0 0) as a don't-know value. See also
3089 ;; `tramp-do-file-attributes-with-ls'.
3090 (if (not (equal modtime '(0 0)))
3091 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
3092 (progn
3093 (tramp-send-command
3094 v
3095 (format "%s -ild %s"
3096 (tramp-get-ls-command v)
3097 (tramp-shell-quote-argument localname)))
3098 (setq attr (buffer-substring (point)
3099 (progn (end-of-line) (point)))))
3100 (tramp-set-file-property
3101 v localname "visited-file-modtime-ild" attr))
3102 (when (boundp 'last-coding-system-used)
3103 (set 'last-coding-system-used coding-system-used))
3104 nil)))))
3105
3106;; This function makes the same assumption as
3107;; `tramp-handle-set-visited-file-modtime'.
3108(defun tramp-handle-verify-visited-file-modtime (buf)
3109 "Like `verify-visited-file-modtime' for Tramp files.
3110At the time `verify-visited-file-modtime' calls this function, we
3111already know that the buffer is visiting a file and that
3112`visited-file-modtime' does not return 0. Do not call this
3113function directly, unless those two cases are already taken care
3114of."
3115 (with-current-buffer buf
3116 (let ((f (buffer-file-name)))
3117 ;; There is no file visiting the buffer, or the buffer has no
3118 ;; recorded last modification time, or there is no established
3119 ;; connection.
3120 (if (or (not f)
3121 (eq (visited-file-modtime) 0)
3122 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
3123 t
3124 (with-parsed-tramp-file-name f nil
3125 (tramp-flush-file-property v localname)
3126 (let* ((attr (file-attributes f))
3127 (modtime (nth 5 attr))
3128 (mt (visited-file-modtime)))
3129
3130 (cond
3131 ;; File exists, and has a known modtime.
3132 ((and attr (not (equal modtime '(0 0))))
3133 (< (abs (tramp-time-diff
3134 modtime
3135 ;; For compatibility, deal with both the old
3136 ;; (HIGH . LOW) and the new (HIGH LOW) return
3137 ;; values of `visited-file-modtime'.
3138 (if (atom (cdr mt))
3139 (list (car mt) (cdr mt))
3140 mt)))
3141 2))
3142 ;; Modtime has the don't know value.
3143 (attr
3144 (tramp-send-command
3145 v
3146 (format "%s -ild %s"
3147 (tramp-get-ls-command v)
3148 (tramp-shell-quote-argument localname)))
3149 (with-current-buffer (tramp-get-buffer v)
3150 (setq attr (buffer-substring
3151 (point) (progn (end-of-line) (point)))))
3152 (equal
3153 attr
3154 (tramp-get-file-property
3155 v localname "visited-file-modtime-ild" "")))
3156 ;; If file does not exist, say it is not modified if and
3157 ;; only if that agrees with the buffer's record.
3158 (t (equal mt '(-1 65535))))))))))
3159
3160(defun tramp-handle-set-file-modes (filename mode)
3161 "Like `set-file-modes' for Tramp files."
3162 (with-parsed-tramp-file-name filename nil
3163 (tramp-flush-file-property v localname)
3164 (unless (zerop (tramp-send-command-and-check
3165 v
3166 (format "chmod %s %s"
3167 (tramp-compat-decimal-to-octal mode)
3168 (tramp-shell-quote-argument localname))))
3169 ;; FIXME: extract the proper text from chmod's stderr.
3170 (tramp-error
3171 v 'file-error "Error while changing file's mode %s" filename))))
3172
3173(defun tramp-handle-set-file-times (filename &optional time)
3174 "Like `set-file-times' for Tramp files."
3175 (zerop
3176 (if (file-remote-p filename)
3177 (with-parsed-tramp-file-name filename nil
3178 (tramp-flush-file-property v localname)
3179 (let ((time (if (or (null time) (equal time '(0 0)))
3180 (current-time)
3181 time))
3182 ;; With GNU Emacs, `format-time-string' has an optional
3183 ;; parameter UNIVERSAL. This is preferred, because we
3184 ;; could handle the case when the remote host is
3185 ;; located in a different time zone as the local host.
3186 (utc (not (featurep 'xemacs))))
3187 (tramp-send-command-and-check
3188 v (format "%s touch -t %s %s"
3189 (if utc "TZ=UTC; export TZ;" "")
3190 (if utc
3191 (format-time-string "%Y%m%d%H%M.%S" time t)
3192 (format-time-string "%Y%m%d%H%M.%S" time))
3193 (tramp-shell-quote-argument localname)))))
3194
3195 ;; We handle also the local part, because in older Emacsen,
3196 ;; without `set-file-times', this function is an alias for this.
3197 ;; We are local, so we don't need the UTC settings.
3198 (tramp-compat-call-process
3199 "touch" nil nil nil "-t"
3200 (format-time-string "%Y%m%d%H%M.%S" time)
3201 (tramp-shell-quote-argument filename)))))
3202
3203(defun tramp-set-file-uid-gid (filename &optional uid gid)
3204 "Set the ownership for FILENAME.
3205If UID and GID are provided, these values are used; otherwise uid
3206and gid of the corresponding user is taken. Both parameters must be integers."
3207 ;; Modern Unices allow chown only for root. So we might need
3208 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3209 ;; working with su(do)? when it is needed, so it shall succeed in
3210 ;; the majority of cases.
3211 ;; Don't modify `last-coding-system-used' by accident.
3212 (let ((last-coding-system-used last-coding-system-used))
3213 (if (file-remote-p filename)
3214 (with-parsed-tramp-file-name filename nil
3215 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3216 ;; If we are root on the local host, we can do it directly.
3217 (tramp-set-file-uid-gid localname uid gid)
3218 (let ((uid (or (and (integerp uid) uid)
3219 (tramp-get-remote-uid v 'integer)))
3220 (gid (or (and (integerp gid) gid)
3221 (tramp-get-remote-gid v 'integer))))
3222 (tramp-send-command
3223 v (format
3224 "chown %d:%d %s" uid gid
3225 (tramp-shell-quote-argument localname))))))
3226
3227 ;; We handle also the local part, because there doesn't exist
3228 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3229 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3230 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3231 (tramp-compat-call-process
3232 "chown" nil nil nil
3233 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
3234
3235(defun tramp-remote-selinux-p (vec)
3236 "Check, whether SELINUX is enabled on the remote host."
3237 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3238 (let ((result (tramp-find-executable
3239 vec "getenforce" (tramp-get-remote-path vec) t t)))
3240 (and result
3241 (string-equal
3242 (tramp-send-command-and-read
3243 vec (format "echo \\\"`%S`\\\"" result))
3244 "Enforcing")))))
3245
3246(defun tramp-handle-file-selinux-context (filename)
3247 "Like `file-selinux-context' for Tramp files."
3248 (with-parsed-tramp-file-name filename nil
3249 (with-file-property v localname "file-selinux-context"
3250 (let ((context '(nil nil nil nil))
3251 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3252 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
3253 (when (and (tramp-remote-selinux-p v)
3254 (zerop (tramp-send-command-and-check
3255 v (format
3256 "%s -d -Z %s"
3257 (tramp-get-ls-command v)
3258 (tramp-shell-quote-argument localname)))))
3259 (with-current-buffer (tramp-get-connection-buffer v)
3260 (goto-char (point-min))
3261 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3262 (setq context (list (match-string 1) (match-string 2)
3263 (match-string 3) (match-string 4))))))
3264 ;; Return the context.
3265 context))))
3266
3267(defun tramp-handle-set-file-selinux-context (filename context)
3268 "Like `set-file-selinux-context' for Tramp files."
3269 (with-parsed-tramp-file-name filename nil
3270 (if (and (consp context)
3271 (tramp-remote-selinux-p v)
3272 (zerop (tramp-send-command-and-check
3273 v (format "chcon %s %s %s %s %s"
3274 (if (stringp (nth 0 context))
3275 (format "--user=%s" (nth 0 context)) "")
3276 (if (stringp (nth 1 context))
3277 (format "--role=%s" (nth 1 context)) "")
3278 (if (stringp (nth 2 context))
3279 (format "--type=%s" (nth 2 context)) "")
3280 (if (stringp (nth 3 context))
3281 (format "--range=%s" (nth 3 context)) "")
3282 (tramp-shell-quote-argument localname)))))
3283 (tramp-set-file-property v localname "file-selinux-context" context)
3284 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3285 ;; We always return nil.
3286 nil)
3287
3288;; Simple functions using the `test' command.
3289
3290(defun tramp-handle-file-executable-p (filename)
3291 "Like `file-executable-p' for Tramp files."
3292 (with-parsed-tramp-file-name filename nil
3293 (with-file-property v localname "file-executable-p"
3294 ;; Examine `file-attributes' cache to see if request can be
3295 ;; satisfied without remote operation.
3296 (or (tramp-check-cached-permissions v ?x)
3297 (zerop (tramp-run-test "-x" filename))))))
3298
3299(defun tramp-handle-file-readable-p (filename)
3300 "Like `file-readable-p' for Tramp files."
3301 (with-parsed-tramp-file-name filename nil
3302 (with-file-property v localname "file-readable-p"
3303 ;; Examine `file-attributes' cache to see if request can be
3304 ;; satisfied without remote operation.
3305 (or (tramp-check-cached-permissions v ?r)
3306 (zerop (tramp-run-test "-r" filename))))))
3307
3308;; When the remote shell is started, it looks for a shell which groks
3309;; tilde expansion. Here, we assume that all shells which grok tilde
3310;; expansion will also provide a `test' command which groks `-nt' (for
3311;; newer than). If this breaks, tell me about it and I'll try to do
3312;; something smarter about it.
3313(defun tramp-handle-file-newer-than-file-p (file1 file2)
3314 "Like `file-newer-than-file-p' for Tramp files."
3315 (cond ((not (file-exists-p file1))
3316 nil)
3317 ((not (file-exists-p file2))
3318 t)
3319 ;; We are sure both files exist at this point.
3320 (t
3321 (save-excursion
3322 ;; We try to get the mtime of both files. If they are not
3323 ;; equal to the "dont-know" value, then we subtract the times
3324 ;; and obtain the result.
3325 (let ((fa1 (file-attributes file1))
3326 (fa2 (file-attributes file2)))
3327 (if (and (not (equal (nth 5 fa1) '(0 0)))
3328 (not (equal (nth 5 fa2) '(0 0))))
3329 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
3330 ;; If one of them is the dont-know value, then we can
3331 ;; still try to run a shell command on the remote host.
3332 ;; However, this only works if both files are Tramp
3333 ;; files and both have the same method, same user, same
3334 ;; host.
3335 (unless (tramp-equal-remote file1 file2)
3336 (with-parsed-tramp-file-name
3337 (if (tramp-tramp-file-p file1) file1 file2) nil
3338 (tramp-error
3339 v 'file-error
3340 "Files %s and %s must have same method, user, host"
3341 file1 file2)))
3342 (with-parsed-tramp-file-name file1 nil
3343 (zerop (tramp-run-test2
3344 (tramp-get-test-nt-command v) file1 file2)))))))))
3345
3346;; Functions implemented using the basic functions above.
3347
3348(defun tramp-handle-file-modes (filename)
3349 "Like `file-modes' for Tramp files."
3350 (let ((truename (or (file-truename filename) filename)))
3351 (when (file-exists-p truename)
3352 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
3353
3354(defun tramp-default-file-modes (filename) 1608(defun tramp-default-file-modes (filename)
3355 "Return file modes of FILENAME as integer. 1609 "Return file modes of FILENAME as integer.
3356If the file modes of FILENAME cannot be determined, return the 1610If the file modes of FILENAME cannot be determined, return the
@@ -3358,1212 +1612,6 @@ value of `default-file-modes', without execute permissions."
3358 (or (file-modes filename) 1612 (or (file-modes filename)
3359 (logand (default-file-modes) (tramp-compat-octal-to-decimal "0666")))) 1613 (logand (default-file-modes) (tramp-compat-octal-to-decimal "0666"))))
3360 1614
3361(defun tramp-handle-file-directory-p (filename)
3362 "Like `file-directory-p' for Tramp files."
3363 ;; Care must be taken that this function returns `t' for symlinks
3364 ;; pointing to directories. Surely the most obvious implementation
3365 ;; would be `test -d', but that returns false for such symlinks.
3366 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3367 ;; I now think he's right. So we could be using `test -d', couldn't
3368 ;; we?
3369 ;;
3370 ;; Alternatives: `cd %s', `test -d %s'
3371 (with-parsed-tramp-file-name filename nil
3372 (with-file-property v localname "file-directory-p"
3373 (zerop (tramp-run-test "-d" filename)))))
3374
3375(defun tramp-handle-file-regular-p (filename)
3376 "Like `file-regular-p' for Tramp files."
3377 (and (file-exists-p filename)
3378 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3379
3380(defun tramp-handle-file-symlink-p (filename)
3381 "Like `file-symlink-p' for Tramp files."
3382 (with-parsed-tramp-file-name filename nil
3383 (let ((x (car (file-attributes filename))))
3384 (when (stringp x)
3385 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3386 ;; might do weird things.
3387 (if (file-name-absolute-p x)
3388 (tramp-make-tramp-file-name method user host x)
3389 x)))))
3390
3391(defun tramp-handle-file-writable-p (filename)
3392 "Like `file-writable-p' for Tramp files."
3393 (with-parsed-tramp-file-name filename nil
3394 (with-file-property v localname "file-writable-p"
3395 (if (file-exists-p filename)
3396 ;; Examine `file-attributes' cache to see if request can be
3397 ;; satisfied without remote operation.
3398 (or (tramp-check-cached-permissions v ?w)
3399 (zerop (tramp-run-test "-w" filename)))
3400 ;; If file doesn't exist, check if directory is writable.
3401 (and (zerop (tramp-run-test
3402 "-d" (file-name-directory filename)))
3403 (zerop (tramp-run-test
3404 "-w" (file-name-directory filename))))))))
3405
3406(defun tramp-handle-file-ownership-preserved-p (filename)
3407 "Like `file-ownership-preserved-p' for Tramp files."
3408 (with-parsed-tramp-file-name filename nil
3409 (with-file-property v localname "file-ownership-preserved-p"
3410 (let ((attributes (file-attributes filename)))
3411 ;; Return t if the file doesn't exist, since it's true that no
3412 ;; information would be lost by an (attempted) delete and create.
3413 (or (null attributes)
3414 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
3415
3416;; Other file name ops.
3417
3418(defun tramp-handle-directory-file-name (directory)
3419 "Like `directory-file-name' for Tramp files."
3420 ;; If localname component of filename is "/", leave it unchanged.
3421 ;; Otherwise, remove any trailing slash from localname component.
3422 ;; Method, host, etc, are unchanged. Does it make sense to try
3423 ;; to avoid parsing the filename?
3424 (with-parsed-tramp-file-name directory nil
3425 (if (and (not (zerop (length localname)))
3426 (eq (aref localname (1- (length localname))) ?/)
3427 (not (string= localname "/")))
3428 (substring directory 0 -1)
3429 directory)))
3430
3431;; Directory listings.
3432
3433(defun tramp-handle-directory-files
3434 (directory &optional full match nosort files-only)
3435 "Like `directory-files' for Tramp files."
3436 ;; FILES-ONLY is valid for XEmacs only.
3437 (when (file-directory-p directory)
3438 (setq directory (file-name-as-directory (expand-file-name directory)))
3439 (let ((temp (nreverse (file-name-all-completions "" directory)))
3440 result item)
3441
3442 (while temp
3443 (setq item (directory-file-name (pop temp)))
3444 (when (and (or (null match) (string-match match item))
3445 (or (null files-only)
3446 ;; Files only.
3447 (and (equal files-only t) (file-regular-p item))
3448 ;; Directories only.
3449 (file-directory-p item)))
3450 (push (if full (concat directory item) item)
3451 result)))
3452 (if nosort result (sort result 'string<)))))
3453
3454(defun tramp-handle-directory-files-and-attributes
3455 (directory &optional full match nosort id-format)
3456 "Like `directory-files-and-attributes' for Tramp files."
3457 (unless id-format (setq id-format 'integer))
3458 (when (file-directory-p directory)
3459 (setq directory (expand-file-name directory))
3460 (let* ((temp
3461 (copy-tree
3462 (with-parsed-tramp-file-name directory nil
3463 (with-file-property
3464 v localname
3465 (format "directory-files-and-attributes-%s" id-format)
3466 (save-excursion
3467 (mapcar
3468 (lambda (x)
3469 (cons (car x)
3470 (tramp-convert-file-attributes v (cdr x))))
3471 (cond
3472 ((tramp-get-remote-stat v)
3473 (tramp-do-directory-files-and-attributes-with-stat
3474 v localname id-format))
3475 ((tramp-get-remote-perl v)
3476 (tramp-do-directory-files-and-attributes-with-perl
3477 v localname id-format)))))))))
3478 result item)
3479
3480 (while temp
3481 (setq item (pop temp))
3482 (when (or (null match) (string-match match (car item)))
3483 (when full
3484 (setcar item (expand-file-name (car item) directory)))
3485 (push item result)))
3486
3487 (if nosort
3488 result
3489 (sort result (lambda (x y) (string< (car x) (car y))))))))
3490
3491(defun tramp-do-directory-files-and-attributes-with-perl
3492 (vec localname &optional id-format)
3493 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3494 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3495 (tramp-maybe-send-script
3496 vec tramp-perl-directory-files-and-attributes
3497 "tramp_perl_directory_files_and_attributes")
3498 (let ((object
3499 (tramp-send-command-and-read
3500 vec
3501 (format "tramp_perl_directory_files_and_attributes %s %s"
3502 (tramp-shell-quote-argument localname) id-format))))
3503 (when (stringp object) (tramp-error vec 'file-error object))
3504 object))
3505
3506(defun tramp-do-directory-files-and-attributes-with-stat
3507 (vec localname &optional id-format)
3508 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3509 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3510 (tramp-send-command-and-read
3511 vec
3512 (format
3513 (concat
3514 ;; We must care about filenames with spaces, or starting with
3515 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3516 ;; but it does not work on all remote systems. Therefore, we
3517 ;; quote the filenames via sed.
3518 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3519 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3520 "echo \")\"")
3521 (tramp-shell-quote-argument localname)
3522 (tramp-get-ls-command vec)
3523 (tramp-get-remote-stat vec)
3524 (if (eq id-format 'integer) "%u" "\"%U\"")
3525 (if (eq id-format 'integer) "%g" "\"%G\""))))
3526
3527;; This function should return "foo/" for directories and "bar" for
3528;; files.
3529(defun tramp-handle-file-name-all-completions (filename directory)
3530 "Like `file-name-all-completions' for Tramp files."
3531 (unless (save-match-data (string-match "/" filename))
3532 (with-parsed-tramp-file-name (expand-file-name directory) nil
3533
3534 (all-completions
3535 filename
3536 (mapcar
3537 'list
3538 (or
3539 ;; Try cache first
3540 (and
3541 ;; Ignore if expired
3542 (or (not (integerp tramp-completion-reread-directory-timeout))
3543 (<= (tramp-time-diff
3544 (current-time)
3545 (tramp-get-file-property
3546 v localname "last-completion" '(0 0 0)))
3547 tramp-completion-reread-directory-timeout))
3548
3549 ;; Try cache entries for filename, filename with last
3550 ;; character removed, filename with last two characters
3551 ;; removed, ..., and finally the empty string - all
3552 ;; concatenated to the local directory name
3553
3554 ;; This is inefficient for very long filenames, pity
3555 ;; `reduce' is not available...
3556 (car
3557 (apply
3558 'append
3559 (mapcar
3560 (lambda (x)
3561 (let ((cache-hit
3562 (tramp-get-file-property
3563 v
3564 (concat localname (substring filename 0 x))
3565 "file-name-all-completions"
3566 nil)))
3567 (when cache-hit (list cache-hit))))
3568 (tramp-compat-number-sequence (length filename) 0 -1)))))
3569
3570 ;; Cache expired or no matching cache entry found so we need
3571 ;; to perform a remote operation
3572 (let (result)
3573 ;; Get a list of directories and files, including reliably
3574 ;; tagging the directories with a trailing '/'. Because I
3575 ;; rock. --daniel@danann.net
3576
3577 ;; Changed to perform `cd' in the same remote op and only
3578 ;; get entries starting with `filename'. Capture any `cd'
3579 ;; error messages. Ensure any `cd' and `echo' aliases are
3580 ;; ignored.
3581 (tramp-send-command
3582 v
3583 (if (tramp-get-remote-perl v)
3584 (progn
3585 (tramp-maybe-send-script
3586 v tramp-perl-file-name-all-completions
3587 "tramp_perl_file_name_all_completions")
3588 (format "tramp_perl_file_name_all_completions %s %s %d"
3589 (tramp-shell-quote-argument localname)
3590 (tramp-shell-quote-argument filename)
3591 (if (symbol-value
3592 ;; `read-file-name-completion-ignore-case'
3593 ;; is introduced with Emacs 22.1.
3594 (if (boundp
3595 'read-file-name-completion-ignore-case)
3596 'read-file-name-completion-ignore-case
3597 'completion-ignore-case))
3598 1 0)))
3599
3600 (format (concat
3601 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3602 ;; `ls' with wildcard might fail with `Argument
3603 ;; list too long' error in some corner cases; if
3604 ;; `ls' fails after `cd' succeeded, chances are
3605 ;; that's the case, so let's retry without
3606 ;; wildcard. This will return "too many" entries
3607 ;; but that isn't harmful.
3608 " || %s -a 2>/dev/null)"
3609 " | while read f; do"
3610 " if %s -d \"$f\" 2>/dev/null;"
3611 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3612 " && \\echo ok) || \\echo fail")
3613 (tramp-shell-quote-argument localname)
3614 (tramp-get-ls-command v)
3615 ;; When `filename' is empty, just `ls' without
3616 ;; filename argument is more efficient than `ls *'
3617 ;; for very large directories and might avoid the
3618 ;; `Argument list too long' error.
3619 ;;
3620 ;; With and only with wildcard, we need to add
3621 ;; `-d' to prevent `ls' from descending into
3622 ;; sub-directories.
3623 (if (zerop (length filename))
3624 "."
3625 (concat (tramp-shell-quote-argument filename) "* -d"))
3626 (tramp-get-ls-command v)
3627 (tramp-get-test-command v))))
3628
3629 ;; Now grab the output.
3630 (with-current-buffer (tramp-get-buffer v)
3631 (goto-char (point-max))
3632
3633 ;; Check result code, found in last line of output
3634 (forward-line -1)
3635 (if (looking-at "^fail$")
3636 (progn
3637 ;; Grab error message from line before last line
3638 ;; (it was put there by `cd 2>&1')
3639 (forward-line -1)
3640 (tramp-error
3641 v 'file-error
3642 "tramp-handle-file-name-all-completions: %s"
3643 (buffer-substring
3644 (point) (tramp-compat-line-end-position))))
3645 ;; For peace of mind, if buffer doesn't end in `fail'
3646 ;; then it should end in `ok'. If neither are in the
3647 ;; buffer something went seriously wrong on the remote
3648 ;; side.
3649 (unless (looking-at "^ok$")
3650 (tramp-error
3651 v 'file-error
3652 "\
3653tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3654 (tramp-shell-quote-argument localname) (buffer-string))))
3655
3656 (while (zerop (forward-line -1))
3657 (push (buffer-substring
3658 (point) (tramp-compat-line-end-position))
3659 result)))
3660
3661 ;; Because the remote op went through OK we know the
3662 ;; directory we `cd'-ed to exists
3663 (tramp-set-file-property
3664 v localname "file-exists-p" t)
3665
3666 ;; Because the remote op went through OK we know every
3667 ;; file listed by `ls' exists.
3668 (mapc (lambda (entry)
3669 (tramp-set-file-property
3670 v (concat localname entry) "file-exists-p" t))
3671 result)
3672
3673 (tramp-set-file-property
3674 v localname "last-completion" (current-time))
3675
3676 ;; Store result in the cache
3677 (tramp-set-file-property
3678 v (concat localname filename)
3679 "file-name-all-completions"
3680 result))))))))
3681
3682(defun tramp-handle-file-name-completion
3683 (filename directory &optional predicate)
3684 "Like `file-name-completion' for Tramp files."
3685 (unless (tramp-tramp-file-p directory)
3686 (error
3687 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3688 directory))
3689 (try-completion
3690 filename
3691 (mapcar 'list (file-name-all-completions filename directory))
3692 (when predicate
3693 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
3694
3695;; cp, mv and ln
3696
3697(defun tramp-handle-add-name-to-file
3698 (filename newname &optional ok-if-already-exists)
3699 "Like `add-name-to-file' for Tramp files."
3700 (unless (tramp-equal-remote filename newname)
3701 (with-parsed-tramp-file-name
3702 (if (tramp-tramp-file-p filename) filename newname) nil
3703 (tramp-error
3704 v 'file-error
3705 "add-name-to-file: %s"
3706 "only implemented for same method, same user, same host")))
3707 (with-parsed-tramp-file-name filename v1
3708 (with-parsed-tramp-file-name newname v2
3709 (let ((ln (when v1 (tramp-get-remote-ln v1))))
3710 (when (and (not ok-if-already-exists)
3711 (file-exists-p newname)
3712 (not (numberp ok-if-already-exists))
3713 (y-or-n-p
3714 (format
3715 "File %s already exists; make it a new name anyway? "
3716 newname)))
3717 (tramp-error
3718 v2 'file-error
3719 "add-name-to-file: file %s already exists" newname))
3720 (tramp-flush-file-property v2 (file-name-directory v2-localname))
3721 (tramp-flush-file-property v2 v2-localname)
3722 (tramp-barf-unless-okay
3723 v1
3724 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3725 (tramp-shell-quote-argument v2-localname))
3726 "error with add-name-to-file, see buffer `%s' for details"
3727 (buffer-name))))))
3728
3729(defun tramp-handle-copy-file
3730 (filename newname &optional ok-if-already-exists keep-date
3731 preserve-uid-gid preserve-selinux-context)
3732 "Like `copy-file' for Tramp files."
3733 (setq filename (expand-file-name filename))
3734 (setq newname (expand-file-name newname))
3735 (cond
3736 ;; At least one file a Tramp file?
3737 ((or (tramp-tramp-file-p filename)
3738 (tramp-tramp-file-p newname))
3739 (tramp-do-copy-or-rename-file
3740 'copy filename newname ok-if-already-exists keep-date
3741 preserve-uid-gid preserve-selinux-context))
3742 ;; Compat section.
3743 (preserve-selinux-context
3744 (tramp-run-real-handler
3745 'copy-file
3746 (list filename newname ok-if-already-exists keep-date
3747 preserve-uid-gid preserve-selinux-context)))
3748 (preserve-uid-gid
3749 (tramp-run-real-handler
3750 'copy-file
3751 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3752 (t
3753 (tramp-run-real-handler
3754 'copy-file (list filename newname ok-if-already-exists keep-date)))))
3755
3756(defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3757 "Like `copy-directory' for Tramp files."
3758 (let ((t1 (tramp-tramp-file-p dirname))
3759 (t2 (tramp-tramp-file-p newname)))
3760 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3761 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3762 ;; When DIRNAME and NEWNAME are remote, they must have
3763 ;; the same method.
3764 (or (null t1) (null t2)
3765 (string-equal
3766 (tramp-file-name-method (tramp-dissect-file-name dirname))
3767 (tramp-file-name-method (tramp-dissect-file-name newname)))))
3768 ;; scp or rsync DTRT.
3769 (progn
3770 (setq dirname (directory-file-name (expand-file-name dirname))
3771 newname (directory-file-name (expand-file-name newname)))
3772 (if (and (file-directory-p newname)
3773 (not (string-equal (file-name-nondirectory dirname)
3774 (file-name-nondirectory newname))))
3775 (setq newname
3776 (expand-file-name
3777 (file-name-nondirectory dirname) newname)))
3778 (if (not (file-directory-p (file-name-directory newname)))
3779 (make-directory (file-name-directory newname) parents))
3780 (tramp-do-copy-or-rename-file-out-of-band
3781 'copy dirname newname keep-date))
3782 ;; We must do it file-wise.
3783 (tramp-run-real-handler
3784 'copy-directory (list dirname newname keep-date parents)))
3785
3786 ;; When newname did exist, we have wrong cached values.
3787 (when t2
3788 (with-parsed-tramp-file-name newname nil
3789 (tramp-flush-file-property v (file-name-directory localname))
3790 (tramp-flush-file-property v localname))))))
3791
3792(defun tramp-handle-rename-file
3793 (filename newname &optional ok-if-already-exists)
3794 "Like `rename-file' for Tramp files."
3795 ;; Check if both files are local -- invoke normal rename-file.
3796 ;; Otherwise, use Tramp from local system.
3797 (setq filename (expand-file-name filename))
3798 (setq newname (expand-file-name newname))
3799 ;; At least one file a Tramp file?
3800 (if (or (tramp-tramp-file-p filename)
3801 (tramp-tramp-file-p newname))
3802 (tramp-do-copy-or-rename-file
3803 'rename filename newname ok-if-already-exists t t)
3804 (tramp-run-real-handler
3805 'rename-file (list filename newname ok-if-already-exists))))
3806
3807(defun tramp-do-copy-or-rename-file
3808 (op filename newname &optional ok-if-already-exists keep-date
3809 preserve-uid-gid preserve-selinux-context)
3810 "Copy or rename a remote file.
3811OP must be `copy' or `rename' and indicates the operation to perform.
3812FILENAME specifies the file to copy or rename, NEWNAME is the name of
3813the new file (for copy) or the new name of the file (for rename).
3814OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3815KEEP-DATE means to make sure that NEWNAME has the same timestamp
3816as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3817the uid and gid if both files are on the same host.
3818PRESERVE-SELINUX-CONTEXT activates selinux commands.
3819
3820This function is invoked by `tramp-handle-copy-file' and
3821`tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3822and `rename'. FILENAME and NEWNAME must be absolute file names."
3823 (unless (memq op '(copy rename))
3824 (error "Unknown operation `%s', must be `copy' or `rename'" op))
3825 (let ((t1 (tramp-tramp-file-p filename))
3826 (t2 (tramp-tramp-file-p newname))
3827 (context (and preserve-selinux-context
3828 (apply 'file-selinux-context (list filename))))
3829 pr tm)
3830
3831 (with-parsed-tramp-file-name (if t1 filename newname) nil
3832 (when (and (not ok-if-already-exists) (file-exists-p newname))
3833 (tramp-error
3834 v 'file-already-exists "File %s already exists" newname))
3835
3836 (with-progress-reporter
3837 v 0 (format "%s %s to %s"
3838 (if (eq op 'copy) "Copying" "Renaming")
3839 filename newname)
3840
3841 (cond
3842 ;; Both are Tramp files.
3843 ((and t1 t2)
3844 (with-parsed-tramp-file-name filename v1
3845 (with-parsed-tramp-file-name newname v2
3846 (cond
3847 ;; Shortcut: if method, host, user are the same for
3848 ;; both files, we invoke `cp' or `mv' on the remote
3849 ;; host directly.
3850 ((tramp-equal-remote filename newname)
3851 (tramp-do-copy-or-rename-file-directly
3852 op filename newname
3853 ok-if-already-exists keep-date preserve-uid-gid))
3854
3855 ;; Try out-of-band operation.
3856 ((tramp-method-out-of-band-p
3857 v1 (nth 7 (file-attributes filename)))
3858 (tramp-do-copy-or-rename-file-out-of-band
3859 op filename newname keep-date))
3860
3861 ;; No shortcut was possible. So we copy the file
3862 ;; first. If the operation was `rename', we go back
3863 ;; and delete the original file (if the copy was
3864 ;; successful). The approach is simple-minded: we
3865 ;; create a new buffer, insert the contents of the
3866 ;; source file into it, then write out the buffer to
3867 ;; the target file. The advantage is that it doesn't
3868 ;; matter which filename handlers are used for the
3869 ;; source and target file.
3870 (t
3871 (tramp-do-copy-or-rename-file-via-buffer
3872 op filename newname keep-date))))))
3873
3874 ;; One file is a Tramp file, the other one is local.
3875 ((or t1 t2)
3876 (cond
3877 ;; Fast track on local machine.
3878 ((tramp-local-host-p v)
3879 (tramp-do-copy-or-rename-file-directly
3880 op filename newname
3881 ok-if-already-exists keep-date preserve-uid-gid))
3882
3883 ;; If the Tramp file has an out-of-band method, the
3884 ;; corresponding copy-program can be invoked.
3885 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3886 (tramp-do-copy-or-rename-file-out-of-band
3887 op filename newname keep-date))
3888
3889 ;; Use the inline method via a Tramp buffer.
3890 (t (tramp-do-copy-or-rename-file-via-buffer
3891 op filename newname keep-date))))
3892
3893 (t
3894 ;; One of them must be a Tramp file.
3895 (error "Tramp implementation says this cannot happen")))
3896
3897 ;; Handle `preserve-selinux-context'.
3898 (when context (apply 'set-file-selinux-context (list newname context)))
3899
3900 ;; In case of `rename', we must flush the cache of the source file.
3901 (when (and t1 (eq op 'rename))
3902 (with-parsed-tramp-file-name filename v1
3903 (tramp-flush-file-property v1 (file-name-directory localname))
3904 (tramp-flush-file-property v1 localname)))
3905
3906 ;; When newname did exist, we have wrong cached values.
3907 (when t2
3908 (with-parsed-tramp-file-name newname v2
3909 (tramp-flush-file-property v2 (file-name-directory localname))
3910 (tramp-flush-file-property v2 localname)))))))
3911
3912(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3913 "Use an Emacs buffer to copy or rename a file.
3914First arg OP is either `copy' or `rename' and indicates the operation.
3915FILENAME is the source file, NEWNAME the target file.
3916KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3917 (with-temp-buffer
3918 ;; We must disable multibyte, because binary data shall not be
3919 ;; converted.
3920 (set-buffer-multibyte nil)
3921 (let ((coding-system-for-read 'binary)
3922 (jka-compr-inhibit t))
3923 (insert-file-contents-literally filename))
3924 ;; We don't want the target file to be compressed, so we let-bind
3925 ;; `jka-compr-inhibit' to t.
3926 (let ((coding-system-for-write 'binary)
3927 (jka-compr-inhibit t))
3928 (write-region (point-min) (point-max) newname)))
3929 ;; KEEP-DATE handling.
3930 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3931 ;; Set the mode.
3932 (set-file-modes newname (tramp-default-file-modes filename))
3933 ;; If the operation was `rename', delete the original file.
3934 (unless (eq op 'copy) (delete-file filename)))
3935
3936(defun tramp-do-copy-or-rename-file-directly
3937 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
3938 "Invokes `cp' or `mv' on the remote system.
3939OP must be one of `copy' or `rename', indicating `cp' or `mv',
3940respectively. FILENAME specifies the file to copy or rename,
3941NEWNAME is the name of the new file (for copy) or the new name of
3942the file (for rename). Both files must reside on the same host.
3943KEEP-DATE means to make sure that NEWNAME has the same timestamp
3944as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3945the uid and gid from FILENAME."
3946 (let ((t1 (tramp-tramp-file-p filename))
3947 (t2 (tramp-tramp-file-p newname))
3948 (file-times (nth 5 (file-attributes filename)))
3949 (file-modes (tramp-default-file-modes filename)))
3950 (with-parsed-tramp-file-name (if t1 filename newname) nil
3951 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3952 ((eq op 'copy) "cp -f")
3953 ((eq op 'rename) "mv -f")
3954 (t (tramp-error
3955 v 'file-error
3956 "Unknown operation `%s', must be `copy' or `rename'"
3957 op))))
3958 (localname1
3959 (if t1
3960 (tramp-file-name-handler 'file-remote-p filename 'localname)
3961 filename))
3962 (localname2
3963 (if t2
3964 (tramp-file-name-handler 'file-remote-p newname 'localname)
3965 newname))
3966 (prefix (file-remote-p (if t1 filename newname)))
3967 cmd-result)
3968
3969 (cond
3970 ;; Both files are on a remote host, with same user.
3971 ((and t1 t2)
3972 (setq cmd-result
3973 (tramp-send-command-and-check
3974 v
3975 (format "%s %s %s" cmd
3976 (tramp-shell-quote-argument localname1)
3977 (tramp-shell-quote-argument localname2))))
3978 (with-current-buffer (tramp-get-buffer v)
3979 (goto-char (point-min))
3980 (unless
3981 (or
3982 (and keep-date
3983 ;; Mask cp -f error.
3984 (re-search-forward
3985 tramp-operation-not-permitted-regexp nil t))
3986 (zerop cmd-result))
3987 (tramp-error-with-buffer
3988 nil v 'file-error
3989 "Copying directly failed, see buffer `%s' for details."
3990 (buffer-name)))))
3991
3992 ;; We are on the local host.
3993 ((or t1 t2)
3994 (cond
3995 ;; We can do it directly.
3996 ((let (file-name-handler-alist)
3997 (and (file-readable-p localname1)
3998 (file-writable-p (file-name-directory localname2))
3999 (or (file-directory-p localname2)
4000 (file-writable-p localname2))))
4001 (if (eq op 'copy)
4002 (tramp-compat-copy-file
4003 localname1 localname2 ok-if-already-exists
4004 keep-date preserve-uid-gid)
4005 (tramp-run-real-handler
4006 'rename-file (list localname1 localname2 ok-if-already-exists))))
4007
4008 ;; We can do it directly with `tramp-send-command'
4009 ((and (file-readable-p (concat prefix localname1))
4010 (file-writable-p
4011 (file-name-directory (concat prefix localname2)))
4012 (or (file-directory-p (concat prefix localname2))
4013 (file-writable-p (concat prefix localname2))))
4014 (tramp-do-copy-or-rename-file-directly
4015 op (concat prefix localname1) (concat prefix localname2)
4016 ok-if-already-exists keep-date t)
4017 ;; We must change the ownership to the local user.
4018 (tramp-set-file-uid-gid
4019 (concat prefix localname2)
4020 (tramp-get-local-uid 'integer)
4021 (tramp-get-local-gid 'integer)))
4022
4023 ;; We need a temporary file in between.
4024 (t
4025 ;; Create the temporary file.
4026 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
4027 (unwind-protect
4028 (progn
4029 (cond
4030 (t1
4031 (or
4032 (zerop
4033 (tramp-send-command-and-check
4034 v (format
4035 "%s %s %s" cmd
4036 (tramp-shell-quote-argument localname1)
4037 (tramp-shell-quote-argument tmpfile))))
4038 (tramp-error-with-buffer
4039 nil v 'file-error
4040 "Copying directly failed, see buffer `%s' for details."
4041 (tramp-get-buffer v)))
4042 ;; We must change the ownership as remote user.
4043 ;; Since this does not work reliable, we also
4044 ;; give read permissions.
4045 (set-file-modes
4046 (concat prefix tmpfile)
4047 (tramp-compat-octal-to-decimal "0777"))
4048 (tramp-set-file-uid-gid
4049 (concat prefix tmpfile)
4050 (tramp-get-local-uid 'integer)
4051 (tramp-get-local-gid 'integer)))
4052 (t2
4053 (if (eq op 'copy)
4054 (tramp-compat-copy-file
4055 localname1 tmpfile t
4056 keep-date preserve-uid-gid)
4057 (tramp-run-real-handler
4058 'rename-file
4059 (list localname1 tmpfile t)))
4060 ;; We must change the ownership as local user.
4061 ;; Since this does not work reliable, we also
4062 ;; give read permissions.
4063 (set-file-modes
4064 tmpfile (tramp-compat-octal-to-decimal "0777"))
4065 (tramp-set-file-uid-gid
4066 tmpfile
4067 (tramp-get-remote-uid v 'integer)
4068 (tramp-get-remote-gid v 'integer))))
4069
4070 ;; Move the temporary file to its destination.
4071 (cond
4072 (t2
4073 (or
4074 (zerop
4075 (tramp-send-command-and-check
4076 v (format
4077 "cp -f -p %s %s"
4078 (tramp-shell-quote-argument tmpfile)
4079 (tramp-shell-quote-argument localname2))))
4080 (tramp-error-with-buffer
4081 nil v 'file-error
4082 "Copying directly failed, see buffer `%s' for details."
4083 (tramp-get-buffer v))))
4084 (t1
4085 (tramp-run-real-handler
4086 'rename-file
4087 (list tmpfile localname2 ok-if-already-exists)))))
4088
4089 ;; Save exit.
4090 (condition-case nil
4091 (delete-file tmpfile)
4092 (error)))))))))
4093
4094 ;; Set the time and mode. Mask possible errors.
4095 (condition-case nil
4096 (when keep-date
4097 (set-file-times newname file-times)
4098 (set-file-modes newname file-modes))
4099 (error)))))
4100
4101(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
4102 "Invoke rcp program to copy.
4103The method used must be an out-of-band method."
4104 (let ((t1 (tramp-tramp-file-p filename))
4105 (t2 (tramp-tramp-file-p newname))
4106 copy-program copy-args copy-env copy-keep-date port spec
4107 source target)
4108
4109 (with-parsed-tramp-file-name (if t1 filename newname) nil
4110 (if (and t1 t2)
4111
4112 ;; Both are Tramp files. We shall optimize it, when the
4113 ;; methods for filename and newname are the same.
4114 (let* ((dir-flag (file-directory-p filename))
4115 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
4116 (if dir-flag
4117 (setq tmpfile
4118 (expand-file-name
4119 (file-name-nondirectory newname) tmpfile)))
4120 (unwind-protect
4121 (progn
4122 (tramp-do-copy-or-rename-file-out-of-band
4123 op filename tmpfile keep-date)
4124 (tramp-do-copy-or-rename-file-out-of-band
4125 'rename tmpfile newname keep-date))
4126 ;; Save exit.
4127 (condition-case nil
4128 (if dir-flag
4129 (tramp-compat-delete-directory
4130 (expand-file-name ".." tmpfile) 'recursive)
4131 (delete-file tmpfile))
4132 (error))))
4133
4134 ;; Expand hops. Might be necessary for gateway methods.
4135 (setq v (car (tramp-compute-multi-hops v)))
4136 (aset v 3 localname)
4137
4138 ;; Check which ones of source and target are Tramp files.
4139 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
4140 target (funcall
4141 (if (and (file-directory-p filename)
4142 (string-equal
4143 (file-name-nondirectory filename)
4144 (file-name-nondirectory newname)))
4145 'file-name-directory
4146 'identity)
4147 (if t2 (tramp-make-copy-program-file-name v) newname)))
4148
4149 ;; Check for port number. Until now, there's no need for handling
4150 ;; like method, user, host.
4151 (setq host (tramp-file-name-real-host v)
4152 port (tramp-file-name-port v)
4153 port (or (and port (number-to-string port)) ""))
4154
4155 ;; Compose copy command.
4156 (setq spec (format-spec-make
4157 ?h host ?u user ?p port
4158 ?t (tramp-get-connection-property
4159 (tramp-get-connection-process v) "temp-file" "")
4160 ?k (if keep-date " " ""))
4161 copy-program (tramp-get-method-parameter
4162 method 'tramp-copy-program)
4163 copy-keep-date (tramp-get-method-parameter
4164 method 'tramp-copy-keep-date)
4165 copy-args
4166 (delq
4167 nil
4168 (mapcar
4169 (lambda (x)
4170 (setq
4171 x
4172 ;; " " is indication for keep-date argument.
4173 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
4174 (unless (member "" x) (mapconcat 'identity x " ")))
4175 (tramp-get-method-parameter method 'tramp-copy-args)))
4176 copy-env
4177 (delq
4178 nil
4179 (mapcar
4180 (lambda (x)
4181 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4182 (unless (member "" x) (mapconcat 'identity x " ")))
4183 (tramp-get-method-parameter method 'tramp-copy-env))))
4184
4185 ;; Check for program.
4186 (when (and (fboundp 'executable-find)
4187 (not (let ((default-directory
4188 (tramp-compat-temporary-file-directory)))
4189 (executable-find copy-program))))
4190 (tramp-error
4191 v 'file-error "Cannot find copy program: %s" copy-program))
4192
4193 ;; Set variables for computing the prompt for reading
4194 ;; password.
4195 (setq tramp-current-method (tramp-file-name-method v)
4196 tramp-current-user (tramp-file-name-user v)
4197 tramp-current-host (tramp-file-name-host v))
4198
4199 (unwind-protect
4200 (with-temp-buffer
4201 ;; The default directory must be remote.
4202 (let ((default-directory
4203 (file-name-directory (if t1 filename newname)))
4204 (process-environment (copy-sequence process-environment)))
4205 ;; Set the transfer process properties.
4206 (tramp-set-connection-property
4207 v "process-name" (buffer-name (current-buffer)))
4208 (tramp-set-connection-property
4209 v "process-buffer" (current-buffer))
4210 (while copy-env
4211 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4212 (setenv (pop copy-env) (pop copy-env)))
4213
4214 ;; Use an asynchronous process. By this, password can
4215 ;; be handled. The default directory must be local, in
4216 ;; order to apply the correct `copy-program'. We don't
4217 ;; set a timeout, because the copying of large files can
4218 ;; last longer than 60 secs.
4219 (let ((p (let ((default-directory
4220 (tramp-compat-temporary-file-directory)))
4221 (apply 'start-process
4222 (tramp-get-connection-property
4223 v "process-name" nil)
4224 (tramp-get-connection-property
4225 v "process-buffer" nil)
4226 copy-program
4227 (append copy-args (list source target))))))
4228 (tramp-message
4229 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4230 (tramp-set-process-query-on-exit-flag p nil)
4231 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
4232
4233 ;; Reset the transfer process properties.
4234 (tramp-set-connection-property v "process-name" nil)
4235 (tramp-set-connection-property v "process-buffer" nil))
4236
4237 ;; Handle KEEP-DATE argument.
4238 (when (and keep-date (not copy-keep-date))
4239 (set-file-times newname (nth 5 (file-attributes filename))))
4240
4241 ;; Set the mode.
4242 (unless (and keep-date copy-keep-date)
4243 (ignore-errors
4244 (set-file-modes newname (tramp-default-file-modes filename)))))
4245
4246 ;; If the operation was `rename', delete the original file.
4247 (unless (eq op 'copy)
4248 (if (file-regular-p filename)
4249 (delete-file filename)
4250 (tramp-compat-delete-directory filename 'recursive))))))
4251
4252(defun tramp-handle-make-directory (dir &optional parents)
4253 "Like `make-directory' for Tramp files."
4254 (setq dir (expand-file-name dir))
4255 (with-parsed-tramp-file-name dir nil
4256 (tramp-flush-directory-property v (file-name-directory localname))
4257 (save-excursion
4258 (tramp-barf-unless-okay
4259 v
4260 (format "%s %s"
4261 (if parents "mkdir -p" "mkdir")
4262 (tramp-shell-quote-argument localname))
4263 "Couldn't make directory %s" dir))))
4264
4265(defun tramp-handle-delete-directory (directory &optional recursive)
4266 "Like `delete-directory' for Tramp files."
4267 (setq directory (expand-file-name directory))
4268 (with-parsed-tramp-file-name directory nil
4269 (tramp-flush-file-property v (file-name-directory localname))
4270 (tramp-flush-directory-property v localname)
4271 (unless (zerop (tramp-send-command-and-check
4272 v
4273 (format
4274 "%s %s"
4275 (if recursive "rm -rf" "rmdir")
4276 (tramp-shell-quote-argument localname))))
4277 (tramp-error v 'file-error "Couldn't delete %s" directory))))
4278
4279(defun tramp-handle-delete-file (filename &optional trash)
4280 "Like `delete-file' for Tramp files."
4281 (setq filename (expand-file-name filename))
4282 (with-parsed-tramp-file-name filename nil
4283 (tramp-flush-file-property v (file-name-directory localname))
4284 (tramp-flush-file-property v localname)
4285 (unless
4286 (zerop
4287 (tramp-send-command-and-check
4288 v (format "%s %s"
4289 (or (and trash (tramp-get-remote-trash v)) "rm -f")
4290 (tramp-shell-quote-argument localname))))
4291 (tramp-error v 'file-error "Couldn't delete %s" filename))))
4292
4293;; Dired.
4294
4295;; CCC: This does not seem to be enough. Something dies when
4296;; we try and delete two directories under Tramp :/
4297(defun tramp-handle-dired-recursive-delete-directory (filename)
4298 "Recursively delete the directory given.
4299This is like `dired-recursive-delete-directory' for Tramp files."
4300 (with-parsed-tramp-file-name filename nil
4301 ;; Run a shell command 'rm -r <localname>'
4302 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
4303 (unless (file-exists-p filename)
4304 (tramp-error v 'file-error "No such directory: %s" filename))
4305 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
4306 (tramp-send-command
4307 v
4308 (format "rm -rf %s" (tramp-shell-quote-argument localname))
4309 ;; Don't read the output, do it explicitely.
4310 nil t)
4311 ;; Wait for the remote system to return to us...
4312 ;; This might take a while, allow it plenty of time.
4313 (tramp-wait-for-output (tramp-get-connection-process v) 120)
4314 ;; Make sure that it worked...
4315 (tramp-flush-file-property v (file-name-directory localname))
4316 (tramp-flush-directory-property v localname)
4317 (and (file-exists-p filename)
4318 (tramp-error
4319 v 'file-error "Failed to recursively delete %s" filename))))
4320
4321(defun tramp-handle-dired-compress-file (file &rest ok-flag)
4322 "Like `dired-compress-file' for Tramp files."
4323 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4324 ;; Code stolen mainly from dired-aux.el.
4325 (with-parsed-tramp-file-name file nil
4326 (tramp-flush-file-property v localname)
4327 (save-excursion
4328 (let ((suffixes
4329 (if (not (featurep 'xemacs))
4330 ;; Emacs case
4331 (symbol-value 'dired-compress-file-suffixes)
4332 ;; XEmacs has `dired-compression-method-alist', which is
4333 ;; transformed into `dired-compress-file-suffixes' structure.
4334 (mapcar
4335 (lambda (x)
4336 (list (concat (regexp-quote (nth 1 x)) "\\'")
4337 nil
4338 (mapconcat 'identity (nth 3 x) " ")))
4339 (symbol-value 'dired-compression-method-alist))))
4340 suffix)
4341 ;; See if any suffix rule matches this file name.
4342 (while suffixes
4343 (let (case-fold-search)
4344 (if (string-match (car (car suffixes)) localname)
4345 (setq suffix (car suffixes) suffixes nil))
4346 (setq suffixes (cdr suffixes))))
4347
4348 (cond ((file-symlink-p file)
4349 nil)
4350 ((and suffix (nth 2 suffix))
4351 ;; We found an uncompression rule.
4352 (with-progress-reporter v 0 (format "Uncompressing %s" file)
4353 (when (zerop
4354 (tramp-send-command-and-check
4355 v (concat (nth 2 suffix) " "
4356 (tramp-shell-quote-argument localname))))
4357 ;; `dired-remove-file' is not defined in XEmacs.
4358 (tramp-compat-funcall 'dired-remove-file file)
4359 (string-match (car suffix) file)
4360 (concat (substring file 0 (match-beginning 0))))))
4361 (t
4362 ;; We don't recognize the file as compressed, so compress it.
4363 ;; Try gzip.
4364 (with-progress-reporter v 0 (format "Compressing %s" file)
4365 (when (zerop
4366 (tramp-send-command-and-check
4367 v (concat "gzip -f "
4368 (tramp-shell-quote-argument localname))))
4369 ;; `dired-remove-file' is not defined in XEmacs.
4370 (tramp-compat-funcall 'dired-remove-file file)
4371 (cond ((file-exists-p (concat file ".gz"))
4372 (concat file ".gz"))
4373 ((file-exists-p (concat file ".z"))
4374 (concat file ".z"))
4375 (t nil))))))))))
4376
4377(defun tramp-handle-dired-uncache (dir &optional dir-p)
4378 "Like `dired-uncache' for Tramp files."
4379 ;; DIR-P is valid for XEmacs only.
4380 (with-parsed-tramp-file-name
4381 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
4382 (tramp-flush-directory-property v localname)))
4383
4384;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4385;; not sure at all that this is the right way to do it, but let's hope
4386;; it works for now, and wait for a guru to point out the Right Way to
4387;; achieve this.
4388;;(eval-when-compile
4389;; (unless (fboundp 'dired-insert-set-properties)
4390;; (fset 'dired-insert-set-properties 'ignore)))
4391;; Gerd suggests this:
4392(eval-when-compile (require 'dired))
4393;; Note that dired is required at run-time, too, when it is needed.
4394;; It is only needed on XEmacs for the function
4395;; `dired-insert-set-properties'.
4396
4397(defun tramp-handle-insert-directory
4398 (filename switches &optional wildcard full-directory-p)
4399 "Like `insert-directory' for Tramp files."
4400 (setq filename (expand-file-name filename))
4401 (with-parsed-tramp-file-name filename nil
4402 (if (and (featurep 'ls-lisp)
4403 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4404 (tramp-run-real-handler
4405 'insert-directory (list filename switches wildcard full-directory-p))
4406 (when (stringp switches)
4407 (setq switches (split-string switches)))
4408 (when (and (member "--dired" switches)
4409 (not (tramp-get-ls-command-with-dired v)))
4410 (setq switches (delete "--dired" switches)))
4411 (when wildcard
4412 (setq wildcard (tramp-run-real-handler
4413 'file-name-nondirectory (list localname)))
4414 (setq localname (tramp-run-real-handler
4415 'file-name-directory (list localname))))
4416 (unless full-directory-p
4417 (setq switches (add-to-list 'switches "-d" 'append)))
4418 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
4419 (when wildcard
4420 (setq switches (concat switches " " wildcard)))
4421 (tramp-message
4422 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4423 switches filename (if wildcard "yes" "no")
4424 (if full-directory-p "yes" "no"))
4425 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4426 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4427 (if full-directory-p
4428 (tramp-send-command
4429 v
4430 (format "%s %s %s 2>/dev/null"
4431 (tramp-get-ls-command v)
4432 switches
4433 (if wildcard
4434 localname
4435 (tramp-shell-quote-argument (concat localname ".")))))
4436 (tramp-barf-unless-okay
4437 v
4438 (format "cd %s" (tramp-shell-quote-argument
4439 (tramp-run-real-handler
4440 'file-name-directory (list localname))))
4441 "Couldn't `cd %s'"
4442 (tramp-shell-quote-argument
4443 (tramp-run-real-handler 'file-name-directory (list localname))))
4444 (tramp-send-command
4445 v
4446 (format "%s %s %s"
4447 (tramp-get-ls-command v)
4448 switches
4449 (if (or wildcard
4450 (zerop (length
4451 (tramp-run-real-handler
4452 'file-name-nondirectory (list localname)))))
4453 ""
4454 (tramp-shell-quote-argument
4455 (tramp-run-real-handler
4456 'file-name-nondirectory (list localname)))))))
4457 (let ((beg (point)))
4458 ;; We cannot use `insert-buffer-substring' because the Tramp
4459 ;; buffer changes its contents before insertion due to calling
4460 ;; `expand-file' and alike.
4461 (insert
4462 (with-current-buffer (tramp-get-buffer v)
4463 (buffer-string)))
4464
4465 ;; Check for "--dired" output.
4466 (forward-line -2)
4467 (when (looking-at "//SUBDIRED//")
4468 (forward-line -1))
4469 (when (looking-at "//DIRED//\\s-+")
4470 (let ((databeg (match-end 0))
4471 (end (tramp-compat-line-end-position)))
4472 ;; Now read the numeric positions of file names.
4473 (goto-char databeg)
4474 (while (< (point) end)
4475 (let ((start (+ beg (read (current-buffer))))
4476 (end (+ beg (read (current-buffer)))))
4477 (if (memq (char-after end) '(?\n ?\ ))
4478 ;; End is followed by \n or by " -> ".
4479 (put-text-property start end 'dired-filename t))))))
4480 ;; Remove trailing lines.
4481 (goto-char (tramp-compat-line-beginning-position))
4482 (while (looking-at "//")
4483 (forward-line 1)
4484 (delete-region (match-beginning 0) (point)))
4485
4486 ;; The inserted file could be from somewhere else.
4487 (when (and (not wildcard) (not full-directory-p))
4488 (goto-char (point-max))
4489 (when (file-symlink-p filename)
4490 (goto-char (search-backward "->" beg 'noerror)))
4491 (search-backward
4492 (if (zerop (length (file-name-nondirectory filename)))
4493 "."
4494 (file-name-nondirectory filename))
4495 beg 'noerror)
4496 (replace-match (file-relative-name filename) t))
4497
4498 (goto-char (point-max))))))
4499
4500(defun tramp-handle-unhandled-file-name-directory (filename)
4501 "Like `unhandled-file-name-directory' for Tramp files."
4502 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4503 ;; for backward compatibility.
4504 (expand-file-name "~/"))
4505
4506;; Canonicalization of file names.
4507
4508(defun tramp-handle-expand-file-name (name &optional dir)
4509 "Like `expand-file-name' for Tramp files.
4510If the localname part of the given filename starts with \"/../\" then
4511the result will be a local, non-Tramp, filename."
4512 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4513 (setq dir (or dir default-directory "/"))
4514 ;; Unless NAME is absolute, concat DIR and NAME.
4515 (unless (file-name-absolute-p name)
4516 (setq name (concat (file-name-as-directory dir) name)))
4517 ;; If NAME is not a Tramp file, run the real handler.
4518 (if (not (tramp-connectable-p name))
4519 (tramp-run-real-handler 'expand-file-name (list name nil))
4520 ;; Dissect NAME.
4521 (with-parsed-tramp-file-name name nil
4522 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
4523 (setq localname (concat "~/" localname)))
4524 ;; Tilde expansion if necessary. This needs a shell which
4525 ;; groks tilde expansion! The function `tramp-find-shell' is
4526 ;; supposed to find such a shell on the remote host. Please
4527 ;; tell me about it when this doesn't work on your system.
4528 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4529 (let ((uname (match-string 1 localname))
4530 (fname (match-string 2 localname)))
4531 ;; We cannot simply apply "~/", because under sudo "~/" is
4532 ;; expanded to the local user home directory but to the
4533 ;; root home directory. On the other hand, using always
4534 ;; the default user name for tilde expansion is not
4535 ;; appropriate either, because ssh and companions might
4536 ;; use a user name from the config file.
4537 (when (and (string-equal uname "~")
4538 (string-match "\\`su\\(do\\)?\\'" method))
4539 (setq uname (concat uname user)))
4540 (setq uname
4541 (with-connection-property v uname
4542 (tramp-send-command
4543 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4544 (with-current-buffer (tramp-get-buffer v)
4545 (goto-char (point-min))
4546 (buffer-substring
4547 (point) (tramp-compat-line-end-position)))))
4548 (setq localname (concat uname fname))))
4549 ;; There might be a double slash, for example when "~/"
4550 ;; expands to "/". Remove this.
4551 (while (string-match "//" localname)
4552 (setq localname (replace-match "/" t t localname)))
4553 ;; No tilde characters in file name, do normal
4554 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4555 ;; `directory-sep-char' here for XEmacs on Windows, which would
4556 ;; otherwise use backslash. `default-directory' is bound,
4557 ;; because on Windows there would be problems with UNC shares or
4558 ;; Cygwin mounts.
4559 (let ((directory-sep-char ?/)
4560 (default-directory (tramp-compat-temporary-file-directory)))
4561 (tramp-make-tramp-file-name
4562 method user host
4563 (tramp-drop-volume-letter
4564 (tramp-run-real-handler
4565 'expand-file-name (list localname))))))))
4566
4567(defun tramp-replace-environment-variables (filename) 1615(defun tramp-replace-environment-variables (filename)
4568 "Replace environment variables in FILENAME. 1616 "Replace environment variables in FILENAME.
4569Return the string with the replaced variables." 1617Return the string with the replaced variables."
@@ -4579,38 +1627,6 @@ Return the string with the replaced variables."
4579 t nil filename))) 1627 t nil filename)))
4580 filename))) 1628 filename)))
4581 1629
4582(defun tramp-handle-substitute-in-file-name (filename)
4583 "Like `substitute-in-file-name' for Tramp files.
4584\"//\" and \"/~\" substitute only in the local filename part.
4585If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4586beginning of local filename are not substituted."
4587 ;; First, we must replace environment variables.
4588 (setq filename (tramp-replace-environment-variables filename))
4589 (with-parsed-tramp-file-name filename nil
4590 (if (equal tramp-syntax 'url)
4591 ;; We need to check localname only. The other parts cannot contain
4592 ;; "//" or "/~".
4593 (if (and (> (length localname) 1)
4594 (or (string-match "//" localname)
4595 (string-match "/~" localname 1)))
4596 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4597 (tramp-make-tramp-file-name
4598 (when method (substitute-in-file-name method))
4599 (when user (substitute-in-file-name user))
4600 (when host (substitute-in-file-name host))
4601 (when localname
4602 (tramp-run-real-handler
4603 'substitute-in-file-name (list localname)))))
4604 ;; Ignore in LOCALNAME everything before "//" or "/~".
4605 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4606 (setq filename
4607 (concat (file-remote-p filename)
4608 (replace-match "\\1" nil nil localname)))
4609 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4610 (when (string-match "~$" filename)
4611 (setq filename (concat filename "/"))))
4612 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4613
4614;; In XEmacs, electricity is implemented via a key map for ?/ and ?~, 1630;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4615;; which calls corresponding functions (see minibuf.el). 1631;; which calls corresponding functions (see minibuf.el).
4616(when (fboundp 'minibuffer-electric-separator) 1632(when (fboundp 'minibuffer-electric-separator)
@@ -4640,392 +1656,9 @@ beginning of local filename are not substituted."
4640 '(minibuffer-electric-separator 1656 '(minibuffer-electric-separator
4641 minibuffer-electric-tilde))) 1657 minibuffer-electric-tilde)))
4642 1658
4643
4644;;; Remote commands:
4645
4646(defun tramp-handle-executable-find (command)
4647 "Like `executable-find' for Tramp files."
4648 (with-parsed-tramp-file-name default-directory nil
4649 (tramp-find-executable v command (tramp-get-remote-path v) t)))
4650
4651(defun tramp-process-sentinel (proc event)
4652 "Flush file caches."
4653 (unless (memq (process-status proc) '(run open))
4654 (let ((vec (tramp-get-connection-property proc "vector" nil)))
4655 (when vec
4656 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
4657 (tramp-flush-directory-property vec "")))))
4658
4659;; We use BUFFER also as connection buffer during setup. Because of
4660;; this, its original contents must be saved, and restored once
4661;; connection has been setup.
4662(defun tramp-handle-start-file-process (name buffer program &rest args)
4663 "Like `start-file-process' for Tramp files."
4664 (with-parsed-tramp-file-name default-directory nil
4665 (unwind-protect
4666 ;; When PROGRAM is nil, we just provide a tty.
4667 (let ((command
4668 (when (stringp program)
4669 (format "cd %s; exec %s"
4670 (tramp-shell-quote-argument localname)
4671 (mapconcat 'tramp-shell-quote-argument
4672 (cons program args) " "))))
4673 (tramp-process-connection-type
4674 (or (null program) tramp-process-connection-type))
4675 (name1 name)
4676 (i 0))
4677 (unless buffer
4678 ;; BUFFER can be nil. We use a temporary buffer.
4679 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
4680 (while (get-process name1)
4681 ;; NAME must be unique as process name.
4682 (setq i (1+ i)
4683 name1 (format "%s<%d>" name i)))
4684 (setq name name1)
4685 ;; Set the new process properties.
4686 (tramp-set-connection-property v "process-name" name)
4687 (tramp-set-connection-property v "process-buffer" buffer)
4688 ;; Activate narrowing in order to save BUFFER contents.
4689 ;; Clear also the modification time; otherwise we might be
4690 ;; interrupted by `verify-visited-file-modtime'.
4691 (with-current-buffer (tramp-get-connection-buffer v)
4692 (clear-visited-file-modtime)
4693 (narrow-to-region (point-max) (point-max)))
4694 (if command
4695 ;; Send the command.
4696 (tramp-send-command v command nil t) ; nooutput
4697 ;; Check, whether a pty is associated.
4698 (tramp-maybe-open-connection v)
4699 (unless (process-get (tramp-get-connection-process v) 'remote-tty)
4700 (tramp-error
4701 v 'file-error "pty association is not supported for `%s'" name)))
4702 (let ((p (tramp-get-connection-process v)))
4703 ;; Set sentinel and query flag for this process.
4704 (tramp-set-connection-property p "vector" v)
4705 (set-process-sentinel p 'tramp-process-sentinel)
4706 (tramp-set-process-query-on-exit-flag p t)
4707 ;; Return process.
4708 p))
4709 ;; Save exit.
4710 (with-current-buffer (tramp-get-connection-buffer v)
4711 (if (string-match tramp-temp-buffer-name (buffer-name))
4712 (progn
4713 (set-process-buffer (tramp-get-connection-process v) nil)
4714 (kill-buffer (current-buffer)))
4715 (widen)
4716 (goto-char (point-max))))
4717 (tramp-set-connection-property v "process-name" nil)
4718 (tramp-set-connection-property v "process-buffer" nil))))
4719
4720(defun tramp-handle-process-file
4721 (program &optional infile destination display &rest args)
4722 "Like `process-file' for Tramp files."
4723 ;; The implementation is not complete yet.
4724 (when (and (numberp destination) (zerop destination))
4725 (error "Implementation does not handle immediate return"))
4726
4727 (with-parsed-tramp-file-name default-directory nil
4728 (let (command input tmpinput stderr tmpstderr outbuf ret)
4729 ;; Compute command.
4730 (setq command (mapconcat 'tramp-shell-quote-argument
4731 (cons program args) " "))
4732 ;; Determine input.
4733 (if (null infile)
4734 (setq input "/dev/null")
4735 (setq infile (expand-file-name infile))
4736 (if (tramp-equal-remote default-directory infile)
4737 ;; INFILE is on the same remote host.
4738 (setq input (with-parsed-tramp-file-name infile nil localname))
4739 ;; INFILE must be copied to remote host.
4740 (setq input (tramp-make-tramp-temp-file v)
4741 tmpinput (tramp-make-tramp-file-name method user host input))
4742 (copy-file infile tmpinput t)))
4743 (when input (setq command (format "%s <%s" command input)))
4744
4745 ;; Determine output.
4746 (cond
4747 ;; Just a buffer.
4748 ((bufferp destination)
4749 (setq outbuf destination))
4750 ;; A buffer name.
4751 ((stringp destination)
4752 (setq outbuf (get-buffer-create destination)))
4753 ;; (REAL-DESTINATION ERROR-DESTINATION)
4754 ((consp destination)
4755 ;; output.
4756 (cond
4757 ((bufferp (car destination))
4758 (setq outbuf (car destination)))
4759 ((stringp (car destination))
4760 (setq outbuf (get-buffer-create (car destination))))
4761 ((car destination)
4762 (setq outbuf (current-buffer))))
4763 ;; stderr.
4764 (cond
4765 ((stringp (cadr destination))
4766 (setcar (cdr destination) (expand-file-name (cadr destination)))
4767 (if (tramp-equal-remote default-directory (cadr destination))
4768 ;; stderr is on the same remote host.
4769 (setq stderr (with-parsed-tramp-file-name
4770 (cadr destination) nil localname))
4771 ;; stderr must be copied to remote host. The temporary
4772 ;; file must be deleted after execution.
4773 (setq stderr (tramp-make-tramp-temp-file v)
4774 tmpstderr (tramp-make-tramp-file-name
4775 method user host stderr))))
4776 ;; stderr to be discarded.
4777 ((null (cadr destination))
4778 (setq stderr "/dev/null"))))
4779 ;; 't
4780 (destination
4781 (setq outbuf (current-buffer))))
4782 (when stderr (setq command (format "%s 2>%s" command stderr)))
4783
4784 ;; Send the command. It might not return in time, so we protect
4785 ;; it. Call it in a subshell, in order to preserve working
4786 ;; directory.
4787 (condition-case nil
4788 (unwind-protect
4789 (setq ret
4790 (tramp-send-command-and-check
4791 v (format "\\cd %s; %s"
4792 (tramp-shell-quote-argument localname)
4793 command)
4794 t t))
4795 ;; We should show the output anyway.
4796 (when outbuf
4797 (with-current-buffer outbuf
4798 (insert
4799 (with-current-buffer (tramp-get-connection-buffer v)
4800 (buffer-string))))
4801 (when display (display-buffer outbuf))))
4802 ;; When the user did interrupt, we should do it also. We use
4803 ;; return code -1 as marker.
4804 (quit
4805 (kill-buffer (tramp-get-connection-buffer v))
4806 (setq ret -1))
4807 ;; Handle errors.
4808 (error
4809 (kill-buffer (tramp-get-connection-buffer v))
4810 (setq ret 1)))
4811
4812 ;; Provide error file.
4813 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
4814
4815 ;; Cleanup. We remove all file cache values for the connection,
4816 ;; because the remote process could have changed them.
4817 (when tmpinput (delete-file tmpinput))
4818
4819 ;; `process-file-side-effects' has been introduced with GNU
4820 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4821 ;; by `program'. If it doesn't exist, we assume its default
4822 ;; value 't'.
4823 (unless (and (boundp 'process-file-side-effects)
4824 (not (symbol-value 'process-file-side-effects)))
4825 (tramp-flush-directory-property v ""))
4826
4827 ;; Return exit status.
4828 (if (equal ret -1)
4829 (keyboard-quit)
4830 ret))))
4831
4832(defun tramp-handle-call-process-region
4833 (start end program &optional delete buffer display &rest args)
4834 "Like `call-process-region' for Tramp files."
4835 (let ((tmpfile (tramp-compat-make-temp-file "")))
4836 (write-region start end tmpfile)
4837 (when delete (delete-region start end))
4838 (unwind-protect
4839 (apply 'call-process program tmpfile buffer display args)
4840 (delete-file tmpfile))))
4841
4842(defun tramp-handle-shell-command
4843 (command &optional output-buffer error-buffer)
4844 "Like `shell-command' for Tramp files."
4845 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
4846 ;; We cannot use `shell-file-name' and `shell-command-switch',
4847 ;; they are variables of the local host.
4848 (args (list
4849 (tramp-get-method-parameter
4850 (tramp-file-name-method
4851 (tramp-dissect-file-name default-directory))
4852 'tramp-remote-sh)
4853 "-c" (substring command 0 asynchronous)))
4854 current-buffer-p
4855 (output-buffer
4856 (cond
4857 ((bufferp output-buffer) output-buffer)
4858 ((stringp output-buffer) (get-buffer-create output-buffer))
4859 (output-buffer
4860 (setq current-buffer-p t)
4861 (current-buffer))
4862 (t (get-buffer-create
4863 (if asynchronous
4864 "*Async Shell Command*"
4865 "*Shell Command Output*")))))
4866 (error-buffer
4867 (cond
4868 ((bufferp error-buffer) error-buffer)
4869 ((stringp error-buffer) (get-buffer-create error-buffer))))
4870 (buffer
4871 (if (and (not asynchronous) error-buffer)
4872 (with-parsed-tramp-file-name default-directory nil
4873 (list output-buffer (tramp-make-tramp-temp-file v)))
4874 output-buffer))
4875 (p (get-buffer-process output-buffer)))
4876
4877 ;; Check whether there is another process running. Tramp does not
4878 ;; support 2 (asynchronous) processes in parallel.
4879 (when p
4880 (if (yes-or-no-p "A command is running. Kill it? ")
4881 (condition-case nil
4882 (kill-process p)
4883 (error nil))
4884 (error "Shell command in progress")))
4885
4886 (if current-buffer-p
4887 (progn
4888 (barf-if-buffer-read-only)
4889 (push-mark nil t))
4890 (with-current-buffer output-buffer
4891 (setq buffer-read-only nil)
4892 (erase-buffer)))
4893
4894 (if (and (not current-buffer-p) (integerp asynchronous))
4895 (prog1
4896 ;; Run the process.
4897 (apply 'start-file-process "*Async Shell*" buffer args)
4898 ;; Display output.
4899 (pop-to-buffer output-buffer)
4900 (setq mode-line-process '(":%s"))
4901 (shell-mode))
4902
4903 (prog1
4904 ;; Run the process.
4905 (apply 'process-file (car args) nil buffer nil (cdr args))
4906 ;; Insert error messages if they were separated.
4907 (when (listp buffer)
4908 (with-current-buffer error-buffer
4909 (insert-file-contents (cadr buffer)))
4910 (delete-file (cadr buffer)))
4911 (if current-buffer-p
4912 ;; This is like exchange-point-and-mark, but doesn't
4913 ;; activate the mark. It is cleaner to avoid activation,
4914 ;; even though the command loop would deactivate the mark
4915 ;; because we inserted text.
4916 (goto-char (prog1 (mark t)
4917 (set-marker (mark-marker) (point)
4918 (current-buffer))))
4919 ;; There's some output, display it.
4920 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4921 (if (functionp 'display-message-or-buffer)
4922 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
4923 (pop-to-buffer output-buffer))))))))
4924
4925;; File Editing.
4926
4927(defvar tramp-handle-file-local-copy-hook nil 1659(defvar tramp-handle-file-local-copy-hook nil
4928 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.") 1660 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4929 1661
4930(defun tramp-handle-file-local-copy (filename)
4931 "Like `file-local-copy' for Tramp files."
4932
4933 (with-parsed-tramp-file-name filename nil
4934 (unless (file-exists-p filename)
4935 (tramp-error
4936 v 'file-error
4937 "Cannot make local copy of non-existing file `%s'" filename))
4938
4939 (let* ((size (nth 7 (file-attributes filename)))
4940 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4941 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4942 (tmpfile (tramp-compat-make-temp-file filename)))
4943
4944 (condition-case err
4945 (cond
4946 ;; `copy-file' handles direct copy and out-of-band methods.
4947 ((or (tramp-local-host-p v)
4948 (tramp-method-out-of-band-p v size))
4949 (copy-file filename tmpfile t t))
4950
4951 ;; Use inline encoding for file transfer.
4952 (rem-enc
4953 (save-excursion
4954 (with-progress-reporter
4955 v 3 (format "Encoding remote file %s" filename)
4956 (tramp-barf-unless-okay
4957 v (format rem-enc (tramp-shell-quote-argument localname))
4958 "Encoding remote file failed"))
4959
4960 (if (functionp loc-dec)
4961 ;; If local decoding is a function, we call it. We
4962 ;; must disable multibyte, because
4963 ;; `uudecode-decode-region' doesn't handle it
4964 ;; correctly.
4965 (with-temp-buffer
4966 (set-buffer-multibyte nil)
4967 (insert-buffer-substring (tramp-get-buffer v))
4968 (with-progress-reporter
4969 v 3 (format "Decoding remote file %s with function %s"
4970 filename loc-dec)
4971 (funcall loc-dec (point-min) (point-max))
4972 ;; Unset `file-name-handler-alist'. Otherwise,
4973 ;; epa-file gets confused.
4974 (let (file-name-handler-alist
4975 (coding-system-for-write 'binary))
4976 (write-region (point-min) (point-max) tmpfile))))
4977
4978 ;; If tramp-decoding-function is not defined for this
4979 ;; method, we invoke tramp-decoding-command instead.
4980 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
4981 ;; Unset `file-name-handler-alist'. Otherwise,
4982 ;; epa-file gets confused.
4983 (let (file-name-handler-alist
4984 (coding-system-for-write 'binary))
4985 (write-region (point-min) (point-max) tmpfile2))
4986 (with-progress-reporter
4987 v 3 (format "Decoding remote file %s with command %s"
4988 filename loc-dec)
4989 (unwind-protect
4990 (tramp-call-local-coding-command
4991 loc-dec tmpfile2 tmpfile)
4992 (delete-file tmpfile2)))))
4993
4994 ;; Set proper permissions.
4995 (set-file-modes tmpfile (tramp-default-file-modes filename))
4996 ;; Set local user ownership.
4997 (tramp-set-file-uid-gid tmpfile)))
4998
4999 ;; Oops, I don't know what to do.
5000 (t (tramp-error
5001 v 'file-error "Wrong method specification for `%s'" method)))
5002
5003 ;; Error handling.
5004 ((error quit)
5005 (delete-file tmpfile)
5006 (signal (car err) (cdr err))))
5007
5008 (run-hooks 'tramp-handle-file-local-copy-hook)
5009 tmpfile)))
5010
5011(defun tramp-handle-file-remote-p (filename &optional identification connected)
5012 "Like `file-remote-p' for Tramp files."
5013 (let ((tramp-verbose 3))
5014 (when (tramp-tramp-file-p filename)
5015 (let* ((v (tramp-dissect-file-name filename))
5016 (p (tramp-get-connection-process v))
5017 (c (and p (processp p) (memq (process-status p) '(run open)))))
5018 ;; We expand the file name only, if there is already a connection.
5019 (with-parsed-tramp-file-name
5020 (if c (expand-file-name filename) filename) nil
5021 (and (or (not connected) c)
5022 (cond
5023 ((eq identification 'method) method)
5024 ((eq identification 'user) user)
5025 ((eq identification 'host) host)
5026 ((eq identification 'localname) localname)
5027 (t (tramp-make-tramp-file-name method user host "")))))))))
5028
5029(defun tramp-find-file-name-coding-system-alist (filename tmpname) 1662(defun tramp-find-file-name-coding-system-alist (filename tmpname)
5030 "Like `find-operation-coding-system' for Tramp filenames. 1663 "Like `find-operation-coding-system' for Tramp filenames.
5031Tramp's `insert-file-contents' and `write-region' work over 1664Tramp's `insert-file-contents' and `write-region' work over
@@ -5041,533 +1674,6 @@ coding system might not be determined. This function repairs it."
5041 (add-to-list 1674 (add-to-list
5042 'result (cons (regexp-quote tmpname) (cdr elt)) 'append))))) 1675 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
5043 1676
5044(defun tramp-handle-insert-file-contents
5045 (filename &optional visit beg end replace)
5046 "Like `insert-file-contents' for Tramp files."
5047 (barf-if-buffer-read-only)
5048 (setq filename (expand-file-name filename))
5049 (let (result local-copy remote-copy)
5050 (with-parsed-tramp-file-name filename nil
5051 (unwind-protect
5052 (if (not (file-exists-p filename))
5053 ;; We don't raise a Tramp error, because it might be
5054 ;; suppressed, like in `find-file-noselect-1'.
5055 (signal 'file-error
5056 (list "File not found on remote host" filename))
5057
5058 (if (and (tramp-local-host-p v)
5059 (let (file-name-handler-alist)
5060 (file-readable-p localname)))
5061 ;; Short track: if we are on the local host, we can
5062 ;; run directly.
5063 (setq result
5064 (tramp-run-real-handler
5065 'insert-file-contents
5066 (list localname visit beg end replace)))
5067
5068 ;; When we shall insert only a part of the file, we copy
5069 ;; this part.
5070 (when (or beg end)
5071 (setq remote-copy (tramp-make-tramp-temp-file v))
5072 (tramp-send-command
5073 v
5074 (cond
5075 ((and beg end)
5076 (format "tail -c +%d %s | head -c +%d >%s"
5077 (1+ beg) (tramp-shell-quote-argument localname)
5078 (- end beg) remote-copy))
5079 (beg
5080 (format "tail -c +%d %s >%s"
5081 (1+ beg) (tramp-shell-quote-argument localname)
5082 remote-copy))
5083 (end
5084 (format "head -c +%d %s >%s"
5085 (1+ end) (tramp-shell-quote-argument localname)
5086 remote-copy)))))
5087
5088 ;; `insert-file-contents-literally' takes care to avoid
5089 ;; calling jka-compr. By let-binding
5090 ;; `inhibit-file-name-operation', we propagate that care
5091 ;; to the `file-local-copy' operation.
5092 (setq local-copy
5093 (let ((inhibit-file-name-operation
5094 (when (eq inhibit-file-name-operation
5095 'insert-file-contents)
5096 'file-local-copy)))
5097 (cond
5098 ((stringp remote-copy)
5099 (file-local-copy
5100 (tramp-make-tramp-file-name
5101 method user host remote-copy)))
5102 ((stringp tramp-temp-buffer-file-name)
5103 (copy-file filename tramp-temp-buffer-file-name 'ok)
5104 tramp-temp-buffer-file-name)
5105 (t (file-local-copy filename)))))
5106
5107 ;; When the file is not readable for the owner, it
5108 ;; cannot be inserted, even it is redable for the group
5109 ;; or for everybody.
5110 (set-file-modes local-copy (tramp-compat-octal-to-decimal "0600"))
5111
5112 (when (and (null remote-copy)
5113 (tramp-get-method-parameter
5114 method 'tramp-copy-keep-tmpfile))
5115 ;; We keep the local file for performance reasons,
5116 ;; useful for "rsync".
5117 (setq tramp-temp-buffer-file-name local-copy)
5118 (put 'tramp-temp-buffer-file-name 'permanent-local t))
5119
5120 (with-progress-reporter
5121 v 3 (format "Inserting local temp file `%s'" local-copy)
5122 ;; We must ensure that `file-coding-system-alist'
5123 ;; matches `local-copy'.
5124 (let ((file-coding-system-alist
5125 (tramp-find-file-name-coding-system-alist
5126 filename local-copy)))
5127 (setq result
5128 (insert-file-contents
5129 local-copy nil nil nil replace))))))
5130
5131 ;; Save exit.
5132 (progn
5133 (when visit
5134 (setq buffer-file-name filename)
5135 (setq buffer-read-only (not (file-writable-p filename)))
5136 (set-visited-file-modtime)
5137 (set-buffer-modified-p nil))
5138 (when (and (stringp local-copy)
5139 (or remote-copy (null tramp-temp-buffer-file-name)))
5140 (delete-file local-copy))
5141 (when (stringp remote-copy)
5142 (delete-file
5143 (tramp-make-tramp-file-name method user host remote-copy))))))
5144
5145 ;; Result.
5146 (list (expand-file-name filename)
5147 (cadr result))))
5148
5149;; This is needed for XEmacs only. Code stolen from files.el.
5150(defun tramp-handle-insert-file-contents-literally
5151 (filename &optional visit beg end replace)
5152 "Like `insert-file-contents-literally' for Tramp files."
5153 (let ((format-alist nil)
5154 (after-insert-file-functions nil)
5155 (coding-system-for-read 'no-conversion)
5156 (coding-system-for-write 'no-conversion)
5157 (find-buffer-file-type-function
5158 (if (fboundp 'find-buffer-file-type)
5159 (symbol-function 'find-buffer-file-type)
5160 nil))
5161 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
5162 (inhibit-file-name-operation 'insert-file-contents))
5163 (unwind-protect
5164 (progn
5165 (fset 'find-buffer-file-type (lambda (filename) t))
5166 (insert-file-contents filename visit beg end replace))
5167 ;; Save exit.
5168 (if find-buffer-file-type-function
5169 (fset 'find-buffer-file-type find-buffer-file-type-function)
5170 (fmakunbound 'find-buffer-file-type)))))
5171
5172(defun tramp-handle-find-backup-file-name (filename)
5173 "Like `find-backup-file-name' for Tramp files."
5174 (with-parsed-tramp-file-name filename nil
5175 ;; We set both variables. It doesn't matter whether it is
5176 ;; Emacs or XEmacs.
5177 (let ((backup-directory-alist
5178 ;; Emacs case.
5179 (when (boundp 'backup-directory-alist)
5180 (if (symbol-value 'tramp-backup-directory-alist)
5181 (mapcar
5182 (lambda (x)
5183 (cons
5184 (car x)
5185 (if (and (stringp (cdr x))
5186 (file-name-absolute-p (cdr x))
5187 (not (tramp-file-name-p (cdr x))))
5188 (tramp-make-tramp-file-name method user host (cdr x))
5189 (cdr x))))
5190 (symbol-value 'tramp-backup-directory-alist))
5191 (symbol-value 'backup-directory-alist))))
5192
5193 (bkup-backup-directory-info
5194 ;; XEmacs case.
5195 (when (boundp 'bkup-backup-directory-info)
5196 (if (symbol-value 'tramp-bkup-backup-directory-info)
5197 (mapcar
5198 (lambda (x)
5199 (nconc
5200 (list (car x))
5201 (list
5202 (if (and (stringp (car (cdr x)))
5203 (file-name-absolute-p (car (cdr x)))
5204 (not (tramp-file-name-p (car (cdr x)))))
5205 (tramp-make-tramp-file-name
5206 method user host (car (cdr x)))
5207 (car (cdr x))))
5208 (cdr (cdr x))))
5209 (symbol-value 'tramp-bkup-backup-directory-info))
5210 (symbol-value 'bkup-backup-directory-info)))))
5211
5212 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
5213
5214(defun tramp-handle-make-auto-save-file-name ()
5215 "Like `make-auto-save-file-name' for Tramp files.
5216Returns a file name in `tramp-auto-save-directory' for autosaving this file."
5217 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5218 (buffer-file-name
5219 (tramp-subst-strs-in-string
5220 '(("_" . "|")
5221 ("/" . "_a")
5222 (":" . "_b")
5223 ("|" . "__")
5224 ("[" . "_l")
5225 ("]" . "_r"))
5226 (buffer-file-name))))
5227 ;; File name must be unique. This is ensured with Emacs 22 (see
5228 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5229 ;; all other cases we must do it ourselves.
5230 (when (boundp 'auto-save-file-name-transforms)
5231 (mapc
5232 (lambda (x)
5233 (when (and (string-match (car x) buffer-file-name)
5234 (not (car (cddr x))))
5235 (setq tramp-auto-save-directory
5236 (or tramp-auto-save-directory
5237 (tramp-compat-temporary-file-directory)))))
5238 (symbol-value 'auto-save-file-name-transforms)))
5239 ;; Create directory.
5240 (when tramp-auto-save-directory
5241 (setq buffer-file-name
5242 (expand-file-name buffer-file-name tramp-auto-save-directory))
5243 (unless (file-exists-p tramp-auto-save-directory)
5244 (make-directory tramp-auto-save-directory t)))
5245 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5246 ;; it is not a magic file name operation (since Emacs 22).
5247 ;; We must deactivate it temporarily.
5248 (if (not (ad-is-active 'make-auto-save-file-name))
5249 (tramp-run-real-handler 'make-auto-save-file-name nil)
5250 ;; else
5251 (ad-deactivate 'make-auto-save-file-name)
5252 (prog1
5253 (tramp-run-real-handler 'make-auto-save-file-name nil)
5254 (ad-activate 'make-auto-save-file-name)))))
5255
5256(defvar tramp-handle-write-region-hook nil
5257 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5258
5259;; CCC grok LOCKNAME
5260(defun tramp-handle-write-region
5261 (start end filename &optional append visit lockname confirm)
5262 "Like `write-region' for Tramp files."
5263 (setq filename (expand-file-name filename))
5264 (with-parsed-tramp-file-name filename nil
5265 ;; Following part commented out because we don't know what to do about
5266 ;; file locking, and it does not appear to be a problem to ignore it.
5267 ;; Ange-ftp ignores it, too.
5268 ;; (when (and lockname (stringp lockname))
5269 ;; (setq lockname (expand-file-name lockname)))
5270 ;; (unless (or (eq lockname nil)
5271 ;; (string= lockname filename))
5272 ;; (error
5273 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
5274
5275 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
5276 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5277 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5278 (tramp-error v 'file-error "File not overwritten")))
5279
5280 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
5281 (tramp-get-remote-uid v 'integer)))
5282 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
5283 (tramp-get-remote-gid v 'integer))))
5284
5285 (if (and (tramp-local-host-p v)
5286 ;; `file-writable-p' calls `file-expand-file-name'. We
5287 ;; cannot use `tramp-run-real-handler' therefore.
5288 (let (file-name-handler-alist)
5289 (and
5290 (file-writable-p (file-name-directory localname))
5291 (or (file-directory-p localname)
5292 (file-writable-p localname)))))
5293 ;; Short track: if we are on the local host, we can run directly.
5294 (tramp-run-real-handler
5295 'write-region
5296 (list start end localname append 'no-message lockname confirm))
5297
5298 (let ((modes (save-excursion (tramp-default-file-modes filename)))
5299 ;; We use this to save the value of
5300 ;; `last-coding-system-used' after writing the tmp
5301 ;; file. At the end of the function, we set
5302 ;; `last-coding-system-used' to this saved value. This
5303 ;; way, any intermediary coding systems used while
5304 ;; talking to the remote shell or suchlike won't hose
5305 ;; this variable. This approach was snarfed from
5306 ;; ange-ftp.el.
5307 coding-system-used
5308 ;; Write region into a tmp file. This isn't really
5309 ;; needed if we use an encoding function, but currently
5310 ;; we use it always because this makes the logic
5311 ;; simpler.
5312 (tmpfile (or tramp-temp-buffer-file-name
5313 (tramp-compat-make-temp-file filename))))
5314
5315 ;; If `append' is non-nil, we copy the file locally, and let
5316 ;; the native `write-region' implementation do the job.
5317 (when append (copy-file filename tmpfile 'ok))
5318
5319 ;; We say `no-message' here because we don't want the
5320 ;; visited file modtime data to be clobbered from the temp
5321 ;; file. We call `set-visited-file-modtime' ourselves later
5322 ;; on. We must ensure that `file-coding-system-alist'
5323 ;; matches `tmpfile'.
5324 (let (file-name-handler-alist
5325 (file-coding-system-alist
5326 (tramp-find-file-name-coding-system-alist filename tmpfile)))
5327 (condition-case err
5328 (tramp-run-real-handler
5329 'write-region
5330 (list start end tmpfile append 'no-message lockname confirm))
5331 ((error quit)
5332 (setq tramp-temp-buffer-file-name nil)
5333 (delete-file tmpfile)
5334 (signal (car err) (cdr err))))
5335
5336 ;; Now, `last-coding-system-used' has the right value. Remember it.
5337 (when (boundp 'last-coding-system-used)
5338 (setq coding-system-used
5339 (symbol-value 'last-coding-system-used))))
5340
5341 ;; The permissions of the temporary file should be set. If
5342 ;; filename does not exist (eq modes nil) it has been
5343 ;; renamed to the backup file. This case `save-buffer'
5344 ;; handles permissions.
5345 ;; Ensure, that it is still readable.
5346 (when modes
5347 (set-file-modes
5348 tmpfile
5349 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
5350
5351 ;; This is a bit lengthy due to the different methods
5352 ;; possible for file transfer. First, we check whether the
5353 ;; method uses an rcp program. If so, we call it.
5354 ;; Otherwise, both encoding and decoding command must be
5355 ;; specified. However, if the method _also_ specifies an
5356 ;; encoding function, then that is used for encoding the
5357 ;; contents of the tmp file.
5358 (let* ((size (nth 7 (file-attributes tmpfile)))
5359 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5360 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5361 (cond
5362 ;; `copy-file' handles direct copy and out-of-band methods.
5363 ((or (tramp-local-host-p v)
5364 (tramp-method-out-of-band-p v size))
5365 (if (and (not (stringp start))
5366 (= (or end (point-max)) (point-max))
5367 (= (or start (point-min)) (point-min))
5368 (tramp-get-method-parameter
5369 method 'tramp-copy-keep-tmpfile))
5370 (progn
5371 (setq tramp-temp-buffer-file-name tmpfile)
5372 (condition-case err
5373 ;; We keep the local file for performance
5374 ;; reasons, useful for "rsync".
5375 (copy-file tmpfile filename t)
5376 ((error quit)
5377 (setq tramp-temp-buffer-file-name nil)
5378 (delete-file tmpfile)
5379 (signal (car err) (cdr err)))))
5380 (setq tramp-temp-buffer-file-name nil)
5381 ;; Don't rename, in order to keep context in SELinux.
5382 (unwind-protect
5383 (copy-file tmpfile filename t)
5384 (delete-file tmpfile))))
5385
5386 ;; Use inline file transfer.
5387 (rem-dec
5388 ;; Encode tmpfile.
5389 (unwind-protect
5390 (with-temp-buffer
5391 (set-buffer-multibyte nil)
5392 ;; Use encoding function or command.
5393 (if (functionp loc-enc)
5394 (with-progress-reporter
5395 v 3 (format "Encoding region using function `%s'"
5396 loc-enc)
5397 (let ((coding-system-for-read 'binary))
5398 (insert-file-contents-literally tmpfile))
5399 ;; The following `let' is a workaround for the
5400 ;; base64.el that comes with pgnus-0.84. If
5401 ;; both of the following conditions are
5402 ;; satisfied, it tries to write to a local
5403 ;; file in default-directory, but at this
5404 ;; point, default-directory is remote.
5405 ;; (`call-process-region' can't write to
5406 ;; remote files, it seems.) The file in
5407 ;; question is a tmp file anyway.
5408 (let ((default-directory
5409 (tramp-compat-temporary-file-directory)))
5410 (funcall loc-enc (point-min) (point-max))))
5411
5412 (with-progress-reporter
5413 v 3 (format "Encoding region using command `%s'"
5414 loc-enc)
5415 (unless (zerop (tramp-call-local-coding-command
5416 loc-enc tmpfile t))
5417 (tramp-error
5418 v 'file-error
5419 (concat "Cannot write to `%s', "
5420 "local encoding command `%s' failed")
5421 filename loc-enc))))
5422
5423 ;; Send buffer into remote decoding command which
5424 ;; writes to remote file. Because this happens on
5425 ;; the remote host, we cannot use the function.
5426 (with-progress-reporter
5427 v 3
5428 (format "Decoding region into remote file %s" filename)
5429 (goto-char (point-max))
5430 (unless (bolp) (newline))
5431 (tramp-send-command
5432 v
5433 (format
5434 (concat rem-dec " <<'EOF'\n%sEOF")
5435 (tramp-shell-quote-argument localname)
5436 (buffer-string)))
5437 (tramp-barf-unless-okay
5438 v nil
5439 "Couldn't write region to `%s', decode using `%s' failed"
5440 filename rem-dec)
5441 ;; When `file-precious-flag' is set, the region is
5442 ;; written to a temporary file. Check that the
5443 ;; checksum is equal to that from the local tmpfile.
5444 (when file-precious-flag
5445 (erase-buffer)
5446 (and
5447 ;; cksum runs locally, if possible.
5448 (zerop (tramp-compat-call-process "cksum" tmpfile t))
5449 ;; cksum runs remotely.
5450 (zerop
5451 (tramp-send-command-and-check
5452 v
5453 (format
5454 "cksum <%s"
5455 (tramp-shell-quote-argument localname))))
5456 ;; ... they are different.
5457 (not
5458 (string-equal
5459 (buffer-string)
5460 (with-current-buffer (tramp-get-buffer v)
5461 (buffer-string))))
5462 (tramp-error
5463 v 'file-error
5464 (concat "Couldn't write region to `%s',"
5465 " decode using `%s' failed")
5466 filename rem-dec)))))
5467
5468 ;; Save exit.
5469 (delete-file tmpfile)))
5470
5471 ;; That's not expected.
5472 (t
5473 (tramp-error
5474 v 'file-error
5475 (concat "Method `%s' should specify both encoding and "
5476 "decoding command or an rcp program")
5477 method))))
5478
5479 ;; Make `last-coding-system-used' have the right value.
5480 (when coding-system-used
5481 (set 'last-coding-system-used coding-system-used))))
5482
5483 (tramp-flush-file-property v (file-name-directory localname))
5484 (tramp-flush-file-property v localname)
5485
5486 ;; We must protect `last-coding-system-used', now we have set it
5487 ;; to its correct value.
5488 (let (last-coding-system-used (need-chown t))
5489 ;; Set file modification time.
5490 (when (or (eq visit t) (stringp visit))
5491 (let ((file-attr (file-attributes filename)))
5492 (set-visited-file-modtime
5493 ;; We must pass modtime explicitely, because filename can
5494 ;; be different from (buffer-file-name), f.e. if
5495 ;; `file-precious-flag' is set.
5496 (nth 5 file-attr))
5497 (when (and (eq (nth 2 file-attr) uid)
5498 (eq (nth 3 file-attr) gid))
5499 (setq need-chown nil))))
5500
5501 ;; Set the ownership.
5502 (when need-chown
5503 (tramp-set-file-uid-gid filename uid gid))
5504 (when (or (eq visit t) (null visit) (stringp visit))
5505 (tramp-message v 0 "Wrote %s" filename))
5506 (run-hooks 'tramp-handle-write-region-hook)))))
5507
5508(defvar tramp-vc-registered-file-names nil
5509 "List used to collect file names, which are checked during `vc-registered'.")
5510
5511;; VC backends check for the existence of various different special
5512;; files. This is very time consuming, because every single check
5513;; requires a remote command (the file cache must be invalidated).
5514;; Therefore, we apply a kind of optimization. We install the file
5515;; name handler `tramp-vc-file-name-handler', which does nothing but
5516;; remembers all file names for which `file-exists-p' or
5517;; `file-readable-p' has been applied. A first run of `vc-registered'
5518;; is performed. Afterwards, a script is applied for all collected
5519;; file names, using just one remote command. The result of this
5520;; script is used to fill the file cache with actual values. Now we
5521;; can reset the file name handlers, and we make a second run of
5522;; `vc-registered', which returns the expected result without sending
5523;; any other remote command.
5524(defun tramp-handle-vc-registered (file)
5525 "Like `vc-registered' for Tramp files."
5526 (with-temp-message ""
5527 (with-parsed-tramp-file-name file nil
5528 (with-progress-reporter
5529 v 3 (format "Checking `vc-registered' for %s" file)
5530
5531 ;; There could be new files, created by the vc backend. We
5532 ;; cannot reuse the old cache entries, therefore.
5533 (let (tramp-vc-registered-file-names
5534 (tramp-cache-inhibit-cache (current-time))
5535 (file-name-handler-alist
5536 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5537
5538 ;; Here we collect only file names, which need an operation.
5539 (tramp-run-real-handler 'vc-registered (list file))
5540 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5541
5542 ;; Send just one command, in order to fill the cache.
5543 (when tramp-vc-registered-file-names
5544 (tramp-maybe-send-script
5545 v
5546 (format tramp-vc-registered-read-file-names
5547 (tramp-get-file-exists-command v)
5548 (format "%s -r" (tramp-get-test-command v)))
5549 "tramp_vc_registered_read_file_names")
5550
5551 (dolist
5552 (elt
5553 (tramp-send-command-and-read
5554 v
5555 (format
5556 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
5557 (mapconcat 'tramp-shell-quote-argument
5558 tramp-vc-registered-file-names
5559 "\n"))))
5560
5561 (tramp-set-file-property
5562 v (car elt) (cadr elt) (cadr (cdr elt))))))
5563
5564 ;; Second run. Now all `file-exists-p' or `file-readable-p'
5565 ;; calls shall be answered from the file cache. We unset
5566 ;; `process-file-side-effects' in order to keep the cache when
5567 ;; `process-file' calls appear.
5568 (let (process-file-side-effects)
5569 (tramp-run-real-handler 'vc-registered (list file)))))))
5570
5571;;;###autoload 1677;;;###autoload
5572(progn (defun tramp-run-real-handler (operation args) 1678(progn (defun tramp-run-real-handler (operation args)
5573 "Invoke normal file name handler for OPERATION. 1679 "Invoke normal file name handler for OPERATION.
@@ -5728,8 +1834,7 @@ Falls back to normal file name handler if no Tramp file name handler exists."
5728 (condition-case err 1834 (condition-case err
5729 (apply foreign operation args) 1835 (apply foreign operation args)
5730 1836
5731 ;; Trace that somebody has interrupted the 1837 ;; Trace, that somebody has interrupted the operation.
5732 ;; operation.
5733 (quit 1838 (quit
5734 (let (tramp-message-show-message) 1839 (let (tramp-message-show-message)
5735 (tramp-message 1840 (tramp-message
@@ -5787,48 +1892,6 @@ preventing reentrant calls of Tramp.")
5787Together with `tramp-locked', this implements a locking mechanism 1892Together with `tramp-locked', this implements a locking mechanism
5788preventing reentrant calls of Tramp.") 1893preventing reentrant calls of Tramp.")
5789 1894
5790(defun tramp-sh-file-name-handler (operation &rest args)
5791 "Invoke remote-shell Tramp file name handler.
5792Fall back to normal file name handler if no Tramp handler exists."
5793 (when (and tramp-locked (not tramp-locker))
5794 (setq tramp-locked nil)
5795 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
5796 (let ((tl tramp-locked))
5797 (unwind-protect
5798 (progn
5799 (setq tramp-locked t)
5800 (let ((tramp-locker t))
5801 (save-match-data
5802 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5803 (if fn
5804 (apply (cdr fn) args)
5805 (tramp-run-real-handler operation args))))))
5806 (setq tramp-locked tl))))
5807
5808(defun tramp-vc-file-name-handler (operation &rest args)
5809 "Invoke special file name handler, which collects files to be handled."
5810 (save-match-data
5811 (let ((filename
5812 (tramp-replace-environment-variables
5813 (apply 'tramp-file-name-for-operation operation args)))
5814 (fn (assoc operation tramp-file-name-handler-alist)))
5815 (with-parsed-tramp-file-name filename nil
5816 (cond
5817 ;; That's what we want: file names, for which checks are
5818 ;; applied. We assume, that VC uses only `file-exists-p' and
5819 ;; `file-readable-p' checks; otherwise we must extend the
5820 ;; list. We do not perform any action, but return nil, in
5821 ;; order to keep `vc-registered' running.
5822 ((and fn (memq operation '(file-exists-p file-readable-p)))
5823 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5824 nil)
5825 ;; Tramp file name handlers like `expand-file-name'. They
5826 ;; must still work.
5827 (fn
5828 (save-match-data (apply (cdr fn) args)))
5829 ;; Default file name handlers, we don't care.
5830 (t (tramp-run-real-handler operation args)))))))
5831
5832;;;###autoload 1895;;;###autoload
5833(progn (defun tramp-completion-file-name-handler (operation &rest args) 1896(progn (defun tramp-completion-file-name-handler (operation &rest args)
5834 "Invoke Tramp file name completion handler. 1897 "Invoke Tramp file name completion handler.
@@ -6027,12 +2090,11 @@ not in completion mode."
6027 ;; Complete local parts. 2090 ;; Complete local parts.
6028 (append 2091 (append
6029 result1 2092 result1
6030 (condition-case nil 2093 (ignore-errors
6031 (apply (if (tramp-connectable-p fullname) 2094 (apply (if (tramp-connectable-p fullname)
6032 'tramp-completion-run-real-handler 2095 'tramp-completion-run-real-handler
6033 'tramp-run-real-handler) 2096 'tramp-run-real-handler)
6034 'file-name-all-completions (list (list filename directory))) 2097 'file-name-all-completions (list (list filename directory)))))))
6035 (error nil)))))
6036 2098
6037;; Method, host name and user name completion for a file. 2099;; Method, host name and user name completion for a file.
6038;;;###autoload 2100;;;###autoload
@@ -6490,69 +2552,10 @@ User is always nil."
6490 (forward-line 1) 2552 (forward-line 1)
6491 result)) 2553 result))
6492 2554
6493;;; Internal Functions:
6494
6495(defun tramp-maybe-send-script (vec script name)
6496 "Define in remote shell function NAME implemented as SCRIPT.
6497Only send the definition if it has not already been done."
6498 (let* ((p (tramp-get-connection-process vec))
6499 (scripts (tramp-get-connection-property p "scripts" nil)))
6500 (unless (member name scripts)
6501 (with-progress-reporter vec 5 (format "Sending script `%s'" name)
6502 ;; The script could contain a call of Perl. This is masked with `%s'.
6503 (tramp-send-command-and-check
6504 vec
6505 (format "%s () {\n%s\n}" name
6506 (format script (tramp-get-remote-perl vec))))
6507 (tramp-set-connection-property p "scripts" (cons name scripts))))))
6508
6509(defun tramp-set-auto-save ()
6510 (when (and ;; ange-ftp has its own auto-save mechanism
6511 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6512 'tramp-sh-file-name-handler)
6513 auto-save-default)
6514 (auto-save-mode 1)))
6515(add-hook 'find-file-hooks 'tramp-set-auto-save t)
6516(add-hook 'tramp-unload-hook
6517 (lambda ()
6518 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
6519
6520(defun tramp-run-test (switch filename)
6521 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6522Returns the exit code of the `test' program."
6523 (with-parsed-tramp-file-name filename nil
6524 (tramp-send-command-and-check
6525 v
6526 (format
6527 "%s %s %s"
6528 (tramp-get-test-command v)
6529 switch
6530 (tramp-shell-quote-argument localname)))))
6531
6532(defun tramp-run-test2 (format-string file1 file2)
6533 "Run `test'-like program on the remote system, given FILE1, FILE2.
6534FORMAT-STRING contains the program name, switches, and place holders.
6535Returns the exit code of the `test' program. Barfs if the methods,
6536hosts, or files, disagree."
6537 (unless (tramp-equal-remote file1 file2)
6538 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6539 (tramp-error
6540 v 'file-error
6541 "tramp-run-test2 only implemented for same method, user, host")))
6542 (with-parsed-tramp-file-name file1 v1
6543 (with-parsed-tramp-file-name file1 v2
6544 (tramp-send-command-and-check
6545 v1
6546 (format format-string
6547 (tramp-shell-quote-argument v1-localname)
6548 (tramp-shell-quote-argument v2-localname))))))
6549
6550(defun tramp-delete-temp-file-function () 2555(defun tramp-delete-temp-file-function ()
6551 "Remove temporary files related to current buffer." 2556 "Remove temporary files related to current buffer."
6552 (when (stringp tramp-temp-buffer-file-name) 2557 (when (stringp tramp-temp-buffer-file-name)
6553 (condition-case nil 2558 (ignore-errors (delete-file tramp-temp-buffer-file-name))))
6554 (delete-file tramp-temp-buffer-file-name)
6555 (error nil))))
6556 2559
6557(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function) 2560(add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6558(add-hook 'tramp-cache-unload-hook 2561(add-hook 'tramp-cache-unload-hook
@@ -6560,173 +2563,6 @@ hosts, or files, disagree."
6560 (remove-hook 'kill-buffer-hook 2563 (remove-hook 'kill-buffer-hook
6561 'tramp-delete-temp-file-function))) 2564 'tramp-delete-temp-file-function)))
6562 2565
6563(defun tramp-find-executable
6564 (vec progname dirlist &optional ignore-tilde ignore-path)
6565 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6566First arg VEC specifies the connection, PROGNAME is the program
6567to search for, and DIRLIST gives the list of directories to
6568search. If IGNORE-TILDE is non-nil, directory names starting
6569with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6570only in DIRLIST.
6571
6572Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6573
6574This function expects to be in the right *tramp* buffer."
6575 (with-current-buffer (tramp-get-connection-buffer vec)
6576 (let (result)
6577 ;; Check whether the executable is in $PATH. "which(1)" does not
6578 ;; report always a correct error code; therefore we check the
6579 ;; number of words it returns.
6580 (unless ignore-path
6581 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6582 (goto-char (point-min))
6583 (if (looking-at "^\\s-*1$")
6584 (setq result (concat "\\" progname))))
6585 (unless result
6586 (when ignore-tilde
6587 ;; Remove all ~/foo directories from dirlist. In XEmacs,
6588 ;; `remove' is in CL, and we want to avoid CL dependencies.
6589 (let (newdl d)
6590 (while dirlist
6591 (setq d (car dirlist))
6592 (setq dirlist (cdr dirlist))
6593 (unless (char-equal ?~ (aref d 0))
6594 (setq newdl (cons d newdl))))
6595 (setq dirlist (nreverse newdl))))
6596 (tramp-send-command
6597 vec
6598 (format (concat "while read d; "
6599 "do if test -x $d/%s -a -f $d/%s; "
6600 "then echo tramp_executable $d/%s; "
6601 "break; fi; done <<'EOF'\n"
6602 "%s\nEOF")
6603 progname progname progname (mapconcat 'identity dirlist "\n")))
6604 (goto-char (point-max))
6605 (when (search-backward "tramp_executable " nil t)
6606 (skip-chars-forward "^ ")
6607 (skip-chars-forward " ")
6608 (setq result (buffer-substring
6609 (point) (tramp-compat-line-end-position)))))
6610 result)))
6611
6612(defun tramp-set-remote-path (vec)
6613 "Sets the remote environment PATH to existing directories.
6614I.e., for each directory in `tramp-remote-path', it is tested
6615whether it exists and if so, it is added to the environment
6616variable PATH."
6617 (tramp-message vec 5 (format "Setting $PATH environment variable"))
6618 (tramp-send-command
6619 vec (format "PATH=%s; export PATH"
6620 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
6621
6622;; ------------------------------------------------------------
6623;; -- Communication with external shell --
6624;; ------------------------------------------------------------
6625
6626(defun tramp-find-file-exists-command (vec)
6627 "Find a command on the remote host for checking if a file exists.
6628Here, we are looking for a command which has zero exit status if the
6629file exists and nonzero exit status otherwise."
6630 (let ((existing "/")
6631 (nonexisting
6632 (tramp-shell-quote-argument "/ this file does not exist "))
6633 result)
6634 ;; The algorithm is as follows: we try a list of several commands.
6635 ;; For each command, we first run `$cmd /' -- this should return
6636 ;; true, as the root directory always exists. And then we run
6637 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6638 ;; does not exist. This should return false. We use the first
6639 ;; command we find that seems to work.
6640 ;; The list of commands to try is as follows:
6641 ;; `ls -d' This works on most systems, but NetBSD 1.4
6642 ;; has a bug: `ls' always returns zero exit
6643 ;; status, even for files which don't exist.
6644 ;; `test -e' Some Bourne shells have a `test' builtin
6645 ;; which does not know the `-e' option.
6646 ;; `/bin/test -e' For those, the `test' binary on disk normally
6647 ;; provides the option. Alas, the binary
6648 ;; is sometimes `/bin/test' and sometimes it's
6649 ;; `/usr/bin/test'.
6650 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6651 (unless (or
6652 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6653 (zerop (tramp-send-command-and-check
6654 vec (format "%s %s" result existing)))
6655 (not (zerop (tramp-send-command-and-check
6656 vec (format "%s %s" result nonexisting)))))
6657 (and (setq result "/bin/test -e")
6658 (zerop (tramp-send-command-and-check
6659 vec (format "%s %s" result existing)))
6660 (not (zerop (tramp-send-command-and-check
6661 vec (format "%s %s" result nonexisting)))))
6662 (and (setq result "/usr/bin/test -e")
6663 (zerop (tramp-send-command-and-check
6664 vec (format "%s %s" result existing)))
6665 (not (zerop (tramp-send-command-and-check
6666 vec (format "%s %s" result nonexisting)))))
6667 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6668 (zerop (tramp-send-command-and-check
6669 vec (format "%s %s" result existing)))
6670 (not (zerop (tramp-send-command-and-check
6671 vec (format "%s %s" result nonexisting))))))
6672 (tramp-error
6673 vec 'file-error "Couldn't find command to check if file exists"))
6674 result))
6675
6676(defun tramp-open-shell (vec shell)
6677 "Opens shell SHELL."
6678 (with-progress-reporter vec 5 (format "Opening remote shell `%s'" shell)
6679 ;; Find arguments for this shell.
6680 (let ((tramp-end-of-output tramp-initial-end-of-output)
6681 (alist tramp-sh-extra-args)
6682 item extra-args)
6683 (while (and alist (null extra-args))
6684 (setq item (pop alist))
6685 (when (string-match (car item) shell)
6686 (setq extra-args (cdr item))))
6687 (when extra-args (setq shell (concat shell " " extra-args)))
6688 (tramp-send-command
6689 vec (format "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6690 (shell-quote-argument tramp-end-of-output) shell)
6691 t))
6692 ;; Setting prompts.
6693 (tramp-send-command
6694 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6695 (tramp-send-command vec "PS2=''" t)
6696 (tramp-send-command vec "PS3=''" t)
6697 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
6698
6699(defun tramp-find-shell (vec)
6700 "Opens a shell on the remote host which groks tilde expansion."
6701 (unless (tramp-get-connection-property vec "remote-shell" nil)
6702 (let (shell)
6703 (with-current-buffer (tramp-get-buffer vec)
6704 (tramp-send-command vec "echo ~root" t)
6705 (cond
6706 ((or (string-match "^~root$" (buffer-string))
6707 ;; The default shell (ksh93) of OpenSolaris is buggy.
6708 (string-equal (tramp-get-connection-property vec "uname" "")
6709 "SunOS 5.11"))
6710 (setq shell
6711 (or (tramp-find-executable
6712 vec "bash" (tramp-get-remote-path vec) t t)
6713 (tramp-find-executable
6714 vec "ksh" (tramp-get-remote-path vec) t t)))
6715 (unless shell
6716 (tramp-error
6717 vec 'file-error
6718 "Couldn't find a shell which groks tilde expansion"))
6719 (tramp-message
6720 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
6721 (tramp-open-shell vec shell))
6722
6723 (t (tramp-message
6724 vec 5 "Remote `%s' groks tilde expansion, good"
6725 (tramp-set-connection-property
6726 vec "remote-shell"
6727 (tramp-get-method-parameter
6728 (tramp-file-name-method vec) 'tramp-remote-sh)))))))))
6729
6730;; ------------------------------------------------------------ 2566;; ------------------------------------------------------------
6731;; -- Functions for establishing connection -- 2567;; -- Functions for establishing connection --
6732;; ------------------------------------------------------------ 2568;; ------------------------------------------------------------
@@ -6960,17 +2796,6 @@ nil."
6960 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp))) 2796 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6961 found))) 2797 found)))
6962 2798
6963(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6964 "Wait for shell prompt and barf if none appears.
6965Looks at process PROC to see if a shell prompt appears in TIMEOUT
6966seconds. If not, it produces an error message with the given ERROR-ARGS."
6967 (unless
6968 (tramp-wait-for-regexp
6969 proc timeout
6970 (format
6971 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
6972 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6973
6974;; We don't call `tramp-send-string' in order to hide the password 2799;; We don't call `tramp-send-string' in order to hide the password
6975;; from the debug buffer, and because end-of-line handling of the 2800;; from the debug buffer, and because end-of-line handling of the
6976;; string. 2801;; string.
@@ -6983,819 +2808,6 @@ seconds. If not, it produces an error message with the given ERROR-ARGS."
6983 'tramp-password-end-of-line) 2808 'tramp-password-end-of-line)
6984 tramp-default-password-end-of-line)))) 2809 tramp-default-password-end-of-line))))
6985 2810
6986(defun tramp-open-connection-setup-interactive-shell (proc vec)
6987 "Set up an interactive shell.
6988Mainly sets the prompt and the echo correctly. PROC is the shell
6989process to set up. VEC specifies the connection."
6990 (let ((tramp-end-of-output tramp-initial-end-of-output))
6991 ;; It is useful to set the prompt in the following command because
6992 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6993 ;; about and thus /bin/sh will display a strange prompt. For
6994 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6995 ;; display the current working directory but /bin/sh will display
6996 ;; a dollar sign. The following command line sets $PS1 to a sane
6997 ;; value, and works under Bourne-ish shells as well as csh-like
6998 ;; shells. Daniel Pittman reports that the unusual positioning of
6999 ;; the single quotes makes it work under `rc', too. We also unset
7000 ;; the variable $ENV because that is read by some sh
7001 ;; implementations (eg, bash when called as sh) on startup; this
7002 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
7003 ;; is another way to set the prompt in /bin/bash, it must be
7004 ;; discarded as well.
7005 (tramp-open-shell
7006 vec
7007 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-remote-sh))
7008
7009 ;; Disable echo.
7010 (tramp-message vec 5 "Setting up remote shell environment")
7011 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
7012 ;; Check whether the echo has really been disabled. Some
7013 ;; implementations, like busybox of embedded GNU/Linux, don't
7014 ;; support disabling.
7015 (tramp-send-command vec "echo foo" t)
7016 (with-current-buffer (process-buffer proc)
7017 (goto-char (point-min))
7018 (when (looking-at "echo foo")
7019 (tramp-set-connection-property proc "remote-echo" t)
7020 (tramp-message vec 5 "Remote echo still on. Ok.")
7021 ;; Make sure backspaces and their echo are enabled and no line
7022 ;; width magic interferes with them.
7023 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
7024
7025 (tramp-message vec 5 "Setting shell prompt")
7026 (tramp-send-command
7027 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
7028 (tramp-send-command vec "PS2=''" t)
7029 (tramp-send-command vec "PS3=''" t)
7030 (tramp-send-command vec "PROMPT_COMMAND=''" t)
7031
7032 ;; Try to set up the coding system correctly.
7033 ;; CCC this can't be the right way to do it. Hm.
7034 (tramp-message vec 5 "Determining coding system")
7035 (tramp-send-command vec "echo foo ; echo bar" t)
7036 (with-current-buffer (process-buffer proc)
7037 (goto-char (point-min))
7038 (if (featurep 'mule)
7039 ;; Use MULE to select the right EOL convention for communicating
7040 ;; with the process.
7041 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
7042 (cons 'undecided 'undecided)))
7043 cs-decode cs-encode)
7044 (when (symbolp cs) (setq cs (cons cs cs)))
7045 (setq cs-decode (car cs))
7046 (setq cs-encode (cdr cs))
7047 (unless cs-decode (setq cs-decode 'undecided))
7048 (unless cs-encode (setq cs-encode 'undecided))
7049 (setq cs-encode (tramp-coding-system-change-eol-conversion
7050 cs-encode 'unix))
7051 (when (search-forward "\r" nil t)
7052 (setq cs-decode (tramp-coding-system-change-eol-conversion
7053 cs-decode 'dos)))
7054 (tramp-compat-funcall
7055 'set-buffer-process-coding-system cs-decode cs-encode)
7056 (tramp-message
7057 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
7058 ;; Look for ^M and do something useful if found.
7059 (when (search-forward "\r" nil t)
7060 ;; We have found a ^M but cannot frob the process coding system
7061 ;; because we're running on a non-MULE Emacs. Let's try
7062 ;; stty, instead.
7063 (tramp-send-command vec "stty -onlcr" t))))
7064 ;; Dump stty settings in the traces.
7065 (when (>= tramp-verbose 9)
7066 (tramp-send-command vec "stty -a" t))
7067 (tramp-send-command vec "set +o vi +o emacs" t)
7068
7069 ;; Check whether the output of "uname -sr" has been changed. If
7070 ;; yes, this is a strong indication that we must expire all
7071 ;; connection properties. We start again with
7072 ;; `tramp-maybe-open-connection', it will be catched there.
7073 (tramp-message vec 5 "Checking system information")
7074 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
7075 (new-uname
7076 (tramp-set-connection-property
7077 vec "uname"
7078 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
7079 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
7080 (with-current-buffer (tramp-get-debug-buffer vec)
7081 ;; Keep the debug buffer.
7082 (rename-buffer
7083 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
7084 (tramp-compat-funcall 'tramp-cleanup-connection vec)
7085 (if (= (point-min) (point-max))
7086 (kill-buffer nil)
7087 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
7088 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
7089 (tramp-get-buffer vec)
7090 (tramp-message
7091 vec 3
7092 "Connection reset, because remote host changed from `%s' to `%s'"
7093 old-uname new-uname)
7094 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
7095
7096 ;; Check whether the remote host suffers from buggy
7097 ;; `send-process-string'. This is known for FreeBSD (see comment in
7098 ;; `send_process', file process.c). I've tested sending 624 bytes
7099 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
7100 ;; this host type is detected locally. It cannot handle remote
7101 ;; hosts, though.
7102 (with-connection-property proc "chunksize"
7103 (cond
7104 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
7105 tramp-chunksize)
7106 (t
7107 (tramp-message
7108 vec 5 "Checking remote host type for `send-process-string' bug")
7109 (if (string-match
7110 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
7111 500 0))))
7112
7113 ;; Set remote PATH variable.
7114 (tramp-set-remote-path vec)
7115
7116 ;; Search for a good shell before searching for a command which
7117 ;; checks if a file exists. This is done because Tramp wants to use
7118 ;; "test foo; echo $?" to check if various conditions hold, and
7119 ;; there are buggy /bin/sh implementations which don't execute the
7120 ;; "echo $?" part if the "test" part has an error. In particular,
7121 ;; the OpenSolaris /bin/sh is a problem. There are also other
7122 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
7123 ;; in function declarations, or changing HISTFILE in place.
7124 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
7125 ;; detected.
7126 (tramp-find-shell vec)
7127
7128 ;; Disable unexpected output.
7129 (tramp-send-command vec "mesg n; biff n" t)
7130
7131 ;; IRIX64 bash expands "!" even when in single quotes. This
7132 ;; destroys our shell functions, we must disable it. See
7133 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
7134 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
7135 (tramp-send-command vec "set +H" t))
7136
7137 ;; Set `remote-tty' process property.
7138 (ignore-errors
7139 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"")))
7140 (unless (zerop (length tty)) (process-put proc 'remote-tty tty))))
7141
7142 ;; Set the environment.
7143 (tramp-message vec 5 "Setting default environment")
7144
7145 (let ((env (copy-sequence tramp-remote-process-environment))
7146 unset item)
7147 (while env
7148 (setq item (tramp-compat-split-string (car env) "="))
7149 (setcdr item (mapconcat 'identity (cdr item) "="))
7150 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
7151 (tramp-send-command
7152 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
7153 (push (car item) unset))
7154 (setq env (cdr env)))
7155 (when unset
7156 (tramp-send-command
7157 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
7158
7159;; CCC: We should either implement a Perl version of base64 encoding
7160;; and decoding. Then we just use that in the last item. The other
7161;; alternative is to use the Perl version of UU encoding. But then
7162;; we need a Lisp version of uuencode.
7163;;
7164;; Old text from documentation of tramp-methods:
7165;; Using a uuencode/uudecode inline method is discouraged, please use one
7166;; of the base64 methods instead since base64 encoding is much more
7167;; reliable and the commands are more standardized between the different
7168;; Unix versions. But if you can't use base64 for some reason, please
7169;; note that the default uudecode command does not work well for some
7170;; Unices, in particular AIX and Irix. For AIX, you might want to use
7171;; the following command for uudecode:
7172;;
7173;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7174;;
7175;; For Irix, no solution is known yet.
7176
7177(defconst tramp-local-coding-commands
7178 '((b64 base64-encode-region base64-decode-region)
7179 (uu tramp-uuencode-region uudecode-decode-region)
7180 (pack
7181 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7182 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7183 "List of local coding commands for inline transfer.
7184Each item is a list that looks like this:
7185
7186\(FORMAT ENCODING DECODING\)
7187
7188FORMAT is symbol describing the encoding/decoding format. It can be
7189`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7190
7191ENCODING and DECODING can be strings, giving commands, or symbols,
7192giving functions. If they are strings, then they can contain
7193the \"%s\" format specifier. If that specifier is present, the input
7194filename will be put into the command line at that spot. If the
7195specifier is not present, the input should be read from standard
7196input.
7197
7198If they are functions, they will be called with two arguments, start
7199and end of region, and are expected to replace the region contents
7200with the encoded or decoded results, respectively.")
7201
7202(defconst tramp-remote-coding-commands
7203 '((b64 "base64" "base64 -d")
7204 (b64 "mimencode -b" "mimencode -u -b")
7205 (b64 "mmencode -b" "mmencode -u -b")
7206 (b64 "recode data..base64" "recode base64..data")
7207 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7208 (b64 tramp-perl-encode tramp-perl-decode)
7209 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7210 (uu "uuencode xxx" "uudecode -o -")
7211 (uu "uuencode xxx" "uudecode -p")
7212 (uu "uuencode xxx" tramp-uudecode)
7213 (pack
7214 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7215 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7216 "List of remote coding commands for inline transfer.
7217Each item is a list that looks like this:
7218
7219\(FORMAT ENCODING DECODING\)
7220
7221FORMAT is symbol describing the encoding/decoding format. It can be
7222`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7223
7224ENCODING and DECODING can be strings, giving commands, or symbols,
7225giving variables. If they are strings, then they can contain
7226the \"%s\" format specifier. If that specifier is present, the input
7227filename will be put into the command line at that spot. If the
7228specifier is not present, the input should be read from standard
7229input.
7230
7231If they are variables, this variable is a string containing a Perl
7232implementation for this functionality. This Perl program will be transferred
7233to the remote host, and it is available as shell function with the same name.")
7234
7235(defun tramp-find-inline-encoding (vec)
7236 "Find an inline transfer encoding that works.
7237Goes through the list `tramp-local-coding-commands' and
7238`tramp-remote-coding-commands'."
7239 (save-excursion
7240 (let ((local-commands tramp-local-coding-commands)
7241 (magic "xyzzy")
7242 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7243 (while (and local-commands (not found))
7244 (setq litem (pop local-commands))
7245 (catch 'wont-work-local
7246 (let ((format (nth 0 litem))
7247 (remote-commands tramp-remote-coding-commands))
7248 (setq loc-enc (nth 1 litem))
7249 (setq loc-dec (nth 2 litem))
7250 ;; If the local encoder or decoder is a string, the
7251 ;; corresponding command has to work locally.
7252 (if (not (stringp loc-enc))
7253 (tramp-message
7254 vec 5 "Checking local encoding function `%s'" loc-enc)
7255 (tramp-message
7256 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7257 (unless (zerop (tramp-call-local-coding-command
7258 loc-enc nil nil))
7259 (throw 'wont-work-local nil)))
7260 (if (not (stringp loc-dec))
7261 (tramp-message
7262 vec 5 "Checking local decoding function `%s'" loc-dec)
7263 (tramp-message
7264 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7265 (unless (zerop (tramp-call-local-coding-command
7266 loc-dec nil nil))
7267 (throw 'wont-work-local nil)))
7268 ;; Search for remote coding commands with the same format
7269 (while (and remote-commands (not found))
7270 (setq ritem (pop remote-commands))
7271 (catch 'wont-work-remote
7272 (when (equal format (nth 0 ritem))
7273 (setq rem-enc (nth 1 ritem))
7274 (setq rem-dec (nth 2 ritem))
7275 ;; Check if remote encoding and decoding commands can be
7276 ;; called remotely with null input and output. This makes
7277 ;; sure there are no syntax errors and the command is really
7278 ;; found. Note that we do not redirect stdout to /dev/null,
7279 ;; for two reasons: when checking the decoding command, we
7280 ;; actually check the output it gives. And also, when
7281 ;; redirecting "mimencode" output to /dev/null, then as root
7282 ;; it might change the permissions of /dev/null!
7283 (when (not (stringp rem-enc))
7284 (let ((name (symbol-name rem-enc)))
7285 (while (string-match (regexp-quote "-") name)
7286 (setq name (replace-match "_" nil t name)))
7287 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7288 (setq rem-enc name)))
7289 (tramp-message
7290 vec 5
7291 "Checking remote encoding command `%s' for sanity" rem-enc)
7292 (unless (zerop (tramp-send-command-and-check
7293 vec (format "%s </dev/null" rem-enc) t))
7294 (throw 'wont-work-remote nil))
7295
7296 (when (not (stringp rem-dec))
7297 (let ((name (symbol-name rem-dec)))
7298 (while (string-match (regexp-quote "-") name)
7299 (setq name (replace-match "_" nil t name)))
7300 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7301 (setq rem-dec name)))
7302 (tramp-message
7303 vec 5
7304 "Checking remote decoding command `%s' for sanity" rem-dec)
7305 (unless (zerop (tramp-send-command-and-check
7306 vec
7307 (format "echo %s | %s | %s"
7308 magic rem-enc rem-dec)
7309 t))
7310 (throw 'wont-work-remote nil))
7311
7312 (with-current-buffer (tramp-get-buffer vec)
7313 (goto-char (point-min))
7314 (unless (looking-at (regexp-quote magic))
7315 (throw 'wont-work-remote nil)))
7316
7317 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7318 (setq rem-enc (nth 1 ritem))
7319 (setq rem-dec (nth 2 ritem))
7320 (setq found t)))))))
7321
7322 ;; Did we find something?
7323 (unless found
7324 (tramp-error
7325 vec 'file-error "Couldn't find an inline transfer encoding"))
7326
7327 ;; Set connection properties.
7328 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7329 (tramp-set-connection-property vec "local-encoding" loc-enc)
7330 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7331 (tramp-set-connection-property vec "local-decoding" loc-dec)
7332 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7333 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7334 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7335 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
7336
7337(defun tramp-call-local-coding-command (cmd input output)
7338 "Call the local encoding or decoding command.
7339If CMD contains \"%s\", provide input file INPUT there in command.
7340Otherwise, INPUT is passed via standard input.
7341INPUT can also be nil which means `/dev/null'.
7342OUTPUT can be a string (which specifies a filename), or t (which
7343means standard output and thus the current buffer), or nil (which
7344means discard it)."
7345 (tramp-compat-call-process
7346 tramp-encoding-shell
7347 (when (and input (not (string-match "%s" cmd))) input)
7348 (if (eq output t) t nil)
7349 nil
7350 tramp-encoding-command-switch
7351 (concat
7352 (if (string-match "%s" cmd) (format cmd input) cmd)
7353 (if (stringp output) (concat "> " output) ""))))
7354
7355(defconst tramp-inline-compress-commands
7356 '(("gzip" "gzip -d")
7357 ("bzip2" "bzip2 -d")
7358 ("compress" "compress -d"))
7359 "List of compress and decompress commands for inline transfer.
7360Each item is a list that looks like this:
7361
7362\(COMPRESS DECOMPRESS\)
7363
7364COMPRESS or DECOMPRESS are strings with the respective commands.")
7365
7366(defun tramp-find-inline-compress (vec)
7367 "Find an inline transfer compress command that works.
7368Goes through the list `tramp-inline-compress-commands'."
7369 (save-excursion
7370 (let ((commands tramp-inline-compress-commands)
7371 (magic "xyzzy")
7372 item compress decompress
7373 found)
7374 (while (and commands (not found))
7375 (catch 'next
7376 (setq item (pop commands)
7377 compress (nth 0 item)
7378 decompress (nth 1 item))
7379 (tramp-message
7380 vec 5
7381 "Checking local compress command `%s', `%s' for sanity"
7382 compress decompress)
7383 (unless (zerop (tramp-call-local-coding-command
7384 (format "echo %s | %s | %s"
7385 magic compress decompress) nil nil))
7386 (throw 'next nil))
7387 (tramp-message
7388 vec 5
7389 "Checking remote compress command `%s', `%s' for sanity"
7390 compress decompress)
7391 (unless (zerop (tramp-send-command-and-check
7392 vec (format "echo %s | %s | %s"
7393 magic compress decompress) t))
7394 (throw 'next nil))
7395 (setq found t)))
7396
7397 ;; Did we find something?
7398 (if found
7399 (progn
7400 ;; Set connection properties.
7401 (tramp-message
7402 vec 5 "Using inline transfer compress command `%s'" compress)
7403 (tramp-set-connection-property vec "inline-compress" compress)
7404 (tramp-message
7405 vec 5 "Using inline transfer decompress command `%s'" decompress)
7406 (tramp-set-connection-property vec "inline-decompress" decompress))
7407
7408 (tramp-set-connection-property vec "inline-compress" nil)
7409 (tramp-set-connection-property vec "inline-decompress" nil)
7410 (tramp-message
7411 vec 2 "Couldn't find an inline transfer compress command")))))
7412
7413(defun tramp-compute-multi-hops (vec)
7414 "Expands VEC according to `tramp-default-proxies-alist'.
7415Gateway hops are already opened."
7416 (let ((target-alist `(,vec))
7417 (choices tramp-default-proxies-alist)
7418 item proxy)
7419
7420 ;; Look for proxy hosts to be passed.
7421 (while choices
7422 (setq item (pop choices)
7423 proxy (eval (nth 2 item)))
7424 (when (and
7425 ;; host
7426 (string-match (or (eval (nth 0 item)) "")
7427 (or (tramp-file-name-host (car target-alist)) ""))
7428 ;; user
7429 (string-match (or (eval (nth 1 item)) "")
7430 (or (tramp-file-name-user (car target-alist)) "")))
7431 (if (null proxy)
7432 ;; No more hops needed.
7433 (setq choices nil)
7434 ;; Replace placeholders.
7435 (setq proxy
7436 (format-spec
7437 proxy
7438 (format-spec-make
7439 ?u (or (tramp-file-name-user (car target-alist)) "")
7440 ?h (or (tramp-file-name-host (car target-alist)) ""))))
7441 (with-parsed-tramp-file-name proxy l
7442 ;; Add the hop.
7443 (add-to-list 'target-alist l)
7444 ;; Start next search.
7445 (setq choices tramp-default-proxies-alist)))))
7446
7447 ;; Handle gateways.
7448 (when (string-match
7449 (format
7450 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
7451 (tramp-file-name-method (car target-alist)))
7452 (let ((gw (pop target-alist))
7453 (hop (pop target-alist)))
7454 ;; Is the method prepared for gateways?
7455 (unless (tramp-get-method-parameter
7456 (tramp-file-name-method hop) 'tramp-default-port)
7457 (tramp-error
7458 vec 'file-error
7459 "Method `%s' is not supported for gateway access."
7460 (tramp-file-name-method hop)))
7461 ;; Add default port if needed.
7462 (unless
7463 (string-match
7464 tramp-host-with-port-regexp (tramp-file-name-host hop))
7465 (aset hop 2
7466 (concat
7467 (tramp-file-name-host hop) tramp-prefix-port-format
7468 (number-to-string
7469 (tramp-get-method-parameter
7470 (tramp-file-name-method hop) 'tramp-default-port)))))
7471 ;; Open the gateway connection.
7472 (add-to-list
7473 'target-alist
7474 (vector
7475 (tramp-file-name-method hop) (tramp-file-name-user hop)
7476 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
7477 ;; For the password prompt, we need the correct values.
7478 ;; Therefore, we must remember the gateway vector. But we
7479 ;; cannot do it as connection property, because it shouldn't
7480 ;; be persistent. And we have no started process yet either.
7481 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7482
7483 ;; Foreign and out-of-band methods are not supported for multi-hops.
7484 (when (cdr target-alist)
7485 (setq choices target-alist)
7486 (while choices
7487 (setq item (pop choices))
7488 (when
7489 (or
7490 (not
7491 (tramp-get-method-parameter
7492 (tramp-file-name-method item) 'tramp-login-program))
7493 (tramp-get-method-parameter
7494 (tramp-file-name-method item) 'tramp-copy-program))
7495 (tramp-error
7496 vec 'file-error
7497 "Method `%s' is not supported for multi-hops."
7498 (tramp-file-name-method item)))))
7499
7500 ;; In case the host name is not used for the remote shell
7501 ;; command, the user could be misguided by applying a random
7502 ;; hostname.
7503 (let* ((v (car target-alist))
7504 (method (tramp-file-name-method v))
7505 (host (tramp-file-name-host v)))
7506 (unless
7507 (or
7508 ;; There are multi-hops.
7509 (cdr target-alist)
7510 ;; The host name is used for the remote shell command.
7511 (member
7512 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7513 ;; The host is local. We cannot use `tramp-local-host-p'
7514 ;; here, because it opens a connection as well.
7515 (string-match tramp-local-host-regexp host))
7516 (tramp-error
7517 v 'file-error
7518 "Host `%s' looks like a remote host, `%s' can only use the local host"
7519 host method)))
7520
7521 ;; Result.
7522 target-alist))
7523
7524(defun tramp-maybe-open-connection (vec)
7525 "Maybe open a connection VEC.
7526Does not do anything if a connection is already open, but re-opens the
7527connection if a previous connection has died for some reason."
7528 (catch 'uname-changed
7529 (let ((p (tramp-get-connection-process vec))
7530 (process-name (tramp-get-connection-property vec "process-name" nil))
7531 (process-environment (copy-sequence process-environment)))
7532
7533 ;; If too much time has passed since last command was sent, look
7534 ;; whether process is still alive. If it isn't, kill it. When
7535 ;; using ssh, it can sometimes happen that the remote end has
7536 ;; hung up but the local ssh client doesn't recognize this until
7537 ;; it tries to send some data to the remote end. So that's why
7538 ;; we try to send a command from time to time, then look again
7539 ;; whether the process is really alive.
7540 (condition-case nil
7541 (when (and (> (tramp-time-diff
7542 (current-time)
7543 (tramp-get-connection-property
7544 p "last-cmd-time" '(0 0 0)))
7545 60)
7546 p (processp p) (memq (process-status p) '(run open)))
7547 (tramp-send-command vec "echo are you awake" t t)
7548 (unless (and (memq (process-status p) '(run open))
7549 (tramp-wait-for-output p 10))
7550 ;; The error will be catched locally.
7551 (tramp-error vec 'file-error "Awake did fail")))
7552 (file-error
7553 (tramp-flush-connection-property vec)
7554 (tramp-flush-connection-property p)
7555 (delete-process p)
7556 (setq p nil)))
7557
7558 ;; New connection must be opened.
7559 (unless (and p (processp p) (memq (process-status p) '(run open)))
7560
7561 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7562 ;; messages from the beginning.
7563 (tramp-get-buffer vec)
7564 (with-progress-reporter
7565 vec 3
7566 (if (zerop (length (tramp-file-name-user vec)))
7567 (format "Opening connection for %s using %s"
7568 (tramp-file-name-host vec)
7569 (tramp-file-name-method vec))
7570 (format "Opening connection for %s@%s using %s"
7571 (tramp-file-name-user vec)
7572 (tramp-file-name-host vec)
7573 (tramp-file-name-method vec)))
7574
7575 ;; Start new process.
7576 (when (and p (processp p))
7577 (delete-process p))
7578 (setenv "TERM" tramp-terminal-type)
7579 (setenv "LC_ALL" "C")
7580 (setenv "PROMPT_COMMAND")
7581 (setenv "PS1" tramp-initial-end-of-output)
7582 (let* ((target-alist (tramp-compute-multi-hops vec))
7583 (process-connection-type tramp-process-connection-type)
7584 (process-adaptive-read-buffering nil)
7585 (coding-system-for-read nil)
7586 ;; This must be done in order to avoid our file name handler.
7587 (p (let ((default-directory
7588 (tramp-compat-temporary-file-directory)))
7589 (start-process
7590 (or process-name (tramp-buffer-name vec))
7591 (tramp-get-connection-buffer vec)
7592 tramp-encoding-shell))))
7593
7594 (tramp-message
7595 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7596
7597 ;; Check whether process is alive.
7598 (tramp-set-process-query-on-exit-flag p nil)
7599 (tramp-barf-if-no-shell-prompt
7600 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell)
7601
7602 ;; Now do all the connections as specified.
7603 (while target-alist
7604 (let* ((hop (car target-alist))
7605 (l-method (tramp-file-name-method hop))
7606 (l-user (tramp-file-name-user hop))
7607 (l-host (tramp-file-name-host hop))
7608 (l-port nil)
7609 (login-program
7610 (tramp-get-method-parameter
7611 l-method 'tramp-login-program))
7612 (login-args
7613 (tramp-get-method-parameter l-method 'tramp-login-args))
7614 (async-args
7615 (tramp-get-method-parameter l-method 'tramp-async-args))
7616 (gw-args
7617 (tramp-get-method-parameter l-method 'tramp-gw-args))
7618 (gw (tramp-get-file-property hop "" "gateway" nil))
7619 (g-method (and gw (tramp-file-name-method gw)))
7620 (g-user (and gw (tramp-file-name-user gw)))
7621 (g-host (and gw (tramp-file-name-host gw)))
7622 (command login-program)
7623 ;; We don't create the temporary file. In fact,
7624 ;; it is just a prefix for the ControlPath option
7625 ;; of ssh; the real temporary file has another
7626 ;; name, and it is created and protected by ssh.
7627 ;; It is also removed by ssh, when the connection
7628 ;; is closed.
7629 (tmpfile
7630 (tramp-set-connection-property
7631 p "temp-file"
7632 (make-temp-name
7633 (expand-file-name
7634 tramp-temp-name-prefix
7635 (tramp-compat-temporary-file-directory)))))
7636 spec)
7637
7638 ;; Add arguments for asynchrononous processes.
7639 (when (and process-name async-args)
7640 (setq login-args (append async-args login-args)))
7641
7642 ;; Add gateway arguments if necessary.
7643 (when (and gw gw-args)
7644 (setq login-args (append gw-args login-args)))
7645
7646 ;; Check for port number. Until now, there's no need
7647 ;; for handling like method, user, host.
7648 (when (string-match tramp-host-with-port-regexp l-host)
7649 (setq l-port (match-string 2 l-host)
7650 l-host (match-string 1 l-host)))
7651
7652 ;; Set variables for computing the prompt for reading
7653 ;; password. They can also be derived from a gateway.
7654 (setq tramp-current-method (or g-method l-method)
7655 tramp-current-user (or g-user l-user)
7656 tramp-current-host (or g-host l-host))
7657
7658 ;; Replace login-args place holders.
7659 (setq
7660 l-host (or l-host "")
7661 l-user (or l-user "")
7662 l-port (or l-port "")
7663 spec (format-spec-make
7664 ?h l-host ?u l-user ?p l-port ?t tmpfile)
7665 command
7666 (concat
7667 ;; We do not want to see the trailing local prompt in
7668 ;; `start-file-process'.
7669 (unless (memq system-type '(windows-nt)) "exec ")
7670 command " "
7671 (mapconcat
7672 (lambda (x)
7673 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7674 (unless (member "" x) (mapconcat 'identity x " ")))
7675 login-args " ")
7676 ;; Local shell could be a Windows COMSPEC. It
7677 ;; doesn't know the ";" syntax, but we must exit
7678 ;; always for `start-file-process'. "exec" does not
7679 ;; work either.
7680 (if (memq system-type '(windows-nt)) " && exit || exit")))
7681
7682 ;; Send the command.
7683 (tramp-message vec 3 "Sending command `%s'" command)
7684 (tramp-send-command vec command t t)
7685 (tramp-process-actions p vec tramp-actions-before-shell 60)
7686 (tramp-message
7687 vec 3 "Found remote shell prompt on `%s'" l-host))
7688 ;; Next hop.
7689 (setq target-alist (cdr target-alist)))
7690
7691 ;; Make initial shell settings.
7692 (tramp-open-connection-setup-interactive-shell p vec)))))))
7693
7694(defun tramp-send-command (vec command &optional neveropen nooutput)
7695 "Send the COMMAND to connection VEC.
7696Erases temporary buffer before sending the command. If optional
7697arg NEVEROPEN is non-nil, never try to open the connection. This
7698is meant to be used from `tramp-maybe-open-connection' only. The
7699function waits for output unless NOOUTPUT is set."
7700 (unless neveropen (tramp-maybe-open-connection vec))
7701 (let ((p (tramp-get-connection-process vec)))
7702 (when (tramp-get-connection-property p "remote-echo" nil)
7703 ;; We mark the command string that it can be erased in the output buffer.
7704 (tramp-set-connection-property p "check-remote-echo" t)
7705 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7706 (tramp-message vec 6 "%s" command)
7707 (tramp-send-string vec command)
7708 (unless nooutput (tramp-wait-for-output p))))
7709
7710(defun tramp-wait-for-output (proc &optional timeout)
7711 "Wait for output from remote command."
7712 (unless (buffer-live-p (process-buffer proc))
7713 (delete-process proc)
7714 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
7715 (with-current-buffer (process-buffer proc)
7716 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
7717 ;; be leading escape sequences, which must be ignored.
7718 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
7719 ;; Sometimes, the commands do not return a newline but a
7720 ;; null byte before the shell prompt, for example "git
7721 ;; ls-files -c -z ...".
7722 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7723 (found (tramp-wait-for-regexp proc timeout regexp1)))
7724 (if found
7725 (let (buffer-read-only)
7726 ;; A simple-minded busybox has sent " ^H" sequences.
7727 ;; Delete them.
7728 (goto-char (point-min))
7729 (when (re-search-forward
7730 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7731 (forward-line 1)
7732 (delete-region (point-min) (point)))
7733 ;; Delete the prompt.
7734 (goto-char (point-max))
7735 (re-search-backward regexp nil t)
7736 (delete-region (point) (point-max)))
7737 (if timeout
7738 (tramp-error
7739 proc 'file-error
7740 "[[Remote prompt `%s' not found in %d secs]]"
7741 tramp-end-of-output timeout)
7742 (tramp-error
7743 proc 'file-error
7744 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7745 ;; Return value is whether end-of-output sentinel was found.
7746 found)))
7747
7748;;;###tramp-autoload
7749(defun tramp-send-command-and-check
7750 (vec command &optional subshell dont-suppress-err)
7751 "Run COMMAND and check its exit status.
7752Sends `echo $?' along with the COMMAND for checking the exit status. If
7753COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7754
7755If the optional argument SUBSHELL is non-nil, the command is
7756executed in a subshell, ie surrounded by parentheses. If
7757DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
7758 (tramp-send-command
7759 vec
7760 (concat (if subshell "( " "")
7761 command
7762 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
7763 "echo tramp_exit_status $?"
7764 (if subshell " )" "")))
7765 (with-current-buffer (tramp-get-connection-buffer vec)
7766 (goto-char (point-max))
7767 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7768 (tramp-error
7769 vec 'file-error "Couldn't find exit status of `%s'" command))
7770 (skip-chars-forward "^ ")
7771 (prog1
7772 (read (current-buffer))
7773 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7774
7775(defun tramp-barf-unless-okay (vec command fmt &rest args)
7776 "Run COMMAND, check exit status, throw error if exit status not okay.
7777Similar to `tramp-send-command-and-check' but accepts two more arguments
7778FMT and ARGS which are passed to `error'."
7779 (unless (zerop (tramp-send-command-and-check vec command))
7780 (apply 'tramp-error vec 'file-error fmt args)))
7781
7782(defun tramp-send-command-and-read (vec command)
7783 "Run COMMAND and return the output, which must be a Lisp expression.
7784In case there is no valid Lisp expression, it raises an error"
7785 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7786 (with-current-buffer (tramp-get-connection-buffer vec)
7787 ;; Read the expression.
7788 (goto-char (point-min))
7789 (condition-case nil
7790 (prog1 (read (current-buffer))
7791 ;; Error handling.
7792 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
7793 (error nil)))
7794 (error (tramp-error
7795 vec 'file-error
7796 "`%s' does not return a valid Lisp expression: `%s'"
7797 command (buffer-string))))))
7798
7799;; It seems that Tru64 Unix does not like it if long strings are sent 2811;; It seems that Tru64 Unix does not like it if long strings are sent
7800;; to it in one go. (This happens when sending the Perl 2812;; to it in one go. (This happens when sending the Perl
7801;; `file-attributes' implementation, for instance.) Therefore, we 2813;; `file-attributes' implementation, for instance.) Therefore, we
@@ -7838,181 +2850,6 @@ the remote host use line-endings as defined in the variable
7838 (setq pos (+ pos chunksize)))) 2850 (setq pos (+ pos chunksize))))
7839 (process-send-string p string))))) 2851 (process-send-string p string)))))
7840 2852
7841(defun tramp-mode-string-to-int (mode-string)
7842 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7843 (let* (case-fold-search
7844 (mode-chars (string-to-vector mode-string))
7845 (owner-read (aref mode-chars 1))
7846 (owner-write (aref mode-chars 2))
7847 (owner-execute-or-setid (aref mode-chars 3))
7848 (group-read (aref mode-chars 4))
7849 (group-write (aref mode-chars 5))
7850 (group-execute-or-setid (aref mode-chars 6))
7851 (other-read (aref mode-chars 7))
7852 (other-write (aref mode-chars 8))
7853 (other-execute-or-sticky (aref mode-chars 9)))
7854 (save-match-data
7855 (logior
7856 (cond
7857 ((char-equal owner-read ?r) (tramp-compat-octal-to-decimal "00400"))
7858 ((char-equal owner-read ?-) 0)
7859 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7860 (cond
7861 ((char-equal owner-write ?w) (tramp-compat-octal-to-decimal "00200"))
7862 ((char-equal owner-write ?-) 0)
7863 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7864 (cond
7865 ((char-equal owner-execute-or-setid ?x)
7866 (tramp-compat-octal-to-decimal "00100"))
7867 ((char-equal owner-execute-or-setid ?S)
7868 (tramp-compat-octal-to-decimal "04000"))
7869 ((char-equal owner-execute-or-setid ?s)
7870 (tramp-compat-octal-to-decimal "04100"))
7871 ((char-equal owner-execute-or-setid ?-) 0)
7872 (t (error "Fourth char `%c' must be one of `xsS-'"
7873 owner-execute-or-setid)))
7874 (cond
7875 ((char-equal group-read ?r) (tramp-compat-octal-to-decimal "00040"))
7876 ((char-equal group-read ?-) 0)
7877 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7878 (cond
7879 ((char-equal group-write ?w) (tramp-compat-octal-to-decimal "00020"))
7880 ((char-equal group-write ?-) 0)
7881 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7882 (cond
7883 ((char-equal group-execute-or-setid ?x)
7884 (tramp-compat-octal-to-decimal "00010"))
7885 ((char-equal group-execute-or-setid ?S)
7886 (tramp-compat-octal-to-decimal "02000"))
7887 ((char-equal group-execute-or-setid ?s)
7888 (tramp-compat-octal-to-decimal "02010"))
7889 ((char-equal group-execute-or-setid ?-) 0)
7890 (t (error "Seventh char `%c' must be one of `xsS-'"
7891 group-execute-or-setid)))
7892 (cond
7893 ((char-equal other-read ?r)
7894 (tramp-compat-octal-to-decimal "00004"))
7895 ((char-equal other-read ?-) 0)
7896 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7897 (cond
7898 ((char-equal other-write ?w) (tramp-compat-octal-to-decimal "00002"))
7899 ((char-equal other-write ?-) 0)
7900 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
7901 (cond
7902 ((char-equal other-execute-or-sticky ?x)
7903 (tramp-compat-octal-to-decimal "00001"))
7904 ((char-equal other-execute-or-sticky ?T)
7905 (tramp-compat-octal-to-decimal "01000"))
7906 ((char-equal other-execute-or-sticky ?t)
7907 (tramp-compat-octal-to-decimal "01001"))
7908 ((char-equal other-execute-or-sticky ?-) 0)
7909 (t (error "Tenth char `%c' must be one of `xtT-'"
7910 other-execute-or-sticky)))))))
7911
7912(defun tramp-convert-file-attributes (vec attr)
7913 "Convert file-attributes ATTR generated by perl script, stat or ls.
7914Convert file mode bits to string and set virtual device number.
7915Return ATTR."
7916 (when attr
7917 ;; Convert last access time.
7918 (unless (listp (nth 4 attr))
7919 (setcar (nthcdr 4 attr)
7920 (list (floor (nth 4 attr) 65536)
7921 (floor (mod (nth 4 attr) 65536)))))
7922 ;; Convert last modification time.
7923 (unless (listp (nth 5 attr))
7924 (setcar (nthcdr 5 attr)
7925 (list (floor (nth 5 attr) 65536)
7926 (floor (mod (nth 5 attr) 65536)))))
7927 ;; Convert last status change time.
7928 (unless (listp (nth 6 attr))
7929 (setcar (nthcdr 6 attr)
7930 (list (floor (nth 6 attr) 65536)
7931 (floor (mod (nth 6 attr) 65536)))))
7932 ;; Convert file size.
7933 (when (< (nth 7 attr) 0)
7934 (setcar (nthcdr 7 attr) -1))
7935 (when (and (floatp (nth 7 attr))
7936 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7937 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7938 ;; Convert file mode bits to string.
7939 (unless (stringp (nth 8 attr))
7940 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7941 (when (stringp (car attr))
7942 (aset (nth 8 attr) 0 ?l)))
7943 ;; Convert directory indication bit.
7944 (when (string-match "^d" (nth 8 attr))
7945 (setcar attr t))
7946 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7947 (when (consp (car attr))
7948 (if (and (stringp (caar attr))
7949 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7950 (setcar attr (match-string 1 (caar attr)))
7951 (setcar attr nil)))
7952 ;; Set file's gid change bit.
7953 (setcar (nthcdr 9 attr)
7954 (if (numberp (nth 3 attr))
7955 (not (= (nth 3 attr)
7956 (tramp-get-remote-gid vec 'integer)))
7957 (not (string-equal
7958 (nth 3 attr)
7959 (tramp-get-remote-gid vec 'string)))))
7960 ;; Convert inode.
7961 (unless (listp (nth 10 attr))
7962 (setcar (nthcdr 10 attr)
7963 (condition-case nil
7964 (cons (floor (nth 10 attr) 65536)
7965 (floor (mod (nth 10 attr) 65536)))
7966 ;; Inodes can be incredible huge. We must hide this.
7967 (error (tramp-get-inode vec)))))
7968 ;; Set virtual device number.
7969 (setcar (nthcdr 11 attr)
7970 (tramp-get-device vec))
7971 attr))
7972
7973(defun tramp-check-cached-permissions (vec access)
7974 "Check `file-attributes' caches for VEC.
7975Return t if according to the cache access type ACCESS is known to
7976be granted."
7977 (let ((result nil)
7978 (offset (cond
7979 ((eq ?r access) 1)
7980 ((eq ?w access) 2)
7981 ((eq ?x access) 3))))
7982 (dolist (suffix '("string" "integer") result)
7983 (setq
7984 result
7985 (or
7986 result
7987 (let ((file-attr
7988 (tramp-get-file-property
7989 vec (tramp-file-name-localname vec)
7990 (concat "file-attributes-" suffix) nil))
7991 (remote-uid
7992 (tramp-get-connection-property
7993 vec (concat "uid-" suffix) nil))
7994 (remote-gid
7995 (tramp-get-connection-property
7996 vec (concat "gid-" suffix) nil)))
7997 (and
7998 file-attr
7999 (or
8000 ;; Not a symlink
8001 (eq t (car file-attr))
8002 (null (car file-attr)))
8003 (or
8004 ;; World accessible.
8005 (eq access (aref (nth 8 file-attr) (+ offset 6)))
8006 ;; User accessible and owned by user.
8007 (and
8008 (eq access (aref (nth 8 file-attr) offset))
8009 (equal remote-uid (nth 2 file-attr)))
8010 ;; Group accessible and owned by user's
8011 ;; principal group.
8012 (and
8013 (eq access (aref (nth 8 file-attr) (+ offset 3)))
8014 (equal remote-gid (nth 3 file-attr)))))))))))
8015
8016(defun tramp-get-inode (vec) 2853(defun tramp-get-inode (vec)
8017 "Returns the virtual inode number. 2854 "Returns the virtual inode number.
8018If it doesn't exist, generate a new one." 2855If it doesn't exist, generate a new one."
@@ -8039,42 +2876,6 @@ If it doesn't exist, generate a new one."
8039 (list string (length tramp-devices)))) 2876 (list string (length tramp-devices))))
8040 (cons -1 (nth 1 (assoc string tramp-devices))))) 2877 (cons -1 (nth 1 (assoc string tramp-devices)))))
8041 2878
8042(defun tramp-file-mode-from-int (mode)
8043 "Turn an integer representing a file mode into an ls(1)-like string."
8044 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
8045 (user (logand (lsh mode -6) 7))
8046 (group (logand (lsh mode -3) 7))
8047 (other (logand (lsh mode -0) 7))
8048 (suid (> (logand (lsh mode -9) 4) 0))
8049 (sgid (> (logand (lsh mode -9) 2) 0))
8050 (sticky (> (logand (lsh mode -9) 1) 0)))
8051 (setq user (tramp-file-mode-permissions user suid "s"))
8052 (setq group (tramp-file-mode-permissions group sgid "s"))
8053 (setq other (tramp-file-mode-permissions other sticky "t"))
8054 (concat type user group other)))
8055
8056(defun tramp-file-mode-permissions (perm suid suid-text)
8057 "Convert a permission bitset into a string.
8058This is used internally by `tramp-file-mode-from-int'."
8059 (let ((r (> (logand perm 4) 0))
8060 (w (> (logand perm 2) 0))
8061 (x (> (logand perm 1) 0)))
8062 (concat (or (and r "r") "-")
8063 (or (and w "w") "-")
8064 (or (and suid x suid-text) ; suid, execute
8065 (and suid (upcase suid-text)) ; suid, !execute
8066 (and x "x") "-")))) ; !suid
8067
8068(defun tramp-shell-case-fold (string)
8069 "Converts STRING to shell glob pattern which ignores case."
8070 (mapconcat
8071 (lambda (c)
8072 (if (equal (downcase c) (upcase c))
8073 (vector c)
8074 (format "[%c%c]" (downcase c) (upcase c))))
8075 string
8076 ""))
8077
8078(defun tramp-equal-remote (file1 file2) 2879(defun tramp-equal-remote (file1 file2)
8079 "Check, whether the remote parts of FILE1 and FILE2 are identical. 2880 "Check, whether the remote parts of FILE1 and FILE2 are identical.
8080The check depends on method, user and host name of the files. If 2881The check depends on method, user and host name of the files. If
@@ -8093,395 +2894,6 @@ would yield `t'. On the other hand, the following check results in nil:
8093 (stringp (file-remote-p file2)) 2894 (stringp (file-remote-p file2))
8094 (string-equal (file-remote-p file1) (file-remote-p file2)))) 2895 (string-equal (file-remote-p file1) (file-remote-p file2))))
8095 2896
8096(defun tramp-make-copy-program-file-name (vec)
8097 "Create a file name suitable to be passed to `rcp' and workalikes."
8098 (let ((user (tramp-file-name-user vec))
8099 (host (tramp-file-name-real-host vec))
8100 (localname (tramp-shell-quote-argument
8101 (tramp-file-name-localname vec))))
8102 (if (not (zerop (length user)))
8103 (format "%s@%s:%s" user host localname)
8104 (format "%s:%s" host localname))))
8105
8106(defun tramp-method-out-of-band-p (vec size)
8107 "Return t if this is an out-of-band method, nil otherwise."
8108 (and
8109 ;; It shall be an out-of-band method.
8110 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8111 ;; Either the file size is large enough, or (in rare cases) there
8112 ;; does not exist a remote encoding.
8113 (or (null tramp-copy-size-limit)
8114 (> size tramp-copy-size-limit)
8115 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
8116
8117(defun tramp-local-host-p (vec)
8118 "Return t if this points to the local host, nil otherwise."
8119 ;; We cannot use `tramp-file-name-real-host'. A port is an
8120 ;; indication for an ssh tunnel or alike.
8121 (let ((host (tramp-file-name-host vec)))
8122 (and
8123 (stringp host)
8124 (string-match tramp-local-host-regexp host)
8125 ;; The method shall be applied to one of the shell file name
8126 ;; handler. `tramp-local-host-p' is also called for "smb" and
8127 ;; alike, where it must fail.
8128 (tramp-get-method-parameter
8129 (tramp-file-name-method vec) 'tramp-login-program)
8130 ;; The local temp directory must be writable for the other user.
8131 (file-writable-p
8132 (tramp-make-tramp-file-name
8133 (tramp-file-name-method vec)
8134 (tramp-file-name-user vec)
8135 host
8136 (tramp-compat-temporary-file-directory)))
8137 ;; On some systems, chown runs only for root.
8138 (or (zerop (user-uid))
8139 (zerop (tramp-get-remote-uid vec 'integer))))))
8140
8141;; Variables local to connection.
8142
8143;;;###tramp-autoload
8144(defun tramp-get-remote-path (vec)
8145 (with-connection-property
8146 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8147 ;; cache the result for the session only. Otherwise, the result
8148 ;; is cached persistently.
8149 (if (memq 'tramp-own-remote-path tramp-remote-path)
8150 (tramp-get-connection-process vec)
8151 vec)
8152 "remote-path"
8153 (let* ((remote-path (copy-tree tramp-remote-path))
8154 (elt1 (memq 'tramp-default-remote-path remote-path))
8155 (elt2 (memq 'tramp-own-remote-path remote-path))
8156 (default-remote-path
8157 (when elt1
8158 (condition-case nil
8159 (tramp-send-command-and-read
8160 vec "echo \\\"`getconf PATH`\\\"")
8161 ;; Default if "getconf" is not available.
8162 (error
8163 (tramp-message
8164 vec 3
8165 "`getconf PATH' not successful, using default value \"%s\"."
8166 "/bin:/usr/bin")
8167 "/bin:/usr/bin"))))
8168 (own-remote-path
8169 (when elt2
8170 (condition-case nil
8171 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8172 ;; Default if "getconf" is not available.
8173 (error
8174 (tramp-message
8175 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8176 nil)))))
8177
8178 ;; Replace place holder `tramp-default-remote-path'.
8179 (when elt1
8180 (setcdr elt1
8181 (append
8182 (tramp-compat-split-string default-remote-path ":")
8183 (cdr elt1)))
8184 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8185
8186 ;; Replace place holder `tramp-own-remote-path'.
8187 (when elt2
8188 (setcdr elt2
8189 (append
8190 (tramp-compat-split-string own-remote-path ":")
8191 (cdr elt2)))
8192 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8193
8194 ;; Remove double entries.
8195 (setq elt1 remote-path)
8196 (while (consp elt1)
8197 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8198 (setcar elt2 nil))
8199 (setq elt1 (cdr elt1)))
8200
8201 ;; Remove non-existing directories.
8202 (delq
8203 nil
8204 (mapcar
8205 (lambda (x)
8206 (and
8207 (stringp x)
8208 (file-directory-p
8209 (tramp-make-tramp-file-name
8210 (tramp-file-name-method vec)
8211 (tramp-file-name-user vec)
8212 (tramp-file-name-host vec)
8213 x))
8214 x))
8215 remote-path)))))
8216
8217;;;###tramp-autoload
8218(defun tramp-get-remote-tmpdir (vec)
8219 (with-connection-property vec "tmp-directory"
8220 (let ((dir (tramp-shell-quote-argument "/tmp")))
8221 (if (and (zerop
8222 (tramp-send-command-and-check
8223 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8224 (zerop
8225 (tramp-send-command-and-check
8226 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8227 dir
8228 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8229
8230(defun tramp-get-ls-command (vec)
8231 (with-connection-property vec "ls"
8232 (tramp-message vec 5 "Finding a suitable `ls' command")
8233 (or
8234 (catch 'ls-found
8235 (dolist (cmd '("ls" "gnuls" "gls"))
8236 (let ((dl (tramp-get-remote-path vec))
8237 result)
8238 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
8239 ;; Check parameters. On busybox, "ls" output coloring is
8240 ;; enabled by default sometimes. So we try to disable it
8241 ;; when possible. $LS_COLORING is not supported there.
8242 ;; Some "ls" versions are sensible wrt the order of
8243 ;; arguments, they fail when "-al" is after the
8244 ;; "--color=never" argument (for example on FreeBSD).
8245 (when (zerop (tramp-send-command-and-check
8246 vec (format "%s -lnd /" result)))
8247 (when (zerop (tramp-send-command-and-check
8248 vec (format
8249 "%s --color=never -al /dev/null" result)))
8250 (setq result (concat result " --color=never")))
8251 (throw 'ls-found result))
8252 (setq dl (cdr dl))))))
8253 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
8254
8255(defun tramp-get-ls-command-with-dired (vec)
8256 (save-match-data
8257 (with-connection-property vec "ls-dired"
8258 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8259 ;; Some "ls" versions are sensible wrt the order of arguments,
8260 ;; they fail when "-al" is after the "--dired" argument (for
8261 ;; example on FreeBSD).
8262 (zerop (tramp-send-command-and-check
8263 vec (format "%s --dired -al /dev/null"
8264 (tramp-get-ls-command vec)))))))
8265
8266(defun tramp-get-test-command (vec)
8267 (with-connection-property vec "test"
8268 (tramp-message vec 5 "Finding a suitable `test' command")
8269 (if (zerop (tramp-send-command-and-check vec "test 0"))
8270 "test"
8271 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
8272
8273(defun tramp-get-test-nt-command (vec)
8274 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8275 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8276 ;; for otherwise the shell crashes.
8277 (with-connection-property vec "test-nt"
8278 (or
8279 (progn
8280 (tramp-send-command
8281 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8282 (with-current-buffer (tramp-get-buffer vec)
8283 (goto-char (point-min))
8284 (when (looking-at (regexp-quote tramp-end-of-output))
8285 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8286 (progn
8287 (tramp-send-command
8288 vec
8289 (format
8290 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8291 (tramp-get-test-command vec)))
8292 "tramp_test_nt %s %s"))))
8293
8294(defun tramp-get-file-exists-command (vec)
8295 (with-connection-property vec "file-exists"
8296 (tramp-message vec 5 "Finding command to check if file exists")
8297 (tramp-find-file-exists-command vec)))
8298
8299;;;###tramp-autoload
8300(defun tramp-get-remote-ln (vec)
8301 (with-connection-property vec "ln"
8302 (tramp-message vec 5 "Finding a suitable `ln' command")
8303 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
8304
8305(defun tramp-get-remote-perl (vec)
8306 (with-connection-property vec "perl"
8307 (tramp-message vec 5 "Finding a suitable `perl' command")
8308 (let ((result
8309 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8310 (tramp-find-executable
8311 vec "perl" (tramp-get-remote-path vec)))))
8312 ;; We must check also for some Perl modules.
8313 (when result
8314 (with-connection-property vec "perl-file-spec"
8315 (zerop
8316 (tramp-send-command-and-check
8317 vec (format "%s -e 'use File::Spec;'" result))))
8318 (with-connection-property vec "perl-cwd-realpath"
8319 (zerop
8320 (tramp-send-command-and-check
8321 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8322 result)))
8323
8324(defun tramp-get-remote-stat (vec)
8325 (with-connection-property vec "stat"
8326 (tramp-message vec 5 "Finding a suitable `stat' command")
8327 (let ((result (tramp-find-executable
8328 vec "stat" (tramp-get-remote-path vec)))
8329 tmp)
8330 ;; Check whether stat(1) returns usable syntax. %s does not
8331 ;; work on older AIX systems.
8332 (when result
8333 (setq tmp
8334 ;; We don't want to display an error message.
8335 (with-temp-message (or (current-message) "")
8336 (condition-case nil
8337 (tramp-send-command-and-read
8338 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8339 (error nil))))
8340 (unless (and (listp tmp) (stringp (car tmp))
8341 (string-match "^./.$" (car tmp))
8342 (integerp (cadr tmp)))
8343 (setq result nil)))
8344 result)))
8345
8346(defun tramp-get-remote-readlink (vec)
8347 (with-connection-property vec "readlink"
8348 (tramp-message vec 5 "Finding a suitable `readlink' command")
8349 (let ((result (tramp-find-executable
8350 vec "readlink" (tramp-get-remote-path vec))))
8351 (when (and result
8352 ;; We don't want to display an error message.
8353 (with-temp-message (or (current-message) "")
8354 (condition-case nil
8355 (zerop
8356 (tramp-send-command-and-check
8357 vec (format "%s --canonicalize-missing /" result)))
8358 (error nil))))
8359 result))))
8360
8361(defun tramp-get-remote-trash (vec)
8362 (with-connection-property vec "trash"
8363 (tramp-message vec 5 "Finding a suitable `trash' command")
8364 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
8365
8366(defun tramp-get-remote-id (vec)
8367 (with-connection-property vec "id"
8368 (tramp-message vec 5 "Finding POSIX `id' command")
8369 (or
8370 (catch 'id-found
8371 (let ((dl (tramp-get-remote-path vec))
8372 result)
8373 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8374 ;; Check POSIX parameter.
8375 (when (zerop (tramp-send-command-and-check
8376 vec (format "%s -u" result)))
8377 (throw 'id-found result))
8378 (setq dl (cdr dl)))))
8379 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
8380
8381(defun tramp-get-remote-uid (vec id-format)
8382 (with-connection-property vec (format "uid-%s" id-format)
8383 (let ((res (tramp-send-command-and-read
8384 vec
8385 (format "%s -u%s %s"
8386 (tramp-get-remote-id vec)
8387 (if (equal id-format 'integer) "" "n")
8388 (if (equal id-format 'integer)
8389 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8390 ;; The command might not always return a number.
8391 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8392
8393(defun tramp-get-remote-gid (vec id-format)
8394 (with-connection-property vec (format "gid-%s" id-format)
8395 (let ((res (tramp-send-command-and-read
8396 vec
8397 (format "%s -g%s %s"
8398 (tramp-get-remote-id vec)
8399 (if (equal id-format 'integer) "" "n")
8400 (if (equal id-format 'integer)
8401 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8402 ;; The command might not always return a number.
8403 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8404
8405(defun tramp-get-local-uid (id-format)
8406 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8407
8408(defun tramp-get-local-gid (id-format)
8409 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8410
8411;; Some predefined connection properties.
8412(defun tramp-get-inline-compress (vec prop size)
8413 "Return the compress command related to PROP.
8414PROP is either `inline-compress' or `inline-decompress'. SIZE is
8415the length of the file to be compressed.
8416
8417If no corresponding command is found, nil is returned."
8418 (when (and (integerp tramp-inline-compress-start-size)
8419 (> size tramp-inline-compress-start-size))
8420 (with-connection-property vec prop
8421 (tramp-find-inline-compress vec)
8422 (tramp-get-connection-property vec prop nil))))
8423
8424(defun tramp-get-inline-coding (vec prop size)
8425 "Return the coding command related to PROP.
8426PROP is either `remote-encoding', `remode-decoding',
8427`local-encoding' or `local-decoding'.
8428
8429SIZE is the length of the file to be coded. Depending on SIZE,
8430compression might be applied.
8431
8432If no corresponding command is found, nil is returned.
8433Otherwise, either a string is returned which contains a `%s' mark
8434to be used for the respective input or output file; or a Lisp
8435function cell is returned to be applied on a buffer."
8436 (let ((coding
8437 (with-connection-property vec prop
8438 (tramp-find-inline-encoding vec)
8439 (tramp-get-connection-property vec prop nil)))
8440 (prop1 (if (string-match "encoding" prop)
8441 "inline-compress" "inline-decompress"))
8442 compress)
8443 ;; The connection property might have been cached. So we must send
8444 ;; the script to the remote side - maybe.
8445 (when (and coding (symbolp coding) (string-match "remote" prop))
8446 (let ((name (symbol-name coding)))
8447 (while (string-match (regexp-quote "-") name)
8448 (setq name (replace-match "_" nil t name)))
8449 (tramp-maybe-send-script vec (symbol-value coding) name)
8450 (setq coding name)))
8451 (when coding
8452 ;; Check for the `compress' command.
8453 (setq compress (tramp-get-inline-compress vec prop1 size))
8454 ;; Return the value.
8455 (cond
8456 ((and compress (symbolp coding))
8457 (if (string-match "decompress" prop1)
8458 `(lambda (beg end)
8459 (,coding beg end)
8460 (let ((coding-system-for-write 'binary)
8461 (coding-system-for-read 'binary))
8462 (apply
8463 'call-process-region (point-min) (point-max)
8464 (car (split-string ,compress)) t t nil
8465 (cdr (split-string ,compress)))))
8466 `(lambda (beg end)
8467 (let ((coding-system-for-write 'binary)
8468 (coding-system-for-read 'binary))
8469 (apply
8470 'call-process-region beg end
8471 (car (split-string ,compress)) t t nil
8472 (cdr (split-string ,compress))))
8473 (,coding (point-min) (point-max)))))
8474 ((symbolp coding)
8475 coding)
8476 ((and compress (string-match "decoding" prop))
8477 (format "(%s | %s >%%s)" coding compress))
8478 (compress
8479 (format "(%s <%%s | %s)" compress coding))
8480 ((string-match "decoding" prop)
8481 (format "%s >%%s" coding))
8482 (t
8483 (format "%s <%%s" coding))))))
8484
8485(defun tramp-get-method-parameter (method param) 2897(defun tramp-get-method-parameter (method param)
8486 "Return the method parameter PARAM. 2898 "Return the method parameter PARAM.
8487If the `tramp-methods' entry does not exist, return nil." 2899If the `tramp-methods' entry does not exist, return nil."
@@ -8494,27 +2906,26 @@ If the `tramp-methods' entry does not exist, return nil."
8494 "Check, whether OPERATION runs a file name handler." 2906 "Check, whether OPERATION runs a file name handler."
8495 ;; The file name handler is determined on base of either an 2907 ;; The file name handler is determined on base of either an
8496 ;; argument, `buffer-file-name', or `default-directory'. 2908 ;; argument, `buffer-file-name', or `default-directory'.
8497 (condition-case nil 2909 (ignore-errors
8498 (let* ((buffer-file-name "/") 2910 (let* ((buffer-file-name "/")
8499 (default-directory "/") 2911 (default-directory "/")
8500 (fnha file-name-handler-alist) 2912 (fnha file-name-handler-alist)
8501 (check-file-name-operation operation) 2913 (check-file-name-operation operation)
8502 (file-name-handler-alist 2914 (file-name-handler-alist
8503 (list 2915 (list
8504 (cons "/" 2916 (cons "/"
8505 (lambda (operation &rest args) 2917 (lambda (operation &rest args)
8506 "Returns OPERATION if it is the one to be checked." 2918 "Returns OPERATION if it is the one to be checked."
8507 (if (equal check-file-name-operation operation) 2919 (if (equal check-file-name-operation operation)
8508 operation 2920 operation
8509 (let ((file-name-handler-alist fnha)) 2921 (let ((file-name-handler-alist fnha))
8510 (apply operation args)))))))) 2922 (apply operation args))))))))
8511 (equal (apply operation args) operation)) 2923 (equal (apply operation args) operation))))
8512 (error nil)))
8513 2924
8514(unless (tramp-exists-file-name-handler 'make-auto-save-file-name) 2925(unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8515 (defadvice make-auto-save-file-name 2926 (defadvice make-auto-save-file-name
8516 (around tramp-advice-make-auto-save-file-name () activate) 2927 (around tramp-advice-make-auto-save-file-name () activate)
8517 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files." 2928 "Invoke `tramp-*-handle-make-auto-save-file-name' for Tramp files."
8518 (if (tramp-tramp-file-p (buffer-file-name)) 2929 (if (tramp-tramp-file-p (buffer-file-name))
8519 ;; We cannot call `tramp-handle-make-auto-save-file-name' 2930 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8520 ;; directly, because this would bypass the locking mechanism. 2931 ;; directly, because this would bypass the locking mechanism.
@@ -8754,106 +3165,39 @@ Only works for Bourne-like shells."
8754 ;; ange-ftp settings must be enabled. 3165 ;; ange-ftp settings must be enabled.
8755 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp) 3166 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
8756 ;; Maybe it's not loaded yet. 3167 ;; Maybe it's not loaded yet.
8757 (condition-case nil 3168 (ignore-errors (unload-feature 'tramp 'force)))
8758 (unload-feature 'tramp 'force)
8759 (error nil)))
8760
8761(when (and load-in-progress
8762 (string-match "Loading tramp..." (or (current-message) "")))
8763 (message "Loading tramp...done"))
8764 3169
8765(provide 'tramp) 3170(provide 'tramp)
8766 3171
8767;;; TODO: 3172;;; TODO:
8768 3173
8769;; * Handle nonlocal exits such as C-g.
8770;; * But it would probably be better to use with-local-quit at the
8771;; place where it's actually needed: around any potentially
8772;; indefinitely blocking piece of code. In this case it would be
8773;; within Tramp around one of its calls to accept-process-output (or
8774;; around one of the loops that calls accept-process-output)
8775;; (Stefan Monnier).
8776;; * Rewrite `tramp-shell-quote-argument' to abstain from using 3174;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8777;; `shell-quote-argument'. 3175;; `shell-quote-argument'.
8778;; * In Emacs 21, `insert-directory' shows total number of bytes used 3176;; * In Emacs 21, `insert-directory' shows total number of bytes used
8779;; by the files in that directory. Add this here. 3177;; by the files in that directory. Add this here.
8780;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman) 3178;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8781;; * Make ffap.el grok Tramp filenames. (Eli Tziperman) 3179;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8782;; * Don't use globbing for directories with many files, as this is
8783;; likely to produce long command lines, and some shells choke on
8784;; long command lines.
8785;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8786;; * abbreviate-file-name 3180;; * abbreviate-file-name
8787;; * Better error checking. At least whenever we see something 3181;; * Better error checking. At least whenever we see something
8788;; strange when doing zerop, we should kill the process and start 3182;; strange when doing zerop, we should kill the process and start
8789;; again. (Greg Stark) 3183;; again. (Greg Stark)
8790;; * Remove unneeded parameters from methods.
8791;; * Make it work for different encodings, and for different file name
8792;; encodings, too. (Daniel Pittman)
8793;; * Don't search for perl5 and perl. Instead, only search for perl and
8794;; then look if it's the right version (with `perl -v').
8795;; * When editing a remote CVS controlled file as a different user, VC
8796;; gets confused about the file locking status. Try to find out why
8797;; the workaround doesn't work.
8798;; * Username and hostname completion. 3184;; * Username and hostname completion.
8799;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'. 3185;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8800;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'. 3186;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8801;; Code is nearly identical. 3187;; Code is nearly identical.
8802;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8803;; until the last but one hop via `start-file-process'. Apply it
8804;; also for ftp and smb.
8805;; * WIBNI if we had a command "trampclient"? If I was editing in
8806;; some shell with root priviledges, it would be nice if I could
8807;; just call
8808;; trampclient filename.c
8809;; as an editor, and the _current_ shell would connect to an Emacs
8810;; server and would be used in an existing non-priviledged Emacs
8811;; session for doing the editing in question.
8812;; That way, I need not tell Emacs my password again and be afraid
8813;; that it makes it into core dumps or other ugly stuff (I had Emacs
8814;; once display a just typed password in the context of a keyboard
8815;; sequence prompt for a question immediately following in a shell
8816;; script run within Emacs -- nasty).
8817;; And if I have some ssh session running to a different computer,
8818;; having the possibility of passing a local file there to a local
8819;; Emacs session (in case I can arrange for a connection back) would
8820;; be nice.
8821;; Likely the corresponding Tramp server should not allow the
8822;; equivalent of the emacsclient -eval option in order to make this
8823;; reasonably unproblematic. And maybe trampclient should have some
8824;; way of passing credentials, like by using an SSL socket or
8825;; something. (David Kastrup)
8826;; * Reconnect directly to a compliant shell without first going
8827;; through the user's default shell. (Pete Forman)
8828;; * Make `tramp-default-user' obsolete. 3188;; * Make `tramp-default-user' obsolete.
8829;; * How can I interrupt the remote process with a signal
8830;; (interrupt-process seems not to work)? (Markus Triska)
8831;; * Avoid the local shell entirely for starting remote processes. If
8832;; so, I think even a signal, when delivered directly to the local
8833;; SSH instance, would correctly be propagated to the remote process
8834;; automatically; possibly SSH would have to be started with
8835;; "-t". (Markus Triska)
8836;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8837;; isn't on the remote host. (Mark A. Hershberger)
8838;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8839;; * Implement a general server-local-variable mechanism, as there are 3189;; * Implement a general server-local-variable mechanism, as there are
8840;; probably other variables that need different values for different 3190;; probably other variables that need different values for different
8841;; servers too. The user could then configure a variable (such as 3191;; servers too. The user could then configure a variable (such as
8842;; tramp-server-local-variable-alist) to define any such variables 3192;; tramp-server-local-variable-alist) to define any such variables
8843;; that they need to, which would then be let bound as appropriate 3193;; that they need to, which would then be let bound as appropriate
8844;; in tramp functions. (Jason Rumney) 3194;; in tramp functions. (Jason Rumney)
8845;; * Optimize out-of-band copying, when both methods are scp-like (not
8846;; rsync).
8847;; * Keep a second connection open for out-of-band methods like scp or
8848;; rsync.
8849;; * IMHO, it's a drawback that currently Tramp doesn't support 3195;; * IMHO, it's a drawback that currently Tramp doesn't support
8850;; Unicode in Dired file names by default. Is it possible to 3196;; Unicode in Dired file names by default. Is it possible to
8851;; improve Tramp to set LC_ALL to "C" only for commands where Tramp 3197;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8852;; expects English? Or just to set LC_MESSAGES to "C" if Tramp 3198;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
8853;; expects only English messages? (Juri Linkov) 3199;; expects only English messages? (Juri Linkov)
8854;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846) 3200;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8855;; * Try telnet+curl as new method. It might be useful for busybox,
8856;; without built-in uuencode/uudecode.
8857;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'. 3201;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
8858;; * I was wondering it it would be possible to use tramp even if I'm 3202;; * I was wondering it it would be possible to use tramp even if I'm
8859;; actually using sshfs. But when I launch a command I would like 3203;; actually using sshfs. But when I launch a command I would like