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