1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2017 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 vc-handled-backends
)
36 (defvar vc-bzr-program
)
37 (defvar vc-git-program
)
38 (defvar vc-hg-program
)
41 (defcustom tramp-inline-compress-start-size
4096
42 "The minimum size of compressing where inline transfer.
43 When inline transfer, compress transferred data of file
44 whose size is this value or above (up to `tramp-copy-size-limit').
45 If it is nil, no compression at all will be applied."
47 :type
'(choice (const nil
) integer
)
51 (defcustom tramp-copy-size-limit
10240
52 "The maximum file size where inline copying is preferred over an \
54 If it is nil, out-of-the-band copy will be used without a check."
56 :type
'(choice (const nil
) integer
)
60 (defcustom tramp-terminal-type
"dumb"
61 "Value of TERM environment variable for logging in to remote host.
62 Because Tramp wants to parse the output of the remote shell, it is easily
63 confused by ANSI color escape sequences and suchlike. Often, shell init
64 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 unsets any setting of HISTFILE, and sets both
77 HISTFILESIZE and HISTSIZE to 0. If you set this variable to nil,
78 however, the *override* is disabled, so the history will go to
79 the default storage location, e.g. \"$HOME/.sh_history\"."
82 :type
'(choice (const :tag
"Do not override HISTFILE" nil
)
83 (const :tag
"Unset HISTFILE" t
)
84 (string :tag
"Redirect to a file"))
88 (defconst tramp-display-escape-sequence-regexp
"\e[[;0-9]+m"
89 "Terminal control escape sequences for display attributes.")
92 (defconst tramp-device-escape-sequence-regexp
"\e[[0-9]+n"
93 "Terminal control escape sequences for device status.")
95 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
96 ;; root users. It uses the `$' character for other users. In order
97 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
99 (defvar tramp-end-of-output
102 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
103 "String used to recognize end of output.
104 The `$' character at the end is quoted; the string cannot be
105 detected as prompt when being sent on echoing hosts, therefore.")
108 (defconst tramp-initial-end-of-output
"#$ "
109 "Prompt when establishing a connection.")
111 (defconst tramp-end-of-heredoc
(md5 tramp-end-of-output
)
112 "String used to recognize end of heredoc strings.")
115 (defcustom tramp-use-ssh-controlmaster-options t
116 "Whether to use `tramp-ssh-controlmaster-options'."
122 (defvar tramp-ssh-controlmaster-options nil
123 "Which ssh Control* arguments to use.
125 If it is a string, it should have the form
126 \"-o ControlMaster=auto -o ControlPath=\\='tramp.%%r@%%h:%%p\\='
127 -o ControlPersist=no\". Percent characters in the ControlPath
128 spec must be doubled, because the string is used as format string.
130 Otherwise, it will be auto-detected by Tramp, if
131 `tramp-use-ssh-controlmaster-options' is non-nil. The value
132 depends on the installed local ssh version.
134 The string is used in `tramp-methods'.")
136 ;; Initialize `tramp-methods' with the supported methods.
138 (add-to-list 'tramp-methods
140 (tramp-login-program "rsh")
141 (tramp-login-args (("%h") ("-l" "%u")))
142 (tramp-remote-shell "/bin/sh")
143 (tramp-remote-shell-login ("-l"))
144 (tramp-remote-shell-args ("-c"))
145 (tramp-copy-program "rcp")
146 (tramp-copy-args (("-p" "%k") ("-r")))
147 (tramp-copy-keep-date t
)
148 (tramp-copy-recursive t
)))
150 (add-to-list 'tramp-methods
152 (tramp-login-program "remsh")
153 (tramp-login-args (("%h") ("-l" "%u")))
154 (tramp-remote-shell "/bin/sh")
155 (tramp-remote-shell-login ("-l"))
156 (tramp-remote-shell-args ("-c"))
157 (tramp-copy-program "rcp")
158 (tramp-copy-args (("-p" "%k")))
159 (tramp-copy-keep-date t
)))
161 (add-to-list 'tramp-methods
163 (tramp-login-program "ssh")
164 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
165 ("-e" "none") ("%h")))
166 (tramp-async-args (("-q")))
167 (tramp-remote-shell "/bin/sh")
168 (tramp-remote-shell-login ("-l"))
169 (tramp-remote-shell-args ("-c"))
170 (tramp-copy-program "scp")
171 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
172 (tramp-copy-keep-date t
)
173 (tramp-copy-recursive t
)))
175 (add-to-list 'tramp-methods
177 (tramp-login-program "ssh")
178 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
179 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
180 (tramp-async-args (("-q")))
181 (tramp-remote-shell "/bin/sh")
182 (tramp-remote-shell-login ("-l"))
183 (tramp-remote-shell-args ("-c"))
184 (tramp-copy-program "scp")
185 (tramp-copy-args (("-P" "%p") ("-p" "%k")
186 ("-q") ("-r") ("%c")))
187 (tramp-copy-keep-date t
)
188 (tramp-copy-recursive t
)))
190 (add-to-list 'tramp-methods
192 (tramp-login-program "ssh")
193 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
194 ("-e" "none") ("%h")))
195 (tramp-async-args (("-q")))
196 (tramp-remote-shell "/bin/sh")
197 (tramp-remote-shell-login ("-l"))
198 (tramp-remote-shell-args ("-c"))
199 (tramp-copy-program "rsync")
200 (tramp-copy-args (("-t" "%k") ("-p") ("-r") ("-s")))
201 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
202 (tramp-copy-keep-date t
)
203 (tramp-copy-keep-tmpfile t
)
204 (tramp-copy-recursive t
)))
206 (add-to-list 'tramp-methods
208 (tramp-login-program "rsh")
209 (tramp-login-args (("%h") ("-l" "%u")))
210 (tramp-remote-shell "/bin/sh")
211 (tramp-remote-shell-login ("-l"))
212 (tramp-remote-shell-args ("-c"))))
214 (add-to-list 'tramp-methods
216 (tramp-login-program "remsh")
217 (tramp-login-args (("%h") ("-l" "%u")))
218 (tramp-remote-shell "/bin/sh")
219 (tramp-remote-shell-login ("-l"))
220 (tramp-remote-shell-args ("-c"))))
222 (add-to-list 'tramp-methods
224 (tramp-login-program "ssh")
225 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
226 ("-e" "none") ("%h")))
227 (tramp-async-args (("-q")))
228 (tramp-remote-shell "/bin/sh")
229 (tramp-remote-shell-login ("-l"))
230 (tramp-remote-shell-args ("-c"))))
232 (add-to-list 'tramp-methods
234 (tramp-login-program "ssh")
235 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
236 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
237 (tramp-async-args (("-q")))
238 (tramp-remote-shell "/bin/sh")
239 (tramp-remote-shell-login ("-l"))
240 (tramp-remote-shell-args ("-c"))))
242 (add-to-list 'tramp-methods
244 (tramp-login-program "telnet")
245 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
246 (tramp-remote-shell "/bin/sh")
247 (tramp-remote-shell-login ("-l"))
248 (tramp-remote-shell-args ("-c"))))
250 (add-to-list 'tramp-methods
252 (tramp-login-program "telnet")
253 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
254 (tramp-remote-shell "/bin/sh")
255 (tramp-remote-shell-login ("-l"))
256 (tramp-remote-shell-args ("-c"))
257 (tramp-copy-program "nc")
258 ;; We use "-v" for better error tracking.
259 (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r")))
260 (tramp-remote-copy-program "nc")
261 ;; We use "-p" as required for newer busyboxes. For older
262 ;; busybox/nc versions, the value must be (("-l") ("%r")). This
263 ;; can be achieved by tweaking `tramp-connection-properties'.
264 (tramp-remote-copy-args (("-l") ("-p" "%r") ("2>/dev/null")))))
266 (add-to-list 'tramp-methods
268 (tramp-login-program "su")
269 (tramp-login-args (("-") ("%u")))
270 (tramp-remote-shell "/bin/sh")
271 (tramp-remote-shell-login ("-l"))
272 (tramp-remote-shell-args ("-c"))
273 (tramp-connection-timeout 10)))
278 (tramp-login-program "sg")
279 (tramp-login-args (("-") ("%u")))
280 (tramp-remote-shell "/bin/sh")
281 (tramp-remote-shell-args ("-c"))
282 (tramp-connection-timeout 10)))
284 (add-to-list 'tramp-methods
286 (tramp-login-program "sudo")
287 ;; The password template must be masked. Otherwise, it could be
288 ;; interpreted as password prompt if the remote host echoes the command.
289 (tramp-login-args (("-u" "%u") ("-s") ("-H")
290 ("-p" "P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")))
291 ;; Local $SHELL could be a nasty one, like zsh or fish. Let's override it.
292 (tramp-login-env (("SHELL") ("/bin/sh")))
293 (tramp-remote-shell "/bin/sh")
294 (tramp-remote-shell-login ("-l"))
295 (tramp-remote-shell-args ("-c"))
296 (tramp-connection-timeout 10)))
298 (add-to-list 'tramp-methods
300 (tramp-login-program "doas")
301 (tramp-login-args (("-u" "%u") ("-s")))
302 (tramp-remote-shell "/bin/sh")
303 (tramp-remote-shell-args ("-c"))
304 (tramp-connection-timeout 10)))
306 (add-to-list 'tramp-methods
308 (tramp-login-program "ksu")
309 (tramp-login-args (("%u") ("-q")))
310 (tramp-remote-shell "/bin/sh")
311 (tramp-remote-shell-login ("-l"))
312 (tramp-remote-shell-args ("-c"))
313 (tramp-connection-timeout 10)))
315 (add-to-list 'tramp-methods
317 (tramp-login-program "krlogin")
318 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
319 (tramp-remote-shell "/bin/sh")
320 (tramp-remote-shell-login ("-l"))
321 (tramp-remote-shell-args ("-c"))))
323 (add-to-list 'tramp-methods
325 (tramp-login-program "plink")
326 ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
327 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
330 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
332 tramp-initial-end-of-output
))
334 (tramp-remote-shell "/bin/sh")
335 (tramp-remote-shell-login ("-l"))
336 (tramp-remote-shell-args ("-c"))))
338 (add-to-list 'tramp-methods
340 (tramp-login-program "plink")
341 (tramp-login-args (("-load") ("%h") ("-t") ("\"")
343 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
345 tramp-initial-end-of-output
))
347 (tramp-remote-shell "/bin/sh")
348 (tramp-remote-shell-login ("-l"))
349 (tramp-remote-shell-args ("-c"))))
351 (add-to-list 'tramp-methods
353 (tramp-login-program "plink")
354 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
357 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
359 tramp-initial-end-of-output
))
361 (tramp-remote-shell "/bin/sh")
362 (tramp-remote-shell-login ("-l"))
363 (tramp-remote-shell-args ("-c"))
364 (tramp-copy-program "pscp")
365 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
367 (tramp-copy-keep-date t
)
368 (tramp-copy-recursive t
)))
370 (add-to-list 'tramp-methods
372 (tramp-login-program "plink")
373 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
376 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
378 tramp-initial-end-of-output
))
380 (tramp-remote-shell "/bin/sh")
381 (tramp-remote-shell-login ("-l"))
382 (tramp-remote-shell-args ("-c"))
383 (tramp-copy-program "pscp")
384 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
386 (tramp-copy-keep-date t
)))
388 (add-to-list 'tramp-methods
390 (tramp-login-program "fsh")
391 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
392 (tramp-remote-shell "/bin/sh")
393 (tramp-remote-shell-login ("-l"))
394 (tramp-remote-shell-args ("-i") ("-c"))
395 (tramp-copy-program "fcp")
396 (tramp-copy-args (("-p" "%k")))
397 (tramp-copy-keep-date t
)))
400 (add-to-list 'tramp-default-method-alist
401 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
404 (add-to-list 'tramp-default-user-alist
405 `(,(concat "\\`" (regexp-opt '("su" "sudo" "doas" "ksu")) "\\'")
407 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
408 ;; Do not add "plink" based methods, they ask interactively for the user.
410 (add-to-list 'tramp-default-user-alist
414 '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
416 nil
,(user-login-name)))
419 (defconst tramp-completion-function-alist-rsh
420 '((tramp-parse-rhosts "/etc/hosts.equiv")
421 (tramp-parse-rhosts "~/.rhosts"))
422 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
425 (defconst tramp-completion-function-alist-ssh
426 '((tramp-parse-rhosts "/etc/hosts.equiv")
427 (tramp-parse-rhosts "/etc/shosts.equiv")
428 (tramp-parse-shosts "/etc/ssh_known_hosts")
429 (tramp-parse-sconfig "/etc/ssh_config")
430 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
431 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
432 (tramp-parse-rhosts "~/.rhosts")
433 (tramp-parse-rhosts "~/.shosts")
434 (tramp-parse-shosts "~/.ssh/known_hosts")
435 (tramp-parse-sconfig "~/.ssh/config")
436 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
437 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
438 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
441 (defconst tramp-completion-function-alist-telnet
442 '((tramp-parse-hosts "/etc/hosts"))
443 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
446 (defconst tramp-completion-function-alist-su
447 '((tramp-parse-passwd "/etc/passwd"))
448 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
451 (defconst tramp-completion-function-alist-sg
452 '((tramp-parse-etc-group "/etc/group"))
453 "Default list of (FUNCTION FILE) pairs to be examined for sg methods.")
456 (defconst tramp-completion-function-alist-putty
458 ,(if (memq system-type
'(windows-nt))
459 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
460 "~/.putty/sessions")))
461 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
464 (eval-after-load 'tramp
466 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
467 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
468 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
469 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
470 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
471 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
472 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
473 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
474 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
475 (tramp-set-completion-function
476 "telnet" tramp-completion-function-alist-telnet
)
477 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet
)
478 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
479 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
480 (tramp-set-completion-function "doas" tramp-completion-function-alist-su
)
481 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su
)
482 (tramp-set-completion-function "sg" tramp-completion-function-alist-sg
)
483 (tramp-set-completion-function
484 "krlogin" tramp-completion-function-alist-rsh
)
485 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
486 (tramp-set-completion-function
487 "plinkx" tramp-completion-function-alist-putty
)
488 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
489 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh
)
490 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)))
492 ;; "getconf PATH" yields:
493 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
494 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
495 ;; GNU/Linux (Debian, Suse, RHEL): /bin:/usr/bin
496 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
497 ;; Darwin: /usr/bin:/bin:/usr/sbin:/sbin
501 (defcustom tramp-remote-path
502 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
503 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
504 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
505 "/opt/bin" "/opt/sbin" "/opt/local/bin")
506 "List of directories to search for executables on remote host.
507 For every remote host, this variable will be set buffer local,
508 keeping the list of existing directories on that host.
510 You can use `~' in this list, but when searching for a shell which groks
511 tilde expansion, all directory names starting with `~' will be ignored.
513 `Default Directories' represent the list of directories given by
514 the command \"getconf PATH\". It is recommended to use this
515 entry on head of this list, because these are the default
516 directories for POSIX compatible commands. On remote hosts which
517 do not offer the getconf command (like cygwin), the value
518 \"/bin:/usr/bin\" is used instead. This entry is represented in
519 the list by the special value `tramp-default-remote-path'.
521 `Private Directories' are the settings of the $PATH environment,
522 as given in your `~/.profile'. This entry is represented in
523 the list by the special value `tramp-own-remote-path'."
525 :type
'(repeat (choice
526 (const :tag
"Default Directories" tramp-default-remote-path
)
527 (const :tag
"Private Directories" tramp-own-remote-path
)
528 (string :tag
"Directory")))
532 (defcustom tramp-remote-process-environment
533 `("ENV=''" "TMOUT=0" "LC_CTYPE=''"
534 ,(format "TERM=%s" tramp-terminal-type
)
535 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
536 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=cat"
537 "autocorrect=" "correct=")
538 "List of environment variables to be set on the remote host.
540 Each element should be a string of the form ENVVARNAME=VALUE. An
541 entry ENVVARNAME= disables the corresponding environment variable,
542 which might have been set in the init files like ~/.profile.
544 Special handling is applied to the PATH environment, which should
545 not be set here. Instead, it should be set via `tramp-remote-path'."
548 :type
'(repeat string
)
552 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
553 "Alist specifying extra arguments to pass to the remote shell.
554 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
555 matching the shell file name and ARGS is a string specifying the
558 This variable is only used when Tramp needs to start up another shell
559 for tilde expansion. The extra arguments should typically prevent the
560 shell from reading its init file."
562 ;; This might be the wrong way to test whether the widget type
563 ;; `alist' is available. Who knows the right way to test it?
564 :type
(if (get 'alist
'widget-type
)
565 '(alist :key-type string
:value-type string
)
566 '(repeat (cons string string
)))
569 (defconst tramp-actions-before-shell
570 '((tramp-login-prompt-regexp tramp-action-login
)
571 (tramp-password-prompt-regexp tramp-action-password
)
572 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
573 (shell-prompt-pattern tramp-action-succeed
)
574 (tramp-shell-prompt-pattern tramp-action-succeed
)
575 (tramp-yesno-prompt-regexp tramp-action-yesno
)
576 (tramp-yn-prompt-regexp tramp-action-yn
)
577 (tramp-terminal-prompt-regexp tramp-action-terminal
)
578 (tramp-process-alive-regexp tramp-action-process-alive
))
579 "List of pattern/action pairs.
580 Whenever a pattern matches, the corresponding action is performed.
581 Each item looks like (PATTERN ACTION).
583 The PATTERN should be a symbol, a variable. The value of this
584 variable gives the regular expression to search for. Note that the
585 regexp must match at the end of the buffer, \"\\'\" is implicitly
588 The ACTION should also be a symbol, but a function. When the
589 corresponding PATTERN matches, the ACTION function is called.")
591 (defconst tramp-actions-copy-out-of-band
592 '((tramp-password-prompt-regexp tramp-action-password
)
593 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
594 (tramp-copy-failed-regexp tramp-action-permission-denied
)
595 (tramp-process-alive-regexp tramp-action-out-of-band
))
596 "List of pattern/action pairs.
597 This list is used for copying/renaming with out-of-band methods.
599 See `tramp-actions-before-shell' for more info.")
601 (defconst tramp-uudecode
602 "(echo begin 600 %t; tail -n +2) | uudecode
605 "Shell function to implement `uudecode' to standard output.
606 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
607 for this or `uudecode -p', but some systems don't, and for them
608 we have this shell function.")
610 (defconst tramp-perl-file-truename
613 use Cwd \"realpath\";
617 return realpath($file) if -e $file;
621 my ($volume, @dirs) = @_;
622 my $real = myrealpath(File::Spec->catpath(
623 $volume, File::Spec->catdir(@dirs), \"\"));
625 my ($vol, $dir) = File::Spec->splitpath($real, 1);
626 return ($vol, File::Spec->splitdir($dir));
629 my $last = pop(@dirs);
630 ($volume, @dirs) = recursive($volume, @dirs);
632 return ($volume, @dirs);
636 $result = myrealpath($ARGV[0]);
638 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
639 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
641 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
644 $result =~ s/\"/\\\\\"/g;
645 print \"\\\"$result\\\"\\n\";
646 ' \"$1\" 2>/dev/null"
647 "Perl script to produce output suitable for use with `file-truename'
648 on the remote file system.
649 Escape sequence %s is replaced with name of Perl binary.
650 This string is passed to `format', so percent characters need to be doubled.")
652 (defconst tramp-perl-file-name-all-completions
654 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
655 @files = readdir(d); closedir(d);
656 foreach $f (@files) {
657 if (-d \"$ARGV[0]/$f\") {
665 ' \"$1\" 2>/dev/null"
666 "Perl script to produce output suitable for use with
667 `file-name-all-completions' on the remote file system. Escape
668 sequence %s is replaced with name of Perl binary. This string is
669 passed to `format', so percent characters need to be doubled.")
671 ;; Perl script to implement `file-attributes' in a Lisp `read'able
672 ;; output. If you are hacking on this, note that you get *no* output
673 ;; unless this spits out a complete line, including the '\n' at the
675 ;; The device number is returned as "-1", because there will be a virtual
676 ;; device number set in `tramp-sh-handle-file-attributes'.
677 (defconst tramp-perl-file-attributes
679 @stat = lstat($ARGV[0]);
684 if (($stat[2] & 0170000) == 0120000)
686 $type = readlink($ARGV[0]);
687 $type =~ s/\"/\\\\\"/g;
688 $type = \"\\\"$type\\\"\";
690 elsif (($stat[2] & 0170000) == 040000)
698 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
699 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
701 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
706 $stat[8] >> 16 & 0xffff,
708 $stat[9] >> 16 & 0xffff,
710 $stat[10] >> 16 & 0xffff,
714 $stat[1] >> 16 & 0xffff,
716 );' \"$1\" \"$2\" 2>/dev/null"
717 "Perl script to produce output suitable for use with `file-attributes'
718 on the remote file system.
719 Escape sequence %s is replaced with name of Perl binary.
720 This string is passed to `format', so percent characters need to be doubled.")
722 (defconst tramp-perl-directory-files-and-attributes
724 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
725 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
726 @list = readdir(DIR);
730 for($i = 0; $i < $n; $i++)
732 $filename = $list[$i];
733 @stat = lstat($filename);
734 if (($stat[2] & 0170000) == 0120000)
736 $type = readlink($filename);
737 $type =~ s/\"/\\\\\"/g;
738 $type = \"\\\"$type\\\"\";
740 elsif (($stat[2] & 0170000) == 040000)
748 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
749 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
750 $filename =~ s/\"/\\\\\"/g;
752 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
758 $stat[8] >> 16 & 0xffff,
760 $stat[9] >> 16 & 0xffff,
762 $stat[10] >> 16 & 0xffff,
766 $stat[1] >> 16 & 0xffff,
768 $stat[0] >> 16 & 0xffff,
771 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
772 "Perl script implementing `directory-files-attributes' as Lisp `read'able
774 Escape sequence %s is replaced with name of Perl binary.
775 This string is passed to `format', so percent characters need to be doubled.")
777 ;; These two use base64 encoding.
778 (defconst tramp-perl-encode-with-module
779 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
780 "Perl program to use for encoding a file.
781 Escape sequence %s is replaced with name of Perl binary.
782 This string is passed to `format', so percent characters need to be doubled.
783 This implementation requires the MIME::Base64 Perl module to be installed
784 on the remote host.")
786 (defconst tramp-perl-decode-with-module
787 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
788 "Perl program to use for decoding a file.
789 Escape sequence %s is replaced with name of Perl binary.
790 This string is passed to `format', so percent characters need to be doubled.
791 This implementation requires the MIME::Base64 Perl module to be installed
792 on the remote host.")
794 (defconst tramp-perl-encode
796 # This script contributed by Juanma Barranquero <lektu@terra.es>.
797 # Copyright (C) 2002-2017 Free Software Foundation, Inc.
802 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
803 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
807 # We read in chunks of 54 bytes, to generate output lines
808 # of 72 chars (plus end of line)
809 while (read STDIN, $data, 54) {
812 # Only for the last chunk, and only if did not fill the last three-byte packet
814 my $mod = length($data) %% 3;
815 $pad = q(=) x (3 - $mod) if $mod;
818 # Not the fastest method, but it is simple: unpack to binary string, split
819 # by groups of 6 bits and convert back from binary to byte; then map into
820 # the translation table
824 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
828 "Perl program to use for encoding a file.
829 Escape sequence %s is replaced with name of Perl binary.
830 This string is passed to `format', so percent characters need to be doubled.")
832 (defconst tramp-perl-decode
834 # This script contributed by Juanma Barranquero <lektu@terra.es>.
835 # Copyright (C) 2002-2017 Free Software Foundation, Inc.
840 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
841 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
844 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
848 # We are going to accumulate into $pending to accept any line length
849 # (we do not check they are <= 76 chars as the RFC says)
852 while (my $data = <STDIN>) {
855 # If we find one or two =, we have reached the end and
856 # any following data is to be discarded
857 my $finished = $data =~ s/(==?).*/$1/;
860 my $len = length($pending);
861 my $chunk = substr($pending, 0, $len & ~3);
862 $pending = substr($pending, $len & ~3 + 1);
864 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
865 # split in 8-bit chunks and convert back to char.
868 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
872 "Perl program to use for decoding a file.
873 Escape sequence %s is replaced with name of Perl binary.
874 This string is passed to `format', so percent characters need to be doubled.")
876 (defconst tramp-perl-pack
877 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
878 "Perl program to use for encoding a file.
879 Escape sequence %s is replaced with name of Perl binary.")
881 (defconst tramp-perl-unpack
882 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
883 "Perl program to use for decoding a file.
884 Escape sequence %s is replaced with name of Perl binary.")
886 (defconst tramp-awk-encode
887 "od -v -t x1 -A n | busybox awk '\\
889 b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
890 b16 = \"0123456789abcdef\"
893 for (c=1; c<=length($0); c++) {
894 d=index(b16, substr($0,c,1))
896 for (b=1; b<=4; b++) {
897 o=o*2+int(d/8); d=(d*2)%%16
899 printf substr(b64,o+1,1)
900 if (++rc>75) { printf \"\\n\"; rc=0 }
909 tail=(obc==2) ? \"==\\n\" : \"=\\n\"
910 while (obc++<6) { o=o*2 }
911 printf \"%%c\", substr(b64,o+1,1)
917 "Awk program to use for encoding a file.
918 This string is passed to `format', so percent characters need to be doubled.")
920 (defconst tramp-awk-decode
923 b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
926 for (i=1; i<=length($0); i++) {
927 c=index(b64, substr($0,i,1))
930 o=o*2+int(c/32); c=(c*2)%%64
935 system(\"dd if=/dev/zero bs=1 count=1 2>/dev/null\")
943 "Awk program to use for decoding a file.
944 This string is passed to `format', so percent characters need to be doubled.")
946 (defconst tramp-awk-coding-test
947 "test -c /dev/zero && \
948 od -v -t x1 -A n </dev/null && \
949 busybox awk '{}' </dev/null"
950 "Test command for checking `tramp-awk-encode' and `tramp-awk-decode'.")
952 (defconst tramp-stat-marker
"/////"
953 "Marker in stat commands for file attributes.")
955 (defconst tramp-stat-quoted-marker
"\\/\\/\\/\\/\\/"
956 "Quoted marker in stat commands for file attributes.")
958 (defconst tramp-vc-registered-read-file-names
961 if %s \"$file\"; then
962 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
964 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
966 if %s \"$file\"; then
967 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
969 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
973 "Script to check existence of VC related files.
974 It must be send formatted with two strings; the tests for file
975 existence, and file readability. Input shall be read via
976 here-document, otherwise the command could exceed maximum length
979 ;; New handlers should be added here.
980 (defconst tramp-sh-file-name-handler-alist
981 '(;; `access-file' performed by default handler.
982 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
983 ;; `byte-compiler-base-file-name' performed by default handler.
984 (copy-directory . tramp-sh-handle-copy-directory
)
985 (copy-file . tramp-sh-handle-copy-file
)
986 (delete-directory . tramp-sh-handle-delete-directory
)
987 (delete-file . tramp-sh-handle-delete-file
)
988 ;; `diff-latest-backup-file' performed by default handler.
989 (directory-file-name . tramp-handle-directory-file-name
)
990 (directory-files . tramp-handle-directory-files
)
991 (directory-files-and-attributes
992 . tramp-sh-handle-directory-files-and-attributes
)
993 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
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-case-insensitive-p . tramp-handle-file-name-case-insensitive-p
)
1009 (file-name-completion . tramp-handle-file-name-completion
)
1010 (file-name-directory . tramp-handle-file-name-directory
)
1011 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
1012 ;; `file-name-sans-versions' performed by default handler.
1013 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
1014 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch
)
1015 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
1016 (file-notify-valid-p . tramp-handle-file-notify-valid-p
)
1017 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
1018 (file-readable-p . tramp-sh-handle-file-readable-p
)
1019 (file-regular-p . tramp-handle-file-regular-p
)
1020 (file-remote-p . tramp-handle-file-remote-p
)
1021 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
1022 (file-symlink-p . tramp-handle-file-symlink-p
)
1023 (file-truename . tramp-sh-handle-file-truename
)
1024 (file-writable-p . tramp-sh-handle-file-writable-p
)
1025 (find-backup-file-name . tramp-handle-find-backup-file-name
)
1026 ;; `find-file-noselect' performed by default handler.
1027 ;; `get-file-buffer' performed by default handler.
1028 (insert-directory . tramp-sh-handle-insert-directory
)
1029 (insert-file-contents . tramp-handle-insert-file-contents
)
1030 (load . tramp-handle-load
)
1031 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
1032 (make-directory . tramp-sh-handle-make-directory
)
1033 (make-nearby-temp-file . tramp-handle-make-nearby-temp-file
)
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 (temporary-file-directory . tramp-handle-temporary-file-directory
)
1046 (unhandled-file-name-directory . ignore
)
1047 (vc-registered . tramp-sh-handle-vc-registered
)
1048 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
1049 (write-region . tramp-sh-handle-write-region
))
1050 "Alist of handler functions.
1051 Operations not mentioned here will be handled by the normal Emacs functions.")
1053 ;; This must be the last entry, because `identity' always matches.
1054 ;;;###tramp-autoload
1055 (add-to-list 'tramp-foreign-file-name-handler-alist
1056 '(identity . tramp-sh-file-name-handler
) 'append
)
1058 ;;; File Name Handler Functions:
1060 (defun tramp-sh-handle-make-symbolic-link
1061 (filename linkname
&optional ok-if-already-exists
)
1062 "Like `make-symbolic-link' for Tramp files.
1063 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1064 the symlink. If LINKNAME is a Tramp file, only the localname component is
1065 used as the target of the symlink.
1067 If LINKNAME is a Tramp file and the localname component is relative, then
1068 it is expanded first, before the localname component is taken. Note that
1069 this can give surprising results if the user/host for the source and
1070 target of the symlink differ."
1071 (with-parsed-tramp-file-name linkname l
1072 (let ((ln (tramp-get-remote-ln l
))
1073 (cwd (tramp-run-real-handler
1074 'file-name-directory
(list l-localname
))))
1078 "Making a symbolic link. ln(1) does not exist on the remote host."))
1080 ;; Do the 'confirm if exists' thing.
1081 (when (file-exists-p linkname
)
1083 (if (or (null ok-if-already-exists
) ; not allowed to exist
1084 (and (numberp ok-if-already-exists
)
1087 "File %s already exists; make it a link anyway? "
1090 l
'file-already-exists
"File %s already exists" l-localname
)
1091 (delete-file linkname
)))
1093 ;; If FILENAME is a Tramp name, use just the localname component.
1094 (when (tramp-tramp-file-p filename
)
1096 (tramp-file-name-localname
1097 (tramp-dissect-file-name (expand-file-name filename
)))))
1099 (tramp-flush-file-property l
(file-name-directory l-localname
))
1100 (tramp-flush-file-property l l-localname
)
1102 ;; Right, they are on the same host, regardless of user, method,
1103 ;; etc. We now make the link on the remote machine. This will
1104 ;; occur as the user that FILENAME belongs to.
1105 (and (tramp-send-command-and-check
1106 l
(format "cd %s" (tramp-shell-quote-argument cwd
)))
1107 (tramp-send-command-and-check
1111 (tramp-shell-quote-argument filename
)
1112 ;; The command could exceed PATH_MAX, so we use
1113 ;; relative file names. However, relative file names
1114 ;; could start with "-". `tramp-shell-quote-argument'
1115 ;; does not handle this, we must do it ourselves.
1116 (tramp-shell-quote-argument
1117 (concat "./" (file-name-nondirectory l-localname
)))))))))
1119 (defun tramp-sh-handle-file-truename (filename)
1120 "Like `file-truename' for Tramp files."
1123 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1124 (tramp-make-tramp-file-name
1126 (with-tramp-file-property v localname
"file-truename"
1127 (let ((result nil
) ; result steps in reverse order
1128 (quoted (tramp-compat-file-name-quoted-p localname
))
1129 (localname (tramp-compat-file-name-unquote localname
)))
1130 (tramp-message v
4 "Finding true name for `%s'" filename
)
1132 ;; Use GNU readlink --canonicalize-missing where available.
1133 ((tramp-get-remote-readlink v
)
1134 (tramp-send-command-and-check
1136 (format "%s --canonicalize-missing %s"
1137 (tramp-get-remote-readlink v
)
1138 (tramp-shell-quote-argument localname
)))
1139 (with-current-buffer (tramp-get-connection-buffer v
)
1140 (goto-char (point-min))
1141 (setq result
(buffer-substring (point-min) (point-at-eol)))))
1143 ;; Use Perl implementation.
1144 ((and (tramp-get-remote-perl v
)
1145 (tramp-get-connection-property v
"perl-file-spec" nil
)
1146 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
1147 (tramp-maybe-send-script
1148 v tramp-perl-file-truename
"tramp_perl_file_truename")
1150 (tramp-send-command-and-read
1152 (format "tramp_perl_file_truename %s"
1153 (tramp-shell-quote-argument localname
)))))
1156 (t (let ((steps (split-string localname
"/" 'omit
))
1159 ;; Don't make the following value larger than
1160 ;; necessary. People expect an error message in a
1161 ;; timely fashion when something is wrong;
1162 ;; otherwise they might think that Emacs is hung.
1163 ;; Of course, correctness has to come first.
1166 (while (and steps
(< numchase numchase-limit
))
1167 (setq thisstep
(pop steps
))
1170 (mapconcat 'identity
1171 (append '("") (reverse result
) (list thisstep
))
1173 (setq symlink-target
1174 (tramp-compat-file-attribute-type
1176 (tramp-make-tramp-file-name
1178 (mapconcat 'identity
1183 (cond ((string= "." thisstep
)
1184 (tramp-message v
5 "Ignoring step `.'"))
1185 ((string= ".." thisstep
)
1186 (tramp-message v
5 "Processing step `..'")
1188 ((stringp symlink-target
)
1189 ;; It's a symlink, follow it.
1191 v
5 "Follow symlink to %s" symlink-target
)
1192 (setq numchase
(1+ numchase
))
1193 (when (file-name-absolute-p symlink-target
)
1195 ;; If the symlink was absolute, we'll get a
1196 ;; string like "/user@host:/some/target";
1197 ;; extract the "/some/target" part from it.
1198 (when (tramp-tramp-file-p symlink-target
)
1199 (unless (tramp-equal-remote filename symlink-target
)
1202 "Symlink target `%s' on wrong host"
1204 (setq symlink-target localname
))
1207 (split-string symlink-target
"/" 'omit
) steps
)))
1210 (setq result
(cons thisstep result
)))))
1211 (when (>= numchase numchase-limit
)
1214 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1215 (setq result
(reverse result
))
1216 ;; Combine list to form string.
1219 (mapconcat 'identity
(cons "" result
) "/")
1221 (when (string= "" result
)
1222 (setq result
"/")))))
1224 (when quoted
(setq result
(tramp-compat-file-name-quote result
)))
1225 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
1228 ;; Preserve trailing "/".
1229 (if (string-equal (file-name-nondirectory filename
) "") "/" "")))
1233 (defun tramp-sh-handle-file-exists-p (filename)
1234 "Like `file-exists-p' for Tramp files."
1235 (with-parsed-tramp-file-name filename nil
1236 (with-tramp-file-property v localname
"file-exists-p"
1237 (or (not (null (tramp-get-file-property
1238 v localname
"file-attributes-integer" nil
)))
1239 (not (null (tramp-get-file-property
1240 v localname
"file-attributes-string" nil
)))
1241 (tramp-send-command-and-check
1245 (tramp-get-file-exists-command v
)
1246 (tramp-shell-quote-argument localname
)))))))
1248 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1249 "Like `file-attributes' for Tramp files."
1250 (unless id-format
(setq id-format
'integer
))
1252 ;; Don't modify `last-coding-system-used' by accident.
1253 (let ((last-coding-system-used last-coding-system-used
))
1254 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1255 (with-tramp-file-property
1256 v localname
(format "file-attributes-%s" id-format
)
1258 (tramp-convert-file-attributes
1262 ((tramp-get-remote-stat v
)
1263 (tramp-do-file-attributes-with-stat v localname id-format
))
1264 ((tramp-get-remote-perl v
)
1265 (tramp-do-file-attributes-with-perl v localname id-format
))
1267 ;; The scripts could fail, for example with huge file size.
1268 (tramp-do-file-attributes-with-ls v localname id-format
)))))))))
1270 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1271 "Implement `file-attributes' for Tramp files using the ls(1) command."
1273 res-inode res-filemodes res-numlinks
1274 res-uid res-gid res-size res-symlink-target
)
1275 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1276 ;; We cannot send all three commands combined, it could exceed
1277 ;; NAME_MAX or PATH_MAX. Happened on macOS, for example.
1278 (when (or (tramp-send-command-and-check
1281 (tramp-get-file-exists-command vec
)
1282 (tramp-shell-quote-argument localname
)))
1283 (tramp-send-command-and-check
1286 (tramp-get-test-command vec
)
1287 (tramp-shell-quote-argument localname
))))
1290 (format "%s %s %s %s"
1291 (tramp-get-ls-command vec
)
1292 (if (eq id-format
'integer
) "-ildn" "-ild")
1293 ;; On systems which have no quoting style, file names
1294 ;; with special characters could fail.
1296 ((tramp-get-ls-command-with-quoting-style vec
)
1297 "--quoting-style=c")
1298 ((tramp-get-ls-command-with-w-option vec
)
1301 (tramp-shell-quote-argument localname
)))
1302 ;; Parse `ls -l' output ...
1303 (with-current-buffer (tramp-get-buffer vec
)
1304 (when (> (buffer-size) 0)
1305 (goto-char (point-min))
1309 (read (current-buffer))
1310 (invalid-read-syntax
1311 (when (and (equal (cadr err
)
1312 "Integer constant overflow in reader")
1314 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1316 (let* ((big (read (substring (car (cddr err
)) 0
1317 (match-beginning 1))))
1318 (small (read (match-string 1 (car (cddr err
)))))
1319 (twiddle (/ small
65536)))
1320 (cons (+ big twiddle
)
1321 (- small
(* twiddle
65536))))))))
1322 ;; ... file mode flags
1323 (setq res-filemodes
(symbol-name (read (current-buffer))))
1325 (setq res-numlinks
(read (current-buffer)))
1327 (setq res-uid
(read (current-buffer)))
1328 (setq res-gid
(read (current-buffer)))
1329 (if (eq id-format
'integer
)
1331 (unless (numberp res-uid
)
1332 (setq res-uid tramp-unknown-id-integer
))
1333 (unless (numberp res-gid
)
1334 (setq res-gid tramp-unknown-id-integer
)))
1336 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1337 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1339 (setq res-size
(read (current-buffer)))
1340 ;; From the file modes, figure out other stuff.
1341 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1342 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1343 ;; If symlink, find out file name pointed to.
1345 (search-forward "-> ")
1346 (setq res-symlink-target
1347 (if (tramp-get-ls-command-with-quoting-style vec
)
1348 (read (current-buffer))
1349 (buffer-substring (point) (point-at-eol)))))
1350 ;; Return data gathered.
1352 ;; 0. t for directory, string (name linked to) for symbolic
1354 (or dirp res-symlink-target
)
1355 ;; 1. Number of links to file.
1361 ;; 4. Last access time, as a list of integers. Normally
1362 ;; this would be in the same format as `current-time', but
1363 ;; the subseconds part is not currently implemented, and
1364 ;; (0 0) denotes an unknown time.
1365 ;; 5. Last modification time, likewise.
1366 ;; 6. Last status change time, likewise.
1367 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1368 ;; 7. Size in bytes (-1, if number is out of range).
1370 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1372 ;; 9. t if file's gid would change if file were deleted and
1373 ;; recreated. Will be set in `tramp-convert-file-attributes'.
1375 ;; 10. Inode number.
1377 ;; 11. Device number. Will be replaced by a virtual device number.
1380 (defun tramp-do-file-attributes-with-perl
1381 (vec localname
&optional id-format
)
1382 "Implement `file-attributes' for Tramp files using a Perl script."
1383 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1384 (tramp-maybe-send-script
1385 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1386 (tramp-send-command-and-read
1388 (format "tramp_perl_file_attributes %s %s"
1389 (tramp-shell-quote-argument localname
) id-format
)))
1391 (defun tramp-do-file-attributes-with-stat
1392 (vec localname
&optional id-format
)
1393 "Implement `file-attributes' for Tramp files using stat(1) command."
1394 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1395 (tramp-send-command-and-read
1399 ;; On Opsware, pdksh (which is the true name of ksh there)
1400 ;; doesn't parse correctly the sequence "((". Therefore, we add
1401 ;; a space. Apostrophes in the stat output are masked as
1402 ;; `tramp-stat-marker', in order to make a proper shell escape of
1403 ;; them in file names.
1404 "( (%s %s || %s -h %s) && (%s -c "
1405 "'((%s%%N%s) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 %s%%A%s t %%ie0 -1)' "
1406 "%s | sed -e 's/\"/\\\\\"/g' -e 's/%s/\"/g') || echo nil)")
1407 (tramp-get-file-exists-command vec
)
1408 (tramp-shell-quote-argument localname
)
1409 (tramp-get-test-command vec
)
1410 (tramp-shell-quote-argument localname
)
1411 (tramp-get-remote-stat vec
)
1412 tramp-stat-marker tramp-stat-marker
1413 (if (eq id-format
'integer
)
1414 "%ue0" (concat tramp-stat-marker
"%U" tramp-stat-marker
))
1415 (if (eq id-format
'integer
)
1416 "%ge0" (concat tramp-stat-marker
"%G" tramp-stat-marker
))
1417 tramp-stat-marker tramp-stat-marker
1418 (tramp-shell-quote-argument localname
)
1419 tramp-stat-quoted-marker
)))
1421 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1422 "Like `set-visited-file-modtime' for Tramp files."
1423 (unless (buffer-file-name)
1424 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1427 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1428 (let ((f (buffer-file-name))
1430 (with-parsed-tramp-file-name f nil
1431 (let* ((remote-file-name-inhibit-cache t
)
1432 (attr (file-attributes f
))
1433 ;; '(-1 65535) means file doesn't exists yet.
1434 (modtime (or (tramp-compat-file-attribute-modification-time attr
)
1436 (setq coding-system-used last-coding-system-used
)
1437 ;; We use '(0 0) as a don't-know value. See also
1438 ;; `tramp-do-file-attributes-with-ls'.
1439 (if (not (equal modtime
'(0 0)))
1440 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1444 (format "%s -ild %s"
1445 (tramp-get-ls-command v
)
1446 (tramp-shell-quote-argument localname
)))
1447 (setq attr
(buffer-substring (point) (point-at-eol))))
1448 (tramp-set-file-property
1449 v localname
"visited-file-modtime-ild" attr
))
1450 (setq last-coding-system-used coding-system-used
)
1453 ;; This function makes the same assumption as
1454 ;; `tramp-sh-handle-set-visited-file-modtime'.
1455 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf
)
1456 "Like `verify-visited-file-modtime' for Tramp files.
1457 At the time `verify-visited-file-modtime' calls this function, we
1458 already know that the buffer is visiting a file and that
1459 `visited-file-modtime' does not return 0. Do not call this
1460 function directly, unless those two cases are already taken care
1462 (with-current-buffer (or buf
(current-buffer))
1463 (let ((f (buffer-file-name)))
1464 ;; There is no file visiting the buffer, or the buffer has no
1465 ;; recorded last modification time, or there is no established
1468 (eq (visited-file-modtime) 0)
1469 (not (file-remote-p f nil
'connected
)))
1471 (with-parsed-tramp-file-name f nil
1472 (let* ((remote-file-name-inhibit-cache t
)
1473 (attr (file-attributes f
))
1474 (modtime (tramp-compat-file-attribute-modification-time attr
))
1475 (mt (visited-file-modtime)))
1478 ;; File exists, and has a known modtime.
1479 ((and attr
(not (equal modtime
'(0 0))))
1480 (< (abs (tramp-time-diff
1482 ;; For compatibility, deal with both the old
1483 ;; (HIGH . LOW) and the new (HIGH LOW) return
1484 ;; values of `visited-file-modtime'.
1486 (list (car mt
) (cdr mt
))
1489 ;; Modtime has the don't know value.
1493 (format "%s -ild %s"
1494 (tramp-get-ls-command v
)
1495 (tramp-shell-quote-argument localname
)))
1496 (with-current-buffer (tramp-get-buffer v
)
1497 (setq attr
(buffer-substring (point) (point-at-eol))))
1500 (tramp-get-file-property
1501 v localname
"visited-file-modtime-ild" "")))
1502 ;; If file does not exist, say it is not modified if and
1503 ;; only if that agrees with the buffer's record.
1504 (t (equal mt
'(-1 65535))))))))))
1506 (defun tramp-sh-handle-set-file-modes (filename mode
)
1507 "Like `set-file-modes' for Tramp files."
1508 (with-parsed-tramp-file-name filename nil
1509 (tramp-flush-file-property v
(file-name-directory localname
))
1510 (tramp-flush-file-property v localname
)
1511 ;; FIXME: extract the proper text from chmod's stderr.
1512 (tramp-barf-unless-okay
1514 (format "chmod %o %s" mode
(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 (with-parsed-tramp-file-name filename nil
1520 (when (tramp-get-remote-touch v
)
1521 (tramp-flush-file-property v
(file-name-directory localname
))
1522 (tramp-flush-file-property v localname
)
1523 (let ((time (if (or (null time
) (equal time
'(0 0)))
1526 (tramp-send-command-and-check
1528 "env TZ=UTC %s %s %s"
1529 (tramp-get-remote-touch v
)
1530 (if (tramp-get-connection-property v
"touch-t" nil
)
1531 (format "-t %s" (format-time-string "%Y%m%d%H%M.%S" time t
))
1533 (tramp-shell-quote-argument localname
)))))))
1535 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1536 "Set the ownership for FILENAME.
1537 If UID and GID are provided, these values are used; otherwise uid
1538 and gid of the corresponding user is taken. Both parameters must
1539 be non-negative integers."
1540 ;; Modern Unices allow chown only for root. So we might need
1541 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1542 ;; working with su(do)? when it is needed, so it shall succeed in
1543 ;; the majority of cases.
1544 ;; Don't modify `last-coding-system-used' by accident.
1545 (let ((last-coding-system-used last-coding-system-used
))
1546 (if (tramp-tramp-file-p filename
)
1547 (with-parsed-tramp-file-name filename nil
1548 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1549 ;; If we are root on the local host, we can do it directly.
1550 (tramp-set-file-uid-gid localname uid gid
)
1551 (let ((uid (or (and (natnump uid
) uid
)
1552 (tramp-get-remote-uid v
'integer
)))
1553 (gid (or (and (natnump gid
) gid
)
1554 (tramp-get-remote-gid v
'integer
))))
1557 "chown %d:%d %s" uid gid
1558 (tramp-shell-quote-argument localname
))))))
1560 ;; We handle also the local part, because there doesn't exist
1561 ;; `set-file-uid-gid'. On W32 "chown" might not work. We add a
1562 ;; timeout for this.
1563 (with-timeout (5 nil
)
1564 (let ((uid (or (and (natnump uid
) uid
) (tramp-get-local-uid 'integer
)))
1565 (gid (or (and (natnump gid
) gid
) (tramp-get-local-gid 'integer
))))
1567 nil
"chown" nil nil nil
1568 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
)))))))
1570 (defun tramp-remote-selinux-p (vec)
1571 "Check, whether SELINUX is enabled on the remote host."
1572 (with-tramp-connection-property (tramp-get-connection-process vec
) "selinux-p"
1573 (tramp-send-command-and-check vec
"selinuxenabled")))
1575 (defun tramp-sh-handle-file-selinux-context (filename)
1576 "Like `file-selinux-context' for Tramp files."
1577 (with-parsed-tramp-file-name filename nil
1578 (with-tramp-file-property v localname
"file-selinux-context"
1579 (let ((context '(nil nil nil nil
))
1580 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1581 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1582 (when (and (tramp-remote-selinux-p v
)
1583 (tramp-send-command-and-check
1586 (tramp-get-ls-command v
)
1587 (tramp-shell-quote-argument localname
))))
1588 (with-current-buffer (tramp-get-connection-buffer v
)
1589 (goto-char (point-min))
1590 (when (re-search-forward regexp
(point-at-eol) t
)
1591 (setq context
(list (match-string 1) (match-string 2)
1592 (match-string 3) (match-string 4))))))
1593 ;; Return the context.
1596 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1597 "Like `set-file-selinux-context' for Tramp files."
1598 (with-parsed-tramp-file-name filename nil
1599 (when (and (consp context
)
1600 (tramp-remote-selinux-p v
))
1601 (let ((user (and (stringp (nth 0 context
)) (nth 0 context
)))
1602 (role (and (stringp (nth 1 context
)) (nth 1 context
)))
1603 (type (and (stringp (nth 2 context
)) (nth 2 context
)))
1604 (range (and (stringp (nth 3 context
)) (nth 3 context
))))
1605 (when (tramp-send-command-and-check
1606 v
(format "chcon %s %s %s %s %s"
1607 (if user
(format "--user=%s" user
) "")
1608 (if role
(format "--role=%s" role
) "")
1609 (if type
(format "--type=%s" type
) "")
1610 (if range
(format "--range=%s" range
) "")
1611 (tramp-shell-quote-argument localname
)))
1612 (if (and user role type range
)
1613 (tramp-set-file-property
1614 v localname
"file-selinux-context" context
)
1615 (tramp-set-file-property
1616 v localname
"file-selinux-context" 'undef
))
1619 (defun tramp-remote-acl-p (vec)
1620 "Check, whether ACL is enabled on the remote host."
1621 (with-tramp-connection-property (tramp-get-connection-process vec
) "acl-p"
1622 (tramp-send-command-and-check vec
"getfacl /")))
1624 (defun tramp-sh-handle-file-acl (filename)
1625 "Like `file-acl' for Tramp files."
1626 (with-parsed-tramp-file-name filename nil
1627 (with-tramp-file-property v localname
"file-acl"
1628 (when (and (tramp-remote-acl-p v
)
1629 (tramp-send-command-and-check
1632 (tramp-shell-quote-argument localname
))))
1633 (with-current-buffer (tramp-get-connection-buffer v
)
1634 (goto-char (point-max))
1635 (delete-blank-lines)
1636 (when (> (point-max) (point-min))
1637 (substring-no-properties (buffer-string))))))))
1639 (defun tramp-sh-handle-set-file-acl (filename acl-string
)
1640 "Like `set-file-acl' for Tramp files."
1641 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1642 (if (and (stringp acl-string
) (tramp-remote-acl-p v
)
1645 v
(format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1646 (tramp-shell-quote-argument localname
)
1647 tramp-end-of-heredoc
1649 tramp-end-of-heredoc
))
1650 (tramp-send-command-and-check v nil
)))
1653 (tramp-set-file-property v localname
"file-acl" acl-string
)
1655 ;; In case of errors, we return nil.
1656 (tramp-set-file-property v localname
"file-acl-string" 'undef
)
1659 ;; Simple functions using the `test' command.
1661 (defun tramp-sh-handle-file-executable-p (filename)
1662 "Like `file-executable-p' for Tramp files."
1663 (with-parsed-tramp-file-name filename nil
1664 (with-tramp-file-property v localname
"file-executable-p"
1665 ;; Examine `file-attributes' cache to see if request can be
1666 ;; satisfied without remote operation.
1667 (or (tramp-check-cached-permissions v ?x
)
1668 (tramp-run-test "-x" filename
)))))
1670 (defun tramp-sh-handle-file-readable-p (filename)
1671 "Like `file-readable-p' for Tramp files."
1672 (with-parsed-tramp-file-name filename nil
1673 (with-tramp-file-property v localname
"file-readable-p"
1674 ;; Examine `file-attributes' cache to see if request can be
1675 ;; satisfied without remote operation.
1676 (or (tramp-check-cached-permissions v ?r
)
1677 (tramp-run-test "-r" filename
)))))
1679 ;; When the remote shell is started, it looks for a shell which groks
1680 ;; tilde expansion. Here, we assume that all shells which grok tilde
1681 ;; expansion will also provide a `test' command which groks `-nt' (for
1682 ;; newer than). If this breaks, tell me about it and I'll try to do
1683 ;; something smarter about it.
1684 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1685 "Like `file-newer-than-file-p' for Tramp files."
1686 (cond ((not (file-exists-p file1
))
1688 ((not (file-exists-p file2
))
1690 ;; We are sure both files exist at this point.
1693 ;; We try to get the mtime of both files. If they are not
1694 ;; equal to the "dont-know" value, then we subtract the times
1695 ;; and obtain the result.
1696 (let ((fa1 (file-attributes file1
))
1697 (fa2 (file-attributes file2
)))
1700 (equal (tramp-compat-file-attribute-modification-time fa1
)
1703 (equal (tramp-compat-file-attribute-modification-time fa2
)
1705 (> 0 (tramp-time-diff
1706 (tramp-compat-file-attribute-modification-time fa2
)
1707 (tramp-compat-file-attribute-modification-time fa1
)))
1708 ;; If one of them is the dont-know value, then we can
1709 ;; still try to run a shell command on the remote host.
1710 ;; However, this only works if both files are Tramp
1711 ;; files and both have the same method, same user, same
1713 (unless (tramp-equal-remote file1 file2
)
1714 (with-parsed-tramp-file-name
1715 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1718 "Files %s and %s must have same method, user, host"
1720 (with-parsed-tramp-file-name file1 nil
1722 (tramp-get-test-nt-command v
) file1 file2
))))))))
1724 ;; Functions implemented using the basic functions above.
1726 (defun tramp-sh-handle-file-directory-p (filename)
1727 "Like `file-directory-p' for Tramp files."
1728 (with-parsed-tramp-file-name filename nil
1729 ;; `file-directory-p' is used as predicate for file name completion.
1730 ;; Sometimes, when a connection is not established yet, it is
1731 ;; desirable to return t immediately for "/method:foo:". It can
1732 ;; be expected that this is always a directory.
1733 (or (zerop (length localname
))
1734 (with-tramp-file-property v localname
"file-directory-p"
1735 (tramp-run-test "-d" filename
)))))
1737 (defun tramp-sh-handle-file-writable-p (filename)
1738 "Like `file-writable-p' for Tramp files."
1739 (with-parsed-tramp-file-name filename nil
1740 (with-tramp-file-property v localname
"file-writable-p"
1741 (if (file-exists-p filename
)
1742 ;; Examine `file-attributes' cache to see if request can be
1743 ;; satisfied without remote operation.
1744 (or (tramp-check-cached-permissions v ?w
)
1745 (tramp-run-test "-w" filename
))
1746 ;; If file doesn't exist, check if directory is writable.
1747 (and (tramp-run-test "-d" (file-name-directory filename
))
1748 (tramp-run-test "-w" (file-name-directory filename
)))))))
1750 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group
)
1751 "Like `file-ownership-preserved-p' for Tramp files."
1752 (with-parsed-tramp-file-name filename nil
1753 (with-tramp-file-property v localname
"file-ownership-preserved-p"
1754 (let ((attributes (file-attributes filename
)))
1755 ;; Return t if the file doesn't exist, since it's true that no
1756 ;; information would be lost by an (attempted) delete and create.
1757 (or (null attributes
)
1759 (= (tramp-compat-file-attribute-user-id attributes
)
1760 (tramp-get-remote-uid v
'integer
))
1762 (= (tramp-compat-file-attribute-group-id attributes
)
1763 (tramp-get-remote-gid v
'integer
)))))))))
1765 ;; Directory listings.
1767 (defun tramp-sh-handle-directory-files-and-attributes
1768 (directory &optional full match nosort id-format
)
1769 "Like `directory-files-and-attributes' for Tramp files."
1770 (unless id-format
(setq id-format
'integer
))
1771 (when (file-directory-p directory
)
1772 (setq directory
(expand-file-name directory
))
1775 (with-parsed-tramp-file-name directory nil
1776 (with-tramp-file-property
1778 (format "directory-files-and-attributes-%s" id-format
)
1783 (tramp-convert-file-attributes v
(cdr x
))))
1786 ((tramp-get-remote-stat v
)
1787 (tramp-do-directory-files-and-attributes-with-stat
1788 v localname id-format
))
1789 ((tramp-get-remote-perl v
)
1790 (tramp-do-directory-files-and-attributes-with-perl
1791 v localname id-format
))
1796 (setq item
(pop temp
))
1797 (when (or (null match
) (string-match match
(car item
)))
1799 (setcar item
(expand-file-name (car item
) directory
)))
1800 (push item result
)))
1804 (sort result
(lambda (x y
) (string< (car x
) (car y
)))))
1805 ;; The scripts could fail, for example with huge file size.
1806 (tramp-handle-directory-files-and-attributes
1807 directory full match nosort id-format
)))))
1809 (defun tramp-do-directory-files-and-attributes-with-perl
1810 (vec localname
&optional id-format
)
1811 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1812 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1813 (tramp-maybe-send-script
1814 vec tramp-perl-directory-files-and-attributes
1815 "tramp_perl_directory_files_and_attributes")
1817 (tramp-send-command-and-read
1819 (format "tramp_perl_directory_files_and_attributes %s %s"
1820 (tramp-shell-quote-argument localname
) id-format
))))
1821 (when (stringp object
) (tramp-error vec
'file-error object
))
1824 (defun tramp-do-directory-files-and-attributes-with-stat
1825 (vec localname
&optional id-format
)
1826 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1827 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1828 (tramp-send-command-and-read
1832 ;; We must care about file names with spaces, or starting with
1833 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1834 ;; but it does not work on all remote systems. Apostrophes in
1835 ;; the stat output are masked as `tramp-stat-marker', in order to
1836 ;; make a proper shell escape of them in file names.
1837 "cd %s && echo \"(\"; (%s %s -a | "
1839 "'(%s%%n%s (%s%%N%s) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 %s%%A%s t %%ie0 -1)' "
1840 "-- 2>/dev/null | sed -e 's/\"/\\\\\"/g' -e 's/%s/\"/g'); echo \")\"")
1841 (tramp-shell-quote-argument localname
)
1842 (tramp-get-ls-command vec
)
1843 ;; On systems which have no quoting style, file names with special
1844 ;; characters could fail.
1846 ((tramp-get-ls-command-with-quoting-style vec
)
1847 "--quoting-style=shell")
1848 ((tramp-get-ls-command-with-w-option vec
)
1851 (tramp-get-remote-stat vec
)
1852 tramp-stat-marker tramp-stat-marker
1853 tramp-stat-marker tramp-stat-marker
1854 (if (eq id-format
'integer
)
1855 "%ue0" (concat tramp-stat-marker
"%U" tramp-stat-marker
))
1856 (if (eq id-format
'integer
)
1857 "%ge0" (concat tramp-stat-marker
"%G" tramp-stat-marker
))
1858 tramp-stat-marker tramp-stat-marker
1859 tramp-stat-quoted-marker
)))
1861 ;; This function should return "foo/" for directories and "bar" for
1863 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1864 "Like `file-name-all-completions' for Tramp files."
1865 (unless (save-match-data (string-match "/" filename
))
1868 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1869 (with-tramp-file-property v localname
"file-name-all-completions"
1871 ;; Get a list of directories and files, including reliably
1872 ;; tagging the directories with a trailing "/". Because I
1873 ;; rock. --daniel@danann.net
1876 (if (tramp-get-remote-perl v
)
1878 (tramp-maybe-send-script
1879 v tramp-perl-file-name-all-completions
1880 "tramp_perl_file_name_all_completions")
1881 (format "tramp_perl_file_name_all_completions %s"
1882 (tramp-shell-quote-argument localname
)))
1885 "(cd %s 2>&1 && %s -a 2>/dev/null"
1886 " | while IFS= read f; do"
1887 " if %s -d \"$f\" 2>/dev/null;"
1888 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1889 " && \\echo ok) || \\echo fail")
1890 (tramp-shell-quote-argument localname
)
1891 (tramp-get-ls-command v
)
1892 (tramp-get-test-command v
))))
1894 ;; Now grab the output.
1895 (with-current-buffer (tramp-get-buffer v
)
1896 (goto-char (point-max))
1898 ;; Check result code, found in last line of output.
1900 (if (looking-at "^fail$")
1902 ;; Grab error message from line before last line
1903 ;; (it was put there by `cd 2>&1').
1907 "tramp-sh-handle-file-name-all-completions: %s"
1908 (buffer-substring (point) (point-at-eol))))
1909 ;; For peace of mind, if buffer doesn't end in `fail'
1910 ;; then it should end in `ok'. If neither are in the
1911 ;; buffer something went seriously wrong on the remote
1913 (unless (looking-at "^ok$")
1916 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1917 (tramp-shell-quote-argument localname
) (buffer-string))))
1919 (while (zerop (forward-line -
1))
1920 (push (buffer-substring (point) (point-at-eol)) result
)))
1925 (defun tramp-sh-handle-add-name-to-file
1926 (filename newname
&optional ok-if-already-exists
)
1927 "Like `add-name-to-file' for Tramp files."
1928 (unless (tramp-equal-remote filename newname
)
1929 (with-parsed-tramp-file-name
1930 (if (tramp-tramp-file-p filename
) filename newname
) nil
1933 "add-name-to-file: %s"
1934 "only implemented for same method, same user, same host")))
1935 (with-parsed-tramp-file-name filename v1
1936 (with-parsed-tramp-file-name newname v2
1937 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1938 (when (and (numberp ok-if-already-exists
)
1939 (file-exists-p newname
)
1942 "File %s already exists; make it a new name anyway? "
1945 v2
'file-already-exists
1946 "add-name-to-file: file %s already exists" newname
))
1947 (when ok-if-already-exists
(setq ln
(concat ln
" -f")))
1948 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1949 (tramp-flush-file-property v2 v2-localname
)
1950 (tramp-barf-unless-okay
1952 (format "%s %s %s" ln
1953 (tramp-shell-quote-argument v1-localname
)
1954 (tramp-shell-quote-argument v2-localname
))
1955 "error with add-name-to-file, see buffer `%s' for details"
1958 (defun tramp-sh-handle-copy-file
1959 (filename newname
&optional ok-if-already-exists keep-date
1960 preserve-uid-gid preserve-extended-attributes
)
1961 "Like `copy-file' for Tramp files."
1962 (setq filename
(expand-file-name filename
))
1963 (setq newname
(expand-file-name newname
))
1965 ;; At least one file a Tramp file?
1966 ((or (tramp-tramp-file-p filename
)
1967 (tramp-tramp-file-p newname
))
1968 (tramp-do-copy-or-rename-file
1969 'copy filename newname ok-if-already-exists keep-date
1970 preserve-uid-gid preserve-extended-attributes
))
1971 ;; Compat section. PRESERVE-EXTENDED-ATTRIBUTES has been
1972 ;; introduced with Emacs 24.1 (as PRESERVE-SELINUX-CONTEXT), and
1973 ;; renamed in Emacs 24.3.
1974 (preserve-extended-attributes
1975 (tramp-run-real-handler
1977 (list filename newname ok-if-already-exists keep-date
1978 preserve-uid-gid preserve-extended-attributes
)))
1980 (tramp-run-real-handler
1982 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))))
1984 (defun tramp-sh-handle-copy-directory
1985 (dirname newname
&optional keep-date parents copy-contents
)
1986 "Like `copy-directory' for Tramp files."
1987 (let ((t1 (tramp-tramp-file-p dirname
))
1988 (t2 (tramp-tramp-file-p newname
)))
1989 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1990 (if (and (not copy-contents
)
1991 (tramp-get-method-parameter v
'tramp-copy-recursive
)
1992 ;; When DIRNAME and NEWNAME are remote, they must have
1994 (or (null t1
) (null t2
)
1996 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1997 (tramp-file-name-method
1998 (tramp-dissect-file-name newname
)))))
1999 ;; scp or rsync DTRT.
2001 (setq dirname
(directory-file-name (expand-file-name dirname
))
2002 newname
(directory-file-name (expand-file-name newname
)))
2003 (if (and (file-directory-p newname
)
2004 (not (string-equal (file-name-nondirectory dirname
)
2005 (file-name-nondirectory newname
))))
2008 (file-name-nondirectory dirname
) newname
)))
2009 (if (not (file-directory-p (file-name-directory newname
)))
2010 (make-directory (file-name-directory newname
) parents
))
2011 (tramp-do-copy-or-rename-file-out-of-band
2012 'copy dirname newname keep-date
))
2013 ;; We must do it file-wise.
2014 (tramp-run-real-handler
2017 (list dirname newname keep-date parents copy-contents
)
2018 (list dirname newname keep-date parents
))))
2020 ;; When newname did exist, we have wrong cached values.
2022 (with-parsed-tramp-file-name newname nil
2023 (tramp-flush-file-property v
(file-name-directory localname
))
2024 (tramp-flush-file-property v localname
))))))
2026 (defun tramp-sh-handle-rename-file
2027 (filename newname
&optional ok-if-already-exists
)
2028 "Like `rename-file' for Tramp files."
2029 ;; Check if both files are local -- invoke normal rename-file.
2030 ;; Otherwise, use Tramp from local system.
2031 (setq filename
(expand-file-name filename
))
2032 (setq newname
(expand-file-name newname
))
2033 ;; At least one file a Tramp file?
2034 (if (or (tramp-tramp-file-p filename
)
2035 (tramp-tramp-file-p newname
))
2036 (tramp-do-copy-or-rename-file
2037 'rename filename newname ok-if-already-exists
2038 'keep-time
'preserve-uid-gid
)
2039 (tramp-run-real-handler
2040 'rename-file
(list filename newname ok-if-already-exists
))))
2042 (defun tramp-do-copy-or-rename-file
2043 (op filename newname
&optional ok-if-already-exists keep-date
2044 preserve-uid-gid preserve-extended-attributes
)
2045 "Copy or rename a remote file.
2046 OP must be `copy' or `rename' and indicates the operation to perform.
2047 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2048 the new file (for copy) or the new name of the file (for rename).
2049 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2050 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2051 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2052 the uid and gid if both files are on the same host.
2053 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
2055 This function is invoked by `tramp-sh-handle-copy-file' and
2056 `tramp-sh-handle-rename-file'. It is an error if OP is neither
2057 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
2059 (unless (memq op
'(copy rename
))
2060 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
2061 (let ((t1 (tramp-tramp-file-p filename
))
2062 (t2 (tramp-tramp-file-p newname
))
2063 (length (tramp-compat-file-attribute-size
2064 (file-attributes (file-truename filename
))))
2065 (attributes (and preserve-extended-attributes
2066 (apply 'file-extended-attributes
(list filename
)))))
2068 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2069 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
2071 v
'file-already-exists
"File %s already exists" newname
))
2073 (with-tramp-progress-reporter
2074 v
0 (format "%s %s to %s"
2075 (if (eq op
'copy
) "Copying" "Renaming")
2079 ;; Both are Tramp files.
2081 (with-parsed-tramp-file-name filename v1
2082 (with-parsed-tramp-file-name newname v2
2084 ;; Shortcut: if method, host, user are the same for
2085 ;; both files, we invoke `cp' or `mv' on the remote
2087 ((tramp-equal-remote filename newname
)
2088 (tramp-do-copy-or-rename-file-directly
2090 ok-if-already-exists keep-date preserve-uid-gid
))
2092 ;; Try out-of-band operation.
2094 (tramp-method-out-of-band-p v1 length
)
2095 (tramp-method-out-of-band-p v2 length
))
2096 (tramp-do-copy-or-rename-file-out-of-band
2097 op filename newname keep-date
))
2099 ;; No shortcut was possible. So we copy the file
2100 ;; first. If the operation was `rename', we go back
2101 ;; and delete the original file (if the copy was
2102 ;; successful). The approach is simple-minded: we
2103 ;; create a new buffer, insert the contents of the
2104 ;; source file into it, then write out the buffer to
2105 ;; the target file. The advantage is that it doesn't
2106 ;; matter which file name handlers are used for the
2107 ;; source and target file.
2109 (tramp-do-copy-or-rename-file-via-buffer
2110 op filename newname keep-date
))))))
2112 ;; One file is a Tramp file, the other one is local.
2115 ;; Fast track on local machine.
2116 ((tramp-local-host-p v
)
2117 (tramp-do-copy-or-rename-file-directly
2119 ok-if-already-exists keep-date preserve-uid-gid
))
2121 ;; If the Tramp file has an out-of-band method, the
2122 ;; corresponding copy-program can be invoked.
2123 ((tramp-method-out-of-band-p v length
)
2124 (tramp-do-copy-or-rename-file-out-of-band
2125 op filename newname keep-date
))
2127 ;; Use the inline method via a Tramp buffer.
2128 (t (tramp-do-copy-or-rename-file-via-buffer
2129 op filename newname keep-date
))))
2132 ;; One of them must be a Tramp file.
2133 (error "Tramp implementation says this cannot happen")))
2135 ;; Handle `preserve-extended-attributes'. We ignore possible
2136 ;; errors, because ACL strings could be incompatible.
2139 (apply 'set-file-extended-attributes
(list newname attributes
))))
2141 ;; In case of `rename', we must flush the cache of the source file.
2142 (when (and t1
(eq op
'rename
))
2143 (with-parsed-tramp-file-name filename v1
2144 (tramp-flush-file-property v1
(file-name-directory v1-localname
))
2145 (tramp-flush-file-property v1 v1-localname
)))
2147 ;; When newname did exist, we have wrong cached values.
2149 (with-parsed-tramp-file-name newname v2
2150 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
2151 (tramp-flush-file-property v2 v2-localname
)))))))
2153 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
2154 "Use an Emacs buffer to copy or rename a file.
2155 First arg OP is either `copy' or `rename' and indicates the operation.
2156 FILENAME is the source file, NEWNAME the target file.
2157 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2158 ;; We must disable multibyte, because binary data shall not be
2159 ;; converted. We don't want the target file to be compressed, so we
2160 ;; let-bind `jka-compr-inhibit' to t. `epa-file-handler' shall not
2161 ;; be called either. We remove `tramp-file-name-handler' from
2162 ;; `inhibit-file-name-handlers'; otherwise the file name handler for
2163 ;; `insert-file-contents' might be deactivated in some corner cases.
2164 (let ((coding-system-for-read 'binary
)
2165 (coding-system-for-write 'binary
)
2166 (jka-compr-inhibit t
)
2167 (inhibit-file-name-operation 'write-region
)
2168 (inhibit-file-name-handlers
2169 (cons 'epa-file-handler
2170 (remq 'tramp-file-name-handler inhibit-file-name-handlers
))))
2171 (with-temp-file newname
2172 (set-buffer-multibyte nil
)
2173 (insert-file-contents-literally filename
)))
2174 ;; KEEP-DATE handling.
2178 (tramp-compat-file-attribute-modification-time
2179 (file-attributes filename
))))
2181 (set-file-modes newname
(tramp-default-file-modes filename
))
2182 ;; If the operation was `rename', delete the original file.
2183 (unless (eq op
'copy
) (delete-file filename
)))
2185 (defun tramp-do-copy-or-rename-file-directly
2186 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2187 "Invokes `cp' or `mv' on the remote system.
2188 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2189 respectively. FILENAME specifies the file to copy or rename,
2190 NEWNAME is the name of the new file (for copy) or the new name of
2191 the file (for rename). Both files must reside on the same host.
2192 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2193 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2194 the uid and gid from FILENAME."
2195 (let ((t1 (tramp-tramp-file-p filename
))
2196 (t2 (tramp-tramp-file-p newname
))
2197 (file-times (tramp-compat-file-attribute-modification-time
2198 (file-attributes filename
)))
2199 (file-modes (tramp-default-file-modes filename
)))
2200 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2201 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2202 ((eq op
'copy
) "cp -f")
2203 ((eq op
'rename
) "mv -f")
2206 "Unknown operation `%s', must be `copy' or `rename'"
2208 (localname1 (if t1
(file-remote-p filename
'localname
) filename
))
2209 (localname2 (if t2
(file-remote-p newname
'localname
) newname
))
2210 (prefix (file-remote-p (if t1 filename newname
)))
2214 ;; Both files are on a remote host, with same user.
2217 (tramp-send-command-and-check
2218 v
(format "%s %s %s" cmd
2219 (tramp-shell-quote-argument localname1
)
2220 (tramp-shell-quote-argument localname2
))))
2221 (with-current-buffer (tramp-get-buffer v
)
2222 (goto-char (point-min))
2226 ;; Mask cp -f error.
2228 tramp-operation-not-permitted-regexp nil t
))
2230 (tramp-error-with-buffer
2232 "Copying directly failed, see buffer `%s' for details."
2235 ;; We are on the local host.
2238 ;; We can do it directly.
2239 ((let (file-name-handler-alist)
2240 (and (file-readable-p localname1
)
2241 ;; No sticky bit when renaming.
2245 (file-modes (file-name-directory localname1
))
2246 (string-to-number "1000" 8))))
2247 (file-writable-p (file-name-directory localname2
))
2248 (or (file-directory-p localname2
)
2249 (file-writable-p localname2
))))
2252 localname1 localname2 ok-if-already-exists
2253 keep-date preserve-uid-gid
)
2254 (tramp-run-real-handler
2255 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2257 ;; We can do it directly with `tramp-send-command'
2258 ((and (file-readable-p (concat prefix localname1
))
2260 (file-name-directory (concat prefix localname2
)))
2261 (or (file-directory-p (concat prefix localname2
))
2262 (file-writable-p (concat prefix localname2
))))
2263 (tramp-do-copy-or-rename-file-directly
2264 op
(concat prefix localname1
) (concat prefix localname2
)
2265 ok-if-already-exists keep-date t
)
2266 ;; We must change the ownership to the local user.
2267 (tramp-set-file-uid-gid
2268 (concat prefix localname2
)
2269 (tramp-get-local-uid 'integer
)
2270 (tramp-get-local-gid 'integer
)))
2272 ;; We need a temporary file in between.
2274 ;; Create the temporary file.
2275 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2280 (tramp-barf-unless-okay
2283 (tramp-shell-quote-argument localname1
)
2284 (tramp-shell-quote-argument tmpfile
))
2285 "Copying directly failed, see buffer `%s' for details."
2286 (tramp-get-buffer v
))
2287 ;; We must change the ownership as remote user.
2288 ;; Since this does not work reliable, we also
2289 ;; give read permissions.
2291 (concat prefix tmpfile
) (string-to-number "0777" 8))
2292 (tramp-set-file-uid-gid
2293 (concat prefix tmpfile
)
2294 (tramp-get-local-uid 'integer
)
2295 (tramp-get-local-gid 'integer
)))
2299 localname1 tmpfile t keep-date preserve-uid-gid
)
2300 (tramp-run-real-handler
2301 'rename-file
(list localname1 tmpfile t
)))
2302 ;; We must change the ownership as local user.
2303 ;; Since this does not work reliable, we also
2304 ;; give read permissions.
2305 (set-file-modes tmpfile
(string-to-number "0777" 8))
2306 (tramp-set-file-uid-gid
2308 (tramp-get-remote-uid v
'integer
)
2309 (tramp-get-remote-gid v
'integer
))))
2311 ;; Move the temporary file to its destination.
2314 (tramp-barf-unless-okay
2317 (tramp-shell-quote-argument tmpfile
)
2318 (tramp-shell-quote-argument localname2
))
2319 "Copying directly failed, see buffer `%s' for details."
2320 (tramp-get-buffer v
)))
2322 (tramp-run-real-handler
2324 (list tmpfile localname2 ok-if-already-exists
)))))
2327 (ignore-errors (delete-file tmpfile
)))))))))
2329 ;; Set the time and mode. Mask possible errors.
2332 (set-file-times newname file-times
)
2333 (set-file-modes newname file-modes
))))))
2335 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2336 "Invoke `scp' program to copy.
2337 The method used must be an out-of-band method."
2338 (let* ((t1 (tramp-tramp-file-p filename
))
2339 (t2 (tramp-tramp-file-p newname
))
2340 (orig-vec (tramp-dissect-file-name (if t1 filename newname
)))
2341 copy-program copy-args copy-env copy-keep-date port listener spec
2342 options source target remote-copy-program remote-copy-args
)
2344 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2347 ;; Both are Tramp files. We shall optimize it when the
2348 ;; methods for FILENAME and NEWNAME are the same.
2349 (let* ((dir-flag (file-directory-p filename
))
2350 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2354 (file-name-nondirectory newname
) tmpfile
)))
2357 (tramp-do-copy-or-rename-file-out-of-band
2358 op filename tmpfile keep-date
)
2359 (tramp-do-copy-or-rename-file-out-of-band
2360 'rename tmpfile newname keep-date
))
2365 (expand-file-name ".." tmpfile
) 'recursive
)
2366 (delete-file tmpfile
)))))
2368 ;; Set variables for computing the prompt for reading
2370 (setq tramp-current-method
(tramp-file-name-method v
)
2371 tramp-current-user
(or (tramp-file-name-user v
)
2372 (tramp-get-connection-property
2374 tramp-current-host
(tramp-file-name-real-host v
))
2376 ;; Check which ones of source and target are Tramp files.
2377 (setq source
(funcall
2378 (if (and (file-directory-p filename
)
2379 (not (file-exists-p newname
)))
2380 'file-name-as-directory
2383 (tramp-make-copy-program-file-name v
)
2384 (tramp-unquote-shell-quote-argument filename
)))
2386 (tramp-make-copy-program-file-name v
)
2387 (tramp-unquote-shell-quote-argument newname
)))
2389 ;; Check for host and port number.
2390 (setq host
(tramp-file-name-real-host v
)
2391 port
(tramp-file-name-port v
))
2393 ;; Check for user. There might be an interactive setting.
2394 (setq user
(or (tramp-file-name-user v
)
2395 (tramp-get-connection-property v
"login-as" nil
)))
2397 ;; Check for listener port.
2398 (when (tramp-get-method-parameter v
'tramp-remote-copy-args
)
2399 (setq listener
(number-to-string (+ 50000 (random 10000))))
2401 (zerop (tramp-call-process v
"nc" nil nil nil
"-z" host listener
))
2402 (setq listener
(number-to-string (+ 50000 (random 10000))))))
2404 ;; Compose copy command.
2405 (setq host
(or host
"")
2408 spec
(format-spec-make
2409 ?t
(tramp-get-connection-property
2410 (tramp-get-connection-process v
) "temp-file" ""))
2411 options
(format-spec (tramp-ssh-controlmaster-options v
) spec
)
2412 spec
(format-spec-make
2413 ?h host ?u user ?p port ?r listener ?c options
2414 ?k
(if keep-date
" " ""))
2415 copy-program
(tramp-get-method-parameter v
'tramp-copy-program
)
2416 copy-keep-date
(tramp-get-method-parameter
2417 v
'tramp-copy-keep-date
)
2421 ;; " " has either been a replacement of "%k" (when
2422 ;; keep-date argument is non-nil), or a replacement
2423 ;; for the whole keep-date sublist.
2426 (x (tramp-get-method-parameter v
'tramp-copy-args
) copy-args
)
2430 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2431 (if (member "" y
) '(" ") y
))))))
2438 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2439 (unless (member "" x
) (mapconcat 'identity x
" ")))
2440 (tramp-get-method-parameter v
'tramp-copy-env
)))
2443 (tramp-get-method-parameter v
'tramp-remote-copy-program
))
2445 (dolist (x (tramp-get-method-parameter v
'tramp-remote-copy-args
))
2446 (setq remote-copy-args
2449 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2450 (if (member "" y
) '(" ") y
)))))
2452 ;; Check for local copy program.
2453 (unless (executable-find copy-program
)
2455 v
'file-error
"Cannot find local copy program: %s" copy-program
))
2457 ;; Install listener on the remote side. The prompt must be
2458 ;; consumed later on, when the process does not listen anymore.
2459 (when remote-copy-program
2460 (unless (with-tramp-connection-property
2461 v
(concat "remote-copy-program-" remote-copy-program
)
2462 (tramp-find-executable
2463 v remote-copy-program
(tramp-get-remote-path v
)))
2466 "Cannot find remote listener: %s" remote-copy-program
))
2467 (setq remote-copy-program
2471 (list remote-copy-program
) remote-copy-args
2472 (list (if t1
(concat "<" source
) (concat ">" target
)) "&"))
2474 (tramp-send-command v remote-copy-program
)
2478 "Listener process not running on remote host: `%s'"
2479 remote-copy-program
))
2480 (tramp-send-command v
(format "netstat -l | grep -q :%s" listener
))
2481 (while (not (tramp-send-command-and-check v nil
))
2483 v
(format "netstat -l | grep -q :%s" listener
)))))
2487 ;; The default directory must be remote.
2488 (let ((default-directory
2489 (file-name-directory (if t1 filename newname
)))
2490 (process-environment (copy-sequence process-environment
)))
2491 ;; Set the transfer process properties.
2492 (tramp-set-connection-property
2493 v
"process-name" (buffer-name (current-buffer)))
2494 (tramp-set-connection-property
2495 v
"process-buffer" (current-buffer))
2498 orig-vec
6 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2499 (setenv (pop copy-env
) (pop copy-env
)))
2504 (if remote-copy-program
2505 (list (if t1
(concat ">" target
) (concat "<" source
)))
2506 (list source target
))))
2508 ;; Use an asynchronous process. By this, password can
2509 ;; be handled. We don't set a timeout, because the
2510 ;; copying of large files can last longer than 60 secs.
2513 'identity
(append (list copy-program
) copy-args
)
2515 (p (let ((default-directory
2516 (tramp-compat-temporary-file-directory)))
2517 (start-process-shell-command
2518 (tramp-get-connection-name v
)
2519 (tramp-get-connection-buffer v
)
2521 (tramp-message orig-vec
6 "%s" command
)
2522 (tramp-set-connection-property p
"vector" orig-vec
)
2523 (set-process-query-on-exit-flag p nil
)
2525 ;; We must adapt `tramp-local-end-of-line' for
2526 ;; sending the password.
2527 (let ((tramp-local-end-of-line tramp-rsh-end-of-line
))
2528 (tramp-process-actions
2529 p v nil tramp-actions-copy-out-of-band
))))
2531 ;; Reset the transfer process properties.
2532 (tramp-set-connection-property v
"process-name" nil
)
2533 (tramp-set-connection-property v
"process-buffer" nil
)
2534 ;; Clear the remote prompt.
2535 (when (and remote-copy-program
2536 (not (tramp-send-command-and-check v nil
)))
2537 ;; Houston, we have a problem! Likely, the listener is
2538 ;; still running, so let's clear everything (but the
2539 ;; cached password).
2540 (tramp-cleanup-connection v
'keep-debug
'keep-password
))))
2542 ;; Handle KEEP-DATE argument.
2543 (when (and keep-date
(not copy-keep-date
))
2546 (tramp-compat-file-attribute-modification-time
2547 (file-attributes filename
))))
2550 (unless (and keep-date copy-keep-date
)
2552 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2554 ;; If the operation was `rename', delete the original file.
2555 (unless (eq op
'copy
)
2556 (if (file-regular-p filename
)
2557 (delete-file filename
)
2558 (delete-directory filename
'recursive
))))))
2560 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2561 "Like `make-directory' for Tramp files."
2562 (setq dir
(expand-file-name dir
))
2563 (with-parsed-tramp-file-name dir nil
2564 (tramp-flush-directory-property v
(file-name-directory localname
))
2566 (tramp-barf-unless-okay
2568 (if parents
"mkdir -p" "mkdir")
2569 (tramp-shell-quote-argument localname
))
2570 "Couldn't make directory %s" dir
))))
2572 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2573 "Like `delete-directory' for Tramp files."
2574 (setq directory
(expand-file-name directory
))
2575 (with-parsed-tramp-file-name directory nil
2576 (tramp-flush-file-property v
(file-name-directory localname
))
2577 (tramp-flush-directory-property v localname
)
2578 (tramp-barf-unless-okay
2579 v
(format "cd / && %s %s"
2580 (if recursive
"rm -rf" "rmdir")
2581 (tramp-shell-quote-argument localname
))
2582 "Couldn't delete %s" directory
)))
2584 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2585 "Like `delete-file' for Tramp files."
2586 (setq filename
(expand-file-name filename
))
2587 (with-parsed-tramp-file-name filename nil
2588 (tramp-flush-file-property v
(file-name-directory localname
))
2589 (tramp-flush-file-property v localname
)
2590 (tramp-barf-unless-okay
2592 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2593 (tramp-shell-quote-argument localname
))
2594 "Couldn't delete %s" filename
)))
2598 (defvar dired-compress-file-suffixes
)
2599 (declare-function dired-remove-file
"dired-aux")
2601 (defun tramp-sh-handle-dired-compress-file (file)
2602 "Like `dired-compress-file' for Tramp files."
2603 ;; Code stolen mainly from dired-aux.el.
2604 (with-parsed-tramp-file-name file nil
2605 (tramp-flush-file-property v localname
)
2607 (let ((suffixes dired-compress-file-suffixes
)
2609 ;; See if any suffix rule matches this file name.
2611 (let (case-fold-search)
2612 (if (string-match (car (car suffixes
)) localname
)
2613 (setq suffix
(car suffixes
) suffixes nil
))
2614 (setq suffixes
(cdr suffixes
))))
2616 (cond ((file-symlink-p file
)
2618 ((and suffix
(nth 2 suffix
))
2619 ;; We found an uncompression rule.
2620 (with-tramp-progress-reporter
2621 v
0 (format "Uncompressing %s" file
)
2622 (when (tramp-send-command-and-check
2623 v
(concat (nth 2 suffix
) " "
2624 (tramp-shell-quote-argument localname
)))
2625 (dired-remove-file file
)
2626 (string-match (car suffix
) file
)
2627 (concat (substring file
0 (match-beginning 0))))))
2629 ;; We don't recognize the file as compressed, so compress it.
2631 (with-tramp-progress-reporter v
0 (format "Compressing %s" file
)
2632 (when (tramp-send-command-and-check
2633 v
(concat "gzip -f "
2634 (tramp-shell-quote-argument localname
)))
2635 (dired-remove-file file
)
2636 (cond ((file-exists-p (concat file
".gz"))
2637 (concat file
".gz"))
2638 ((file-exists-p (concat file
".z"))
2642 (defun tramp-sh-handle-insert-directory
2643 (filename switches
&optional wildcard full-directory-p
)
2644 "Like `insert-directory' for Tramp files."
2645 (setq filename
(expand-file-name filename
))
2646 (unless switches
(setq switches
""))
2647 (with-parsed-tramp-file-name filename nil
2648 (if (and (featurep 'ls-lisp
)
2649 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2650 (tramp-handle-insert-directory
2651 filename switches wildcard full-directory-p
)
2652 (when (stringp switches
)
2653 (setq switches
(split-string switches
)))
2654 (when (tramp-get-ls-command-with-quoting-style v
)
2655 (setq switches
(append switches
'("--quoting-style=literal"))))
2656 (when (and (member "--dired" switches
)
2657 (not (tramp-get-ls-command-with-dired v
)))
2658 (setq switches
(delete "--dired" switches
)))
2660 (setq wildcard
(tramp-run-real-handler
2661 'file-name-nondirectory
(list localname
)))
2662 (setq localname
(tramp-run-real-handler
2663 'file-name-directory
(list localname
))))
2664 (unless (or full-directory-p
(member "-d" switches
))
2665 (setq switches
(append switches
'("-d"))))
2666 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2668 (setq switches
(concat switches
" " wildcard
)))
2670 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2671 switches filename
(if wildcard
"yes" "no")
2672 (if full-directory-p
"yes" "no"))
2673 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2674 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2675 (if full-directory-p
2678 (format "%s %s %s 2>/dev/null"
2679 (tramp-get-ls-command v
)
2683 (tramp-shell-quote-argument (concat localname
".")))))
2684 (tramp-barf-unless-okay
2686 (format "cd %s" (tramp-shell-quote-argument
2687 (tramp-run-real-handler
2688 'file-name-directory
(list localname
))))
2690 (tramp-shell-quote-argument
2691 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2694 (format "%s %s %s 2>/dev/null"
2695 (tramp-get-ls-command v
)
2699 (tramp-run-real-handler
2700 'file-name-nondirectory
(list localname
)))))
2702 (tramp-shell-quote-argument
2703 (tramp-run-real-handler
2704 'file-name-nondirectory
(list localname
)))))))
2707 (let ((beg (point)))
2708 (narrow-to-region (point) (point))
2709 ;; We cannot use `insert-buffer-substring' because the Tramp
2710 ;; buffer changes its contents before insertion due to calling
2711 ;; `expand-file-name' and alike.
2713 (with-current-buffer (tramp-get-buffer v
)
2716 ;; Check for "--dired" output.
2718 (when (looking-at "//SUBDIRED//")
2720 (when (looking-at "//DIRED//\\s-+")
2721 (let ((databeg (match-end 0))
2722 (end (point-at-eol)))
2723 ;; Now read the numeric positions of file names.
2725 (while (< (point) end
)
2726 (let ((start (+ beg
(read (current-buffer))))
2727 (end (+ beg
(read (current-buffer)))))
2728 (if (memq (char-after end
) '(?
\n ?\
))
2729 ;; End is followed by \n or by " -> ".
2730 (put-text-property start end
'dired-filename t
))))))
2731 ;; Remove trailing lines.
2732 (goto-char (point-at-bol))
2733 (while (looking-at "//")
2735 (delete-region (match-beginning 0) (point)))
2737 ;; Some busyboxes are reluctant to discard colors.
2739 (string-match "color" (tramp-get-connection-property v
"ls" ""))
2742 (re-search-forward tramp-display-escape-sequence-regexp nil t
)
2743 (replace-match "")))
2745 ;; Decode the output, it could be multibyte.
2746 (decode-coding-region
2748 (or file-name-coding-system default-file-name-coding-system
))
2750 ;; The inserted file could be from somewhere else.
2751 (when (and (not wildcard
) (not full-directory-p
))
2752 (goto-char (point-max))
2753 (when (file-symlink-p filename
)
2754 (goto-char (search-backward "->" beg
'noerror
)))
2756 (if (zerop (length (file-name-nondirectory filename
)))
2758 (file-name-nondirectory filename
))
2760 (replace-match (file-relative-name filename
) t
))
2762 (goto-char (point-max)))))))
2764 ;; Canonicalization of file names.
2766 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2767 "Like `expand-file-name' for Tramp files.
2768 If the localname part of the given file name starts with \"/../\" then
2769 the result will be a local, non-Tramp, file name."
2770 ;; If DIR is not given, use `default-directory' or "/".
2771 (setq dir
(or dir default-directory
"/"))
2772 ;; Unless NAME is absolute, concat DIR and NAME.
2773 (unless (file-name-absolute-p name
)
2774 (setq name
(concat (file-name-as-directory dir
) name
)))
2775 ;; If connection is not established yet, run the real handler.
2776 (if (not (tramp-connectable-p name
))
2777 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2779 (with-parsed-tramp-file-name name nil
2780 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2781 (setq localname
(concat "~/" localname
)))
2782 ;; Tilde expansion if necessary. This needs a shell which
2783 ;; groks tilde expansion! The function `tramp-find-shell' is
2784 ;; supposed to find such a shell on the remote host. Please
2785 ;; tell me about it when this doesn't work on your system.
2786 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2787 (let ((uname (match-string 1 localname
))
2788 (fname (match-string 2 localname
)))
2789 ;; We cannot simply apply "~/", because under sudo "~/" is
2790 ;; expanded to the local user home directory but to the
2791 ;; root home directory. On the other hand, using always
2792 ;; the default user name for tilde expansion is not
2793 ;; appropriate either, because ssh and companions might
2794 ;; use a user name from the config file.
2795 (when (and (string-equal uname
"~")
2796 (string-match "\\`su\\(do\\)?\\'" method
))
2797 (setq uname
(concat uname user
)))
2799 (with-tramp-connection-property v uname
2801 v
(format "cd %s && pwd" (tramp-shell-quote-argument uname
)))
2802 (with-current-buffer (tramp-get-buffer v
)
2803 (goto-char (point-min))
2804 (buffer-substring (point) (point-at-eol)))))
2805 (setq localname
(concat uname fname
))))
2806 ;; There might be a double slash, for example when "~/"
2807 ;; expands to "/". Remove this.
2808 (while (string-match "//" localname
)
2809 (setq localname
(replace-match "/" t t localname
)))
2810 ;; No tilde characters in file name, do normal
2811 ;; `expand-file-name' (this does "/./" and "/../").
2812 ;; `default-directory' is bound, because on Windows there would
2813 ;; be problems with UNC shares or Cygwin mounts.
2814 (let ((default-directory (tramp-compat-temporary-file-directory)))
2815 (tramp-make-tramp-file-name
2817 (tramp-drop-volume-letter
2818 (tramp-run-real-handler
2819 'expand-file-name
(list localname
)))
2822 ;;; Remote commands:
2824 (defun tramp-process-sentinel (proc event
)
2825 "Flush file caches."
2826 (unless (tramp-compat-process-live-p proc
)
2827 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2829 (tramp-message vec
5 "Sentinel called: `%S' `%s'" proc event
)
2830 (tramp-flush-connection-property proc
)
2831 (tramp-flush-directory-property vec
"")))))
2833 ;; We use BUFFER also as connection buffer during setup. Because of
2834 ;; this, its original contents must be saved, and restored once
2835 ;; connection has been setup.
2836 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2837 "Like `start-file-process' for Tramp files."
2838 (with-parsed-tramp-file-name (expand-file-name default-directory
) nil
2841 (get-buffer-create buffer
)
2842 ;; BUFFER can be nil. We use a temporary buffer.
2843 (generate-new-buffer tramp-temp-buffer-name
)))
2844 ;; When PROGRAM matches "*sh", and the first arg is "-c",
2845 ;; it might be that the arguments exceed the command line
2846 ;; length. Therefore, we modify the command.
2847 (heredoc (and (stringp program
)
2848 (string-match "sh$" program
)
2849 (string-equal "-c" (car args
))
2850 (= (length args
) 2)))
2851 ;; When PROGRAM is nil, we just provide a tty.
2852 (args (if (not heredoc
) args
2854 (while (and (< i
(length (cadr args
)))
2855 (string-match " " (cadr args
) i
))
2858 (list (replace-match " \\\\\n" nil nil
(cadr args
))))
2859 (setq i
(+ i
250))))
2861 ;; Use a human-friendly prompt, for example for `shell'.
2862 ;; We discard hops, if existing, that's why we cannot use
2864 (prompt (format "PS1=%s %s"
2865 (tramp-make-tramp-file-name
2866 (tramp-file-name-method v
)
2867 (tramp-file-name-user v
)
2868 (tramp-file-name-host v
)
2869 (tramp-file-name-localname v
))
2870 tramp-initial-end-of-output
))
2871 ;; We use as environment the difference to toplevel
2872 ;; `process-environment'.
2874 (env (dolist (elt (cons prompt process-environment
) env
)
2875 (or (member elt
(default-toplevel-value 'process-environment
))
2876 (if (string-match "=" elt
)
2877 (setq env
(append env
`(,elt
)))
2878 (if (tramp-get-env-with-u-option v
)
2879 (setq env
(append `("-u" ,elt
) env
))
2880 (setq uenv
(cons elt uenv
)))))))
2882 (when (stringp program
)
2883 (format "cd %s && %s exec %s env %s %s"
2884 (tramp-shell-quote-argument localname
)
2888 (mapconcat 'tramp-shell-quote-argument uenv
" "))
2890 (if heredoc
(format "<<'%s'" tramp-end-of-heredoc
) "")
2891 (mapconcat 'tramp-shell-quote-argument env
" ")
2893 (format "%s\n(\n%s\n) </dev/tty\n%s"
2894 program
(car args
) tramp-end-of-heredoc
)
2895 (mapconcat 'tramp-shell-quote-argument
2896 (cons program args
) " ")))))
2897 (tramp-process-connection-type
2898 (or (null program
) tramp-process-connection-type
))
2899 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
2902 ;; We do not want to raise an error when
2903 ;; `start-file-process' has been started several times in
2904 ;; `eshell' and friends.
2905 (tramp-current-connection nil
))
2907 (while (get-process name1
)
2908 ;; NAME must be unique as process name.
2910 name1
(format "%s<%d>" name i
)))
2912 ;; Set the new process properties.
2913 (tramp-set-connection-property v
"process-name" name
)
2914 (tramp-set-connection-property v
"process-buffer" buffer
)
2916 (with-current-buffer (tramp-get-connection-buffer v
)
2918 ;; We catch this event. Otherwise, `start-process' could
2919 ;; be called on the local host.
2922 ;; Activate narrowing in order to save BUFFER
2923 ;; contents. Clear also the modification time;
2924 ;; otherwise we might be interrupted by
2925 ;; `verify-visited-file-modtime'.
2926 (let ((buffer-undo-list t
)
2927 (buffer-read-only nil
)
2929 (clear-visited-file-modtime)
2930 (narrow-to-region (point-max) (point-max))
2931 ;; We call `tramp-maybe-open-connection', in order
2932 ;; to cleanup the prompt afterwards.
2934 (tramp-maybe-open-connection v
)
2936 (delete-region mark
(point))
2937 (narrow-to-region (point-max) (point-max))
2940 ;; Send the command.
2941 (tramp-send-command v command nil t
) ; nooutput
2942 ;; Check, whether a pty is associated.
2943 (unless (process-get
2944 (tramp-get-connection-process v
) 'remote-tty
)
2947 "pty association is not supported for `%s'" name
))))
2948 (let ((p (tramp-get-connection-process v
)))
2949 ;; Set query flag and process marker for this
2950 ;; process. We ignore errors, because the process
2951 ;; could have finished already.
2953 (set-process-query-on-exit-flag p t
)
2954 (set-marker (process-mark p
) (point)))
2959 (if (string-match tramp-temp-buffer-name
(buffer-name))
2961 (set-process-buffer (tramp-get-connection-process v
) nil
)
2962 (kill-buffer (current-buffer)))
2963 (set-buffer-modified-p bmp
))
2964 (tramp-set-connection-property v
"process-name" nil
)
2965 (tramp-set-connection-property v
"process-buffer" nil
))))))
2967 (defun tramp-sh-handle-process-file
2968 (program &optional infile destination display
&rest args
)
2969 "Like `process-file' for Tramp files."
2970 ;; The implementation is not complete yet.
2971 (when (and (numberp destination
) (zerop destination
))
2972 (error "Implementation does not handle immediate return"))
2974 (with-parsed-tramp-file-name default-directory nil
2975 (let (command env uenv input tmpinput stderr tmpstderr outbuf ret
)
2977 (setq command
(mapconcat 'tramp-shell-quote-argument
2978 (cons program args
) " "))
2979 ;; We use as environment the difference to toplevel `process-environment'.
2980 (dolist (elt process-environment
)
2981 (or (member elt
(default-toplevel-value 'process-environment
))
2982 (if (string-match "=" elt
)
2983 (setq env
(append env
`(,elt
)))
2984 (if (tramp-get-env-with-u-option v
)
2985 (setq env
(append `("-u" ,elt
) env
))
2986 (setq uenv
(cons elt uenv
))))))
2991 (mapconcat 'tramp-shell-quote-argument env
" ") command
)))
2996 (mapconcat 'tramp-shell-quote-argument uenv
" ") command
)))
2999 (setq input
"/dev/null")
3000 (setq infile
(expand-file-name infile
))
3001 (if (tramp-equal-remote default-directory infile
)
3002 ;; INFILE is on the same remote host.
3003 (setq input
(with-parsed-tramp-file-name infile nil localname
))
3004 ;; INFILE must be copied to remote host.
3005 (setq input
(tramp-make-tramp-temp-file v
)
3006 tmpinput
(tramp-make-tramp-file-name method user host input
))
3007 (copy-file infile tmpinput t
)))
3008 (when input
(setq command
(format "%s <%s" command input
)))
3010 ;; Determine output.
3013 ((bufferp destination
)
3014 (setq outbuf destination
))
3016 ((stringp destination
)
3017 (setq outbuf
(get-buffer-create destination
)))
3018 ;; (REAL-DESTINATION ERROR-DESTINATION)
3019 ((consp destination
)
3022 ((bufferp (car destination
))
3023 (setq outbuf
(car destination
)))
3024 ((stringp (car destination
))
3025 (setq outbuf
(get-buffer-create (car destination
))))
3027 (setq outbuf
(current-buffer))))
3030 ((stringp (cadr destination
))
3031 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
3032 (if (tramp-equal-remote default-directory
(cadr destination
))
3033 ;; stderr is on the same remote host.
3034 (setq stderr
(with-parsed-tramp-file-name
3035 (cadr destination
) nil localname
))
3036 ;; stderr must be copied to remote host. The temporary
3037 ;; file must be deleted after execution.
3038 (setq stderr
(tramp-make-tramp-temp-file v
)
3039 tmpstderr
(tramp-make-tramp-file-name
3040 method user host stderr
))))
3041 ;; stderr to be discarded.
3042 ((null (cadr destination
))
3043 (setq stderr
"/dev/null"))))
3046 (setq outbuf
(current-buffer))))
3047 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
3049 ;; Send the command. It might not return in time, so we protect
3050 ;; it. Call it in a subshell, in order to preserve working
3055 (if (tramp-send-command-and-check
3056 v
(format "cd %s && %s"
3057 (tramp-shell-quote-argument localname
)
3061 ;; We should add the output anyway.
3063 (with-current-buffer outbuf
3065 (with-current-buffer (tramp-get-connection-buffer v
)
3067 (when (and display
(get-buffer-window outbuf t
)) (redisplay))))
3068 ;; When the user did interrupt, we should do it also. We use
3069 ;; return code -1 as marker.
3071 (kill-buffer (tramp-get-connection-buffer v
))
3075 (kill-buffer (tramp-get-connection-buffer v
))
3078 ;; Provide error file.
3079 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
3081 ;; Cleanup. We remove all file cache values for the connection,
3082 ;; because the remote process could have changed them.
3083 (when tmpinput
(delete-file tmpinput
))
3085 (unless process-file-side-effects
3086 (tramp-flush-directory-property v
""))
3088 ;; Return exit status.
3093 (defun tramp-sh-handle-file-local-copy (filename)
3094 "Like `file-local-copy' for Tramp files."
3095 (with-parsed-tramp-file-name filename nil
3096 (unless (file-exists-p filename
)
3098 v tramp-file-missing
3099 "Cannot make local copy of non-existing file `%s'" filename
))
3101 (let* ((size (tramp-compat-file-attribute-size
3102 (file-attributes (file-truename filename
))))
3103 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
3104 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
3105 (tmpfile (tramp-compat-make-temp-file filename
)))
3109 ;; `copy-file' handles direct copy and out-of-band methods.
3110 ((or (tramp-local-host-p v
)
3111 (tramp-method-out-of-band-p v size
))
3112 (copy-file filename tmpfile
'ok-if-already-exists
'keep-time
))
3114 ;; Use inline encoding for file transfer.
3117 (with-tramp-progress-reporter
3119 (format-message "Encoding remote file `%s' with `%s'"
3121 (tramp-barf-unless-okay
3122 v
(format rem-enc
(tramp-shell-quote-argument localname
))
3123 "Encoding remote file failed"))
3125 (with-tramp-progress-reporter
3126 v
3 (format-message "Decoding local file `%s' with `%s'"
3128 (if (functionp loc-dec
)
3129 ;; If local decoding is a function, we call it.
3130 ;; We must disable multibyte, because
3131 ;; `uudecode-decode-region' doesn't handle it
3132 ;; correctly. Unset `file-name-handler-alist'.
3133 ;; Otherwise, epa-file gets confused.
3134 (let (file-name-handler-alist
3135 (coding-system-for-write 'binary
))
3136 (with-temp-file tmpfile
3137 (set-buffer-multibyte nil
)
3138 (insert-buffer-substring (tramp-get-buffer v
))
3139 (funcall loc-dec
(point-min) (point-max))))
3141 ;; If tramp-decoding-function is not defined for this
3142 ;; method, we invoke tramp-decoding-command instead.
3143 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
3144 ;; Unset `file-name-handler-alist'. Otherwise,
3145 ;; epa-file gets confused.
3146 (let (file-name-handler-alist
3147 (coding-system-for-write 'binary
))
3148 (with-current-buffer (tramp-get-buffer v
)
3150 (point-min) (point-max) tmpfile2 nil
'no-message
)))
3152 (tramp-call-local-coding-command
3153 loc-dec tmpfile2 tmpfile
)
3154 (delete-file tmpfile2
)))))
3156 ;; Set proper permissions.
3157 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
3158 ;; Set local user ownership.
3159 (tramp-set-file-uid-gid tmpfile
)))
3161 ;; Oops, I don't know what to do.
3163 v
'file-error
"Wrong method specification for `%s'" method
)))
3167 (delete-file tmpfile
)
3168 (signal (car err
) (cdr err
))))
3170 (run-hooks 'tramp-handle-file-local-copy-hook
)
3173 ;; CCC grok LOCKNAME
3174 (defun tramp-sh-handle-write-region
3175 (start end filename
&optional append visit lockname confirm
)
3176 "Like `write-region' for Tramp files."
3177 (setq filename
(expand-file-name filename
))
3178 (with-parsed-tramp-file-name filename nil
3179 ;; Following part commented out because we don't know what to do about
3180 ;; file locking, and it does not appear to be a problem to ignore it.
3181 ;; Ange-ftp ignores it, too.
3182 ;; (when (and lockname (stringp lockname))
3183 ;; (setq lockname (expand-file-name lockname)))
3184 ;; (unless (or (eq lockname nil)
3185 ;; (string= lockname filename))
3187 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3189 (when (and confirm
(file-exists-p filename
))
3190 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3191 (tramp-error v
'file-error
"File not overwritten")))
3193 (let ((uid (or (tramp-compat-file-attribute-user-id
3194 (file-attributes filename
'integer
))
3195 (tramp-get-remote-uid v
'integer
)))
3196 (gid (or (tramp-compat-file-attribute-group-id
3197 (file-attributes filename
'integer
))
3198 (tramp-get-remote-gid v
'integer
))))
3200 (if (and (tramp-local-host-p v
)
3201 ;; `file-writable-p' calls `file-expand-file-name'. We
3202 ;; cannot use `tramp-run-real-handler' therefore.
3203 (let (file-name-handler-alist)
3205 (file-writable-p (file-name-directory localname
))
3206 (or (file-directory-p localname
)
3207 (file-writable-p localname
)))))
3208 ;; Short track: if we are on the local host, we can run directly.
3209 (tramp-run-real-handler
3211 (list start end localname append
'no-message lockname confirm
))
3213 (let* ((modes (save-excursion (tramp-default-file-modes filename
)))
3214 ;; We use this to save the value of
3215 ;; `last-coding-system-used' after writing the tmp
3216 ;; file. At the end of the function, we set
3217 ;; `last-coding-system-used' to this saved value. This
3218 ;; way, any intermediary coding systems used while
3219 ;; talking to the remote shell or suchlike won't hose
3220 ;; this variable. This approach was snarfed from
3223 ;; Write region into a tmp file. This isn't really
3224 ;; needed if we use an encoding function, but currently
3225 ;; we use it always because this makes the logic
3226 ;; simpler. We must also set `temporary-file-directory',
3227 ;; because it could point to a remote directory.
3228 (temporary-file-directory
3229 (tramp-compat-temporary-file-directory))
3230 (tmpfile (or tramp-temp-buffer-file-name
3231 (tramp-compat-make-temp-file filename
))))
3233 ;; If `append' is non-nil, we copy the file locally, and let
3234 ;; the native `write-region' implementation do the job.
3235 (when append
(copy-file filename tmpfile
'ok
))
3237 ;; We say `no-message' here because we don't want the
3238 ;; visited file modtime data to be clobbered from the temp
3239 ;; file. We call `set-visited-file-modtime' ourselves later
3240 ;; on. We must ensure that `file-coding-system-alist'
3241 ;; matches `tmpfile'.
3242 (let (file-name-handler-alist
3243 (file-coding-system-alist
3244 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3246 (tramp-run-real-handler
3248 (list start end tmpfile append
'no-message lockname confirm
))
3250 (setq tramp-temp-buffer-file-name nil
)
3251 (delete-file tmpfile
)
3252 (signal (car err
) (cdr err
))))
3254 ;; Now, `last-coding-system-used' has the right value. Remember it.
3255 (setq coding-system-used last-coding-system-used
))
3257 ;; The permissions of the temporary file should be set. If
3258 ;; FILENAME does not exist (eq modes nil) it has been
3259 ;; renamed to the backup file. This case `save-buffer'
3260 ;; handles permissions.
3261 ;; Ensure that it is still readable.
3265 (logior (or modes
0) (string-to-number "0400" 8))))
3267 ;; This is a bit lengthy due to the different methods
3268 ;; possible for file transfer. First, we check whether the
3269 ;; method uses an scp program. If so, we call it.
3270 ;; Otherwise, both encoding and decoding command must be
3271 ;; specified. However, if the method _also_ specifies an
3272 ;; encoding function, then that is used for encoding the
3273 ;; contents of the tmp file.
3274 (let* ((size (tramp-compat-file-attribute-size
3275 (file-attributes tmpfile
)))
3276 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3277 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3279 ;; `copy-file' handles direct copy and out-of-band methods.
3280 ((or (tramp-local-host-p v
)
3281 (tramp-method-out-of-band-p v size
))
3282 (if (and (not (stringp start
))
3283 (= (or end
(point-max)) (point-max))
3284 (= (or start
(point-min)) (point-min))
3285 (tramp-get-method-parameter v
'tramp-copy-keep-tmpfile
))
3287 (setq tramp-temp-buffer-file-name tmpfile
)
3289 ;; We keep the local file for performance
3290 ;; reasons, useful for "rsync".
3291 (copy-file tmpfile filename t
)
3293 (setq tramp-temp-buffer-file-name nil
)
3294 (delete-file tmpfile
)
3295 (signal (car err
) (cdr err
)))))
3296 (setq tramp-temp-buffer-file-name nil
)
3297 ;; Don't rename, in order to keep context in SELinux.
3299 (copy-file tmpfile filename t
)
3300 (delete-file tmpfile
))))
3302 ;; Use inline file transfer.
3307 (set-buffer-multibyte nil
)
3308 ;; Use encoding function or command.
3309 (with-tramp-progress-reporter
3311 "Encoding local file `%s' using `%s'"
3313 (if (functionp loc-enc
)
3314 ;; The following `let' is a workaround for
3315 ;; the base64.el that comes with pgnus-0.84.
3316 ;; If both of the following conditions are
3317 ;; satisfied, it tries to write to a local
3318 ;; file in default-directory, but at this
3319 ;; point, default-directory is remote.
3320 ;; (`call-process-region' can't write to
3321 ;; remote files, it seems.) The file in
3322 ;; question is a tmp file anyway.
3323 (let ((coding-system-for-read 'binary
)
3325 (tramp-compat-temporary-file-directory)))
3326 (insert-file-contents-literally tmpfile
)
3327 (funcall loc-enc
(point-min) (point-max)))
3329 (unless (zerop (tramp-call-local-coding-command
3333 (concat "Cannot write to `%s', "
3334 "local encoding command `%s' failed")
3335 filename loc-enc
))))
3337 ;; Send buffer into remote decoding command which
3338 ;; writes to remote file. Because this happens on
3339 ;; the remote host, we cannot use the function.
3340 (with-tramp-progress-reporter
3342 "Decoding remote file `%s' using `%s'"
3344 (goto-char (point-max))
3345 (unless (bolp) (newline))
3349 (concat rem-dec
" <<'%s'\n%s%s")
3350 (tramp-shell-quote-argument localname
)
3351 tramp-end-of-heredoc
3353 tramp-end-of-heredoc
))
3354 (tramp-barf-unless-okay
3356 "Couldn't write region to `%s', decode using `%s' failed"
3358 ;; When `file-precious-flag' is set, the region is
3359 ;; written to a temporary file. Check that the
3360 ;; checksum is equal to that from the local tmpfile.
3361 (when file-precious-flag
3364 ;; cksum runs locally, if possible.
3365 (zerop (tramp-call-process v
"cksum" tmpfile t
))
3366 ;; cksum runs remotely.
3367 (tramp-send-command-and-check
3370 "cksum <%s" (tramp-shell-quote-argument localname
)))
3371 ;; ... they are different.
3375 (with-current-buffer (tramp-get-buffer v
)
3379 (concat "Couldn't write region to `%s',"
3380 " decode using `%s' failed")
3381 filename rem-dec
)))))
3384 (delete-file tmpfile
)))
3386 ;; That's not expected.
3390 (concat "Method `%s' should specify both encoding and "
3391 "decoding command or an scp program")
3394 ;; Make `last-coding-system-used' have the right value.
3395 (when coding-system-used
3396 (set 'last-coding-system-used coding-system-used
))))
3398 (tramp-flush-file-property v
(file-name-directory localname
))
3399 (tramp-flush-file-property v localname
)
3401 ;; We must protect `last-coding-system-used', now we have set it
3402 ;; to its correct value.
3403 (let (last-coding-system-used (need-chown t
))
3404 ;; Set file modification time.
3405 (when (or (eq visit t
) (stringp visit
))
3406 (let ((file-attr (file-attributes filename
'integer
)))
3407 (set-visited-file-modtime
3408 ;; We must pass modtime explicitly, because FILENAME can
3409 ;; be different from (buffer-file-name), f.e. if
3410 ;; `file-precious-flag' is set.
3411 (tramp-compat-file-attribute-modification-time file-attr
))
3412 (when (and (= (tramp-compat-file-attribute-user-id file-attr
) uid
)
3413 (= (tramp-compat-file-attribute-group-id file-attr
) gid
))
3414 (setq need-chown nil
))))
3416 ;; Set the ownership.
3418 (tramp-set-file-uid-gid filename uid gid
))
3419 (when (or (eq visit t
) (null visit
) (stringp visit
))
3420 (tramp-message v
0 "Wrote %s" filename
))
3421 (run-hooks 'tramp-handle-write-region-hook
)))))
3423 (defvar tramp-vc-registered-file-names nil
3424 "List used to collect file names, which are checked during `vc-registered'.")
3426 ;; VC backends check for the existence of various different special
3427 ;; files. This is very time consuming, because every single check
3428 ;; requires a remote command (the file cache must be invalidated).
3429 ;; Therefore, we apply a kind of optimization. We install the file
3430 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3431 ;; remembers all file names for which `file-exists-p' or
3432 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3433 ;; is performed. Afterwards, a script is applied for all collected
3434 ;; file names, using just one remote command. The result of this
3435 ;; script is used to fill the file cache with actual values. Now we
3436 ;; can reset the file name handlers, and we make a second run of
3437 ;; `vc-registered', which returns the expected result without sending
3438 ;; any other remote command.
3439 (defun tramp-sh-handle-vc-registered (file)
3440 "Like `vc-registered' for Tramp files."
3441 (with-temp-message ""
3442 (with-parsed-tramp-file-name file nil
3443 (with-tramp-progress-reporter
3444 v
3 (format-message "Checking `vc-registered' for %s" file
)
3446 ;; There could be new files, created by the vc backend. We
3447 ;; cannot reuse the old cache entries, therefore. In
3448 ;; `tramp-get-file-property', `remote-file-name-inhibit-cache'
3449 ;; could also be a timestamp as `current-time' returns. This
3450 ;; means invalidate all cache entries with an older timestamp.
3451 (let (tramp-vc-registered-file-names
3452 (remote-file-name-inhibit-cache (current-time))
3453 (file-name-handler-alist
3454 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3456 ;; Here we collect only file names, which need an operation.
3457 (ignore-errors (tramp-run-real-handler 'vc-registered
(list file
)))
3458 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3460 ;; Send just one command, in order to fill the cache.
3461 (when tramp-vc-registered-file-names
3462 (tramp-maybe-send-script
3464 (format tramp-vc-registered-read-file-names
3465 (tramp-get-file-exists-command v
)
3466 (format "%s -r" (tramp-get-test-command v
)))
3467 "tramp_vc_registered_read_file_names")
3472 ;; We cannot use `tramp-send-command-and-read',
3473 ;; because this does not cooperate well with
3474 ;; heredoc documents.
3478 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3479 tramp-end-of-heredoc
3480 (mapconcat 'tramp-shell-quote-argument
3481 tramp-vc-registered-file-names
3483 tramp-end-of-heredoc
))
3484 (with-current-buffer (tramp-get-connection-buffer v
)
3485 ;; Read the expression.
3486 (goto-char (point-min))
3487 (read (current-buffer)))))
3489 (tramp-set-file-property
3490 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3492 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3493 ;; calls shall be answered from the file cache. We unset
3494 ;; `process-file-side-effects' and `remote-file-name-inhibit-cache'
3495 ;; in order to keep the cache.
3496 (let ((vc-handled-backends vc-handled-backends
)
3497 remote-file-name-inhibit-cache process-file-side-effects
)
3498 ;; Reduce `vc-handled-backends' in order to minimize process calls.
3499 (when (and (memq 'Bzr vc-handled-backends
)
3500 (boundp 'vc-bzr-program
)
3501 (not (with-tramp-connection-property v vc-bzr-program
3502 (tramp-find-executable
3503 v vc-bzr-program
(tramp-get-remote-path v
)))))
3504 (setq vc-handled-backends
(remq 'Bzr vc-handled-backends
)))
3505 (when (and (memq 'Git vc-handled-backends
)
3506 (boundp 'vc-git-program
)
3507 (not (with-tramp-connection-property v vc-git-program
3508 (tramp-find-executable
3509 v vc-git-program
(tramp-get-remote-path v
)))))
3510 (setq vc-handled-backends
(remq 'Git vc-handled-backends
)))
3511 (when (and (memq 'Hg vc-handled-backends
)
3512 (boundp 'vc-hg-program
)
3513 (not (with-tramp-connection-property v vc-hg-program
3514 (tramp-find-executable
3515 v vc-hg-program
(tramp-get-remote-path v
)))))
3516 (setq vc-handled-backends
(remq 'Hg vc-handled-backends
)))
3519 (tramp-run-real-handler 'vc-registered
(list file
))))))))
3521 ;;;###tramp-autoload
3522 (defun tramp-sh-file-name-handler (operation &rest args
)
3523 "Invoke remote-shell Tramp file name handler.
3524 Fall back to normal file name handler if no Tramp handler exists."
3525 (when (and tramp-locked
(not tramp-locker
))
3526 (setq tramp-locked nil
)
3528 (car-safe tramp-current-connection
) 'file-error
3529 "Forbidden reentrant call of Tramp"))
3530 (let ((tl tramp-locked
))
3531 (setq tramp-locked t
)
3533 (let ((tramp-locker t
))
3535 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3537 (apply (cdr fn
) args
)
3538 (tramp-run-real-handler operation args
)))))
3539 (setq tramp-locked tl
))))
3541 (defun tramp-vc-file-name-handler (operation &rest args
)
3542 "Invoke special file name handler, which collects files to be handled."
3545 (tramp-replace-environment-variables
3546 (apply 'tramp-file-name-for-operation operation args
)))
3547 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3548 (with-parsed-tramp-file-name filename nil
3550 ;; That's what we want: file names, for which checks are
3551 ;; applied. We assume that VC uses only `file-exists-p' and
3552 ;; `file-readable-p' checks; otherwise we must extend the
3553 ;; list. We do not perform any action, but return nil, in
3554 ;; order to keep `vc-registered' running.
3555 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3556 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3558 ;; `process-file' and `start-file-process' shall be ignored.
3559 ((and fn
(eq operation
'process-file
) 0))
3560 ((and fn
(eq operation
'start-file-process
) nil
))
3561 ;; Tramp file name handlers like `expand-file-name'. They
3563 (fn (save-match-data (apply (cdr fn
) args
)))
3564 ;; Default file name handlers, we don't care.
3565 (t (tramp-run-real-handler operation args
)))))))
3567 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback
)
3568 "Like `file-notify-add-watch' for Tramp files."
3569 (setq file-name
(expand-file-name file-name
))
3570 (with-parsed-tramp-file-name file-name nil
3571 (let ((default-directory (file-name-directory file-name
))
3572 command events filter p sequence
)
3574 ;; gvfs-monitor-dir.
3575 ((setq command
(tramp-get-remote-gvfs-monitor-dir v
))
3576 (setq filter
'tramp-sh-gvfs-monitor-dir-process-filter
3579 ((and (memq 'change flags
) (memq 'attribute-change flags
))
3580 '(created changed changes-done-hint moved deleted
3582 ((memq 'change flags
)
3583 '(created changed changes-done-hint moved deleted
))
3584 ((memq 'attribute-change flags
) '(attribute-changed)))
3585 sequence
`(,command
,localname
)))
3587 ((setq command
(tramp-get-remote-inotifywait v
))
3588 (setq filter
'tramp-sh-inotifywait-process-filter
3591 ((and (memq 'change flags
) (memq 'attribute-change flags
))
3592 (concat "create,modify,move,moved_from,moved_to,move_self,"
3593 "delete,delete_self,attrib,ignored"))
3594 ((memq 'change flags
)
3595 (concat "create,modify,move,moved_from,moved_to,move_self,"
3596 "delete,delete_self,ignored"))
3597 ((memq 'attribute-change flags
) "attrib,ignored"))
3598 sequence
`(,command
"-mq" "-e" ,events
,localname
)
3599 ;; Make events a list of symbols.
3602 (lambda (x) (intern-soft (replace-regexp-in-string "_" "-" x
)))
3603 (split-string events
"," 'omit
))))
3606 v
'file-notify-error
3607 "No file notification program found on %s"
3608 (file-remote-p file-name
))))
3612 (file-name-nondirectory command
)
3613 (generate-new-buffer
3614 (format " *%s*" (file-name-nondirectory command
)))
3616 ;; Return the process object as watch-descriptor.
3617 (if (not (processp p
))
3619 v
'file-notify-error
3620 "`%s' failed to start on remote host"
3621 (mapconcat 'identity sequence
" "))
3622 (tramp-message v
6 "Run `%s', %S" (mapconcat 'identity sequence
" ") p
)
3623 (tramp-set-connection-property p
"vector" v
)
3624 ;; Needed for process filter.
3625 (process-put p
'events events
)
3626 (process-put p
'watch-name localname
)
3627 (set-process-query-on-exit-flag p nil
)
3628 (set-process-filter p filter
)
3629 ;; There might be an error if the monitor is not supported.
3630 ;; Give the filter a chance to read the output.
3631 (tramp-accept-process-output p
1)
3632 (unless (tramp-compat-process-live-p p
)
3634 v
'file-notify-error
"Monitoring not supported for `%s'" file-name
))
3637 (defun tramp-sh-gvfs-monitor-dir-process-filter (proc string
)
3638 "Read output from \"gvfs-monitor-dir\" and add corresponding \
3639 file-notify events."
3640 (let ((events (process-get proc
'events
))
3642 (with-current-buffer (process-buffer proc
)
3643 (file-remote-p default-directory
)))
3644 (rest-string (process-get proc
'rest-string
)))
3646 (tramp-message proc
10 "Previous string:\n%s" rest-string
))
3647 (tramp-message proc
6 "%S\n%s" proc string
)
3648 (setq string
(concat rest-string string
)
3649 ;; Attribute change is returned in unused wording.
3650 string
(replace-regexp-in-string
3651 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string
))
3652 (when (string-match "Monitoring not supported" string
)
3653 (delete-process proc
))
3655 (while (string-match
3657 "Directory Monitor Event:[\n\r]+"
3658 "Child = \\([^\n\r]+\\)[\n\r]+"
3659 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3660 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3662 (let* ((file (match-string 1 string
))
3663 (file1 (match-string 3 string
))
3669 (replace-regexp-in-string
3670 "_" "-" (downcase (match-string 4 string
)))))
3671 ;; File names are returned as absolute paths. We must
3672 ;; add the remote prefix.
3673 (concat remote-prefix file
)
3674 (when file1
(concat remote-prefix file1
)))))
3675 (setq string
(replace-match "" nil nil string
))
3676 ;; Remove watch when file or directory to be watched is deleted.
3677 (when (and (member (caadr object
) '(moved deleted
))
3678 (string-equal file
(process-get proc
'watch-name
)))
3679 (delete-process proc
))
3680 ;; Usually, we would add an Emacs event now. Unfortunately,
3681 ;; `unread-command-events' does not accept several events at
3682 ;; once. Therefore, we apply the handler directly.
3683 (when (member (caadr object
) events
)
3684 (tramp-compat-funcall
3685 'file-notify-handle-event
3686 `(file-notify ,object file-notify-callback
)))))
3688 ;; Save rest of the string.
3689 (when (zerop (length string
)) (setq string nil
))
3690 (when string
(tramp-message proc
10 "Rest string:\n%s" string
))
3691 (process-put proc
'rest-string string
)))
3693 (defun tramp-sh-inotifywait-process-filter (proc string
)
3694 "Read output from \"inotifywait\" and add corresponding file-notify events."
3695 (let ((events (process-get proc
'events
)))
3696 (tramp-message proc
6 "%S\n%s" proc string
)
3697 (dolist (line (split-string string
"[\n\r]+" 'omit
))
3698 ;; Check, whether there is a problem.
3701 (concat "^[^[:blank:]]+"
3702 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3703 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3705 (tramp-error proc
'file-notify-error
"%s" line
))
3713 (replace-regexp-in-string "_" "-" (downcase x
))))
3714 (split-string (match-string 1 line
) "," 'omit
))
3715 (match-string 3 line
))))
3716 ;; Remove watch when file or directory to be watched is deleted.
3717 (when (member (caadr object
) '(move-self delete-self ignored
))
3718 (delete-process proc
))
3719 ;; Usually, we would add an Emacs event now. Unfortunately,
3720 ;; `unread-command-events' does not accept several events at
3721 ;; once. Therefore, we apply the handler directly.
3722 (when (member (caadr object
) events
)
3723 (tramp-compat-funcall
3724 'file-notify-handle-event
3725 `(file-notify ,object file-notify-callback
)))))))
3727 ;;; Internal Functions:
3729 (defun tramp-maybe-send-script (vec script name
)
3730 "Define in remote shell function NAME implemented as SCRIPT.
3731 Only send the definition if it has not already been done."
3732 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3734 (let ((scripts (tramp-get-connection-property
3735 (tramp-get-connection-process vec
) "scripts" nil
)))
3736 (unless (member name scripts
)
3737 (with-tramp-progress-reporter
3738 vec
5 (format-message "Sending script `%s'" name
)
3739 ;; In bash, leading TABs like in `tramp-vc-registered-read-file-names'
3740 ;; could result in unwanted command expansion. Avoid this.
3741 (setq script
(replace-regexp-in-string
3742 (make-string 1 ?
\t) (make-string 8 ?
) script
))
3743 ;; The script could contain a call of Perl. This is masked with `%s'.
3744 (when (and (string-match "%s" script
)
3745 (not (tramp-get-remote-perl vec
)))
3746 (tramp-error vec
'file-error
"No Perl available on remote host"))
3747 (tramp-barf-unless-okay
3749 (format "%s () {\n%s\n}"
3750 name
(format script
(tramp-get-remote-perl vec
)))
3751 "Script %s sending failed" name
)
3752 (tramp-set-connection-property
3753 (tramp-get-connection-process vec
) "scripts" (cons name scripts
))))))
3755 (defun tramp-run-test (switch filename
)
3756 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3757 Returns the exit code of the `test' program."
3758 (with-parsed-tramp-file-name filename nil
3759 (tramp-send-command-and-check
3763 (tramp-get-test-command v
)
3765 (tramp-shell-quote-argument localname
)))))
3767 (defun tramp-run-test2 (format-string file1 file2
)
3768 "Run `test'-like program on the remote system, given FILE1, FILE2.
3769 FORMAT-STRING contains the program name, switches, and place holders.
3770 Returns the exit code of the `test' program. Barfs if the methods,
3771 hosts, or files, disagree."
3772 (unless (tramp-equal-remote file1 file2
)
3773 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3776 "tramp-run-test2 only implemented for same method, user, host")))
3777 (with-parsed-tramp-file-name file1 v1
3778 (with-parsed-tramp-file-name file1 v2
3779 (tramp-send-command-and-check
3781 (format format-string
3782 (tramp-shell-quote-argument v1-localname
)
3783 (tramp-shell-quote-argument v2-localname
))))))
3785 (defun tramp-find-executable
3786 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3787 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3788 First arg VEC specifies the connection, PROGNAME is the program
3789 to search for, and DIRLIST gives the list of directories to
3790 search. If IGNORE-TILDE is non-nil, directory names starting
3791 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3794 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3796 This function expects to be in the right *tramp* buffer."
3797 (with-current-buffer (tramp-get-connection-buffer vec
)
3799 ;; Check whether the executable is in $PATH. "which(1)" does not
3800 ;; report always a correct error code; therefore we check the
3801 ;; number of words it returns. "SunOS 5.10" (and maybe "SunOS
3802 ;; 5.11") have problems with this command, we disable the call
3804 (unless (or ignore-path
3806 (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3807 (tramp-get-connection-property vec
"uname" "")))
3808 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3809 (goto-char (point-min))
3810 (if (looking-at "^\\s-*1$")
3811 (setq result
(concat "\\" progname
))))
3814 ;; Remove all ~/foo directories from dirlist.
3817 (setq d
(car dirlist
))
3818 (setq dirlist
(cdr dirlist
))
3819 (unless (char-equal ?~
(aref d
0))
3820 (setq newdl
(cons d newdl
))))
3821 (setq dirlist
(nreverse newdl
))))
3824 (format (concat "while read d; "
3825 "do if test -x $d/%s && test -f $d/%s; "
3826 "then echo tramp_executable $d/%s; "
3827 "break; fi; done <<'%s'\n"
3829 progname progname progname
3830 tramp-end-of-heredoc
3831 (mapconcat 'identity dirlist
"\n")
3832 tramp-end-of-heredoc
))
3833 (goto-char (point-max))
3834 (when (search-backward "tramp_executable " nil t
)
3835 (skip-chars-forward "^ ")
3836 (skip-chars-forward " ")
3837 (setq result
(buffer-substring (point) (point-at-eol)))))
3840 (defun tramp-set-remote-path (vec)
3841 "Sets the remote environment PATH to existing directories.
3842 I.e., for each directory in `tramp-remote-path', it is tested
3843 whether it exists and if so, it is added to the environment
3845 (tramp-message vec
5 "Setting $PATH environment variable")
3847 vec
(format "PATH=%s; export PATH"
3848 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3850 ;; ------------------------------------------------------------
3851 ;; -- Communication with external shell --
3852 ;; ------------------------------------------------------------
3854 (defun tramp-find-file-exists-command (vec)
3855 "Find a command on the remote host for checking if a file exists.
3856 Here, we are looking for a command which has zero exit status if the
3857 file exists and nonzero exit status otherwise."
3858 (let ((existing "/")
3860 (tramp-shell-quote-argument "/ this file does not exist "))
3862 ;; The algorithm is as follows: we try a list of several commands.
3863 ;; For each command, we first run `$cmd /' -- this should return
3864 ;; true, as the root directory always exists. And then we run
3865 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3866 ;; does not exist. This should return false. We use the first
3867 ;; command we find that seems to work.
3868 ;; The list of commands to try is as follows:
3869 ;; `ls -d' This works on most systems, but NetBSD 1.4
3870 ;; has a bug: `ls' always returns zero exit
3871 ;; status, even for files which don't exist.
3872 ;; `test -e' Some Bourne shells have a `test' builtin
3873 ;; which does not know the `-e' option.
3874 ;; `/bin/test -e' For those, the `test' binary on disk normally
3875 ;; provides the option. Alas, the binary
3876 ;; is sometimes `/bin/test' and sometimes it's
3878 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3881 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3882 (tramp-send-command-and-check
3883 vec
(format "%s %s" result existing
))
3884 (not (tramp-send-command-and-check
3885 vec
(format "%s %s" result nonexistent
)))))
3887 (and (setq result
"/bin/test -e")
3888 (tramp-send-command-and-check
3889 vec
(format "%s %s" result existing
))
3890 (not (tramp-send-command-and-check
3891 vec
(format "%s %s" result nonexistent
)))))
3893 (and (setq result
"/usr/bin/test -e")
3894 (tramp-send-command-and-check
3895 vec
(format "%s %s" result existing
))
3896 (not (tramp-send-command-and-check
3897 vec
(format "%s %s" result nonexistent
)))))
3899 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3900 (tramp-send-command-and-check
3901 vec
(format "%s %s" result existing
))
3902 (not (tramp-send-command-and-check
3903 vec
(format "%s %s" result nonexistent
))))))
3905 vec
'file-error
"Couldn't find command to check if file exists"))
3908 (defun tramp-open-shell (vec shell
)
3909 "Opens shell SHELL."
3910 (with-tramp-progress-reporter
3911 vec
5 (format-message "Opening remote shell `%s'" shell
)
3912 ;; Find arguments for this shell.
3913 (let ((alist tramp-sh-extra-args
)
3915 (while (and alist
(null extra-args
))
3916 (setq item
(pop alist
))
3917 (when (string-match (car item
) shell
)
3918 (setq extra-args
(cdr item
))))
3919 ;; It is useful to set the prompt in the following command
3920 ;; because some people have a setting for $PS1 which /bin/sh
3921 ;; doesn't know about and thus /bin/sh will display a strange
3922 ;; prompt. For example, if $PS1 has "${CWD}" in the value, then
3923 ;; ksh will display the current working directory but /bin/sh
3924 ;; will display a dollar sign. The following command line sets
3925 ;; $PS1 to a sane value, and works under Bourne-ish shells as
3926 ;; well as csh-like shells. We also unset the variable $ENV
3927 ;; because that is read by some sh implementations (eg, bash
3928 ;; when called as sh) on startup; this way, we avoid the startup
3929 ;; file clobbering $PS1. $PROMPT_COMMAND is another way to set
3930 ;; the prompt in /bin/bash, it must be discarded as well.
3931 ;; $HISTFILE is set according to `tramp-histfile-override'.
3934 "exec env ENV=%s %s PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3935 (or (getenv-internal "ENV" tramp-remote-process-environment
) "")
3936 (if (stringp tramp-histfile-override
)
3937 (format "HISTFILE=%s"
3938 (tramp-shell-quote-argument tramp-histfile-override
))
3939 (if tramp-histfile-override
3940 "HISTFILE='' HISTFILESIZE=0 HISTSIZE=0"
3942 (tramp-shell-quote-argument tramp-end-of-output
)
3943 shell
(or extra-args
""))
3945 ;; Check proper HISTFILE setting. We give up when not working.
3946 (when (and (stringp tramp-histfile-override
)
3947 (file-name-directory tramp-histfile-override
))
3948 (tramp-barf-unless-okay
3952 (tramp-shell-quote-argument
3953 (file-name-directory tramp-histfile-override
)))
3954 "`tramp-histfile-override' uses invalid file `%s'"
3955 tramp-histfile-override
)))
3957 (tramp-set-connection-property
3958 (tramp-get-connection-process vec
) "remote-shell" shell
)))
3960 (defun tramp-find-shell (vec)
3961 "Opens a shell on the remote host which groks tilde expansion."
3962 (with-current-buffer (tramp-get-buffer vec
)
3963 (let ((default-shell (tramp-get-method-parameter vec
'tramp-remote-shell
))
3966 (with-tramp-connection-property vec
"remote-shell"
3967 ;; CCC: "root" does not exist always, see my QNAP TS-459.
3968 ;; Which check could we apply instead?
3969 (tramp-send-command vec
"echo ~root" t
)
3970 (if (or (string-match "^~root$" (buffer-string))
3971 ;; The default shell (ksh93) of OpenSolaris and
3972 ;; Solaris is buggy. We've got reports for
3973 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3974 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3975 (tramp-get-connection-property
3978 (or (tramp-find-executable
3979 vec
"bash" (tramp-get-remote-path vec
) t t
)
3980 (tramp-find-executable
3981 vec
"ksh" (tramp-get-remote-path vec
) t t
)
3982 ;; Maybe it works at least for some other commands.
3988 "Couldn't find a remote shell which groks tilde "
3989 "expansion, using `%s'")
3994 ;; Open a new shell if needed.
3995 (unless (string-equal shell default-shell
)
3997 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
3998 (tramp-open-shell vec shell
)))))
4000 ;; Utility functions.
4002 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
4003 "Wait for shell prompt and barf if none appears.
4004 Looks at process PROC to see if a shell prompt appears in TIMEOUT
4005 seconds. If not, it produces an error message with the given ERROR-ARGS."
4006 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
4008 (tramp-wait-for-regexp
4011 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
4013 (delete-process proc
)
4014 (apply 'tramp-error-with-buffer
4015 (tramp-get-connection-buffer vec
) vec
'file-error error-args
)))))
4017 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
4018 "Set up an interactive shell.
4019 Mainly sets the prompt and the echo correctly. PROC is the shell
4020 process to set up. VEC specifies the connection."
4021 (let ((tramp-end-of-output tramp-initial-end-of-output
)
4022 (case-fold-search t
))
4023 (tramp-open-shell vec
(tramp-get-method-parameter vec
'tramp-remote-shell
))
4025 ;; Disable echo expansion.
4026 (tramp-message vec
5 "Setting up remote shell environment")
4028 vec
"stty -inlcr -onlcr -echo kill '^U' erase '^H'" t
)
4029 ;; Check whether the echo has really been disabled. Some
4030 ;; implementations, like busybox of embedded GNU/Linux, don't
4031 ;; support disabling.
4032 (tramp-send-command vec
"echo foo" t
)
4033 (with-current-buffer (process-buffer proc
)
4034 (goto-char (point-min))
4035 (when (looking-at "echo foo")
4036 (tramp-set-connection-property proc
"remote-echo" t
)
4037 (tramp-message vec
5 "Remote echo still on. Ok.")
4038 ;; Make sure backspaces and their echo are enabled and no line
4039 ;; width magic interferes with them.
4040 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
4042 (tramp-message vec
5 "Setting shell prompt")
4044 vec
(format "PS1=%s PS2='' PS3='' PROMPT_COMMAND=''"
4045 (tramp-shell-quote-argument tramp-end-of-output
))
4048 ;; Check whether the output of "uname -sr" has been changed. If
4049 ;; yes, this is a strong indication that we must expire all
4050 ;; connection properties. We start again with
4051 ;; `tramp-maybe-open-connection', it will be caught there.
4052 (tramp-message vec
5 "Checking system information")
4053 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
4055 (tramp-set-connection-property
4057 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
4058 (when (and (stringp old-uname
) (not (string-equal old-uname uname
)))
4061 "Connection reset, because remote host changed from `%s' to `%s'"
4063 ;; We want to keep the password.
4064 (tramp-cleanup-connection vec t t
)
4065 (throw 'uname-changed
(tramp-maybe-open-connection vec
)))
4067 ;; Try to set up the coding system correctly.
4068 ;; CCC this can't be the right way to do it. Hm.
4069 (tramp-message vec
5 "Determining coding system")
4070 (with-current-buffer (process-buffer proc
)
4071 ;; Use MULE to select the right EOL convention for communicating
4072 ;; with the process.
4073 (let ((cs (or (and (memq 'utf-8
(coding-system-list))
4074 (string-match "utf-?8" (tramp-get-remote-locale vec
))
4075 (cons 'utf-8
'utf-8
))
4076 (process-coding-system proc
)
4077 (cons 'undecided
'undecided
)))
4078 cs-decode cs-encode
)
4079 (when (symbolp cs
) (setq cs
(cons cs cs
)))
4080 (setq cs-decode
(or (car cs
) 'undecided
)
4081 cs-encode
(or (cdr cs
) 'undecided
)
4083 (coding-system-change-eol-conversion
4084 cs-encode
(if (string-match "^Darwin" uname
) 'mac
'unix
)))
4085 (tramp-send-command vec
"echo foo ; echo bar" t
)
4086 (goto-char (point-min))
4087 (when (search-forward "\r" nil t
)
4088 (setq cs-decode
(coding-system-change-eol-conversion cs-decode
'dos
)))
4089 ;; Special setting for macOS.
4090 (when (and (string-match "^Darwin" uname
)
4091 (memq 'utf-8-hfs
(coding-system-list)))
4092 (setq cs-decode
'utf-8-hfs
4093 cs-encode
'utf-8-hfs
))
4094 (set-buffer-process-coding-system cs-decode cs-encode
)
4096 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
)))
4098 (tramp-send-command vec
"set +o vi +o emacs" t
)
4100 ;; Check whether the remote host suffers from buggy
4101 ;; `send-process-string'. This is known for FreeBSD (see comment
4102 ;; in `send_process', file process.c). I've tested sending 624
4103 ;; bytes successfully, sending 625 bytes failed. Emacs makes a
4104 ;; hack when this host type is detected locally. It cannot handle
4105 ;; remote hosts, though.
4106 (with-tramp-connection-property proc
"chunksize"
4108 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
4112 vec
5 "Checking remote host type for `send-process-string' bug")
4113 (if (string-match "^FreeBSD" uname
) 500 0))))
4115 ;; Set remote PATH variable.
4116 (tramp-set-remote-path vec
)
4118 ;; Search for a good shell before searching for a command which
4119 ;; checks if a file exists. This is done because Tramp wants to
4120 ;; use "test foo; echo $?" to check if various conditions hold,
4121 ;; and there are buggy /bin/sh implementations which don't execute
4122 ;; the "echo $?" part if the "test" part has an error. In
4123 ;; particular, the OpenSolaris /bin/sh is a problem. There are
4124 ;; also other problems with /bin/sh of OpenSolaris, like
4125 ;; redirection of stderr in function declarations, or changing
4126 ;; HISTFILE in place. Therefore, OpenSolaris' /bin/sh is replaced
4127 ;; by bash, when detected.
4128 (tramp-find-shell vec
)
4130 ;; Disable unexpected output.
4131 (tramp-send-command vec
"mesg n 2>/dev/null; biff n 2>/dev/null" t
)
4133 ;; IRIX64 bash expands "!" even when in single quotes. This
4134 ;; destroys our shell functions, we must disable it. See
4135 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4136 (when (string-match "^IRIX64" uname
)
4137 (tramp-send-command vec
"set +H" t
))
4139 ;; Disable tab expansion.
4140 (if (string-match "BSD\\|Darwin" uname
)
4141 (tramp-send-command vec
"stty tabs" t
)
4142 (tramp-send-command vec
"stty tab0" t
))
4144 ;; Set utf8 encoding. Needed for macOS, for example. This is
4145 ;; non-POSIX, so we must expect errors on some systems.
4146 (tramp-send-command vec
"stty iutf8 2>/dev/null" t
)
4148 ;; Set `remote-tty' process property.
4149 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"" 'noerror
)))
4150 (unless (zerop (length tty
))
4151 (process-put proc
'remote-tty tty
)))
4153 ;; Dump stty settings in the traces.
4154 (when (>= tramp-verbose
9)
4155 (tramp-send-command vec
"stty -a" t
))
4157 ;; Set the environment.
4158 (tramp-message vec
5 "Setting default environment")
4161 (dolist (item (reverse
4162 (append `(,(tramp-get-remote-locale vec
))
4163 (copy-sequence tramp-remote-process-environment
))))
4164 (setq item
(split-string item
"=" 'omit
))
4165 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
4166 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
4167 (push (format "%s %s" (car item
) (cdr item
)) vars
)
4168 (push (car item
) unset
)))
4173 "while read var val; do export $var=\"$val\"; done <<'%s'\n%s\n%s"
4174 tramp-end-of-heredoc
4175 (mapconcat 'identity vars
"\n")
4176 tramp-end-of-heredoc
)
4180 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
)))))
4182 ;; Old text from documentation of tramp-methods:
4183 ;; Using a uuencode/uudecode inline method is discouraged, please use one
4184 ;; of the base64 methods instead since base64 encoding is much more
4185 ;; reliable and the commands are more standardized between the different
4186 ;; Unix versions. But if you can't use base64 for some reason, please
4187 ;; note that the default uudecode command does not work well for some
4188 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
4189 ;; the following command for uudecode:
4191 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4193 ;; For Irix, no solution is known yet.
4195 (autoload 'uudecode-decode-region
"uudecode")
4197 (defconst tramp-local-coding-commands
4198 `((b64 base64-encode-region base64-decode-region
)
4199 (uu tramp-uuencode-region uudecode-decode-region
)
4200 (pack ,(format tramp-perl-pack
"perl") ,(format tramp-perl-unpack
"perl")))
4201 "List of local coding commands for inline transfer.
4202 Each item is a list that looks like this:
4204 \(FORMAT ENCODING DECODING)
4206 FORMAT is symbol describing the encoding/decoding format. It can be
4207 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4209 ENCODING and DECODING can be strings, giving commands, or symbols,
4210 giving functions. If they are strings, then they can contain
4211 the \"%s\" format specifier. If that specifier is present, the input
4212 file name will be put into the command line at that spot. If the
4213 specifier is not present, the input should be read from standard
4216 If they are functions, they will be called with two arguments, start
4217 and end of region, and are expected to replace the region contents
4218 with the encoded or decoded results, respectively.")
4220 (defconst tramp-remote-coding-commands
4221 `((b64 "base64" "base64 -d -i")
4222 ;; "-i" is more robust with older base64 from GNU coreutils.
4223 ;; However, I don't know whether all base64 versions do supports
4225 (b64 "base64" "base64 -d")
4226 (b64 "openssl enc -base64" "openssl enc -d -base64")
4227 (b64 "mimencode -b" "mimencode -u -b")
4228 (b64 "mmencode -b" "mmencode -u -b")
4229 (b64 "recode data..base64" "recode base64..data")
4230 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
4231 (b64 tramp-perl-encode tramp-perl-decode
)
4232 ;; This is painful slow, so we put it on the end.
4233 (b64 tramp-awk-encode tramp-awk-decode
,tramp-awk-coding-test
)
4234 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
4235 (uu "uuencode xxx" "uudecode -o -")
4236 (uu "uuencode xxx" "uudecode -p")
4237 (uu "uuencode xxx" tramp-uudecode
)
4238 (pack tramp-perl-pack tramp-perl-unpack
))
4239 "List of remote coding commands for inline transfer.
4240 Each item is a list that looks like this:
4242 \(FORMAT ENCODING DECODING [TEST])
4244 FORMAT is a symbol describing the encoding/decoding format. It can be
4245 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4247 ENCODING and DECODING can be strings, giving commands, or symbols,
4248 giving variables. If they are strings, then they can contain
4249 the \"%s\" format specifier. If that specifier is present, the input
4250 file name will be put into the command line at that spot. If the
4251 specifier is not present, the input should be read from standard
4254 If they are variables, this variable is a string containing a
4255 Perl or Shell implementation for this functionality. This
4256 program will be transferred to the remote host, and it is
4257 available as shell function with the same name. A \"%t\" format
4258 specifier in the variable value denotes a temporary file.
4260 The optional TEST command can be used for further tests, whether
4261 ENCODING and DECODING are applicable.")
4263 (defun tramp-find-inline-encoding (vec)
4264 "Find an inline transfer encoding that works.
4265 Goes through the list `tramp-local-coding-commands' and
4266 `tramp-remote-coding-commands'."
4268 (let ((local-commands tramp-local-coding-commands
)
4270 (p (tramp-get-connection-process vec
))
4271 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found
)
4272 (while (and local-commands
(not found
))
4273 (setq litem
(pop local-commands
))
4274 (catch 'wont-work-local
4275 (let ((format (nth 0 litem
))
4276 (remote-commands tramp-remote-coding-commands
))
4277 (setq loc-enc
(nth 1 litem
))
4278 (setq loc-dec
(nth 2 litem
))
4279 ;; If the local encoder or decoder is a string, the
4280 ;; corresponding command has to work locally.
4281 (if (not (stringp loc-enc
))
4283 vec
5 "Checking local encoding function `%s'" loc-enc
)
4285 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
4286 (unless (zerop (tramp-call-local-coding-command
4288 (throw 'wont-work-local nil
)))
4289 (if (not (stringp loc-dec
))
4291 vec
5 "Checking local decoding function `%s'" loc-dec
)
4293 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
4294 (unless (zerop (tramp-call-local-coding-command
4296 (throw 'wont-work-local nil
)))
4297 ;; Search for remote coding commands with the same format
4298 (while (and remote-commands
(not found
))
4299 (setq ritem
(pop remote-commands
))
4300 (catch 'wont-work-remote
4301 (when (equal format
(nth 0 ritem
))
4302 (setq rem-enc
(nth 1 ritem
))
4303 (setq rem-dec
(nth 2 ritem
))
4304 (setq rem-test
(nth 3 ritem
))
4305 ;; Check the remote test command if exists.
4306 (when (stringp rem-test
)
4308 vec
5 "Checking remote test command `%s'" rem-test
)
4309 (unless (tramp-send-command-and-check vec rem-test t
)
4310 (throw 'wont-work-remote nil
)))
4311 ;; Check if remote perl exists when necessary.
4312 (when (and (symbolp rem-enc
)
4313 (string-match "perl" (symbol-name rem-enc
))
4314 (not (tramp-get-remote-perl vec
)))
4315 (throw 'wont-work-remote nil
))
4316 ;; Check if remote encoding and decoding commands can be
4317 ;; called remotely with null input and output. This makes
4318 ;; sure there are no syntax errors and the command is really
4319 ;; found. Note that we do not redirect stdout to /dev/null,
4320 ;; for two reasons: when checking the decoding command, we
4321 ;; actually check the output it gives. And also, when
4322 ;; redirecting "mimencode" output to /dev/null, then as root
4323 ;; it might change the permissions of /dev/null!
4324 (when (not (stringp rem-enc
))
4325 (let ((name (symbol-name rem-enc
)))
4326 (while (string-match (regexp-quote "-") name
)
4327 (setq name
(replace-match "_" nil t name
)))
4328 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
4329 (setq rem-enc name
)))
4332 "Checking remote encoding command `%s' for sanity" rem-enc
)
4333 (unless (tramp-send-command-and-check
4334 vec
(format "%s </dev/null" rem-enc
) t
)
4335 (throw 'wont-work-remote nil
))
4337 (when (not (stringp rem-dec
))
4338 (let ((name (symbol-name rem-dec
))
4339 (value (symbol-value rem-dec
))
4341 (while (string-match (regexp-quote "-") name
)
4342 (setq name
(replace-match "_" nil t name
)))
4343 (when (string-match "\\(^\\|[^%]\\)%t" value
)
4347 tramp-temp-name-prefix
4348 (tramp-get-remote-tmpdir vec
)))
4354 (file-remote-p tmpfile
'localname
)))))
4355 (tramp-maybe-send-script vec value name
)
4356 (setq rem-dec name
)))
4359 "Checking remote decoding command `%s' for sanity" rem-dec
)
4360 (unless (tramp-send-command-and-check
4362 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
4364 (throw 'wont-work-remote nil
))
4366 (with-current-buffer (tramp-get-buffer vec
)
4367 (goto-char (point-min))
4368 (unless (looking-at (regexp-quote magic
))
4369 (throw 'wont-work-remote nil
)))
4371 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4372 (setq rem-enc
(nth 1 ritem
))
4373 (setq rem-dec
(nth 2 ritem
))
4374 (setq found t
)))))))
4377 ;; Set connection properties. Since the commands are risky
4378 ;; (due to output direction), we cache them in the process cache.
4379 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
4380 (tramp-set-connection-property p
"local-encoding" loc-enc
)
4381 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
4382 (tramp-set-connection-property p
"local-decoding" loc-dec
)
4383 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
4384 (tramp-set-connection-property p
"remote-encoding" rem-enc
)
4385 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
4386 (tramp-set-connection-property p
"remote-decoding" rem-dec
)))))
4388 (defun tramp-call-local-coding-command (cmd input output
)
4389 "Call the local encoding or decoding command.
4390 If CMD contains \"%s\", provide input file INPUT there in command.
4391 Otherwise, INPUT is passed via standard input.
4392 INPUT can also be nil which means `/dev/null'.
4393 OUTPUT can be a string (which specifies a file name), or t (which
4394 means standard output and thus the current buffer), or nil (which
4397 nil tramp-encoding-shell
4398 (when (and input
(not (string-match "%s" cmd
))) input
)
4399 (if (eq output t
) t nil
)
4401 tramp-encoding-command-switch
4403 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4404 (if (stringp output
) (concat " >" output
) ""))))
4406 (defconst tramp-inline-compress-commands
4407 '(("gzip" "gzip -d")
4408 ("bzip2" "bzip2 -d")
4410 ("compress" "compress -d"))
4411 "List of compress and decompress commands for inline transfer.
4412 Each item is a list that looks like this:
4414 \(COMPRESS DECOMPRESS)
4416 COMPRESS or DECOMPRESS are strings with the respective commands.")
4418 (defun tramp-find-inline-compress (vec)
4419 "Find an inline transfer compress command that works.
4420 Goes through the list `tramp-inline-compress-commands'."
4422 (let ((commands tramp-inline-compress-commands
)
4424 (p (tramp-get-connection-process vec
))
4425 item compress decompress found
)
4426 (while (and commands
(not found
))
4428 (setq item
(pop commands
)
4429 compress
(nth 0 item
)
4430 decompress
(nth 1 item
))
4433 "Checking local compress commands `%s', `%s' for sanity"
4434 compress decompress
)
4437 (tramp-call-local-coding-command
4439 ;; Windows shells need the program file name after
4440 ;; the pipe symbol be quoted if they use forward
4441 ;; slashes as directory separators.
4442 (if (memq system-type
'(windows-nt))
4443 "echo %s | \"%s\" | \"%s\""
4444 "echo %s | %s | %s")
4445 magic compress decompress
) nil nil
))
4449 "Checking remote compress commands `%s', `%s' for sanity"
4450 compress decompress
)
4451 (unless (tramp-send-command-and-check
4452 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4456 ;; Did we find something?
4459 ;; Set connection properties. Since the commands are
4460 ;; risky (due to output direction), we cache them in the
4463 vec
5 "Using inline transfer compress command `%s'" compress
)
4464 (tramp-set-connection-property p
"inline-compress" compress
)
4466 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4467 (tramp-set-connection-property p
"inline-decompress" decompress
))
4469 (tramp-set-connection-property p
"inline-compress" nil
)
4470 (tramp-set-connection-property p
"inline-decompress" nil
)
4472 vec
2 "Couldn't find an inline transfer compress command")))))
4474 (defun tramp-compute-multi-hops (vec)
4475 "Expands VEC according to `tramp-default-proxies-alist'."
4476 (let ((target-alist `(,vec
))
4477 (hops (or (tramp-file-name-hop vec
) ""))
4481 ;; Ad-hoc proxy definitions.
4482 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp
'omit
)))
4483 (let ((user (tramp-file-name-user item
))
4484 (host (tramp-file-name-host item
))
4486 tramp-prefix-format proxy tramp-postfix-host-format
)))
4488 vec
5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4489 (and (stringp host
) (regexp-quote host
))
4490 (and (stringp user
) (regexp-quote user
))
4494 'tramp-default-proxies-alist
4495 (list (and (stringp host
) (regexp-quote host
))
4496 (and (stringp user
) (regexp-quote user
))
4498 (setq item
(tramp-dissect-file-name proxy
))))
4499 ;; Save the new value.
4500 (when (and hops tramp-save-ad-hoc-proxies
)
4501 (customize-save-variable
4502 'tramp-default-proxies-alist tramp-default-proxies-alist
))
4504 ;; Look for proxy hosts to be passed.
4505 (setq choices tramp-default-proxies-alist
)
4507 (setq item
(pop choices
)
4508 proxy
(eval (nth 2 item
)))
4511 (string-match (or (eval (nth 0 item
)) "")
4512 (or (tramp-file-name-host (car target-alist
)) ""))
4514 (string-match (or (eval (nth 1 item
)) "")
4515 (or (tramp-file-name-user (car target-alist
)) "")))
4517 ;; No more hops needed.
4519 ;; Replace placeholders.
4524 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4525 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4526 (with-parsed-tramp-file-name proxy l
4528 (push l target-alist
)
4529 ;; Start next search.
4530 (setq choices tramp-default-proxies-alist
)))))
4532 ;; Foreign and out-of-band methods are not supported for multi-hops.
4533 (when (cdr target-alist
)
4534 (setq choices target-alist
)
4535 (while (setq item
(pop choices
))
4536 (when (or (not (tramp-get-method-parameter item
'tramp-login-program
))
4537 (tramp-get-method-parameter item
'tramp-copy-program
))
4540 "Method `%s' is not supported for multi-hops."
4541 (tramp-file-name-method item
)))))
4543 ;; In case the host name is not used for the remote shell
4544 ;; command, the user could be misguided by applying a random
4546 (let* ((v (car target-alist
))
4547 (method (tramp-file-name-method v
))
4548 (host (tramp-file-name-host v
)))
4551 ;; There are multi-hops.
4553 ;; The host name is used for the remote shell command.
4554 (member '("%h") (tramp-get-method-parameter v
'tramp-login-args
))
4555 ;; The host is local. We cannot use `tramp-local-host-p'
4556 ;; here, because it opens a connection as well.
4557 (string-match tramp-local-host-regexp host
))
4560 "Host `%s' looks like a remote host, `%s' can only use the local host"
4566 (defun tramp-ssh-controlmaster-options (vec)
4567 "Return the Control* arguments of the local ssh."
4569 ;; No options to be computed.
4570 ((or (null tramp-use-ssh-controlmaster-options
)
4571 (null (assoc "%c" (tramp-get-method-parameter vec
'tramp-login-args
))))
4574 ;; There is already a value to be used.
4575 ((stringp tramp-ssh-controlmaster-options
) tramp-ssh-controlmaster-options
)
4577 ;; Determine the options.
4578 (t (setq tramp-ssh-controlmaster-options
"")
4579 (let ((case-fold-search t
))
4581 (when (executable-find "ssh")
4583 (tramp-call-process vec
"ssh" nil t nil
"-o" "ControlMaster")
4584 (goto-char (point-min))
4585 (when (search-forward-regexp "missing.+argument" nil t
)
4586 (setq tramp-ssh-controlmaster-options
"-o ControlMaster=auto")))
4587 (unless (zerop (length tramp-ssh-controlmaster-options
))
4589 ;; We use a non-existing IP address, in order to avoid
4590 ;; useless connections, and DNS timeouts.
4592 vec
"ssh" nil t nil
"-o" "ControlPath=%C" "0.0.0.1")
4593 (goto-char (point-min))
4594 (setq tramp-ssh-controlmaster-options
4595 (concat tramp-ssh-controlmaster-options
4596 (if (search-forward-regexp "unknown.+key" nil t
)
4597 " -o ControlPath='tramp.%%r@%%h:%%p'"
4598 " -o ControlPath='tramp.%%C'"))))
4600 (tramp-call-process vec
"ssh" nil t nil
"-o" "ControlPersist")
4601 (goto-char (point-min))
4602 (when (search-forward-regexp "missing.+argument" nil t
)
4603 (setq tramp-ssh-controlmaster-options
4604 (concat tramp-ssh-controlmaster-options
4605 " -o ControlPersist=no"))))))))
4606 tramp-ssh-controlmaster-options
)))
4608 (defun tramp-maybe-open-connection (vec)
4609 "Maybe open a connection VEC.
4610 Does not do anything if a connection is already open, but re-opens the
4611 connection if a previous connection has died for some reason."
4612 (tramp-check-proper-method-and-host vec
)
4614 (let ((p (tramp-get-connection-process vec
))
4615 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4616 (process-environment (copy-sequence process-environment
))
4617 (pos (with-current-buffer (tramp-get-connection-buffer vec
) (point))))
4619 ;; If Tramp opens the same connection within a short time frame,
4620 ;; there is a problem. We shall signal this.
4621 (unless (or (tramp-compat-process-live-p p
)
4622 (not (equal (butlast (append vec nil
) 2)
4623 (car tramp-current-connection
)))
4625 (current-time) (cdr tramp-current-connection
))
4626 (or tramp-connection-min-time-diff
0)))
4627 (throw 'suppress
'suppress
))
4629 ;; If too much time has passed since last command was sent, look
4630 ;; whether process is still alive. If it isn't, kill it. When
4631 ;; using ssh, it can sometimes happen that the remote end has hung
4632 ;; up but the local ssh client doesn't recognize this until it
4633 ;; tries to send some data to the remote end. So that's why we
4634 ;; try to send a command from time to time, then look again
4635 ;; whether the process is really alive.
4637 (when (and (> (tramp-time-diff
4639 (tramp-get-connection-property
4640 p
"last-cmd-time" '(0 0 0)))
4642 (tramp-compat-process-live-p p
))
4643 (tramp-send-command vec
"echo are you awake" t t
)
4644 (unless (and (tramp-compat-process-live-p p
)
4645 (tramp-wait-for-output p
10))
4646 ;; The error will be caught locally.
4647 (tramp-error vec
'file-error
"Awake did fail")))
4649 (tramp-cleanup-connection vec t
)
4652 ;; New connection must be opened.
4654 (unless (tramp-compat-process-live-p p
)
4656 ;; If `non-essential' is non-nil, don't reopen a new connection.
4657 ;; This variable has been introduced with Emacs 24.1.
4658 (when (and (boundp 'non-essential
) (symbol-value 'non-essential
))
4659 (throw 'non-essential
'non-essential
))
4661 (with-tramp-progress-reporter
4663 (if (zerop (length (tramp-file-name-user vec
)))
4664 (format "Opening connection for %s using %s"
4665 (tramp-file-name-host vec
)
4666 (tramp-file-name-method vec
))
4667 (format "Opening connection for %s@%s using %s"
4668 (tramp-file-name-user vec
)
4669 (tramp-file-name-host vec
)
4670 (tramp-file-name-method vec
)))
4672 (catch 'uname-changed
4673 ;; Start new process.
4674 (when (and p
(processp p
))
4676 (setenv "TERM" tramp-terminal-type
)
4677 (setenv "LC_ALL" (tramp-get-local-locale vec
))
4678 (if (stringp tramp-histfile-override
)
4679 (setenv "HISTFILE" tramp-histfile-override
)
4680 (if tramp-histfile-override
4683 (setenv "HISTFILESIZE" "0")
4684 (setenv "HISTSIZE" "0"))))
4685 (setenv "PROMPT_COMMAND")
4686 (setenv "PS1" tramp-initial-end-of-output
)
4687 (unless (stringp tramp-encoding-shell
)
4688 (tramp-error vec
'file-error
"`tramp-encoding-shell' not set"))
4689 (let* ((target-alist (tramp-compute-multi-hops vec
))
4690 ;; We will apply `tramp-ssh-controlmaster-options'
4691 ;; only for the first hop.
4692 (options (tramp-ssh-controlmaster-options vec
))
4693 (process-connection-type tramp-process-connection-type
)
4694 (process-adaptive-read-buffering nil
)
4695 ;; There are unfortunate settings for "cmdproxy" on
4697 (process-coding-system-alist nil
)
4698 (coding-system-for-read nil
)
4699 ;; This must be done in order to avoid our file
4701 (p (let ((default-directory
4702 (tramp-compat-temporary-file-directory)))
4705 (tramp-get-connection-name vec
)
4706 (tramp-get-connection-buffer vec
)
4707 (if tramp-encoding-command-interactive
4708 (list tramp-encoding-shell
4709 tramp-encoding-command-interactive
)
4710 (list tramp-encoding-shell
))))))
4712 ;; Set sentinel and query flag.
4713 (tramp-set-connection-property p
"vector" vec
)
4714 (set-process-sentinel p
'tramp-process-sentinel
)
4715 (set-process-query-on-exit-flag p nil
)
4716 (setq tramp-current-connection
4717 (cons (butlast (append vec nil
) 2) (current-time))
4718 tramp-current-host
(system-name))
4721 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4723 ;; Check whether process is alive.
4724 (tramp-barf-if-no-shell-prompt
4726 "Couldn't find local shell prompt for %s" tramp-encoding-shell
)
4728 ;; Now do all the connections as specified.
4730 (let* ((hop (car target-alist
))
4731 (l-method (tramp-file-name-method hop
))
4732 (l-user (tramp-file-name-user hop
))
4733 (l-host (tramp-file-name-host hop
))
4736 (tramp-get-method-parameter hop
'tramp-login-program
))
4738 (tramp-get-method-parameter hop
'tramp-login-args
))
4740 (tramp-get-method-parameter hop
'tramp-login-env
))
4742 (tramp-get-method-parameter hop
'tramp-async-args
))
4744 (tramp-get-method-parameter
4745 hop
'tramp-connection-timeout
))
4746 (command login-program
)
4747 ;; We don't create the temporary file. In
4748 ;; fact, it is just a prefix for the
4749 ;; ControlPath option of ssh; the real
4750 ;; temporary file has another name, and it is
4751 ;; created and protected by ssh. It is also
4752 ;; removed by ssh when the connection is
4753 ;; closed. The temporary file name is cached
4754 ;; in the main connection process, therefore
4755 ;; we cannot use `tramp-get-connection-process'.
4757 (with-tramp-connection-property
4758 (get-process (tramp-buffer-name vec
)) "temp-file"
4761 tramp-temp-name-prefix
4762 (tramp-compat-temporary-file-directory)))))
4765 ;; Add arguments for asynchronous processes.
4766 (when (and process-name async-args
)
4767 (setq login-args
(append async-args login-args
)))
4769 ;; Check for port number. Until now, there's no
4770 ;; need for handling like method, user, host.
4771 (when (string-match tramp-host-with-port-regexp l-host
)
4772 (setq l-port
(match-string 2 l-host
)
4773 l-host
(match-string 1 l-host
)))
4775 ;; Check, whether there is a restricted shell.
4776 (dolist (elt tramp-restricted-shell-hosts-alist
)
4777 (when (string-match elt tramp-current-host
)
4780 ;; Set variables for computing the prompt for
4781 ;; reading password.
4782 (setq tramp-current-method l-method
4783 tramp-current-user l-user
4784 tramp-current-host l-host
)
4786 ;; Add login environment.
4792 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4793 (unless (member "" x
) (mapconcat 'identity x
" ")))
4800 (tramp-shell-quote-argument (pop login-env
))
4802 (setq command
(concat "env " command
)))
4804 ;; Replace `login-args' place holders.
4806 l-host
(or l-host
"")
4807 l-user
(or l-user
"")
4808 l-port
(or l-port
"")
4809 spec
(format-spec-make ?t tmpfile
)
4810 options
(format-spec options spec
)
4811 spec
(format-spec-make
4812 ?h l-host ?u l-user ?p l-port ?c options
)
4815 ;; We do not want to see the trailing local
4816 ;; prompt in `start-file-process'.
4817 (unless r-shell
"exec ")
4821 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4822 (unless (member "" x
) (mapconcat 'identity x
" ")))
4824 ;; Local shell could be a Windows COMSPEC. It
4825 ;; doesn't know the ";" syntax, but we must exit
4826 ;; always for `start-file-process'. It could
4827 ;; also be a restricted shell, which does not
4829 (when r-shell
" && exit || exit")))
4831 ;; Send the command.
4832 (tramp-message vec
3 "Sending command `%s'" command
)
4833 (tramp-send-command vec command t t
)
4834 (tramp-process-actions
4837 pos
(with-current-buffer (process-buffer p
) (point-max)))
4838 tramp-actions-before-shell
4839 (or connection-timeout tramp-connection-timeout
))
4841 vec
3 "Found remote shell prompt on `%s'" l-host
))
4844 target-alist
(cdr target-alist
)))
4846 ;; Set connection-local variables.
4847 (tramp-set-connection-local-variables vec
)
4849 ;; Make initial shell settings.
4850 (tramp-open-connection-setup-interactive-shell p vec
)
4852 ;; Mark it as connected.
4853 (tramp-set-connection-property p
"connected" t
)))))
4855 ;; Cleanup, and propagate the signal.
4857 (tramp-cleanup-connection vec t
)
4858 (signal (car err
) (cdr err
))))))
4860 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4861 "Send the COMMAND to connection VEC.
4862 Erases temporary buffer before sending the command. If optional
4863 arg NEVEROPEN is non-nil, never try to open the connection. This
4864 is meant to be used from `tramp-maybe-open-connection' only. The
4865 function waits for output unless NOOUTPUT is set."
4866 (unless neveropen
(tramp-maybe-open-connection vec
))
4867 (let ((p (tramp-get-connection-process vec
)))
4868 (when (tramp-get-connection-property p
"remote-echo" nil
)
4869 ;; We mark the command string that it can be erased in the output buffer.
4870 (tramp-set-connection-property p
"check-remote-echo" t
)
4871 ;; If we put `tramp-echo-mark' after a trailing newline (which
4872 ;; is assumed to be unquoted) `tramp-send-string' doesn't see
4873 ;; that newline and adds `tramp-rsh-end-of-line' right after
4874 ;; `tramp-echo-mark', so the remote shell sees two consecutive
4875 ;; trailing line endings and sends two prompts after executing
4876 ;; the command, which confuses `tramp-wait-for-output'.
4877 (when (and (not (string= command
""))
4878 (string-equal (substring command -
1) "\n"))
4879 (setq command
(substring command
0 -
1)))
4880 ;; No need to restore a trailing newline here since `tramp-send-string'
4881 ;; makes sure that the string ends in `tramp-rsh-end-of-line', anyway.
4882 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4883 ;; Send the command.
4884 (tramp-message vec
6 "%s" command
)
4885 (tramp-send-string vec command
)
4886 (unless nooutput
(tramp-wait-for-output p
))))
4888 (defun tramp-wait-for-output (proc &optional timeout
)
4889 "Wait for output from remote command."
4890 (unless (buffer-live-p (process-buffer proc
))
4891 (delete-process proc
)
4892 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4893 (with-current-buffer (process-buffer proc
)
4894 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4895 ;; be leading escape sequences, which must be ignored.
4896 ;; Busyboxes built with the EDITING_ASK_TERMINAL config
4897 ;; option send also escape sequences, which must be
4899 (regexp (format "[^#$\n]*%s\\(%s\\)?\r?$"
4900 (regexp-quote tramp-end-of-output
)
4901 tramp-device-escape-sequence-regexp
))
4902 ;; Sometimes, the commands do not return a newline but a
4903 ;; null byte before the shell prompt, for example "git
4904 ;; ls-files -c -z ...".
4905 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4906 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4908 (let (buffer-read-only)
4909 ;; A simple-minded busybox has sent " ^H" sequences.
4911 (goto-char (point-min))
4912 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t
)
4914 (delete-region (point-min) (point)))
4915 ;; Delete the prompt.
4916 (goto-char (point-max))
4917 (re-search-backward regexp nil t
)
4918 (delete-region (point) (point-max)))
4922 "[[Remote prompt `%s' not found in %d secs]]"
4923 tramp-end-of-output timeout
)
4926 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4927 ;; Return value is whether end-of-output sentinel was found.
4930 (defun tramp-send-command-and-check
4931 (vec command
&optional subshell dont-suppress-err
)
4932 "Run COMMAND and check its exit status.
4933 Sends `echo $?' along with the COMMAND for checking the exit status.
4934 If COMMAND is nil, just sends `echo $?'. Returns t if the exit
4935 status is 0, and nil otherwise.
4937 If the optional argument SUBSHELL is non-nil, the command is
4938 executed in a subshell, ie surrounded by parentheses. If
4939 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4942 (concat (if subshell
"( " "")
4944 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4945 "echo tramp_exit_status $?"
4946 (if subshell
" )" "")))
4947 (with-current-buffer (tramp-get-connection-buffer vec
)
4948 (goto-char (point-max))
4949 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4951 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4952 (skip-chars-forward "^ ")
4954 (zerop (read (current-buffer)))
4955 (let (buffer-read-only)
4956 (delete-region (match-beginning 0) (point-max))))))
4958 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4959 "Run COMMAND, check exit status, throw error if exit status not okay.
4960 Similar to `tramp-send-command-and-check' but accepts two more arguments
4961 FMT and ARGS which are passed to `error'."
4962 (or (tramp-send-command-and-check vec command
)
4963 (apply 'tramp-error vec
'file-error fmt args
)))
4965 (defun tramp-send-command-and-read (vec command
&optional noerror marker
)
4966 "Run COMMAND and return the output, which must be a Lisp expression.
4967 If MARKER is a regexp, read the output after that string.
4968 In case there is no valid Lisp expression and NOERROR is nil, it
4971 (tramp-send-command-and-check vec command
)
4972 (tramp-barf-unless-okay
4973 vec command
"`%s' returns with error" command
))
4974 (with-current-buffer (tramp-get-connection-buffer vec
)
4975 (goto-char (point-min))
4977 (when (stringp marker
)
4979 (re-search-forward marker
)
4980 (error (unless noerror
4983 "`%s' does not return the marker `%s': `%s'"
4984 command marker
(buffer-string))))))
4985 ;; Read the expression.
4987 (prog1 (read (current-buffer))
4989 (when (re-search-forward "\\S-" (point-at-eol) t
)
4991 (error (unless noerror
4994 "`%s' does not return a valid Lisp expression: `%s'"
4995 command
(buffer-string))))))))
4997 (defun tramp-convert-file-attributes (vec attr
)
4998 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4999 Convert file mode bits to string and set virtual device number.
5002 ;; Remove color escape sequences from symlink.
5003 (when (stringp (car attr
))
5004 (while (string-match tramp-display-escape-sequence-regexp
(car attr
))
5005 (setcar attr
(replace-match "" nil nil
(car attr
)))))
5006 ;; Convert uid and gid. Use `tramp-unknown-id-integer' as
5007 ;; indication of unusable value.
5008 (when (and (numberp (nth 2 attr
)) (< (nth 2 attr
) 0))
5009 (setcar (nthcdr 2 attr
) tramp-unknown-id-integer
))
5010 (when (and (floatp (nth 2 attr
))
5011 (<= (nth 2 attr
) most-positive-fixnum
))
5012 (setcar (nthcdr 2 attr
) (round (nth 2 attr
))))
5013 (when (and (numberp (nth 3 attr
)) (< (nth 3 attr
) 0))
5014 (setcar (nthcdr 3 attr
) tramp-unknown-id-integer
))
5015 (when (and (floatp (nth 3 attr
))
5016 (<= (nth 3 attr
) most-positive-fixnum
))
5017 (setcar (nthcdr 3 attr
) (round (nth 3 attr
))))
5018 ;; Convert last access time.
5019 (unless (listp (nth 4 attr
))
5020 (setcar (nthcdr 4 attr
)
5021 (list (floor (nth 4 attr
) 65536)
5022 (floor (mod (nth 4 attr
) 65536)))))
5023 ;; Convert last modification time.
5024 (unless (listp (nth 5 attr
))
5025 (setcar (nthcdr 5 attr
)
5026 (list (floor (nth 5 attr
) 65536)
5027 (floor (mod (nth 5 attr
) 65536)))))
5028 ;; Convert last status change time.
5029 (unless (listp (nth 6 attr
))
5030 (setcar (nthcdr 6 attr
)
5031 (list (floor (nth 6 attr
) 65536)
5032 (floor (mod (nth 6 attr
) 65536)))))
5033 ;; Convert file size.
5034 (when (< (nth 7 attr
) 0)
5035 (setcar (nthcdr 7 attr
) -
1))
5036 (when (and (floatp (nth 7 attr
))
5037 (<= (nth 7 attr
) most-positive-fixnum
))
5038 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
5039 ;; Convert file mode bits to string.
5040 (unless (stringp (nth 8 attr
))
5041 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
5042 (when (stringp (car attr
))
5043 (aset (nth 8 attr
) 0 ?l
)))
5044 ;; Convert directory indication bit.
5045 (when (string-match "^d" (nth 8 attr
))
5047 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
5048 (when (consp (car attr
))
5049 (if (and (stringp (caar attr
))
5050 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
5051 (setcar attr
(match-string 1 (caar attr
)))
5053 ;; Set file's gid change bit.
5054 (setcar (nthcdr 9 attr
)
5055 (if (numberp (nth 3 attr
))
5056 (not (= (nth 3 attr
)
5057 (tramp-get-remote-gid vec
'integer
)))
5060 (tramp-get-remote-gid vec
'string
)))))
5062 (unless (listp (nth 10 attr
))
5063 (setcar (nthcdr 10 attr
)
5065 (cons (floor (nth 10 attr
) 65536)
5066 (floor (mod (nth 10 attr
) 65536)))
5067 ;; Inodes can be incredible huge. We must hide this.
5068 (error (tramp-get-inode vec
)))))
5069 ;; Set virtual device number.
5070 (setcar (nthcdr 11 attr
)
5071 (tramp-get-device vec
))
5074 (defun tramp-shell-case-fold (string)
5075 "Converts STRING to shell glob pattern which ignores case."
5078 (if (equal (downcase c
) (upcase c
))
5080 (format "[%c%c]" (downcase c
) (upcase c
))))
5084 (defun tramp-make-copy-program-file-name (vec)
5085 "Create a file name suitable for `scp', `pscp', or `nc' and workalikes."
5086 (let ((method (tramp-file-name-method vec
))
5087 (user (tramp-file-name-user vec
))
5088 (host (tramp-file-name-real-host vec
))
5090 (directory-file-name (tramp-file-name-unquote-localname vec
))))
5091 (when (string-match tramp-ipv6-regexp host
)
5092 (setq host
(format "[%s]" host
)))
5093 (unless (string-match "ftp$" method
)
5094 (setq localname
(tramp-shell-quote-argument localname
)))
5096 ((tramp-get-method-parameter vec
'tramp-remote-copy-program
)
5098 ((not (zerop (length user
)))
5099 (format "%s@%s:%s" user host
(shell-quote-argument localname
)))
5100 (t (format "%s:%s" host
(shell-quote-argument localname
))))))
5102 (defun tramp-method-out-of-band-p (vec size
)
5103 "Return t if this is an out-of-band method, nil otherwise."
5105 ;; It shall be an out-of-band method.
5106 (tramp-get-method-parameter vec
'tramp-copy-program
)
5107 ;; There must be a size, otherwise the file doesn't exist.
5109 ;; Either the file size is large enough, or (in rare cases) there
5110 ;; does not exist a remote encoding.
5111 (or (null tramp-copy-size-limit
)
5112 (> size tramp-copy-size-limit
)
5113 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
5115 ;; Variables local to connection.
5117 (defun tramp-get-remote-path (vec)
5118 "Compile list of remote directories for $PATH.
5119 Nonexistent directories are removed from spec."
5120 (with-tramp-connection-property
5121 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5122 ;; cache the result for the session only. Otherwise, the result
5123 ;; is cached persistently.
5124 (if (memq 'tramp-own-remote-path tramp-remote-path
)
5125 (tramp-get-connection-process vec
)
5128 (let* ((remote-path (copy-tree tramp-remote-path
))
5129 (elt1 (memq 'tramp-default-remote-path remote-path
))
5130 (elt2 (memq 'tramp-own-remote-path remote-path
))
5131 (default-remote-path
5134 (tramp-send-command-and-read
5135 vec
"echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror
)
5136 ;; Default if "getconf" is not available.
5140 "`getconf PATH' not successful, using default value \"%s\"."
5144 ;; The login shell could return more than just the $PATH
5145 ;; string. So we use `tramp-end-of-heredoc' as marker.
5148 (tramp-send-command-and-read
5151 "%s %s %s 'echo %s \\\"$PATH\\\"'"
5152 (tramp-get-method-parameter vec
'tramp-remote-shell
)
5155 (tramp-get-method-parameter vec
'tramp-remote-shell-login
)
5159 (tramp-get-method-parameter vec
'tramp-remote-shell-args
)
5161 (tramp-shell-quote-argument tramp-end-of-heredoc
))
5162 'noerror
(regexp-quote tramp-end-of-heredoc
))
5165 vec
2 "Could not retrieve `tramp-own-remote-path'")
5168 ;; Replace place holder `tramp-default-remote-path'.
5172 (split-string (or default-remote-path
"") ":" 'omit
)
5174 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
5176 ;; Replace place holder `tramp-own-remote-path'.
5180 (split-string (or own-remote-path
"") ":" 'omit
)
5182 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
5184 ;; Remove double entries.
5185 (setq elt1 remote-path
)
5187 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
5189 (setq elt1
(cdr elt1
)))
5191 ;; Remove non-existing directories.
5199 (tramp-make-tramp-file-name
5200 (tramp-file-name-method vec
)
5201 (tramp-file-name-user vec
)
5202 (tramp-file-name-host vec
)
5207 (defun tramp-get-remote-locale (vec)
5208 "Determine remote locale, supporting UTF8 if possible."
5209 (with-tramp-connection-property vec
"locale"
5210 (tramp-send-command vec
"locale -a")
5211 (let ((candidates '("en_US.utf8" "C.utf8" "en_US.UTF-8"))
5213 (with-current-buffer (tramp-get-connection-buffer vec
)
5215 (goto-char (point-min))
5216 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates
)))
5218 (setq locale
(car candidates
)
5220 (setq candidates
(cdr candidates
)))))
5222 (format "LC_ALL=%s" (or locale
"C")))))
5224 (defun tramp-get-ls-command (vec)
5225 "Determine remote `ls' command."
5226 (with-tramp-connection-property vec
"ls"
5227 (tramp-message vec
5 "Finding a suitable `ls' command")
5230 (dolist (cmd '("ls" "gnuls" "gls"))
5231 (let ((dl (tramp-get-remote-path vec
))
5233 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
5234 ;; Check parameters. On busybox, "ls" output coloring is
5235 ;; enabled by default sometimes. So we try to disable it
5236 ;; when possible. $LS_COLORING is not supported there.
5237 ;; Some "ls" versions are sensible wrt the order of
5238 ;; arguments, they fail when "-al" is after the
5239 ;; "--color=never" argument (for example on FreeBSD).
5240 (when (tramp-send-command-and-check
5241 vec
(format "%s -lnd /" result
))
5242 (when (tramp-send-command-and-check
5244 "%s --color=never -al /dev/null" result
))
5245 (setq result
(concat result
" --color=never")))
5246 (throw 'ls-found result
))
5247 (setq dl
(cdr dl
))))))
5248 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
5250 (defun tramp-get-ls-command-with-dired (vec)
5251 "Check, whether the remote `ls' command supports the --dired option."
5253 (with-tramp-connection-property vec
"ls-dired"
5254 (tramp-message vec
5 "Checking, whether `ls --dired' works")
5255 ;; Some "ls" versions are sensible wrt the order of arguments,
5256 ;; they fail when "-al" is after the "--dired" argument (for
5257 ;; example on FreeBSD).
5258 (tramp-send-command-and-check
5259 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
5261 (defun tramp-get-ls-command-with-quoting-style (vec)
5262 "Check, whether the remote `ls' command supports the --quoting-style option."
5264 (with-tramp-connection-property vec
"ls-quoting-style"
5265 (tramp-message vec
5 "Checking, whether `ls --quoting-style=shell' works")
5266 (tramp-send-command-and-check
5267 vec
(format "%s --quoting-style=shell -al /dev/null"
5268 (tramp-get-ls-command vec
))))))
5270 (defun tramp-get-ls-command-with-w-option (vec)
5271 "Check, whether the remote `ls' command supports the -w option."
5273 (with-tramp-connection-property vec
"ls-w-option"
5274 (tramp-message vec
5 "Checking, whether `ls -w' works")
5275 ;; Option "-w" is available on BSD systems. No argument is
5276 ;; given, because this could return wrong results in case "ls"
5277 ;; supports the "-w NUM" argument, as for busyboxes.
5278 (tramp-send-command-and-check
5279 vec
(format "%s -alw" (tramp-get-ls-command vec
))))))
5281 (defun tramp-get-test-command (vec)
5282 "Determine remote `test' command."
5283 (with-tramp-connection-property vec
"test"
5284 (tramp-message vec
5 "Finding a suitable `test' command")
5285 (if (tramp-send-command-and-check vec
"test 0")
5287 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
5289 (defun tramp-get-test-nt-command (vec)
5290 "Check, whether the remote `test' command supports the -nt option."
5291 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5292 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5293 ;; for otherwise the shell crashes.
5294 (with-tramp-connection-property vec
"test-nt"
5298 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
5299 (with-current-buffer (tramp-get-buffer vec
)
5300 (goto-char (point-min))
5301 (when (looking-at (regexp-quote tramp-end-of-output
))
5302 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
5307 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5308 (tramp-get-test-command vec
)))
5309 "tramp_test_nt %s %s"))))
5311 (defun tramp-get-file-exists-command (vec)
5312 "Determine remote command for file existing check."
5313 (with-tramp-connection-property vec
"file-exists"
5314 (tramp-message vec
5 "Finding command to check if file exists")
5315 (tramp-find-file-exists-command vec
)))
5317 (defun tramp-get-remote-ln (vec)
5318 "Determine remote `ln' command."
5319 (with-tramp-connection-property vec
"ln"
5320 (tramp-message vec
5 "Finding a suitable `ln' command")
5321 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
5323 (defun tramp-get-remote-perl (vec)
5324 "Determine remote `perl' command."
5325 (with-tramp-connection-property vec
"perl"
5326 (tramp-message vec
5 "Finding a suitable `perl' command")
5328 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
5329 (tramp-find-executable vec
"perl" (tramp-get-remote-path vec
)))))
5330 ;; Perform a basic check.
5332 (null (tramp-send-command-and-check
5333 vec
(format "%s -e 'print \"Hello\n\";'" result
)))
5335 ;; We must check also for some Perl modules.
5337 (with-tramp-connection-property vec
"perl-file-spec"
5338 (tramp-send-command-and-check
5339 vec
(format "%s -e 'use File::Spec;'" result
)))
5340 (with-tramp-connection-property vec
"perl-cwd-realpath"
5341 (tramp-send-command-and-check
5342 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
5345 (defun tramp-get-remote-stat (vec)
5346 "Determine remote `stat' command."
5347 (with-tramp-connection-property vec
"stat"
5348 (tramp-message vec
5 "Finding a suitable `stat' command")
5349 (let ((result (tramp-find-executable
5350 vec
"stat" (tramp-get-remote-path vec
)))
5352 ;; Check whether stat(1) returns usable syntax. "%s" does not
5353 ;; work on older AIX systems. Recent GNU stat versions (8.24?)
5354 ;; use shell quoted format for "%N", we check the boundaries "`"
5355 ;; and "'", therefore. See Bug#23422 in coreutils.
5358 (tramp-send-command-and-read
5359 vec
(format "%s -c '(\"%%N\" %%s)' /" result
) 'noerror
))
5360 (unless (and (listp tmp
) (stringp (car tmp
))
5361 (string-match "^`/'$" (car tmp
))
5362 (integerp (cadr tmp
)))
5366 (defun tramp-get-remote-readlink (vec)
5367 "Determine remote `readlink' command."
5368 (with-tramp-connection-property vec
"readlink"
5369 (tramp-message vec
5 "Finding a suitable `readlink' command")
5370 (let ((result (tramp-find-executable
5371 vec
"readlink" (tramp-get-remote-path vec
))))
5373 (tramp-send-command-and-check
5374 vec
(format "%s --canonicalize-missing /" result
)))
5377 (defun tramp-get-remote-trash (vec)
5378 "Determine remote `trash' command."
5379 (with-tramp-connection-property vec
"trash"
5380 (tramp-message vec
5 "Finding a suitable `trash' command")
5381 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
5383 (defun tramp-get-remote-touch (vec)
5384 "Determine remote `touch' command."
5385 (with-tramp-connection-property vec
"touch"
5386 (tramp-message vec
5 "Finding a suitable `touch' command")
5387 (let ((result (tramp-find-executable
5388 vec
"touch" (tramp-get-remote-path vec
)))
5392 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))))
5393 ;; Busyboxes do support the "-t" option only when they have been
5394 ;; built with the DESKTOP config option. Let's check it.
5396 (tramp-set-connection-property
5398 (tramp-send-command-and-check
5403 (format-time-string "%Y%m%d%H%M.%S")
5404 (file-remote-p tmpfile
'localname
))))
5405 (delete-file tmpfile
))
5408 (defun tramp-get-remote-gvfs-monitor-dir (vec)
5409 "Determine remote `gvfs-monitor-dir' command."
5410 (with-tramp-connection-property vec
"gvfs-monitor-dir"
5411 (tramp-message vec
5 "Finding a suitable `gvfs-monitor-dir' command")
5412 (tramp-find-executable
5413 vec
"gvfs-monitor-dir" (tramp-get-remote-path vec
) t t
)))
5415 (defun tramp-get-remote-inotifywait (vec)
5416 "Determine remote `inotifywait' command."
5417 (with-tramp-connection-property vec
"inotifywait"
5418 (tramp-message vec
5 "Finding a suitable `inotifywait' command")
5419 (tramp-find-executable vec
"inotifywait" (tramp-get-remote-path vec
) t t
)))
5421 (defun tramp-get-remote-id (vec)
5422 "Determine remote `id' command."
5423 (with-tramp-connection-property vec
"id"
5424 (tramp-message vec
5 "Finding POSIX `id' command")
5426 (dolist (cmd '("id" "gid"))
5427 (let ((dl (tramp-get-remote-path vec
))
5429 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
5430 ;; Check POSIX parameter.
5431 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
5432 (throw 'id-found result
))
5433 (setq dl
(cdr dl
))))))))
5435 (defun tramp-get-remote-uid-with-id (vec id-format
)
5436 "Implement `tramp-get-remote-uid' for Tramp files using `id'."
5437 (tramp-send-command-and-read
5439 (format "%s -u%s %s"
5440 (tramp-get-remote-id vec
)
5441 (if (equal id-format
'integer
) "" "n")
5442 (if (equal id-format
'integer
)
5443 "" "| sed -e s/^/\\\"/ -e s/\\$/\\\"/"))))
5445 (defun tramp-get-remote-uid-with-perl (vec id-format
)
5446 "Implement `tramp-get-remote-uid' for Tramp files using a Perl script."
5447 (tramp-send-command-and-read
5449 (format "%s -le '%s'"
5450 (tramp-get-remote-perl vec
)
5451 (if (equal id-format
'integer
)
5453 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5455 (defun tramp-get-remote-python (vec)
5456 "Determine remote `python' command."
5457 (with-tramp-connection-property vec
"python"
5458 (tramp-message vec
5 "Finding a suitable `python' command")
5459 (or (tramp-find-executable vec
"python" (tramp-get-remote-path vec
))
5460 (tramp-find-executable vec
"python2" (tramp-get-remote-path vec
))
5461 (tramp-find-executable vec
"python3" (tramp-get-remote-path vec
)))))
5463 (defun tramp-get-remote-uid-with-python (vec id-format
)
5464 "Implement `tramp-get-remote-uid' for Tramp files using `python'."
5465 (tramp-send-command-and-read
5467 (format "%s -c \"%s\""
5468 (tramp-get-remote-python vec
)
5469 (if (equal id-format
'integer
)
5470 "import os; print (os.getuid())"
5471 "import os, pwd; print ('\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"')"))))
5473 (defun tramp-get-remote-uid (vec id-format
)
5474 "The uid of the remote connection VEC, in ID-FORMAT.
5475 ID-FORMAT valid values are `string' and `integer'."
5476 (with-tramp-connection-property vec
(format "uid-%s" id-format
)
5480 ((tramp-get-remote-id vec
)
5481 (tramp-get-remote-uid-with-id vec id-format
))
5482 ((tramp-get-remote-perl vec
)
5483 (tramp-get-remote-uid-with-perl vec id-format
))
5484 ((tramp-get-remote-python vec
)
5485 (tramp-get-remote-uid-with-python vec id-format
))))))
5486 ;; Ensure there is a valid result.
5488 ((and (equal id-format
'integer
) (not (integerp res
)))
5489 tramp-unknown-id-integer
)
5490 ((and (equal id-format
'string
) (not (stringp res
)))
5491 tramp-unknown-id-string
)
5494 (defun tramp-get-remote-gid-with-id (vec id-format
)
5495 "Implement `tramp-get-remote-gid' for Tramp files using `id'."
5496 (tramp-send-command-and-read
5498 (format "%s -g%s %s"
5499 (tramp-get-remote-id vec
)
5500 (if (equal id-format
'integer
) "" "n")
5501 (if (equal id-format
'integer
)
5502 "" "| sed -e s/^/\\\"/ -e s/\\$/\\\"/"))))
5504 (defun tramp-get-remote-gid-with-perl (vec id-format
)
5505 "Implement `tramp-get-remote-gid' for Tramp files using a Perl script."
5506 (tramp-send-command-and-read
5508 (format "%s -le '%s'"
5509 (tramp-get-remote-perl vec
)
5510 (if (equal id-format
'integer
)
5511 "print ($)=~/(\\d+)/)"
5512 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5514 (defun tramp-get-remote-gid-with-python (vec id-format
)
5515 "Implement `tramp-get-remote-gid' for Tramp files using `python'."
5516 (tramp-send-command-and-read
5518 (format "%s -c \"%s\""
5519 (tramp-get-remote-python vec
)
5520 (if (equal id-format
'integer
)
5521 "import os; print (os.getgid())"
5522 "import os, grp; print ('\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"')"))))
5524 (defun tramp-get-remote-gid (vec id-format
)
5525 "The gid of the remote connection VEC, in ID-FORMAT.
5526 ID-FORMAT valid values are `string' and `integer'."
5527 (with-tramp-connection-property vec
(format "gid-%s" id-format
)
5531 ((tramp-get-remote-id vec
)
5532 (tramp-get-remote-gid-with-id vec id-format
))
5533 ((tramp-get-remote-perl vec
)
5534 (tramp-get-remote-gid-with-perl vec id-format
))
5535 ((tramp-get-remote-python vec
)
5536 (tramp-get-remote-gid-with-python vec id-format
))))))
5537 ;; Ensure there is a valid result.
5539 ((and (equal id-format
'integer
) (not (integerp res
)))
5540 tramp-unknown-id-integer
)
5541 ((and (equal id-format
'string
) (not (stringp res
)))
5542 tramp-unknown-id-string
)
5545 (defun tramp-get-env-with-u-option (vec)
5546 "Check, whether the remote `env' command supports the -u option."
5547 (with-tramp-connection-property vec
"env-u-option"
5548 (tramp-message vec
5 "Checking, whether `env -u' works")
5549 ;; Option "-u" is a GNU extension.
5550 (tramp-send-command-and-check
5551 vec
"env FOO=foo env -u FOO 2>/dev/null | grep -qv FOO" t
)))
5553 ;; Some predefined connection properties.
5554 (defun tramp-get-inline-compress (vec prop size
)
5555 "Return the compress command related to PROP.
5556 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5557 the length of the file to be compressed.
5559 If no corresponding command is found, nil is returned."
5560 (when (and (integerp tramp-inline-compress-start-size
)
5561 (> size tramp-inline-compress-start-size
))
5562 (with-tramp-connection-property (tramp-get-connection-process vec
) prop
5563 (tramp-find-inline-compress vec
)
5564 (tramp-get-connection-property
5565 (tramp-get-connection-process vec
) prop nil
))))
5567 (defun tramp-get-inline-coding (vec prop size
)
5568 "Return the coding command related to PROP.
5569 PROP is either `remote-encoding', `remote-decoding',
5570 `local-encoding' or `local-decoding'.
5572 SIZE is the length of the file to be coded. Depending on SIZE,
5573 compression might be applied.
5575 If no corresponding command is found, nil is returned.
5576 Otherwise, either a string is returned which contains a `%s' mark
5577 to be used for the respective input or output file; or a Lisp
5578 function cell is returned to be applied on a buffer."
5579 ;; We must catch the errors, because we want to return nil, when
5580 ;; no inline coding is found.
5583 (with-tramp-connection-property
5584 (tramp-get-connection-process vec
) prop
5585 (tramp-find-inline-encoding vec
)
5586 (tramp-get-connection-property
5587 (tramp-get-connection-process vec
) prop nil
)))
5588 (prop1 (if (string-match "encoding" prop
)
5589 "inline-compress" "inline-decompress"))
5591 ;; The connection property might have been cached. So we must
5592 ;; send the script to the remote side - maybe.
5593 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
5594 (let ((name (symbol-name coding
)))
5595 (while (string-match (regexp-quote "-") name
)
5596 (setq name
(replace-match "_" nil t name
)))
5597 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
5598 (setq coding name
)))
5600 ;; Check for the `compress' command.
5601 (setq compress
(tramp-get-inline-compress vec prop1 size
))
5602 ;; Return the value.
5604 ((and compress
(symbolp coding
))
5605 (if (string-match "decompress" prop1
)
5608 (let ((coding-system-for-write 'binary
)
5609 (coding-system-for-read 'binary
))
5611 'tramp-call-process-region
,vec
(point-min) (point-max)
5612 (car (split-string ,compress
)) t t nil
5613 (cdr (split-string ,compress
)))))
5615 (let ((coding-system-for-write 'binary
)
5616 (coding-system-for-read 'binary
))
5618 'tramp-call-process-region
,vec beg end
5619 (car (split-string ,compress
)) t t nil
5620 (cdr (split-string ,compress
))))
5621 (,coding
(point-min) (point-max)))))
5624 ((and compress
(string-match "decoding" prop
))
5626 ;; Windows shells need the program file name after
5627 ;; the pipe symbol be quoted if they use forward
5628 ;; slashes as directory separators.
5630 ((and (string-match "local" prop
)
5631 (memq system-type
'(windows-nt)))
5633 ((string-match "local" prop
) "(%s | %s)")
5634 (t "(%s | %s >%%s)"))
5638 ;; Windows shells need the program file name after
5639 ;; the pipe symbol be quoted if they use forward
5640 ;; slashes as directory separators.
5641 (if (and (string-match "local" prop
)
5642 (memq system-type
'(windows-nt)))
5643 "(%s <%%s | \"%s\")"
5646 ((string-match "decoding" prop
)
5648 ((string-match "local" prop
) (format "%s" coding
))
5649 (t (format "%s >%%s" coding
))))
5651 (format "%s <%%s" coding
)))))))
5653 (add-hook 'tramp-unload-hook
5655 (unload-feature 'tramp-sh
'force
)))
5661 ;; * Don't use globbing for directories with many files, as this is
5662 ;; likely to produce long command lines, and some shells choke on
5663 ;; long command lines.
5665 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5666 ;; then look if it's the right version (with `perl -v').
5668 ;; * When editing a remote CVS controlled file as a different user, VC
5669 ;; gets confused about the file locking status. Try to find out why
5670 ;; the workaround doesn't work.
5672 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5673 ;; until the last but one hop via `start-file-process'. Apply it
5674 ;; also for ftp and smb.
5676 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5677 ;; some shell with root privileges, it would be nice if I could
5679 ;; trampclient filename.c
5680 ;; as an editor, and the _current_ shell would connect to an Emacs
5681 ;; server and would be used in an existing non-privileged Emacs
5682 ;; session for doing the editing in question.
5683 ;; That way, I need not tell Emacs my password again and be afraid
5684 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5685 ;; once display a just typed password in the context of a keyboard
5686 ;; sequence prompt for a question immediately following in a shell
5687 ;; script run within Emacs -- nasty).
5688 ;; And if I have some ssh session running to a different computer,
5689 ;; having the possibility of passing a local file there to a local
5690 ;; Emacs session (in case I can arrange for a connection back) would
5692 ;; Likely the corresponding Tramp server should not allow the
5693 ;; equivalent of the emacsclient -eval option in order to make this
5694 ;; reasonably unproblematic. And maybe trampclient should have some
5695 ;; way of passing credentials, like by using an SSL socket or
5696 ;; something. (David Kastrup)
5698 ;; * Reconnect directly to a compliant shell without first going
5699 ;; through the user's default shell. (Pete Forman)
5701 ;; * How can I interrupt the remote process with a signal
5702 ;; (interrupt-process seems not to work)? (Markus Triska)
5704 ;; * Avoid the local shell entirely for starting remote processes. If
5705 ;; so, I think even a signal, when delivered directly to the local
5706 ;; SSH instance, would correctly be propagated to the remote process
5707 ;; automatically; possibly SSH would have to be started with
5708 ;; "-t". (Markus Triska)
5710 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5711 ;; isn't on the remote host. (Mark A. Hershberger)
5713 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5715 ;; * Optimize out-of-band copying when both methods are scp-like (not
5718 ;; * Keep a second connection open for out-of-band methods like scp or
5721 ;; * Implement completion for "/method:user@host:~<abc> TAB".
5723 ;; * I think you could get the best of both worlds by using an
5724 ;; approach similar to Tramp but running a little tramp-daemon on
5725 ;; the other end, such that we can use a more efficient
5726 ;; communication protocol (e.g. when saving a file we could locally
5727 ;; diff it against the last version (of which the remote daemon
5728 ;; would also keep a copy), and then only send the diff).
5730 ;; This said, even using such a daemon it might be difficult to get
5731 ;; good performance: part of the problem is the number of
5732 ;; round-trips. E.g. when saving a file we have to check if the
5733 ;; file was modified in the mean time and whether saving into a new
5734 ;; inode would change the owner (etc...), which each require a
5735 ;; round-trip. To get rid of these round-trips, we'd have to
5736 ;; shortcut this code and delegate the higher-level "save file"
5737 ;; operation to the remote server, which then has to perform those
5738 ;; tasks but still obeying the locally set customizations about how
5739 ;; to do each one of those tasks.
5741 ;; We could either put higher-level ops in there (like
5742 ;; `save-buffer'), which implies replicating the whole `save-buffer'
5743 ;; behavior, which is a lot of work and likely to be not 100%
5746 ;; Or we could introduce new low-level ops that are asynchronous,
5747 ;; and then rewrite save-buffer to use them. IOW save-buffer would
5748 ;; start with a bunch of calls like `start-getting-file-attributes'
5749 ;; which could immediately be passed on to the remote side, and
5750 ;; later on checks the return value of those calls as and when
5751 ;; needed. (Stefan Monnier)
5753 ;;; tramp-sh.el ends here