1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
5 ;; (copyright statements below in code to be updated with the above notice)
7 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
12 ;; This file is part of GNU Emacs.
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.
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.
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/>.
31 ;; Pacify byte-compiler.
35 (defvar directory-sep-char)
36 (defvar tramp-gw-tunnel-method)
37 (defvar tramp-gw-socks-method)
38 (defvar vc-handled-backends)
39 (defvar vc-bzr-program)
40 (defvar vc-git-program)
41 (defvar vc-hg-program)
44 (defcustom tramp-inline-compress-start-size 4096
45 "The minimum size of compressing where inline transfer.
46 When inline transfer, compress transferred data of file
47 whose size is this value or above (up to `tramp-copy-size-limit').
48 If it is nil, no compression at all will be applied."
50 :type '(choice (const nil) integer))
53 (defcustom tramp-copy-size-limit 10240
54 "The maximum file size where inline copying is preferred over an \
56 If it is nil, out-of-the-band copy will be used without a check."
58 :type '(choice (const nil) integer))
61 (defcustom tramp-terminal-type "dumb"
62 "Value of TERM environment variable for logging in to remote host.
63 Because Tramp wants to parse the output of the remote shell, it is easily
64 confused by ANSI color escape sequences and suchlike. Often, shell init
65 files conditionalize this setup based on the TERM environment variable."
70 (defcustom tramp-histfile-override ".tramp_history"
71 "When invoking a shell, override the HISTFILE with this value.
72 When setting to a string, it redirects the shell history to that
73 file. Be careful when setting to \"/dev/null\"; this might
74 result in undesired results when using \"bash\" as shell.
76 The value t, the default value, unsets any setting of HISTFILE,
77 and sets both HISTFILESIZE and HISTSIZE to 0. If you set this
78 variable to nil, however, the *override* is disabled, so the
79 history will go to the default storage location,
80 e.g. \"$HOME/.sh_history\"."
83 :type '(choice (const :tag "Do not override HISTFILE" nil)
84 (const :tag "Unset HISTFILE" t)
85 (string :tag "Redirect to a file")))
88 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
89 "Escape sequences produced by the \"ls\" command.")
91 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
92 ;; root users. It uses the `$' character for other users. In order
93 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
95 (defvar tramp-end-of-output
98 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
99 "String used to recognize end of output.
100 The `$' character at the end is quoted; the string cannot be
101 detected as prompt when being sent on echoing hosts, therefore.")
104 (defconst tramp-initial-end-of-output "#$ "
105 "Prompt when establishing a connection.")
107 (defconst tramp-end-of-heredoc (md5 tramp-end-of-output)
108 "String used to recognize end of heredoc strings.")
111 (defcustom tramp-use-ssh-controlmaster-options t
112 "Whether to use `tramp-ssh-controlmaster-options'."
117 (defvar tramp-ssh-controlmaster-options nil
118 "Which ssh Control* arguments to use.
120 If it is a string, it should have the form
121 \"-o ControlMaster=auto -o ControlPath='tramp.%%r@%%h:%%p'
122 -o ControlPersist=no\". Percent characters in the ControlPath
123 spec must be doubled, because the string is used as format string.
125 Otherwise, it will be auto-detected by Tramp, if
126 `tramp-use-ssh-controlmaster-options' is non-nil. The value
127 depends on the installed local ssh version.
129 The string is used in `tramp-methods'.")
131 ;; Initialize `tramp-methods' with the supported methods.
133 (add-to-list 'tramp-methods
135 (tramp-login-program "rsh")
136 (tramp-login-args (("%h") ("-l" "%u")))
137 (tramp-remote-shell "/bin/sh")
138 (tramp-remote-shell-login ("-l"))
139 (tramp-remote-shell-args ("-c"))
140 (tramp-copy-program "rcp")
141 (tramp-copy-args (("-p" "%k") ("-r")))
142 (tramp-copy-keep-date t)
143 (tramp-copy-recursive t)))
145 (add-to-list 'tramp-methods
147 (tramp-login-program "remsh")
148 (tramp-login-args (("%h") ("-l" "%u")))
149 (tramp-remote-shell "/bin/sh")
150 (tramp-remote-shell-login ("-l"))
151 (tramp-remote-shell-args ("-c"))
152 (tramp-copy-program "rcp")
153 (tramp-copy-args (("-p" "%k")))
154 (tramp-copy-keep-date t)))
156 (add-to-list 'tramp-methods
158 (tramp-login-program "ssh")
159 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
160 ("-e" "none") ("%h")))
161 (tramp-async-args (("-q")))
162 (tramp-remote-shell "/bin/sh")
163 (tramp-remote-shell-login ("-l"))
164 (tramp-remote-shell-args ("-c"))
165 (tramp-copy-program "scp")
166 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
167 (tramp-copy-keep-date t)
168 (tramp-copy-recursive t)
169 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
170 ("-o" "UserKnownHostsFile=/dev/null")
171 ("-o" "StrictHostKeyChecking=no")))
172 (tramp-default-port 22)))
174 (add-to-list 'tramp-methods
176 (tramp-login-program "ssh")
177 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
178 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
179 (tramp-async-args (("-q")))
180 (tramp-remote-shell "/bin/sh")
181 (tramp-remote-shell-login ("-l"))
182 (tramp-remote-shell-args ("-c"))
183 (tramp-copy-program "scp")
184 (tramp-copy-args (("-P" "%p") ("-p" "%k")
185 ("-q") ("-r") ("%c")))
186 (tramp-copy-keep-date t)
187 (tramp-copy-recursive t)
188 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
189 ("-o" "UserKnownHostsFile=/dev/null")
190 ("-o" "StrictHostKeyChecking=no")))
191 (tramp-default-port 22)))
193 (add-to-list 'tramp-methods
195 (tramp-login-program "ssh")
196 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
197 ("-e" "none") ("%h")))
198 (tramp-async-args (("-q")))
199 (tramp-remote-shell "/bin/sh")
200 (tramp-remote-shell-login ("-l"))
201 (tramp-remote-shell-args ("-c"))
202 (tramp-copy-program "rsync")
203 (tramp-copy-args (("-t" "%k") ("-r")))
204 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
205 (tramp-copy-keep-date t)
206 (tramp-copy-keep-tmpfile t)
207 (tramp-copy-recursive t)))
209 (add-to-list 'tramp-methods
211 (tramp-login-program "rsh")
212 (tramp-login-args (("%h") ("-l" "%u")))
213 (tramp-remote-shell "/bin/sh")
214 (tramp-remote-shell-login ("-l"))
215 (tramp-remote-shell-args ("-c"))))
217 (add-to-list 'tramp-methods
219 (tramp-login-program "remsh")
220 (tramp-login-args (("%h") ("-l" "%u")))
221 (tramp-remote-shell "/bin/sh")
222 (tramp-remote-shell-login ("-l"))
223 (tramp-remote-shell-args ("-c"))))
225 (add-to-list 'tramp-methods
227 (tramp-login-program "ssh")
228 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
229 ("-e" "none") ("%h")))
230 (tramp-async-args (("-q")))
231 (tramp-remote-shell "/bin/sh")
232 (tramp-remote-shell-login ("-l"))
233 (tramp-remote-shell-args ("-c"))
234 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
235 ("-o" "UserKnownHostsFile=/dev/null")
236 ("-o" "StrictHostKeyChecking=no")))
237 (tramp-default-port 22)))
239 (add-to-list 'tramp-methods
241 (tramp-login-program "ssh")
242 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
243 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
244 (tramp-async-args (("-q")))
245 (tramp-remote-shell "/bin/sh")
246 (tramp-remote-shell-login ("-l"))
247 (tramp-remote-shell-args ("-c"))
248 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
249 ("-o" "UserKnownHostsFile=/dev/null")
250 ("-o" "StrictHostKeyChecking=no")))
251 (tramp-default-port 22)))
253 (add-to-list 'tramp-methods
255 (tramp-login-program "telnet")
256 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
257 (tramp-remote-shell "/bin/sh")
258 (tramp-remote-shell-login ("-l"))
259 (tramp-remote-shell-args ("-c"))
260 (tramp-default-port 23)))
262 (add-to-list 'tramp-methods
264 (tramp-login-program "telnet")
265 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
266 (tramp-remote-shell "/bin/sh")
267 (tramp-remote-shell-login ("-l"))
268 (tramp-remote-shell-args ("-c"))
269 (tramp-copy-program "nc")
270 ;; We use "-v" for better error tracking.
271 (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r")))
272 (tramp-remote-copy-program "nc")
273 ;; We use "-p" as required for newer busyboxes. For older
274 ;; busybox/nc versions, the value must be (("-l") ("%r")). This
275 ;; can be achieved by tweaking `tramp-connection-properties'.
276 (tramp-remote-copy-args (("-l") ("-p" "%r") ("2>/dev/null")))
277 (tramp-default-port 23)))
279 (add-to-list 'tramp-methods
281 (tramp-login-program "su")
282 (tramp-login-args (("-") ("%u")))
283 (tramp-remote-shell "/bin/sh")
284 (tramp-remote-shell-login ("-l"))
285 (tramp-remote-shell-args ("-c"))
286 (tramp-connection-timeout 10)))
288 (add-to-list 'tramp-methods
290 (tramp-login-program "sudo")
291 ;; The password template must be masked. Otherwise, it could be
292 ;; interpreted as password prompt if the remote host echoes the command.
293 (tramp-login-args (("-u" "%u") ("-s") ("-H")
294 ("-p" "P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")))
295 ;; Local $SHELL could be a nasty one, like zsh or fish. Let's override it.
296 (tramp-login-env (("SHELL") ("/bin/sh")))
297 (tramp-remote-shell "/bin/sh")
298 (tramp-remote-shell-login ("-l"))
299 (tramp-remote-shell-args ("-c"))
300 (tramp-connection-timeout 10)))
302 (add-to-list 'tramp-methods
304 (tramp-login-program "ksu")
305 (tramp-login-args (("%u") ("-q")))
306 (tramp-remote-shell "/bin/sh")
307 (tramp-remote-shell-login ("-l"))
308 (tramp-remote-shell-args ("-c"))
309 (tramp-connection-timeout 10)))
311 (add-to-list 'tramp-methods
313 (tramp-login-program "krlogin")
314 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
315 (tramp-remote-shell "/bin/sh")
316 (tramp-remote-shell-login ("-l"))
317 (tramp-remote-shell-args ("-c"))))
319 (add-to-list 'tramp-methods
321 (tramp-login-program "plink")
322 ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
323 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
326 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
328 tramp-initial-end-of-output))
330 (tramp-remote-shell "/bin/sh")
331 (tramp-remote-shell-login ("-l"))
332 (tramp-remote-shell-args ("-c"))
333 (tramp-default-port 22)))
335 (add-to-list 'tramp-methods
337 (tramp-login-program "plink")
338 (tramp-login-args (("-load") ("%h") ("-t") ("\"")
340 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
342 tramp-initial-end-of-output))
344 (tramp-remote-shell "/bin/sh")
345 (tramp-remote-shell-login ("-l"))
346 (tramp-remote-shell-args ("-c"))))
348 (add-to-list 'tramp-methods
350 (tramp-login-program "plink")
351 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
354 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
356 tramp-initial-end-of-output))
358 (tramp-remote-shell "/bin/sh")
359 (tramp-remote-shell-login ("-l"))
360 (tramp-remote-shell-args ("-c"))
361 (tramp-copy-program "pscp")
362 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
364 (tramp-copy-keep-date t)
365 (tramp-copy-recursive t)
366 (tramp-default-port 22)))
368 (add-to-list 'tramp-methods
370 (tramp-login-program "plink")
371 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
374 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
376 tramp-initial-end-of-output))
378 (tramp-remote-shell "/bin/sh")
379 (tramp-remote-shell-login ("-l"))
380 (tramp-remote-shell-args ("-c"))
381 (tramp-copy-program "pscp")
382 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
384 (tramp-copy-keep-date t)
385 (tramp-copy-recursive t)))
387 (add-to-list 'tramp-methods
389 (tramp-login-program "fsh")
390 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
391 (tramp-remote-shell "/bin/sh")
392 (tramp-remote-shell-login ("-l"))
393 (tramp-remote-shell-args ("-i") ("-c"))
394 (tramp-copy-program "fcp")
395 (tramp-copy-args (("-p" "%k")))
396 (tramp-copy-keep-date t)))
399 (add-to-list 'tramp-default-method-alist
400 `(,tramp-local-host-regexp "\\`root\\'" "su"))
403 (add-to-list 'tramp-default-user-alist
404 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
406 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
407 ;; Do not add "plink" based methods, they ask interactively for the user.
409 (add-to-list 'tramp-default-user-alist
413 '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
415 nil ,(user-login-name)))
418 (defconst tramp-completion-function-alist-rsh
419 '((tramp-parse-rhosts "/etc/hosts.equiv")
420 (tramp-parse-rhosts "~/.rhosts"))
421 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
424 (defconst tramp-completion-function-alist-ssh
425 '((tramp-parse-rhosts "/etc/hosts.equiv")
426 (tramp-parse-rhosts "/etc/shosts.equiv")
427 (tramp-parse-shosts "/etc/ssh_known_hosts")
428 (tramp-parse-sconfig "/etc/ssh_config")
429 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
430 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
431 (tramp-parse-rhosts "~/.rhosts")
432 (tramp-parse-rhosts "~/.shosts")
433 (tramp-parse-shosts "~/.ssh/known_hosts")
434 (tramp-parse-sconfig "~/.ssh/config")
435 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
436 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
437 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
440 (defconst tramp-completion-function-alist-telnet
441 '((tramp-parse-hosts "/etc/hosts"))
442 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
445 (defconst tramp-completion-function-alist-su
446 '((tramp-parse-passwd "/etc/passwd"))
447 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
450 (defconst tramp-completion-function-alist-putty
452 ,(if (memq system-type '(windows-nt))
453 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
454 "~/.putty/sessions")))
455 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
458 (eval-after-load 'tramp
460 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
461 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
462 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
463 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
464 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
465 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
466 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
467 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
468 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
469 (tramp-set-completion-function
470 "telnet" tramp-completion-function-alist-telnet)
471 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)
472 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
473 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
474 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
475 (tramp-set-completion-function
476 "krlogin" tramp-completion-function-alist-rsh)
477 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
478 (tramp-set-completion-function
479 "plinkx" tramp-completion-function-alist-putty)
480 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
481 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)
482 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
484 ;; "getconf PATH" yields:
485 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
486 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
487 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
488 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
489 ;; Darwin: /usr/bin:/bin:/usr/sbin:/sbin
492 (defcustom tramp-remote-path
493 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
494 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
495 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
496 "/opt/bin" "/opt/sbin" "/opt/local/bin")
497 "List of directories to search for executables on remote host.
498 For every remote host, this variable will be set buffer local,
499 keeping the list of existing directories on that host.
501 You can use `~' in this list, but when searching for a shell which groks
502 tilde expansion, all directory names starting with `~' will be ignored.
504 `Default Directories' represent the list of directories given by
505 the command \"getconf PATH\". It is recommended to use this
506 entry on top of this list, because these are the default
507 directories for POSIX compatible commands. On remote hosts which
508 do not offer the getconf command (like cygwin), the value
509 \"/bin:/usr/bin\" is used instead of.
511 `Private Directories' are the settings of the $PATH environment,
512 as given in your `~/.profile'."
514 :type '(repeat (choice
515 (const :tag "Default Directories" tramp-default-remote-path)
516 (const :tag "Private Directories" tramp-own-remote-path)
517 (string :tag "Directory"))))
520 (defcustom tramp-remote-process-environment
521 `("TMOUT=0" "LC_CTYPE=''"
522 ,(format "TERM=%s" tramp-terminal-type)
523 "EMACS=t" ;; Deprecated; remove this line once Bash 4.4-or-later is common.
524 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
525 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=cat"
526 "autocorrect=" "correct=")
527 "List of environment variables to be set on the remote host.
529 Each element should be a string of the form ENVVARNAME=VALUE. An
530 entry ENVVARNAME= disables the corresponding environment variable,
531 which might have been set in the init files like ~/.profile.
533 Special handling is applied to the PATH environment, which should
534 not be set here. Instead, it should be set via `tramp-remote-path'."
537 :type '(repeat string))
540 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
541 "Alist specifying extra arguments to pass to the remote shell.
542 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
543 matching the shell file name and ARGS is a string specifying the
546 This variable is only used when Tramp needs to start up another shell
547 for tilde expansion. The extra arguments should typically prevent the
548 shell from reading its init file."
550 ;; This might be the wrong way to test whether the widget type
551 ;; `alist' is available. Who knows the right way to test it?
552 :type (if (get 'alist 'widget-type)
553 '(alist :key-type string :value-type string)
554 '(repeat (cons string string))))
556 (defconst tramp-actions-before-shell
557 '((tramp-login-prompt-regexp tramp-action-login)
558 (tramp-password-prompt-regexp tramp-action-password)
559 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
560 (shell-prompt-pattern tramp-action-succeed)
561 (tramp-shell-prompt-pattern tramp-action-succeed)
562 (tramp-yesno-prompt-regexp tramp-action-yesno)
563 (tramp-yn-prompt-regexp tramp-action-yn)
564 (tramp-terminal-prompt-regexp tramp-action-terminal)
565 (tramp-process-alive-regexp tramp-action-process-alive))
566 "List of pattern/action pairs.
567 Whenever a pattern matches, the corresponding action is performed.
568 Each item looks like (PATTERN ACTION).
570 The PATTERN should be a symbol, a variable. The value of this
571 variable gives the regular expression to search for. Note that the
572 regexp must match at the end of the buffer, \"\\'\" is implicitly
575 The ACTION should also be a symbol, but a function. When the
576 corresponding PATTERN matches, the ACTION function is called.")
578 (defconst tramp-actions-copy-out-of-band
579 '((tramp-password-prompt-regexp tramp-action-password)
580 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
581 (tramp-copy-failed-regexp tramp-action-permission-denied)
582 (tramp-process-alive-regexp tramp-action-out-of-band))
583 "List of pattern/action pairs.
584 This list is used for copying/renaming with out-of-band methods.
586 See `tramp-actions-before-shell' for more info.")
588 (defconst tramp-uudecode
589 "(echo begin 600 %t; tail -n +2) | uudecode
592 "Shell function to implement `uudecode' to standard output.
593 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
594 for this or `uudecode -p', but some systems don't, and for them
595 we have this shell function.")
597 (defconst tramp-perl-file-truename
600 use Cwd \"realpath\";
604 return realpath($file) if -e $file;
608 my ($volume, @dirs) = @_;
609 my $real = myrealpath(File::Spec->catpath(
610 $volume, File::Spec->catdir(@dirs), \"\"));
612 my ($vol, $dir) = File::Spec->splitpath($real, 1);
613 return ($vol, File::Spec->splitdir($dir));
616 my $last = pop(@dirs);
617 ($volume, @dirs) = recursive($volume, @dirs);
619 return ($volume, @dirs);
623 $result = myrealpath($ARGV[0]);
625 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
626 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
628 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
631 $result =~ s/\"/\\\\\"/g;
632 print \"\\\"$result\\\"\\n\";
633 ' \"$1\" 2>/dev/null"
634 "Perl script to produce output suitable for use with `file-truename'
635 on the remote file system.
636 Escape sequence %s is replaced with name of Perl binary.
637 This string is passed to `format', so percent characters need to be doubled.")
639 (defconst tramp-perl-file-name-all-completions
649 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
650 @files = readdir(d); closedir(d);
651 foreach $f (@files) {
652 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
653 if (-d \"$ARGV[0]/$f\") {
662 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
663 "Perl script to produce output suitable for use with
664 `file-name-all-completions' on the remote file system. Escape
665 sequence %s is replaced with name of Perl binary. This string is
666 passed to `format', so percent characters need to be doubled.")
668 ;; Perl script to implement `file-attributes' in a Lisp `read'able
669 ;; output. If you are hacking on this, note that you get *no* output
670 ;; unless this spits out a complete line, including the '\n' at the
672 ;; The device number is returned as "-1", because there will be a virtual
673 ;; device number set in `tramp-sh-handle-file-attributes'.
674 (defconst tramp-perl-file-attributes
676 @stat = lstat($ARGV[0]);
681 if (($stat[2] & 0170000) == 0120000)
683 $type = readlink($ARGV[0]);
684 $type =~ s/\"/\\\\\"/g;
685 $type = \"\\\"$type\\\"\";
687 elsif (($stat[2] & 0170000) == 040000)
695 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
696 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
698 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
703 $stat[8] >> 16 & 0xffff,
705 $stat[9] >> 16 & 0xffff,
707 $stat[10] >> 16 & 0xffff,
711 $stat[1] >> 16 & 0xffff,
713 );' \"$1\" \"$2\" 2>/dev/null"
714 "Perl script to produce output suitable for use with `file-attributes'
715 on the remote file system.
716 Escape sequence %s is replaced with name of Perl binary.
717 This string is passed to `format', so percent characters need to be doubled.")
719 (defconst tramp-perl-directory-files-and-attributes
721 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
722 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
723 @list = readdir(DIR);
727 for($i = 0; $i < $n; $i++)
729 $filename = $list[$i];
730 @stat = lstat($filename);
731 if (($stat[2] & 0170000) == 0120000)
733 $type = readlink($filename);
734 $type =~ s/\"/\\\\\"/g;
735 $type = \"\\\"$type\\\"\";
737 elsif (($stat[2] & 0170000) == 040000)
745 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
746 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
747 $filename =~ s/\"/\\\\\"/g;
749 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
755 $stat[8] >> 16 & 0xffff,
757 $stat[9] >> 16 & 0xffff,
759 $stat[10] >> 16 & 0xffff,
763 $stat[1] >> 16 & 0xffff,
765 $stat[0] >> 16 & 0xffff,
768 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
769 "Perl script implementing `directory-files-attributes' as Lisp `read'able
771 Escape sequence %s is replaced with name of Perl binary.
772 This string is passed to `format', so percent characters need to be doubled.")
774 ;; These two use base64 encoding.
775 (defconst tramp-perl-encode-with-module
776 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
777 "Perl program to use for encoding a file.
778 Escape sequence %s is replaced with name of Perl binary.
779 This string is passed to `format', so percent characters need to be doubled.
780 This implementation requires the MIME::Base64 Perl module to be installed
781 on the remote host.")
783 (defconst tramp-perl-decode-with-module
784 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
785 "Perl program to use for decoding a file.
786 Escape sequence %s is replaced with name of Perl binary.
787 This string is passed to `format', so percent characters need to be doubled.
788 This implementation requires the MIME::Base64 Perl module to be installed
789 on the remote host.")
791 (defconst tramp-perl-encode
793 # This script contributed by Juanma Barranquero <lektu@terra.es>.
794 # Copyright (C) 2002-2016 Free Software Foundation, Inc.
799 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
800 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
804 # We read in chunks of 54 bytes, to generate output lines
805 # of 72 chars (plus end of line)
806 while (read STDIN, $data, 54) {
809 # Only for the last chunk, and only if did not fill the last three-byte packet
811 my $mod = length($data) %% 3;
812 $pad = q(=) x (3 - $mod) if $mod;
815 # Not the fastest method, but it is simple: unpack to binary string, split
816 # by groups of 6 bits and convert back from binary to byte; then map into
817 # the translation table
821 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
825 "Perl program to use for encoding a file.
826 Escape sequence %s is replaced with name of Perl binary.
827 This string is passed to `format', so percent characters need to be doubled.")
829 (defconst tramp-perl-decode
831 # This script contributed by Juanma Barranquero <lektu@terra.es>.
832 # Copyright (C) 2002-2016 Free Software Foundation, Inc.
837 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
838 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
841 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
845 # We are going to accumulate into $pending to accept any line length
846 # (we do not check they are <= 76 chars as the RFC says)
849 while (my $data = <STDIN>) {
852 # If we find one or two =, we have reached the end and
853 # any following data is to be discarded
854 my $finished = $data =~ s/(==?).*/$1/;
857 my $len = length($pending);
858 my $chunk = substr($pending, 0, $len & ~3);
859 $pending = substr($pending, $len & ~3 + 1);
861 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
862 # split in 8-bit chunks and convert back to char.
865 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
869 "Perl program to use for decoding a file.
870 Escape sequence %s is replaced with name of Perl binary.
871 This string is passed to `format', so percent characters need to be doubled.")
873 (defconst tramp-perl-pack
874 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
875 "Perl program to use for encoding a file.
876 Escape sequence %s is replaced with name of Perl binary.")
878 (defconst tramp-perl-unpack
879 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
880 "Perl program to use for decoding a file.
881 Escape sequence %s is replaced with name of Perl binary.")
883 (defconst tramp-awk-encode
884 "od -v -t x1 -A n | busybox awk '\\
886 b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
887 b16 = \"0123456789abcdef\"
890 for (c=1; c<=length($0); c++) {
891 d=index(b16, substr($0,c,1))
893 for (b=1; b<=4; b++) {
894 o=o*2+int(d/8); d=(d*2)%%16
896 printf substr(b64,o+1,1)
897 if (++rc>75) { printf \"\\n\"; rc=0 }
906 tail=(obc==2) ? \"==\\n\" : \"=\\n\"
907 while (obc++<6) { o=o*2 }
908 printf \"%%c\", substr(b64,o+1,1)
914 "Awk program to use for encoding a file.
915 This string is passed to `format', so percent characters need to be doubled.")
917 (defconst tramp-awk-decode
920 b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
923 for (i=1; i<=length($0); i++) {
924 c=index(b64, substr($0,i,1))
927 o=o*2+int(c/32); c=(c*2)%%64
932 system(\"dd if=/dev/zero bs=1 count=1 2>/dev/null\")
940 "Awk program to use for decoding a file.
941 This string is passed to `format', so percent characters need to be doubled.")
943 (defconst tramp-awk-coding-test
944 "test -c /dev/zero && \
945 od -v -t x1 -A n </dev/null && \
946 busybox awk '{}' </dev/null"
947 "Test command for checking `tramp-awk-encode' and `tramp-awk-decode'.")
949 (defconst tramp-stat-marker "/////"
950 "Marker in stat commands for file attributes.")
952 (defconst tramp-stat-quoted-marker "\\/\\/\\/\\/\\/"
953 "Quoted marker in stat commands for file attributes.")
955 (defconst tramp-vc-registered-read-file-names
958 if %s \"$file\"; then
959 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
961 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
963 if %s \"$file\"; then
964 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
966 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
970 "Script to check existence of VC related files.
971 It must be send formatted with two strings; the tests for file
972 existence, and file readability. Input shall be read via
973 here-document, otherwise the command could exceed maximum length
976 ;; New handlers should be added here.
977 (defconst tramp-sh-file-name-handler-alist
978 '(;; `access-file' performed by default handler.
979 (add-name-to-file . tramp-sh-handle-add-name-to-file)
980 ;; `byte-compiler-base-file-name' performed by default handler.
981 (copy-directory . tramp-sh-handle-copy-directory)
982 (copy-file . tramp-sh-handle-copy-file)
983 (delete-directory . tramp-sh-handle-delete-directory)
984 (delete-file . tramp-sh-handle-delete-file)
985 ;; `diff-latest-backup-file' performed by default handler.
986 (directory-file-name . tramp-handle-directory-file-name)
987 (directory-files . tramp-handle-directory-files)
988 (directory-files-and-attributes
989 . tramp-sh-handle-directory-files-and-attributes)
990 ;; `dired-call-process' performed by default handler.
991 (dired-compress-file . tramp-sh-handle-dired-compress-file)
992 (dired-recursive-delete-directory
993 . tramp-sh-handle-dired-recursive-delete-directory)
994 (dired-uncache . tramp-handle-dired-uncache)
995 (expand-file-name . tramp-sh-handle-expand-file-name)
996 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
997 (file-acl . tramp-sh-handle-file-acl)
998 (file-attributes . tramp-sh-handle-file-attributes)
999 (file-directory-p . tramp-sh-handle-file-directory-p)
1000 (file-equal-p . tramp-handle-file-equal-p)
1001 (file-executable-p . tramp-sh-handle-file-executable-p)
1002 (file-exists-p . tramp-sh-handle-file-exists-p)
1003 (file-in-directory-p . tramp-handle-file-in-directory-p)
1004 (file-local-copy . tramp-sh-handle-file-local-copy)
1005 (file-modes . tramp-handle-file-modes)
1006 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
1007 (file-name-as-directory . tramp-handle-file-name-as-directory)
1008 (file-name-completion . tramp-handle-file-name-completion)
1009 (file-name-directory . tramp-handle-file-name-directory)
1010 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1011 ;; `file-name-sans-versions' performed by default handler.
1012 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
1013 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch)
1014 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
1015 (file-notify-valid-p . tramp-handle-file-notify-valid-p)
1016 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
1017 (file-readable-p . tramp-sh-handle-file-readable-p)
1018 (file-regular-p . tramp-handle-file-regular-p)
1019 (file-remote-p . tramp-handle-file-remote-p)
1020 (file-selinux-context . tramp-sh-handle-file-selinux-context)
1021 (file-symlink-p . tramp-handle-file-symlink-p)
1022 (file-truename . tramp-sh-handle-file-truename)
1023 (file-writable-p . tramp-sh-handle-file-writable-p)
1024 (find-backup-file-name . tramp-handle-find-backup-file-name)
1025 ;; `find-file-noselect' performed by default handler.
1026 ;; `get-file-buffer' performed by default handler.
1027 (insert-directory . tramp-sh-handle-insert-directory)
1028 (insert-file-contents . tramp-handle-insert-file-contents)
1029 (insert-file-contents-literally
1030 . tramp-sh-handle-insert-file-contents-literally)
1031 (load . tramp-handle-load)
1032 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1033 (make-directory . tramp-sh-handle-make-directory)
1034 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
1035 (process-file . tramp-sh-handle-process-file)
1036 (rename-file . tramp-sh-handle-rename-file)
1037 (set-file-acl . tramp-sh-handle-set-file-acl)
1038 (set-file-modes . tramp-sh-handle-set-file-modes)
1039 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
1040 (set-file-times . tramp-sh-handle-set-file-times)
1041 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
1042 (shell-command . tramp-handle-shell-command)
1043 (start-file-process . tramp-sh-handle-start-file-process)
1044 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
1045 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1046 (vc-registered . tramp-sh-handle-vc-registered)
1047 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
1048 (write-region . tramp-sh-handle-write-region))
1049 "Alist of handler functions.
1050 Operations not mentioned here will be handled by the normal Emacs functions.")
1052 ;; This must be the last entry, because `identity' always matches.
1053 ;;;###tramp-autoload
1054 (add-to-list 'tramp-foreign-file-name-handler-alist
1055 '(identity . tramp-sh-file-name-handler) 'append)
1057 ;;; File Name Handler Functions:
1059 (defun tramp-sh-handle-make-symbolic-link
1060 (filename linkname &optional ok-if-already-exists)
1061 "Like `make-symbolic-link' for Tramp files.
1062 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1063 the symlink. If LINKNAME is a Tramp file, only the localname component is
1064 used as the target of the symlink.
1066 If LINKNAME is a Tramp file and the localname component is relative, then
1067 it is expanded first, before the localname component is taken. Note that
1068 this can give surprising results if the user/host for the source and
1069 target of the symlink differ."
1070 (with-parsed-tramp-file-name linkname l
1071 (let ((ln (tramp-get-remote-ln l))
1072 (cwd (tramp-run-real-handler
1073 'file-name-directory (list l-localname))))
1077 "Making a symbolic link. ln(1) does not exist on the remote host."))
1079 ;; Do the 'confirm if exists' thing.
1080 (when (file-exists-p linkname)
1082 (if (or (null ok-if-already-exists) ; not allowed to exist
1083 (and (numberp ok-if-already-exists)
1086 "File %s already exists; make it a link anyway? "
1089 l 'file-already-exists "File %s already exists" l-localname)
1090 (delete-file linkname)))
1092 ;; If FILENAME is a Tramp name, use just the localname component.
1093 (when (tramp-tramp-file-p filename)
1095 (tramp-file-name-localname
1096 (tramp-dissect-file-name (expand-file-name filename)))))
1098 (tramp-flush-file-property l (file-name-directory l-localname))
1099 (tramp-flush-file-property l l-localname)
1101 ;; Right, they are on the same host, regardless of user, method,
1102 ;; etc. We now make the link on the remote machine. This will
1103 ;; occur as the user that FILENAME belongs to.
1104 (and (tramp-send-command-and-check
1105 l (format "cd %s" (tramp-shell-quote-argument cwd)))
1106 (tramp-send-command-and-check
1110 (tramp-shell-quote-argument filename)
1111 ;; The command could exceed PATH_MAX, so we use
1112 ;; relative file names. However, relative file names
1113 ;; could start with "-". `tramp-shell-quote-argument'
1114 ;; does not handle this, we must do it ourselves.
1115 (tramp-shell-quote-argument
1116 (concat "./" (file-name-nondirectory l-localname)))))))))
1118 (defun tramp-sh-handle-file-truename (filename)
1119 "Like `file-truename' for Tramp files."
1122 (with-parsed-tramp-file-name (expand-file-name filename) nil
1123 (tramp-make-tramp-file-name
1125 (with-tramp-file-property v localname "file-truename"
1126 (let ((result nil)) ; result steps in reverse order
1127 (tramp-message v 4 "Finding true name for `%s'" filename)
1129 ;; Use GNU readlink --canonicalize-missing where available.
1130 ((tramp-get-remote-readlink v)
1131 (tramp-send-command-and-check
1133 (format "%s --canonicalize-missing %s"
1134 (tramp-get-remote-readlink v)
1135 (tramp-shell-quote-argument localname)))
1136 (with-current-buffer (tramp-get-connection-buffer v)
1137 (goto-char (point-min))
1138 (setq result (buffer-substring (point-min) (point-at-eol)))))
1140 ;; Use Perl implementation.
1141 ((and (tramp-get-remote-perl v)
1142 (tramp-get-connection-property v "perl-file-spec" nil)
1143 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1144 (tramp-maybe-send-script
1145 v tramp-perl-file-truename "tramp_perl_file_truename")
1147 (tramp-send-command-and-read
1149 (format "tramp_perl_file_truename %s"
1150 (tramp-shell-quote-argument localname)))))
1152 ;; Do it yourself. We bind `directory-sep-char' here for
1153 ;; XEmacs on Windows, which would otherwise use backslash.
1154 (t (let ((directory-sep-char ?/)
1155 (steps (tramp-compat-split-string localname "/"))
1158 ;; Don't make the following value larger than
1159 ;; necessary. People expect an error message in a
1160 ;; timely fashion when something is wrong;
1161 ;; otherwise they might think that Emacs is hung.
1162 ;; Of course, correctness has to come first.
1165 (while (and steps (< numchase numchase-limit))
1166 (setq thisstep (pop steps))
1169 (mapconcat 'identity
1170 (append '("") (reverse result) (list thisstep))
1172 (setq symlink-target
1173 (nth 0 (file-attributes
1174 (tramp-make-tramp-file-name
1176 (mapconcat 'identity
1181 (cond ((string= "." thisstep)
1182 (tramp-message v 5 "Ignoring step `.'"))
1183 ((string= ".." thisstep)
1184 (tramp-message v 5 "Processing step `..'")
1186 ((stringp symlink-target)
1187 ;; It's a symlink, follow it.
1189 v 5 "Follow symlink to %s" symlink-target)
1190 (setq numchase (1+ numchase))
1191 (when (file-name-absolute-p symlink-target)
1193 ;; If the symlink was absolute, we'll get a
1194 ;; string like "/user@host:/some/target";
1195 ;; extract the "/some/target" part from it.
1196 (when (tramp-tramp-file-p symlink-target)
1197 (unless (tramp-equal-remote filename symlink-target)
1200 "Symlink target `%s' on wrong host"
1202 (setq symlink-target localname))
1204 (append (tramp-compat-split-string
1209 (setq result (cons thisstep result)))))
1210 (when (>= numchase numchase-limit)
1213 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1214 (setq result (reverse result))
1215 ;; Combine list to form string.
1218 (mapconcat 'identity (cons "" result) "/")
1220 (when (string= "" result)
1221 (setq result "/")))))
1223 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1226 ;; Preserve trailing "/".
1227 (if (string-equal (file-name-nondirectory filename) "") "/" "")))
1231 (defun tramp-sh-handle-file-exists-p (filename)
1232 "Like `file-exists-p' for Tramp files."
1233 (with-parsed-tramp-file-name filename nil
1234 (with-tramp-file-property v localname "file-exists-p"
1235 (or (not (null (tramp-get-file-property
1236 v localname "file-attributes-integer" nil)))
1237 (not (null (tramp-get-file-property
1238 v localname "file-attributes-string" nil)))
1239 (tramp-send-command-and-check
1243 (tramp-get-file-exists-command v)
1244 (tramp-shell-quote-argument localname)))))))
1246 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1247 "Like `file-attributes' for Tramp files."
1248 (unless id-format (setq id-format 'integer))
1250 ;; Don't modify `last-coding-system-used' by accident.
1251 (let ((last-coding-system-used last-coding-system-used))
1252 (with-parsed-tramp-file-name (expand-file-name filename) nil
1253 (with-tramp-file-property
1254 v localname (format "file-attributes-%s" id-format)
1256 (tramp-convert-file-attributes
1260 ((tramp-get-remote-stat v)
1261 (tramp-do-file-attributes-with-stat v localname id-format))
1262 ((tramp-get-remote-perl v)
1263 (tramp-do-file-attributes-with-perl v localname id-format))
1265 ;; The scripts could fail, for example with huge file size.
1266 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
1268 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1269 "Implement `file-attributes' for Tramp files using the ls(1) command."
1271 res-inode res-filemodes res-numlinks
1272 res-uid res-gid res-size res-symlink-target)
1273 (tramp-message vec 5 "file attributes with ls: %s" localname)
1274 ;; We cannot send all three commands combined, it could exceed
1275 ;; NAME_MAX or PATH_MAX. Happened on Mac OS X, for example.
1276 (when (or (tramp-send-command-and-check
1279 (tramp-get-file-exists-command vec)
1280 (tramp-shell-quote-argument localname)))
1281 (tramp-send-command-and-check
1284 (tramp-get-test-command vec)
1285 (tramp-shell-quote-argument localname))))
1288 (format "%s %s %s %s"
1289 (tramp-get-ls-command vec)
1290 (if (eq id-format 'integer) "-ildn" "-ild")
1291 ;; On systems which have no quoting style, file names
1292 ;; with special characters could fail.
1294 ((tramp-get-ls-command-with-quoting-style vec)
1295 "--quoting-style=c")
1296 ((tramp-get-ls-command-with-w-option vec)
1299 (tramp-shell-quote-argument localname)))
1300 ;; Parse `ls -l' output ...
1301 (with-current-buffer (tramp-get-buffer vec)
1302 (when (> (buffer-size) 0)
1303 (goto-char (point-min))
1307 (read (current-buffer))
1308 (invalid-read-syntax
1309 (when (and (equal (cadr err)
1310 "Integer constant overflow in reader")
1312 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1314 (let* ((big (read (substring (car (cddr err)) 0
1315 (match-beginning 1))))
1316 (small (read (match-string 1 (car (cddr err)))))
1317 (twiddle (/ small 65536)))
1318 (cons (+ big twiddle)
1319 (- small (* twiddle 65536))))))))
1320 ;; ... file mode flags
1321 (setq res-filemodes (symbol-name (read (current-buffer))))
1323 (setq res-numlinks (read (current-buffer)))
1325 (setq res-uid (read (current-buffer)))
1326 (setq res-gid (read (current-buffer)))
1327 (if (eq id-format 'integer)
1329 (unless (numberp res-uid) (setq res-uid -1))
1330 (unless (numberp res-gid) (setq res-gid -1)))
1332 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1333 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1335 (setq res-size (read (current-buffer)))
1336 ;; From the file modes, figure out other stuff.
1337 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1338 (setq dirp (eq ?d (aref res-filemodes 0)))
1339 ;; If symlink, find out file name pointed to.
1341 (search-forward "-> ")
1342 (setq res-symlink-target
1343 (if (tramp-get-ls-command-with-quoting-style vec)
1344 (read (current-buffer))
1345 (buffer-substring (point) (point-at-eol)))))
1346 ;; Return data gathered.
1348 ;; 0. t for directory, string (name linked to) for symbolic
1350 (or dirp res-symlink-target)
1351 ;; 1. Number of links to file.
1357 ;; 4. Last access time, as a list of integers. Normally
1358 ;; this would be in the same format as `current-time', but
1359 ;; the subseconds part is not currently implemented, and (0
1360 ;; 0) denotes an unknown time.
1361 ;; 5. Last modification time, likewise.
1362 ;; 6. Last status change time, likewise.
1363 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1364 ;; 7. Size in bytes (-1, if number is out of range).
1366 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1368 ;; 9. t if file's gid would change if file were deleted and
1369 ;; recreated. Will be set in `tramp-convert-file-attributes'.
1371 ;; 10. Inode number.
1373 ;; 11. Device number. Will be replaced by a virtual device number.
1377 (defun tramp-do-file-attributes-with-perl
1378 (vec localname &optional id-format)
1379 "Implement `file-attributes' for Tramp files using a Perl script."
1380 (tramp-message vec 5 "file attributes with perl: %s" localname)
1381 (tramp-maybe-send-script
1382 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1383 (tramp-send-command-and-read
1385 (format "tramp_perl_file_attributes %s %s"
1386 (tramp-shell-quote-argument localname) id-format)))
1388 (defun tramp-do-file-attributes-with-stat
1389 (vec localname &optional id-format)
1390 "Implement `file-attributes' for Tramp files using stat(1) command."
1391 (tramp-message vec 5 "file attributes with stat: %s" localname)
1392 (tramp-send-command-and-read
1396 ;; On Opsware, pdksh (which is the true name of ksh there)
1397 ;; doesn't parse correctly the sequence "((". Therefore, we add
1398 ;; a space. Apostrophes in the stat output are masked as
1399 ;; `tramp-stat-marker', in order to make a proper shell escape of
1400 ;; them in file names.
1401 "( (%s %s || %s -h %s) && (%s -c "
1402 "'((%s%%N%s) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 %s%%A%s t %%ie0 -1)' "
1403 "%s | sed -e 's/\"/\\\\\"/g' -e 's/%s/\"/g') || echo nil)")
1404 (tramp-get-file-exists-command vec)
1405 (tramp-shell-quote-argument localname)
1406 (tramp-get-test-command vec)
1407 (tramp-shell-quote-argument localname)
1408 (tramp-get-remote-stat vec)
1409 tramp-stat-marker tramp-stat-marker
1410 (if (eq id-format 'integer)
1411 "%ue0" (concat tramp-stat-marker "%U" tramp-stat-marker))
1412 (if (eq id-format 'integer)
1413 "%ge0" (concat tramp-stat-marker "%G" tramp-stat-marker))
1414 tramp-stat-marker tramp-stat-marker
1415 (tramp-shell-quote-argument localname)
1416 tramp-stat-quoted-marker)))
1418 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1419 "Like `set-visited-file-modtime' for Tramp files."
1420 (unless (buffer-file-name)
1421 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1424 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1425 (let ((f (buffer-file-name))
1427 (with-parsed-tramp-file-name f nil
1428 (let* ((remote-file-name-inhibit-cache t)
1429 (attr (file-attributes f))
1430 ;; '(-1 65535) means file doesn't exists yet.
1431 (modtime (or (nth 5 attr) '(-1 65535))))
1432 (when (boundp 'last-coding-system-used)
1433 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1434 ;; We use '(0 0) as a don't-know value. See also
1435 ;; `tramp-do-file-attributes-with-ls'.
1436 (if (not (equal modtime '(0 0)))
1437 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1441 (format "%s -ild %s"
1442 (tramp-get-ls-command v)
1443 (tramp-shell-quote-argument localname)))
1444 (setq attr (buffer-substring (point) (point-at-eol))))
1445 (tramp-set-file-property
1446 v localname "visited-file-modtime-ild" attr))
1447 (when (boundp 'last-coding-system-used)
1448 (set 'last-coding-system-used coding-system-used))
1451 ;; This function makes the same assumption as
1452 ;; `tramp-sh-handle-set-visited-file-modtime'.
1453 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf)
1454 "Like `verify-visited-file-modtime' for Tramp files.
1455 At the time `verify-visited-file-modtime' calls this function, we
1456 already know that the buffer is visiting a file and that
1457 `visited-file-modtime' does not return 0. Do not call this
1458 function directly, unless those two cases are already taken care
1460 (with-current-buffer (or buf (current-buffer))
1461 (let ((f (buffer-file-name)))
1462 ;; There is no file visiting the buffer, or the buffer has no
1463 ;; recorded last modification time, or there is no established
1466 (eq (visited-file-modtime) 0)
1467 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1469 (with-parsed-tramp-file-name f nil
1470 (let* ((remote-file-name-inhibit-cache t)
1471 (attr (file-attributes f))
1472 (modtime (nth 5 attr))
1473 (mt (visited-file-modtime)))
1476 ;; File exists, and has a known modtime.
1477 ((and attr (not (equal modtime '(0 0))))
1478 (< (abs (tramp-time-diff
1480 ;; For compatibility, deal with both the old
1481 ;; (HIGH . LOW) and the new (HIGH LOW) return
1482 ;; values of `visited-file-modtime'.
1484 (list (car mt) (cdr mt))
1487 ;; Modtime has the don't know value.
1491 (format "%s -ild %s"
1492 (tramp-get-ls-command v)
1493 (tramp-shell-quote-argument localname)))
1494 (with-current-buffer (tramp-get-buffer v)
1495 (setq attr (buffer-substring (point) (point-at-eol))))
1498 (tramp-get-file-property
1499 v localname "visited-file-modtime-ild" "")))
1500 ;; If file does not exist, say it is not modified if and
1501 ;; only if that agrees with the buffer's record.
1502 (t (equal mt '(-1 65535))))))))))
1504 (defun tramp-sh-handle-set-file-modes (filename mode)
1505 "Like `set-file-modes' for Tramp files."
1506 (with-parsed-tramp-file-name filename nil
1507 (tramp-flush-file-property v (file-name-directory localname))
1508 (tramp-flush-file-property v localname)
1509 ;; FIXME: extract the proper text from chmod's stderr.
1510 (tramp-barf-unless-okay
1512 (format "chmod %s %s"
1513 (tramp-compat-decimal-to-octal mode)
1514 (tramp-shell-quote-argument localname))
1515 "Error while changing file's mode %s" filename)))
1517 (defun tramp-sh-handle-set-file-times (filename &optional time)
1518 "Like `set-file-times' for Tramp files."
1519 (if (tramp-tramp-file-p filename)
1520 (with-parsed-tramp-file-name filename nil
1521 (when (tramp-get-remote-touch v)
1522 (tramp-flush-file-property v (file-name-directory localname))
1523 (tramp-flush-file-property v localname)
1524 (let ((time (if (or (null time) (equal time '(0 0)))
1527 ;; With GNU Emacs, `format-time-string' has an
1528 ;; optional parameter UNIVERSAL. This is preferred,
1529 ;; because we could handle the case when the remote
1530 ;; host is located in a different time zone as the
1532 (utc (not (featurep 'xemacs))))
1533 (tramp-send-command-and-check
1536 (if utc "env TZ=UTC" "")
1537 (tramp-get-remote-touch v)
1538 (if (tramp-get-connection-property v "touch-t" nil)
1541 (format-time-string "%Y%m%d%H%M.%S" time t)
1542 (format-time-string "%Y%m%d%H%M.%S" time)))
1544 (tramp-shell-quote-argument localname))))))
1546 ;; We handle also the local part, because in older Emacsen,
1547 ;; without `set-file-times', this function is an alias for this.
1548 ;; We are local, so we don't need the UTC settings.
1551 nil "touch" nil nil nil "-t"
1552 (format-time-string "%Y%m%d%H%M.%S" time)
1553 (tramp-shell-quote-argument filename)))))
1555 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1556 "Set the ownership for FILENAME.
1557 If UID and GID are provided, these values are used; otherwise uid
1558 and gid of the corresponding user is taken. Both parameters must
1559 be non-negative integers."
1560 ;; Modern Unices allow chown only for root. So we might need
1561 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1562 ;; working with su(do)? when it is needed, so it shall succeed in
1563 ;; the majority of cases.
1564 ;; Don't modify `last-coding-system-used' by accident.
1565 (let ((last-coding-system-used last-coding-system-used))
1566 (if (tramp-tramp-file-p filename)
1567 (with-parsed-tramp-file-name filename nil
1568 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1569 ;; If we are root on the local host, we can do it directly.
1570 (tramp-set-file-uid-gid localname uid gid)
1571 (let ((uid (or (and (natnump uid) uid)
1572 (tramp-get-remote-uid v 'integer)))
1573 (gid (or (and (natnump gid) gid)
1574 (tramp-get-remote-gid v 'integer))))
1577 "chown %d:%d %s" uid gid
1578 (tramp-shell-quote-argument localname))))))
1580 ;; We handle also the local part, because there doesn't exist
1581 ;; `set-file-uid-gid'. On W32 "chown" might not work. We add a
1582 ;; timeout for this.
1583 (with-timeout (5 nil)
1584 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1585 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1587 nil "chown" nil nil nil
1588 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename)))))))
1590 (defun tramp-remote-selinux-p (vec)
1591 "Check, whether SELINUX is enabled on the remote host."
1592 (with-tramp-connection-property (tramp-get-connection-process vec) "selinux-p"
1593 (tramp-send-command-and-check vec "selinuxenabled")))
1595 (defun tramp-sh-handle-file-selinux-context (filename)
1596 "Like `file-selinux-context' for Tramp files."
1597 (with-parsed-tramp-file-name filename nil
1598 (with-tramp-file-property v localname "file-selinux-context"
1599 (let ((context '(nil nil nil nil))
1600 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1601 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1602 (when (and (tramp-remote-selinux-p v)
1603 (tramp-send-command-and-check
1606 (tramp-get-ls-command v)
1607 (tramp-shell-quote-argument localname))))
1608 (with-current-buffer (tramp-get-connection-buffer v)
1609 (goto-char (point-min))
1610 (when (re-search-forward regexp (point-at-eol) t)
1611 (setq context (list (match-string 1) (match-string 2)
1612 (match-string 3) (match-string 4))))))
1613 ;; Return the context.
1616 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1617 "Like `set-file-selinux-context' for Tramp files."
1618 (with-parsed-tramp-file-name filename nil
1619 (when (and (consp context)
1620 (tramp-remote-selinux-p v))
1621 (let ((user (and (stringp (nth 0 context)) (nth 0 context)))
1622 (role (and (stringp (nth 1 context)) (nth 1 context)))
1623 (type (and (stringp (nth 2 context)) (nth 2 context)))
1624 (range (and (stringp (nth 3 context)) (nth 3 context))))
1625 (when (tramp-send-command-and-check
1626 v (format "chcon %s %s %s %s %s"
1627 (if user (format "--user=%s" user) "")
1628 (if role (format "--role=%s" role) "")
1629 (if type (format "--type=%s" type) "")
1630 (if range (format "--range=%s" range) "")
1631 (tramp-shell-quote-argument localname)))
1632 (if (and user role type range)
1633 (tramp-set-file-property
1634 v localname "file-selinux-context" context)
1635 (tramp-set-file-property
1636 v localname "file-selinux-context" 'undef))
1639 (defun tramp-remote-acl-p (vec)
1640 "Check, whether ACL is enabled on the remote host."
1641 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1642 (tramp-send-command-and-check vec "getfacl /")))
1644 (defun tramp-sh-handle-file-acl (filename)
1645 "Like `file-acl' for Tramp files."
1646 (with-parsed-tramp-file-name filename nil
1647 (with-tramp-file-property v localname "file-acl"
1648 (when (and (tramp-remote-acl-p v)
1649 (tramp-send-command-and-check
1652 (tramp-shell-quote-argument localname))))
1653 (with-current-buffer (tramp-get-connection-buffer v)
1654 (goto-char (point-max))
1655 (delete-blank-lines)
1656 (when (> (point-max) (point-min))
1657 (tramp-compat-funcall
1658 'substring-no-properties (buffer-string))))))))
1660 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1661 "Like `set-file-acl' for Tramp files."
1662 (with-parsed-tramp-file-name (expand-file-name filename) nil
1663 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1666 v (format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1667 (tramp-shell-quote-argument localname)
1668 tramp-end-of-heredoc
1670 tramp-end-of-heredoc))
1671 (tramp-send-command-and-check v nil)))
1674 (tramp-set-file-property v localname "file-acl" acl-string)
1676 ;; In case of errors, we return nil.
1677 (tramp-set-file-property v localname "file-acl-string" 'undef)
1680 ;; Simple functions using the `test' command.
1682 (defun tramp-sh-handle-file-executable-p (filename)
1683 "Like `file-executable-p' for Tramp files."
1684 (with-parsed-tramp-file-name filename nil
1685 (with-tramp-file-property v localname "file-executable-p"
1686 ;; Examine `file-attributes' cache to see if request can be
1687 ;; satisfied without remote operation.
1688 (or (tramp-check-cached-permissions v ?x)
1689 (tramp-run-test "-x" filename)))))
1691 (defun tramp-sh-handle-file-readable-p (filename)
1692 "Like `file-readable-p' for Tramp files."
1693 (with-parsed-tramp-file-name filename nil
1694 (with-tramp-file-property v localname "file-readable-p"
1695 ;; Examine `file-attributes' cache to see if request can be
1696 ;; satisfied without remote operation.
1697 (or (tramp-check-cached-permissions v ?r)
1698 (tramp-run-test "-r" filename)))))
1700 ;; When the remote shell is started, it looks for a shell which groks
1701 ;; tilde expansion. Here, we assume that all shells which grok tilde
1702 ;; expansion will also provide a `test' command which groks `-nt' (for
1703 ;; newer than). If this breaks, tell me about it and I'll try to do
1704 ;; something smarter about it.
1705 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1706 "Like `file-newer-than-file-p' for Tramp files."
1707 (cond ((not (file-exists-p file1))
1709 ((not (file-exists-p file2))
1711 ;; We are sure both files exist at this point.
1714 ;; We try to get the mtime of both files. If they are not
1715 ;; equal to the "dont-know" value, then we subtract the times
1716 ;; and obtain the result.
1717 (let ((fa1 (file-attributes file1))
1718 (fa2 (file-attributes file2)))
1719 (if (and (not (equal (nth 5 fa1) '(0 0)))
1720 (not (equal (nth 5 fa2) '(0 0))))
1721 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1722 ;; If one of them is the dont-know value, then we can
1723 ;; still try to run a shell command on the remote host.
1724 ;; However, this only works if both files are Tramp
1725 ;; files and both have the same method, same user, same
1727 (unless (tramp-equal-remote file1 file2)
1728 (with-parsed-tramp-file-name
1729 (if (tramp-tramp-file-p file1) file1 file2) nil
1732 "Files %s and %s must have same method, user, host"
1734 (with-parsed-tramp-file-name file1 nil
1736 (tramp-get-test-nt-command v) file1 file2))))))))
1738 ;; Functions implemented using the basic functions above.
1740 (defun tramp-sh-handle-file-directory-p (filename)
1741 "Like `file-directory-p' for Tramp files."
1742 (with-parsed-tramp-file-name filename nil
1743 ;; `file-directory-p' is used as predicate for file name completion.
1744 ;; Sometimes, when a connection is not established yet, it is
1745 ;; desirable to return t immediately for "/method:foo:". It can
1746 ;; be expected that this is always a directory.
1747 (or (zerop (length localname))
1748 (with-tramp-file-property v localname "file-directory-p"
1749 (tramp-run-test "-d" filename)))))
1751 (defun tramp-sh-handle-file-writable-p (filename)
1752 "Like `file-writable-p' for Tramp files."
1753 (with-parsed-tramp-file-name filename nil
1754 (with-tramp-file-property v localname "file-writable-p"
1755 (if (file-exists-p filename)
1756 ;; Examine `file-attributes' cache to see if request can be
1757 ;; satisfied without remote operation.
1758 (or (tramp-check-cached-permissions v ?w)
1759 (tramp-run-test "-w" filename))
1760 ;; If file doesn't exist, check if directory is writable.
1761 (and (tramp-run-test "-d" (file-name-directory filename))
1762 (tramp-run-test "-w" (file-name-directory filename)))))))
1764 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1765 "Like `file-ownership-preserved-p' for Tramp files."
1766 (with-parsed-tramp-file-name filename nil
1767 (with-tramp-file-property v localname "file-ownership-preserved-p"
1768 (let ((attributes (file-attributes filename)))
1769 ;; Return t if the file doesn't exist, since it's true that no
1770 ;; information would be lost by an (attempted) delete and create.
1771 (or (null attributes)
1773 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1775 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1777 ;; Directory listings.
1779 (defun tramp-sh-handle-directory-files-and-attributes
1780 (directory &optional full match nosort id-format)
1781 "Like `directory-files-and-attributes' for Tramp files."
1782 (unless id-format (setq id-format 'integer))
1783 (when (file-directory-p directory)
1784 (setq directory (expand-file-name directory))
1787 (with-parsed-tramp-file-name directory nil
1788 (with-tramp-file-property
1790 (format "directory-files-and-attributes-%s" id-format)
1795 (tramp-convert-file-attributes v (cdr x))))
1798 ((tramp-get-remote-stat v)
1799 (tramp-do-directory-files-and-attributes-with-stat
1800 v localname id-format))
1801 ((tramp-get-remote-perl v)
1802 (tramp-do-directory-files-and-attributes-with-perl
1803 v localname id-format))
1808 (setq item (pop temp))
1809 (when (or (null match) (string-match match (car item)))
1811 (setcar item (expand-file-name (car item) directory)))
1812 (push item result)))
1816 (sort result (lambda (x y) (string< (car x) (car y)))))
1817 ;; The scripts could fail, for example with huge file size.
1818 (tramp-handle-directory-files-and-attributes
1819 directory full match nosort id-format)))))
1821 (defun tramp-do-directory-files-and-attributes-with-perl
1822 (vec localname &optional id-format)
1823 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1824 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1825 (tramp-maybe-send-script
1826 vec tramp-perl-directory-files-and-attributes
1827 "tramp_perl_directory_files_and_attributes")
1829 (tramp-send-command-and-read
1831 (format "tramp_perl_directory_files_and_attributes %s %s"
1832 (tramp-shell-quote-argument localname) id-format))))
1833 (when (stringp object) (tramp-error vec 'file-error object))
1836 (defun tramp-do-directory-files-and-attributes-with-stat
1837 (vec localname &optional id-format)
1838 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1839 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1840 (tramp-send-command-and-read
1844 ;; We must care about file names with spaces, or starting with
1845 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1846 ;; but it does not work on all remote systems. Apostrophes in
1847 ;; the stat output are masked as `tramp-stat-marker', in order to
1848 ;; make a proper shell escape of them in file names.
1849 "cd %s && echo \"(\"; (%s %s -a | "
1851 "'(%s%%n%s (%s%%N%s) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 %s%%A%s t %%ie0 -1)' "
1852 "-- 2>/dev/null | sed -e 's/\"/\\\\\"/g' -e 's/%s/\"/g'); echo \")\"")
1853 (tramp-shell-quote-argument localname)
1854 (tramp-get-ls-command vec)
1855 ;; On systems which have no quoting style, file names with special
1856 ;; characters could fail.
1858 ((tramp-get-ls-command-with-quoting-style vec)
1859 "--quoting-style=shell")
1860 ((tramp-get-ls-command-with-w-option vec)
1863 (tramp-get-remote-stat vec)
1864 tramp-stat-marker tramp-stat-marker
1865 tramp-stat-marker tramp-stat-marker
1866 (if (eq id-format 'integer)
1867 "%ue0" (concat tramp-stat-marker "%U" tramp-stat-marker))
1868 (if (eq id-format 'integer)
1869 "%ge0" (concat tramp-stat-marker "%G" tramp-stat-marker))
1870 tramp-stat-marker tramp-stat-marker
1871 tramp-stat-quoted-marker)))
1873 ;; This function should return "foo/" for directories and "bar" for
1875 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1876 "Like `file-name-all-completions' for Tramp files."
1877 (unless (save-match-data (string-match "/" filename))
1878 (with-parsed-tramp-file-name (expand-file-name directory) nil
1885 ;; Try cache entries for `filename', `filename' with last
1886 ;; character removed, `filename' with last two characters
1887 ;; removed, ..., and finally the empty string - all
1888 ;; concatenated to the local directory name.
1889 (let ((remote-file-name-inhibit-cache
1890 (or remote-file-name-inhibit-cache
1891 tramp-completion-reread-directory-timeout)))
1893 ;; This is inefficient for very long file names, pity
1894 ;; `reduce' is not available...
1901 (tramp-get-file-property
1903 (concat localname (substring filename 0 x))
1904 "file-name-all-completions"
1906 (when cache-hit (list cache-hit))))
1907 ;; We cannot use a length of 0, because file properties
1908 ;; for "foo" and "foo/" are identical.
1909 (tramp-compat-number-sequence (length filename) 1 -1)))))
1911 ;; Cache expired or no matching cache entry found so we need
1912 ;; to perform a remote operation.
1914 ;; Get a list of directories and files, including reliably
1915 ;; tagging the directories with a trailing '/'. Because I
1916 ;; rock. --daniel@danann.net
1918 ;; Changed to perform `cd' in the same remote op and only
1919 ;; get entries starting with `filename'. Capture any `cd'
1920 ;; error messages. Ensure any `cd' and `echo' aliases are
1924 (if (tramp-get-remote-perl v)
1926 (tramp-maybe-send-script
1927 v tramp-perl-file-name-all-completions
1928 "tramp_perl_file_name_all_completions")
1929 (format "tramp_perl_file_name_all_completions %s %s %d"
1930 (tramp-shell-quote-argument localname)
1931 (tramp-shell-quote-argument filename)
1933 ;; `read-file-name-completion-ignore-case'
1934 ;; is introduced with Emacs 22.1.
1936 'read-file-name-completion-ignore-case)
1937 'read-file-name-completion-ignore-case
1938 'completion-ignore-case))
1942 "(cd %s 2>&1 && (%s -a %s 2>/dev/null"
1943 ;; `ls' with wildcard might fail with `Argument
1944 ;; list too long' error in some corner cases; if
1945 ;; `ls' fails after `cd' succeeded, chances are
1946 ;; that's the case, so let's retry without
1947 ;; wildcard. This will return "too many" entries
1948 ;; but that isn't harmful.
1949 " || %s -a 2>/dev/null)"
1950 " | while IFS= read f; do"
1951 " if %s -d \"$f\" 2>/dev/null;"
1952 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1953 " && \\echo ok) || \\echo fail")
1954 (tramp-shell-quote-argument localname)
1955 (tramp-get-ls-command v)
1956 ;; When `filename' is empty, just `ls' without
1957 ;; `filename' argument is more efficient than `ls *'
1958 ;; for very large directories and might avoid the
1959 ;; `Argument list too long' error.
1961 ;; With and only with wildcard, we need to add
1962 ;; `-d' to prevent `ls' from descending into
1964 (if (zerop (length filename))
1966 (format "-d %s*" (tramp-shell-quote-argument filename)))
1967 (tramp-get-ls-command v)
1968 (tramp-get-test-command v))))
1970 ;; Now grab the output.
1971 (with-current-buffer (tramp-get-buffer v)
1972 (goto-char (point-max))
1974 ;; Check result code, found in last line of output.
1976 (if (looking-at "^fail$")
1978 ;; Grab error message from line before last line
1979 ;; (it was put there by `cd 2>&1').
1983 "tramp-sh-handle-file-name-all-completions: %s"
1984 (buffer-substring (point) (point-at-eol))))
1985 ;; For peace of mind, if buffer doesn't end in `fail'
1986 ;; then it should end in `ok'. If neither are in the
1987 ;; buffer something went seriously wrong on the remote
1989 (unless (looking-at "^ok$")
1993 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1994 (tramp-shell-quote-argument localname) (buffer-string))))
1996 (while (zerop (forward-line -1))
1997 (push (buffer-substring (point) (point-at-eol)) result)))
1999 ;; Because the remote op went through OK we know the
2000 ;; directory we `cd'-ed to exists.
2001 (tramp-set-file-property v localname "file-exists-p" t)
2003 ;; Because the remote op went through OK we know every
2004 ;; file listed by `ls' exists.
2005 (mapc (lambda (entry)
2006 (tramp-set-file-property
2007 v (concat localname entry) "file-exists-p" t))
2010 ;; Store result in the cache.
2011 (tramp-set-file-property
2012 v (concat localname filename)
2013 "file-name-all-completions" result))))))))
2017 (defun tramp-sh-handle-add-name-to-file
2018 (filename newname &optional ok-if-already-exists)
2019 "Like `add-name-to-file' for Tramp files."
2020 (unless (tramp-equal-remote filename newname)
2021 (with-parsed-tramp-file-name
2022 (if (tramp-tramp-file-p filename) filename newname) nil
2025 "add-name-to-file: %s"
2026 "only implemented for same method, same user, same host")))
2027 (with-parsed-tramp-file-name filename v1
2028 (with-parsed-tramp-file-name newname v2
2029 (let ((ln (when v1 (tramp-get-remote-ln v1))))
2030 (when (and (numberp ok-if-already-exists)
2031 (file-exists-p newname)
2034 "File %s already exists; make it a new name anyway? "
2037 v2 'file-error "add-name-to-file: file %s already exists" newname))
2038 (when ok-if-already-exists (setq ln (concat ln " -f")))
2039 (tramp-flush-file-property v2 (file-name-directory v2-localname))
2040 (tramp-flush-file-property v2 v2-localname)
2041 (tramp-barf-unless-okay
2043 (format "%s %s %s" ln
2044 (tramp-shell-quote-argument v1-localname)
2045 (tramp-shell-quote-argument v2-localname))
2046 "error with add-name-to-file, see buffer `%s' for details"
2049 (defun tramp-sh-handle-copy-file
2050 (filename newname &optional ok-if-already-exists keep-date
2051 preserve-uid-gid preserve-extended-attributes)
2052 "Like `copy-file' for Tramp files."
2053 (setq filename (expand-file-name filename))
2054 (setq newname (expand-file-name newname))
2056 ;; At least one file a Tramp file?
2057 ((or (tramp-tramp-file-p filename)
2058 (tramp-tramp-file-p newname))
2059 (tramp-do-copy-or-rename-file
2060 'copy filename newname ok-if-already-exists keep-date
2061 preserve-uid-gid preserve-extended-attributes))
2063 (preserve-extended-attributes
2064 (tramp-run-real-handler
2066 (list filename newname ok-if-already-exists keep-date
2067 preserve-uid-gid preserve-extended-attributes)))
2069 (tramp-run-real-handler
2071 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
2073 (tramp-run-real-handler
2074 'copy-file (list filename newname ok-if-already-exists keep-date)))))
2076 (defun tramp-sh-handle-copy-directory
2077 (dirname newname &optional keep-date parents copy-contents)
2078 "Like `copy-directory' for Tramp files."
2079 (let ((t1 (tramp-tramp-file-p dirname))
2080 (t2 (tramp-tramp-file-p newname)))
2081 (with-parsed-tramp-file-name (if t1 dirname newname) nil
2082 (if (and (not copy-contents)
2083 (tramp-get-method-parameter v 'tramp-copy-recursive)
2084 ;; When DIRNAME and NEWNAME are remote, they must have
2086 (or (null t1) (null t2)
2088 (tramp-file-name-method (tramp-dissect-file-name dirname))
2089 (tramp-file-name-method
2090 (tramp-dissect-file-name newname)))))
2091 ;; scp or rsync DTRT.
2093 (setq dirname (directory-file-name (expand-file-name dirname))
2094 newname (directory-file-name (expand-file-name newname)))
2095 (if (and (file-directory-p newname)
2096 (not (string-equal (file-name-nondirectory dirname)
2097 (file-name-nondirectory newname))))
2100 (file-name-nondirectory dirname) newname)))
2101 (if (not (file-directory-p (file-name-directory newname)))
2102 (make-directory (file-name-directory newname) parents))
2103 (tramp-do-copy-or-rename-file-out-of-band
2104 'copy dirname newname keep-date))
2105 ;; We must do it file-wise.
2106 (tramp-run-real-handler
2109 (list dirname newname keep-date parents copy-contents)
2110 (list dirname newname keep-date parents))))
2112 ;; When newname did exist, we have wrong cached values.
2114 (with-parsed-tramp-file-name newname nil
2115 (tramp-flush-file-property v (file-name-directory localname))
2116 (tramp-flush-file-property v localname))))))
2118 (defun tramp-sh-handle-rename-file
2119 (filename newname &optional ok-if-already-exists)
2120 "Like `rename-file' for Tramp files."
2121 ;; Check if both files are local -- invoke normal rename-file.
2122 ;; Otherwise, use Tramp from local system.
2123 (setq filename (expand-file-name filename))
2124 (setq newname (expand-file-name newname))
2125 ;; At least one file a Tramp file?
2126 (if (or (tramp-tramp-file-p filename)
2127 (tramp-tramp-file-p newname))
2128 (tramp-do-copy-or-rename-file
2129 'rename filename newname ok-if-already-exists t t)
2130 (tramp-run-real-handler
2131 'rename-file (list filename newname ok-if-already-exists))))
2133 (defun tramp-do-copy-or-rename-file
2134 (op filename newname &optional ok-if-already-exists keep-date
2135 preserve-uid-gid preserve-extended-attributes)
2136 "Copy or rename a remote file.
2137 OP must be `copy' or `rename' and indicates the operation to perform.
2138 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2139 the new file (for copy) or the new name of the file (for rename).
2140 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2141 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2142 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2143 the uid and gid if both files are on the same host.
2144 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
2146 This function is invoked by `tramp-sh-handle-copy-file' and
2147 `tramp-sh-handle-rename-file'. It is an error if OP is neither
2148 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
2150 (unless (memq op '(copy rename))
2151 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2152 (let ((t1 (tramp-tramp-file-p filename))
2153 (t2 (tramp-tramp-file-p newname))
2154 (length (nth 7 (file-attributes (file-truename filename))))
2155 (attributes (and preserve-extended-attributes
2156 (apply 'file-extended-attributes (list filename)))))
2158 (with-parsed-tramp-file-name (if t1 filename newname) nil
2159 (when (and (not ok-if-already-exists) (file-exists-p newname))
2161 v 'file-already-exists "File %s already exists" newname))
2163 (with-tramp-progress-reporter
2164 v 0 (format "%s %s to %s"
2165 (if (eq op 'copy) "Copying" "Renaming")
2169 ;; Both are Tramp files.
2171 (with-parsed-tramp-file-name filename v1
2172 (with-parsed-tramp-file-name newname v2
2174 ;; Shortcut: if method, host, user are the same for
2175 ;; both files, we invoke `cp' or `mv' on the remote
2177 ((tramp-equal-remote filename newname)
2178 (tramp-do-copy-or-rename-file-directly
2180 ok-if-already-exists keep-date preserve-uid-gid))
2182 ;; Try out-of-band operation.
2184 (tramp-method-out-of-band-p v1 length)
2185 (tramp-method-out-of-band-p v2 length))
2186 (tramp-do-copy-or-rename-file-out-of-band
2187 op filename newname keep-date))
2189 ;; No shortcut was possible. So we copy the file
2190 ;; first. If the operation was `rename', we go back
2191 ;; and delete the original file (if the copy was
2192 ;; successful). The approach is simple-minded: we
2193 ;; create a new buffer, insert the contents of the
2194 ;; source file into it, then write out the buffer to
2195 ;; the target file. The advantage is that it doesn't
2196 ;; matter which file name handlers are used for the
2197 ;; source and target file.
2199 (tramp-do-copy-or-rename-file-via-buffer
2200 op filename newname keep-date))))))
2202 ;; One file is a Tramp file, the other one is local.
2205 ;; Fast track on local machine.
2206 ((tramp-local-host-p v)
2207 (tramp-do-copy-or-rename-file-directly
2209 ok-if-already-exists keep-date preserve-uid-gid))
2211 ;; If the Tramp file has an out-of-band method, the
2212 ;; corresponding copy-program can be invoked.
2213 ((tramp-method-out-of-band-p v length)
2214 (tramp-do-copy-or-rename-file-out-of-band
2215 op filename newname keep-date))
2217 ;; Use the inline method via a Tramp buffer.
2218 (t (tramp-do-copy-or-rename-file-via-buffer
2219 op filename newname keep-date))))
2222 ;; One of them must be a Tramp file.
2223 (error "Tramp implementation says this cannot happen")))
2225 ;; Handle `preserve-extended-attributes'. We ignore possible
2226 ;; errors, because ACL strings could be incompatible.
2229 (apply 'set-file-extended-attributes (list newname attributes))))
2231 ;; In case of `rename', we must flush the cache of the source file.
2232 (when (and t1 (eq op 'rename))
2233 (with-parsed-tramp-file-name filename v1
2234 (tramp-flush-file-property v1 (file-name-directory v1-localname))
2235 (tramp-flush-file-property v1 v1-localname)))
2237 ;; When newname did exist, we have wrong cached values.
2239 (with-parsed-tramp-file-name newname v2
2240 (tramp-flush-file-property v2 (file-name-directory v2-localname))
2241 (tramp-flush-file-property v2 v2-localname)))))))
2243 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2244 "Use an Emacs buffer to copy or rename a file.
2245 First arg OP is either `copy' or `rename' and indicates the operation.
2246 FILENAME is the source file, NEWNAME the target file.
2247 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2248 ;; We must disable multibyte, because binary data shall not be
2249 ;; converted. We don't want the target file to be compressed, so we
2250 ;; let-bind `jka-compr-inhibit' to t. `epa-file-handler' shall not
2251 ;; be called either. We remove `tramp-file-name-handler' from
2252 ;; `inhibit-file-name-handlers'; otherwise the file name handler for
2253 ;; `insert-file-contents' might be deactivated in some corner cases.
2254 (let ((coding-system-for-read 'binary)
2255 (coding-system-for-write 'binary)
2256 (jka-compr-inhibit t)
2257 (inhibit-file-name-operation 'write-region)
2258 (inhibit-file-name-handlers
2259 (cons 'epa-file-handler
2260 (remq 'tramp-file-name-handler inhibit-file-name-handlers))))
2261 (with-temp-file newname
2262 (set-buffer-multibyte nil)
2263 (insert-file-contents-literally filename)))
2264 ;; KEEP-DATE handling.
2265 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2267 (set-file-modes newname (tramp-default-file-modes filename))
2268 ;; If the operation was `rename', delete the original file.
2269 (unless (eq op 'copy) (delete-file filename)))
2271 (defun tramp-do-copy-or-rename-file-directly
2272 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2273 "Invokes `cp' or `mv' on the remote system.
2274 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2275 respectively. FILENAME specifies the file to copy or rename,
2276 NEWNAME is the name of the new file (for copy) or the new name of
2277 the file (for rename). Both files must reside on the same host.
2278 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2279 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2280 the uid and gid from FILENAME."
2281 (let ((t1 (tramp-tramp-file-p filename))
2282 (t2 (tramp-tramp-file-p newname))
2283 (file-times (nth 5 (file-attributes filename)))
2284 (file-modes (tramp-default-file-modes filename)))
2285 (with-parsed-tramp-file-name (if t1 filename newname) nil
2286 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2287 ((eq op 'copy) "cp -f")
2288 ((eq op 'rename) "mv -f")
2291 "Unknown operation `%s', must be `copy' or `rename'"
2295 (tramp-file-name-handler 'file-remote-p filename 'localname)
2299 (tramp-file-name-handler 'file-remote-p newname 'localname)
2301 (prefix (file-remote-p (if t1 filename newname)))
2305 ;; Both files are on a remote host, with same user.
2308 (tramp-send-command-and-check
2309 v (format "%s %s %s" cmd
2310 (tramp-shell-quote-argument localname1)
2311 (tramp-shell-quote-argument localname2))))
2312 (with-current-buffer (tramp-get-buffer v)
2313 (goto-char (point-min))
2317 ;; Mask cp -f error.
2319 tramp-operation-not-permitted-regexp nil t))
2321 (tramp-error-with-buffer
2323 "Copying directly failed, see buffer `%s' for details."
2326 ;; We are on the local host.
2329 ;; We can do it directly.
2330 ((let (file-name-handler-alist)
2331 (and (file-readable-p localname1)
2332 ;; No sticky bit when renaming.
2336 (file-modes (file-name-directory localname1))
2337 (tramp-compat-octal-to-decimal "1000"))))
2338 (file-writable-p (file-name-directory localname2))
2339 (or (file-directory-p localname2)
2340 (file-writable-p localname2))))
2342 (tramp-compat-copy-file
2343 localname1 localname2 ok-if-already-exists
2344 keep-date preserve-uid-gid)
2345 (tramp-run-real-handler
2346 'rename-file (list localname1 localname2 ok-if-already-exists))))
2348 ;; We can do it directly with `tramp-send-command'
2349 ((and (file-readable-p (concat prefix localname1))
2351 (file-name-directory (concat prefix localname2)))
2352 (or (file-directory-p (concat prefix localname2))
2353 (file-writable-p (concat prefix localname2))))
2354 (tramp-do-copy-or-rename-file-directly
2355 op (concat prefix localname1) (concat prefix localname2)
2356 ok-if-already-exists keep-date t)
2357 ;; We must change the ownership to the local user.
2358 (tramp-set-file-uid-gid
2359 (concat prefix localname2)
2360 (tramp-get-local-uid 'integer)
2361 (tramp-get-local-gid 'integer)))
2363 ;; We need a temporary file in between.
2365 ;; Create the temporary file.
2366 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2371 (tramp-barf-unless-okay
2374 (tramp-shell-quote-argument localname1)
2375 (tramp-shell-quote-argument tmpfile))
2376 "Copying directly failed, see buffer `%s' for details."
2377 (tramp-get-buffer v))
2378 ;; We must change the ownership as remote user.
2379 ;; Since this does not work reliable, we also
2380 ;; give read permissions.
2382 (concat prefix tmpfile)
2383 (tramp-compat-octal-to-decimal "0777"))
2384 (tramp-set-file-uid-gid
2385 (concat prefix tmpfile)
2386 (tramp-get-local-uid 'integer)
2387 (tramp-get-local-gid 'integer)))
2390 (tramp-compat-copy-file
2391 localname1 tmpfile t
2392 keep-date preserve-uid-gid)
2393 (tramp-run-real-handler
2395 (list localname1 tmpfile t)))
2396 ;; We must change the ownership as local user.
2397 ;; Since this does not work reliable, we also
2398 ;; give read permissions.
2400 tmpfile (tramp-compat-octal-to-decimal "0777"))
2401 (tramp-set-file-uid-gid
2403 (tramp-get-remote-uid v 'integer)
2404 (tramp-get-remote-gid v 'integer))))
2406 ;; Move the temporary file to its destination.
2409 (tramp-barf-unless-okay
2412 (tramp-shell-quote-argument tmpfile)
2413 (tramp-shell-quote-argument localname2))
2414 "Copying directly failed, see buffer `%s' for details."
2415 (tramp-get-buffer v)))
2417 (tramp-run-real-handler
2419 (list tmpfile localname2 ok-if-already-exists)))))
2422 (ignore-errors (delete-file tmpfile)))))))))
2424 ;; Set the time and mode. Mask possible errors.
2427 (set-file-times newname file-times)
2428 (set-file-modes newname file-modes))))))
2430 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2431 "Invoke `scp' program to copy.
2432 The method used must be an out-of-band method."
2433 (let* ((t1 (tramp-tramp-file-p filename))
2434 (t2 (tramp-tramp-file-p newname))
2435 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2436 copy-program copy-args copy-env copy-keep-date port listener spec
2437 options source target remote-copy-program remote-copy-args)
2439 (with-parsed-tramp-file-name (if t1 filename newname) nil
2442 ;; Both are Tramp files. We shall optimize it when the
2443 ;; methods for FILENAME and NEWNAME are the same.
2444 (let* ((dir-flag (file-directory-p filename))
2445 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2449 (file-name-nondirectory newname) tmpfile)))
2452 (tramp-do-copy-or-rename-file-out-of-band
2453 op filename tmpfile keep-date)
2454 (tramp-do-copy-or-rename-file-out-of-band
2455 'rename tmpfile newname keep-date))
2459 (tramp-compat-delete-directory
2460 (expand-file-name ".." tmpfile) 'recursive)
2461 (delete-file tmpfile)))))
2463 ;; Set variables for computing the prompt for reading
2465 (setq tramp-current-method (tramp-file-name-method v)
2466 tramp-current-user (or (tramp-file-name-user v)
2467 (tramp-get-connection-property
2469 tramp-current-host (tramp-file-name-real-host v))
2471 ;; Expand hops. Might be necessary for gateway methods.
2472 (setq v (car (tramp-compute-multi-hops v)))
2473 (aset v 3 localname)
2475 ;; Check which ones of source and target are Tramp files.
2477 (tramp-make-copy-program-file-name v)
2478 (shell-quote-argument filename))
2480 (tramp-make-copy-program-file-name v)
2481 (shell-quote-argument
2483 (if (and (file-directory-p filename)
2485 (file-name-nondirectory filename)
2486 (file-name-nondirectory newname)))
2487 'file-name-directory
2491 ;; Check for host and port number. We cannot use
2492 ;; `tramp-file-name-port', because this returns also
2493 ;; `tramp-default-port', which might clash with settings in
2495 (setq host (tramp-file-name-host v)
2497 (when (string-match tramp-host-with-port-regexp host)
2498 (setq port (string-to-number (match-string 2 host))
2499 host (string-to-number (match-string 1 host))))
2501 ;; Check for user. There might be an interactive setting.
2502 (setq user (or (tramp-file-name-user v)
2503 (tramp-get-connection-property v "login-as" nil)))
2505 ;; Check for listener port.
2506 (when (tramp-get-method-parameter v 'tramp-remote-copy-args)
2507 (setq listener (number-to-string (+ 50000 (random 10000))))
2509 (zerop (tramp-call-process v "nc" nil nil nil "-z" host listener))
2510 (setq listener (number-to-string (+ 50000 (random 10000))))))
2512 ;; Compose copy command.
2513 (setq host (or host "")
2516 spec (format-spec-make
2517 ?t (tramp-get-connection-property
2518 (tramp-get-connection-process v) "temp-file" ""))
2519 options (format-spec (tramp-ssh-controlmaster-options v) spec)
2520 spec (format-spec-make
2521 ?h host ?u user ?p port ?r listener ?c options
2522 ?k (if keep-date " " ""))
2523 copy-program (tramp-get-method-parameter v 'tramp-copy-program)
2524 copy-keep-date (tramp-get-method-parameter
2525 v 'tramp-copy-keep-date)
2529 ;; " " has either been a replacement of "%k" (when
2530 ;; keep-date argument is non-nil), or a replacement
2531 ;; for the whole keep-date sublist.
2534 (x (tramp-get-method-parameter v 'tramp-copy-args) copy-args)
2538 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2539 (if (member "" y) '(" ") y))))))
2546 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2547 (unless (member "" x) (mapconcat 'identity x " ")))
2548 (tramp-get-method-parameter v 'tramp-copy-env)))
2551 (tramp-get-method-parameter v 'tramp-remote-copy-program))
2553 (dolist (x (tramp-get-method-parameter v 'tramp-remote-copy-args))
2554 (setq remote-copy-args
2557 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2558 (if (member "" y) '(" ") y)))))
2560 ;; Check for local copy program.
2561 (unless (executable-find copy-program)
2563 v 'file-error "Cannot find local copy program: %s" copy-program))
2565 ;; Install listener on the remote side. The prompt must be
2566 ;; consumed later on, when the process does not listen anymore.
2567 (when remote-copy-program
2568 (unless (with-tramp-connection-property
2569 v (concat "remote-copy-program-" remote-copy-program)
2570 (tramp-find-executable
2571 v remote-copy-program (tramp-get-remote-path v)))
2574 "Cannot find remote listener: %s" remote-copy-program))
2575 (setq remote-copy-program
2579 (list remote-copy-program) remote-copy-args
2580 (list (if t1 (concat "<" source) (concat ">" target)) "&"))
2582 (tramp-send-command v remote-copy-program)
2586 "Listener process not running on remote host: `%s'"
2587 remote-copy-program))
2588 (tramp-send-command v (format "netstat -l | grep -q :%s" listener))
2589 (while (not (tramp-send-command-and-check v nil))
2591 v (format "netstat -l | grep -q :%s" listener)))))
2595 ;; The default directory must be remote.
2596 (let ((default-directory
2597 (file-name-directory (if t1 filename newname)))
2598 (process-environment (copy-sequence process-environment)))
2599 ;; Set the transfer process properties.
2600 (tramp-set-connection-property
2601 v "process-name" (buffer-name (current-buffer)))
2602 (tramp-set-connection-property
2603 v "process-buffer" (current-buffer))
2606 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2607 (setenv (pop copy-env) (pop copy-env)))
2612 (if remote-copy-program
2613 (list (if t1 (concat ">" target) (concat "<" source)))
2614 (list source target))))
2616 ;; Use an asynchronous process. By this, password can
2617 ;; be handled. We don't set a timeout, because the
2618 ;; copying of large files can last longer than 60
2620 (let ((p (apply 'start-process-shell-command
2621 (tramp-get-connection-name v)
2622 (tramp-get-connection-buffer v)
2626 (list "&&" "echo" "tramp_exit_status" "0"
2627 "||" "echo" "tramp_exit_status" "1")))))
2630 (mapconcat 'identity (process-command p) " "))
2631 (tramp-set-connection-property p "vector" orig-vec)
2632 (tramp-compat-set-process-query-on-exit-flag p nil)
2634 ;; We must adapt `tramp-local-end-of-line' for
2635 ;; sending the password.
2636 (let ((tramp-local-end-of-line tramp-rsh-end-of-line))
2637 (tramp-process-actions
2638 p v nil tramp-actions-copy-out-of-band))
2640 ;; Check the return code.
2641 (goto-char (point-max))
2643 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2645 orig-vec 'file-error
2646 "Couldn't find exit status of `%s'"
2647 (mapconcat 'identity (process-command p) " ")))
2648 (skip-chars-forward "^ ")
2649 (unless (zerop (read (current-buffer)))
2652 orig-vec 'file-error
2653 "Error copying: `%s'"
2654 (buffer-substring (point-min) (point-at-eol))))))
2656 ;; Reset the transfer process properties.
2657 (tramp-set-connection-property v "process-name" nil)
2658 (tramp-set-connection-property v "process-buffer" nil)
2659 ;; Clear the remote prompt.
2660 (when (and remote-copy-program
2661 (not (tramp-send-command-and-check v nil)))
2662 ;; Houston, we have a problem! Likely, the listener is
2663 ;; still running, so let's clear everything (but the
2664 ;; cached password).
2665 (tramp-cleanup-connection v 'keep-debug 'keep-password))))
2667 ;; Handle KEEP-DATE argument.
2668 (when (and keep-date (not copy-keep-date))
2669 (set-file-times newname (nth 5 (file-attributes filename))))
2672 (unless (and keep-date copy-keep-date)
2674 (set-file-modes newname (tramp-default-file-modes filename)))))
2676 ;; If the operation was `rename', delete the original file.
2677 (unless (eq op 'copy)
2678 (if (file-regular-p filename)
2679 (delete-file filename)
2680 (tramp-compat-delete-directory filename 'recursive))))))
2682 (defun tramp-sh-handle-make-directory (dir &optional parents)
2683 "Like `make-directory' for Tramp files."
2684 (setq dir (expand-file-name dir))
2685 (with-parsed-tramp-file-name dir nil
2686 (tramp-flush-directory-property v (file-name-directory localname))
2688 (tramp-barf-unless-okay
2690 (if parents "mkdir -p" "mkdir")
2691 (tramp-shell-quote-argument localname))
2692 "Couldn't make directory %s" dir))))
2694 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2695 "Like `delete-directory' for Tramp files."
2696 (setq directory (expand-file-name directory))
2697 (with-parsed-tramp-file-name directory nil
2698 (tramp-flush-file-property v (file-name-directory localname))
2699 (tramp-flush-directory-property v localname)
2700 (tramp-barf-unless-okay
2701 v (format "cd / && %s %s"
2702 (if recursive "rm -rf" "rmdir")
2703 (tramp-shell-quote-argument localname))
2704 "Couldn't delete %s" directory)))
2706 (defun tramp-sh-handle-delete-file (filename &optional trash)
2707 "Like `delete-file' for Tramp files."
2708 (setq filename (expand-file-name filename))
2709 (with-parsed-tramp-file-name filename nil
2710 (tramp-flush-file-property v (file-name-directory localname))
2711 (tramp-flush-file-property v localname)
2712 (tramp-barf-unless-okay
2714 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2715 (tramp-shell-quote-argument localname))
2716 "Couldn't delete %s" filename)))
2720 ;; CCC: This does not seem to be enough. Something dies when
2721 ;; we try and delete two directories under Tramp :/
2722 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2723 "Recursively delete the directory given.
2724 This is like `dired-recursive-delete-directory' for Tramp files."
2725 (with-parsed-tramp-file-name filename nil
2726 ;; Run a shell command 'rm -r <localname>'.
2727 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2728 (unless (file-exists-p filename)
2729 (tramp-error v 'file-error "No such directory: %s" filename))
2730 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2733 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2734 ;; Don't read the output, do it explicitly.
2736 ;; Wait for the remote system to return to us...
2737 ;; This might take a while, allow it plenty of time.
2738 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2739 ;; Make sure that it worked...
2740 (tramp-flush-file-property v (file-name-directory localname))
2741 (tramp-flush-directory-property v localname)
2742 (and (file-exists-p filename)
2744 v 'file-error "Failed to recursively delete %s" filename))))
2746 (defun tramp-sh-handle-dired-compress-file (file &rest _ok-flag)
2747 "Like `dired-compress-file' for Tramp files."
2748 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2749 ;; Code stolen mainly from dired-aux.el.
2750 (with-parsed-tramp-file-name file nil
2751 (tramp-flush-file-property v localname)
2754 (if (not (featurep 'xemacs))
2756 (symbol-value 'dired-compress-file-suffixes)
2757 ;; XEmacs has `dired-compression-method-alist', which is
2758 ;; transformed into `dired-compress-file-suffixes' structure.
2761 (list (concat (regexp-quote (nth 1 x)) "\\'")
2763 (mapconcat 'identity (nth 3 x) " ")))
2764 (symbol-value 'dired-compression-method-alist))))
2766 ;; See if any suffix rule matches this file name.
2768 (let (case-fold-search)
2769 (if (string-match (car (car suffixes)) localname)
2770 (setq suffix (car suffixes) suffixes nil))
2771 (setq suffixes (cdr suffixes))))
2773 (cond ((file-symlink-p file)
2775 ((and suffix (nth 2 suffix))
2776 ;; We found an uncompression rule.
2777 (with-tramp-progress-reporter
2778 v 0 (format "Uncompressing %s" file)
2779 (when (tramp-send-command-and-check
2780 v (concat (nth 2 suffix) " "
2781 (tramp-shell-quote-argument localname)))
2782 ;; `dired-remove-file' is not defined in XEmacs.
2783 (tramp-compat-funcall 'dired-remove-file file)
2784 (string-match (car suffix) file)
2785 (concat (substring file 0 (match-beginning 0))))))
2787 ;; We don't recognize the file as compressed, so compress it.
2789 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2790 (when (tramp-send-command-and-check
2791 v (concat "gzip -f "
2792 (tramp-shell-quote-argument localname)))
2793 ;; `dired-remove-file' is not defined in XEmacs.
2794 (tramp-compat-funcall 'dired-remove-file file)
2795 (cond ((file-exists-p (concat file ".gz"))
2796 (concat file ".gz"))
2797 ((file-exists-p (concat file ".z"))
2801 (defun tramp-sh-handle-insert-directory
2802 (filename switches &optional wildcard full-directory-p)
2803 "Like `insert-directory' for Tramp files."
2804 (setq filename (expand-file-name filename))
2805 (unless switches (setq switches ""))
2806 (with-parsed-tramp-file-name filename nil
2807 (if (and (featurep 'ls-lisp)
2808 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2809 (tramp-handle-insert-directory
2810 filename switches wildcard full-directory-p)
2811 (when (stringp switches)
2812 (setq switches (split-string switches)))
2813 (when (and (member "--dired" switches)
2814 (not (tramp-get-ls-command-with-dired v)))
2815 (setq switches (delete "--dired" switches)))
2817 (setq wildcard (tramp-run-real-handler
2818 'file-name-nondirectory (list localname)))
2819 (setq localname (tramp-run-real-handler
2820 'file-name-directory (list localname))))
2821 (unless (or full-directory-p (member "-d" switches))
2822 (setq switches (append switches '("-d"))))
2823 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2825 (setq switches (concat switches " " wildcard)))
2827 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2828 switches filename (if wildcard "yes" "no")
2829 (if full-directory-p "yes" "no"))
2830 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2831 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2832 (if full-directory-p
2835 (format "%s %s %s 2>/dev/null"
2836 (tramp-get-ls-command v)
2840 (tramp-shell-quote-argument (concat localname ".")))))
2841 (tramp-barf-unless-okay
2843 (format "cd %s" (tramp-shell-quote-argument
2844 (tramp-run-real-handler
2845 'file-name-directory (list localname))))
2847 (tramp-shell-quote-argument
2848 (tramp-run-real-handler 'file-name-directory (list localname))))
2851 (format "%s %s %s 2>/dev/null"
2852 (tramp-get-ls-command v)
2856 (tramp-run-real-handler
2857 'file-name-nondirectory (list localname)))))
2859 (tramp-shell-quote-argument
2860 (tramp-run-real-handler
2861 'file-name-nondirectory (list localname)))))))
2864 (let ((beg (point)))
2865 (narrow-to-region (point) (point))
2866 ;; We cannot use `insert-buffer-substring' because the Tramp
2867 ;; buffer changes its contents before insertion due to calling
2868 ;; `expand-file' and alike.
2870 (with-current-buffer (tramp-get-buffer v)
2873 ;; Check for "--dired" output.
2875 (when (looking-at "//SUBDIRED//")
2877 (when (looking-at "//DIRED//\\s-+")
2878 (let ((databeg (match-end 0))
2879 (end (point-at-eol)))
2880 ;; Now read the numeric positions of file names.
2882 (while (< (point) end)
2883 (let ((start (+ beg (read (current-buffer))))
2884 (end (+ beg (read (current-buffer)))))
2885 (if (memq (char-after end) '(?\n ?\ ))
2886 ;; End is followed by \n or by " -> ".
2887 (put-text-property start end 'dired-filename t))))))
2888 ;; Remove trailing lines.
2889 (goto-char (point-at-bol))
2890 (while (looking-at "//")
2892 (delete-region (match-beginning 0) (point)))
2894 ;; Some busyboxes are reluctant to discard colors.
2896 (string-match "color" (tramp-get-connection-property v "ls" ""))
2898 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2899 (replace-match "")))
2901 ;; Decode the output, it could be multibyte.
2902 (decode-coding-region
2904 (or file-name-coding-system
2905 (and (boundp 'default-file-name-coding-system)
2906 (symbol-value 'default-file-name-coding-system))))
2908 ;; The inserted file could be from somewhere else.
2909 (when (and (not wildcard) (not full-directory-p))
2910 (goto-char (point-max))
2911 (when (file-symlink-p filename)
2912 (goto-char (search-backward "->" beg 'noerror)))
2914 (if (zerop (length (file-name-nondirectory filename)))
2916 (file-name-nondirectory filename))
2918 (replace-match (file-relative-name filename) t))
2920 (goto-char (point-max)))))))
2922 ;; Canonicalization of file names.
2924 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2925 "Like `expand-file-name' for Tramp files.
2926 If the localname part of the given file name starts with \"/../\" then
2927 the result will be a local, non-Tramp, file name."
2928 ;; If DIR is not given, use `default-directory' or "/".
2929 (setq dir (or dir default-directory "/"))
2930 ;; Unless NAME is absolute, concat DIR and NAME.
2931 (unless (file-name-absolute-p name)
2932 (setq name (concat (file-name-as-directory dir) name)))
2933 ;; If NAME is not a Tramp file, run the real handler.
2934 (if (not (tramp-connectable-p name))
2935 (tramp-run-real-handler 'expand-file-name (list name nil))
2937 (with-parsed-tramp-file-name name nil
2938 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2939 (setq localname (concat "~/" localname)))
2940 ;; Tilde expansion if necessary. This needs a shell which
2941 ;; groks tilde expansion! The function `tramp-find-shell' is
2942 ;; supposed to find such a shell on the remote host. Please
2943 ;; tell me about it when this doesn't work on your system.
2944 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2945 (let ((uname (match-string 1 localname))
2946 (fname (match-string 2 localname)))
2947 ;; We cannot simply apply "~/", because under sudo "~/" is
2948 ;; expanded to the local user home directory but to the
2949 ;; root home directory. On the other hand, using always
2950 ;; the default user name for tilde expansion is not
2951 ;; appropriate either, because ssh and companions might
2952 ;; use a user name from the config file.
2953 (when (and (string-equal uname "~")
2954 (string-match "\\`su\\(do\\)?\\'" method))
2955 (setq uname (concat uname user)))
2957 (with-tramp-connection-property v uname
2959 v (format "cd %s && pwd" (tramp-shell-quote-argument uname)))
2960 (with-current-buffer (tramp-get-buffer v)
2961 (goto-char (point-min))
2962 (buffer-substring (point) (point-at-eol)))))
2963 (setq localname (concat uname fname))))
2964 ;; There might be a double slash, for example when "~/"
2965 ;; expands to "/". Remove this.
2966 (while (string-match "//" localname)
2967 (setq localname (replace-match "/" t t localname)))
2968 ;; No tilde characters in file name, do normal
2969 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2970 ;; `directory-sep-char' here for XEmacs on Windows, which would
2971 ;; otherwise use backslash. `default-directory' is bound,
2972 ;; because on Windows there would be problems with UNC shares or
2974 (let ((directory-sep-char ?/)
2975 (default-directory (tramp-compat-temporary-file-directory)))
2976 (tramp-make-tramp-file-name
2978 (tramp-drop-volume-letter
2979 (tramp-run-real-handler
2980 'expand-file-name (list localname)))
2983 ;;; Remote commands:
2985 (defun tramp-process-sentinel (proc event)
2986 "Flush file caches."
2987 (unless (memq (process-status proc) '(run open))
2988 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2990 (tramp-message vec 5 "Sentinel called: `%S' `%s'" proc event)
2991 (tramp-flush-connection-property proc)
2992 (tramp-flush-directory-property vec "")))))
2994 ;; We use BUFFER also as connection buffer during setup. Because of
2995 ;; this, its original contents must be saved, and restored once
2996 ;; connection has been setup.
2997 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2998 "Like `start-file-process' for Tramp files."
2999 (with-parsed-tramp-file-name (expand-file-name default-directory) nil
3000 (let* (;; When PROGRAM matches "*sh", and the first arg is "-c",
3001 ;; it might be that the arguments exceed the command line
3002 ;; length. Therefore, we modify the command.
3003 (heredoc (and (stringp program)
3004 (string-match "sh$" program)
3005 (string-equal "-c" (car args))
3006 (= (length args) 2)))
3007 ;; When PROGRAM is nil, we just provide a tty.
3008 (args (if (not heredoc) args
3010 (while (and (< i (length (cadr args)))
3011 (string-match " " (cadr args) i))
3014 (list (replace-match " \\\\\n" nil nil (cadr args))))
3015 (setq i (+ i 250))))
3017 ;; Use a human-friendly prompt, for example for `shell'.
3018 ;; We discard hops, if existing, that's why we cannot use
3020 (prompt (format "PS1=%s %s"
3021 (tramp-make-tramp-file-name
3022 (tramp-file-name-method v)
3023 (tramp-file-name-user v)
3024 (tramp-file-name-host v)
3025 (tramp-file-name-localname v))
3026 tramp-initial-end-of-output))
3027 ;; We use as environment the difference to toplevel
3028 ;; `process-environment'.
3033 (cons prompt (nreverse (copy-sequence process-environment)))
3035 (or (member elt (default-toplevel-value 'process-environment))
3036 (setq env (cons elt env)))))
3038 (when (stringp program)
3039 (format "cd %s && exec %s env %s %s"
3040 (tramp-shell-quote-argument localname)
3041 (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
3042 (mapconcat 'tramp-shell-quote-argument env " ")
3044 (format "%s\n(\n%s\n) </dev/tty\n%s"
3045 program (car args) tramp-end-of-heredoc)
3046 (mapconcat 'tramp-shell-quote-argument
3047 (cons program args) " ")))))
3048 (tramp-process-connection-type
3049 (or (null program) tramp-process-connection-type))
3050 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
3053 ;; We do not want to raise an error when
3054 ;; `start-file-process' has been started several times in
3055 ;; `eshell' and friends.
3056 (tramp-current-connection nil))
3059 ;; BUFFER can be nil. We use a temporary buffer.
3060 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
3061 (while (get-process name1)
3062 ;; NAME must be unique as process name.
3064 name1 (format "%s<%d>" name i)))
3066 ;; Set the new process properties.
3067 (tramp-set-connection-property v "process-name" name)
3068 (tramp-set-connection-property v "process-buffer" buffer)
3070 (with-current-buffer (tramp-get-connection-buffer v)
3072 ;; We catch this event. Otherwise, `start-process' could
3073 ;; be called on the local host.
3076 ;; Activate narrowing in order to save BUFFER
3077 ;; contents. Clear also the modification time;
3078 ;; otherwise we might be interrupted by
3079 ;; `verify-visited-file-modtime'.
3080 (let ((buffer-undo-list t)
3081 (buffer-read-only nil)
3083 (clear-visited-file-modtime)
3084 (narrow-to-region (point-max) (point-max))
3085 ;; We call `tramp-maybe-open-connection', in order
3086 ;; to cleanup the prompt afterwards.
3088 (tramp-maybe-open-connection v)
3090 (delete-region mark (point))
3091 (narrow-to-region (point-max) (point-max))
3094 ;; Send the command.
3095 (tramp-send-command v command nil t) ; nooutput
3096 ;; Check, whether a pty is associated.
3097 (unless (tramp-compat-process-get
3098 (tramp-get-connection-process v) 'remote-tty)
3101 "pty association is not supported for `%s'" name))))
3102 (let ((p (tramp-get-connection-process v)))
3103 ;; Set query flag and process marker for this
3104 ;; process. We ignore errors, because the process
3105 ;; could have finished already.
3107 (tramp-compat-set-process-query-on-exit-flag p t)
3108 (set-marker (process-mark p) (point)))
3113 (if (string-match tramp-temp-buffer-name (buffer-name))
3115 (set-process-buffer (tramp-get-connection-process v) nil)
3116 (kill-buffer (current-buffer)))
3117 (set-buffer-modified-p bmp))
3118 (tramp-set-connection-property v "process-name" nil)
3119 (tramp-set-connection-property v "process-buffer" nil))))))
3121 (defun tramp-sh-handle-process-file
3122 (program &optional infile destination display &rest args)
3123 "Like `process-file' for Tramp files."
3124 ;; The implementation is not complete yet.
3125 (when (and (numberp destination) (zerop destination))
3126 (error "Implementation does not handle immediate return"))
3128 (with-parsed-tramp-file-name default-directory nil
3129 (let (command env input tmpinput stderr tmpstderr outbuf ret)
3131 (setq command (mapconcat 'tramp-shell-quote-argument
3132 (cons program args) " "))
3133 ;; We use as environment the difference to toplevel `process-environment'.
3135 (dolist (elt (nreverse (copy-sequence process-environment)) env)
3136 (or (member elt (default-toplevel-value 'process-environment))
3137 (setq env (cons elt env)))))
3142 (mapconcat 'tramp-shell-quote-argument env " ") command)))
3145 (setq input "/dev/null")
3146 (setq infile (expand-file-name infile))
3147 (if (tramp-equal-remote default-directory infile)
3148 ;; INFILE is on the same remote host.
3149 (setq input (with-parsed-tramp-file-name infile nil localname))
3150 ;; INFILE must be copied to remote host.
3151 (setq input (tramp-make-tramp-temp-file v)
3152 tmpinput (tramp-make-tramp-file-name method user host input))
3153 (copy-file infile tmpinput t)))
3154 (when input (setq command (format "%s <%s" command input)))
3156 ;; Determine output.
3159 ((bufferp destination)
3160 (setq outbuf destination))
3162 ((stringp destination)
3163 (setq outbuf (get-buffer-create destination)))
3164 ;; (REAL-DESTINATION ERROR-DESTINATION)
3165 ((consp destination)
3168 ((bufferp (car destination))
3169 (setq outbuf (car destination)))
3170 ((stringp (car destination))
3171 (setq outbuf (get-buffer-create (car destination))))
3173 (setq outbuf (current-buffer))))
3176 ((stringp (cadr destination))
3177 (setcar (cdr destination) (expand-file-name (cadr destination)))
3178 (if (tramp-equal-remote default-directory (cadr destination))
3179 ;; stderr is on the same remote host.
3180 (setq stderr (with-parsed-tramp-file-name
3181 (cadr destination) nil localname))
3182 ;; stderr must be copied to remote host. The temporary
3183 ;; file must be deleted after execution.
3184 (setq stderr (tramp-make-tramp-temp-file v)
3185 tmpstderr (tramp-make-tramp-file-name
3186 method user host stderr))))
3187 ;; stderr to be discarded.
3188 ((null (cadr destination))
3189 (setq stderr "/dev/null"))))
3192 (setq outbuf (current-buffer))))
3193 (when stderr (setq command (format "%s 2>%s" command stderr)))
3195 ;; Send the command. It might not return in time, so we protect
3196 ;; it. Call it in a subshell, in order to preserve working
3201 (if (tramp-send-command-and-check
3202 v (format "cd %s && %s"
3203 (tramp-shell-quote-argument localname)
3207 ;; We should add the output anyway.
3209 (with-current-buffer outbuf
3211 (with-current-buffer (tramp-get-connection-buffer v)
3213 (when (and display (get-buffer-window outbuf t)) (redisplay))))
3214 ;; When the user did interrupt, we should do it also. We use
3215 ;; return code -1 as marker.
3217 (kill-buffer (tramp-get-connection-buffer v))
3221 (kill-buffer (tramp-get-connection-buffer v))
3224 ;; Provide error file.
3225 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
3227 ;; Cleanup. We remove all file cache values for the connection,
3228 ;; because the remote process could have changed them.
3229 (when tmpinput (delete-file tmpinput))
3231 ;; `process-file-side-effects' has been introduced with GNU
3232 ;; Emacs 23.2. If set to nil, no remote file will be changed
3233 ;; by `program'. If it doesn't exist, we assume its default
3235 (unless (and (boundp 'process-file-side-effects)
3236 (not (symbol-value 'process-file-side-effects)))
3237 (tramp-flush-directory-property v ""))
3239 ;; Return exit status.
3244 (defun tramp-sh-handle-file-local-copy (filename)
3245 "Like `file-local-copy' for Tramp files."
3246 (with-parsed-tramp-file-name filename nil
3247 (unless (file-exists-p filename)
3250 "Cannot make local copy of non-existing file `%s'" filename))
3252 (let* ((size (nth 7 (file-attributes (file-truename filename))))
3253 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
3254 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
3255 (tmpfile (tramp-compat-make-temp-file filename)))
3259 ;; `copy-file' handles direct copy and out-of-band methods.
3260 ((or (tramp-local-host-p v)
3261 (tramp-method-out-of-band-p v size))
3262 (copy-file filename tmpfile t t))
3264 ;; Use inline encoding for file transfer.
3267 (with-tramp-progress-reporter
3269 (format-message "Encoding remote file `%s' with `%s'"
3271 (tramp-barf-unless-okay
3272 v (format rem-enc (tramp-shell-quote-argument localname))
3273 "Encoding remote file failed"))
3275 (with-tramp-progress-reporter
3276 v 3 (format-message "Decoding local file `%s' with `%s'"
3278 (if (functionp loc-dec)
3279 ;; If local decoding is a function, we call it.
3280 ;; We must disable multibyte, because
3281 ;; `uudecode-decode-region' doesn't handle it
3282 ;; correctly. Unset `file-name-handler-alist'.
3283 ;; Otherwise, epa-file gets confused.
3284 (let (file-name-handler-alist
3285 (coding-system-for-write 'binary))
3286 (with-temp-file tmpfile
3287 (set-buffer-multibyte nil)
3288 (insert-buffer-substring (tramp-get-buffer v))
3289 (funcall loc-dec (point-min) (point-max))))
3291 ;; If tramp-decoding-function is not defined for this
3292 ;; method, we invoke tramp-decoding-command instead.
3293 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
3294 ;; Unset `file-name-handler-alist'. Otherwise,
3295 ;; epa-file gets confused.
3296 (let (file-name-handler-alist
3297 (coding-system-for-write 'binary))
3298 (with-current-buffer (tramp-get-buffer v)
3300 (point-min) (point-max) tmpfile2 nil 'no-message)))
3302 (tramp-call-local-coding-command
3303 loc-dec tmpfile2 tmpfile)
3304 (delete-file tmpfile2)))))
3306 ;; Set proper permissions.
3307 (set-file-modes tmpfile (tramp-default-file-modes filename))
3308 ;; Set local user ownership.
3309 (tramp-set-file-uid-gid tmpfile)))
3311 ;; Oops, I don't know what to do.
3313 v 'file-error "Wrong method specification for `%s'" method)))
3317 (delete-file tmpfile)
3318 (signal (car err) (cdr err))))
3320 (run-hooks 'tramp-handle-file-local-copy-hook)
3323 ;; This is needed for XEmacs only. Code stolen from files.el.
3324 (defun tramp-sh-handle-insert-file-contents-literally
3325 (filename &optional visit beg end replace)
3326 "Like `insert-file-contents-literally' for Tramp files."
3327 (let ((format-alist nil)
3328 (after-insert-file-functions nil)
3329 (coding-system-for-read 'no-conversion)
3330 (coding-system-for-write 'no-conversion)
3331 (find-buffer-file-type-function
3332 (if (fboundp 'find-buffer-file-type)
3333 (symbol-function 'find-buffer-file-type)
3335 (inhibit-file-name-handlers
3336 '(epa-file-handler image-file-handler jka-compr-handler))
3337 (inhibit-file-name-operation 'insert-file-contents))
3340 (fset 'find-buffer-file-type (lambda (_filename) t))
3341 (insert-file-contents filename visit beg end replace))
3343 (if find-buffer-file-type-function
3344 (fset 'find-buffer-file-type find-buffer-file-type-function)
3345 (fmakunbound 'find-buffer-file-type)))))
3347 ;; CCC grok LOCKNAME
3348 (defun tramp-sh-handle-write-region
3349 (start end filename &optional append visit lockname confirm)
3350 "Like `write-region' for Tramp files."
3351 (setq filename (expand-file-name filename))
3352 (with-parsed-tramp-file-name filename nil
3353 ;; Following part commented out because we don't know what to do about
3354 ;; file locking, and it does not appear to be a problem to ignore it.
3355 ;; Ange-ftp ignores it, too.
3356 ;; (when (and lockname (stringp lockname))
3357 ;; (setq lockname (expand-file-name lockname)))
3358 ;; (unless (or (eq lockname nil)
3359 ;; (string= lockname filename))
3361 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3363 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3364 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3365 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3366 (tramp-error v 'file-error "File not overwritten")))
3368 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3369 (tramp-get-remote-uid v 'integer)))
3370 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3371 (tramp-get-remote-gid v 'integer))))
3373 (if (and (tramp-local-host-p v)
3374 ;; `file-writable-p' calls `file-expand-file-name'. We
3375 ;; cannot use `tramp-run-real-handler' therefore.
3376 (let (file-name-handler-alist)
3378 (file-writable-p (file-name-directory localname))
3379 (or (file-directory-p localname)
3380 (file-writable-p localname)))))
3381 ;; Short track: if we are on the local host, we can run directly.
3382 (tramp-run-real-handler
3384 (list start end localname append 'no-message lockname confirm))
3386 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3387 ;; We use this to save the value of
3388 ;; `last-coding-system-used' after writing the tmp
3389 ;; file. At the end of the function, we set
3390 ;; `last-coding-system-used' to this saved value. This
3391 ;; way, any intermediary coding systems used while
3392 ;; talking to the remote shell or suchlike won't hose
3393 ;; this variable. This approach was snarfed from
3396 ;; Write region into a tmp file. This isn't really
3397 ;; needed if we use an encoding function, but currently
3398 ;; we use it always because this makes the logic
3399 ;; simpler. We must also set `temporary-file-directory',
3400 ;; because it could point to a remote directory.
3401 (temporary-file-directory
3402 (tramp-compat-temporary-file-directory))
3403 (tmpfile (or tramp-temp-buffer-file-name
3404 (tramp-compat-make-temp-file filename))))
3406 ;; If `append' is non-nil, we copy the file locally, and let
3407 ;; the native `write-region' implementation do the job.
3408 (when append (copy-file filename tmpfile 'ok))
3410 ;; We say `no-message' here because we don't want the
3411 ;; visited file modtime data to be clobbered from the temp
3412 ;; file. We call `set-visited-file-modtime' ourselves later
3413 ;; on. We must ensure that `file-coding-system-alist'
3414 ;; matches `tmpfile'.
3415 (let (file-name-handler-alist
3416 (file-coding-system-alist
3417 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3419 (tramp-run-real-handler
3421 (list start end tmpfile append 'no-message lockname confirm))
3423 (setq tramp-temp-buffer-file-name nil)
3424 (delete-file tmpfile)
3425 (signal (car err) (cdr err))))
3427 ;; Now, `last-coding-system-used' has the right value. Remember it.
3428 (when (boundp 'last-coding-system-used)
3429 (setq coding-system-used
3430 (symbol-value 'last-coding-system-used))))
3432 ;; The permissions of the temporary file should be set. If
3433 ;; FILENAME does not exist (eq modes nil) it has been
3434 ;; renamed to the backup file. This case `save-buffer'
3435 ;; handles permissions.
3436 ;; Ensure that it is still readable.
3440 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3442 ;; This is a bit lengthy due to the different methods
3443 ;; possible for file transfer. First, we check whether the
3444 ;; method uses an scp program. If so, we call it.
3445 ;; Otherwise, both encoding and decoding command must be
3446 ;; specified. However, if the method _also_ specifies an
3447 ;; encoding function, then that is used for encoding the
3448 ;; contents of the tmp file.
3449 (let* ((size (nth 7 (file-attributes tmpfile)))
3450 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3451 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3453 ;; `copy-file' handles direct copy and out-of-band methods.
3454 ((or (tramp-local-host-p v)
3455 (tramp-method-out-of-band-p v size))
3456 (if (and (not (stringp start))
3457 (= (or end (point-max)) (point-max))
3458 (= (or start (point-min)) (point-min))
3459 (tramp-get-method-parameter v 'tramp-copy-keep-tmpfile))
3461 (setq tramp-temp-buffer-file-name tmpfile)
3463 ;; We keep the local file for performance
3464 ;; reasons, useful for "rsync".
3465 (copy-file tmpfile filename t)
3467 (setq tramp-temp-buffer-file-name nil)
3468 (delete-file tmpfile)
3469 (signal (car err) (cdr err)))))
3470 (setq tramp-temp-buffer-file-name nil)
3471 ;; Don't rename, in order to keep context in SELinux.
3473 (copy-file tmpfile filename t)
3474 (delete-file tmpfile))))
3476 ;; Use inline file transfer.
3481 (set-buffer-multibyte nil)
3482 ;; Use encoding function or command.
3483 (with-tramp-progress-reporter
3485 "Encoding local file `%s' using `%s'"
3487 (if (functionp loc-enc)
3488 ;; The following `let' is a workaround for
3489 ;; the base64.el that comes with pgnus-0.84.
3490 ;; If both of the following conditions are
3491 ;; satisfied, it tries to write to a local
3492 ;; file in default-directory, but at this
3493 ;; point, default-directory is remote.
3494 ;; (`call-process-region' can't write to
3495 ;; remote files, it seems.) The file in
3496 ;; question is a tmp file anyway.
3497 (let ((coding-system-for-read 'binary)
3499 (tramp-compat-temporary-file-directory)))
3500 (insert-file-contents-literally tmpfile)
3501 (funcall loc-enc (point-min) (point-max)))
3503 (unless (zerop (tramp-call-local-coding-command
3507 (concat "Cannot write to `%s', "
3508 "local encoding command `%s' failed")
3509 filename loc-enc))))
3511 ;; Send buffer into remote decoding command which
3512 ;; writes to remote file. Because this happens on
3513 ;; the remote host, we cannot use the function.
3514 (with-tramp-progress-reporter
3516 "Decoding remote file `%s' using `%s'"
3518 (goto-char (point-max))
3519 (unless (bolp) (newline))
3523 (concat rem-dec " <<'%s'\n%s%s")
3524 (tramp-shell-quote-argument localname)
3525 tramp-end-of-heredoc
3527 tramp-end-of-heredoc))
3528 (tramp-barf-unless-okay
3530 "Couldn't write region to `%s', decode using `%s' failed"
3532 ;; When `file-precious-flag' is set, the region is
3533 ;; written to a temporary file. Check that the
3534 ;; checksum is equal to that from the local tmpfile.
3535 (when file-precious-flag
3538 ;; cksum runs locally, if possible.
3539 (zerop (tramp-call-process v "cksum" tmpfile t))
3540 ;; cksum runs remotely.
3541 (tramp-send-command-and-check
3544 "cksum <%s" (tramp-shell-quote-argument localname)))
3545 ;; ... they are different.
3549 (with-current-buffer (tramp-get-buffer v)
3553 (concat "Couldn't write region to `%s',"
3554 " decode using `%s' failed")
3555 filename rem-dec)))))
3558 (delete-file tmpfile)))
3560 ;; That's not expected.
3564 (concat "Method `%s' should specify both encoding and "
3565 "decoding command or an scp program")
3568 ;; Make `last-coding-system-used' have the right value.
3569 (when coding-system-used
3570 (set 'last-coding-system-used coding-system-used))))
3572 (tramp-flush-file-property v (file-name-directory localname))
3573 (tramp-flush-file-property v localname)
3575 ;; We must protect `last-coding-system-used', now we have set it
3576 ;; to its correct value.
3577 (let (last-coding-system-used (need-chown t))
3578 ;; Set file modification time.
3579 (when (or (eq visit t) (stringp visit))
3580 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3581 (set-visited-file-modtime
3582 ;; We must pass modtime explicitly, because FILENAME can
3583 ;; be different from (buffer-file-name), f.e. if
3584 ;; `file-precious-flag' is set.
3586 (when (and (= (nth 2 file-attr) uid)
3587 (= (nth 3 file-attr) gid))
3588 (setq need-chown nil))))
3590 ;; Set the ownership.
3592 (tramp-set-file-uid-gid filename uid gid))
3593 (when (or (eq visit t) (null visit) (stringp visit))
3594 (tramp-message v 0 "Wrote %s" filename))
3595 (run-hooks 'tramp-handle-write-region-hook)))))
3597 (defvar tramp-vc-registered-file-names nil
3598 "List used to collect file names, which are checked during `vc-registered'.")
3600 ;; VC backends check for the existence of various different special
3601 ;; files. This is very time consuming, because every single check
3602 ;; requires a remote command (the file cache must be invalidated).
3603 ;; Therefore, we apply a kind of optimization. We install the file
3604 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3605 ;; remembers all file names for which `file-exists-p' or
3606 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3607 ;; is performed. Afterwards, a script is applied for all collected
3608 ;; file names, using just one remote command. The result of this
3609 ;; script is used to fill the file cache with actual values. Now we
3610 ;; can reset the file name handlers, and we make a second run of
3611 ;; `vc-registered', which returns the expected result without sending
3612 ;; any other remote command.
3613 (defun tramp-sh-handle-vc-registered (file)
3614 "Like `vc-registered' for Tramp files."
3615 (tramp-compat-with-temp-message ""
3616 (with-parsed-tramp-file-name file nil
3617 (with-tramp-progress-reporter
3618 v 3 (format-message "Checking `vc-registered' for %s" file)
3620 ;; There could be new files, created by the vc backend. We
3621 ;; cannot reuse the old cache entries, therefore. In
3622 ;; `tramp-get-file-property', `remote-file-name-inhibit-cache'
3623 ;; could also be a timestamp as `current-time' returns. This
3624 ;; means invalidate all cache entries with an older timestamp.
3625 (let (tramp-vc-registered-file-names
3626 (remote-file-name-inhibit-cache (current-time))
3627 (file-name-handler-alist
3628 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3630 ;; Here we collect only file names, which need an operation.
3631 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3632 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3634 ;; Send just one command, in order to fill the cache.
3635 (when tramp-vc-registered-file-names
3636 (tramp-maybe-send-script
3638 (format tramp-vc-registered-read-file-names
3639 (tramp-get-file-exists-command v)
3640 (format "%s -r" (tramp-get-test-command v)))
3641 "tramp_vc_registered_read_file_names")
3646 ;; We cannot use `tramp-send-command-and-read',
3647 ;; because this does not cooperate well with
3648 ;; heredoc documents.
3652 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3653 tramp-end-of-heredoc
3654 (mapconcat 'tramp-shell-quote-argument
3655 tramp-vc-registered-file-names
3657 tramp-end-of-heredoc))
3658 (with-current-buffer (tramp-get-connection-buffer v)
3659 ;; Read the expression.
3660 (goto-char (point-min))
3661 (read (current-buffer)))))
3663 (tramp-set-file-property
3664 v (car elt) (cadr elt) (cadr (cdr elt))))))
3666 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3667 ;; calls shall be answered from the file cache. We unset
3668 ;; `process-file-side-effects' and `remote-file-name-inhibit-cache'
3669 ;; in order to keep the cache.
3670 (let ((vc-handled-backends vc-handled-backends)
3671 remote-file-name-inhibit-cache process-file-side-effects)
3672 ;; Reduce `vc-handled-backends' in order to minimize process calls.
3673 (when (and (memq 'Bzr vc-handled-backends)
3674 (boundp 'vc-bzr-program)
3675 (not (with-tramp-connection-property v vc-bzr-program
3676 (tramp-find-executable
3677 v vc-bzr-program (tramp-get-remote-path v)))))
3678 (setq vc-handled-backends (remq 'Bzr vc-handled-backends)))
3679 (when (and (memq 'Git vc-handled-backends)
3680 (boundp 'vc-git-program)
3681 (not (with-tramp-connection-property v vc-git-program
3682 (tramp-find-executable
3683 v vc-git-program (tramp-get-remote-path v)))))
3684 (setq vc-handled-backends (remq 'Git vc-handled-backends)))
3685 (when (and (memq 'Hg vc-handled-backends)
3686 (boundp 'vc-hg-program)
3687 (not (with-tramp-connection-property v vc-hg-program
3688 (tramp-find-executable
3689 v vc-hg-program (tramp-get-remote-path v)))))
3690 (setq vc-handled-backends (remq 'Hg vc-handled-backends)))
3693 (tramp-run-real-handler 'vc-registered (list file))))))))
3695 ;;;###tramp-autoload
3696 (defun tramp-sh-file-name-handler (operation &rest args)
3697 "Invoke remote-shell Tramp file name handler.
3698 Fall back to normal file name handler if no Tramp handler exists."
3699 (when (and tramp-locked (not tramp-locker))
3700 (setq tramp-locked nil)
3702 (car-safe tramp-current-connection) 'file-error
3703 "Forbidden reentrant call of Tramp"))
3704 (let ((tl tramp-locked))
3705 (setq tramp-locked t)
3707 (let ((tramp-locker t))
3709 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3711 (apply (cdr fn) args)
3712 (tramp-run-real-handler operation args)))))
3713 (setq tramp-locked tl))))
3715 (defun tramp-vc-file-name-handler (operation &rest args)
3716 "Invoke special file name handler, which collects files to be handled."
3719 (tramp-replace-environment-variables
3720 (apply 'tramp-file-name-for-operation operation args)))
3721 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3722 (with-parsed-tramp-file-name filename nil
3724 ;; That's what we want: file names, for which checks are
3725 ;; applied. We assume that VC uses only `file-exists-p' and
3726 ;; `file-readable-p' checks; otherwise we must extend the
3727 ;; list. We do not perform any action, but return nil, in
3728 ;; order to keep `vc-registered' running.
3729 ((and fn (memq operation '(file-exists-p file-readable-p)))
3730 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3732 ;; `process-file' and `start-file-process' shall be ignored.
3733 ((and fn (eq operation 'process-file) 0))
3734 ((and fn (eq operation 'start-file-process) nil))
3735 ;; Tramp file name handlers like `expand-file-name'. They
3737 (fn (save-match-data (apply (cdr fn) args)))
3738 ;; Default file name handlers, we don't care.
3739 (t (tramp-run-real-handler operation args)))))))
3741 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback)
3742 "Like `file-notify-add-watch' for Tramp files."
3743 (setq file-name (expand-file-name file-name))
3744 (with-parsed-tramp-file-name file-name nil
3745 (let ((default-directory (file-name-directory file-name))
3746 command events filter p sequence)
3748 ;; gvfs-monitor-dir.
3749 ((setq command (tramp-get-remote-gvfs-monitor-dir v))
3750 (setq filter 'tramp-sh-gvfs-monitor-dir-process-filter
3753 ((and (memq 'change flags) (memq 'attribute-change flags))
3754 '(created changed changes-done-hint moved deleted
3756 ((memq 'change flags)
3757 '(created changed changes-done-hint moved deleted))
3758 ((memq 'attribute-change flags) '(attribute-changed)))
3759 sequence `(,command ,localname)))
3761 ((setq command (tramp-get-remote-inotifywait v))
3762 (setq filter 'tramp-sh-inotifywait-process-filter
3765 ((and (memq 'change flags) (memq 'attribute-change flags))
3766 (concat "create,modify,move,moved_from,moved_to,move_self,"
3767 "delete,delete_self,attrib,ignored"))
3768 ((memq 'change flags)
3769 (concat "create,modify,move,moved_from,moved_to,move_self,"
3770 "delete,delete_self,ignored"))
3771 ((memq 'attribute-change flags) "attrib,ignored"))
3772 sequence `(,command "-mq" "-e" ,events ,localname)))
3775 v 'file-notify-error
3776 "No file notification program found on %s"
3777 (file-remote-p file-name))))
3781 (file-name-nondirectory command)
3782 (generate-new-buffer
3783 (format " *%s*" (file-name-nondirectory command)))
3785 ;; Return the process object as watch-descriptor.
3786 (if (not (processp p))
3788 v 'file-notify-error
3789 "`%s' failed to start on remote host"
3790 (mapconcat 'identity sequence " "))
3791 (tramp-message v 6 "Run `%s', %S" (mapconcat 'identity sequence " ") p)
3792 (tramp-set-connection-property p "vector" v)
3793 ;; Needed for `tramp-sh-gvfs-monitor-dir-process-filter'.
3794 (tramp-compat-process-put p 'events events)
3795 (tramp-compat-process-put p 'watch-name localname)
3796 (tramp-compat-set-process-query-on-exit-flag p nil)
3797 (set-process-filter p filter)
3798 ;; There might be an error if the monitor is not supported.
3799 ;; Give the filter a chance to read the output.
3800 (tramp-accept-process-output p 1)
3801 (unless (memq (process-status p) '(run open))
3803 v 'file-notify-error "Monitoring not supported for `%s'" file-name))
3806 (defun tramp-sh-gvfs-monitor-dir-process-filter (proc string)
3807 "Read output from \"gvfs-monitor-dir\" and add corresponding \
3808 file-notify events."
3809 (let ((remote-prefix
3810 (with-current-buffer (process-buffer proc)
3811 (file-remote-p default-directory)))
3812 (rest-string (tramp-compat-process-get proc 'rest-string)))
3814 (tramp-message proc 10 "Previous string:\n%s" rest-string))
3815 (tramp-message proc 6 "%S\n%s" proc string)
3816 (setq string (concat rest-string string)
3817 ;; Attribute change is returned in unused wording.
3818 string (tramp-compat-replace-regexp-in-string
3819 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
3820 (when (string-match "Monitoring not supported" string)
3821 (delete-process proc))
3823 (while (string-match
3825 "Directory Monitor Event:[\n\r]+"
3826 "Child = \\([^\n\r]+\\)[\n\r]+"
3827 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3828 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3830 (let* ((file (match-string 1 string))
3831 (file1 (match-string 3 string))
3836 (tramp-compat-replace-regexp-in-string
3837 "_" "-" (downcase (match-string 4 string))))
3838 ;; File names are returned as absolute paths. We must
3839 ;; add the remote prefix.
3840 (concat remote-prefix file)
3841 (when file1 (concat remote-prefix file1)))))
3842 (setq string (replace-match "" nil nil string))
3843 ;; Remove watch when file or directory to be watched is deleted.
3844 (when (and (member (cadr object) '(moved deleted))
3846 file (tramp-compat-process-get proc 'watch-name)))
3847 (delete-process proc))
3848 ;; Usually, we would add an Emacs event now. Unfortunately,
3849 ;; `unread-command-events' does not accept several events at
3850 ;; once. Therefore, we apply the callback directly.
3851 (when (member (cadr object) (tramp-compat-process-get proc 'events))
3852 (tramp-compat-funcall 'file-notify-callback object))))
3854 ;; Save rest of the string.
3855 (when (zerop (length string)) (setq string nil))
3856 (when string (tramp-message proc 10 "Rest string:\n%s" string))
3857 (tramp-compat-process-put proc 'rest-string string)))
3859 (defun tramp-sh-inotifywait-process-filter (proc string)
3860 "Read output from \"inotifywait\" and add corresponding file-notify events."
3861 (tramp-message proc 6 "%S\n%s" proc string)
3862 (dolist (line (split-string string "[\n\r]+" 'omit-nulls))
3863 ;; Check, whether there is a problem.
3866 (concat "^[^[:blank:]]+"
3867 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3868 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3870 (tramp-error proc 'file-notify-error "%s" line))
3878 (tramp-compat-replace-regexp-in-string "_" "-" (downcase x))))
3879 (split-string (match-string 1 line) "," 'omit-nulls))
3880 (match-string 3 line))))
3881 ;; Remove watch when file or directory to be watched is deleted.
3882 (when (equal (cadr object) 'ignored)
3883 (delete-process proc))
3884 ;; Usually, we would add an Emacs event now. Unfortunately,
3885 ;; `unread-command-events' does not accept several events at
3886 ;; once. Therefore, we apply the callback directly.
3887 (tramp-compat-funcall 'file-notify-callback object))))
3889 ;;; Internal Functions:
3891 (defun tramp-maybe-send-script (vec script name)
3892 "Define in remote shell function NAME implemented as SCRIPT.
3893 Only send the definition if it has not already been done."
3894 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3896 (let ((scripts (tramp-get-connection-property
3897 (tramp-get-connection-process vec) "scripts" nil)))
3898 (unless (member name scripts)
3899 (with-tramp-progress-reporter
3900 vec 5 (format-message "Sending script `%s'" name)
3901 ;; In bash, leading TABs like in `tramp-vc-registered-read-file-names'
3902 ;; could result in unwanted command expansion. Avoid this.
3903 (setq script (tramp-compat-replace-regexp-in-string
3904 (make-string 1 ?\t) (make-string 8 ? ) script))
3905 ;; The script could contain a call of Perl. This is masked with `%s'.
3906 (when (and (string-match "%s" script)
3907 (not (tramp-get-remote-perl vec)))
3908 (tramp-error vec 'file-error "No Perl available on remote host"))
3909 (tramp-barf-unless-okay
3911 (format "%s () {\n%s\n}"
3912 name (format script (tramp-get-remote-perl vec)))
3913 "Script %s sending failed" name)
3914 (tramp-set-connection-property
3915 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3917 (defun tramp-run-test (switch filename)
3918 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3919 Returns the exit code of the `test' program."
3920 (with-parsed-tramp-file-name filename nil
3921 (tramp-send-command-and-check
3925 (tramp-get-test-command v)
3927 (tramp-shell-quote-argument localname)))))
3929 (defun tramp-run-test2 (format-string file1 file2)
3930 "Run `test'-like program on the remote system, given FILE1, FILE2.
3931 FORMAT-STRING contains the program name, switches, and place holders.
3932 Returns the exit code of the `test' program. Barfs if the methods,
3933 hosts, or files, disagree."
3934 (unless (tramp-equal-remote file1 file2)
3935 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3938 "tramp-run-test2 only implemented for same method, user, host")))
3939 (with-parsed-tramp-file-name file1 v1
3940 (with-parsed-tramp-file-name file1 v2
3941 (tramp-send-command-and-check
3943 (format format-string
3944 (tramp-shell-quote-argument v1-localname)
3945 (tramp-shell-quote-argument v2-localname))))))
3947 (defun tramp-find-executable
3948 (vec progname dirlist &optional ignore-tilde ignore-path)
3949 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3950 First arg VEC specifies the connection, PROGNAME is the program
3951 to search for, and DIRLIST gives the list of directories to
3952 search. If IGNORE-TILDE is non-nil, directory names starting
3953 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3956 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3958 This function expects to be in the right *tramp* buffer."
3959 (with-current-buffer (tramp-get-connection-buffer vec)
3961 ;; Check whether the executable is in $PATH. "which(1)" does not
3962 ;; report always a correct error code; therefore we check the
3963 ;; number of words it returns. "SunOS 5.10" (and maybe "SunOS
3964 ;; 5.11") have problems with this command, we disable the call
3966 (unless (or ignore-path
3968 (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3969 (tramp-get-connection-property vec "uname" "")))
3970 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3971 (goto-char (point-min))
3972 (if (looking-at "^\\s-*1$")
3973 (setq result (concat "\\" progname))))
3976 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3977 ;; `remove' is in CL, and we want to avoid CL dependencies.
3980 (setq d (car dirlist))
3981 (setq dirlist (cdr dirlist))
3982 (unless (char-equal ?~ (aref d 0))
3983 (setq newdl (cons d newdl))))
3984 (setq dirlist (nreverse newdl))))
3987 (format (concat "while read d; "
3988 "do if test -x $d/%s && test -f $d/%s; "
3989 "then echo tramp_executable $d/%s; "
3990 "break; fi; done <<'%s'\n"
3992 progname progname progname
3993 tramp-end-of-heredoc
3994 (mapconcat 'identity dirlist "\n")
3995 tramp-end-of-heredoc))
3996 (goto-char (point-max))
3997 (when (search-backward "tramp_executable " nil t)
3998 (skip-chars-forward "^ ")
3999 (skip-chars-forward " ")
4000 (setq result (buffer-substring (point) (point-at-eol)))))
4003 (defun tramp-set-remote-path (vec)
4004 "Sets the remote environment PATH to existing directories.
4005 I.e., for each directory in `tramp-remote-path', it is tested
4006 whether it exists and if so, it is added to the environment
4008 (tramp-message vec 5 "Setting $PATH environment variable")
4010 vec (format "PATH=%s; export PATH"
4011 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
4013 ;; ------------------------------------------------------------
4014 ;; -- Communication with external shell --
4015 ;; ------------------------------------------------------------
4017 (defun tramp-find-file-exists-command (vec)
4018 "Find a command on the remote host for checking if a file exists.
4019 Here, we are looking for a command which has zero exit status if the
4020 file exists and nonzero exit status otherwise."
4021 (let ((existing "/")
4023 (tramp-shell-quote-argument "/ this file does not exist "))
4025 ;; The algorithm is as follows: we try a list of several commands.
4026 ;; For each command, we first run `$cmd /' -- this should return
4027 ;; true, as the root directory always exists. And then we run
4028 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
4029 ;; does not exist. This should return false. We use the first
4030 ;; command we find that seems to work.
4031 ;; The list of commands to try is as follows:
4032 ;; `ls -d' This works on most systems, but NetBSD 1.4
4033 ;; has a bug: `ls' always returns zero exit
4034 ;; status, even for files which don't exist.
4035 ;; `test -e' Some Bourne shells have a `test' builtin
4036 ;; which does not know the `-e' option.
4037 ;; `/bin/test -e' For those, the `test' binary on disk normally
4038 ;; provides the option. Alas, the binary
4039 ;; is sometimes `/bin/test' and sometimes it's
4041 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
4044 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
4045 (tramp-send-command-and-check
4046 vec (format "%s %s" result existing))
4047 (not (tramp-send-command-and-check
4048 vec (format "%s %s" result nonexistent)))))
4050 (and (setq result "/bin/test -e")
4051 (tramp-send-command-and-check
4052 vec (format "%s %s" result existing))
4053 (not (tramp-send-command-and-check
4054 vec (format "%s %s" result nonexistent)))))
4056 (and (setq result "/usr/bin/test -e")
4057 (tramp-send-command-and-check
4058 vec (format "%s %s" result existing))
4059 (not (tramp-send-command-and-check
4060 vec (format "%s %s" result nonexistent)))))
4062 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
4063 (tramp-send-command-and-check
4064 vec (format "%s %s" result existing))
4065 (not (tramp-send-command-and-check
4066 vec (format "%s %s" result nonexistent))))))
4068 vec 'file-error "Couldn't find command to check if file exists"))
4071 (defun tramp-open-shell (vec shell)
4072 "Opens shell SHELL."
4073 (with-tramp-progress-reporter
4074 vec 5 (format-message "Opening remote shell `%s'" shell)
4075 ;; Find arguments for this shell.
4076 (let ((alist tramp-sh-extra-args)
4078 (while (and alist (null extra-args))
4079 (setq item (pop alist))
4080 (when (string-match (car item) shell)
4081 (setq extra-args (cdr item))))
4082 ;; It is useful to set the prompt in the following command
4083 ;; because some people have a setting for $PS1 which /bin/sh
4084 ;; doesn't know about and thus /bin/sh will display a strange
4085 ;; prompt. For example, if $PS1 has "${CWD}" in the value, then
4086 ;; ksh will display the current working directory but /bin/sh
4087 ;; will display a dollar sign. The following command line sets
4088 ;; $PS1 to a sane value, and works under Bourne-ish shells as
4089 ;; well as csh-like shells. We also unset the variable $ENV
4090 ;; because that is read by some sh implementations (eg, bash
4091 ;; when called as sh) on startup; this way, we avoid the startup
4092 ;; file clobbering $PS1. $PROMPT_COMMAND is another way to set
4093 ;; the prompt in /bin/bash, it must be discarded as well.
4094 ;; $HISTFILE is set according to `tramp-histfile-override'.
4097 "exec env ENV='' %s PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
4098 (if (stringp tramp-histfile-override)
4099 (format "HISTFILE=%s"
4100 (tramp-shell-quote-argument tramp-histfile-override))
4101 (if tramp-histfile-override
4102 "HISTFILE='' HISTFILESIZE=0 HISTSIZE=0"
4104 (tramp-shell-quote-argument tramp-end-of-output)
4105 shell (or extra-args ""))
4107 (tramp-set-connection-property
4108 (tramp-get-connection-process vec) "remote-shell" shell)))
4110 (defun tramp-find-shell (vec)
4111 "Opens a shell on the remote host which groks tilde expansion."
4112 (with-current-buffer (tramp-get-buffer vec)
4113 (let ((default-shell (tramp-get-method-parameter vec 'tramp-remote-shell))
4116 (with-tramp-connection-property vec "remote-shell"
4117 ;; CCC: "root" does not exist always, see QNAP 459.
4118 ;; Which check could we apply instead?
4119 (tramp-send-command vec "echo ~root" t)
4120 (if (or (string-match "^~root$" (buffer-string))
4121 ;; The default shell (ksh93) of OpenSolaris and
4122 ;; Solaris is buggy. We've got reports for
4123 ;; "SunOS 5.10" and "SunOS 5.11" so far.
4124 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
4125 (tramp-get-connection-property
4128 (or (tramp-find-executable
4129 vec "bash" (tramp-get-remote-path vec) t t)
4130 (tramp-find-executable
4131 vec "ksh" (tramp-get-remote-path vec) t t)
4132 ;; Maybe it works at least for some other commands.
4138 "Couldn't find a remote shell which groks tilde "
4139 "expansion, using `%s'")
4144 ;; Open a new shell if needed.
4145 (unless (string-equal shell default-shell)
4147 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
4148 (tramp-open-shell vec shell)))))
4150 ;; Utility functions.
4152 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
4153 "Wait for shell prompt and barf if none appears.
4154 Looks at process PROC to see if a shell prompt appears in TIMEOUT
4155 seconds. If not, it produces an error message with the given ERROR-ARGS."
4156 (let ((vec (tramp-get-connection-property proc "vector" nil)))
4158 (tramp-wait-for-regexp
4161 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
4163 (delete-process proc)
4164 (apply 'tramp-error-with-buffer
4165 (tramp-get-connection-buffer vec) vec 'file-error error-args)))))
4167 (defun tramp-open-connection-setup-interactive-shell (proc vec)
4168 "Set up an interactive shell.
4169 Mainly sets the prompt and the echo correctly. PROC is the shell
4170 process to set up. VEC specifies the connection."
4171 (let ((tramp-end-of-output tramp-initial-end-of-output)
4172 (case-fold-search t))
4173 (tramp-open-shell vec (tramp-get-method-parameter vec 'tramp-remote-shell))
4175 ;; Disable tab and echo expansion.
4176 (tramp-message vec 5 "Setting up remote shell environment")
4178 vec "stty tab0 -inlcr -onlcr -echo kill '^U' erase '^H'" t)
4179 ;; Check whether the echo has really been disabled. Some
4180 ;; implementations, like busybox of embedded GNU/Linux, don't
4181 ;; support disabling.
4182 (tramp-send-command vec "echo foo" t)
4183 (with-current-buffer (process-buffer proc)
4184 (goto-char (point-min))
4185 (when (looking-at "echo foo")
4186 (tramp-set-connection-property proc "remote-echo" t)
4187 (tramp-message vec 5 "Remote echo still on. Ok.")
4188 ;; Make sure backspaces and their echo are enabled and no line
4189 ;; width magic interferes with them.
4190 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
4192 (tramp-message vec 5 "Setting shell prompt")
4194 vec (format "PS1=%s PS2='' PS3='' PROMPT_COMMAND=''"
4195 (tramp-shell-quote-argument tramp-end-of-output)) t)
4197 ;; Check whether the output of "uname -sr" has been changed. If
4198 ;; yes, this is a strong indication that we must expire all
4199 ;; connection properties. We start again with
4200 ;; `tramp-maybe-open-connection', it will be caught there.
4201 (tramp-message vec 5 "Checking system information")
4202 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
4204 (tramp-set-connection-property
4206 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
4207 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
4210 "Connection reset, because remote host changed from `%s' to `%s'"
4211 old-uname new-uname)
4212 ;; We want to keep the password.
4213 (tramp-cleanup-connection vec t t)
4214 (throw 'uname-changed (tramp-maybe-open-connection vec))))
4216 ;; Try to set up the coding system correctly.
4217 ;; CCC this can't be the right way to do it. Hm.
4218 (tramp-message vec 5 "Determining coding system")
4219 (with-current-buffer (process-buffer proc)
4220 (if (featurep 'mule)
4221 ;; Use MULE to select the right EOL convention for communicating
4222 ;; with the process.
4223 (let ((cs (or (and (memq 'utf-8 (coding-system-list))
4224 (string-match "utf-?8" (tramp-get-remote-locale vec))
4225 (cons 'utf-8 'utf-8))
4226 (tramp-compat-funcall 'process-coding-system proc)
4227 (cons 'undecided 'undecided)))
4228 cs-decode cs-encode)
4229 (when (symbolp cs) (setq cs (cons cs cs)))
4230 (setq cs-decode (or (car cs) 'undecided)
4231 cs-encode (or (cdr cs) 'undecided))
4233 (tramp-compat-coding-system-change-eol-conversion
4236 "^Darwin" (tramp-get-connection-property vec "uname" ""))
4238 (tramp-send-command vec "echo foo ; echo bar" t)
4239 (goto-char (point-min))
4240 (when (search-forward "\r" nil t)
4241 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
4243 ;; Special setting for Mac OS X.
4244 (when (and (string-match
4245 "^Darwin" (tramp-get-connection-property vec "uname" ""))
4246 (memq 'utf-8-hfs (coding-system-list)))
4247 (setq cs-decode 'utf-8-hfs
4248 cs-encode 'utf-8-hfs))
4249 (tramp-compat-funcall
4250 'set-buffer-process-coding-system cs-decode cs-encode)
4252 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
4253 ;; Look for ^M and do something useful if found.
4254 (when (search-forward "\r" nil t)
4255 ;; We have found a ^M but cannot frob the process coding system
4256 ;; because we're running on a non-MULE Emacs. Let's try
4258 (tramp-send-command vec "stty -onlcr" t))))
4260 (tramp-send-command vec "set +o vi +o emacs" t)
4262 ;; Check whether the remote host suffers from buggy
4263 ;; `send-process-string'. This is known for FreeBSD (see comment in
4264 ;; `send_process', file process.c). I've tested sending 624 bytes
4265 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
4266 ;; this host type is detected locally. It cannot handle remote
4268 (with-tramp-connection-property proc "chunksize"
4270 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
4274 vec 5 "Checking remote host type for `send-process-string' bug")
4276 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
4279 ;; Set remote PATH variable.
4280 (tramp-set-remote-path vec)
4282 ;; Search for a good shell before searching for a command which
4283 ;; checks if a file exists. This is done because Tramp wants to use
4284 ;; "test foo; echo $?" to check if various conditions hold, and
4285 ;; there are buggy /bin/sh implementations which don't execute the
4286 ;; "echo $?" part if the "test" part has an error. In particular,
4287 ;; the OpenSolaris /bin/sh is a problem. There are also other
4288 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
4289 ;; in function declarations, or changing HISTFILE in place.
4290 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
4292 (tramp-find-shell vec)
4294 ;; Disable unexpected output.
4295 (tramp-send-command vec "mesg n 2>/dev/null; biff n 2>/dev/null" t)
4297 ;; IRIX64 bash expands "!" even when in single quotes. This
4298 ;; destroys our shell functions, we must disable it. See
4299 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4300 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
4301 (tramp-send-command vec "set +H" t))
4303 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
4304 (when (string-match "BSD\\|Darwin"
4305 (tramp-get-connection-property vec "uname" ""))
4306 (tramp-send-command vec "stty -oxtabs" t))
4308 ;; Set utf8 encoding. Needed for Mac OS X, for example. This is
4309 ;; non-POSIX, so we must expect errors on some systems.
4310 (tramp-send-command vec "stty iutf8 2>/dev/null" t)
4312 ;; Set `remote-tty' process property.
4313 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
4314 (unless (zerop (length tty))
4315 (tramp-compat-process-put proc 'remote-tty tty)))
4317 ;; Dump stty settings in the traces.
4318 (when (>= tramp-verbose 9)
4319 (tramp-send-command vec "stty -a" t))
4321 ;; Set the environment.
4322 (tramp-message vec 5 "Setting default environment")
4324 (let ((env (append `(,(tramp-get-remote-locale vec))
4325 (copy-sequence tramp-remote-process-environment)))
4328 (setq item (tramp-compat-split-string (car env) "="))
4329 (setcdr item (mapconcat 'identity (cdr item) "="))
4330 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
4331 (push (format "%s %s" (car item) (cdr item)) vars)
4332 (push (car item) unset))
4333 (setq env (cdr env)))
4337 (format "while read var val; do export $var=$val; done <<'%s'\n%s\n%s"
4338 tramp-end-of-heredoc
4339 (mapconcat 'identity vars "\n")
4340 tramp-end-of-heredoc)
4344 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
4346 ;; Old text from documentation of tramp-methods:
4347 ;; Using a uuencode/uudecode inline method is discouraged, please use one
4348 ;; of the base64 methods instead since base64 encoding is much more
4349 ;; reliable and the commands are more standardized between the different
4350 ;; Unix versions. But if you can't use base64 for some reason, please
4351 ;; note that the default uudecode command does not work well for some
4352 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
4353 ;; the following command for uudecode:
4355 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4357 ;; For Irix, no solution is known yet.
4359 (autoload 'uudecode-decode-region "uudecode")
4361 (defconst tramp-local-coding-commands
4362 `((b64 base64-encode-region base64-decode-region)
4363 (uu tramp-uuencode-region uudecode-decode-region)
4364 (pack ,(format tramp-perl-pack "perl") ,(format tramp-perl-unpack "perl")))
4365 "List of local coding commands for inline transfer.
4366 Each item is a list that looks like this:
4368 \(FORMAT ENCODING DECODING)
4370 FORMAT is symbol describing the encoding/decoding format. It can be
4371 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4373 ENCODING and DECODING can be strings, giving commands, or symbols,
4374 giving functions. If they are strings, then they can contain
4375 the \"%s\" format specifier. If that specifier is present, the input
4376 file name will be put into the command line at that spot. If the
4377 specifier is not present, the input should be read from standard
4380 If they are functions, they will be called with two arguments, start
4381 and end of region, and are expected to replace the region contents
4382 with the encoded or decoded results, respectively.")
4384 (defconst tramp-remote-coding-commands
4385 `((b64 "base64" "base64 -d -i")
4386 ;; "-i" is more robust with older base64 from GNU coreutils.
4387 ;; However, I don't know whether all base64 versions do supports
4389 (b64 "base64" "base64 -d")
4390 (b64 "openssl enc -base64" "openssl enc -d -base64")
4391 (b64 "mimencode -b" "mimencode -u -b")
4392 (b64 "mmencode -b" "mmencode -u -b")
4393 (b64 "recode data..base64" "recode base64..data")
4394 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
4395 (b64 tramp-perl-encode tramp-perl-decode)
4396 ;; This is painful slow, so we put it on the end.
4397 (b64 tramp-awk-encode tramp-awk-decode ,tramp-awk-coding-test)
4398 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
4399 (uu "uuencode xxx" "uudecode -o -")
4400 (uu "uuencode xxx" "uudecode -p")
4401 (uu "uuencode xxx" tramp-uudecode)
4402 (pack tramp-perl-pack tramp-perl-unpack))
4403 "List of remote coding commands for inline transfer.
4404 Each item is a list that looks like this:
4406 \(FORMAT ENCODING DECODING [TEST])
4408 FORMAT is a symbol describing the encoding/decoding format. It can be
4409 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4411 ENCODING and DECODING can be strings, giving commands, or symbols,
4412 giving variables. If they are strings, then they can contain
4413 the \"%s\" format specifier. If that specifier is present, the input
4414 file name will be put into the command line at that spot. If the
4415 specifier is not present, the input should be read from standard
4418 If they are variables, this variable is a string containing a
4419 Perl or Shell implementation for this functionality. This
4420 program will be transferred to the remote host, and it is
4421 available as shell function with the same name. A \"%t\" format
4422 specifier in the variable value denotes a temporary file.
4424 The optional TEST command can be used for further tests, whether
4425 ENCODING and DECODING are applicable.")
4427 (defun tramp-find-inline-encoding (vec)
4428 "Find an inline transfer encoding that works.
4429 Goes through the list `tramp-local-coding-commands' and
4430 `tramp-remote-coding-commands'."
4432 (let ((local-commands tramp-local-coding-commands)
4434 (p (tramp-get-connection-process vec))
4435 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
4436 (while (and local-commands (not found))
4437 (setq litem (pop local-commands))
4438 (catch 'wont-work-local
4439 (let ((format (nth 0 litem))
4440 (remote-commands tramp-remote-coding-commands))
4441 (setq loc-enc (nth 1 litem))
4442 (setq loc-dec (nth 2 litem))
4443 ;; If the local encoder or decoder is a string, the
4444 ;; corresponding command has to work locally.
4445 (if (not (stringp loc-enc))
4447 vec 5 "Checking local encoding function `%s'" loc-enc)
4449 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4450 (unless (zerop (tramp-call-local-coding-command
4452 (throw 'wont-work-local nil)))
4453 (if (not (stringp loc-dec))
4455 vec 5 "Checking local decoding function `%s'" loc-dec)
4457 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4458 (unless (zerop (tramp-call-local-coding-command
4460 (throw 'wont-work-local nil)))
4461 ;; Search for remote coding commands with the same format
4462 (while (and remote-commands (not found))
4463 (setq ritem (pop remote-commands))
4464 (catch 'wont-work-remote
4465 (when (equal format (nth 0 ritem))
4466 (setq rem-enc (nth 1 ritem))
4467 (setq rem-dec (nth 2 ritem))
4468 (setq rem-test (nth 3 ritem))
4469 ;; Check the remote test command if exists.
4470 (when (stringp rem-test)
4472 vec 5 "Checking remote test command `%s'" rem-test)
4473 (unless (tramp-send-command-and-check vec rem-test t)
4474 (throw 'wont-work-remote nil)))
4475 ;; Check if remote perl exists when necessary.
4476 (when (and (symbolp rem-enc)
4477 (string-match "perl" (symbol-name rem-enc))
4478 (not (tramp-get-remote-perl vec)))
4479 (throw 'wont-work-remote nil))
4480 ;; Check if remote encoding and decoding commands can be
4481 ;; called remotely with null input and output. This makes
4482 ;; sure there are no syntax errors and the command is really
4483 ;; found. Note that we do not redirect stdout to /dev/null,
4484 ;; for two reasons: when checking the decoding command, we
4485 ;; actually check the output it gives. And also, when
4486 ;; redirecting "mimencode" output to /dev/null, then as root
4487 ;; it might change the permissions of /dev/null!
4488 (when (not (stringp rem-enc))
4489 (let ((name (symbol-name rem-enc)))
4490 (while (string-match (regexp-quote "-") name)
4491 (setq name (replace-match "_" nil t name)))
4492 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4493 (setq rem-enc name)))
4496 "Checking remote encoding command `%s' for sanity" rem-enc)
4497 (unless (tramp-send-command-and-check
4498 vec (format "%s </dev/null" rem-enc) t)
4499 (throw 'wont-work-remote nil))
4501 (when (not (stringp rem-dec))
4502 (let ((name (symbol-name rem-dec))
4503 (value (symbol-value rem-dec))
4505 (while (string-match (regexp-quote "-") name)
4506 (setq name (replace-match "_" nil t name)))
4507 (when (string-match "\\(^\\|[^%]\\)%t" value)
4511 tramp-temp-name-prefix
4512 (tramp-get-remote-tmpdir vec)))
4518 (tramp-file-name-handler
4519 'file-remote-p tmpfile 'localname)))))
4520 (tramp-maybe-send-script vec value name)
4521 (setq rem-dec name)))
4524 "Checking remote decoding command `%s' for sanity" rem-dec)
4525 (unless (tramp-send-command-and-check
4527 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4529 (throw 'wont-work-remote nil))
4531 (with-current-buffer (tramp-get-buffer vec)
4532 (goto-char (point-min))
4533 (unless (looking-at (regexp-quote magic))
4534 (throw 'wont-work-remote nil)))
4536 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4537 (setq rem-enc (nth 1 ritem))
4538 (setq rem-dec (nth 2 ritem))
4539 (setq found t)))))))
4542 ;; Set connection properties. Since the commands are risky
4543 ;; (due to output direction), we cache them in the process cache.
4544 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4545 (tramp-set-connection-property p "local-encoding" loc-enc)
4546 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4547 (tramp-set-connection-property p "local-decoding" loc-dec)
4548 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4549 (tramp-set-connection-property p "remote-encoding" rem-enc)
4550 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4551 (tramp-set-connection-property p "remote-decoding" rem-dec)))))
4553 (defun tramp-call-local-coding-command (cmd input output)
4554 "Call the local encoding or decoding command.
4555 If CMD contains \"%s\", provide input file INPUT there in command.
4556 Otherwise, INPUT is passed via standard input.
4557 INPUT can also be nil which means `/dev/null'.
4558 OUTPUT can be a string (which specifies a file name), or t (which
4559 means standard output and thus the current buffer), or nil (which
4562 nil tramp-encoding-shell
4563 (when (and input (not (string-match "%s" cmd))) input)
4564 (if (eq output t) t nil)
4566 tramp-encoding-command-switch
4568 (if (string-match "%s" cmd) (format cmd input) cmd)
4569 (if (stringp output) (concat " >" output) ""))))
4571 (defconst tramp-inline-compress-commands
4572 '(("gzip" "gzip -d")
4573 ("bzip2" "bzip2 -d")
4575 ("compress" "compress -d"))
4576 "List of compress and decompress commands for inline transfer.
4577 Each item is a list that looks like this:
4579 \(COMPRESS DECOMPRESS)
4581 COMPRESS or DECOMPRESS are strings with the respective commands.")
4583 (defun tramp-find-inline-compress (vec)
4584 "Find an inline transfer compress command that works.
4585 Goes through the list `tramp-inline-compress-commands'."
4587 (let ((commands tramp-inline-compress-commands)
4589 (p (tramp-get-connection-process vec))
4590 item compress decompress found)
4591 (while (and commands (not found))
4593 (setq item (pop commands)
4594 compress (nth 0 item)
4595 decompress (nth 1 item))
4598 "Checking local compress commands `%s', `%s' for sanity"
4599 compress decompress)
4602 (tramp-call-local-coding-command
4604 ;; Windows shells need the program file name after
4605 ;; the pipe symbol be quoted if they use forward
4606 ;; slashes as directory separators.
4607 (if (memq system-type '(windows-nt))
4608 "echo %s | \"%s\" | \"%s\""
4609 "echo %s | %s | %s")
4610 magic compress decompress) nil nil))
4614 "Checking remote compress commands `%s', `%s' for sanity"
4615 compress decompress)
4616 (unless (tramp-send-command-and-check
4617 vec (format "echo %s | %s | %s" magic compress decompress) t)
4621 ;; Did we find something?
4624 ;; Set connection properties. Since the commands are
4625 ;; risky (due to output direction), we cache them in the
4628 vec 5 "Using inline transfer compress command `%s'" compress)
4629 (tramp-set-connection-property p "inline-compress" compress)
4631 vec 5 "Using inline transfer decompress command `%s'" decompress)
4632 (tramp-set-connection-property p "inline-decompress" decompress))
4634 (tramp-set-connection-property p "inline-compress" nil)
4635 (tramp-set-connection-property p "inline-decompress" nil)
4637 vec 2 "Couldn't find an inline transfer compress command")))))
4639 (defun tramp-compute-multi-hops (vec)
4640 "Expands VEC according to `tramp-default-proxies-alist'.
4641 Gateway hops are already opened."
4642 (let ((target-alist `(,vec))
4643 (hops (or (tramp-file-name-hop vec) ""))
4647 ;; Ad-hoc proxy definitions.
4648 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4649 (let ((user (tramp-file-name-user item))
4650 (host (tramp-file-name-host item))
4652 tramp-prefix-format proxy tramp-postfix-host-format)))
4654 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4655 (and (stringp host) (regexp-quote host))
4656 (and (stringp user) (regexp-quote user))
4660 'tramp-default-proxies-alist
4661 (list (and (stringp host) (regexp-quote host))
4662 (and (stringp user) (regexp-quote user))
4664 (setq item (tramp-dissect-file-name proxy))))
4665 ;; Save the new value.
4666 (when (and hops tramp-save-ad-hoc-proxies)
4667 (customize-save-variable
4668 'tramp-default-proxies-alist tramp-default-proxies-alist))
4670 ;; Look for proxy hosts to be passed.
4671 (setq choices tramp-default-proxies-alist)
4673 (setq item (pop choices)
4674 proxy (eval (nth 2 item)))
4677 (string-match (or (eval (nth 0 item)) "")
4678 (or (tramp-file-name-host (car target-alist)) ""))
4680 (string-match (or (eval (nth 1 item)) "")
4681 (or (tramp-file-name-user (car target-alist)) "")))
4683 ;; No more hops needed.
4685 ;; Replace placeholders.
4690 ?u (or (tramp-file-name-user (car target-alist)) "")
4691 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4692 (with-parsed-tramp-file-name proxy l
4694 (push l target-alist)
4695 ;; Start next search.
4696 (setq choices tramp-default-proxies-alist)))))
4699 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4702 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4703 (tramp-file-name-method (car target-alist))))
4704 (let ((gw (pop target-alist))
4705 (hop (pop target-alist)))
4706 ;; Is the method prepared for gateways?
4707 (unless (tramp-file-name-port hop)
4710 "Connection `%s' is not supported for gateway access." hop))
4711 ;; Open the gateway connection.
4714 (tramp-file-name-method hop) (tramp-file-name-user hop)
4715 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil)
4717 ;; For the password prompt, we need the correct values.
4718 ;; Therefore, we must remember the gateway vector. But we
4719 ;; cannot do it as connection property, because it shouldn't
4720 ;; be persistent. And we have no started process yet either.
4721 (let ((tramp-verbose 0))
4722 (tramp-set-file-property (car target-alist) "" "gateway" hop))))
4724 ;; Foreign and out-of-band methods are not supported for multi-hops.
4725 (when (cdr target-alist)
4726 (setq choices target-alist)
4727 (while (setq item (pop choices))
4728 (when (or (not (tramp-get-method-parameter item 'tramp-login-program))
4729 (tramp-get-method-parameter item 'tramp-copy-program))
4732 "Method `%s' is not supported for multi-hops."
4733 (tramp-file-name-method item)))))
4735 ;; In case the host name is not used for the remote shell
4736 ;; command, the user could be misguided by applying a random
4738 (let* ((v (car target-alist))
4739 (method (tramp-file-name-method v))
4740 (host (tramp-file-name-host v)))
4743 ;; There are multi-hops.
4745 ;; The host name is used for the remote shell command.
4746 (member '("%h") (tramp-get-method-parameter v 'tramp-login-args))
4747 ;; The host is local. We cannot use `tramp-local-host-p'
4748 ;; here, because it opens a connection as well.
4749 (string-match tramp-local-host-regexp host))
4752 "Host `%s' looks like a remote host, `%s' can only use the local host"
4758 (defun tramp-ssh-controlmaster-options (vec)
4759 "Return the Control* arguments of the local ssh."
4761 ;; No options to be computed.
4762 ((or (null tramp-use-ssh-controlmaster-options)
4763 (null (assoc "%c" (tramp-get-method-parameter vec 'tramp-login-args))))
4766 ;; There is already a value to be used.
4767 ((stringp tramp-ssh-controlmaster-options) tramp-ssh-controlmaster-options)
4769 ;; Determine the options.
4770 (t (setq tramp-ssh-controlmaster-options "")
4771 (let ((case-fold-search t))
4773 (when (executable-find "ssh")
4775 (tramp-call-process vec "ssh" nil t nil "-o" "ControlMaster")
4776 (goto-char (point-min))
4777 (when (search-forward-regexp "missing.+argument" nil t)
4778 (setq tramp-ssh-controlmaster-options "-o ControlMaster=auto")))
4779 (unless (zerop (length tramp-ssh-controlmaster-options))
4781 ;; We use a non-existing IP address, in order to avoid
4782 ;; useless connections, and DNS timeouts.
4784 vec "ssh" nil t nil "-o" "ControlPath=%C" "0.0.0.1")
4785 (goto-char (point-min))
4786 (setq tramp-ssh-controlmaster-options
4787 (concat tramp-ssh-controlmaster-options
4788 (if (search-forward-regexp "unknown.+key" nil t)
4789 " -o ControlPath='tramp.%%r@%%h:%%p'"
4790 " -o ControlPath='tramp.%%C'"))))
4792 (tramp-call-process vec "ssh" nil t nil "-o" "ControlPersist")
4793 (goto-char (point-min))
4794 (when (search-forward-regexp "missing.+argument" nil t)
4795 (setq tramp-ssh-controlmaster-options
4796 (concat tramp-ssh-controlmaster-options
4797 " -o ControlPersist=no"))))))))
4798 tramp-ssh-controlmaster-options)))
4800 (defun tramp-maybe-open-connection (vec)
4801 "Maybe open a connection VEC.
4802 Does not do anything if a connection is already open, but re-opens the
4803 connection if a previous connection has died for some reason."
4804 (tramp-check-proper-method-and-host vec)
4806 (let ((p (tramp-get-connection-process vec))
4807 (process-name (tramp-get-connection-property vec "process-name" nil))
4808 (process-environment (copy-sequence process-environment))
4809 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4811 ;; If Tramp opens the same connection within a short time frame,
4812 ;; there is a problem. We shall signal this.
4813 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4814 (not (equal (butlast (append vec nil) 2)
4815 (car tramp-current-connection)))
4817 (current-time) (cdr tramp-current-connection))
4818 (or tramp-connection-min-time-diff 0)))
4819 (throw 'suppress 'suppress))
4821 ;; If too much time has passed since last command was sent, look
4822 ;; whether process is still alive. If it isn't, kill it. When
4823 ;; using ssh, it can sometimes happen that the remote end has hung
4824 ;; up but the local ssh client doesn't recognize this until it
4825 ;; tries to send some data to the remote end. So that's why we
4826 ;; try to send a command from time to time, then look again
4827 ;; whether the process is really alive.
4829 (when (and (> (tramp-time-diff
4831 (tramp-get-connection-property
4832 p "last-cmd-time" '(0 0 0)))
4834 p (processp p) (memq (process-status p) '(run open)))
4835 (tramp-send-command vec "echo are you awake" t t)
4836 (unless (and (memq (process-status p) '(run open))
4837 (tramp-wait-for-output p 10))
4838 ;; The error will be caught locally.
4839 (tramp-error vec 'file-error "Awake did fail")))
4841 (tramp-cleanup-connection vec t)
4844 ;; New connection must be opened.
4846 (unless (and p (processp p) (memq (process-status p) '(run open)))
4848 ;; If `non-essential' is non-nil, don't reopen a new connection.
4849 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4850 (throw 'non-essential 'non-essential))
4852 (with-tramp-progress-reporter
4854 (if (zerop (length (tramp-file-name-user vec)))
4855 (format "Opening connection for %s using %s"
4856 (tramp-file-name-host vec)
4857 (tramp-file-name-method vec))
4858 (format "Opening connection for %s@%s using %s"
4859 (tramp-file-name-user vec)
4860 (tramp-file-name-host vec)
4861 (tramp-file-name-method vec)))
4863 (catch 'uname-changed
4864 ;; Start new process.
4865 (when (and p (processp p))
4867 (setenv "TERM" tramp-terminal-type)
4868 (setenv "LC_ALL" "en_US.utf8")
4869 (if (stringp tramp-histfile-override)
4870 (setenv "HISTFILE" tramp-histfile-override)
4871 (if tramp-histfile-override
4874 (setenv "HISTFILESIZE" "0")
4875 (setenv "HISTSIZE" "0"))))
4876 (setenv "PROMPT_COMMAND")
4877 (setenv "PS1" tramp-initial-end-of-output)
4878 (let* ((target-alist (tramp-compute-multi-hops vec))
4879 ;; We will apply `tramp-ssh-controlmaster-options'
4880 ;; only for the first hop.
4881 (options (tramp-ssh-controlmaster-options vec))
4882 (process-connection-type tramp-process-connection-type)
4883 (process-adaptive-read-buffering nil)
4884 (coding-system-for-read nil)
4885 ;; This must be done in order to avoid our file
4887 (p (let ((default-directory
4888 (tramp-compat-temporary-file-directory)))
4891 (tramp-get-connection-name vec)
4892 (tramp-get-connection-buffer vec)
4893 (if tramp-encoding-command-interactive
4894 (list tramp-encoding-shell
4895 tramp-encoding-command-interactive)
4896 (list tramp-encoding-shell))))))
4898 ;; Set sentinel and query flag.
4899 (tramp-set-connection-property p "vector" vec)
4900 (set-process-sentinel p 'tramp-process-sentinel)
4901 (tramp-compat-set-process-query-on-exit-flag p nil)
4902 (setq tramp-current-connection
4903 (cons (butlast (append vec nil) 2) (current-time))
4904 tramp-current-host (system-name))
4907 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4909 ;; Check whether process is alive.
4910 (tramp-barf-if-no-shell-prompt
4912 "Couldn't find local shell prompt for %s" tramp-encoding-shell)
4914 ;; Now do all the connections as specified.
4916 (let* ((hop (car target-alist))
4917 (l-method (tramp-file-name-method hop))
4918 (l-user (tramp-file-name-user hop))
4919 (l-host (tramp-file-name-host hop))
4922 (tramp-get-method-parameter hop 'tramp-login-program))
4924 (tramp-get-method-parameter hop 'tramp-login-args))
4926 (tramp-get-method-parameter hop 'tramp-login-env))
4928 (tramp-get-method-parameter hop 'tramp-async-args))
4930 (tramp-get-method-parameter
4931 hop 'tramp-connection-timeout))
4933 (tramp-get-method-parameter hop 'tramp-gw-args))
4934 (gw (let ((tramp-verbose 0))
4935 (tramp-get-file-property hop "" "gateway" nil)))
4936 (g-method (and gw (tramp-file-name-method gw)))
4937 (g-user (and gw (tramp-file-name-user gw)))
4938 (g-host (and gw (tramp-file-name-real-host gw)))
4939 (command login-program)
4940 ;; We don't create the temporary file. In
4941 ;; fact, it is just a prefix for the
4942 ;; ControlPath option of ssh; the real
4943 ;; temporary file has another name, and it is
4944 ;; created and protected by ssh. It is also
4945 ;; removed by ssh when the connection is
4946 ;; closed. The temporary file name is cached
4947 ;; in the main connection process, therefore
4948 ;; we cannot use `tramp-get-connection-process'.
4950 (with-tramp-connection-property
4951 (get-process (tramp-buffer-name vec)) "temp-file"
4954 tramp-temp-name-prefix
4955 (tramp-compat-temporary-file-directory)))))
4958 ;; Add arguments for asynchronous processes.
4959 (when (and process-name async-args)
4960 (setq login-args (append async-args login-args)))
4962 ;; Add gateway arguments if necessary.
4964 (tramp-set-connection-property p "gateway" t)
4966 (setq login-args (append gw-args login-args))))
4968 ;; Check for port number. Until now, there's no
4969 ;; need for handling like method, user, host.
4970 (when (string-match tramp-host-with-port-regexp l-host)
4971 (setq l-port (match-string 2 l-host)
4972 l-host (match-string 1 l-host)))
4974 ;; Check, whether there is a restricted shell.
4975 (dolist (elt tramp-restricted-shell-hosts-alist)
4976 (when (string-match elt tramp-current-host)
4979 ;; Set variables for computing the prompt for
4980 ;; reading password. They can also be derived
4982 (setq tramp-current-method (or g-method l-method)
4983 tramp-current-user (or g-user l-user)
4984 tramp-current-host (or g-host l-host))
4986 ;; Add login environment.
4992 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4993 (unless (member "" x) (mapconcat 'identity x " ")))
5000 (tramp-shell-quote-argument (pop login-env))
5002 (setq command (concat "env " command)))
5004 ;; Replace `login-args' place holders.
5006 l-host (or l-host "")
5007 l-user (or l-user "")
5008 l-port (or l-port "")
5009 spec (format-spec-make ?t tmpfile)
5010 options (format-spec options spec)
5011 spec (format-spec-make
5012 ?h l-host ?u l-user ?p l-port ?c options)
5015 ;; We do not want to see the trailing local
5016 ;; prompt in `start-file-process'.
5017 (unless r-shell "exec ")
5021 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
5022 (unless (member "" x) (mapconcat 'identity x " ")))
5024 ;; Local shell could be a Windows COMSPEC. It
5025 ;; doesn't know the ";" syntax, but we must exit
5026 ;; always for `start-file-process'. It could
5027 ;; also be a restricted shell, which does not
5029 (when r-shell " && exit || exit")))
5031 ;; Send the command.
5032 (tramp-message vec 3 "Sending command `%s'" command)
5033 (tramp-send-command vec command t t)
5034 (tramp-process-actions
5035 p vec pos tramp-actions-before-shell
5036 (or connection-timeout tramp-connection-timeout))
5038 vec 3 "Found remote shell prompt on `%s'" l-host))
5041 target-alist (cdr target-alist)))
5043 ;; Make initial shell settings.
5044 (tramp-open-connection-setup-interactive-shell p vec)
5046 ;; Mark it as connected.
5047 (tramp-set-connection-property p "connected" t)))))
5049 ;; When the user did interrupt, we must cleanup.
5051 (tramp-cleanup-connection vec t)
5052 ;; Propagate the quit signal.
5053 (signal (car err) (cdr err))))))
5055 (defun tramp-send-command (vec command &optional neveropen nooutput)
5056 "Send the COMMAND to connection VEC.
5057 Erases temporary buffer before sending the command. If optional
5058 arg NEVEROPEN is non-nil, never try to open the connection. This
5059 is meant to be used from `tramp-maybe-open-connection' only. The
5060 function waits for output unless NOOUTPUT is set."
5061 (unless neveropen (tramp-maybe-open-connection vec))
5062 (let ((p (tramp-get-connection-process vec)))
5063 (when (tramp-get-connection-property p "remote-echo" nil)
5064 ;; We mark the command string that it can be erased in the output buffer.
5065 (tramp-set-connection-property p "check-remote-echo" t)
5066 ;; If we put `tramp-echo-mark' after a trailing newline (which
5067 ;; is assumed to be unquoted) `tramp-send-string' doesn't see
5068 ;; that newline and adds `tramp-rsh-end-of-line' right after
5069 ;; `tramp-echo-mark', so the remote shell sees two consecutive
5070 ;; trailing line endings and sends two prompts after executing
5071 ;; the command, which confuses `tramp-wait-for-output'.
5072 (when (and (not (string= command ""))
5073 (string-equal (substring command -1) "\n"))
5074 (setq command (substring command 0 -1)))
5075 ;; No need to restore a trailing newline here since `tramp-send-string'
5076 ;; makes sure that the string ends in `tramp-rsh-end-of-line', anyway.
5077 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
5078 ;; Send the command.
5079 (tramp-message vec 6 "%s" command)
5080 (tramp-send-string vec command)
5081 (unless nooutput (tramp-wait-for-output p))))
5083 (defun tramp-wait-for-output (proc &optional timeout)
5084 "Wait for output from remote command."
5085 (unless (buffer-live-p (process-buffer proc))
5086 (delete-process proc)
5087 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
5088 (with-current-buffer (process-buffer proc)
5089 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
5090 ;; be leading escape sequences, which must be ignored.
5091 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
5092 ;; Sometimes, the commands do not return a newline but a
5093 ;; null byte before the shell prompt, for example "git
5094 ;; ls-files -c -z ...".
5095 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
5096 (found (tramp-wait-for-regexp proc timeout regexp1)))
5098 (let (buffer-read-only)
5099 ;; A simple-minded busybox has sent " ^H" sequences.
5101 (goto-char (point-min))
5102 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
5104 (delete-region (point-min) (point)))
5105 ;; Delete the prompt.
5106 (goto-char (point-max))
5107 (re-search-backward regexp nil t)
5108 (delete-region (point) (point-max)))
5112 "[[Remote prompt `%s' not found in %d secs]]"
5113 tramp-end-of-output timeout)
5116 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
5117 ;; Return value is whether end-of-output sentinel was found.
5120 (defun tramp-send-command-and-check
5121 (vec command &optional subshell dont-suppress-err)
5122 "Run COMMAND and check its exit status.
5123 Sends `echo $?' along with the COMMAND for checking the exit status.
5124 If COMMAND is nil, just sends `echo $?'. Returns t if the exit
5125 status is 0, and nil otherwise.
5127 If the optional argument SUBSHELL is non-nil, the command is
5128 executed in a subshell, ie surrounded by parentheses. If
5129 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
5132 (concat (if subshell "( " "")
5134 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
5135 "echo tramp_exit_status $?"
5136 (if subshell " )" "")))
5137 (with-current-buffer (tramp-get-connection-buffer vec)
5138 (goto-char (point-max))
5139 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
5141 vec 'file-error "Couldn't find exit status of `%s'" command))
5142 (skip-chars-forward "^ ")
5144 (zerop (read (current-buffer)))
5145 (let (buffer-read-only)
5146 (delete-region (match-beginning 0) (point-max))))))
5148 (defun tramp-barf-unless-okay (vec command fmt &rest args)
5149 "Run COMMAND, check exit status, throw error if exit status not okay.
5150 Similar to `tramp-send-command-and-check' but accepts two more arguments
5151 FMT and ARGS which are passed to `error'."
5152 (or (tramp-send-command-and-check vec command)
5153 (apply 'tramp-error vec 'file-error fmt args)))
5155 (defun tramp-send-command-and-read (vec command &optional noerror marker)
5156 "Run COMMAND and return the output, which must be a Lisp expression.
5157 If MARKER is a regexp, read the output after that string.
5158 In case there is no valid Lisp expression and NOERROR is nil, it
5161 (tramp-send-command-and-check vec command)
5162 (tramp-barf-unless-okay
5163 vec command "`%s' returns with error" command))
5164 (with-current-buffer (tramp-get-connection-buffer vec)
5165 (goto-char (point-min))
5167 (when (stringp marker)
5169 (re-search-forward marker)
5170 (error (unless noerror
5173 "`%s' does not return the marker `%s': `%s'"
5174 command marker (buffer-string))))))
5175 ;; Read the expression.
5177 (prog1 (read (current-buffer))
5179 (when (re-search-forward "\\S-" (point-at-eol) t)
5181 (error (unless noerror
5184 "`%s' does not return a valid Lisp expression: `%s'"
5185 command (buffer-string))))))))
5187 (defun tramp-convert-file-attributes (vec attr)
5188 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
5189 Convert file mode bits to string and set virtual device number.
5192 ;; Remove color escape sequences from symlink.
5193 (when (stringp (car attr))
5194 (while (string-match tramp-color-escape-sequence-regexp (car attr))
5195 (setcar attr (replace-match "" nil nil (car attr)))))
5196 ;; Convert uid and gid. Use -1 as indication of unusable value.
5197 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
5198 (setcar (nthcdr 2 attr) -1))
5199 (when (and (floatp (nth 2 attr))
5200 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
5201 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
5202 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
5203 (setcar (nthcdr 3 attr) -1))
5204 (when (and (floatp (nth 3 attr))
5205 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
5206 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
5207 ;; Convert last access time.
5208 (unless (listp (nth 4 attr))
5209 (setcar (nthcdr 4 attr)
5210 (list (floor (nth 4 attr) 65536)
5211 (floor (mod (nth 4 attr) 65536)))))
5212 ;; Convert last modification time.
5213 (unless (listp (nth 5 attr))
5214 (setcar (nthcdr 5 attr)
5215 (list (floor (nth 5 attr) 65536)
5216 (floor (mod (nth 5 attr) 65536)))))
5217 ;; Convert last status change time.
5218 (unless (listp (nth 6 attr))
5219 (setcar (nthcdr 6 attr)
5220 (list (floor (nth 6 attr) 65536)
5221 (floor (mod (nth 6 attr) 65536)))))
5222 ;; Convert file size.
5223 (when (< (nth 7 attr) 0)
5224 (setcar (nthcdr 7 attr) -1))
5225 (when (and (floatp (nth 7 attr))
5226 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
5227 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
5228 ;; Convert file mode bits to string.
5229 (unless (stringp (nth 8 attr))
5230 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
5231 (when (stringp (car attr))
5232 (aset (nth 8 attr) 0 ?l)))
5233 ;; Convert directory indication bit.
5234 (when (string-match "^d" (nth 8 attr))
5236 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
5237 (when (consp (car attr))
5238 (if (and (stringp (caar attr))
5239 (string-match ".+ -> .\\(.+\\)." (caar attr)))
5240 (setcar attr (match-string 1 (caar attr)))
5242 ;; Set file's gid change bit.
5243 (setcar (nthcdr 9 attr)
5244 (if (numberp (nth 3 attr))
5245 (not (= (nth 3 attr)
5246 (tramp-get-remote-gid vec 'integer)))
5249 (tramp-get-remote-gid vec 'string)))))
5251 (unless (listp (nth 10 attr))
5252 (setcar (nthcdr 10 attr)
5254 (cons (floor (nth 10 attr) 65536)
5255 (floor (mod (nth 10 attr) 65536)))
5256 ;; Inodes can be incredible huge. We must hide this.
5257 (error (tramp-get-inode vec)))))
5258 ;; Set virtual device number.
5259 (setcar (nthcdr 11 attr)
5260 (tramp-get-device vec))
5263 (defun tramp-shell-case-fold (string)
5264 "Converts STRING to shell glob pattern which ignores case."
5267 (if (equal (downcase c) (upcase c))
5269 (format "[%c%c]" (downcase c) (upcase c))))
5273 (defun tramp-make-copy-program-file-name (vec)
5274 "Create a file name suitable for `scp', `pscp', or `nc' and workalikes."
5275 (let ((method (tramp-file-name-method vec))
5276 (user (tramp-file-name-user vec))
5277 (host (tramp-file-name-real-host vec))
5278 (localname (tramp-file-name-localname vec)))
5279 (when (string-match tramp-ipv6-regexp host)
5280 (setq host (format "[%s]" host)))
5281 (unless (string-match "ftp$" method)
5282 (setq localname (tramp-shell-quote-argument localname)))
5284 ((tramp-get-method-parameter vec 'tramp-remote-copy-program)
5286 ((not (zerop (length user)))
5287 (shell-quote-argument (format "%s@%s:%s" user host localname)))
5288 (t (shell-quote-argument (format "%s:%s" host localname))))))
5290 (defun tramp-method-out-of-band-p (vec size)
5291 "Return t if this is an out-of-band method, nil otherwise."
5293 ;; It shall be an out-of-band method.
5294 (tramp-get-method-parameter vec 'tramp-copy-program)
5295 ;; There must be a size, otherwise the file doesn't exist.
5297 ;; Either the file size is large enough, or (in rare cases) there
5298 ;; does not exist a remote encoding.
5299 (or (null tramp-copy-size-limit)
5300 (> size tramp-copy-size-limit)
5301 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
5303 ;; Variables local to connection.
5305 (defun tramp-get-remote-path (vec)
5306 (with-tramp-connection-property
5307 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5308 ;; cache the result for the session only. Otherwise, the result
5309 ;; is cached persistently.
5310 (if (memq 'tramp-own-remote-path tramp-remote-path)
5311 (tramp-get-connection-process vec)
5314 (let* ((remote-path (copy-tree tramp-remote-path))
5315 (elt1 (memq 'tramp-default-remote-path remote-path))
5316 (elt2 (memq 'tramp-own-remote-path remote-path))
5317 (default-remote-path
5320 (tramp-send-command-and-read
5321 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
5322 ;; Default if "getconf" is not available.
5326 "`getconf PATH' not successful, using default value \"%s\"."
5330 ;; The login shell could return more than just the $PATH
5331 ;; string. So we use `tramp-end-of-heredoc' as marker.
5334 (tramp-send-command-and-read
5337 "%s %s %s 'echo %s \\\"$PATH\\\"'"
5338 (tramp-get-method-parameter vec 'tramp-remote-shell)
5341 (tramp-get-method-parameter vec 'tramp-remote-shell-login)
5345 (tramp-get-method-parameter vec 'tramp-remote-shell-args)
5347 (tramp-shell-quote-argument tramp-end-of-heredoc))
5348 'noerror (regexp-quote tramp-end-of-heredoc))
5351 vec 2 "Could not retrieve `tramp-own-remote-path'")
5354 ;; Replace place holder `tramp-default-remote-path'.
5358 (tramp-compat-split-string (or default-remote-path "") ":")
5360 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
5362 ;; Replace place holder `tramp-own-remote-path'.
5366 (tramp-compat-split-string (or own-remote-path "") ":")
5368 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
5370 ;; Remove double entries.
5371 (setq elt1 remote-path)
5373 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
5375 (setq elt1 (cdr elt1)))
5377 ;; Remove non-existing directories.
5385 (tramp-make-tramp-file-name
5386 (tramp-file-name-method vec)
5387 (tramp-file-name-user vec)
5388 (tramp-file-name-host vec)
5393 (defun tramp-get-remote-locale (vec)
5394 (with-tramp-connection-property vec "locale"
5395 (tramp-send-command vec "locale -a")
5396 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
5398 (with-current-buffer (tramp-get-connection-buffer vec)
5400 (goto-char (point-min))
5401 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates)))
5403 (setq locale (car candidates)
5405 (setq candidates (cdr candidates)))))
5407 (format "LC_ALL=%s" (or locale "C")))))
5409 (defun tramp-get-ls-command (vec)
5410 (with-tramp-connection-property vec "ls"
5411 (tramp-message vec 5 "Finding a suitable `ls' command")
5414 (dolist (cmd '("ls" "gnuls" "gls"))
5415 (let ((dl (tramp-get-remote-path vec))
5417 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5418 ;; Check parameters. On busybox, "ls" output coloring is
5419 ;; enabled by default sometimes. So we try to disable it
5420 ;; when possible. $LS_COLORING is not supported there.
5421 ;; Some "ls" versions are sensible wrt the order of
5422 ;; arguments, they fail when "-al" is after the
5423 ;; "--color=never" argument (for example on FreeBSD).
5424 (when (tramp-send-command-and-check
5425 vec (format "%s -lnd /" result))
5426 (when (tramp-send-command-and-check
5428 "%s --color=never -al /dev/null" result))
5429 (setq result (concat result " --color=never")))
5430 (throw 'ls-found result))
5431 (setq dl (cdr dl))))))
5432 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
5434 (defun tramp-get-ls-command-with-dired (vec)
5436 (with-tramp-connection-property vec "ls-dired"
5437 (tramp-message vec 5 "Checking, whether `ls --dired' works")
5438 ;; Some "ls" versions are sensible wrt the order of arguments,
5439 ;; they fail when "-al" is after the "--dired" argument (for
5440 ;; example on FreeBSD).
5441 (tramp-send-command-and-check
5442 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
5444 (defun tramp-get-ls-command-with-quoting-style (vec)
5446 (with-tramp-connection-property vec "ls-quoting-style"
5447 (tramp-message vec 5 "Checking, whether `ls --quoting-style=shell' works")
5448 (tramp-send-command-and-check
5449 vec (format "%s --quoting-style=shell -al /dev/null"
5450 (tramp-get-ls-command vec))))))
5452 (defun tramp-get-ls-command-with-w-option (vec)
5454 (with-tramp-connection-property vec "ls-w-option"
5455 (tramp-message vec 5 "Checking, whether `ls -w' works")
5456 ;; Option "-w" is available on BSD systems. No argument is
5457 ;; given, because this could return wrong results in case "ls"
5458 ;; supports the "-w NUM" argument, as for busyboxes.
5459 (tramp-send-command-and-check
5460 vec (format "%s -alw" (tramp-get-ls-command vec))))))
5462 (defun tramp-get-test-command (vec)
5463 (with-tramp-connection-property vec "test"
5464 (tramp-message vec 5 "Finding a suitable `test' command")
5465 (if (tramp-send-command-and-check vec "test 0")
5467 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
5469 (defun tramp-get-test-nt-command (vec)
5470 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5471 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5472 ;; for otherwise the shell crashes.
5473 (with-tramp-connection-property vec "test-nt"
5477 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
5478 (with-current-buffer (tramp-get-buffer vec)
5479 (goto-char (point-min))
5480 (when (looking-at (regexp-quote tramp-end-of-output))
5481 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
5486 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5487 (tramp-get-test-command vec)))
5488 "tramp_test_nt %s %s"))))
5490 (defun tramp-get-file-exists-command (vec)
5491 (with-tramp-connection-property vec "file-exists"
5492 (tramp-message vec 5 "Finding command to check if file exists")
5493 (tramp-find-file-exists-command vec)))
5495 (defun tramp-get-remote-ln (vec)
5496 (with-tramp-connection-property vec "ln"
5497 (tramp-message vec 5 "Finding a suitable `ln' command")
5498 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
5500 (defun tramp-get-remote-perl (vec)
5501 (with-tramp-connection-property vec "perl"
5502 (tramp-message vec 5 "Finding a suitable `perl' command")
5504 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
5505 (tramp-find-executable
5506 vec "perl" (tramp-get-remote-path vec)))))
5507 ;; We must check also for some Perl modules.
5509 (with-tramp-connection-property vec "perl-file-spec"
5510 (tramp-send-command-and-check
5511 vec (format "%s -e 'use File::Spec;'" result)))
5512 (with-tramp-connection-property vec "perl-cwd-realpath"
5513 (tramp-send-command-and-check
5514 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
5517 (defun tramp-get-remote-stat (vec)
5518 (with-tramp-connection-property vec "stat"
5519 (tramp-message vec 5 "Finding a suitable `stat' command")
5520 (let ((result (tramp-find-executable
5521 vec "stat" (tramp-get-remote-path vec)))
5523 ;; Check whether stat(1) returns usable syntax. "%s" does not
5524 ;; work on older AIX systems.
5527 (tramp-send-command-and-read
5528 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
5529 (unless (and (listp tmp) (stringp (car tmp))
5530 (string-match "^./.$" (car tmp))
5531 (integerp (cadr tmp)))
5535 (defun tramp-get-remote-readlink (vec)
5536 (with-tramp-connection-property vec "readlink"
5537 (tramp-message vec 5 "Finding a suitable `readlink' command")
5538 (let ((result (tramp-find-executable
5539 vec "readlink" (tramp-get-remote-path vec))))
5541 (tramp-send-command-and-check
5542 vec (format "%s --canonicalize-missing /" result)))
5545 (defun tramp-get-remote-trash (vec)
5546 (with-tramp-connection-property vec "trash"
5547 (tramp-message vec 5 "Finding a suitable `trash' command")
5548 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
5550 (defun tramp-get-remote-touch (vec)
5551 (with-tramp-connection-property vec "touch"
5552 (tramp-message vec 5 "Finding a suitable `touch' command")
5553 (let ((result (tramp-find-executable
5554 vec "touch" (tramp-get-remote-path vec)))
5558 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
5559 ;; Busyboxes do support the "-t" option only when they have been
5560 ;; built with the DESKTOP config option. Let's check it.
5562 (tramp-set-connection-property
5564 (tramp-send-command-and-check
5569 (format-time-string "%Y%m%d%H%M.%S")
5570 (tramp-file-name-handler 'file-remote-p tmpfile 'localname))))
5571 (delete-file tmpfile))
5574 (defun tramp-get-remote-gvfs-monitor-dir (vec)
5575 (with-tramp-connection-property vec "gvfs-monitor-dir"
5576 (tramp-message vec 5 "Finding a suitable `gvfs-monitor-dir' command")
5577 (tramp-find-executable
5578 vec "gvfs-monitor-dir" (tramp-get-remote-path vec) t t)))
5580 (defun tramp-get-remote-inotifywait (vec)
5581 (with-tramp-connection-property vec "inotifywait"
5582 (tramp-message vec 5 "Finding a suitable `inotifywait' command")
5583 (tramp-find-executable vec "inotifywait" (tramp-get-remote-path vec) t t)))
5585 (defun tramp-get-remote-id (vec)
5586 (with-tramp-connection-property vec "id"
5587 (tramp-message vec 5 "Finding POSIX `id' command")
5589 (dolist (cmd '("id" "gid"))
5590 (let ((dl (tramp-get-remote-path vec))
5592 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5593 ;; Check POSIX parameter.
5594 (when (tramp-send-command-and-check vec (format "%s -u" result))
5595 (throw 'id-found result))
5596 (setq dl (cdr dl))))))))
5598 (defun tramp-get-remote-uid-with-id (vec id-format)
5599 (tramp-send-command-and-read
5601 (format "%s -u%s %s"
5602 (tramp-get-remote-id vec)
5603 (if (equal id-format 'integer) "" "n")
5604 (if (equal id-format 'integer)
5605 "" "| sed -e s/^/\\\"/ -e s/\\$/\\\"/"))))
5607 (defun tramp-get-remote-uid-with-perl (vec id-format)
5608 (tramp-send-command-and-read
5610 (format "%s -le '%s'"
5611 (tramp-get-remote-perl vec)
5612 (if (equal id-format 'integer)
5614 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5616 (defun tramp-get-remote-python (vec)
5617 (with-tramp-connection-property vec "python"
5618 (tramp-message vec 5 "Finding a suitable `python' command")
5619 (or (tramp-find-executable vec "python" (tramp-get-remote-path vec))
5620 (tramp-find-executable vec "python2" (tramp-get-remote-path vec))
5621 (tramp-find-executable vec "python3" (tramp-get-remote-path vec)))))
5623 (defun tramp-get-remote-uid-with-python (vec id-format)
5624 (tramp-send-command-and-read
5626 (format "%s -c \"%s\""
5627 (tramp-get-remote-python vec)
5628 (if (equal id-format 'integer)
5629 "import os; print (os.getuid())"
5630 "import os, pwd; print ('\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"')"))))
5632 (defun tramp-get-remote-uid (vec id-format)
5633 (with-tramp-connection-property vec (format "uid-%s" id-format)
5637 ((tramp-get-remote-id vec)
5638 (tramp-get-remote-uid-with-id vec id-format))
5639 ((tramp-get-remote-perl vec)
5640 (tramp-get-remote-uid-with-perl vec id-format))
5641 ((tramp-get-remote-python vec)
5642 (tramp-get-remote-uid-with-python vec id-format))))))
5643 ;; Ensure there is a valid result.
5645 ((and (equal id-format 'integer) (not (integerp res))) -1)
5646 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5649 (defun tramp-get-remote-gid-with-id (vec id-format)
5650 (tramp-send-command-and-read
5652 (format "%s -g%s %s"
5653 (tramp-get-remote-id vec)
5654 (if (equal id-format 'integer) "" "n")
5655 (if (equal id-format 'integer)
5656 "" "| sed -e s/^/\\\"/ -e s/\\$/\\\"/"))))
5658 (defun tramp-get-remote-gid-with-perl (vec id-format)
5659 (tramp-send-command-and-read
5661 (format "%s -le '%s'"
5662 (tramp-get-remote-perl vec)
5663 (if (equal id-format 'integer)
5664 "print ($)=~/(\\d+)/)"
5665 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5667 (defun tramp-get-remote-gid-with-python (vec id-format)
5668 (tramp-send-command-and-read
5670 (format "%s -c \"%s\""
5671 (tramp-get-remote-python vec)
5672 (if (equal id-format 'integer)
5673 "import os; print (os.getgid())"
5674 "import os, grp; print ('\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"')"))))
5676 (defun tramp-get-remote-gid (vec id-format)
5677 (with-tramp-connection-property vec (format "gid-%s" id-format)
5681 ((tramp-get-remote-id vec)
5682 (tramp-get-remote-gid-with-id vec id-format))
5683 ((tramp-get-remote-perl vec)
5684 (tramp-get-remote-gid-with-perl vec id-format))
5685 ((tramp-get-remote-python vec)
5686 (tramp-get-remote-gid-with-python vec id-format))))))
5687 ;; Ensure there is a valid result.
5689 ((and (equal id-format 'integer) (not (integerp res))) -1)
5690 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5693 ;; Some predefined connection properties.
5694 (defun tramp-get-inline-compress (vec prop size)
5695 "Return the compress command related to PROP.
5696 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5697 the length of the file to be compressed.
5699 If no corresponding command is found, nil is returned."
5700 (when (and (integerp tramp-inline-compress-start-size)
5701 (> size tramp-inline-compress-start-size))
5702 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5703 (tramp-find-inline-compress vec)
5704 (tramp-get-connection-property
5705 (tramp-get-connection-process vec) prop nil))))
5707 (defun tramp-get-inline-coding (vec prop size)
5708 "Return the coding command related to PROP.
5709 PROP is either `remote-encoding', `remote-decoding',
5710 `local-encoding' or `local-decoding'.
5712 SIZE is the length of the file to be coded. Depending on SIZE,
5713 compression might be applied.
5715 If no corresponding command is found, nil is returned.
5716 Otherwise, either a string is returned which contains a `%s' mark
5717 to be used for the respective input or output file; or a Lisp
5718 function cell is returned to be applied on a buffer."
5719 ;; We must catch the errors, because we want to return nil, when
5720 ;; no inline coding is found.
5723 (with-tramp-connection-property
5724 (tramp-get-connection-process vec) prop
5725 (tramp-find-inline-encoding vec)
5726 (tramp-get-connection-property
5727 (tramp-get-connection-process vec) prop nil)))
5728 (prop1 (if (string-match "encoding" prop)
5729 "inline-compress" "inline-decompress"))
5731 ;; The connection property might have been cached. So we must
5732 ;; send the script to the remote side - maybe.
5733 (when (and coding (symbolp coding) (string-match "remote" prop))
5734 (let ((name (symbol-name coding)))
5735 (while (string-match (regexp-quote "-") name)
5736 (setq name (replace-match "_" nil t name)))
5737 (tramp-maybe-send-script vec (symbol-value coding) name)
5738 (setq coding name)))
5740 ;; Check for the `compress' command.
5741 (setq compress (tramp-get-inline-compress vec prop1 size))
5742 ;; Return the value.
5744 ((and compress (symbolp coding))
5745 (if (string-match "decompress" prop1)
5748 (let ((coding-system-for-write 'binary)
5749 (coding-system-for-read 'binary)
5751 (tramp-compat-temporary-file-directory)))
5753 'tramp-call-process-region ,vec (point-min) (point-max)
5754 (car (split-string ,compress)) t t nil
5755 (cdr (split-string ,compress)))))
5757 (let ((coding-system-for-write 'binary)
5758 (coding-system-for-read 'binary)
5760 (tramp-compat-temporary-file-directory)))
5762 'tramp-call-process-region ,vec beg end
5763 (car (split-string ,compress)) t t nil
5764 (cdr (split-string ,compress))))
5765 (,coding (point-min) (point-max)))))
5768 ((and compress (string-match "decoding" prop))
5770 ;; Windows shells need the program file name after
5771 ;; the pipe symbol be quoted if they use forward
5772 ;; slashes as directory separators.
5774 ((and (string-match "local" prop)
5775 (memq system-type '(windows-nt)))
5777 ((string-match "local" prop) "(%s | %s)")
5778 (t "(%s | %s >%%s)"))
5782 ;; Windows shells need the program file name after
5783 ;; the pipe symbol be quoted if they use forward
5784 ;; slashes as directory separators.
5785 (if (and (string-match "local" prop)
5786 (memq system-type '(windows-nt)))
5787 "(%s <%%s | \"%s\")"
5790 ((string-match "decoding" prop)
5792 ((string-match "local" prop) (format "%s" coding))
5793 (t (format "%s >%%s" coding))))
5795 (format "%s <%%s" coding)))))))
5797 (add-hook 'tramp-unload-hook
5799 (unload-feature 'tramp-sh 'force)))
5805 ;; * Don't use globbing for directories with many files, as this is
5806 ;; likely to produce long command lines, and some shells choke on
5807 ;; long command lines.
5808 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5809 ;; then look if it's the right version (with `perl -v').
5810 ;; * When editing a remote CVS controlled file as a different user, VC
5811 ;; gets confused about the file locking status. Try to find out why
5812 ;; the workaround doesn't work.
5813 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5814 ;; until the last but one hop via `start-file-process'. Apply it
5815 ;; also for ftp and smb.
5816 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5817 ;; some shell with root privileges, it would be nice if I could
5819 ;; trampclient filename.c
5820 ;; as an editor, and the _current_ shell would connect to an Emacs
5821 ;; server and would be used in an existing non-privileged Emacs
5822 ;; session for doing the editing in question.
5823 ;; That way, I need not tell Emacs my password again and be afraid
5824 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5825 ;; once display a just typed password in the context of a keyboard
5826 ;; sequence prompt for a question immediately following in a shell
5827 ;; script run within Emacs -- nasty).
5828 ;; And if I have some ssh session running to a different computer,
5829 ;; having the possibility of passing a local file there to a local
5830 ;; Emacs session (in case I can arrange for a connection back) would
5832 ;; Likely the corresponding Tramp server should not allow the
5833 ;; equivalent of the emacsclient -eval option in order to make this
5834 ;; reasonably unproblematic. And maybe trampclient should have some
5835 ;; way of passing credentials, like by using an SSL socket or
5836 ;; something. (David Kastrup)
5837 ;; * Reconnect directly to a compliant shell without first going
5838 ;; through the user's default shell. (Pete Forman)
5839 ;; * How can I interrupt the remote process with a signal
5840 ;; (interrupt-process seems not to work)? (Markus Triska)
5841 ;; * Avoid the local shell entirely for starting remote processes. If
5842 ;; so, I think even a signal, when delivered directly to the local
5843 ;; SSH instance, would correctly be propagated to the remote process
5844 ;; automatically; possibly SSH would have to be started with
5845 ;; "-t". (Markus Triska)
5846 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5847 ;; isn't on the remote host. (Mark A. Hershberger)
5848 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5849 ;; * Optimize out-of-band copying when both methods are scp-like (not
5851 ;; * Keep a second connection open for out-of-band methods like scp or
5854 ;;; tramp-sh.el ends here