Fix permissions bugs with setgid directories etc.
[bpt/emacs.git] / lisp / net / tramp-sh.el
CommitLineData
03c1ad43
MA
1;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
2
acaf905b 3;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
03c1ad43
MA
4
5;; (copyright statements below in code to be updated with the above notice)
6
7;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8;; Michael Albinus <michael.albinus@gmx.de>
9;; Keywords: comm, processes
10;; Package: tramp
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software: you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27;;; Code:
28
6139f995 29(eval-when-compile (require 'cl)) ; ignore-errors
03c1ad43 30(require 'tramp)
03c1ad43
MA
31
32;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
33;; not sure at all that this is the right way to do it, but let's hope
34;; it works for now, and wait for a guru to point out the Right Way to
35;; achieve this.
36;;(eval-when-compile
37;; (unless (fboundp 'dired-insert-set-properties)
38;; (fset 'dired-insert-set-properties 'ignore)))
39;; Gerd suggests this:
40(eval-when-compile (require 'dired))
41;; Note that dired is required at run-time, too, when it is needed.
42;; It is only needed on XEmacs for the function
43;; `dired-insert-set-properties'.
44
45(defcustom tramp-inline-compress-start-size 4096
fb7ada5f 46 "The minimum size of compressing where inline transfer.
333f9019 47When inline transfer, compress transferred data of file
03c1ad43
MA
48whose size is this value or above (up to `tramp-copy-size-limit').
49If it is nil, no compression at all will be applied."
50 :group 'tramp
51 :type '(choice (const nil) integer))
52
53(defcustom tramp-copy-size-limit 10240
2fe4b125
MA
54 "The maximum file size where inline copying is preferred over an \
55out-of-the-band copy.
56If it is nil, out-of-the-band copy will be used without a check."
03c1ad43
MA
57 :group 'tramp
58 :type '(choice (const nil) integer))
59
60;;;###tramp-autoload
61(defcustom tramp-terminal-type "dumb"
fb7ada5f 62 "Value of TERM environment variable for logging in to remote host.
03c1ad43
MA
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
6d95bd46
MA
69(defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
70 "Escape sequences produced by the \"ls\" command.")
71
2c68ca0e 72;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
03c1ad43 73;; root users. It uses the `$' character for other users. In order
a857d3c7 74;; to guarantee a proper prompt, we use "#$ " for the prompt.
03c1ad43
MA
75
76(defvar tramp-end-of-output
77 (format
78 "///%s#$"
79 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
80 "String used to recognize end of output.
81The '$' character at the end is quoted; the string cannot be
82detected as prompt when being sent on echoing hosts, therefore.")
83
84;;;###tramp-autoload
85(defconst tramp-initial-end-of-output "#$ "
86 "Prompt when establishing a connection.")
87
88;; Initialize `tramp-methods' with the supported methods.
89;;;###tramp-autoload
90(add-to-list 'tramp-methods
91 '("rcp"
92 (tramp-login-program "rsh")
93 (tramp-login-args (("%h") ("-l" "%u")))
f5e29b9b
MA
94 (tramp-remote-shell "/bin/sh")
95 (tramp-remote-shell-args ("-c"))
03c1ad43 96 (tramp-copy-program "rcp")
f3afd36b 97 (tramp-copy-args (("-p" "%k") ("-r")))
03c1ad43
MA
98 (tramp-copy-keep-date t)
99 (tramp-copy-recursive t)))
100;;;###tramp-autoload
101(add-to-list 'tramp-methods
102 '("remcp"
103 (tramp-login-program "remsh")
104 (tramp-login-args (("%h") ("-l" "%u")))
f5e29b9b
MA
105 (tramp-remote-shell "/bin/sh")
106 (tramp-remote-shell-args ("-c"))
03c1ad43 107 (tramp-copy-program "rcp")
f3afd36b 108 (tramp-copy-args (("-p" "%k")))
03c1ad43
MA
109 (tramp-copy-keep-date t)))
110;;;###tramp-autoload
66feec8b 111(add-to-list 'tramp-methods
7398933f
MA
112 '("scp"
113 (tramp-login-program "ssh")
03c1ad43
MA
114 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
115 (tramp-async-args (("-q")))
f5e29b9b
MA
116 (tramp-remote-shell "/bin/sh")
117 (tramp-remote-shell-args ("-c"))
03c1ad43 118 (tramp-copy-program "scp")
f3afd36b 119 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
03c1ad43
MA
120 (tramp-copy-keep-date t)
121 (tramp-copy-recursive t)
122 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
123 ("-o" "UserKnownHostsFile=/dev/null")
124 ("-o" "StrictHostKeyChecking=no")))
125 (tramp-default-port 22)))
126;;;###tramp-autoload
127(add-to-list 'tramp-methods
128 '("scp1"
129 (tramp-login-program "ssh")
130 (tramp-login-args (("-l" "%u") ("-p" "%p")
131 ("-1") ("-e" "none") ("%h")))
132 (tramp-async-args (("-q")))
f5e29b9b
MA
133 (tramp-remote-shell "/bin/sh")
134 (tramp-remote-shell-args ("-c"))
03c1ad43 135 (tramp-copy-program "scp")
f3afd36b 136 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
03c1ad43
MA
137 (tramp-copy-keep-date t)
138 (tramp-copy-recursive t)
139 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
140 ("-o" "UserKnownHostsFile=/dev/null")
141 ("-o" "StrictHostKeyChecking=no")))
142 (tramp-default-port 22)))
143;;;###tramp-autoload
144(add-to-list 'tramp-methods
145 '("scp2"
146 (tramp-login-program "ssh")
147 (tramp-login-args (("-l" "%u") ("-p" "%p")
148 ("-2") ("-e" "none") ("%h")))
149 (tramp-async-args (("-q")))
f5e29b9b
MA
150 (tramp-remote-shell "/bin/sh")
151 (tramp-remote-shell-args ("-c"))
03c1ad43 152 (tramp-copy-program "scp")
f3afd36b 153 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
03c1ad43
MA
154 (tramp-copy-keep-date t)
155 (tramp-copy-recursive t)
156 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
157 ("-o" "UserKnownHostsFile=/dev/null")
158 ("-o" "StrictHostKeyChecking=no")))
159 (tramp-default-port 22)))
160;;;###tramp-autoload
161(add-to-list 'tramp-methods
162 '("scpc"
163 (tramp-login-program "ssh")
164 (tramp-login-args (("-l" "%u") ("-p" "%p")
165 ("-o" "ControlPath=%t.%%r@%%h:%%p")
166 ("-o" "ControlMaster=yes")
167 ("-e" "none") ("%h")))
168 (tramp-async-args (("-q")))
f5e29b9b
MA
169 (tramp-remote-shell "/bin/sh")
170 (tramp-remote-shell-args ("-c"))
03c1ad43 171 (tramp-copy-program "scp")
f3afd36b 172 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")
03c1ad43
MA
173 ("-o" "ControlPath=%t.%%r@%%h:%%p")
174 ("-o" "ControlMaster=auto")))
175 (tramp-copy-keep-date t)
8d68c659 176 (tramp-copy-recursive t)
03c1ad43
MA
177 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
178 ("-o" "UserKnownHostsFile=/dev/null")
179 ("-o" "StrictHostKeyChecking=no")))
180 (tramp-default-port 22)))
181;;;###tramp-autoload
182(add-to-list 'tramp-methods
183 '("scpx"
184 (tramp-login-program "ssh")
185 (tramp-login-args (("-l" "%u") ("-p" "%p")
186 ("-e" "none") ("-t" "-t")
187 ("%h") ("/bin/sh")))
188 (tramp-async-args (("-q")))
f5e29b9b
MA
189 (tramp-remote-shell "/bin/sh")
190 (tramp-remote-shell-args ("-c"))
03c1ad43 191 (tramp-copy-program "scp")
f3afd36b 192 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
03c1ad43 193 (tramp-copy-keep-date t)
8d68c659 194 (tramp-copy-recursive t)
03c1ad43
MA
195 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
196 ("-o" "UserKnownHostsFile=/dev/null")
197 ("-o" "StrictHostKeyChecking=no")))
198 (tramp-default-port 22)))
199;;;###tramp-autoload
200(add-to-list 'tramp-methods
201 '("sftp"
202 (tramp-login-program "ssh")
203 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
204 (tramp-async-args (("-q")))
f5e29b9b
MA
205 (tramp-remote-shell "/bin/sh")
206 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
207 (tramp-copy-program "sftp")))
208;;;###tramp-autoload
209(add-to-list 'tramp-methods
210 '("rsync"
211 (tramp-login-program "ssh")
212 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
213 (tramp-async-args (("-q")))
f5e29b9b
MA
214 (tramp-remote-shell "/bin/sh")
215 (tramp-remote-shell-args ("-c"))
03c1ad43 216 (tramp-copy-program "rsync")
f3afd36b 217 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
03c1ad43
MA
218 (tramp-copy-keep-date t)
219 (tramp-copy-keep-tmpfile t)
220 (tramp-copy-recursive t)))
221;;;###tramp-autoload
222(add-to-list 'tramp-methods
223 `("rsyncc"
224 (tramp-login-program "ssh")
225 (tramp-login-args (("-l" "%u") ("-p" "%p")
226 ("-o" "ControlPath=%t.%%r@%%h:%%p")
227 ("-o" "ControlMaster=yes")
228 ("-e" "none") ("%h")))
229 (tramp-async-args (("-q")))
f5e29b9b
MA
230 (tramp-remote-shell "/bin/sh")
231 (tramp-remote-shell-args ("-c"))
03c1ad43 232 (tramp-copy-program "rsync")
f3afd36b 233 (tramp-copy-args (("-t" "%k") ("-r")))
03c1ad43
MA
234 (tramp-copy-env (("RSYNC_RSH")
235 (,(concat
236 "ssh"
237 " -o ControlPath=%t.%%r@%%h:%%p"
238 " -o ControlMaster=auto"))))
239 (tramp-copy-keep-date t)
240 (tramp-copy-keep-tmpfile t)
241 (tramp-copy-recursive t)))
242;;;###tramp-autoload
243(add-to-list 'tramp-methods
244 '("rsh"
245 (tramp-login-program "rsh")
246 (tramp-login-args (("%h") ("-l" "%u")))
f5e29b9b
MA
247 (tramp-remote-shell "/bin/sh")
248 (tramp-remote-shell-args ("-c"))))
03c1ad43
MA
249;;;###tramp-autoload
250(add-to-list 'tramp-methods
251 '("remsh"
252 (tramp-login-program "remsh")
253 (tramp-login-args (("%h") ("-l" "%u")))
f5e29b9b
MA
254 (tramp-remote-shell "/bin/sh")
255 (tramp-remote-shell-args ("-c"))))
03c1ad43
MA
256;;;###tramp-autoload
257(add-to-list 'tramp-methods
258 '("ssh"
259 (tramp-login-program "ssh")
260 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
261 (tramp-async-args (("-q")))
f5e29b9b
MA
262 (tramp-remote-shell "/bin/sh")
263 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
264 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
265 ("-o" "UserKnownHostsFile=/dev/null")
266 ("-o" "StrictHostKeyChecking=no")))
267 (tramp-default-port 22)))
268;;;###tramp-autoload
269(add-to-list 'tramp-methods
270 '("ssh1"
271 (tramp-login-program "ssh")
272 (tramp-login-args (("-l" "%u") ("-p" "%p")
273 ("-1") ("-e" "none") ("%h")))
274 (tramp-async-args (("-q")))
f5e29b9b
MA
275 (tramp-remote-shell "/bin/sh")
276 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
277 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
278 ("-o" "UserKnownHostsFile=/dev/null")
279 ("-o" "StrictHostKeyChecking=no")))
280 (tramp-default-port 22)))
281;;;###tramp-autoload
282(add-to-list 'tramp-methods
283 '("ssh2"
284 (tramp-login-program "ssh")
285 (tramp-login-args (("-l" "%u") ("-p" "%p")
286 ("-2") ("-e" "none") ("%h")))
287 (tramp-async-args (("-q")))
f5e29b9b
MA
288 (tramp-remote-shell "/bin/sh")
289 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
290 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
291 ("-o" "UserKnownHostsFile=/dev/null")
292 ("-o" "StrictHostKeyChecking=no")))
293 (tramp-default-port 22)))
294;;;###tramp-autoload
295(add-to-list 'tramp-methods
296 '("sshx"
297 (tramp-login-program "ssh")
298 (tramp-login-args (("-l" "%u") ("-p" "%p")
299 ("-e" "none") ("-t" "-t")
300 ("%h") ("/bin/sh")))
301 (tramp-async-args (("-q")))
f5e29b9b
MA
302 (tramp-remote-shell "/bin/sh")
303 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
304 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
305 ("-o" "UserKnownHostsFile=/dev/null")
306 ("-o" "StrictHostKeyChecking=no")))
307 (tramp-default-port 22)))
308;;;###tramp-autoload
309(add-to-list 'tramp-methods
310 '("telnet"
311 (tramp-login-program "telnet")
312 (tramp-login-args (("%h") ("%p")))
f5e29b9b
MA
313 (tramp-remote-shell "/bin/sh")
314 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
315 (tramp-default-port 23)))
316;;;###tramp-autoload
317(add-to-list 'tramp-methods
318 '("su"
319 (tramp-login-program "su")
320 (tramp-login-args (("-") ("%u")))
f5e29b9b
MA
321 (tramp-remote-shell "/bin/sh")
322 (tramp-remote-shell-args ("-c"))))
03c1ad43
MA
323;;;###tramp-autoload
324(add-to-list 'tramp-methods
325 '("sudo"
326 (tramp-login-program "sudo")
327 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
f5e29b9b
MA
328 (tramp-remote-shell "/bin/sh")
329 (tramp-remote-shell-args ("-c"))))
03c1ad43 330;;;###tramp-autoload
d68b0220
MA
331(add-to-list 'tramp-methods
332 '("ksu"
333 (tramp-login-program "ksu")
334 (tramp-login-args (("%u") ("-q")))
f5e29b9b
MA
335 (tramp-remote-shell "/bin/sh")
336 (tramp-remote-shell-args ("-c"))))
d68b0220 337;;;###tramp-autoload
03c1ad43
MA
338(add-to-list 'tramp-methods
339 '("krlogin"
340 (tramp-login-program "krlogin")
341 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
f5e29b9b
MA
342 (tramp-remote-shell "/bin/sh")
343 (tramp-remote-shell-args ("-c"))))
03c1ad43
MA
344;;;###tramp-autoload
345(add-to-list 'tramp-methods
346 '("plink"
347 (tramp-login-program "plink")
348 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
f5e29b9b
MA
349 (tramp-remote-shell "/bin/sh")
350 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
351 (tramp-default-port 22)))
352;;;###tramp-autoload
353(add-to-list 'tramp-methods
354 '("plink1"
355 (tramp-login-program "plink")
356 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
f5e29b9b
MA
357 (tramp-remote-shell "/bin/sh")
358 (tramp-remote-shell-args ("-c"))
03c1ad43
MA
359 (tramp-default-port 22)))
360;;;###tramp-autoload
361(add-to-list 'tramp-methods
362 `("plinkx"
363 (tramp-login-program "plink")
364 ;; ("%h") must be a single element, see
365 ;; `tramp-compute-multi-hops'.
366 (tramp-login-args (("-load") ("%h") ("-t")
367 (,(format
368 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
369 tramp-terminal-type
370 tramp-initial-end-of-output))
371 ("/bin/sh")))
f5e29b9b
MA
372 (tramp-remote-shell "/bin/sh")
373 (tramp-remote-shell-args ("-c"))))
03c1ad43
MA
374;;;###tramp-autoload
375(add-to-list 'tramp-methods
376 '("pscp"
377 (tramp-login-program "plink")
378 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
f5e29b9b
MA
379 (tramp-remote-shell "/bin/sh")
380 (tramp-remote-shell-args ("-c"))
03c1ad43 381 (tramp-copy-program "pscp")
a5509865 382 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
66feec8b 383 ("-q") ("-r")))
03c1ad43 384 (tramp-copy-keep-date t)
66feec8b 385 (tramp-copy-recursive t)
03c1ad43
MA
386 (tramp-default-port 22)))
387;;;###tramp-autoload
388(add-to-list 'tramp-methods
389 '("psftp"
390 (tramp-login-program "plink")
391 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
f5e29b9b
MA
392 (tramp-remote-shell "/bin/sh")
393 (tramp-remote-shell-args ("-c"))
03c1ad43 394 (tramp-copy-program "pscp")
a5509865 395 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
66feec8b 396 ("-q") ("-r")))
03c1ad43 397 (tramp-copy-keep-date t)
2fe4b125 398 (tramp-copy-recursive t)))
03c1ad43
MA
399;;;###tramp-autoload
400(add-to-list 'tramp-methods
401 '("fcp"
402 (tramp-login-program "fsh")
403 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
f5e29b9b
MA
404 (tramp-remote-shell "/bin/sh")
405 (tramp-remote-shell-args ("-i") ("-c"))
03c1ad43 406 (tramp-copy-program "fcp")
f3afd36b 407 (tramp-copy-args (("-p" "%k")))
03c1ad43
MA
408 (tramp-copy-keep-date t)))
409
b191c9d9 410;;;###tramp-autoload
03c1ad43
MA
411(add-to-list 'tramp-default-method-alist
412 `(,tramp-local-host-regexp "\\`root\\'" "su"))
413
b191c9d9 414;;;###tramp-autoload
03c1ad43 415(add-to-list 'tramp-default-user-alist
d68b0220
MA
416 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
417 nil "root"))
fe99f704 418;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
a5509865 419;; Do not add "plink" based methods, they ask interactively for the user.
b191c9d9 420;;;###tramp-autoload
03c1ad43 421(add-to-list 'tramp-default-user-alist
d68b0220
MA
422 `(,(concat
423 "\\`"
a5509865 424 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
d68b0220 425 "\\'")
03c1ad43
MA
426 nil ,(user-login-name)))
427
f8f91c2b 428;;;###tramp-autoload
03c1ad43
MA
429(defconst tramp-completion-function-alist-rsh
430 '((tramp-parse-rhosts "/etc/hosts.equiv")
431 (tramp-parse-rhosts "~/.rhosts"))
432 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
433
f8f91c2b 434;;;###tramp-autoload
03c1ad43
MA
435(defconst tramp-completion-function-alist-ssh
436 '((tramp-parse-rhosts "/etc/hosts.equiv")
437 (tramp-parse-rhosts "/etc/shosts.equiv")
438 (tramp-parse-shosts "/etc/ssh_known_hosts")
439 (tramp-parse-sconfig "/etc/ssh_config")
440 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
441 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
442 (tramp-parse-rhosts "~/.rhosts")
443 (tramp-parse-rhosts "~/.shosts")
444 (tramp-parse-shosts "~/.ssh/known_hosts")
445 (tramp-parse-sconfig "~/.ssh/config")
446 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
447 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
448 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
449
f8f91c2b 450;;;###tramp-autoload
03c1ad43
MA
451(defconst tramp-completion-function-alist-telnet
452 '((tramp-parse-hosts "/etc/hosts"))
453 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
454
f8f91c2b 455;;;###tramp-autoload
03c1ad43
MA
456(defconst tramp-completion-function-alist-su
457 '((tramp-parse-passwd "/etc/passwd"))
458 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
459
f8f91c2b 460;;;###tramp-autoload
03c1ad43 461(defconst tramp-completion-function-alist-putty
2fe4b125
MA
462 `((tramp-parse-putty
463 ,(if (memq system-type '(windows-nt))
464 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
465 "~/.putty/sessions")))
466 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
03c1ad43 467
f8f91c2b
MA
468;;;###tramp-autoload
469(eval-after-load 'tramp
470 '(progn
471 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
472 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
473 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
474 (tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh)
475 (tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh)
476 (tramp-set-completion-function "scpc" tramp-completion-function-alist-ssh)
477 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
478 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh)
479 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
480 (tramp-set-completion-function
481 "rsyncc" tramp-completion-function-alist-ssh)
482 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
483 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
484 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
485 (tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh)
486 (tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh)
487 (tramp-set-completion-function
488 "ssh1_old" tramp-completion-function-alist-ssh)
489 (tramp-set-completion-function
490 "ssh2_old" tramp-completion-function-alist-ssh)
491 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
492 (tramp-set-completion-function
493 "telnet" tramp-completion-function-alist-telnet)
494 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
495 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
496 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
497 (tramp-set-completion-function
498 "krlogin" tramp-completion-function-alist-rsh)
499 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
500 (tramp-set-completion-function
501 "plink1" tramp-completion-function-alist-ssh)
502 (tramp-set-completion-function
503 "plinkx" tramp-completion-function-alist-putty)
504 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
505 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
03c1ad43
MA
506
507;; "getconf PATH" yields:
508;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
509;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
510;; GNU/Linux (Debian, Suse): /bin:/usr/bin
511;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
512;; IRIX64: /usr/bin
97912def 513;;;###tramp-autoload
03c1ad43 514(defcustom tramp-remote-path
c2c43c23
MA
515 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
516 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
517 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
2fe4b125 518 "/opt/bin" "/opt/sbin" "/opt/local/bin")
fb7ada5f 519 "List of directories to search for executables on remote host.
03c1ad43
MA
520For every remote host, this variable will be set buffer local,
521keeping the list of existing directories on that host.
522
523You can use `~' in this list, but when searching for a shell which groks
524tilde expansion, all directory names starting with `~' will be ignored.
525
526`Default Directories' represent the list of directories given by
527the command \"getconf PATH\". It is recommended to use this
528entry on top of this list, because these are the default
a857d3c7
MA
529directories for POSIX compatible commands. On remote hosts which
530do not offer the getconf command (like cygwin), the value
531\"/bin:/usr/bin\" is used instead of.
03c1ad43
MA
532
533`Private Directories' are the settings of the $PATH environment,
534as given in your `~/.profile'."
535 :group 'tramp
536 :type '(repeat (choice
537 (const :tag "Default Directories" tramp-default-remote-path)
538 (const :tag "Private Directories" tramp-own-remote-path)
539 (string :tag "Directory"))))
540
541(defcustom tramp-remote-process-environment
542 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
543 ,(format "TERM=%s" tramp-terminal-type)
544 "EMACS=t" ;; Deprecated.
545 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
99a54f21 546 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
03c1ad43 547 "autocorrect=" "correct=")
fb7ada5f 548 "List of environment variables to be set on the remote host.
03c1ad43
MA
549
550Each element should be a string of the form ENVVARNAME=VALUE. An
4c36be58 551entry ENVVARNAME= disables the corresponding environment variable,
03c1ad43
MA
552which might have been set in the init files like ~/.profile.
553
554Special handling is applied to the PATH environment, which should
2c68ca0e 555not be set here. Instead, it should be set via `tramp-remote-path'."
03c1ad43
MA
556 :group 'tramp
557 :type '(repeat string))
558
559(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
fb7ada5f 560 "Alist specifying extra arguments to pass to the remote shell.
03c1ad43
MA
561Entries are (REGEXP . ARGS) where REGEXP is a regular expression
562matching the shell file name and ARGS is a string specifying the
563arguments.
564
565This variable is only used when Tramp needs to start up another shell
566for tilde expansion. The extra arguments should typically prevent the
567shell from reading its init file."
568 :group 'tramp
569 ;; This might be the wrong way to test whether the widget type
570 ;; `alist' is available. Who knows the right way to test it?
571 :type (if (get 'alist 'widget-type)
572 '(alist :key-type string :value-type string)
573 '(repeat (cons string string))))
574
575(defconst tramp-actions-before-shell
576 '((tramp-login-prompt-regexp tramp-action-login)
577 (tramp-password-prompt-regexp tramp-action-password)
578 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
579 (shell-prompt-pattern tramp-action-succeed)
580 (tramp-shell-prompt-pattern tramp-action-succeed)
581 (tramp-yesno-prompt-regexp tramp-action-yesno)
582 (tramp-yn-prompt-regexp tramp-action-yn)
583 (tramp-terminal-prompt-regexp tramp-action-terminal)
584 (tramp-process-alive-regexp tramp-action-process-alive))
585 "List of pattern/action pairs.
586Whenever a pattern matches, the corresponding action is performed.
587Each item looks like (PATTERN ACTION).
588
589The PATTERN should be a symbol, a variable. The value of this
590variable gives the regular expression to search for. Note that the
591regexp must match at the end of the buffer, \"\\'\" is implicitly
592appended to it.
593
594The ACTION should also be a symbol, but a function. When the
595corresponding PATTERN matches, the ACTION function is called.")
596
597(defconst tramp-actions-copy-out-of-band
598 '((tramp-password-prompt-regexp tramp-action-password)
599 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
600 (tramp-copy-failed-regexp tramp-action-permission-denied)
601 (tramp-process-alive-regexp tramp-action-out-of-band))
602 "List of pattern/action pairs.
603This list is used for copying/renaming with out-of-band methods.
604
605See `tramp-actions-before-shell' for more info.")
606
607(defconst tramp-uudecode
608 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
609cat /tmp/tramp.$$
610rm -f /tmp/tramp.$$"
611 "Shell function to implement `uudecode' to standard output.
612Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
613for this or `uudecode -p', but some systems don't, and for them
614we have this shell function.")
615
616(defconst tramp-perl-file-truename
617 "%s -e '
618use File::Spec;
619use Cwd \"realpath\";
620
621sub recursive {
622 my ($volume, @dirs) = @_;
623 my $real = realpath(File::Spec->catpath(
624 $volume, File::Spec->catdir(@dirs), \"\"));
625 if ($real) {
626 my ($vol, $dir) = File::Spec->splitpath($real, 1);
627 return ($vol, File::Spec->splitdir($dir));
628 }
629 else {
630 my $last = pop(@dirs);
631 ($volume, @dirs) = recursive($volume, @dirs);
632 push(@dirs, $last);
633 return ($volume, @dirs);
634 }
635}
636
637$result = realpath($ARGV[0]);
638if (!$result) {
639 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
640 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
641
642 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
643}
644
645if ($ARGV[0] =~ /\\/$/) {
646 $result = $result . \"/\";
647}
648
649print \"\\\"$result\\\"\\n\";
650' \"$1\" 2>/dev/null"
651 "Perl script to produce output suitable for use with `file-truename'
652on the remote file system.
653Escape sequence %s is replaced with name of Perl binary.
654This string is passed to `format', so percent characters need to be doubled.")
655
656(defconst tramp-perl-file-name-all-completions
657 "%s -e 'sub case {
658 my $str = shift;
659 if ($ARGV[2]) {
660 return lc($str);
661 }
662 else {
663 return $str;
664 }
665}
666opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
667@files = readdir(d); closedir(d);
668foreach $f (@files) {
669 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
670 if (-d \"$ARGV[0]/$f\") {
671 print \"$f/\\n\";
672 }
673 else {
674 print \"$f\\n\";
675 }
676 }
677}
678print \"ok\\n\"
679' \"$1\" \"$2\" \"$3\" 2>/dev/null"
680 "Perl script to produce output suitable for use with
681`file-name-all-completions' on the remote file system. Escape
682sequence %s is replaced with name of Perl binary. This string is
683passed to `format', so percent characters need to be doubled.")
684
685;; Perl script to implement `file-attributes' in a Lisp `read'able
686;; output. If you are hacking on this, note that you get *no* output
687;; unless this spits out a complete line, including the '\n' at the
688;; end.
689;; The device number is returned as "-1", because there will be a virtual
4a93e698 690;; device number set in `tramp-sh-handle-file-attributes'.
03c1ad43
MA
691(defconst tramp-perl-file-attributes
692 "%s -e '
693@stat = lstat($ARGV[0]);
694if (!@stat) {
695 print \"nil\\n\";
696 exit 0;
697}
698if (($stat[2] & 0170000) == 0120000)
699{
700 $type = readlink($ARGV[0]);
701 $type = \"\\\"$type\\\"\";
702}
703elsif (($stat[2] & 0170000) == 040000)
704{
705 $type = \"t\";
706}
707else
708{
709 $type = \"nil\"
710};
711$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
712$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
713printf(
714 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
715 $type,
716 $stat[3],
717 $uid,
718 $gid,
719 $stat[8] >> 16 & 0xffff,
720 $stat[8] & 0xffff,
721 $stat[9] >> 16 & 0xffff,
722 $stat[9] & 0xffff,
723 $stat[10] >> 16 & 0xffff,
724 $stat[10] & 0xffff,
725 $stat[7],
726 $stat[2],
727 $stat[1] >> 16 & 0xffff,
728 $stat[1] & 0xffff
729);' \"$1\" \"$2\" 2>/dev/null"
730 "Perl script to produce output suitable for use with `file-attributes'
731on the remote file system.
732Escape sequence %s is replaced with name of Perl binary.
733This string is passed to `format', so percent characters need to be doubled.")
734
735(defconst tramp-perl-directory-files-and-attributes
736 "%s -e '
737chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
738opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
739@list = readdir(DIR);
740closedir(DIR);
741$n = scalar(@list);
742printf(\"(\\n\");
743for($i = 0; $i < $n; $i++)
744{
745 $filename = $list[$i];
746 @stat = lstat($filename);
747 if (($stat[2] & 0170000) == 0120000)
748 {
749 $type = readlink($filename);
750 $type = \"\\\"$type\\\"\";
751 }
752 elsif (($stat[2] & 0170000) == 040000)
753 {
754 $type = \"t\";
755 }
756 else
757 {
758 $type = \"nil\"
759 };
760 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
761 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
762 printf(
763 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
764 $filename,
765 $type,
766 $stat[3],
767 $uid,
768 $gid,
769 $stat[8] >> 16 & 0xffff,
770 $stat[8] & 0xffff,
771 $stat[9] >> 16 & 0xffff,
772 $stat[9] & 0xffff,
773 $stat[10] >> 16 & 0xffff,
774 $stat[10] & 0xffff,
775 $stat[7],
776 $stat[2],
777 $stat[1] >> 16 & 0xffff,
778 $stat[1] & 0xffff,
779 $stat[0] >> 16 & 0xffff,
780 $stat[0] & 0xffff);
781}
782printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
783 "Perl script implementing `directory-files-attributes' as Lisp `read'able
784output.
785Escape sequence %s is replaced with name of Perl binary.
786This string is passed to `format', so percent characters need to be doubled.")
787
788;; These two use base64 encoding.
789(defconst tramp-perl-encode-with-module
790 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
791 "Perl program to use for encoding a file.
792Escape sequence %s is replaced with name of Perl binary.
793This string is passed to `format', so percent characters need to be doubled.
794This implementation requires the MIME::Base64 Perl module to be installed
795on the remote host.")
796
797(defconst tramp-perl-decode-with-module
798 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
799 "Perl program to use for decoding a file.
800Escape sequence %s is replaced with name of Perl binary.
801This string is passed to `format', so percent characters need to be doubled.
802This implementation requires the MIME::Base64 Perl module to be installed
803on the remote host.")
804
805(defconst tramp-perl-encode
806 "%s -e '
807# This script contributed by Juanma Barranquero <lektu@terra.es>.
acaf905b 808# Copyright (C) 2002-2012 Free Software Foundation, Inc.
03c1ad43
MA
809use strict;
810
811my %%trans = do {
812 my $i = 0;
813 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
814 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
815};
c2f5b60a 816my $data;
03c1ad43
MA
817
818# We read in chunks of 54 bytes, to generate output lines
819# of 72 chars (plus end of line)
c2f5b60a 820while (read STDIN, $data, 54) {
03c1ad43
MA
821 my $pad = q();
822
823 # Only for the last chunk, and only if did not fill the last three-byte packet
824 if (eof) {
825 my $mod = length($data) %% 3;
826 $pad = q(=) x (3 - $mod) if $mod;
827 }
828
829 # Not the fastest method, but it is simple: unpack to binary string, split
830 # by groups of 6 bits and convert back from binary to byte; then map into
831 # the translation table
832 print
833 join q(),
834 map($trans{$_},
835 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
836 $pad,
837 qq(\\n);
838}' 2>/dev/null"
839 "Perl program to use for encoding a file.
840Escape sequence %s is replaced with name of Perl binary.
841This string is passed to `format', so percent characters need to be doubled.")
842
843(defconst tramp-perl-decode
844 "%s -e '
845# This script contributed by Juanma Barranquero <lektu@terra.es>.
acaf905b 846# Copyright (C) 2002-2012 Free Software Foundation, Inc.
03c1ad43
MA
847use strict;
848
849my %%trans = do {
850 my $i = 0;
851 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
852 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
853};
854
855my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
856
857binmode(\\*STDOUT);
858
859# We are going to accumulate into $pending to accept any line length
860# (we do not check they are <= 76 chars as the RFC says)
861my $pending = q();
862
863while (my $data = <STDIN>) {
864 chomp $data;
865
866 # If we find one or two =, we have reached the end and
867 # any following data is to be discarded
868 my $finished = $data =~ s/(==?).*/$1/;
869 $pending .= $data;
870
871 my $len = length($pending);
872 my $chunk = substr($pending, 0, $len & ~3);
873 $pending = substr($pending, $len & ~3 + 1);
874
875 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
876 # split in 8-bit chunks and convert back to char.
877 print join q(),
878 map $bytes{$_},
879 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
880
881 last if $finished;
882}' 2>/dev/null"
883 "Perl program to use for decoding a file.
884Escape sequence %s is replaced with name of Perl binary.
885This string is passed to `format', so percent characters need to be doubled.")
886
887(defconst tramp-vc-registered-read-file-names
888 "echo \"(\"
889while read file; do
890 if %s \"$file\"; then
891 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
892 else
893 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
894 fi
895 if %s \"$file\"; then
896 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
897 else
898 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
899 fi
900done
901echo \")\""
902 "Script to check existence of VC related files.
903It must be send formatted with two strings; the tests for file
904existence, and file readability. Input shall be read via
905here-document, otherwise the command could exceed maximum length
906of command line.")
907
908(defconst tramp-file-mode-type-map
909 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
910 (1 . "p") ; fifo
911 (2 . "c") ; character device
912 (3 . "m") ; multiplexed character device (v7)
913 (4 . "d") ; directory
914 (5 . "?") ; Named special file (XENIX)
915 (6 . "b") ; block device
916 (7 . "?") ; multiplexed block device (v7)
917 (8 . "-") ; regular file
918 (9 . "n") ; network special file (HP-UX)
919 (10 . "l") ; symlink
920 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
921 (12 . "s") ; socket
922 (13 . "D") ; door special (Solaris)
923 (14 . "w")) ; whiteout (BSD)
924 "A list of file types returned from the `stat' system call.
925This is used to map a mode number to a permission string.")
926
927;; New handlers should be added here. The following operations can be
928;; handled using the normal primitives: file-name-sans-versions,
929;; get-file-buffer.
930(defconst tramp-sh-file-name-handler-alist
931 '((load . tramp-handle-load)
4a93e698 932 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
03c1ad43
MA
933 (file-name-as-directory . tramp-handle-file-name-as-directory)
934 (file-name-directory . tramp-handle-file-name-directory)
935 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
4a93e698
MA
936 (file-truename . tramp-sh-handle-file-truename)
937 (file-exists-p . tramp-sh-handle-file-exists-p)
938 (file-directory-p . tramp-sh-handle-file-directory-p)
939 (file-executable-p . tramp-sh-handle-file-executable-p)
940 (file-readable-p . tramp-sh-handle-file-readable-p)
03c1ad43
MA
941 (file-regular-p . tramp-handle-file-regular-p)
942 (file-symlink-p . tramp-handle-file-symlink-p)
4a93e698
MA
943 (file-writable-p . tramp-sh-handle-file-writable-p)
944 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
945 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
946 (file-attributes . tramp-sh-handle-file-attributes)
03c1ad43
MA
947 (file-modes . tramp-handle-file-modes)
948 (directory-files . tramp-handle-directory-files)
4a93e698
MA
949 (directory-files-and-attributes
950 . tramp-sh-handle-directory-files-and-attributes)
951 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
03c1ad43 952 (file-name-completion . tramp-handle-file-name-completion)
4a93e698
MA
953 (add-name-to-file . tramp-sh-handle-add-name-to-file)
954 (copy-file . tramp-sh-handle-copy-file)
955 (copy-directory . tramp-sh-handle-copy-directory)
956 (rename-file . tramp-sh-handle-rename-file)
957 (set-file-modes . tramp-sh-handle-set-file-modes)
958 (set-file-times . tramp-sh-handle-set-file-times)
959 (make-directory . tramp-sh-handle-make-directory)
960 (delete-directory . tramp-sh-handle-delete-directory)
961 (delete-file . tramp-sh-handle-delete-file)
03c1ad43
MA
962 (directory-file-name . tramp-handle-directory-file-name)
963 ;; `executable-find' is not official yet.
4a93e698
MA
964 (executable-find . tramp-sh-handle-executable-find)
965 (start-file-process . tramp-sh-handle-start-file-process)
966 (process-file . tramp-sh-handle-process-file)
f5e29b9b 967 (shell-command . tramp-handle-shell-command)
4a93e698
MA
968 (insert-directory . tramp-sh-handle-insert-directory)
969 (expand-file-name . tramp-sh-handle-expand-file-name)
03c1ad43 970 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
4a93e698 971 (file-local-copy . tramp-sh-handle-file-local-copy)
03c1ad43
MA
972 (file-remote-p . tramp-handle-file-remote-p)
973 (insert-file-contents . tramp-handle-insert-file-contents)
974 (insert-file-contents-literally
4a93e698
MA
975 . tramp-sh-handle-insert-file-contents-literally)
976 (write-region . tramp-sh-handle-write-region)
e44eccd7 977 (find-backup-file-name . tramp-handle-find-backup-file-name)
4a93e698 978 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
03c1ad43 979 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
4a93e698 980 (dired-compress-file . tramp-sh-handle-dired-compress-file)
03c1ad43 981 (dired-recursive-delete-directory
4a93e698 982 . tramp-sh-handle-dired-recursive-delete-directory)
03c1ad43 983 (dired-uncache . tramp-handle-dired-uncache)
4a93e698
MA
984 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
985 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
986 (file-selinux-context . tramp-sh-handle-file-selinux-context)
987 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
988 (vc-registered . tramp-sh-handle-vc-registered))
03c1ad43
MA
989 "Alist of handler functions.
990Operations not mentioned here will be handled by the normal Emacs functions.")
991
992;; This must be the last entry, because `identity' always matches.
993;;;###tramp-autoload
994(add-to-list 'tramp-foreign-file-name-handler-alist
995 '(identity . tramp-sh-file-name-handler) 'append)
996
997;;; File Name Handler Functions:
998
4a93e698 999(defun tramp-sh-handle-make-symbolic-link
03c1ad43
MA
1000 (filename linkname &optional ok-if-already-exists)
1001 "Like `make-symbolic-link' for Tramp files.
1002If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1003the symlink. If LINKNAME is a Tramp file, only the localname component is
1004used as the target of the symlink.
1005
1006If LINKNAME is a Tramp file and the localname component is relative, then
1007it is expanded first, before the localname component is taken. Note that
1008this can give surprising results if the user/host for the source and
1009target of the symlink differ."
1010 (with-parsed-tramp-file-name linkname l
1011 (let ((ln (tramp-get-remote-ln l))
1012 (cwd (tramp-run-real-handler
1013 'file-name-directory (list l-localname))))
1014 (unless ln
1015 (tramp-error
1016 l 'file-error
1017 "Making a symbolic link. ln(1) does not exist on the remote host."))
1018
1019 ;; Do the 'confirm if exists' thing.
1020 (when (file-exists-p linkname)
1021 ;; What to do?
1022 (if (or (null ok-if-already-exists) ; not allowed to exist
1023 (and (numberp ok-if-already-exists)
1024 (not (yes-or-no-p
1025 (format
1026 "File %s already exists; make it a link anyway? "
1027 l-localname)))))
1028 (tramp-error
1029 l 'file-already-exists "File %s already exists" l-localname)
1030 (delete-file linkname)))
1031
1032 ;; If FILENAME is a Tramp name, use just the localname component.
1033 (when (tramp-tramp-file-p filename)
1034 (setq filename
1035 (tramp-file-name-localname
1036 (tramp-dissect-file-name (expand-file-name filename)))))
1037
1038 (tramp-flush-file-property l (file-name-directory l-localname))
1039 (tramp-flush-file-property l l-localname)
1040
6bdac736
MA
1041 ;; Right, they are on the same host, regardless of user, method,
1042 ;; etc. We now make the link on the remote machine. This will
1043 ;; occur as the user that FILENAME belongs to.
03c1ad43
MA
1044 (tramp-send-command-and-check
1045 l
1046 (format
1047 "cd %s && %s -sf %s %s"
1048 (tramp-shell-quote-argument cwd)
1049 ln
1050 (tramp-shell-quote-argument filename)
1051 (tramp-shell-quote-argument l-localname))
1052 t))))
1053
4a93e698 1054(defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
03c1ad43
MA
1055 "Like `file-truename' for Tramp files."
1056 (with-parsed-tramp-file-name (expand-file-name filename) nil
d0c8fc8a 1057 (tramp-make-tramp-file-name method user host
1d51f99c 1058 (with-tramp-file-property v localname "file-truename"
d0c8fc8a
MA
1059 (let ((result nil)) ; result steps in reverse order
1060 (tramp-message v 4 "Finding true name for `%s'" filename)
1061 (cond
1062 ;; Use GNU readlink --canonicalize-missing where available.
1063 ((tramp-get-remote-readlink v)
1064 (setq result
1065 (tramp-send-command-and-read
1066 v
1067 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1068 (tramp-get-remote-readlink v)
1069 (tramp-shell-quote-argument localname)))))
1070
1071 ;; Use Perl implementation.
1072 ((and (tramp-get-remote-perl v)
1073 (tramp-get-connection-property v "perl-file-spec" nil)
1074 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1075 (tramp-maybe-send-script
1076 v tramp-perl-file-truename "tramp_perl_file_truename")
1077 (setq result
1078 (tramp-send-command-and-read
1079 v
1080 (format "tramp_perl_file_truename %s"
1081 (tramp-shell-quote-argument localname)))))
1082
1083 ;; Do it yourself. We bind `directory-sep-char' here for
1084 ;; XEmacs on Windows, which would otherwise use backslash.
1085 (t (let* ((directory-sep-char ?/)
1086 (steps (tramp-compat-split-string localname "/"))
1087 (localnamedir (tramp-run-real-handler
1088 'file-name-as-directory (list localname)))
1089 (is-dir (string= localname localnamedir))
1090 (thisstep nil)
1091 (numchase 0)
1092 ;; Don't make the following value larger than
1093 ;; necessary. People expect an error message in
1094 ;; a timely fashion when something is wrong;
1095 ;; otherwise they might think that Emacs is hung.
1096 ;; Of course, correctness has to come first.
1097 (numchase-limit 20)
1098 symlink-target)
1099 (while (and steps (< numchase numchase-limit))
1100 (setq thisstep (pop steps))
1101 (tramp-message
1102 v 5 "Check %s"
1103 (mapconcat 'identity
1104 (append '("") (reverse result) (list thisstep))
1105 "/"))
1106 (setq symlink-target
1107 (nth 0 (file-attributes
1108 (tramp-make-tramp-file-name
1109 method user host
1110 (mapconcat 'identity
1111 (append '("")
1112 (reverse result)
1113 (list thisstep))
1114 "/")))))
1115 (cond ((string= "." thisstep)
1116 (tramp-message v 5 "Ignoring step `.'"))
1117 ((string= ".." thisstep)
1118 (tramp-message v 5 "Processing step `..'")
1119 (pop result))
1120 ((stringp symlink-target)
1121 ;; It's a symlink, follow it.
1122 (tramp-message
1123 v 5 "Follow symlink to %s" symlink-target)
1124 (setq numchase (1+ numchase))
1125 (when (file-name-absolute-p symlink-target)
1126 (setq result nil))
1127 ;; If the symlink was absolute, we'll get a
1128 ;; string like "/user@host:/some/target";
1129 ;; extract the "/some/target" part from it.
1130 (when (tramp-tramp-file-p symlink-target)
1131 (unless (tramp-equal-remote filename symlink-target)
1132 (tramp-error
1133 v 'file-error
1134 "Symlink target `%s' on wrong host"
1135 symlink-target))
1136 (setq symlink-target localname))
1137 (setq steps
1138 (append (tramp-compat-split-string
1139 symlink-target "/")
1140 steps)))
1141 (t
1142 ;; It's a file.
1143 (setq result (cons thisstep result)))))
1144 (when (>= numchase numchase-limit)
1145 (tramp-error
1146 v 'file-error
1147 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1148 (setq result (reverse result))
1149 ;; Combine list to form string.
1150 (setq result
1151 (if result
1152 (mapconcat 'identity (cons "" result) "/")
1153 "/"))
1154 (when (and is-dir
1155 (or (string= "" result)
1156 (not (string= (substring result -1) "/"))))
1157 (setq result (concat result "/"))))))
1158
1159 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1160 result)))))
03c1ad43
MA
1161
1162;; Basic functions.
1163
4a93e698 1164(defun tramp-sh-handle-file-exists-p (filename)
03c1ad43
MA
1165 "Like `file-exists-p' for Tramp files."
1166 (with-parsed-tramp-file-name filename nil
1d51f99c 1167 (with-tramp-file-property v localname "file-exists-p"
03c1ad43
MA
1168 (or (not (null (tramp-get-file-property
1169 v localname "file-attributes-integer" nil)))
1170 (not (null (tramp-get-file-property
1171 v localname "file-attributes-string" nil)))
1172 (tramp-send-command-and-check
1173 v
1174 (format
1175 "%s %s"
1176 (tramp-get-file-exists-command v)
1177 (tramp-shell-quote-argument localname)))))))
1178
4a93e698 1179(defun tramp-sh-handle-file-attributes (filename &optional id-format)
03c1ad43
MA
1180 "Like `file-attributes' for Tramp files."
1181 (unless id-format (setq id-format 'integer))
1182 ;; Don't modify `last-coding-system-used' by accident.
1183 (let ((last-coding-system-used last-coding-system-used))
1184 (with-parsed-tramp-file-name (expand-file-name filename) nil
1d51f99c
MA
1185 (with-tramp-file-property
1186 v localname (format "file-attributes-%s" id-format)
03c1ad43
MA
1187 (save-excursion
1188 (tramp-convert-file-attributes
1189 v
f42efeb5
MA
1190 (or
1191 (cond
1192 ((tramp-get-remote-stat v)
1193 (tramp-do-file-attributes-with-stat v localname id-format))
1194 ((tramp-get-remote-perl v)
1195 (tramp-do-file-attributes-with-perl v localname id-format))
1196 (t nil))
1197 ;; The scripts could fail, for example with huge file size.
1198 (tramp-do-file-attributes-with-ls v localname id-format))))))))
03c1ad43
MA
1199
1200(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1201 "Implement `file-attributes' for Tramp files using the ls(1) command."
1202 (let (symlinkp dirp
1203 res-inode res-filemodes res-numlinks
1204 res-uid res-gid res-size res-symlink-target)
1205 (tramp-message vec 5 "file attributes with ls: %s" localname)
1206 (tramp-send-command
1207 vec
1208 (format "(%s %s || %s -h %s) && %s %s %s"
1209 (tramp-get-file-exists-command vec)
1210 (tramp-shell-quote-argument localname)
1211 (tramp-get-test-command vec)
1212 (tramp-shell-quote-argument localname)
1213 (tramp-get-ls-command vec)
1214 (if (eq id-format 'integer) "-ildn" "-ild")
1215 (tramp-shell-quote-argument localname)))
1216 ;; parse `ls -l' output ...
1217 (with-current-buffer (tramp-get-buffer vec)
1218 (when (> (buffer-size) 0)
1219 (goto-char (point-min))
1220 ;; ... inode
1221 (setq res-inode
1222 (condition-case err
1223 (read (current-buffer))
1224 (invalid-read-syntax
1225 (when (and (equal (cadr err)
1226 "Integer constant overflow in reader")
1227 (string-match
1228 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1229 (car (cddr err))))
1230 (let* ((big (read (substring (car (cddr err)) 0
1231 (match-beginning 1))))
1232 (small (read (match-string 1 (car (cddr err)))))
1233 (twiddle (/ small 65536)))
1234 (cons (+ big twiddle)
1235 (- small (* twiddle 65536))))))))
1236 ;; ... file mode flags
1237 (setq res-filemodes (symbol-name (read (current-buffer))))
1238 ;; ... number links
1239 (setq res-numlinks (read (current-buffer)))
1240 ;; ... uid and gid
1241 (setq res-uid (read (current-buffer)))
1242 (setq res-gid (read (current-buffer)))
1243 (if (eq id-format 'integer)
1244 (progn
1245 (unless (numberp res-uid) (setq res-uid -1))
1246 (unless (numberp res-gid) (setq res-gid -1)))
1247 (progn
1248 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1249 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1250 ;; ... size
1251 (setq res-size (read (current-buffer)))
1252 ;; From the file modes, figure out other stuff.
1253 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1254 (setq dirp (eq ?d (aref res-filemodes 0)))
1255 ;; if symlink, find out file name pointed to
1256 (when symlinkp
1257 (search-forward "-> ")
6e060cee 1258 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
03c1ad43
MA
1259 ;; return data gathered
1260 (list
1261 ;; 0. t for directory, string (name linked to) for symbolic
1262 ;; link, or nil.
1263 (or dirp res-symlink-target)
1264 ;; 1. Number of links to file.
1265 res-numlinks
1266 ;; 2. File uid.
1267 res-uid
1268 ;; 3. File gid.
1269 res-gid
d7f2a65c
MA
1270 ;; 4. Last access time, as a list of integers. Normally this
1271 ;; would be in the same format as `current-time', but the
1272 ;; subseconds part is not currently implemented, and (0 0)
1273 ;; denotes an unknown time.
03c1ad43
MA
1274 ;; 5. Last modification time, likewise.
1275 ;; 6. Last status change time, likewise.
535efd4a 1276 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
03c1ad43
MA
1277 ;; 7. Size in bytes (-1, if number is out of range).
1278 res-size
1279 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1280 res-filemodes
1281 ;; 9. t if file's gid would change if file were deleted and
1282 ;; recreated. Will be set in `tramp-convert-file-attributes'
1283 t
1284 ;; 10. inode number.
1285 res-inode
1286 ;; 11. Device number. Will be replaced by a virtual device number.
1287 -1
1288 )))))
1289
1290(defun tramp-do-file-attributes-with-perl
1291 (vec localname &optional id-format)
1292 "Implement `file-attributes' for Tramp files using a Perl script."
1293 (tramp-message vec 5 "file attributes with perl: %s" localname)
1294 (tramp-maybe-send-script
1295 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1296 (tramp-send-command-and-read
1297 vec
1298 (format "tramp_perl_file_attributes %s %s"
1299 (tramp-shell-quote-argument localname) id-format)))
1300
1301(defun tramp-do-file-attributes-with-stat
1302 (vec localname &optional id-format)
1303 "Implement `file-attributes' for Tramp files using stat(1) command."
1304 (tramp-message vec 5 "file attributes with stat: %s" localname)
1305 (tramp-send-command-and-read
1306 vec
1307 (format
1308 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1309 ;; parse correctly the sequence "((". Therefore, we add a space.
01d884cf 1310 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
03c1ad43
MA
1311 (tramp-get-file-exists-command vec)
1312 (tramp-shell-quote-argument localname)
1313 (tramp-get-test-command vec)
1314 (tramp-shell-quote-argument localname)
1315 (tramp-get-remote-stat vec)
2fe4b125
MA
1316 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1317 (if (eq id-format 'integer) "%ge0" "\"%G\"")
03c1ad43
MA
1318 (tramp-shell-quote-argument localname))))
1319
4a93e698 1320(defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
03c1ad43
MA
1321 "Like `set-visited-file-modtime' for Tramp files."
1322 (unless (buffer-file-name)
1323 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1324 (buffer-name)))
1325 (if time-list
1326 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1327 (let ((f (buffer-file-name))
1328 coding-system-used)
1329 (with-parsed-tramp-file-name f nil
1330 (let* ((attr (file-attributes f))
1331 ;; '(-1 65535) means file doesn't exists yet.
1332 (modtime (or (nth 5 attr) '(-1 65535))))
1333 (when (boundp 'last-coding-system-used)
1334 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1335 ;; We use '(0 0) as a don't-know value. See also
1336 ;; `tramp-do-file-attributes-with-ls'.
1337 (if (not (equal modtime '(0 0)))
1338 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1339 (progn
1340 (tramp-send-command
1341 v
1342 (format "%s -ild %s"
1343 (tramp-get-ls-command v)
1344 (tramp-shell-quote-argument localname)))
1345 (setq attr (buffer-substring (point)
1346 (progn (end-of-line) (point)))))
1347 (tramp-set-file-property
1348 v localname "visited-file-modtime-ild" attr))
1349 (when (boundp 'last-coding-system-used)
1350 (set 'last-coding-system-used coding-system-used))
1351 nil)))))
1352
1353;; This function makes the same assumption as
4a93e698
MA
1354;; `tramp-sh-handle-set-visited-file-modtime'.
1355(defun tramp-sh-handle-verify-visited-file-modtime (buf)
03c1ad43
MA
1356 "Like `verify-visited-file-modtime' for Tramp files.
1357At the time `verify-visited-file-modtime' calls this function, we
1358already know that the buffer is visiting a file and that
1359`visited-file-modtime' does not return 0. Do not call this
1360function directly, unless those two cases are already taken care
1361of."
1362 (with-current-buffer buf
1363 (let ((f (buffer-file-name)))
1364 ;; There is no file visiting the buffer, or the buffer has no
1365 ;; recorded last modification time, or there is no established
1366 ;; connection.
1367 (if (or (not f)
1368 (eq (visited-file-modtime) 0)
1369 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1370 t
1371 (with-parsed-tramp-file-name f nil
4bc3c53d
MA
1372 (let* ((remote-file-name-inhibit-cache t)
1373 (attr (file-attributes f))
03c1ad43
MA
1374 (modtime (nth 5 attr))
1375 (mt (visited-file-modtime)))
1376
1377 (cond
1378 ;; File exists, and has a known modtime.
1379 ((and attr (not (equal modtime '(0 0))))
1380 (< (abs (tramp-time-diff
1381 modtime
1382 ;; For compatibility, deal with both the old
1383 ;; (HIGH . LOW) and the new (HIGH LOW) return
1384 ;; values of `visited-file-modtime'.
1385 (if (atom (cdr mt))
1386 (list (car mt) (cdr mt))
1387 mt)))
1388 2))
1389 ;; Modtime has the don't know value.
1390 (attr
1391 (tramp-send-command
1392 v
1393 (format "%s -ild %s"
1394 (tramp-get-ls-command v)
1395 (tramp-shell-quote-argument localname)))
1396 (with-current-buffer (tramp-get-buffer v)
1397 (setq attr (buffer-substring
1398 (point) (progn (end-of-line) (point)))))
1399 (equal
1400 attr
1401 (tramp-get-file-property
1402 v localname "visited-file-modtime-ild" "")))
1403 ;; If file does not exist, say it is not modified if and
1404 ;; only if that agrees with the buffer's record.
1405 (t (equal mt '(-1 65535))))))))))
1406
4a93e698 1407(defun tramp-sh-handle-set-file-modes (filename mode)
03c1ad43
MA
1408 "Like `set-file-modes' for Tramp files."
1409 (with-parsed-tramp-file-name filename nil
1410 (tramp-flush-file-property v localname)
1411 ;; FIXME: extract the proper text from chmod's stderr.
1412 (tramp-barf-unless-okay
1413 v
1414 (format "chmod %s %s"
1415 (tramp-compat-decimal-to-octal mode)
1416 (tramp-shell-quote-argument localname))
1417 "Error while changing file's mode %s" filename)))
1418
4a93e698 1419(defun tramp-sh-handle-set-file-times (filename &optional time)
03c1ad43
MA
1420 "Like `set-file-times' for Tramp files."
1421 (if (file-remote-p filename)
1422 (with-parsed-tramp-file-name filename nil
1423 (tramp-flush-file-property v localname)
1424 (let ((time (if (or (null time) (equal time '(0 0)))
1425 (current-time)
1426 time))
1427 ;; With GNU Emacs, `format-time-string' has an optional
1428 ;; parameter UNIVERSAL. This is preferred, because we
1429 ;; could handle the case when the remote host is located
1430 ;; in a different time zone as the local host.
1431 (utc (not (featurep 'xemacs))))
1432 (tramp-send-command-and-check
1433 v (format "%s touch -t %s %s"
1434 (if utc "TZ=UTC; export TZ;" "")
1435 (if utc
1436 (format-time-string "%Y%m%d%H%M.%S" time t)
1437 (format-time-string "%Y%m%d%H%M.%S" time))
1438 (tramp-shell-quote-argument localname)))))
1439
1440 ;; We handle also the local part, because in older Emacsen,
1441 ;; without `set-file-times', this function is an alias for this.
1442 ;; We are local, so we don't need the UTC settings.
1443 (zerop
1444 (tramp-compat-call-process
1445 "touch" nil nil nil "-t"
1446 (format-time-string "%Y%m%d%H%M.%S" time)
1447 (tramp-shell-quote-argument filename)))))
1448
1449(defun tramp-set-file-uid-gid (filename &optional uid gid)
1450 "Set the ownership for FILENAME.
1451If UID and GID are provided, these values are used; otherwise uid
1452and gid of the corresponding user is taken. Both parameters must be integers."
1453 ;; Modern Unices allow chown only for root. So we might need
1454 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1455 ;; working with su(do)? when it is needed, so it shall succeed in
1456 ;; the majority of cases.
1457 ;; Don't modify `last-coding-system-used' by accident.
1458 (let ((last-coding-system-used last-coding-system-used))
1459 (if (file-remote-p filename)
1460 (with-parsed-tramp-file-name filename nil
1461 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1462 ;; If we are root on the local host, we can do it directly.
1463 (tramp-set-file-uid-gid localname uid gid)
1464 (let ((uid (or (and (integerp uid) uid)
1465 (tramp-get-remote-uid v 'integer)))
1466 (gid (or (and (integerp gid) gid)
1467 (tramp-get-remote-gid v 'integer))))
1468 (tramp-send-command
1469 v (format
1470 "chown %d:%d %s" uid gid
1471 (tramp-shell-quote-argument localname))))))
1472
1473 ;; We handle also the local part, because there doesn't exist
1474 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1475 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
1476 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
1477 (tramp-compat-call-process
1478 "chown" nil nil nil
1479 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1480
1481(defun tramp-remote-selinux-p (vec)
1482 "Check, whether SELINUX is enabled on the remote host."
1d51f99c
MA
1483 (with-tramp-connection-property
1484 (tramp-get-connection-process vec) "selinux-p"
03c1ad43
MA
1485 (let ((result (tramp-find-executable
1486 vec "getenforce" (tramp-get-remote-path vec) t t)))
1487 (and result
1488 (string-equal
1489 (tramp-send-command-and-read
1490 vec (format "echo \\\"`%S`\\\"" result))
1491 "Enforcing")))))
1492
4a93e698 1493(defun tramp-sh-handle-file-selinux-context (filename)
03c1ad43
MA
1494 "Like `file-selinux-context' for Tramp files."
1495 (with-parsed-tramp-file-name filename nil
1d51f99c 1496 (with-tramp-file-property v localname "file-selinux-context"
03c1ad43
MA
1497 (let ((context '(nil nil nil nil))
1498 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1499 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1500 (when (and (tramp-remote-selinux-p v)
1501 (tramp-send-command-and-check
1502 v (format
1503 "%s -d -Z %s"
1504 (tramp-get-ls-command v)
1505 (tramp-shell-quote-argument localname))))
1506 (with-current-buffer (tramp-get-connection-buffer v)
1507 (goto-char (point-min))
6e060cee 1508 (when (re-search-forward regexp (point-at-eol) t)
03c1ad43
MA
1509 (setq context (list (match-string 1) (match-string 2)
1510 (match-string 3) (match-string 4))))))
1511 ;; Return the context.
1512 context))))
1513
4a93e698 1514(defun tramp-sh-handle-set-file-selinux-context (filename context)
03c1ad43
MA
1515 "Like `set-file-selinux-context' for Tramp files."
1516 (with-parsed-tramp-file-name filename nil
1517 (if (and (consp context)
1518 (tramp-remote-selinux-p v)
1519 (tramp-send-command-and-check
1520 v (format "chcon %s %s %s %s %s"
1521 (if (stringp (nth 0 context))
1522 (format "--user=%s" (nth 0 context)) "")
1523 (if (stringp (nth 1 context))
1524 (format "--role=%s" (nth 1 context)) "")
1525 (if (stringp (nth 2 context))
1526 (format "--type=%s" (nth 2 context)) "")
1527 (if (stringp (nth 3 context))
1528 (format "--range=%s" (nth 3 context)) "")
1529 (tramp-shell-quote-argument localname))))
1530 (tramp-set-file-property v localname "file-selinux-context" context)
1531 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
1532 ;; We always return nil.
1533 nil)
1534
1535;; Simple functions using the `test' command.
1536
4a93e698 1537(defun tramp-sh-handle-file-executable-p (filename)
03c1ad43
MA
1538 "Like `file-executable-p' for Tramp files."
1539 (with-parsed-tramp-file-name filename nil
1d51f99c 1540 (with-tramp-file-property v localname "file-executable-p"
03c1ad43
MA
1541 ;; Examine `file-attributes' cache to see if request can be
1542 ;; satisfied without remote operation.
1543 (or (tramp-check-cached-permissions v ?x)
1544 (tramp-run-test "-x" filename)))))
1545
4a93e698 1546(defun tramp-sh-handle-file-readable-p (filename)
03c1ad43
MA
1547 "Like `file-readable-p' for Tramp files."
1548 (with-parsed-tramp-file-name filename nil
1d51f99c 1549 (with-tramp-file-property v localname "file-readable-p"
03c1ad43
MA
1550 ;; Examine `file-attributes' cache to see if request can be
1551 ;; satisfied without remote operation.
1552 (or (tramp-check-cached-permissions v ?r)
1553 (tramp-run-test "-r" filename)))))
1554
1555;; When the remote shell is started, it looks for a shell which groks
1556;; tilde expansion. Here, we assume that all shells which grok tilde
1557;; expansion will also provide a `test' command which groks `-nt' (for
1558;; newer than). If this breaks, tell me about it and I'll try to do
1559;; something smarter about it.
4a93e698 1560(defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
03c1ad43
MA
1561 "Like `file-newer-than-file-p' for Tramp files."
1562 (cond ((not (file-exists-p file1))
1563 nil)
1564 ((not (file-exists-p file2))
1565 t)
1566 ;; We are sure both files exist at this point.
1567 (t
1568 (save-excursion
1569 ;; We try to get the mtime of both files. If they are not
1570 ;; equal to the "dont-know" value, then we subtract the times
1571 ;; and obtain the result.
1572 (let ((fa1 (file-attributes file1))
1573 (fa2 (file-attributes file2)))
1574 (if (and (not (equal (nth 5 fa1) '(0 0)))
1575 (not (equal (nth 5 fa2) '(0 0))))
1576 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1577 ;; If one of them is the dont-know value, then we can
1578 ;; still try to run a shell command on the remote host.
1579 ;; However, this only works if both files are Tramp
1580 ;; files and both have the same method, same user, same
1581 ;; host.
1582 (unless (tramp-equal-remote file1 file2)
1583 (with-parsed-tramp-file-name
1584 (if (tramp-tramp-file-p file1) file1 file2) nil
1585 (tramp-error
1586 v 'file-error
1587 "Files %s and %s must have same method, user, host"
1588 file1 file2)))
1589 (with-parsed-tramp-file-name file1 nil
1590 (tramp-run-test2
1591 (tramp-get-test-nt-command v) file1 file2))))))))
1592
1593;; Functions implemented using the basic functions above.
1594
4a93e698 1595(defun tramp-sh-handle-file-directory-p (filename)
03c1ad43 1596 "Like `file-directory-p' for Tramp files."
03c1ad43 1597 (with-parsed-tramp-file-name filename nil
3f04efd6
MA
1598 ;; `file-directory-p' is used as predicate for filename completion.
1599 ;; Sometimes, when a connection is not established yet, it is
1600 ;; desirable to return t immediately for "/method:foo:". It can
1601 ;; be expected that this is always a directory.
1602 (or (zerop (length localname))
1d51f99c 1603 (with-tramp-file-property v localname "file-directory-p"
3f04efd6 1604 (tramp-run-test "-d" filename)))))
03c1ad43 1605
4a93e698 1606(defun tramp-sh-handle-file-writable-p (filename)
03c1ad43
MA
1607 "Like `file-writable-p' for Tramp files."
1608 (with-parsed-tramp-file-name filename nil
1d51f99c 1609 (with-tramp-file-property v localname "file-writable-p"
03c1ad43
MA
1610 (if (file-exists-p filename)
1611 ;; Examine `file-attributes' cache to see if request can be
1612 ;; satisfied without remote operation.
1613 (or (tramp-check-cached-permissions v ?w)
1614 (tramp-run-test "-w" filename))
1615 ;; If file doesn't exist, check if directory is writable.
1616 (and (tramp-run-test "-d" (file-name-directory filename))
1617 (tramp-run-test "-w" (file-name-directory filename)))))))
1618
97976f9f 1619(defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
03c1ad43
MA
1620 "Like `file-ownership-preserved-p' for Tramp files."
1621 (with-parsed-tramp-file-name filename nil
1d51f99c 1622 (with-tramp-file-property v localname "file-ownership-preserved-p"
03c1ad43
MA
1623 (let ((attributes (file-attributes filename)))
1624 ;; Return t if the file doesn't exist, since it's true that no
1625 ;; information would be lost by an (attempted) delete and create.
1626 (or (null attributes)
97976f9f
PE
1627 (and
1628 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1629 (or (not group)
1630 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
03c1ad43 1631
03c1ad43
MA
1632;; Directory listings.
1633
4a93e698 1634(defun tramp-sh-handle-directory-files-and-attributes
03c1ad43
MA
1635 (directory &optional full match nosort id-format)
1636 "Like `directory-files-and-attributes' for Tramp files."
1637 (unless id-format (setq id-format 'integer))
1638 (when (file-directory-p directory)
1639 (setq directory (expand-file-name directory))
1640 (let* ((temp
1641 (copy-tree
1642 (with-parsed-tramp-file-name directory nil
1d51f99c 1643 (with-tramp-file-property
03c1ad43
MA
1644 v localname
1645 (format "directory-files-and-attributes-%s" id-format)
1646 (save-excursion
1647 (mapcar
1648 (lambda (x)
1649 (cons (car x)
1650 (tramp-convert-file-attributes v (cdr x))))
1651 (cond
1652 ((tramp-get-remote-stat v)
1653 (tramp-do-directory-files-and-attributes-with-stat
1654 v localname id-format))
1655 ((tramp-get-remote-perl v)
1656 (tramp-do-directory-files-and-attributes-with-perl
1657 v localname id-format)))))))))
1658 result item)
1659
1660 (while temp
1661 (setq item (pop temp))
1662 (when (or (null match) (string-match match (car item)))
1663 (when full
1664 (setcar item (expand-file-name (car item) directory)))
1665 (push item result)))
1666
1667 (if nosort
1668 result
1669 (sort result (lambda (x y) (string< (car x) (car y))))))))
1670
1671(defun tramp-do-directory-files-and-attributes-with-perl
1672 (vec localname &optional id-format)
1673 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1674 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1675 (tramp-maybe-send-script
1676 vec tramp-perl-directory-files-and-attributes
1677 "tramp_perl_directory_files_and_attributes")
1678 (let ((object
1679 (tramp-send-command-and-read
1680 vec
1681 (format "tramp_perl_directory_files_and_attributes %s %s"
1682 (tramp-shell-quote-argument localname) id-format))))
1683 (when (stringp object) (tramp-error vec 'file-error object))
1684 object))
1685
1686(defun tramp-do-directory-files-and-attributes-with-stat
1687 (vec localname &optional id-format)
1688 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1689 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1690 (tramp-send-command-and-read
1691 vec
1692 (format
1693 (concat
1694 ;; We must care about filenames with spaces, or starting with
1695 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1696 ;; but it does not work on all remote systems. Therefore, we
1697 ;; quote the filenames via sed.
957b3189
MA
1698 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1699 "xargs %s -c "
1700 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1701 " 2>/dev/null); echo \")\"")
03c1ad43
MA
1702 (tramp-shell-quote-argument localname)
1703 (tramp-get-ls-command vec)
1704 (tramp-get-remote-stat vec)
2fe4b125
MA
1705 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1706 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
03c1ad43
MA
1707
1708;; This function should return "foo/" for directories and "bar" for
1709;; files.
4a93e698 1710(defun tramp-sh-handle-file-name-all-completions (filename directory)
03c1ad43
MA
1711 "Like `file-name-all-completions' for Tramp files."
1712 (unless (save-match-data (string-match "/" filename))
1713 (with-parsed-tramp-file-name (expand-file-name directory) nil
1714
1715 (all-completions
1716 filename
1717 (mapcar
1718 'list
1719 (or
4bc3c53d
MA
1720 ;; Try cache entries for filename, filename with last
1721 ;; character removed, filename with last two characters
1722 ;; removed, ..., and finally the empty string - all
1723 ;; concatenated to the local directory name.
1724 (let ((remote-file-name-inhibit-cache
1725 (or remote-file-name-inhibit-cache
1726 tramp-completion-reread-directory-timeout)))
1727
1728 ;; This is inefficient for very long filenames, pity
1729 ;; `reduce' is not available...
1730 (car
1731 (apply
1732 'append
1733 (mapcar
1734 (lambda (x)
1735 (let ((cache-hit
1736 (tramp-get-file-property
1737 v
1738 (concat localname (substring filename 0 x))
1739 "file-name-all-completions"
1740 nil)))
1741 (when cache-hit (list cache-hit))))
91683089
MA
1742 ;; We cannot use a length of 0, because file properties
1743 ;; for "foo" and "foo/" are identical.
1744 (tramp-compat-number-sequence (length filename) 1 -1)))))
03c1ad43
MA
1745
1746 ;; Cache expired or no matching cache entry found so we need
4bc3c53d 1747 ;; to perform a remote operation.
03c1ad43
MA
1748 (let (result)
1749 ;; Get a list of directories and files, including reliably
1750 ;; tagging the directories with a trailing '/'. Because I
1751 ;; rock. --daniel@danann.net
1752
1753 ;; Changed to perform `cd' in the same remote op and only
4bc3c53d 1754 ;; get entries starting with `filename'. Capture any `cd'
03c1ad43
MA
1755 ;; error messages. Ensure any `cd' and `echo' aliases are
1756 ;; ignored.
1757 (tramp-send-command
1758 v
1759 (if (tramp-get-remote-perl v)
1760 (progn
1761 (tramp-maybe-send-script
1762 v tramp-perl-file-name-all-completions
1763 "tramp_perl_file_name_all_completions")
1764 (format "tramp_perl_file_name_all_completions %s %s %d"
1765 (tramp-shell-quote-argument localname)
1766 (tramp-shell-quote-argument filename)
1767 (if (symbol-value
1768 ;; `read-file-name-completion-ignore-case'
1769 ;; is introduced with Emacs 22.1.
1770 (if (boundp
1771 'read-file-name-completion-ignore-case)
1772 'read-file-name-completion-ignore-case
1773 'completion-ignore-case))
1774 1 0)))
1775
1776 (format (concat
1777 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1778 ;; `ls' with wildcard might fail with `Argument
1779 ;; list too long' error in some corner cases; if
1780 ;; `ls' fails after `cd' succeeded, chances are
1781 ;; that's the case, so let's retry without
1782 ;; wildcard. This will return "too many" entries
1783 ;; but that isn't harmful.
1784 " || %s -a 2>/dev/null)"
1785 " | while read f; do"
1786 " if %s -d \"$f\" 2>/dev/null;"
1787 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1788 " && \\echo ok) || \\echo fail")
1789 (tramp-shell-quote-argument localname)
1790 (tramp-get-ls-command v)
1791 ;; When `filename' is empty, just `ls' without
1792 ;; filename argument is more efficient than `ls *'
1793 ;; for very large directories and might avoid the
1794 ;; `Argument list too long' error.
1795 ;;
1796 ;; With and only with wildcard, we need to add
1797 ;; `-d' to prevent `ls' from descending into
1798 ;; sub-directories.
1799 (if (zerop (length filename))
1800 "."
1801 (concat (tramp-shell-quote-argument filename) "* -d"))
1802 (tramp-get-ls-command v)
1803 (tramp-get-test-command v))))
1804
1805 ;; Now grab the output.
1806 (with-current-buffer (tramp-get-buffer v)
1807 (goto-char (point-max))
1808
91683089 1809 ;; Check result code, found in last line of output.
03c1ad43
MA
1810 (forward-line -1)
1811 (if (looking-at "^fail$")
1812 (progn
1813 ;; Grab error message from line before last line
91683089 1814 ;; (it was put there by `cd 2>&1').
03c1ad43
MA
1815 (forward-line -1)
1816 (tramp-error
1817 v 'file-error
4a93e698 1818 "tramp-sh-handle-file-name-all-completions: %s"
6e060cee 1819 (buffer-substring (point) (point-at-eol))))
03c1ad43
MA
1820 ;; For peace of mind, if buffer doesn't end in `fail'
1821 ;; then it should end in `ok'. If neither are in the
1822 ;; buffer something went seriously wrong on the remote
1823 ;; side.
1824 (unless (looking-at "^ok$")
1825 (tramp-error
1826 v 'file-error
1827 "\
4a93e698 1828tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
03c1ad43
MA
1829 (tramp-shell-quote-argument localname) (buffer-string))))
1830
1831 (while (zerop (forward-line -1))
6e060cee 1832 (push (buffer-substring (point) (point-at-eol)) result)))
03c1ad43
MA
1833
1834 ;; Because the remote op went through OK we know the
91683089
MA
1835 ;; directory we `cd'-ed to exists.
1836 (tramp-set-file-property v localname "file-exists-p" t)
03c1ad43
MA
1837
1838 ;; Because the remote op went through OK we know every
1839 ;; file listed by `ls' exists.
1840 (mapc (lambda (entry)
1841 (tramp-set-file-property
1842 v (concat localname entry) "file-exists-p" t))
1843 result)
1844
91683089 1845 ;; Store result in the cache.
03c1ad43
MA
1846 (tramp-set-file-property
1847 v (concat localname filename)
91683089 1848 "file-name-all-completions" result))))))))
03c1ad43 1849
03c1ad43
MA
1850;; cp, mv and ln
1851
4a93e698 1852(defun tramp-sh-handle-add-name-to-file
03c1ad43
MA
1853 (filename newname &optional ok-if-already-exists)
1854 "Like `add-name-to-file' for Tramp files."
1855 (unless (tramp-equal-remote filename newname)
1856 (with-parsed-tramp-file-name
1857 (if (tramp-tramp-file-p filename) filename newname) nil
1858 (tramp-error
1859 v 'file-error
1860 "add-name-to-file: %s"
1861 "only implemented for same method, same user, same host")))
1862 (with-parsed-tramp-file-name filename v1
1863 (with-parsed-tramp-file-name newname v2
1864 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1865 (when (and (not ok-if-already-exists)
1866 (file-exists-p newname)
1867 (not (numberp ok-if-already-exists))
1868 (y-or-n-p
1869 (format
1870 "File %s already exists; make it a new name anyway? "
1871 newname)))
1872 (tramp-error
1873 v2 'file-error
1874 "add-name-to-file: file %s already exists" newname))
1875 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1876 (tramp-flush-file-property v2 v2-localname)
1877 (tramp-barf-unless-okay
1878 v1
1879 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1880 (tramp-shell-quote-argument v2-localname))
1881 "error with add-name-to-file, see buffer `%s' for details"
1882 (buffer-name))))))
1883
4a93e698 1884(defun tramp-sh-handle-copy-file
03c1ad43
MA
1885 (filename newname &optional ok-if-already-exists keep-date
1886 preserve-uid-gid preserve-selinux-context)
1887 "Like `copy-file' for Tramp files."
1888 (setq filename (expand-file-name filename))
1889 (setq newname (expand-file-name newname))
1890 (cond
1891 ;; At least one file a Tramp file?
1892 ((or (tramp-tramp-file-p filename)
1893 (tramp-tramp-file-p newname))
1894 (tramp-do-copy-or-rename-file
1895 'copy filename newname ok-if-already-exists keep-date
1896 preserve-uid-gid preserve-selinux-context))
1897 ;; Compat section.
1898 (preserve-selinux-context
1899 (tramp-run-real-handler
1900 'copy-file
1901 (list filename newname ok-if-already-exists keep-date
1902 preserve-uid-gid preserve-selinux-context)))
1903 (preserve-uid-gid
1904 (tramp-run-real-handler
1905 'copy-file
1906 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1907 (t
1908 (tramp-run-real-handler
1909 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1910
4a93e698 1911(defun tramp-sh-handle-copy-directory
a3fcfa99 1912 (dirname newname &optional keep-date parents copy-contents)
03c1ad43
MA
1913 "Like `copy-directory' for Tramp files."
1914 (let ((t1 (tramp-tramp-file-p dirname))
1915 (t2 (tramp-tramp-file-p newname)))
1916 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1917 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1918 ;; When DIRNAME and NEWNAME are remote, they must have
1919 ;; the same method.
1920 (or (null t1) (null t2)
1921 (string-equal
1922 (tramp-file-name-method (tramp-dissect-file-name dirname))
1923 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1924 ;; scp or rsync DTRT.
1925 (progn
1926 (setq dirname (directory-file-name (expand-file-name dirname))
1927 newname (directory-file-name (expand-file-name newname)))
1928 (if (and (file-directory-p newname)
1929 (not (string-equal (file-name-nondirectory dirname)
1930 (file-name-nondirectory newname))))
1931 (setq newname
1932 (expand-file-name
1933 (file-name-nondirectory dirname) newname)))
1934 (if (not (file-directory-p (file-name-directory newname)))
1935 (make-directory (file-name-directory newname) parents))
1936 (tramp-do-copy-or-rename-file-out-of-band
1937 'copy dirname newname keep-date))
1938 ;; We must do it file-wise.
1939 (tramp-run-real-handler
1940 'copy-directory (list dirname newname keep-date parents)))
1941
1942 ;; When newname did exist, we have wrong cached values.
1943 (when t2
1944 (with-parsed-tramp-file-name newname nil
1945 (tramp-flush-file-property v (file-name-directory localname))
1946 (tramp-flush-file-property v localname))))))
1947
4a93e698 1948(defun tramp-sh-handle-rename-file
03c1ad43
MA
1949 (filename newname &optional ok-if-already-exists)
1950 "Like `rename-file' for Tramp files."
1951 ;; Check if both files are local -- invoke normal rename-file.
1952 ;; Otherwise, use Tramp from local system.
1953 (setq filename (expand-file-name filename))
1954 (setq newname (expand-file-name newname))
1955 ;; At least one file a Tramp file?
1956 (if (or (tramp-tramp-file-p filename)
1957 (tramp-tramp-file-p newname))
1958 (tramp-do-copy-or-rename-file
1959 'rename filename newname ok-if-already-exists t t)
1960 (tramp-run-real-handler
1961 'rename-file (list filename newname ok-if-already-exists))))
1962
1963(defun tramp-do-copy-or-rename-file
1964 (op filename newname &optional ok-if-already-exists keep-date
1965 preserve-uid-gid preserve-selinux-context)
1966 "Copy or rename a remote file.
1967OP must be `copy' or `rename' and indicates the operation to perform.
1968FILENAME specifies the file to copy or rename, NEWNAME is the name of
1969the new file (for copy) or the new name of the file (for rename).
1970OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1971KEEP-DATE means to make sure that NEWNAME has the same timestamp
1972as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1973the uid and gid if both files are on the same host.
1974PRESERVE-SELINUX-CONTEXT activates selinux commands.
1975
4a93e698
MA
1976This function is invoked by `tramp-sh-handle-copy-file' and
1977`tramp-sh-handle-rename-file'. It is an error if OP is neither
1978of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1979file names."
03c1ad43
MA
1980 (unless (memq op '(copy rename))
1981 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1982 (let ((t1 (tramp-tramp-file-p filename))
1983 (t2 (tramp-tramp-file-p newname))
d7f2a65c 1984 (length (nth 7 (file-attributes (file-truename filename))))
03c1ad43
MA
1985 (context (and preserve-selinux-context
1986 (apply 'file-selinux-context (list filename))))
1987 pr tm)
1988
1989 (with-parsed-tramp-file-name (if t1 filename newname) nil
1990 (when (and (not ok-if-already-exists) (file-exists-p newname))
1991 (tramp-error
1992 v 'file-already-exists "File %s already exists" newname))
1993
1d51f99c 1994 (with-tramp-progress-reporter
03c1ad43
MA
1995 v 0 (format "%s %s to %s"
1996 (if (eq op 'copy) "Copying" "Renaming")
1997 filename newname)
1998
1999 (cond
2000 ;; Both are Tramp files.
2001 ((and t1 t2)
2002 (with-parsed-tramp-file-name filename v1
2003 (with-parsed-tramp-file-name newname v2
2004 (cond
2005 ;; Shortcut: if method, host, user are the same for
2006 ;; both files, we invoke `cp' or `mv' on the remote
2007 ;; host directly.
2008 ((tramp-equal-remote filename newname)
2009 (tramp-do-copy-or-rename-file-directly
2010 op filename newname
2011 ok-if-already-exists keep-date preserve-uid-gid))
2012
2013 ;; Try out-of-band operation.
d7f2a65c
MA
2014 ((and
2015 (tramp-method-out-of-band-p v1 length)
2016 (tramp-method-out-of-band-p v2 length))
03c1ad43
MA
2017 (tramp-do-copy-or-rename-file-out-of-band
2018 op filename newname keep-date))
2019
2020 ;; No shortcut was possible. So we copy the file
2021 ;; first. If the operation was `rename', we go back
2022 ;; and delete the original file (if the copy was
2023 ;; successful). The approach is simple-minded: we
2024 ;; create a new buffer, insert the contents of the
2025 ;; source file into it, then write out the buffer to
2026 ;; the target file. The advantage is that it doesn't
2027 ;; matter which filename handlers are used for the
2028 ;; source and target file.
2029 (t
2030 (tramp-do-copy-or-rename-file-via-buffer
2031 op filename newname keep-date))))))
2032
2033 ;; One file is a Tramp file, the other one is local.
2034 ((or t1 t2)
2035 (cond
2036 ;; Fast track on local machine.
2037 ((tramp-local-host-p v)
2038 (tramp-do-copy-or-rename-file-directly
2039 op filename newname
2040 ok-if-already-exists keep-date preserve-uid-gid))
2041
2042 ;; If the Tramp file has an out-of-band method, the
2043 ;; corresponding copy-program can be invoked.
d7f2a65c 2044 ((tramp-method-out-of-band-p v length)
03c1ad43
MA
2045 (tramp-do-copy-or-rename-file-out-of-band
2046 op filename newname keep-date))
2047
2048 ;; Use the inline method via a Tramp buffer.
2049 (t (tramp-do-copy-or-rename-file-via-buffer
2050 op filename newname keep-date))))
2051
2052 (t
2053 ;; One of them must be a Tramp file.
2054 (error "Tramp implementation says this cannot happen")))
2055
2056 ;; Handle `preserve-selinux-context'.
2057 (when context (apply 'set-file-selinux-context (list newname context)))
2058
2059 ;; In case of `rename', we must flush the cache of the source file.
2060 (when (and t1 (eq op 'rename))
2061 (with-parsed-tramp-file-name filename v1
2062 (tramp-flush-file-property v1 (file-name-directory localname))
2063 (tramp-flush-file-property v1 localname)))
2064
2065 ;; When newname did exist, we have wrong cached values.
2066 (when t2
2067 (with-parsed-tramp-file-name newname v2
2068 (tramp-flush-file-property v2 (file-name-directory localname))
2069 (tramp-flush-file-property v2 localname)))))))
2070
2071(defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2072 "Use an Emacs buffer to copy or rename a file.
2073First arg OP is either `copy' or `rename' and indicates the operation.
2074FILENAME is the source file, NEWNAME the target file.
2075KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2076 (with-temp-buffer
2077 ;; We must disable multibyte, because binary data shall not be
2078 ;; converted.
2079 (set-buffer-multibyte nil)
2080 (let ((coding-system-for-read 'binary)
2081 (jka-compr-inhibit t))
2082 (insert-file-contents-literally filename))
2083 ;; We don't want the target file to be compressed, so we let-bind
2084 ;; `jka-compr-inhibit' to t.
2085 (let ((coding-system-for-write 'binary)
2086 (jka-compr-inhibit t))
2087 (write-region (point-min) (point-max) newname)))
2088 ;; KEEP-DATE handling.
2089 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2090 ;; Set the mode.
2091 (set-file-modes newname (tramp-default-file-modes filename))
2092 ;; If the operation was `rename', delete the original file.
2093 (unless (eq op 'copy) (delete-file filename)))
2094
2095(defun tramp-do-copy-or-rename-file-directly
2096 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2097 "Invokes `cp' or `mv' on the remote system.
2098OP must be one of `copy' or `rename', indicating `cp' or `mv',
2099respectively. FILENAME specifies the file to copy or rename,
2100NEWNAME is the name of the new file (for copy) or the new name of
2101the file (for rename). Both files must reside on the same host.
2102KEEP-DATE means to make sure that NEWNAME has the same timestamp
2103as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2104the uid and gid from FILENAME."
2105 (let ((t1 (tramp-tramp-file-p filename))
2106 (t2 (tramp-tramp-file-p newname))
2107 (file-times (nth 5 (file-attributes filename)))
2108 (file-modes (tramp-default-file-modes filename)))
2109 (with-parsed-tramp-file-name (if t1 filename newname) nil
2110 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2111 ((eq op 'copy) "cp -f")
2112 ((eq op 'rename) "mv -f")
2113 (t (tramp-error
2114 v 'file-error
2115 "Unknown operation `%s', must be `copy' or `rename'"
2116 op))))
2117 (localname1
2118 (if t1
2119 (tramp-file-name-handler 'file-remote-p filename 'localname)
2120 filename))
2121 (localname2
2122 (if t2
2123 (tramp-file-name-handler 'file-remote-p newname 'localname)
2124 newname))
2125 (prefix (file-remote-p (if t1 filename newname)))
2126 cmd-result)
2127
2128 (cond
2129 ;; Both files are on a remote host, with same user.
2130 ((and t1 t2)
2131 (setq cmd-result
2132 (tramp-send-command-and-check
2133 v (format "%s %s %s" cmd
2134 (tramp-shell-quote-argument localname1)
2135 (tramp-shell-quote-argument localname2))))
2136 (with-current-buffer (tramp-get-buffer v)
2137 (goto-char (point-min))
2138 (unless
2139 (or
2140 (and keep-date
2141 ;; Mask cp -f error.
2142 (re-search-forward
2143 tramp-operation-not-permitted-regexp nil t))
2144 cmd-result)
2145 (tramp-error-with-buffer
2146 nil v 'file-error
2147 "Copying directly failed, see buffer `%s' for details."
2148 (buffer-name)))))
2149
2150 ;; We are on the local host.
2151 ((or t1 t2)
2152 (cond
2153 ;; We can do it directly.
2154 ((let (file-name-handler-alist)
2155 (and (file-readable-p localname1)
2156 (file-writable-p (file-name-directory localname2))
2157 (or (file-directory-p localname2)
2158 (file-writable-p localname2))))
2159 (if (eq op 'copy)
2160 (tramp-compat-copy-file
2161 localname1 localname2 ok-if-already-exists
2162 keep-date preserve-uid-gid)
2163 (tramp-run-real-handler
2164 'rename-file (list localname1 localname2 ok-if-already-exists))))
2165
2166 ;; We can do it directly with `tramp-send-command'
2167 ((and (file-readable-p (concat prefix localname1))
2168 (file-writable-p
2169 (file-name-directory (concat prefix localname2)))
2170 (or (file-directory-p (concat prefix localname2))
2171 (file-writable-p (concat prefix localname2))))
2172 (tramp-do-copy-or-rename-file-directly
2173 op (concat prefix localname1) (concat prefix localname2)
2174 ok-if-already-exists keep-date t)
2175 ;; We must change the ownership to the local user.
2176 (tramp-set-file-uid-gid
2177 (concat prefix localname2)
2178 (tramp-get-local-uid 'integer)
2179 (tramp-get-local-gid 'integer)))
2180
2181 ;; We need a temporary file in between.
2182 (t
2183 ;; Create the temporary file.
2184 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2185 (unwind-protect
2186 (progn
2187 (cond
2188 (t1
2189 (tramp-barf-unless-okay
2190 v (format
2191 "%s %s %s" cmd
2192 (tramp-shell-quote-argument localname1)
2193 (tramp-shell-quote-argument tmpfile))
2194 "Copying directly failed, see buffer `%s' for details."
2195 (tramp-get-buffer v))
2196 ;; We must change the ownership as remote user.
2197 ;; Since this does not work reliable, we also
2198 ;; give read permissions.
2199 (set-file-modes
2200 (concat prefix tmpfile)
2201 (tramp-compat-octal-to-decimal "0777"))
2202 (tramp-set-file-uid-gid
2203 (concat prefix tmpfile)
2204 (tramp-get-local-uid 'integer)
2205 (tramp-get-local-gid 'integer)))
2206 (t2
2207 (if (eq op 'copy)
2208 (tramp-compat-copy-file
2209 localname1 tmpfile t
2210 keep-date preserve-uid-gid)
2211 (tramp-run-real-handler
2212 'rename-file
2213 (list localname1 tmpfile t)))
2214 ;; We must change the ownership as local user.
2215 ;; Since this does not work reliable, we also
2216 ;; give read permissions.
2217 (set-file-modes
2218 tmpfile (tramp-compat-octal-to-decimal "0777"))
2219 (tramp-set-file-uid-gid
2220 tmpfile
2221 (tramp-get-remote-uid v 'integer)
2222 (tramp-get-remote-gid v 'integer))))
2223
2224 ;; Move the temporary file to its destination.
2225 (cond
2226 (t2
2227 (tramp-barf-unless-okay
2228 v (format
2229 "cp -f -p %s %s"
2230 (tramp-shell-quote-argument tmpfile)
2231 (tramp-shell-quote-argument localname2))
2232 "Copying directly failed, see buffer `%s' for details."
2233 (tramp-get-buffer v)))
2234 (t1
2235 (tramp-run-real-handler
2236 'rename-file
2237 (list tmpfile localname2 ok-if-already-exists)))))
2238
2239 ;; Save exit.
7398933f 2240 (ignore-errors (delete-file tmpfile)))))))))
03c1ad43
MA
2241
2242 ;; Set the time and mode. Mask possible errors.
7398933f 2243 (ignore-errors
03c1ad43
MA
2244 (when keep-date
2245 (set-file-times newname file-times)
7398933f 2246 (set-file-modes newname file-modes))))))
03c1ad43
MA
2247
2248(defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2249 "Invoke rcp program to copy.
2250The method used must be an out-of-band method."
66feec8b
MA
2251 (let* ((t1 (tramp-tramp-file-p filename))
2252 (t2 (tramp-tramp-file-p newname))
2253 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2254 copy-program copy-args copy-env copy-keep-date port spec
2255 source target)
03c1ad43
MA
2256
2257 (with-parsed-tramp-file-name (if t1 filename newname) nil
2258 (if (and t1 t2)
2259
2c68ca0e 2260 ;; Both are Tramp files. We shall optimize it when the
03c1ad43
MA
2261 ;; methods for filename and newname are the same.
2262 (let* ((dir-flag (file-directory-p filename))
2263 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2264 (if dir-flag
2265 (setq tmpfile
2266 (expand-file-name
2267 (file-name-nondirectory newname) tmpfile)))
2268 (unwind-protect
2269 (progn
2270 (tramp-do-copy-or-rename-file-out-of-band
2271 op filename tmpfile keep-date)
2272 (tramp-do-copy-or-rename-file-out-of-band
2273 'rename tmpfile newname keep-date))
2274 ;; Save exit.
7398933f
MA
2275 (ignore-errors
2276 (if dir-flag
2277 (tramp-compat-delete-directory
2278 (expand-file-name ".." tmpfile) 'recursive)
2279 (delete-file tmpfile)))))
03c1ad43 2280
66feec8b
MA
2281 ;; Set variables for computing the prompt for reading
2282 ;; password.
2283 (setq tramp-current-method (tramp-file-name-method v)
a5509865
MA
2284 tramp-current-user (or (tramp-file-name-user v)
2285 (tramp-get-connection-property
2286 v "login-as" nil))
2287 tramp-current-host (tramp-file-name-real-host v))
66feec8b 2288
03c1ad43
MA
2289 ;; Expand hops. Might be necessary for gateway methods.
2290 (setq v (car (tramp-compute-multi-hops v)))
2291 (aset v 3 localname)
2292
2293 ;; Check which ones of source and target are Tramp files.
2294 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2295 target (funcall
2296 (if (and (file-directory-p filename)
2297 (string-equal
2298 (file-name-nondirectory filename)
2299 (file-name-nondirectory newname)))
2300 'file-name-directory
2301 'identity)
2302 (if t2 (tramp-make-copy-program-file-name v) newname)))
2303
ee545c35
MA
2304 ;; Check for host and port number. We cannot use
2305 ;; `tramp-file-name-port', because this returns also
2306 ;; `tramp-default-port', which might clash with settings in
2307 ;; "~/.ssh/config".
2308 (setq host (tramp-file-name-host v)
2309 port "")
2310 (when (string-match tramp-host-with-port-regexp host)
23c22e9a
MA
2311 (setq port (string-to-number (match-string 2 host))
2312 host (string-to-number (match-string 1 host))))
03c1ad43 2313
a5509865
MA
2314 ;; Check for user. There might be an interactive setting.
2315 (setq user (or (tramp-file-name-user v)
2316 (tramp-get-connection-property v "login-as" nil)))
2317
03c1ad43 2318 ;; Compose copy command.
a5509865
MA
2319 (setq host (or host "")
2320 user (or user "")
2321 port (or port "")
2322 spec (format-spec-make
03c1ad43
MA
2323 ?h host ?u user ?p port
2324 ?t (tramp-get-connection-property
2325 (tramp-get-connection-process v) "temp-file" "")
2326 ?k (if keep-date " " ""))
2327 copy-program (tramp-get-method-parameter
2328 method 'tramp-copy-program)
2329 copy-keep-date (tramp-get-method-parameter
2330 method 'tramp-copy-keep-date)
2331 copy-args
66feec8b
MA
2332 (delete
2333 ;; " " has either been a replacement of "%k" (when
ee545c35 2334 ;; keep-date argument is non-nil), or a replacement
66feec8b
MA
2335 ;; for the whole keep-date sublist.
2336 " "
2337 (dolist
2338 (x
2339 (tramp-get-method-parameter method 'tramp-copy-args)
2340 copy-args)
2341 (setq copy-args
2342 (append
2343 copy-args
2344 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
ee545c35 2345 (if (member "" y) '(" ") y))))))
03c1ad43
MA
2346 copy-env
2347 (delq
2348 nil
2349 (mapcar
2350 (lambda (x)
2351 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2352 (unless (member "" x) (mapconcat 'identity x " ")))
2353 (tramp-get-method-parameter method 'tramp-copy-env))))
2354
2355 ;; Check for program.
f42efeb5
MA
2356 (unless (let ((default-directory
2357 (tramp-compat-temporary-file-directory)))
2358 (executable-find copy-program))
03c1ad43
MA
2359 (tramp-error
2360 v 'file-error "Cannot find copy program: %s" copy-program))
2361
66feec8b
MA
2362 (with-temp-buffer
2363 (unwind-protect
03c1ad43
MA
2364 ;; The default directory must be remote.
2365 (let ((default-directory
2366 (file-name-directory (if t1 filename newname)))
2367 (process-environment (copy-sequence process-environment)))
2368 ;; Set the transfer process properties.
2369 (tramp-set-connection-property
2370 v "process-name" (buffer-name (current-buffer)))
2371 (tramp-set-connection-property
2372 v "process-buffer" (current-buffer))
2373 (while copy-env
66feec8b
MA
2374 (tramp-message
2375 orig-vec 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
03c1ad43
MA
2376 (setenv (pop copy-env) (pop copy-env)))
2377
2378 ;; Use an asynchronous process. By this, password can
2379 ;; be handled. The default directory must be local, in
2380 ;; order to apply the correct `copy-program'. We don't
2381 ;; set a timeout, because the copying of large files can
2382 ;; last longer than 60 secs.
2383 (let ((p (let ((default-directory
2384 (tramp-compat-temporary-file-directory)))
d5f97f62 2385 (apply 'start-process-shell-command
66feec8b
MA
2386 (tramp-get-connection-name v)
2387 (tramp-get-connection-buffer v)
03c1ad43 2388 copy-program
d5f97f62
MA
2389 (append
2390 copy-args
2391 (list
2392 (shell-quote-argument source)
82176fcd
MA
2393 (shell-quote-argument target)
2394 "&&" "echo" "tramp_exit_status" "0"
2395 "||" "echo" "tramp_exit_status" "1"))))))
03c1ad43 2396 (tramp-message
66feec8b
MA
2397 orig-vec 6 "%s"
2398 (mapconcat 'identity (process-command p) " "))
bd8fadca 2399 (tramp-compat-set-process-query-on-exit-flag p nil)
bfd31217 2400 (tramp-process-actions
d5f97f62
MA
2401 p v nil tramp-actions-copy-out-of-band)
2402
82176fcd
MA
2403 ;; Check the return code.
2404 (goto-char (point-max))
2405 (unless
2406 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2407 (tramp-error
2408 orig-vec 'file-error
2409 "Couldn't find exit status of `%s'" (process-command p)))
2410 (skip-chars-forward "^ ")
2411 (unless (zerop (read (current-buffer)))
2412 (forward-line -1)
2413 (tramp-error
2414 orig-vec 'file-error
2415 "Error copying: `%s'"
2416 (buffer-substring (point-min) (point-at-eol))))))
03c1ad43 2417
66feec8b 2418 ;; Reset the transfer process properties.
2fe4b125 2419 (tramp-message orig-vec 6 "\n%s" (buffer-string))
66feec8b
MA
2420 (tramp-set-connection-property v "process-name" nil)
2421 (tramp-set-connection-property v "process-buffer" nil)))
03c1ad43
MA
2422
2423 ;; Handle KEEP-DATE argument.
2424 (when (and keep-date (not copy-keep-date))
2425 (set-file-times newname (nth 5 (file-attributes filename))))
2426
2427 ;; Set the mode.
2428 (unless (and keep-date copy-keep-date)
2429 (ignore-errors
2430 (set-file-modes newname (tramp-default-file-modes filename)))))
2431
2432 ;; If the operation was `rename', delete the original file.
2433 (unless (eq op 'copy)
2434 (if (file-regular-p filename)
2435 (delete-file filename)
2436 (tramp-compat-delete-directory filename 'recursive))))))
2437
4a93e698 2438(defun tramp-sh-handle-make-directory (dir &optional parents)
03c1ad43
MA
2439 "Like `make-directory' for Tramp files."
2440 (setq dir (expand-file-name dir))
2441 (with-parsed-tramp-file-name dir nil
2442 (tramp-flush-directory-property v (file-name-directory localname))
2443 (save-excursion
2444 (tramp-barf-unless-okay
2445 v (format "%s %s"
2446 (if parents "mkdir -p" "mkdir")
2447 (tramp-shell-quote-argument localname))
2448 "Couldn't make directory %s" dir))))
2449
4a93e698 2450(defun tramp-sh-handle-delete-directory (directory &optional recursive)
03c1ad43
MA
2451 "Like `delete-directory' for Tramp files."
2452 (setq directory (expand-file-name directory))
2453 (with-parsed-tramp-file-name directory nil
2454 (tramp-flush-file-property v (file-name-directory localname))
2455 (tramp-flush-directory-property v localname)
2456 (tramp-barf-unless-okay
2457 v (format "%s %s"
2458 (if recursive "rm -rf" "rmdir")
2459 (tramp-shell-quote-argument localname))
2460 "Couldn't delete %s" directory)))
2461
4a93e698 2462(defun tramp-sh-handle-delete-file (filename &optional trash)
03c1ad43
MA
2463 "Like `delete-file' for Tramp files."
2464 (setq filename (expand-file-name filename))
2465 (with-parsed-tramp-file-name filename nil
2466 (tramp-flush-file-property v (file-name-directory localname))
2467 (tramp-flush-file-property v localname)
2468 (tramp-barf-unless-okay
2469 v (format "%s %s"
2470 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2471 (tramp-shell-quote-argument localname))
2472 "Couldn't delete %s" filename)))
2473
2474;; Dired.
2475
2476;; CCC: This does not seem to be enough. Something dies when
2477;; we try and delete two directories under Tramp :/
4a93e698 2478(defun tramp-sh-handle-dired-recursive-delete-directory (filename)
03c1ad43
MA
2479 "Recursively delete the directory given.
2480This is like `dired-recursive-delete-directory' for Tramp files."
2481 (with-parsed-tramp-file-name filename nil
2fe4b125 2482 ;; Run a shell command 'rm -r <localname>'.
03c1ad43
MA
2483 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2484 (unless (file-exists-p filename)
2485 (tramp-error v 'file-error "No such directory: %s" filename))
2fe4b125 2486 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
03c1ad43
MA
2487 (tramp-send-command
2488 v
2489 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2c68ca0e 2490 ;; Don't read the output, do it explicitly.
03c1ad43
MA
2491 nil t)
2492 ;; Wait for the remote system to return to us...
2493 ;; This might take a while, allow it plenty of time.
2494 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2495 ;; Make sure that it worked...
2496 (tramp-flush-file-property v (file-name-directory localname))
2497 (tramp-flush-directory-property v localname)
2498 (and (file-exists-p filename)
2499 (tramp-error
2500 v 'file-error "Failed to recursively delete %s" filename))))
2501
4a93e698 2502(defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
03c1ad43
MA
2503 "Like `dired-compress-file' for Tramp files."
2504 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2505 ;; Code stolen mainly from dired-aux.el.
2506 (with-parsed-tramp-file-name file nil
2507 (tramp-flush-file-property v localname)
2508 (save-excursion
2509 (let ((suffixes
2510 (if (not (featurep 'xemacs))
2511 ;; Emacs case
2512 (symbol-value 'dired-compress-file-suffixes)
2513 ;; XEmacs has `dired-compression-method-alist', which is
2514 ;; transformed into `dired-compress-file-suffixes' structure.
2515 (mapcar
2516 (lambda (x)
2517 (list (concat (regexp-quote (nth 1 x)) "\\'")
2518 nil
2519 (mapconcat 'identity (nth 3 x) " ")))
2520 (symbol-value 'dired-compression-method-alist))))
2521 suffix)
2522 ;; See if any suffix rule matches this file name.
2523 (while suffixes
2524 (let (case-fold-search)
2525 (if (string-match (car (car suffixes)) localname)
2526 (setq suffix (car suffixes) suffixes nil))
2527 (setq suffixes (cdr suffixes))))
2528
2529 (cond ((file-symlink-p file)
2530 nil)
2531 ((and suffix (nth 2 suffix))
2532 ;; We found an uncompression rule.
1d51f99c 2533 (with-tramp-progress-reporter
7d520089 2534 v 0 (format "Uncompressing %s" file)
03c1ad43
MA
2535 (when (tramp-send-command-and-check
2536 v (concat (nth 2 suffix) " "
2537 (tramp-shell-quote-argument localname)))
2538 ;; `dired-remove-file' is not defined in XEmacs.
2539 (tramp-compat-funcall 'dired-remove-file file)
2540 (string-match (car suffix) file)
2541 (concat (substring file 0 (match-beginning 0))))))
2542 (t
2543 ;; We don't recognize the file as compressed, so compress it.
2544 ;; Try gzip.
1d51f99c 2545 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
03c1ad43
MA
2546 (when (tramp-send-command-and-check
2547 v (concat "gzip -f "
2548 (tramp-shell-quote-argument localname)))
2549 ;; `dired-remove-file' is not defined in XEmacs.
2550 (tramp-compat-funcall 'dired-remove-file file)
2551 (cond ((file-exists-p (concat file ".gz"))
2552 (concat file ".gz"))
2553 ((file-exists-p (concat file ".z"))
2554 (concat file ".z"))
2555 (t nil))))))))))
2556
4a93e698 2557(defun tramp-sh-handle-insert-directory
03c1ad43
MA
2558 (filename switches &optional wildcard full-directory-p)
2559 "Like `insert-directory' for Tramp files."
2560 (setq filename (expand-file-name filename))
2561 (with-parsed-tramp-file-name filename nil
2562 (if (and (featurep 'ls-lisp)
2563 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2564 (tramp-run-real-handler
2565 'insert-directory (list filename switches wildcard full-directory-p))
2566 (when (stringp switches)
2567 (setq switches (split-string switches)))
2568 (when (and (member "--dired" switches)
2569 (not (tramp-get-ls-command-with-dired v)))
2570 (setq switches (delete "--dired" switches)))
2571 (when wildcard
2572 (setq wildcard (tramp-run-real-handler
2573 'file-name-nondirectory (list localname)))
2574 (setq localname (tramp-run-real-handler
2575 'file-name-directory (list localname))))
2576 (unless full-directory-p
2577 (setq switches (add-to-list 'switches "-d" 'append)))
2578 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2579 (when wildcard
2580 (setq switches (concat switches " " wildcard)))
2581 (tramp-message
2582 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2583 switches filename (if wildcard "yes" "no")
2584 (if full-directory-p "yes" "no"))
2585 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2586 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2587 (if full-directory-p
2588 (tramp-send-command
2589 v
2590 (format "%s %s %s 2>/dev/null"
2591 (tramp-get-ls-command v)
2592 switches
2593 (if wildcard
2594 localname
2595 (tramp-shell-quote-argument (concat localname ".")))))
2596 (tramp-barf-unless-okay
2597 v
2598 (format "cd %s" (tramp-shell-quote-argument
2599 (tramp-run-real-handler
2600 'file-name-directory (list localname))))
2601 "Couldn't `cd %s'"
2602 (tramp-shell-quote-argument
2603 (tramp-run-real-handler 'file-name-directory (list localname))))
2604 (tramp-send-command
2605 v
2606 (format "%s %s %s"
2607 (tramp-get-ls-command v)
2608 switches
2609 (if (or wildcard
2610 (zerop (length
2611 (tramp-run-real-handler
2612 'file-name-nondirectory (list localname)))))
2613 ""
2614 (tramp-shell-quote-argument
2615 (tramp-run-real-handler
2616 'file-name-nondirectory (list localname)))))))
2617 (let ((beg (point)))
2618 ;; We cannot use `insert-buffer-substring' because the Tramp
2619 ;; buffer changes its contents before insertion due to calling
2620 ;; `expand-file' and alike.
2621 (insert
2622 (with-current-buffer (tramp-get-buffer v)
2623 (buffer-string)))
2624
2625 ;; Check for "--dired" output.
2626 (forward-line -2)
2627 (when (looking-at "//SUBDIRED//")
2628 (forward-line -1))
2629 (when (looking-at "//DIRED//\\s-+")
2630 (let ((databeg (match-end 0))
6e060cee 2631 (end (point-at-eol)))
03c1ad43
MA
2632 ;; Now read the numeric positions of file names.
2633 (goto-char databeg)
2634 (while (< (point) end)
2635 (let ((start (+ beg (read (current-buffer))))
2636 (end (+ beg (read (current-buffer)))))
2637 (if (memq (char-after end) '(?\n ?\ ))
2638 ;; End is followed by \n or by " -> ".
2639 (put-text-property start end 'dired-filename t))))))
2640 ;; Remove trailing lines.
6e060cee 2641 (goto-char (point-at-bol))
03c1ad43
MA
2642 (while (looking-at "//")
2643 (forward-line 1)
2644 (delete-region (match-beginning 0) (point)))
2645
6d95bd46
MA
2646 ;; Some busyboxes are reluctant to discard colors.
2647 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2648 (goto-char beg)
2649 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2650 (replace-match "")))
2651
03c1ad43
MA
2652 ;; The inserted file could be from somewhere else.
2653 (when (and (not wildcard) (not full-directory-p))
2654 (goto-char (point-max))
2655 (when (file-symlink-p filename)
2656 (goto-char (search-backward "->" beg 'noerror)))
2657 (search-backward
2658 (if (zerop (length (file-name-nondirectory filename)))
2659 "."
2660 (file-name-nondirectory filename))
2661 beg 'noerror)
2662 (replace-match (file-relative-name filename) t))
2663
2664 (goto-char (point-max))))))
2665
03c1ad43
MA
2666;; Canonicalization of file names.
2667
4a93e698 2668(defun tramp-sh-handle-expand-file-name (name &optional dir)
03c1ad43
MA
2669 "Like `expand-file-name' for Tramp files.
2670If the localname part of the given filename starts with \"/../\" then
2671the result will be a local, non-Tramp, filename."
2672 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2673 (setq dir (or dir default-directory "/"))
2674 ;; Unless NAME is absolute, concat DIR and NAME.
2675 (unless (file-name-absolute-p name)
2676 (setq name (concat (file-name-as-directory dir) name)))
2677 ;; If NAME is not a Tramp file, run the real handler.
2678 (if (not (tramp-connectable-p name))
2679 (tramp-run-real-handler 'expand-file-name (list name nil))
2680 ;; Dissect NAME.
2681 (with-parsed-tramp-file-name name nil
2682 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2683 (setq localname (concat "~/" localname)))
2684 ;; Tilde expansion if necessary. This needs a shell which
2685 ;; groks tilde expansion! The function `tramp-find-shell' is
2686 ;; supposed to find such a shell on the remote host. Please
2687 ;; tell me about it when this doesn't work on your system.
2688 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2689 (let ((uname (match-string 1 localname))
2690 (fname (match-string 2 localname)))
2691 ;; We cannot simply apply "~/", because under sudo "~/" is
2692 ;; expanded to the local user home directory but to the
2693 ;; root home directory. On the other hand, using always
2694 ;; the default user name for tilde expansion is not
2695 ;; appropriate either, because ssh and companions might
2696 ;; use a user name from the config file.
2697 (when (and (string-equal uname "~")
2698 (string-match "\\`su\\(do\\)?\\'" method))
2699 (setq uname (concat uname user)))
2700 (setq uname
1d51f99c 2701 (with-tramp-connection-property v uname
03c1ad43
MA
2702 (tramp-send-command
2703 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2704 (with-current-buffer (tramp-get-buffer v)
2705 (goto-char (point-min))
6e060cee 2706 (buffer-substring (point) (point-at-eol)))))
03c1ad43
MA
2707 (setq localname (concat uname fname))))
2708 ;; There might be a double slash, for example when "~/"
2709 ;; expands to "/". Remove this.
2710 (while (string-match "//" localname)
2711 (setq localname (replace-match "/" t t localname)))
2712 ;; No tilde characters in file name, do normal
2713 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2714 ;; `directory-sep-char' here for XEmacs on Windows, which would
2715 ;; otherwise use backslash. `default-directory' is bound,
2716 ;; because on Windows there would be problems with UNC shares or
2717 ;; Cygwin mounts.
2718 (let ((directory-sep-char ?/)
2719 (default-directory (tramp-compat-temporary-file-directory)))
2720 (tramp-make-tramp-file-name
2721 method user host
2722 (tramp-drop-volume-letter
2723 (tramp-run-real-handler
2fe4b125
MA
2724 'expand-file-name (list localname)))
2725 hop)))))
03c1ad43 2726
03c1ad43
MA
2727;;; Remote commands:
2728
4a93e698 2729(defun tramp-sh-handle-executable-find (command)
03c1ad43
MA
2730 "Like `executable-find' for Tramp files."
2731 (with-parsed-tramp-file-name default-directory nil
2732 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2733
2734(defun tramp-process-sentinel (proc event)
2735 "Flush file caches."
2736 (unless (memq (process-status proc) '(run open))
2737 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2738 (when vec
2739 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
6d95bd46 2740 (tramp-flush-connection-property proc)
03c1ad43
MA
2741 (tramp-flush-directory-property vec "")))))
2742
2743;; We use BUFFER also as connection buffer during setup. Because of
2744;; this, its original contents must be saved, and restored once
2745;; connection has been setup.
4a93e698 2746(defun tramp-sh-handle-start-file-process (name buffer program &rest args)
03c1ad43
MA
2747 "Like `start-file-process' for Tramp files."
2748 (with-parsed-tramp-file-name default-directory nil
f49d1f52
SM
2749 ;; When PROGRAM is nil, we just provide a tty.
2750 (let ((command
2751 (when (stringp program)
afae1d68 2752 (format "cd %s; exec env PS1=%s %s"
f49d1f52 2753 (tramp-shell-quote-argument localname)
afae1d68
MA
2754 ;; Use a human-friendly prompt, for example for `shell'.
2755 (tramp-shell-quote-argument
2756 (format "%s %s"
2757 (file-remote-p default-directory)
2758 tramp-initial-end-of-output))
f49d1f52
SM
2759 (mapconcat 'tramp-shell-quote-argument
2760 (cons program args) " "))))
2761 (tramp-process-connection-type
2762 (or (null program) tramp-process-connection-type))
2763 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2764 (name1 name)
2765 (i 0))
4a6bc3fd
MA
2766
2767 (unless buffer
2768 ;; BUFFER can be nil. We use a temporary buffer.
2769 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2770 (while (get-process name1)
2771 ;; NAME must be unique as process name.
2772 (setq i (1+ i)
2773 name1 (format "%s<%d>" name i)))
2774 (setq name name1)
2775 ;; Set the new process properties.
2776 (tramp-set-connection-property v "process-name" name)
2777 (tramp-set-connection-property v "process-buffer" buffer)
2778
2779 (with-current-buffer (tramp-get-connection-buffer v)
2780 (unwind-protect
2781 (save-excursion
2782 (save-restriction
2783 ;; Activate narrowing in order to save BUFFER
2784 ;; contents. Clear also the modification time;
2785 ;; otherwise we might be interrupted by
2786 ;; `verify-visited-file-modtime'.
2787 (let ((buffer-undo-list t)
2788 (buffer-read-only nil)
2789 (mark (point)))
f49d1f52
SM
2790 (clear-visited-file-modtime)
2791 (narrow-to-region (point-max) (point-max))
4a6bc3fd
MA
2792 ;; We call `tramp-maybe-open-connection', in order
2793 ;; to cleanup the prompt afterwards.
2794 (tramp-maybe-open-connection v)
2795 (widen)
2796 (delete-region mark (point))
2797 (narrow-to-region (point-max) (point-max))
2798 ;; Now do it.
f49d1f52
SM
2799 (if command
2800 ;; Send the command.
d68b0220
MA
2801 (tramp-send-command v command nil t) ; nooutput
2802 ;; Check, whether a pty is associated.
d68b0220
MA
2803 (unless (tramp-compat-process-get
2804 (tramp-get-connection-process v) 'remote-tty)
2805 (tramp-error
f49d1f52 2806 v 'file-error
4a6bc3fd
MA
2807 "pty association is not supported for `%s'" name))))
2808 (let ((p (tramp-get-connection-process v)))
2809 ;; Set query flag for this process. We ignore errors,
2810 ;; because the process could have finished already.
2811 (ignore-errors
2812 (tramp-compat-set-process-query-on-exit-flag p t))
2813 ;; Return process.
2814 p)))
2815
2816 ;; Save exit.
f49d1f52
SM
2817 (if (string-match tramp-temp-buffer-name (buffer-name))
2818 (progn
2819 (set-process-buffer (tramp-get-connection-process v) nil)
2820 (kill-buffer (current-buffer)))
4a6bc3fd
MA
2821 (set-buffer-modified-p bmp))
2822 (tramp-set-connection-property v "process-name" nil)
2823 (tramp-set-connection-property v "process-buffer" nil))))))
03c1ad43 2824
4a93e698 2825(defun tramp-sh-handle-process-file
03c1ad43
MA
2826 (program &optional infile destination display &rest args)
2827 "Like `process-file' for Tramp files."
2828 ;; The implementation is not complete yet.
2829 (when (and (numberp destination) (zerop destination))
2830 (error "Implementation does not handle immediate return"))
2831
2832 (with-parsed-tramp-file-name default-directory nil
2833 (let (command input tmpinput stderr tmpstderr outbuf ret)
2834 ;; Compute command.
2835 (setq command (mapconcat 'tramp-shell-quote-argument
2836 (cons program args) " "))
2837 ;; Determine input.
2838 (if (null infile)
2839 (setq input "/dev/null")
2840 (setq infile (expand-file-name infile))
2841 (if (tramp-equal-remote default-directory infile)
2842 ;; INFILE is on the same remote host.
2843 (setq input (with-parsed-tramp-file-name infile nil localname))
2844 ;; INFILE must be copied to remote host.
2845 (setq input (tramp-make-tramp-temp-file v)
2846 tmpinput (tramp-make-tramp-file-name method user host input))
2847 (copy-file infile tmpinput t)))
2848 (when input (setq command (format "%s <%s" command input)))
2849
2850 ;; Determine output.
2851 (cond
2852 ;; Just a buffer.
2853 ((bufferp destination)
2854 (setq outbuf destination))
2855 ;; A buffer name.
2856 ((stringp destination)
2857 (setq outbuf (get-buffer-create destination)))
2858 ;; (REAL-DESTINATION ERROR-DESTINATION)
2859 ((consp destination)
2860 ;; output.
2861 (cond
2862 ((bufferp (car destination))
2863 (setq outbuf (car destination)))
2864 ((stringp (car destination))
2865 (setq outbuf (get-buffer-create (car destination))))
2866 ((car destination)
2867 (setq outbuf (current-buffer))))
2868 ;; stderr.
2869 (cond
2870 ((stringp (cadr destination))
2871 (setcar (cdr destination) (expand-file-name (cadr destination)))
2872 (if (tramp-equal-remote default-directory (cadr destination))
2873 ;; stderr is on the same remote host.
2874 (setq stderr (with-parsed-tramp-file-name
2875 (cadr destination) nil localname))
2876 ;; stderr must be copied to remote host. The temporary
2877 ;; file must be deleted after execution.
2878 (setq stderr (tramp-make-tramp-temp-file v)
2879 tmpstderr (tramp-make-tramp-file-name
2880 method user host stderr))))
2881 ;; stderr to be discarded.
2882 ((null (cadr destination))
2883 (setq stderr "/dev/null"))))
2884 ;; 't
2885 (destination
2886 (setq outbuf (current-buffer))))
2887 (when stderr (setq command (format "%s 2>%s" command stderr)))
2888
2889 ;; Send the command. It might not return in time, so we protect
2890 ;; it. Call it in a subshell, in order to preserve working
2891 ;; directory.
2892 (condition-case nil
2893 (unwind-protect
2894 (setq ret
2895 (if (tramp-send-command-and-check
2896 v (format "\\cd %s; %s"
2897 (tramp-shell-quote-argument localname)
2898 command)
2899 t t)
2900 0 1))
2901 ;; We should show the output anyway.
2902 (when outbuf
2903 (with-current-buffer outbuf
2904 (insert
2905 (with-current-buffer (tramp-get-connection-buffer v)
2906 (buffer-string))))
2907 (when display (display-buffer outbuf))))
2908 ;; When the user did interrupt, we should do it also. We use
2909 ;; return code -1 as marker.
2910 (quit
2911 (kill-buffer (tramp-get-connection-buffer v))
2912 (setq ret -1))
2913 ;; Handle errors.
2914 (error
2915 (kill-buffer (tramp-get-connection-buffer v))
2916 (setq ret 1)))
2917
2918 ;; Provide error file.
2919 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2920
2921 ;; Cleanup. We remove all file cache values for the connection,
2922 ;; because the remote process could have changed them.
2923 (when tmpinput (delete-file tmpinput))
2924
2925 ;; `process-file-side-effects' has been introduced with GNU
2926 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2927 ;; by `program'. If it doesn't exist, we assume its default
f5e29b9b 2928 ;; value `t'.
03c1ad43
MA
2929 (unless (and (boundp 'process-file-side-effects)
2930 (not (symbol-value 'process-file-side-effects)))
2931 (tramp-flush-directory-property v ""))
2932
2933 ;; Return exit status.
2934 (if (equal ret -1)
2935 (keyboard-quit)
2936 ret))))
2937
4a93e698 2938(defun tramp-sh-handle-file-local-copy (filename)
03c1ad43 2939 "Like `file-local-copy' for Tramp files."
03c1ad43
MA
2940 (with-parsed-tramp-file-name filename nil
2941 (unless (file-exists-p filename)
2942 (tramp-error
2943 v 'file-error
2944 "Cannot make local copy of non-existing file `%s'" filename))
2945
66feec8b 2946 (let* ((size (nth 7 (file-attributes (file-truename filename))))
03c1ad43
MA
2947 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2948 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2949 (tmpfile (tramp-compat-make-temp-file filename)))
2950
2951 (condition-case err
2952 (cond
2953 ;; `copy-file' handles direct copy and out-of-band methods.
2954 ((or (tramp-local-host-p v)
2955 (tramp-method-out-of-band-p v size))
2956 (copy-file filename tmpfile t t))
2957
2958 ;; Use inline encoding for file transfer.
2959 (rem-enc
2960 (save-excursion
1d51f99c 2961 (with-tramp-progress-reporter
03c1ad43
MA
2962 v 3 (format "Encoding remote file %s" filename)
2963 (tramp-barf-unless-okay
2964 v (format rem-enc (tramp-shell-quote-argument localname))
2965 "Encoding remote file failed"))
2966
2967 (if (functionp loc-dec)
2968 ;; If local decoding is a function, we call it. We
2969 ;; must disable multibyte, because
2970 ;; `uudecode-decode-region' doesn't handle it
2971 ;; correctly.
2972 (with-temp-buffer
2973 (set-buffer-multibyte nil)
2974 (insert-buffer-substring (tramp-get-buffer v))
1d51f99c 2975 (with-tramp-progress-reporter
03c1ad43
MA
2976 v 3 (format "Decoding remote file %s with function %s"
2977 filename loc-dec)
2978 (funcall loc-dec (point-min) (point-max))
2979 ;; Unset `file-name-handler-alist'. Otherwise,
2980 ;; epa-file gets confused.
2981 (let (file-name-handler-alist
2982 (coding-system-for-write 'binary))
2983 (write-region (point-min) (point-max) tmpfile))))
2984
2985 ;; If tramp-decoding-function is not defined for this
2986 ;; method, we invoke tramp-decoding-command instead.
2987 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
2988 ;; Unset `file-name-handler-alist'. Otherwise,
2989 ;; epa-file gets confused.
2990 (let (file-name-handler-alist
2991 (coding-system-for-write 'binary))
2992 (write-region (point-min) (point-max) tmpfile2))
1d51f99c 2993 (with-tramp-progress-reporter
03c1ad43
MA
2994 v 3 (format "Decoding remote file %s with command %s"
2995 filename loc-dec)
2996 (unwind-protect
2997 (tramp-call-local-coding-command
2998 loc-dec tmpfile2 tmpfile)
2999 (delete-file tmpfile2)))))
3000
3001 ;; Set proper permissions.
3002 (set-file-modes tmpfile (tramp-default-file-modes filename))
3003 ;; Set local user ownership.
3004 (tramp-set-file-uid-gid tmpfile)))
3005
3006 ;; Oops, I don't know what to do.
3007 (t (tramp-error
3008 v 'file-error "Wrong method specification for `%s'" method)))
3009
3010 ;; Error handling.
3011 ((error quit)
3012 (delete-file tmpfile)
3013 (signal (car err) (cdr err))))
3014
3015 (run-hooks 'tramp-handle-file-local-copy-hook)
3016 tmpfile)))
3017
03c1ad43 3018;; This is needed for XEmacs only. Code stolen from files.el.
4a93e698 3019(defun tramp-sh-handle-insert-file-contents-literally
03c1ad43
MA
3020 (filename &optional visit beg end replace)
3021 "Like `insert-file-contents-literally' for Tramp files."
3022 (let ((format-alist nil)
3023 (after-insert-file-functions nil)
3024 (coding-system-for-read 'no-conversion)
3025 (coding-system-for-write 'no-conversion)
3026 (find-buffer-file-type-function
3027 (if (fboundp 'find-buffer-file-type)
3028 (symbol-function 'find-buffer-file-type)
3029 nil))
3030 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3031 (inhibit-file-name-operation 'insert-file-contents))
3032 (unwind-protect
3033 (progn
3034 (fset 'find-buffer-file-type (lambda (filename) t))
3035 (insert-file-contents filename visit beg end replace))
3036 ;; Save exit.
3037 (if find-buffer-file-type-function
3038 (fset 'find-buffer-file-type find-buffer-file-type-function)
3039 (fmakunbound 'find-buffer-file-type)))))
3040
4a93e698 3041(defun tramp-sh-handle-make-auto-save-file-name ()
03c1ad43
MA
3042 "Like `make-auto-save-file-name' for Tramp files.
3043Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3044 (let ((tramp-auto-save-directory tramp-auto-save-directory)
3045 (buffer-file-name
3046 (tramp-subst-strs-in-string
3047 '(("_" . "|")
3048 ("/" . "_a")
3049 (":" . "_b")
3050 ("|" . "__")
3051 ("[" . "_l")
3052 ("]" . "_r"))
3053 (buffer-file-name))))
3054 ;; File name must be unique. This is ensured with Emacs 22 (see
3055 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3056 ;; all other cases we must do it ourselves.
3057 (when (boundp 'auto-save-file-name-transforms)
3058 (mapc
3059 (lambda (x)
3060 (when (and (string-match (car x) buffer-file-name)
3061 (not (car (cddr x))))
3062 (setq tramp-auto-save-directory
3063 (or tramp-auto-save-directory
3064 (tramp-compat-temporary-file-directory)))))
3065 (symbol-value 'auto-save-file-name-transforms)))
3066 ;; Create directory.
3067 (when tramp-auto-save-directory
3068 (setq buffer-file-name
3069 (expand-file-name buffer-file-name tramp-auto-save-directory))
3070 (unless (file-exists-p tramp-auto-save-directory)
3071 (make-directory tramp-auto-save-directory t)))
3072 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3073 ;; it is not a magic file name operation (since Emacs 22).
3074 ;; We must deactivate it temporarily.
3075 (if (not (ad-is-active 'make-auto-save-file-name))
3076 (tramp-run-real-handler 'make-auto-save-file-name nil)
3077 ;; else
3078 (ad-deactivate 'make-auto-save-file-name)
3079 (prog1
3080 (tramp-run-real-handler 'make-auto-save-file-name nil)
3081 (ad-activate 'make-auto-save-file-name)))))
3082
03c1ad43 3083;; CCC grok LOCKNAME
4a93e698 3084(defun tramp-sh-handle-write-region
03c1ad43
MA
3085 (start end filename &optional append visit lockname confirm)
3086 "Like `write-region' for Tramp files."
3087 (setq filename (expand-file-name filename))
3088 (with-parsed-tramp-file-name filename nil
3089 ;; Following part commented out because we don't know what to do about
3090 ;; file locking, and it does not appear to be a problem to ignore it.
3091 ;; Ange-ftp ignores it, too.
3092 ;; (when (and lockname (stringp lockname))
3093 ;; (setq lockname (expand-file-name lockname)))
3094 ;; (unless (or (eq lockname nil)
3095 ;; (string= lockname filename))
3096 ;; (error
4a93e698 3097 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
03c1ad43
MA
3098
3099 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3100 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3101 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3102 (tramp-error v 'file-error "File not overwritten")))
3103
3104 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3105 (tramp-get-remote-uid v 'integer)))
3106 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3107 (tramp-get-remote-gid v 'integer))))
3108
3109 (if (and (tramp-local-host-p v)
3110 ;; `file-writable-p' calls `file-expand-file-name'. We
3111 ;; cannot use `tramp-run-real-handler' therefore.
3112 (let (file-name-handler-alist)
3113 (and
3114 (file-writable-p (file-name-directory localname))
3115 (or (file-directory-p localname)
3116 (file-writable-p localname)))))
3117 ;; Short track: if we are on the local host, we can run directly.
3118 (tramp-run-real-handler
3119 'write-region
3120 (list start end localname append 'no-message lockname confirm))
3121
0372ee92
MA
3122 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3123 ;; We use this to save the value of
3124 ;; `last-coding-system-used' after writing the tmp
3125 ;; file. At the end of the function, we set
3126 ;; `last-coding-system-used' to this saved value. This
3127 ;; way, any intermediary coding systems used while
3128 ;; talking to the remote shell or suchlike won't hose
3129 ;; this variable. This approach was snarfed from
3130 ;; ange-ftp.el.
3131 coding-system-used
3132 ;; Write region into a tmp file. This isn't really
3133 ;; needed if we use an encoding function, but currently
3134 ;; we use it always because this makes the logic
3135 ;; simpler. We must also set `temporary-file-directory',
3136 ;; because it could point to a remote directory.
3137 (temporary-file-directory
3138 (tramp-compat-temporary-file-directory))
3139 (tmpfile (or tramp-temp-buffer-file-name
3140 (tramp-compat-make-temp-file filename))))
03c1ad43
MA
3141
3142 ;; If `append' is non-nil, we copy the file locally, and let
3143 ;; the native `write-region' implementation do the job.
3144 (when append (copy-file filename tmpfile 'ok))
3145
3146 ;; We say `no-message' here because we don't want the
3147 ;; visited file modtime data to be clobbered from the temp
3148 ;; file. We call `set-visited-file-modtime' ourselves later
3149 ;; on. We must ensure that `file-coding-system-alist'
3150 ;; matches `tmpfile'.
3151 (let (file-name-handler-alist
3152 (file-coding-system-alist
3153 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3154 (condition-case err
3155 (tramp-run-real-handler
3156 'write-region
3157 (list start end tmpfile append 'no-message lockname confirm))
3158 ((error quit)
3159 (setq tramp-temp-buffer-file-name nil)
3160 (delete-file tmpfile)
3161 (signal (car err) (cdr err))))
3162
3163 ;; Now, `last-coding-system-used' has the right value. Remember it.
3164 (when (boundp 'last-coding-system-used)
3165 (setq coding-system-used
3166 (symbol-value 'last-coding-system-used))))
3167
3168 ;; The permissions of the temporary file should be set. If
3169 ;; filename does not exist (eq modes nil) it has been
3170 ;; renamed to the backup file. This case `save-buffer'
3171 ;; handles permissions.
2c68ca0e 3172 ;; Ensure that it is still readable.
03c1ad43
MA
3173 (when modes
3174 (set-file-modes
3175 tmpfile
3176 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3177
3178 ;; This is a bit lengthy due to the different methods
3179 ;; possible for file transfer. First, we check whether the
3180 ;; method uses an rcp program. If so, we call it.
3181 ;; Otherwise, both encoding and decoding command must be
3182 ;; specified. However, if the method _also_ specifies an
3183 ;; encoding function, then that is used for encoding the
3184 ;; contents of the tmp file.
3185 (let* ((size (nth 7 (file-attributes tmpfile)))
3186 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3187 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3188 (cond
3189 ;; `copy-file' handles direct copy and out-of-band methods.
3190 ((or (tramp-local-host-p v)
3191 (tramp-method-out-of-band-p v size))
3192 (if (and (not (stringp start))
3193 (= (or end (point-max)) (point-max))
3194 (= (or start (point-min)) (point-min))
3195 (tramp-get-method-parameter
3196 method 'tramp-copy-keep-tmpfile))
3197 (progn
3198 (setq tramp-temp-buffer-file-name tmpfile)
3199 (condition-case err
3200 ;; We keep the local file for performance
3201 ;; reasons, useful for "rsync".
3202 (copy-file tmpfile filename t)
3203 ((error quit)
3204 (setq tramp-temp-buffer-file-name nil)
3205 (delete-file tmpfile)
3206 (signal (car err) (cdr err)))))
3207 (setq tramp-temp-buffer-file-name nil)
3208 ;; Don't rename, in order to keep context in SELinux.
3209 (unwind-protect
3210 (copy-file tmpfile filename t)
3211 (delete-file tmpfile))))
3212
3213 ;; Use inline file transfer.
3214 (rem-dec
3215 ;; Encode tmpfile.
3216 (unwind-protect
3217 (with-temp-buffer
3218 (set-buffer-multibyte nil)
3219 ;; Use encoding function or command.
3220 (if (functionp loc-enc)
1d51f99c 3221 (with-tramp-progress-reporter
03c1ad43
MA
3222 v 3 (format "Encoding region using function `%s'"
3223 loc-enc)
3224 (let ((coding-system-for-read 'binary))
3225 (insert-file-contents-literally tmpfile))
3226 ;; The following `let' is a workaround for the
3227 ;; base64.el that comes with pgnus-0.84. If
3228 ;; both of the following conditions are
3229 ;; satisfied, it tries to write to a local
3230 ;; file in default-directory, but at this
3231 ;; point, default-directory is remote.
3232 ;; (`call-process-region' can't write to
3233 ;; remote files, it seems.) The file in
3234 ;; question is a tmp file anyway.
3235 (let ((default-directory
3236 (tramp-compat-temporary-file-directory)))
3237 (funcall loc-enc (point-min) (point-max))))
3238
1d51f99c 3239 (with-tramp-progress-reporter
03c1ad43
MA
3240 v 3 (format "Encoding region using command `%s'"
3241 loc-enc)
3242 (unless (zerop (tramp-call-local-coding-command
3243 loc-enc tmpfile t))
3244 (tramp-error
3245 v 'file-error
3246 (concat "Cannot write to `%s', "
3247 "local encoding command `%s' failed")
3248 filename loc-enc))))
3249
3250 ;; Send buffer into remote decoding command which
3251 ;; writes to remote file. Because this happens on
3252 ;; the remote host, we cannot use the function.
1d51f99c 3253 (with-tramp-progress-reporter
03c1ad43
MA
3254 v 3
3255 (format "Decoding region into remote file %s" filename)
3256 (goto-char (point-max))
3257 (unless (bolp) (newline))
3258 (tramp-send-command
3259 v
3260 (format
3261 (concat rem-dec " <<'EOF'\n%sEOF")
3262 (tramp-shell-quote-argument localname)
3263 (buffer-string)))
3264 (tramp-barf-unless-okay
3265 v nil
3266 "Couldn't write region to `%s', decode using `%s' failed"
3267 filename rem-dec)
3268 ;; When `file-precious-flag' is set, the region is
3269 ;; written to a temporary file. Check that the
3270 ;; checksum is equal to that from the local tmpfile.
3271 (when file-precious-flag
3272 (erase-buffer)
3273 (and
3274 ;; cksum runs locally, if possible.
3275 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3276 ;; cksum runs remotely.
3277 (tramp-send-command-and-check
3278 v
3279 (format
3280 "cksum <%s" (tramp-shell-quote-argument localname)))
3281 ;; ... they are different.
3282 (not
3283 (string-equal
3284 (buffer-string)
3285 (with-current-buffer (tramp-get-buffer v)
3286 (buffer-string))))
3287 (tramp-error
3288 v 'file-error
3289 (concat "Couldn't write region to `%s',"
3290 " decode using `%s' failed")
3291 filename rem-dec)))))
3292
3293 ;; Save exit.
3294 (delete-file tmpfile)))
3295
3296 ;; That's not expected.
3297 (t
3298 (tramp-error
3299 v 'file-error
3300 (concat "Method `%s' should specify both encoding and "
3301 "decoding command or an rcp program")
3302 method))))
3303
3304 ;; Make `last-coding-system-used' have the right value.
3305 (when coding-system-used
3306 (set 'last-coding-system-used coding-system-used))))
3307
3308 (tramp-flush-file-property v (file-name-directory localname))
3309 (tramp-flush-file-property v localname)
3310
3311 ;; We must protect `last-coding-system-used', now we have set it
3312 ;; to its correct value.
3313 (let (last-coding-system-used (need-chown t))
3314 ;; Set file modification time.
3315 (when (or (eq visit t) (stringp visit))
957b3189 3316 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
03c1ad43 3317 (set-visited-file-modtime
2c68ca0e 3318 ;; We must pass modtime explicitly, because filename can
03c1ad43
MA
3319 ;; be different from (buffer-file-name), f.e. if
3320 ;; `file-precious-flag' is set.
3321 (nth 5 file-attr))
957b3189
MA
3322 (when (and (= (nth 2 file-attr) uid)
3323 (= (nth 3 file-attr) gid))
03c1ad43
MA
3324 (setq need-chown nil))))
3325
3326 ;; Set the ownership.
3327 (when need-chown
3328 (tramp-set-file-uid-gid filename uid gid))
3329 (when (or (eq visit t) (null visit) (stringp visit))
3330 (tramp-message v 0 "Wrote %s" filename))
3331 (run-hooks 'tramp-handle-write-region-hook)))))
3332
3333(defvar tramp-vc-registered-file-names nil
3334 "List used to collect file names, which are checked during `vc-registered'.")
3335
3336;; VC backends check for the existence of various different special
3337;; files. This is very time consuming, because every single check
3338;; requires a remote command (the file cache must be invalidated).
3339;; Therefore, we apply a kind of optimization. We install the file
3340;; name handler `tramp-vc-file-name-handler', which does nothing but
3341;; remembers all file names for which `file-exists-p' or
3342;; `file-readable-p' has been applied. A first run of `vc-registered'
3343;; is performed. Afterwards, a script is applied for all collected
3344;; file names, using just one remote command. The result of this
3345;; script is used to fill the file cache with actual values. Now we
3346;; can reset the file name handlers, and we make a second run of
3347;; `vc-registered', which returns the expected result without sending
3348;; any other remote command.
4a93e698 3349(defun tramp-sh-handle-vc-registered (file)
03c1ad43 3350 "Like `vc-registered' for Tramp files."
6139f995 3351 (tramp-compat-with-temp-message ""
03c1ad43 3352 (with-parsed-tramp-file-name file nil
1d51f99c 3353 (with-tramp-progress-reporter
03c1ad43
MA
3354 v 3 (format "Checking `vc-registered' for %s" file)
3355
3356 ;; There could be new files, created by the vc backend. We
3357 ;; cannot reuse the old cache entries, therefore.
3358 (let (tramp-vc-registered-file-names
4bc3c53d 3359 (remote-file-name-inhibit-cache (current-time))
03c1ad43
MA
3360 (file-name-handler-alist
3361 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3362
3363 ;; Here we collect only file names, which need an operation.
957b3189 3364 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
03c1ad43
MA
3365 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3366
3367 ;; Send just one command, in order to fill the cache.
3368 (when tramp-vc-registered-file-names
3369 (tramp-maybe-send-script
3370 v
3371 (format tramp-vc-registered-read-file-names
3372 (tramp-get-file-exists-command v)
3373 (format "%s -r" (tramp-get-test-command v)))
3374 "tramp_vc_registered_read_file_names")
3375
3376 (dolist
3377 (elt
3378 (tramp-send-command-and-read
3379 v
3380 (format
3381 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3382 (mapconcat 'tramp-shell-quote-argument
3383 tramp-vc-registered-file-names
3384 "\n"))))
3385
3386 (tramp-set-file-property
3387 v (car elt) (cadr elt) (cadr (cdr elt))))))
3388
3389 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3390 ;; calls shall be answered from the file cache. We unset
3391 ;; `process-file-side-effects' in order to keep the cache when
3392 ;; `process-file' calls appear.
3393 (let (process-file-side-effects)
3394 (tramp-run-real-handler 'vc-registered (list file)))))))
3395
3396;;;###tramp-autoload
3397(defun tramp-sh-file-name-handler (operation &rest args)
3398 "Invoke remote-shell Tramp file name handler.
3399Fall back to normal file name handler if no Tramp handler exists."
3400 (when (and tramp-locked (not tramp-locker))
3401 (setq tramp-locked nil)
3402 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3403 (let ((tl tramp-locked))
3404 (unwind-protect
3405 (progn
3406 (setq tramp-locked t)
3407 (let ((tramp-locker t))
3408 (save-match-data
3409 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3410 (if fn
3411 (apply (cdr fn) args)
3412 (tramp-run-real-handler operation args))))))
3413 (setq tramp-locked tl))))
3414
3415(defun tramp-vc-file-name-handler (operation &rest args)
3416 "Invoke special file name handler, which collects files to be handled."
3417 (save-match-data
3418 (let ((filename
3419 (tramp-replace-environment-variables
3420 (apply 'tramp-file-name-for-operation operation args)))
3421 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3422 (with-parsed-tramp-file-name filename nil
3423 (cond
3424 ;; That's what we want: file names, for which checks are
2c68ca0e 3425 ;; applied. We assume that VC uses only `file-exists-p' and
03c1ad43
MA
3426 ;; `file-readable-p' checks; otherwise we must extend the
3427 ;; list. We do not perform any action, but return nil, in
3428 ;; order to keep `vc-registered' running.
3429 ((and fn (memq operation '(file-exists-p file-readable-p)))
3430 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3431 nil)
957b3189
MA
3432 ;; `process-file' and `start-file-process' shall be ignored.
3433 ((and fn (eq operation 'process-file) 0))
3434 ((and fn (eq operation 'start-file-process) nil))
03c1ad43
MA
3435 ;; Tramp file name handlers like `expand-file-name'. They
3436 ;; must still work.
957b3189 3437 (fn (save-match-data (apply (cdr fn) args)))
03c1ad43
MA
3438 ;; Default file name handlers, we don't care.
3439 (t (tramp-run-real-handler operation args)))))))
3440
3441;;; Internal Functions:
3442
3443(defun tramp-maybe-send-script (vec script name)
3444 "Define in remote shell function NAME implemented as SCRIPT.
3445Only send the definition if it has not already been done."
8a7eddd7
MA
3446 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3447 ;; might be nil.
3448 (let ((scripts (tramp-get-connection-property
3449 (tramp-get-connection-process vec) "scripts" nil)))
03c1ad43 3450 (unless (member name scripts)
1d51f99c 3451 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
03c1ad43
MA
3452 ;; The script could contain a call of Perl. This is masked with `%s'.
3453 (tramp-barf-unless-okay
3454 vec
3455 (format "%s () {\n%s\n}" name
3456 (format script (tramp-get-remote-perl vec)))
3457 "Script %s sending failed" name)
8a7eddd7
MA
3458 (tramp-set-connection-property
3459 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
03c1ad43
MA
3460
3461(defun tramp-set-auto-save ()
3462 (when (and ;; ange-ftp has its own auto-save mechanism
3463 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3464 'tramp-sh-file-name-handler)
3465 auto-save-default)
3466 (auto-save-mode 1)))
3467(add-hook 'find-file-hooks 'tramp-set-auto-save t)
3468(add-hook 'tramp-unload-hook
3469 (lambda ()
3470 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3471
3472(defun tramp-run-test (switch filename)
3473 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3474Returns the exit code of the `test' program."
3475 (with-parsed-tramp-file-name filename nil
3476 (tramp-send-command-and-check
3477 v
3478 (format
3479 "%s %s %s"
3480 (tramp-get-test-command v)
3481 switch
3482 (tramp-shell-quote-argument localname)))))
3483
3484(defun tramp-run-test2 (format-string file1 file2)
3485 "Run `test'-like program on the remote system, given FILE1, FILE2.
3486FORMAT-STRING contains the program name, switches, and place holders.
3487Returns the exit code of the `test' program. Barfs if the methods,
3488hosts, or files, disagree."
3489 (unless (tramp-equal-remote file1 file2)
3490 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3491 (tramp-error
3492 v 'file-error
3493 "tramp-run-test2 only implemented for same method, user, host")))
3494 (with-parsed-tramp-file-name file1 v1
3495 (with-parsed-tramp-file-name file1 v2
3496 (tramp-send-command-and-check
3497 v1
3498 (format format-string
3499 (tramp-shell-quote-argument v1-localname)
3500 (tramp-shell-quote-argument v2-localname))))))
3501
3502(defun tramp-find-executable
3503 (vec progname dirlist &optional ignore-tilde ignore-path)
3504 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3505First arg VEC specifies the connection, PROGNAME is the program
3506to search for, and DIRLIST gives the list of directories to
3507search. If IGNORE-TILDE is non-nil, directory names starting
3508with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3509only in DIRLIST.
3510
3511Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3512
3513This function expects to be in the right *tramp* buffer."
3514 (with-current-buffer (tramp-get-connection-buffer vec)
3515 (let (result)
3516 ;; Check whether the executable is in $PATH. "which(1)" does not
3517 ;; report always a correct error code; therefore we check the
3518 ;; number of words it returns.
3519 (unless ignore-path
3520 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3521 (goto-char (point-min))
3522 (if (looking-at "^\\s-*1$")
3523 (setq result (concat "\\" progname))))
3524 (unless result
3525 (when ignore-tilde
3526 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3527 ;; `remove' is in CL, and we want to avoid CL dependencies.
3528 (let (newdl d)
3529 (while dirlist
3530 (setq d (car dirlist))
3531 (setq dirlist (cdr dirlist))
3532 (unless (char-equal ?~ (aref d 0))
3533 (setq newdl (cons d newdl))))
3534 (setq dirlist (nreverse newdl))))
3535 (tramp-send-command
3536 vec
3537 (format (concat "while read d; "
3538 "do if test -x $d/%s -a -f $d/%s; "
3539 "then echo tramp_executable $d/%s; "
3540 "break; fi; done <<'EOF'\n"
3541 "%s\nEOF")
3542 progname progname progname (mapconcat 'identity dirlist "\n")))
3543 (goto-char (point-max))
3544 (when (search-backward "tramp_executable " nil t)
3545 (skip-chars-forward "^ ")
3546 (skip-chars-forward " ")
6e060cee 3547 (setq result (buffer-substring (point) (point-at-eol)))))
03c1ad43
MA
3548 result)))
3549
3550(defun tramp-set-remote-path (vec)
3551 "Sets the remote environment PATH to existing directories.
3552I.e., for each directory in `tramp-remote-path', it is tested
3553whether it exists and if so, it is added to the environment
3554variable PATH."
3555 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3556 (tramp-send-command
3557 vec (format "PATH=%s; export PATH"
3558 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3559
3560;; ------------------------------------------------------------
3561;; -- Communication with external shell --
3562;; ------------------------------------------------------------
3563
3564(defun tramp-find-file-exists-command (vec)
3565 "Find a command on the remote host for checking if a file exists.
3566Here, we are looking for a command which has zero exit status if the
3567file exists and nonzero exit status otherwise."
3568 (let ((existing "/")
fac916bf 3569 (nonexistent
03c1ad43
MA
3570 (tramp-shell-quote-argument "/ this file does not exist "))
3571 result)
3572 ;; The algorithm is as follows: we try a list of several commands.
3573 ;; For each command, we first run `$cmd /' -- this should return
3574 ;; true, as the root directory always exists. And then we run
3575 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3576 ;; does not exist. This should return false. We use the first
3577 ;; command we find that seems to work.
3578 ;; The list of commands to try is as follows:
3579 ;; `ls -d' This works on most systems, but NetBSD 1.4
3580 ;; has a bug: `ls' always returns zero exit
3581 ;; status, even for files which don't exist.
3582 ;; `test -e' Some Bourne shells have a `test' builtin
3583 ;; which does not know the `-e' option.
3584 ;; `/bin/test -e' For those, the `test' binary on disk normally
3585 ;; provides the option. Alas, the binary
3586 ;; is sometimes `/bin/test' and sometimes it's
3587 ;; `/usr/bin/test'.
3588 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3589 (unless (or
d9f9b465
MA
3590 (ignore-errors
3591 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3592 (tramp-send-command-and-check
3593 vec (format "%s %s" result existing))
3594 (not (tramp-send-command-and-check
3595 vec (format "%s %s" result nonexistent)))))
3596 (ignore-errors
3597 (and (setq result "/bin/test -e")
3598 (tramp-send-command-and-check
3599 vec (format "%s %s" result existing))
3600 (not (tramp-send-command-and-check
3601 vec (format "%s %s" result nonexistent)))))
3602 (ignore-errors
3603 (and (setq result "/usr/bin/test -e")
3604 (tramp-send-command-and-check
3605 vec (format "%s %s" result existing))
3606 (not (tramp-send-command-and-check
3607 vec (format "%s %s" result nonexistent)))))
3608 (ignore-errors
3609 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3610 (tramp-send-command-and-check
3611 vec (format "%s %s" result existing))
3612 (not (tramp-send-command-and-check
3613 vec (format "%s %s" result nonexistent))))))
03c1ad43
MA
3614 (tramp-error
3615 vec 'file-error "Couldn't find command to check if file exists"))
3616 result))
3617
3618(defun tramp-open-shell (vec shell)
3619 "Opens shell SHELL."
1d51f99c 3620 (with-tramp-progress-reporter
7d520089 3621 vec 5 (format "Opening remote shell `%s'" shell)
03c1ad43
MA
3622 ;; Find arguments for this shell.
3623 (let ((tramp-end-of-output tramp-initial-end-of-output)
3624 (alist tramp-sh-extra-args)
3625 item extra-args)
3626 (while (and alist (null extra-args))
3627 (setq item (pop alist))
3628 (when (string-match (car item) shell)
3629 (setq extra-args (cdr item))))
03c1ad43 3630 (tramp-send-command
e1873bd0
MA
3631 vec (format
3632 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3633 (tramp-shell-quote-argument tramp-end-of-output)
3634 shell (or extra-args ""))
03c1ad43 3635 t))
e1873bd0
MA
3636 (tramp-set-connection-property
3637 (tramp-get-connection-process vec) "remote-shell" shell)
03c1ad43
MA
3638 ;; Setting prompts.
3639 (tramp-send-command
63648a95 3640 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
03c1ad43
MA
3641 (tramp-send-command vec "PS2=''" t)
3642 (tramp-send-command vec "PS3=''" t)
3643 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3644
3645(defun tramp-find-shell (vec)
3646 "Opens a shell on the remote host which groks tilde expansion."
d9f9b465 3647 (with-current-buffer (tramp-get-buffer vec)
e1873bd0
MA
3648 (let ((default-shell
3649 (or
3650 (tramp-get-connection-property
3651 (tramp-get-connection-process vec) "remote-shell" nil)
3652 (tramp-get-method-parameter
3653 (tramp-file-name-method vec) 'tramp-remote-shell)))
d9f9b465
MA
3654 shell)
3655 (setq shell
1d51f99c 3656 (with-tramp-connection-property vec "remote-shell"
d9f9b465
MA
3657 ;; CCC: "root" does not exist always, see QNAP 459.
3658 ;; Which check could we apply instead?
3659 (tramp-send-command vec "echo ~root" t)
3660 (if (or (string-match "^~root$" (buffer-string))
3661 ;; The default shell (ksh93) of OpenSolaris and
3662 ;; Solaris is buggy. We've got reports for
3663 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3664 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3665 (tramp-get-connection-property
3666 vec "uname" "")))
3667
3668 (or (tramp-find-executable
3669 vec "bash" (tramp-get-remote-path vec) t t)
3670 (tramp-find-executable
3671 vec "ksh" (tramp-get-remote-path vec) t t)
3672 ;; Maybe it works at least for some other commands.
3673 (prog1
3674 default-shell
3675 (tramp-message
3676 vec 2
3677 (concat
3678 "Couldn't find a remote shell which groks tilde "
3679 "expansion, using `%s'")
3680 default-shell)))
3681
3682 default-shell)))
3683
3684 ;; Open a new shell if needed.
3685 (unless (string-equal shell default-shell)
3686 (tramp-message
3687 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3688 (tramp-open-shell vec shell))
3689
3690 ;; Busyboxes tend to behave strange. We check for the existence.
1d51f99c 3691 (with-tramp-connection-property vec "busybox"
d9f9b465
MA
3692 (tramp-send-command vec (format "%s --version" shell) t)
3693 (let ((case-fold-search t))
3694 (and (string-match "busybox" (buffer-string)) t))))))
03c1ad43
MA
3695
3696;; Utility functions.
3697
3698(defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3699 "Wait for shell prompt and barf if none appears.
3700Looks at process PROC to see if a shell prompt appears in TIMEOUT
3701seconds. If not, it produces an error message with the given ERROR-ARGS."
3702 (unless
3703 (tramp-wait-for-regexp
3704 proc timeout
3705 (format
3706 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3707 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3708
3709(defun tramp-open-connection-setup-interactive-shell (proc vec)
3710 "Set up an interactive shell.
3711Mainly sets the prompt and the echo correctly. PROC is the shell
3712process to set up. VEC specifies the connection."
3713 (let ((tramp-end-of-output tramp-initial-end-of-output))
3714 ;; It is useful to set the prompt in the following command because
3715 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3716 ;; about and thus /bin/sh will display a strange prompt. For
3717 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3718 ;; display the current working directory but /bin/sh will display
3719 ;; a dollar sign. The following command line sets $PS1 to a sane
3720 ;; value, and works under Bourne-ish shells as well as csh-like
3721 ;; shells. Daniel Pittman reports that the unusual positioning of
3722 ;; the single quotes makes it work under `rc', too. We also unset
3723 ;; the variable $ENV because that is read by some sh
3724 ;; implementations (eg, bash when called as sh) on startup; this
6e060cee 3725 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
03c1ad43
MA
3726 ;; is another way to set the prompt in /bin/bash, it must be
3727 ;; discarded as well.
3728 (tramp-open-shell
3729 vec
e1873bd0
MA
3730 (or (tramp-get-connection-property vec "remote-shell" nil)
3731 (tramp-get-method-parameter
3732 (tramp-file-name-method vec) 'tramp-remote-shell)))
03c1ad43
MA
3733
3734 ;; Disable echo.
3735 (tramp-message vec 5 "Setting up remote shell environment")
3736 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3737 ;; Check whether the echo has really been disabled. Some
3738 ;; implementations, like busybox of embedded GNU/Linux, don't
3739 ;; support disabling.
3740 (tramp-send-command vec "echo foo" t)
3741 (with-current-buffer (process-buffer proc)
3742 (goto-char (point-min))
3743 (when (looking-at "echo foo")
3744 (tramp-set-connection-property proc "remote-echo" t)
3745 (tramp-message vec 5 "Remote echo still on. Ok.")
3746 ;; Make sure backspaces and their echo are enabled and no line
3747 ;; width magic interferes with them.
3748 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3749
3750 (tramp-message vec 5 "Setting shell prompt")
3751 (tramp-send-command
63648a95 3752 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
03c1ad43
MA
3753 (tramp-send-command vec "PS2=''" t)
3754 (tramp-send-command vec "PS3=''" t)
3755 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3756
3757 ;; Try to set up the coding system correctly.
3758 ;; CCC this can't be the right way to do it. Hm.
3759 (tramp-message vec 5 "Determining coding system")
3760 (tramp-send-command vec "echo foo ; echo bar" t)
3761 (with-current-buffer (process-buffer proc)
3762 (goto-char (point-min))
3763 (if (featurep 'mule)
3764 ;; Use MULE to select the right EOL convention for communicating
3765 ;; with the process.
3766 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3767 (cons 'undecided 'undecided)))
3768 cs-decode cs-encode)
3769 (when (symbolp cs) (setq cs (cons cs cs)))
3770 (setq cs-decode (car cs))
3771 (setq cs-encode (cdr cs))
3772 (unless cs-decode (setq cs-decode 'undecided))
3773 (unless cs-encode (setq cs-encode 'undecided))
bd8fadca 3774 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
03c1ad43
MA
3775 cs-encode 'unix))
3776 (when (search-forward "\r" nil t)
bd8fadca 3777 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
03c1ad43
MA
3778 cs-decode 'dos)))
3779 (tramp-compat-funcall
3780 'set-buffer-process-coding-system cs-decode cs-encode)
3781 (tramp-message
3782 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3783 ;; Look for ^M and do something useful if found.
3784 (when (search-forward "\r" nil t)
3785 ;; We have found a ^M but cannot frob the process coding system
3786 ;; because we're running on a non-MULE Emacs. Let's try
3787 ;; stty, instead.
3788 (tramp-send-command vec "stty -onlcr" t))))
aa095b2d 3789
03c1ad43
MA
3790 (tramp-send-command vec "set +o vi +o emacs" t)
3791
3792 ;; Check whether the output of "uname -sr" has been changed. If
3793 ;; yes, this is a strong indication that we must expire all
3794 ;; connection properties. We start again with
333f9019 3795 ;; `tramp-maybe-open-connection', it will be caught there.
03c1ad43
MA
3796 (tramp-message vec 5 "Checking system information")
3797 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3798 (new-uname
3799 (tramp-set-connection-property
3800 vec "uname"
3801 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3802 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
2fe4b125
MA
3803 (tramp-cleanup vec)
3804 (tramp-message
3805 vec 3
3806 "Connection reset, because remote host changed from `%s' to `%s'"
3807 old-uname new-uname)
3808 (throw 'uname-changed (tramp-maybe-open-connection vec))))
03c1ad43
MA
3809
3810 ;; Check whether the remote host suffers from buggy
3811 ;; `send-process-string'. This is known for FreeBSD (see comment in
3812 ;; `send_process', file process.c). I've tested sending 624 bytes
3813 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3814 ;; this host type is detected locally. It cannot handle remote
3815 ;; hosts, though.
1d51f99c 3816 (with-tramp-connection-property proc "chunksize"
03c1ad43
MA
3817 (cond
3818 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3819 tramp-chunksize)
3820 (t
3821 (tramp-message
3822 vec 5 "Checking remote host type for `send-process-string' bug")
3823 (if (string-match
3824 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3825 500 0))))
3826
3827 ;; Set remote PATH variable.
3828 (tramp-set-remote-path vec)
3829
3830 ;; Search for a good shell before searching for a command which
d9f9b465 3831 ;; checks if a file exists. This is done because Tramp wants to use
03c1ad43
MA
3832 ;; "test foo; echo $?" to check if various conditions hold, and
3833 ;; there are buggy /bin/sh implementations which don't execute the
3834 ;; "echo $?" part if the "test" part has an error. In particular,
3835 ;; the OpenSolaris /bin/sh is a problem. There are also other
3836 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3837 ;; in function declarations, or changing HISTFILE in place.
3838 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3839 ;; detected.
3840 (tramp-find-shell vec)
3841
3842 ;; Disable unexpected output.
3843 (tramp-send-command vec "mesg n; biff n" t)
3844
3845 ;; IRIX64 bash expands "!" even when in single quotes. This
3846 ;; destroys our shell functions, we must disable it. See
3847 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3848 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3849 (tramp-send-command vec "set +H" t))
3850
aa095b2d
MA
3851 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3852 (when (string-match "BSD\\|Darwin"
3853 (tramp-get-connection-property vec "uname" ""))
3854 (tramp-send-command vec "stty -oxtabs" t))
3855
03c1ad43 3856 ;; Set `remote-tty' process property.
6d95bd46
MA
3857 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3858 (unless (zerop (length tty))
3859 (tramp-compat-process-put proc 'remote-tty tty)))
03c1ad43 3860
aa095b2d
MA
3861 ;; Dump stty settings in the traces.
3862 (when (>= tramp-verbose 9)
3863 (tramp-send-command vec "stty -a" t))
3864
03c1ad43
MA
3865 ;; Set the environment.
3866 (tramp-message vec 5 "Setting default environment")
3867
3868 (let ((env (copy-sequence tramp-remote-process-environment))
3869 unset item)
3870 (while env
3871 (setq item (tramp-compat-split-string (car env) "="))
3872 (setcdr item (mapconcat 'identity (cdr item) "="))
3873 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3874 (tramp-send-command
3875 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3876 (push (car item) unset))
3877 (setq env (cdr env)))
3878 (when unset
3879 (tramp-send-command
3880 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3881
3882;; CCC: We should either implement a Perl version of base64 encoding
3883;; and decoding. Then we just use that in the last item. The other
3884;; alternative is to use the Perl version of UU encoding. But then
3885;; we need a Lisp version of uuencode.
3886;;
3887;; Old text from documentation of tramp-methods:
3888;; Using a uuencode/uudecode inline method is discouraged, please use one
3889;; of the base64 methods instead since base64 encoding is much more
3890;; reliable and the commands are more standardized between the different
3891;; Unix versions. But if you can't use base64 for some reason, please
3892;; note that the default uudecode command does not work well for some
3893;; Unices, in particular AIX and Irix. For AIX, you might want to use
3894;; the following command for uudecode:
3895;;
3896;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3897;;
3898;; For Irix, no solution is known yet.
3899
3900(autoload 'uudecode-decode-region "uudecode")
3901
3902(defconst tramp-local-coding-commands
3903 '((b64 base64-encode-region base64-decode-region)
3904 (uu tramp-uuencode-region uudecode-decode-region)
3905 (pack
3906 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3907 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3908 "List of local coding commands for inline transfer.
3909Each item is a list that looks like this:
3910
3911\(FORMAT ENCODING DECODING\)
3912
3913FORMAT is symbol describing the encoding/decoding format. It can be
3914`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3915
3916ENCODING and DECODING can be strings, giving commands, or symbols,
3917giving functions. If they are strings, then they can contain
3918the \"%s\" format specifier. If that specifier is present, the input
3919filename will be put into the command line at that spot. If the
3920specifier is not present, the input should be read from standard
3921input.
3922
3923If they are functions, they will be called with two arguments, start
3924and end of region, and are expected to replace the region contents
3925with the encoded or decoded results, respectively.")
3926
3927(defconst tramp-remote-coding-commands
6e060cee
MA
3928 '((b64 "base64" "base64 -d -i")
3929 ;; "-i" is more robust with older base64 from GNU coreutils.
3930 ;; However, I don't know whether all base64 versions do supports
3931 ;; this option.
3932 (b64 "base64" "base64 -d")
03c1ad43
MA
3933 (b64 "mimencode -b" "mimencode -u -b")
3934 (b64 "mmencode -b" "mmencode -u -b")
3935 (b64 "recode data..base64" "recode base64..data")
3936 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3937 (b64 tramp-perl-encode tramp-perl-decode)
2fe4b125 3938 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
03c1ad43
MA
3939 (uu "uuencode xxx" "uudecode -o -")
3940 (uu "uuencode xxx" "uudecode -p")
3941 (uu "uuencode xxx" tramp-uudecode)
3942 (pack
3943 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3944 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3945 "List of remote coding commands for inline transfer.
3946Each item is a list that looks like this:
3947
2fe4b125 3948\(FORMAT ENCODING DECODING [TEST]\)
03c1ad43
MA
3949
3950FORMAT is symbol describing the encoding/decoding format. It can be
3951`b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3952
3953ENCODING and DECODING can be strings, giving commands, or symbols,
3954giving variables. If they are strings, then they can contain
3955the \"%s\" format specifier. If that specifier is present, the input
3956filename will be put into the command line at that spot. If the
3957specifier is not present, the input should be read from standard
3958input.
3959
3960If they are variables, this variable is a string containing a Perl
3961implementation for this functionality. This Perl program will be transferred
2fe4b125
MA
3962to the remote host, and it is available as shell function with the same name.
3963
3964The optional TEST command can be used for further tests, whether
3965ENCODING and DECODING are applicable.")
03c1ad43
MA
3966
3967(defun tramp-find-inline-encoding (vec)
3968 "Find an inline transfer encoding that works.
3969Goes through the list `tramp-local-coding-commands' and
3970`tramp-remote-coding-commands'."
3971 (save-excursion
3972 (let ((local-commands tramp-local-coding-commands)
3973 (magic "xyzzy")
2fe4b125
MA
3974 (p (tramp-get-connection-process vec))
3975 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
03c1ad43
MA
3976 (while (and local-commands (not found))
3977 (setq litem (pop local-commands))
3978 (catch 'wont-work-local
3979 (let ((format (nth 0 litem))
3980 (remote-commands tramp-remote-coding-commands))
3981 (setq loc-enc (nth 1 litem))
3982 (setq loc-dec (nth 2 litem))
3983 ;; If the local encoder or decoder is a string, the
3984 ;; corresponding command has to work locally.
3985 (if (not (stringp loc-enc))
3986 (tramp-message
3987 vec 5 "Checking local encoding function `%s'" loc-enc)
3988 (tramp-message
3989 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
3990 (unless (zerop (tramp-call-local-coding-command
3991 loc-enc nil nil))
3992 (throw 'wont-work-local nil)))
3993 (if (not (stringp loc-dec))
3994 (tramp-message
3995 vec 5 "Checking local decoding function `%s'" loc-dec)
3996 (tramp-message
3997 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
3998 (unless (zerop (tramp-call-local-coding-command
3999 loc-dec nil nil))
4000 (throw 'wont-work-local nil)))
4001 ;; Search for remote coding commands with the same format
4002 (while (and remote-commands (not found))
4003 (setq ritem (pop remote-commands))
4004 (catch 'wont-work-remote
4005 (when (equal format (nth 0 ritem))
4006 (setq rem-enc (nth 1 ritem))
4007 (setq rem-dec (nth 2 ritem))
2fe4b125
MA
4008 (setq rem-test (nth 3 ritem))
4009 ;; Check the remote test command if exists.
4010 (when (stringp rem-test)
4011 (tramp-message
4012 vec 5 "Checking remote test command `%s'" rem-test)
4013 (unless (tramp-send-command-and-check vec rem-test t)
4014 (throw 'wont-work-remote nil)))
03c1ad43
MA
4015 ;; Check if remote encoding and decoding commands can be
4016 ;; called remotely with null input and output. This makes
4017 ;; sure there are no syntax errors and the command is really
4018 ;; found. Note that we do not redirect stdout to /dev/null,
4019 ;; for two reasons: when checking the decoding command, we
4020 ;; actually check the output it gives. And also, when
4021 ;; redirecting "mimencode" output to /dev/null, then as root
4022 ;; it might change the permissions of /dev/null!
4023 (when (not (stringp rem-enc))
4024 (let ((name (symbol-name rem-enc)))
4025 (while (string-match (regexp-quote "-") name)
4026 (setq name (replace-match "_" nil t name)))
4027 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4028 (setq rem-enc name)))
4029 (tramp-message
4030 vec 5
4031 "Checking remote encoding command `%s' for sanity" rem-enc)
4032 (unless (tramp-send-command-and-check
4033 vec (format "%s </dev/null" rem-enc) t)
4034 (throw 'wont-work-remote nil))
4035
4036 (when (not (stringp rem-dec))
4037 (let ((name (symbol-name rem-dec)))
4038 (while (string-match (regexp-quote "-") name)
4039 (setq name (replace-match "_" nil t name)))
4040 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
4041 (setq rem-dec name)))
4042 (tramp-message
4043 vec 5
4044 "Checking remote decoding command `%s' for sanity" rem-dec)
4045 (unless (tramp-send-command-and-check
4046 vec
4047 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4048 t)
4049 (throw 'wont-work-remote nil))
4050
4051 (with-current-buffer (tramp-get-buffer vec)
4052 (goto-char (point-min))
4053 (unless (looking-at (regexp-quote magic))
4054 (throw 'wont-work-remote nil)))
4055
4056 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4057 (setq rem-enc (nth 1 ritem))
4058 (setq rem-dec (nth 2 ritem))
4059 (setq found t)))))))
4060
4061 ;; Did we find something?
4062 (unless found
4063 (tramp-error
4064 vec 'file-error "Couldn't find an inline transfer encoding"))
4065
2fe4b125
MA
4066 ;; Set connection properties. Since the commands are risky (due
4067 ;; to output direction), we cache them in the process cache.
03c1ad43 4068 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
2fe4b125 4069 (tramp-set-connection-property p "local-encoding" loc-enc)
03c1ad43 4070 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
2fe4b125 4071 (tramp-set-connection-property p "local-decoding" loc-dec)
03c1ad43 4072 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
2fe4b125 4073 (tramp-set-connection-property p "remote-encoding" rem-enc)
03c1ad43 4074 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
2fe4b125 4075 (tramp-set-connection-property p "remote-decoding" rem-dec))))
03c1ad43
MA
4076
4077(defun tramp-call-local-coding-command (cmd input output)
4078 "Call the local encoding or decoding command.
4079If CMD contains \"%s\", provide input file INPUT there in command.
4080Otherwise, INPUT is passed via standard input.
4081INPUT can also be nil which means `/dev/null'.
4082OUTPUT can be a string (which specifies a filename), or t (which
4083means standard output and thus the current buffer), or nil (which
4084means discard it)."
4085 (tramp-compat-call-process
4086 tramp-encoding-shell
4087 (when (and input (not (string-match "%s" cmd))) input)
4088 (if (eq output t) t nil)
4089 nil
4090 tramp-encoding-command-switch
4091 (concat
4092 (if (string-match "%s" cmd) (format cmd input) cmd)
4093 (if (stringp output) (concat "> " output) ""))))
4094
4095(defconst tramp-inline-compress-commands
4096 '(("gzip" "gzip -d")
4097 ("bzip2" "bzip2 -d")
443d6696 4098 ("xz" "xz -d")
03c1ad43
MA
4099 ("compress" "compress -d"))
4100 "List of compress and decompress commands for inline transfer.
4101Each item is a list that looks like this:
4102
4103\(COMPRESS DECOMPRESS\)
4104
4105COMPRESS or DECOMPRESS are strings with the respective commands.")
4106
4107(defun tramp-find-inline-compress (vec)
4108 "Find an inline transfer compress command that works.
4109Goes through the list `tramp-inline-compress-commands'."
4110 (save-excursion
4111 (let ((commands tramp-inline-compress-commands)
4112 (magic "xyzzy")
2fe4b125
MA
4113 (p (tramp-get-connection-process vec))
4114 item compress decompress found)
03c1ad43
MA
4115 (while (and commands (not found))
4116 (catch 'next
4117 (setq item (pop commands)
4118 compress (nth 0 item)
4119 decompress (nth 1 item))
4120 (tramp-message
4121 vec 5
4122 "Checking local compress command `%s', `%s' for sanity"
4123 compress decompress)
362b9d48
GM
4124 (unless
4125 (zerop
4126 (tramp-call-local-coding-command
4127 (format
4128 ;; Windows shells need the program file name after
4129 ;; the pipe symbol be quoted if they use forward
4130 ;; slashes as directory separators.
4131 (if (memq system-type '(windows-nt))
4132 "echo %s | \"%s\" | \"%s\""
4133 "echo %s | %s | %s")
4134 magic compress decompress) nil nil))
03c1ad43
MA
4135 (throw 'next nil))
4136 (tramp-message
4137 vec 5
4138 "Checking remote compress command `%s', `%s' for sanity"
4139 compress decompress)
4140 (unless (tramp-send-command-and-check
4141 vec (format "echo %s | %s | %s" magic compress decompress) t)
4142 (throw 'next nil))
4143 (setq found t)))
4144
4145 ;; Did we find something?
4146 (if found
4147 (progn
2fe4b125
MA
4148 ;; Set connection properties. Since the commands are
4149 ;; risky (due to output direction), we cache them in the
4150 ;; process cache.
03c1ad43
MA
4151 (tramp-message
4152 vec 5 "Using inline transfer compress command `%s'" compress)
2fe4b125 4153 (tramp-set-connection-property p "inline-compress" compress)
03c1ad43
MA
4154 (tramp-message
4155 vec 5 "Using inline transfer decompress command `%s'" decompress)
2fe4b125 4156 (tramp-set-connection-property p "inline-decompress" decompress))
03c1ad43 4157
2fe4b125
MA
4158 (tramp-set-connection-property p "inline-compress" nil)
4159 (tramp-set-connection-property p "inline-decompress" nil)
03c1ad43
MA
4160 (tramp-message
4161 vec 2 "Couldn't find an inline transfer compress command")))))
4162
4163(defun tramp-compute-multi-hops (vec)
4164 "Expands VEC according to `tramp-default-proxies-alist'.
4165Gateway hops are already opened."
4166 (let ((target-alist `(,vec))
2fe4b125
MA
4167 (hops (or (tramp-file-name-hop vec) ""))
4168 (item vec)
4169 choices proxy)
4170
4171 ;; Ad-hoc proxy definitions.
4172 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4173 (let ((user (tramp-file-name-user item))
4174 (host (tramp-file-name-host item))
4175 (proxy (concat
4176 tramp-prefix-format proxy tramp-postfix-host-format)))
4177 (tramp-message
4178 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4179 (and (stringp host) (regexp-quote host))
4180 (and (stringp user) (regexp-quote user))
4181 proxy)
4182 ;; Add the hop.
4183 (add-to-list
4184 'tramp-default-proxies-alist
4185 (list (and (stringp host) (regexp-quote host))
4186 (and (stringp user) (regexp-quote user))
4187 proxy))
4188 (setq item (tramp-dissect-file-name proxy))))
4189 ;; Save the new value.
4190 (when (and hops tramp-save-ad-hoc-proxies)
4191 (customize-save-variable
4192 'tramp-default-proxies-alist tramp-default-proxies-alist))
03c1ad43
MA
4193
4194 ;; Look for proxy hosts to be passed.
2fe4b125 4195 (setq choices tramp-default-proxies-alist)
03c1ad43
MA
4196 (while choices
4197 (setq item (pop choices)
4198 proxy (eval (nth 2 item)))
4199 (when (and
2fe4b125 4200 ;; Host.
03c1ad43
MA
4201 (string-match (or (eval (nth 0 item)) "")
4202 (or (tramp-file-name-host (car target-alist)) ""))
2fe4b125 4203 ;; User.
03c1ad43
MA
4204 (string-match (or (eval (nth 1 item)) "")
4205 (or (tramp-file-name-user (car target-alist)) "")))
4206 (if (null proxy)
4207 ;; No more hops needed.
4208 (setq choices nil)
4209 ;; Replace placeholders.
4210 (setq proxy
4211 (format-spec
4212 proxy
4213 (format-spec-make
4214 ?u (or (tramp-file-name-user (car target-alist)) "")
4215 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4216 (with-parsed-tramp-file-name proxy l
4217 ;; Add the hop.
4218 (add-to-list 'target-alist l)
4219 ;; Start next search.
4220 (setq choices tramp-default-proxies-alist)))))
4221
4222 ;; Handle gateways.
4223 (when (string-match
4224 (format
4225 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4226 (tramp-file-name-method (car target-alist)))
4227 (let ((gw (pop target-alist))
4228 (hop (pop target-alist)))
4229 ;; Is the method prepared for gateways?
66feec8b 4230 (unless (tramp-file-name-port hop)
03c1ad43
MA
4231 (tramp-error
4232 vec 'file-error
66feec8b 4233 "Connection `%s' is not supported for gateway access." hop))
03c1ad43
MA
4234 ;; Open the gateway connection.
4235 (add-to-list
4236 'target-alist
4237 (vector
4238 (tramp-file-name-method hop) (tramp-file-name-user hop)
2fe4b125 4239 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
03c1ad43
MA
4240 ;; For the password prompt, we need the correct values.
4241 ;; Therefore, we must remember the gateway vector. But we
4242 ;; cannot do it as connection property, because it shouldn't
4243 ;; be persistent. And we have no started process yet either.
4244 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4245
4246 ;; Foreign and out-of-band methods are not supported for multi-hops.
4247 (when (cdr target-alist)
4248 (setq choices target-alist)
4249 (while choices
4250 (setq item (pop choices))
4251 (when
4252 (or
4253 (not
4254 (tramp-get-method-parameter
4255 (tramp-file-name-method item) 'tramp-login-program))
4256 (tramp-get-method-parameter
4257 (tramp-file-name-method item) 'tramp-copy-program))
4258 (tramp-error
4259 vec 'file-error
4260 "Method `%s' is not supported for multi-hops."
4261 (tramp-file-name-method item)))))
4262
4263 ;; In case the host name is not used for the remote shell
4264 ;; command, the user could be misguided by applying a random
4265 ;; hostname.
4266 (let* ((v (car target-alist))
4267 (method (tramp-file-name-method v))
4268 (host (tramp-file-name-host v)))
4269 (unless
4270 (or
4271 ;; There are multi-hops.
4272 (cdr target-alist)
4273 ;; The host name is used for the remote shell command.
4274 (member
4275 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4276 ;; The host is local. We cannot use `tramp-local-host-p'
4277 ;; here, because it opens a connection as well.
4278 (string-match tramp-local-host-regexp host))
4279 (tramp-error
4280 v 'file-error
4281 "Host `%s' looks like a remote host, `%s' can only use the local host"
4282 host method)))
4283
4284 ;; Result.
4285 target-alist))
4286
2fe4b125
MA
4287(defvar tramp-current-connection nil
4288 "Last connection timestamp.")
4289
03c1ad43
MA
4290(defun tramp-maybe-open-connection (vec)
4291 "Maybe open a connection VEC.
4292Does not do anything if a connection is already open, but re-opens the
4293connection if a previous connection has died for some reason."
4294 (catch 'uname-changed
4295 (let ((p (tramp-get-connection-process vec))
4296 (process-name (tramp-get-connection-property vec "process-name" nil))
bfd31217
MA
4297 (process-environment (copy-sequence process-environment))
4298 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
03c1ad43 4299
2fe4b125
MA
4300 ;; If Tramp opens the same connection within a short time frame,
4301 ;; there is a problem. We shall signal this.
4302 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4303 (not (equal (butlast (append vec nil))
4304 (car tramp-current-connection)))
4305 (> (tramp-time-diff
4306 (current-time) (cdr tramp-current-connection))
4307 5))
4308 (throw 'suppress 'suppress))
4309
03c1ad43
MA
4310 ;; If too much time has passed since last command was sent, look
4311 ;; whether process is still alive. If it isn't, kill it. When
4312 ;; using ssh, it can sometimes happen that the remote end has
4313 ;; hung up but the local ssh client doesn't recognize this until
4314 ;; it tries to send some data to the remote end. So that's why
4315 ;; we try to send a command from time to time, then look again
4316 ;; whether the process is really alive.
4317 (condition-case nil
4318 (when (and (> (tramp-time-diff
4319 (current-time)
4320 (tramp-get-connection-property
4321 p "last-cmd-time" '(0 0 0)))
4322 60)
4323 p (processp p) (memq (process-status p) '(run open)))
4324 (tramp-send-command vec "echo are you awake" t t)
4325 (unless (and (memq (process-status p) '(run open))
4326 (tramp-wait-for-output p 10))
333f9019 4327 ;; The error will be caught locally.
03c1ad43
MA
4328 (tramp-error vec 'file-error "Awake did fail")))
4329 (file-error
2fe4b125 4330 (tramp-cleanup vec)
03c1ad43
MA
4331 (setq p nil)))
4332
4333 ;; New connection must be opened.
6bdac736
MA
4334 (condition-case err
4335 (unless (and p (processp p) (memq (process-status p) '(run open)))
4336
4337 ;; We call `tramp-get-buffer' in order to get a debug
4338 ;; buffer for messages from the beginning.
4339 (tramp-get-buffer vec)
29855149
MA
4340
4341 ;; If `non-essential' is non-nil, don't reopen a new connection.
957b3189 4342 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
29855149
MA
4343 (throw 'non-essential 'non-essential))
4344
1d51f99c 4345 (with-tramp-progress-reporter
6bdac736
MA
4346 vec 3
4347 (if (zerop (length (tramp-file-name-user vec)))
4348 (format "Opening connection for %s using %s"
4349 (tramp-file-name-host vec)
4350 (tramp-file-name-method vec))
4351 (format "Opening connection for %s@%s using %s"
4352 (tramp-file-name-user vec)
4353 (tramp-file-name-host vec)
4354 (tramp-file-name-method vec)))
4355
4356 ;; Start new process.
4357 (when (and p (processp p))
4358 (delete-process p))
4359 (setenv "TERM" tramp-terminal-type)
4360 (setenv "LC_ALL" "C")
4361 (setenv "PROMPT_COMMAND")
4362 (setenv "PS1" tramp-initial-end-of-output)
4363 (let* ((target-alist (tramp-compute-multi-hops vec))
4364 (process-connection-type tramp-process-connection-type)
4365 (process-adaptive-read-buffering nil)
4366 (coding-system-for-read nil)
4367 ;; This must be done in order to avoid our file
4368 ;; name handler.
4369 (p (let ((default-directory
4370 (tramp-compat-temporary-file-directory)))
4371 (apply
4372 'start-process
4373 (tramp-get-connection-name vec)
4374 (tramp-get-connection-buffer vec)
4375 (if tramp-encoding-command-interactive
4376 (list tramp-encoding-shell
4377 tramp-encoding-command-interactive)
4378 (list tramp-encoding-shell))))))
4379
4380 ;; Set sentinel and query flag.
4381 (tramp-set-connection-property p "vector" vec)
4382 (set-process-sentinel p 'tramp-process-sentinel)
4383 (tramp-compat-set-process-query-on-exit-flag p nil)
2fe4b125 4384 (setq tramp-current-connection
07b151f1
MA
4385 (cons (butlast (append vec nil)) (current-time))
4386 tramp-current-host (system-name))
6d95bd46 4387
03c1ad43 4388 (tramp-message
6bdac736
MA
4389 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4390
4391 ;; Check whether process is alive.
4392 (tramp-barf-if-no-shell-prompt
4393 p 60
4394 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4395
4396 ;; Now do all the connections as specified.
4397 (while target-alist
4398 (let* ((hop (car target-alist))
4399 (l-method (tramp-file-name-method hop))
4400 (l-user (tramp-file-name-user hop))
4401 (l-host (tramp-file-name-host hop))
4402 (l-port nil)
4403 (login-program
4404 (tramp-get-method-parameter
4405 l-method 'tramp-login-program))
4406 (login-args
4407 (tramp-get-method-parameter
4408 l-method 'tramp-login-args))
4409 (async-args
4410 (tramp-get-method-parameter
4411 l-method 'tramp-async-args))
4412 (gw-args
4413 (tramp-get-method-parameter l-method 'tramp-gw-args))
4414 (gw (tramp-get-file-property hop "" "gateway" nil))
4415 (g-method (and gw (tramp-file-name-method gw)))
4416 (g-user (and gw (tramp-file-name-user gw)))
4417 (g-host (and gw (tramp-file-name-real-host gw)))
4418 (command login-program)
4419 ;; We don't create the temporary file. In
4420 ;; fact, it is just a prefix for the
4421 ;; ControlPath option of ssh; the real
4422 ;; temporary file has another name, and it is
4423 ;; created and protected by ssh. It is also
4424 ;; removed by ssh when the connection is
4425 ;; closed.
4426 (tmpfile
4427 (tramp-set-connection-property
4428 p "temp-file"
4429 (make-temp-name
4430 (expand-file-name
4431 tramp-temp-name-prefix
4432 (tramp-compat-temporary-file-directory)))))
07b151f1 4433 spec r-shell)
6bdac736
MA
4434
4435 ;; Add arguments for asynchronous processes.
4436 (when (and process-name async-args)
4437 (setq login-args (append async-args login-args)))
4438
4439 ;; Add gateway arguments if necessary.
4440 (when (and gw gw-args)
4441 (setq login-args (append gw-args login-args)))
4442
4443 ;; Check for port number. Until now, there's no
4444 ;; need for handling like method, user, host.
4445 (when (string-match tramp-host-with-port-regexp l-host)
4446 (setq l-port (match-string 2 l-host)
4447 l-host (match-string 1 l-host)))
4448
07b151f1
MA
4449 ;; Check, whether there is a restricted shell.
4450 (dolist (elt tramp-restricted-shell-hosts-alist)
4451 (when (string-match elt tramp-current-host)
4452 (setq r-shell t)))
4453
6bdac736
MA
4454 ;; Set variables for computing the prompt for
4455 ;; reading password. They can also be derived
4456 ;; from a gateway.
4457 (setq tramp-current-method (or g-method l-method)
4458 tramp-current-user (or g-user l-user)
4459 tramp-current-host (or g-host l-host))
4460
4461 ;; Replace login-args place holders.
4462 (setq
4463 l-host (or l-host "")
4464 l-user (or l-user "")
4465 l-port (or l-port "")
4466 spec (format-spec-make
4467 ?h l-host ?u l-user ?p l-port ?t tmpfile)
4468 command
4469 (concat
4470 ;; We do not want to see the trailing local
4471 ;; prompt in `start-file-process'.
07b151f1 4472 (unless r-shell "exec ")
6bdac736
MA
4473 command " "
4474 (mapconcat
4475 (lambda (x)
4476 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4477 (unless (member "" x) (mapconcat 'identity x " ")))
4478 login-args " ")
4479 ;; Local shell could be a Windows COMSPEC. It
4480 ;; doesn't know the ";" syntax, but we must exit
07b151f1
MA
4481 ;; always for `start-file-process'. It could
4482 ;; also be a restricted shell, which does not
4483 ;; allow "exec".
4484 (when r-shell " && exit || exit")))
6bdac736
MA
4485
4486 ;; Send the command.
4487 (tramp-message vec 3 "Sending command `%s'" command)
4488 (tramp-send-command vec command t t)
4489 (tramp-process-actions
4490 p vec pos tramp-actions-before-shell 60)
4491 (tramp-message
4492 vec 3 "Found remote shell prompt on `%s'" l-host))
4493 ;; Next hop.
4494 (setq target-alist (cdr target-alist)))
4495
4496 ;; Make initial shell settings.
4497 (tramp-open-connection-setup-interactive-shell p vec))))
4498
4499 ;; When the user did interrupt, we must cleanup.
4500 (quit
2fe4b125 4501 (tramp-cleanup vec)
6bdac736
MA
4502 ;; Propagate the quit signal.
4503 (signal (car err) (cdr err)))))))
03c1ad43
MA
4504
4505(defun tramp-send-command (vec command &optional neveropen nooutput)
4506 "Send the COMMAND to connection VEC.
4507Erases temporary buffer before sending the command. If optional
4508arg NEVEROPEN is non-nil, never try to open the connection. This
4509is meant to be used from `tramp-maybe-open-connection' only. The
4510function waits for output unless NOOUTPUT is set."
4511 (unless neveropen (tramp-maybe-open-connection vec))
4512 (let ((p (tramp-get-connection-process vec)))
4513 (when (tramp-get-connection-property p "remote-echo" nil)
4514 ;; We mark the command string that it can be erased in the output buffer.
4515 (tramp-set-connection-property p "check-remote-echo" t)
4516 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
98c8795a
MA
4517 (when (and (string-match "<<'EOF'" command)
4518 (not (tramp-get-connection-property vec "busybox" nil)))
6e060cee
MA
4519 ;; Unset $PS1 when using here documents, in order to avoid
4520 ;; multiple prompts.
01d884cf
MA
4521 (setq command (concat "(PS1= ; " command "\n)")))
4522 ;; Send the command.
03c1ad43
MA
4523 (tramp-message vec 6 "%s" command)
4524 (tramp-send-string vec command)
4525 (unless nooutput (tramp-wait-for-output p))))
4526
4527(defun tramp-wait-for-output (proc &optional timeout)
4528 "Wait for output from remote command."
4529 (unless (buffer-live-p (process-buffer proc))
4530 (delete-process proc)
4531 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4532 (with-current-buffer (process-buffer proc)
4533 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4534 ;; be leading escape sequences, which must be ignored.
4535 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4536 ;; Sometimes, the commands do not return a newline but a
4537 ;; null byte before the shell prompt, for example "git
4538 ;; ls-files -c -z ...".
4539 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4540 (found (tramp-wait-for-regexp proc timeout regexp1)))
4541 (if found
4542 (let (buffer-read-only)
4543 ;; A simple-minded busybox has sent " ^H" sequences.
4544 ;; Delete them.
4545 (goto-char (point-min))
6e060cee 4546 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
03c1ad43
MA
4547 (forward-line 1)
4548 (delete-region (point-min) (point)))
4549 ;; Delete the prompt.
4550 (goto-char (point-max))
4551 (re-search-backward regexp nil t)
4552 (delete-region (point) (point-max)))
4553 (if timeout
4554 (tramp-error
4555 proc 'file-error
4556 "[[Remote prompt `%s' not found in %d secs]]"
4557 tramp-end-of-output timeout)
4558 (tramp-error
4559 proc 'file-error
4560 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4561 ;; Return value is whether end-of-output sentinel was found.
4562 found)))
4563
4564(defun tramp-send-command-and-check
4565 (vec command &optional subshell dont-suppress-err)
4566 "Run COMMAND and check its exit status.
4567Sends `echo $?' along with the COMMAND for checking the exit status. If
4568COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4569
4570If the optional argument SUBSHELL is non-nil, the command is
4571executed in a subshell, ie surrounded by parentheses. If
4572DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4573 (tramp-send-command
4574 vec
4575 (concat (if subshell "( " "")
4576 command
4577 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4578 "echo tramp_exit_status $?"
4579 (if subshell " )" "")))
4580 (with-current-buffer (tramp-get-connection-buffer vec)
4581 (goto-char (point-max))
4582 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4583 (tramp-error
4584 vec 'file-error "Couldn't find exit status of `%s'" command))
4585 (skip-chars-forward "^ ")
4586 (prog1
4587 (zerop (read (current-buffer)))
4588 (let (buffer-read-only)
4589 (delete-region (match-beginning 0) (point-max))))))
4590
4591(defun tramp-barf-unless-okay (vec command fmt &rest args)
4592 "Run COMMAND, check exit status, throw error if exit status not okay.
4593Similar to `tramp-send-command-and-check' but accepts two more arguments
4594FMT and ARGS which are passed to `error'."
0b3f36df
MA
4595 (or (tramp-send-command-and-check vec command)
4596 (apply 'tramp-error vec 'file-error fmt args)))
03c1ad43 4597
6d95bd46 4598(defun tramp-send-command-and-read (vec command &optional noerror)
03c1ad43 4599 "Run COMMAND and return the output, which must be a Lisp expression.
6d95bd46
MA
4600In case there is no valid Lisp expression and NOERROR is nil, it
4601raises an error."
0b3f36df
MA
4602 (when (if noerror
4603 (tramp-send-command-and-check vec command)
4604 (tramp-barf-unless-okay
4605 vec command "`%s' returns with error" command))
4606 (with-current-buffer (tramp-get-connection-buffer vec)
4607 ;; Read the expression.
4608 (goto-char (point-min))
4609 (condition-case nil
4610 (prog1 (read (current-buffer))
4611 ;; Error handling.
4612 (when (re-search-forward "\\S-" (point-at-eol) t)
4613 (error nil)))
4614 (error (unless noerror
4615 (tramp-error
4616 vec 'file-error
4617 "`%s' does not return a valid Lisp expression: `%s'"
4618 command (buffer-string))))))))
03c1ad43 4619
03c1ad43
MA
4620(defun tramp-convert-file-attributes (vec attr)
4621 "Convert file-attributes ATTR generated by perl script, stat or ls.
4622Convert file mode bits to string and set virtual device number.
4623Return ATTR."
4624 (when attr
6d95bd46
MA
4625 ;; Remove color escape sequences from symlink.
4626 (when (stringp (car attr))
4627 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4628 (setcar attr (replace-match "" nil nil (car attr)))))
03c1ad43
MA
4629 ;; Convert last access time.
4630 (unless (listp (nth 4 attr))
4631 (setcar (nthcdr 4 attr)
4632 (list (floor (nth 4 attr) 65536)
4633 (floor (mod (nth 4 attr) 65536)))))
4634 ;; Convert last modification time.
4635 (unless (listp (nth 5 attr))
4636 (setcar (nthcdr 5 attr)
4637 (list (floor (nth 5 attr) 65536)
4638 (floor (mod (nth 5 attr) 65536)))))
4639 ;; Convert last status change time.
4640 (unless (listp (nth 6 attr))
4641 (setcar (nthcdr 6 attr)
4642 (list (floor (nth 6 attr) 65536)
4643 (floor (mod (nth 6 attr) 65536)))))
4644 ;; Convert file size.
4645 (when (< (nth 7 attr) 0)
4646 (setcar (nthcdr 7 attr) -1))
4647 (when (and (floatp (nth 7 attr))
4648 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4649 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4650 ;; Convert file mode bits to string.
4651 (unless (stringp (nth 8 attr))
4652 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4653 (when (stringp (car attr))
4654 (aset (nth 8 attr) 0 ?l)))
4655 ;; Convert directory indication bit.
4656 (when (string-match "^d" (nth 8 attr))
4657 (setcar attr t))
4658 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4659 (when (consp (car attr))
4660 (if (and (stringp (caar attr))
4661 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4662 (setcar attr (match-string 1 (caar attr)))
4663 (setcar attr nil)))
4664 ;; Set file's gid change bit.
4665 (setcar (nthcdr 9 attr)
4666 (if (numberp (nth 3 attr))
4667 (not (= (nth 3 attr)
4668 (tramp-get-remote-gid vec 'integer)))
4669 (not (string-equal
4670 (nth 3 attr)
4671 (tramp-get-remote-gid vec 'string)))))
4672 ;; Convert inode.
4673 (unless (listp (nth 10 attr))
4674 (setcar (nthcdr 10 attr)
4675 (condition-case nil
4676 (cons (floor (nth 10 attr) 65536)
4677 (floor (mod (nth 10 attr) 65536)))
4678 ;; Inodes can be incredible huge. We must hide this.
4679 (error (tramp-get-inode vec)))))
4680 ;; Set virtual device number.
4681 (setcar (nthcdr 11 attr)
4682 (tramp-get-device vec))
4683 attr))
4684
4685(defun tramp-check-cached-permissions (vec access)
4686 "Check `file-attributes' caches for VEC.
4687Return t if according to the cache access type ACCESS is known to
4688be granted."
4689 (let ((result nil)
4690 (offset (cond
4691 ((eq ?r access) 1)
4692 ((eq ?w access) 2)
4693 ((eq ?x access) 3))))
4694 (dolist (suffix '("string" "integer") result)
4695 (setq
4696 result
4697 (or
4698 result
4699 (let ((file-attr
4700 (tramp-get-file-property
4701 vec (tramp-file-name-localname vec)
4702 (concat "file-attributes-" suffix) nil))
4703 (remote-uid
4704 (tramp-get-connection-property
4705 vec (concat "uid-" suffix) nil))
4706 (remote-gid
4707 (tramp-get-connection-property
4708 vec (concat "gid-" suffix) nil)))
4709 (and
4710 file-attr
4711 (or
4712 ;; Not a symlink
4713 (eq t (car file-attr))
4714 (null (car file-attr)))
4715 (or
4716 ;; World accessible.
4717 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4718 ;; User accessible and owned by user.
4719 (and
4720 (eq access (aref (nth 8 file-attr) offset))
4721 (equal remote-uid (nth 2 file-attr)))
4722 ;; Group accessible and owned by user's
4723 ;; principal group.
4724 (and
4725 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4726 (equal remote-gid (nth 3 file-attr)))))))))))
4727
4728(defun tramp-file-mode-from-int (mode)
4729 "Turn an integer representing a file mode into an ls(1)-like string."
4730 (let ((type (cdr
4731 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4732 (user (logand (lsh mode -6) 7))
4733 (group (logand (lsh mode -3) 7))
4734 (other (logand (lsh mode -0) 7))
4735 (suid (> (logand (lsh mode -9) 4) 0))
4736 (sgid (> (logand (lsh mode -9) 2) 0))
4737 (sticky (> (logand (lsh mode -9) 1) 0)))
4738 (setq user (tramp-file-mode-permissions user suid "s"))
4739 (setq group (tramp-file-mode-permissions group sgid "s"))
4740 (setq other (tramp-file-mode-permissions other sticky "t"))
4741 (concat type user group other)))
4742
4743(defun tramp-file-mode-permissions (perm suid suid-text)
4744 "Convert a permission bitset into a string.
4745This is used internally by `tramp-file-mode-from-int'."
4746 (let ((r (> (logand perm 4) 0))
4747 (w (> (logand perm 2) 0))
4748 (x (> (logand perm 1) 0)))
4749 (concat (or (and r "r") "-")
4750 (or (and w "w") "-")
4751 (or (and suid x suid-text) ; suid, execute
4752 (and suid (upcase suid-text)) ; suid, !execute
4753 (and x "x") "-")))) ; !suid
4754
4755(defun tramp-shell-case-fold (string)
4756 "Converts STRING to shell glob pattern which ignores case."
4757 (mapconcat
4758 (lambda (c)
4759 (if (equal (downcase c) (upcase c))
4760 (vector c)
4761 (format "[%c%c]" (downcase c) (upcase c))))
4762 string
4763 ""))
4764
4765(defun tramp-make-copy-program-file-name (vec)
4766 "Create a file name suitable to be passed to `rcp' and workalikes."
4767 (let ((user (tramp-file-name-user vec))
4768 (host (tramp-file-name-real-host vec))
4769 (localname (tramp-shell-quote-argument
4770 (tramp-file-name-localname vec))))
4771 (if (not (zerop (length user)))
4772 (format "%s@%s:%s" user host localname)
4773 (format "%s:%s" host localname))))
4774
4775(defun tramp-method-out-of-band-p (vec size)
4776 "Return t if this is an out-of-band method, nil otherwise."
4777 (and
4778 ;; It shall be an out-of-band method.
4779 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
d59eb518
MA
4780 ;; There must be a size, otherwise the file doesn't exist.
4781 (numberp size)
03c1ad43
MA
4782 ;; Either the file size is large enough, or (in rare cases) there
4783 ;; does not exist a remote encoding.
4784 (or (null tramp-copy-size-limit)
4785 (> size tramp-copy-size-limit)
4786 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4787
03c1ad43
MA
4788;; Variables local to connection.
4789
4790(defun tramp-get-remote-path (vec)
1d51f99c 4791 (with-tramp-connection-property
03c1ad43
MA
4792 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4793 ;; cache the result for the session only. Otherwise, the result
4794 ;; is cached persistently.
4795 (if (memq 'tramp-own-remote-path tramp-remote-path)
4796 (tramp-get-connection-process vec)
4797 vec)
4798 "remote-path"
4799 (let* ((remote-path (copy-tree tramp-remote-path))
4800 (elt1 (memq 'tramp-default-remote-path remote-path))
4801 (elt2 (memq 'tramp-own-remote-path remote-path))
4802 (default-remote-path
4803 (when elt1
a857d3c7
MA
4804 (or
4805 (tramp-send-command-and-read
6d95bd46 4806 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
a857d3c7
MA
4807 ;; Default if "getconf" is not available.
4808 (progn
03c1ad43
MA
4809 (tramp-message
4810 vec 3
4811 "`getconf PATH' not successful, using default value \"%s\"."
4812 "/bin:/usr/bin")
4813 "/bin:/usr/bin"))))
4814 (own-remote-path
4815 (when elt2
4816 (condition-case nil
4817 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
03c1ad43
MA
4818 (error
4819 (tramp-message
4820 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4821 nil)))))
4822
4823 ;; Replace place holder `tramp-default-remote-path'.
4824 (when elt1
4825 (setcdr elt1
4826 (append
4827 (tramp-compat-split-string default-remote-path ":")
4828 (cdr elt1)))
4829 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4830
4831 ;; Replace place holder `tramp-own-remote-path'.
4832 (when elt2
4833 (setcdr elt2
4834 (append
4835 (tramp-compat-split-string own-remote-path ":")
4836 (cdr elt2)))
4837 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4838
4839 ;; Remove double entries.
4840 (setq elt1 remote-path)
4841 (while (consp elt1)
4842 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4843 (setcar elt2 nil))
4844 (setq elt1 (cdr elt1)))
4845
4846 ;; Remove non-existing directories.
4847 (delq
4848 nil
4849 (mapcar
4850 (lambda (x)
4851 (and
4852 (stringp x)
4853 (file-directory-p
4854 (tramp-make-tramp-file-name
4855 (tramp-file-name-method vec)
4856 (tramp-file-name-user vec)
4857 (tramp-file-name-host vec)
4858 x))
4859 x))
4860 remote-path)))))
4861
03c1ad43 4862(defun tramp-get-ls-command (vec)
1d51f99c 4863 (with-tramp-connection-property vec "ls"
03c1ad43
MA
4864 (tramp-message vec 5 "Finding a suitable `ls' command")
4865 (or
4866 (catch 'ls-found
4867 (dolist (cmd '("ls" "gnuls" "gls"))
4868 (let ((dl (tramp-get-remote-path vec))
4869 result)
4870 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4871 ;; Check parameters. On busybox, "ls" output coloring is
4872 ;; enabled by default sometimes. So we try to disable it
4873 ;; when possible. $LS_COLORING is not supported there.
4874 ;; Some "ls" versions are sensible wrt the order of
4875 ;; arguments, they fail when "-al" is after the
4876 ;; "--color=never" argument (for example on FreeBSD).
4877 (when (tramp-send-command-and-check
4878 vec (format "%s -lnd /" result))
4879 (when (tramp-send-command-and-check
4880 vec (format
4881 "%s --color=never -al /dev/null" result))
4882 (setq result (concat result " --color=never")))
4883 (throw 'ls-found result))
4884 (setq dl (cdr dl))))))
4885 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4886
4887(defun tramp-get-ls-command-with-dired (vec)
4888 (save-match-data
1d51f99c 4889 (with-tramp-connection-property vec "ls-dired"
03c1ad43
MA
4890 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4891 ;; Some "ls" versions are sensible wrt the order of arguments,
4892 ;; they fail when "-al" is after the "--dired" argument (for
4893 ;; example on FreeBSD).
4894 (tramp-send-command-and-check
4895 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4896
4897(defun tramp-get-test-command (vec)
1d51f99c 4898 (with-tramp-connection-property vec "test"
03c1ad43
MA
4899 (tramp-message vec 5 "Finding a suitable `test' command")
4900 (if (tramp-send-command-and-check vec "test 0")
4901 "test"
4902 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4903
4904(defun tramp-get-test-nt-command (vec)
4905 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4906 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4907 ;; for otherwise the shell crashes.
1d51f99c 4908 (with-tramp-connection-property vec "test-nt"
03c1ad43
MA
4909 (or
4910 (progn
4911 (tramp-send-command
4912 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4913 (with-current-buffer (tramp-get-buffer vec)
4914 (goto-char (point-min))
4915 (when (looking-at (regexp-quote tramp-end-of-output))
4916 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4917 (progn
4918 (tramp-send-command
4919 vec
4920 (format
4921 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4922 (tramp-get-test-command vec)))
4923 "tramp_test_nt %s %s"))))
4924
4925(defun tramp-get-file-exists-command (vec)
1d51f99c 4926 (with-tramp-connection-property vec "file-exists"
03c1ad43
MA
4927 (tramp-message vec 5 "Finding command to check if file exists")
4928 (tramp-find-file-exists-command vec)))
4929
4930(defun tramp-get-remote-ln (vec)
1d51f99c 4931 (with-tramp-connection-property vec "ln"
03c1ad43
MA
4932 (tramp-message vec 5 "Finding a suitable `ln' command")
4933 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4934
4935(defun tramp-get-remote-perl (vec)
1d51f99c 4936 (with-tramp-connection-property vec "perl"
03c1ad43
MA
4937 (tramp-message vec 5 "Finding a suitable `perl' command")
4938 (let ((result
4939 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4940 (tramp-find-executable
4941 vec "perl" (tramp-get-remote-path vec)))))
4942 ;; We must check also for some Perl modules.
4943 (when result
1d51f99c 4944 (with-tramp-connection-property vec "perl-file-spec"
03c1ad43
MA
4945 (tramp-send-command-and-check
4946 vec (format "%s -e 'use File::Spec;'" result)))
1d51f99c 4947 (with-tramp-connection-property vec "perl-cwd-realpath"
03c1ad43
MA
4948 (tramp-send-command-and-check
4949 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
4950 result)))
4951
4952(defun tramp-get-remote-stat (vec)
1d51f99c 4953 (with-tramp-connection-property vec "stat"
03c1ad43
MA
4954 (tramp-message vec 5 "Finding a suitable `stat' command")
4955 (let ((result (tramp-find-executable
4956 vec "stat" (tramp-get-remote-path vec)))
4957 tmp)
6d95bd46 4958 ;; Check whether stat(1) returns usable syntax. "%s" does not
03c1ad43
MA
4959 ;; work on older AIX systems.
4960 (when result
4961 (setq tmp
6d95bd46
MA
4962 (tramp-send-command-and-read
4963 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
03c1ad43
MA
4964 (unless (and (listp tmp) (stringp (car tmp))
4965 (string-match "^./.$" (car tmp))
4966 (integerp (cadr tmp)))
4967 (setq result nil)))
4968 result)))
4969
4970(defun tramp-get-remote-readlink (vec)
1d51f99c 4971 (with-tramp-connection-property vec "readlink"
03c1ad43
MA
4972 (tramp-message vec 5 "Finding a suitable `readlink' command")
4973 (let ((result (tramp-find-executable
4974 vec "readlink" (tramp-get-remote-path vec))))
4975 (when (and result
6d95bd46
MA
4976 (tramp-send-command-and-check
4977 vec (format "%s --canonicalize-missing /" result)))
03c1ad43
MA
4978 result))))
4979
4980(defun tramp-get-remote-trash (vec)
1d51f99c 4981 (with-tramp-connection-property vec "trash"
03c1ad43
MA
4982 (tramp-message vec 5 "Finding a suitable `trash' command")
4983 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
4984
4985(defun tramp-get-remote-id (vec)
1d51f99c 4986 (with-tramp-connection-property vec "id"
03c1ad43
MA
4987 (tramp-message vec 5 "Finding POSIX `id' command")
4988 (or
4989 (catch 'id-found
4990 (let ((dl (tramp-get-remote-path vec))
4991 result)
4992 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
4993 ;; Check POSIX parameter.
4994 (when (tramp-send-command-and-check vec (format "%s -u" result))
4995 (throw 'id-found result))
4996 (setq dl (cdr dl)))))
4997 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
4998
4999(defun tramp-get-remote-uid (vec id-format)
1d51f99c 5000 (with-tramp-connection-property vec (format "uid-%s" id-format)
03c1ad43
MA
5001 (let ((res (tramp-send-command-and-read
5002 vec
5003 (format "%s -u%s %s"
5004 (tramp-get-remote-id vec)
5005 (if (equal id-format 'integer) "" "n")
5006 (if (equal id-format 'integer)
5007 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5008 ;; The command might not always return a number.
5009 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5010
5011(defun tramp-get-remote-gid (vec id-format)
1d51f99c 5012 (with-tramp-connection-property vec (format "gid-%s" id-format)
03c1ad43
MA
5013 (let ((res (tramp-send-command-and-read
5014 vec
5015 (format "%s -g%s %s"
5016 (tramp-get-remote-id vec)
5017 (if (equal id-format 'integer) "" "n")
5018 (if (equal id-format 'integer)
5019 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5020 ;; The command might not always return a number.
5021 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5022
5023(defun tramp-get-local-uid (id-format)
5024 (if (equal id-format 'integer) (user-uid) (user-login-name)))
5025
5026(defun tramp-get-local-gid (id-format)
97976f9f
PE
5027 (if (and (fboundp 'group-gid) (equal id-format 'integer))
5028 (tramp-compat-funcall 'group-gid)
5029 (nth 3 (tramp-compat-file-attributes "~/" id-format))))
03c1ad43
MA
5030
5031;; Some predefined connection properties.
5032(defun tramp-get-inline-compress (vec prop size)
5033 "Return the compress command related to PROP.
5034PROP is either `inline-compress' or `inline-decompress'. SIZE is
5035the length of the file to be compressed.
5036
5037If no corresponding command is found, nil is returned."
5038 (when (and (integerp tramp-inline-compress-start-size)
5039 (> size tramp-inline-compress-start-size))
1d51f99c 5040 (with-tramp-connection-property (tramp-get-connection-process vec) prop
03c1ad43 5041 (tramp-find-inline-compress vec)
2fe4b125
MA
5042 (tramp-get-connection-property
5043 (tramp-get-connection-process vec) prop nil))))
03c1ad43
MA
5044
5045(defun tramp-get-inline-coding (vec prop size)
5046 "Return the coding command related to PROP.
40ba43b4 5047PROP is either `remote-encoding', `remote-decoding',
03c1ad43
MA
5048`local-encoding' or `local-decoding'.
5049
5050SIZE is the length of the file to be coded. Depending on SIZE,
5051compression might be applied.
5052
5053If no corresponding command is found, nil is returned.
5054Otherwise, either a string is returned which contains a `%s' mark
5055to be used for the respective input or output file; or a Lisp
5056function cell is returned to be applied on a buffer."
01d884cf
MA
5057 ;; We must catch the errors, because we want to return `nil', when
5058 ;; no inline coding is found.
5059 (ignore-errors
5060 (let ((coding
1d51f99c
MA
5061 (with-tramp-connection-property
5062 (tramp-get-connection-process vec) prop
01d884cf 5063 (tramp-find-inline-encoding vec)
2fe4b125
MA
5064 (tramp-get-connection-property
5065 (tramp-get-connection-process vec) prop nil)))
01d884cf
MA
5066 (prop1 (if (string-match "encoding" prop)
5067 "inline-compress" "inline-decompress"))
5068 compress)
5069 ;; The connection property might have been cached. So we must
5070 ;; send the script to the remote side - maybe.
5071 (when (and coding (symbolp coding) (string-match "remote" prop))
5072 (let ((name (symbol-name coding)))
5073 (while (string-match (regexp-quote "-") name)
5074 (setq name (replace-match "_" nil t name)))
5075 (tramp-maybe-send-script vec (symbol-value coding) name)
5076 (setq coding name)))
5077 (when coding
5078 ;; Check for the `compress' command.
5079 (setq compress (tramp-get-inline-compress vec prop1 size))
5080 ;; Return the value.
5081 (cond
5082 ((and compress (symbolp coding))
5083 (if (string-match "decompress" prop1)
5084 `(lambda (beg end)
5085 (,coding beg end)
5086 (let ((coding-system-for-write 'binary)
5087 (coding-system-for-read 'binary))
5088 (apply
5089 'call-process-region (point-min) (point-max)
5090 (car (split-string ,compress)) t t nil
5091 (cdr (split-string ,compress)))))
03c1ad43 5092 `(lambda (beg end)
03c1ad43
MA
5093 (let ((coding-system-for-write 'binary)
5094 (coding-system-for-read 'binary))
5095 (apply
01d884cf 5096 'call-process-region beg end
03c1ad43 5097 (car (split-string ,compress)) t t nil
01d884cf
MA
5098 (cdr (split-string ,compress))))
5099 (,coding (point-min) (point-max)))))
5100 ((symbolp coding)
5101 coding)
5102 ((and compress (string-match "decoding" prop))
362b9d48
GM
5103 (format
5104 ;; Windows shells need the program file name after
5105 ;; the pipe symbol be quoted if they use forward
5106 ;; slashes as directory separators.
5107 (if (and (string-match "local" prop)
5108 (memq system-type '(windows-nt)))
5109 "(%s | \"%s\" >%%s)"
5110 "(%s | %s >%%s)")
5111 coding compress))
01d884cf 5112 (compress
362b9d48
GM
5113 (format
5114 ;; Windows shells need the program file name after
5115 ;; the pipe symbol be quoted if they use forward
5116 ;; slashes as directory separators.
5117 (if (and (string-match "local" prop)
5118 (memq system-type '(windows-nt)))
5119 "(%s <%%s | \"%s\")"
5120 "(%s <%%s | %s)")
5121 compress coding))
01d884cf
MA
5122 ((string-match "decoding" prop)
5123 (format "%s >%%s" coding))
5124 (t
5125 (format "%s <%%s" coding)))))))
03c1ad43
MA
5126
5127;;; Integration of eshell.el:
5128
5129(eval-when-compile
5130 (defvar eshell-path-env))
5131
5132;; eshell.el keeps the path in `eshell-path-env'. We must change it
5133;; when `default-directory' points to another host.
5134(defun tramp-eshell-directory-change ()
5135 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
5136 (setq eshell-path-env
5137 (if (file-remote-p default-directory)
5138 (with-parsed-tramp-file-name default-directory nil
5139 (mapconcat
5140 'identity
5141 (tramp-get-remote-path v)
5142 ":"))
5143 (getenv "PATH"))))
5144
5145(eval-after-load "esh-util"
5146 '(progn
5147 (tramp-eshell-directory-change)
5148 (add-hook 'eshell-directory-change-hook
5149 'tramp-eshell-directory-change)
5150 (add-hook 'tramp-unload-hook
5151 (lambda ()
5152 (remove-hook 'eshell-directory-change-hook
5153 'tramp-eshell-directory-change)))))
5154
5155(add-hook 'tramp-unload-hook
5156 (lambda ()
5157 (unload-feature 'tramp-sh 'force)))
5158
5159(provide 'tramp-sh)
5160
5161;;; TODO:
5162
5163;; * Don't use globbing for directories with many files, as this is
5164;; likely to produce long command lines, and some shells choke on
5165;; long command lines.
5166;; * Make it work for different encodings, and for different file name
5167;; encodings, too. (Daniel Pittman)
5168;; * Don't search for perl5 and perl. Instead, only search for perl and
5169;; then look if it's the right version (with `perl -v').
5170;; * When editing a remote CVS controlled file as a different user, VC
5171;; gets confused about the file locking status. Try to find out why
5172;; the workaround doesn't work.
5173;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5174;; until the last but one hop via `start-file-process'. Apply it
5175;; also for ftp and smb.
5176;; * WIBNI if we had a command "trampclient"? If I was editing in
e1dbe924 5177;; some shell with root privileges, it would be nice if I could
03c1ad43
MA
5178;; just call
5179;; trampclient filename.c
5180;; as an editor, and the _current_ shell would connect to an Emacs
e1dbe924 5181;; server and would be used in an existing non-privileged Emacs
03c1ad43
MA
5182;; session for doing the editing in question.
5183;; That way, I need not tell Emacs my password again and be afraid
5184;; that it makes it into core dumps or other ugly stuff (I had Emacs
5185;; once display a just typed password in the context of a keyboard
5186;; sequence prompt for a question immediately following in a shell
5187;; script run within Emacs -- nasty).
5188;; And if I have some ssh session running to a different computer,
5189;; having the possibility of passing a local file there to a local
5190;; Emacs session (in case I can arrange for a connection back) would
5191;; be nice.
5192;; Likely the corresponding Tramp server should not allow the
5193;; equivalent of the emacsclient -eval option in order to make this
5194;; reasonably unproblematic. And maybe trampclient should have some
5195;; way of passing credentials, like by using an SSL socket or
5196;; something. (David Kastrup)
5197;; * Reconnect directly to a compliant shell without first going
5198;; through the user's default shell. (Pete Forman)
5199;; * How can I interrupt the remote process with a signal
5200;; (interrupt-process seems not to work)? (Markus Triska)
5201;; * Avoid the local shell entirely for starting remote processes. If
5202;; so, I think even a signal, when delivered directly to the local
5203;; SSH instance, would correctly be propagated to the remote process
5204;; automatically; possibly SSH would have to be started with
5205;; "-t". (Markus Triska)
5206;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5207;; isn't on the remote host. (Mark A. Hershberger)
5208;; * Use lsh instead of ssh. (Alfred M. Szmidt)
2c68ca0e 5209;; * Optimize out-of-band copying when both methods are scp-like (not
03c1ad43
MA
5210;; rsync).
5211;; * Keep a second connection open for out-of-band methods like scp or
5212;; rsync.
5213;; * Try telnet+curl as new method. It might be useful for busybox,
5214;; without built-in uuencode/uudecode.
5215
5216;;; tramp-sh.el ends here