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