1 ;;; tramp-fish.el --- Tramp access functions for FISH protocol
3 ;; Copyright (C) 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Access functions for FIles transferred over SHell protocol from Tramp.
27 ;; FISH is a protocol developped for the GNU Midnight Commander
28 ;; <https://savannah.gnu.org/projects/mc>. A client connects to a
29 ;; remote host via ssh (or rsh, shall be configurable), and starts
30 ;; there a fish server via the command "start_fish_server". All
31 ;; commands from the client have the form "#FISH_COMMAND\n" (always
32 ;; one line), followed by equivalent shell commands in case there is
33 ;; no fish server running.
35 ;; The fish server (or the equivalent shell commands) must return the
36 ;; response, which is finished by a line "### xxx <optional text>\n".
37 ;; "xxx" stands for 3 digits, representing a return code. Return
38 ;; codes "# 000" and "# 001" are reserved for fallback implementation
39 ;; with native shell commands; they are not used inside the server. See
40 ;; <http://cvs.savannah.gnu.org/viewcvs/mc/vfs/README.fish?root=mc&view=markup>
41 ;; for details of original specification.
43 ;; The GNU Midnight Commander implements the original fish protocol
44 ;; version 0.0.2. The KDE Konqueror has its own implementation, which
46 ;; <http://websvn.kde.org/branches/KDE/3.5/kdebase/kioslave/fish>. It
47 ;; implements an extended protocol version 0.0.3. Additionally, it
48 ;; provides a fish server implementation in Perl (which is the only
49 ;; implementation I've heard of). The following command reference is
50 ;; based on that implementation.
52 ;; All commands return either "### 2xx\n" (OK) or "### 5xx <optional text>\n"
53 ;; (NOK). Return codes are mentioned only if they are different from this.
54 ;; Spaces in any parameter must be escaped by "\ ".
56 ;; Command/Return Code Comment
58 ;; #FISH initial connection, not used
60 ;; ### 100 transfer fish server missing server, or wrong checksum
63 ;; #VER a.b.c <commands requested>
64 ;; VER x.y.z <commands offered> .fishsrv.pl response is not uptodate
71 ;; #COPY /path/a /path/b version 0.0.3 only
73 ;; #RENAME /path/a /path/b
75 ;; #SYMLINK /path/a /path/b
77 ;; #LINK /path/a /path/b
85 ;; #CHOWN user /file/name
87 ;; #CHGRP group /file/name
91 ;; #READ <offset> <size> /path/and/filename
92 ;; ### 291 successful exit when reading
94 ;; ### 292 successful exit when reading
97 ;; #WRITE <offset> <size> /path/and/filename
99 ;; #APPEND <size> /path/and/filename version 0.0.3 only
102 ;; <number of entries> version 0.0.3 only
103 ;; ### 100 version 0.0.3 only
104 ;; P<unix permissions> <owner>.<group>
106 ;; d<3-letters month name> <day> <year or HH:MM>
107 ;; D<year> <month> <day> <hour> <minute> <second>[.1234]
108 ;; E<major-of-device>,<minor>
110 ;; L<filename symlink points to>
111 ;; M<mimetype> version 0.0.3 only
112 ;; <blank line to separate items>
114 ;; #STAT /file version 0.0.3 only
115 ;; like #LIST except for directories
116 ;; <number of entries>
118 ;; P<unix permissions> <owner>.<group>
120 ;; d<3-letters month name> <day> <year or HH:MM>
121 ;; D<year> <month> <day> <hour> <minute> <second>[.1234]
122 ;; E<major-of-device>,<minor>
124 ;; L<filename symlink points to>
125 ;; <blank line to separate items>
130 ;; <binary data> exactly filesize bytes
131 ;; ### 200 with no preceding newline
133 ;; #STOR <size> /file/name
135 ;; <data> exactly size bytes
136 ;; ### 001 partial success
138 ;; #EXEC <command> <tmpfile> version 0.0.3 only
139 ;; <tmpfile> must not exists. It contains the output of <command>.
140 ;; It can be retrieved afterwards. Last line is
141 ;; ###RESULT: <returncode>
143 ;; This implementation is meant as proof of the concept, whether there
144 ;; is a better performance compared with the native ssh method. It
145 ;; looks like the file information retrieval is slower, especially the
146 ;; #LIST command. On the other hand, the file contents transmission
147 ;; seems to perform better than other inline methods, because there is
148 ;; no need for data encoding/decoding, and it supports the APPEND
149 ;; parameter of `write-region'. Transfer of binary data fails due to
150 ;; Emacs' process input/output handling.
156 (require 'tramp-cache
)
157 (require 'tramp-compat
)
159 ;; Define FISH method ...
160 (defcustom tramp-fish-method
"fish"
161 "*Method to connect via FISH protocol."
165 ;; ... and add it to the method list.
166 (add-to-list 'tramp-methods
(cons tramp-fish-method nil
))
168 ;; Add a default for `tramp-default-user-alist'. Default is the local user.
169 (add-to-list 'tramp-default-user-alist
170 `(,tramp-fish-method nil
,(user-login-name)))
172 ;; Add completion function for FISH method.
173 (tramp-set-completion-function
174 tramp-fish-method tramp-completion-function-alist-ssh
)
176 (defconst tramp-fish-continue-prompt-regexp
"^### 100.*\n"
177 "FISH return code OK.")
179 ;; It cannot be a defconst, occasionally we bind it locally.
180 (defvar tramp-fish-ok-prompt-regexp
"^### 200\n"
181 "FISH return code OK.")
183 (defconst tramp-fish-error-prompt-regexp
"^### \\(4\\|5\\)[0-9]+.*\n"
184 "Regexp for possible error strings of FISH servers.
185 Used instead of analyzing error codes of commands.")
187 (defcustom tramp-fish-start-fish-server-command
188 (concat "stty intr \"\" quit \"\" erase \"\" kill \"\" eof \"\" eol \"\" eol2 \"\" swtch \"\" start \"\" stop \"\" susp \"\" rprnt \"\" werase \"\" lnext \"\" flush \"\"; "
190 "`grep 'ARGV\\[0\\]' .fishsrv.pl | "
191 "sed -e 's/^[^\"]*\"//' -e 's/\"[^\"]*$//'`; "
193 "*Command to connect via FISH protocol."
197 ;; New handlers should be added here.
198 (defconst tramp-fish-file-name-handler-alist
200 ;; `access-file' performed by default handler
201 (add-name-to-file . tramp-fish-handle-add-name-to-file
)
202 ;; `byte-compiler-base-file-name' performed by default handler
203 (copy-file . tramp-fish-handle-copy-file
)
204 (delete-directory . tramp-fish-handle-delete-directory
)
205 (delete-file . tramp-fish-handle-delete-file
)
206 ;; `diff-latest-backup-file' performed by default handler
207 (directory-file-name . tramp-handle-directory-file-name
)
208 (directory-files . tramp-handle-directory-files
)
209 (directory-files-and-attributes . tramp-fish-handle-directory-files-and-attributes
)
210 ;; `dired-call-process' performed by default handler
211 ;; `dired-compress-file' performed by default handler
212 (dired-uncache . tramp-handle-dired-uncache
)
213 (expand-file-name . tramp-fish-handle-expand-file-name
)
214 ;; `file-accessible-directory-p' performed by default handler
215 (file-attributes . tramp-fish-handle-file-attributes
)
216 (file-directory-p . tramp-fish-handle-file-directory-p
)
217 (file-executable-p . tramp-fish-handle-file-executable-p
)
218 (file-exists-p . tramp-fish-handle-file-exists-p
)
219 (file-local-copy . tramp-fish-handle-file-local-copy
)
220 (file-remote-p . tramp-handle-file-remote-p
)
221 (file-modes . tramp-handle-file-modes
)
222 (file-name-all-completions . tramp-fish-handle-file-name-all-completions
)
223 (file-name-as-directory . tramp-handle-file-name-as-directory
)
224 (file-name-completion . tramp-handle-file-name-completion
)
225 (file-name-directory . tramp-handle-file-name-directory
)
226 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
227 ;; `file-name-sans-versions' performed by default handler
228 (file-newer-than-file-p . tramp-fish-handle-file-newer-than-file-p
)
229 (file-ownership-preserved-p . ignore
)
230 (file-readable-p . tramp-fish-handle-file-readable-p
)
231 (file-regular-p . tramp-handle-file-regular-p
)
232 (file-symlink-p . tramp-handle-file-symlink-p
)
233 ;; `file-truename' performed by default handler
234 (file-writable-p . tramp-fish-handle-file-writable-p
)
235 (find-backup-file-name . tramp-handle-find-backup-file-name
)
236 ;; `find-file-noselect' performed by default handler
237 ;; `get-file-buffer' performed by default handler
238 (insert-directory . tramp-fish-handle-insert-directory
)
239 (insert-file-contents . tramp-fish-handle-insert-file-contents
)
240 (load . tramp-handle-load
)
241 (make-directory . tramp-fish-handle-make-directory
)
242 (make-directory-internal . tramp-fish-handle-make-directory-internal
)
243 (make-symbolic-link . tramp-fish-handle-make-symbolic-link
)
244 (rename-file . tramp-fish-handle-rename-file
)
245 (set-file-modes . tramp-fish-handle-set-file-modes
)
246 (set-file-times . tramp-fish-handle-set-file-times
)
247 (set-visited-file-modtime . ignore
)
248 (shell-command . tramp-handle-shell-command
)
249 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
250 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
251 (vc-registered . ignore
)
252 (verify-visited-file-modtime . ignore
)
253 (write-region . tramp-fish-handle-write-region
)
254 (executable-find . tramp-fish-handle-executable-find
)
255 (start-file-process . ignore
)
256 (process-file . tramp-fish-handle-process-file
)
258 "Alist of handler functions for Tramp FISH method.
259 Operations not mentioned here will be handled by the default Emacs primitives.")
261 (defun tramp-fish-file-name-p (filename)
262 "Check if it's a filename for FISH protocol."
263 (let ((v (tramp-dissect-file-name filename
)))
264 (string= (tramp-file-name-method v
) tramp-fish-method
)))
266 (defun tramp-fish-file-name-handler (operation &rest args
)
267 "Invoke the FISH related OPERATION.
268 First arg specifies the OPERATION, second arg is a list of arguments to
269 pass to the OPERATION."
270 (let ((fn (assoc operation tramp-fish-file-name-handler-alist
)))
272 (save-match-data (apply (cdr fn
) args
))
273 (tramp-run-real-handler operation args
))))
275 (add-to-list 'tramp-foreign-file-name-handler-alist
276 (cons 'tramp-fish-file-name-p
'tramp-fish-file-name-handler
))
279 ;; File name primitives
281 (defun tramp-fish-handle-add-name-to-file
282 (filename newname
&optional ok-if-already-exists
)
283 "Like `add-name-to-file' for Tramp files."
284 (unless (tramp-equal-remote filename newname
)
285 (with-parsed-tramp-file-name
286 (if (tramp-tramp-file-p filename
) filename newname
) nil
289 "add-name-to-file: %s"
290 "only implemented for same method, same user, same host")))
291 (with-parsed-tramp-file-name filename v1
292 (with-parsed-tramp-file-name newname v2
293 (when (and (not ok-if-already-exists
)
294 (file-exists-p newname
)
295 (not (numberp ok-if-already-exists
))
298 "File %s already exists; make it a new name anyway? "
302 "add-name-to-file: file %s already exists" newname
))
303 (tramp-flush-file-property v2 v2-localname
)
304 (unless (tramp-fish-send-command-and-check
305 v1
(format "#LINK %s %s" v1-localname v2-localname
))
307 v1
'file-error
"Error with add-name-to-file %s" newname
)))))
309 (defun tramp-fish-handle-copy-file
310 (filename newname
&optional ok-if-already-exists keep-date preserve-uid-gid
)
311 "Like `copy-file' for Tramp files."
312 (tramp-fish-do-copy-or-rename-file
313 'copy filename newname ok-if-already-exists keep-date preserve-uid-gid
))
315 (defun tramp-fish-handle-delete-directory (directory &optional recursive
)
316 "Like `delete-directory' for Tramp files."
317 (when (file-exists-p directory
)
321 (if (file-directory-p file
)
322 (tramp-compat-delete-directory file recursive
)
324 ;; We do not want to delete "." and "..".
326 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
327 (with-parsed-tramp-file-name
328 (directory-file-name (expand-file-name directory
)) nil
329 (tramp-flush-directory-property v localname
)
330 (tramp-fish-send-command-and-check v
(format "#RMD %s" localname
)))))
332 (defun tramp-fish-handle-delete-file (filename)
333 "Like `delete-file' for Tramp files."
334 (when (file-exists-p filename
)
335 (with-parsed-tramp-file-name (expand-file-name filename
) nil
336 (tramp-flush-file-property v localname
)
337 (tramp-fish-send-command-and-check v
(format "#DELE %s" localname
)))))
339 (defun tramp-fish-handle-directory-files-and-attributes
340 (directory &optional full match nosort id-format
)
341 "Like `directory-files-and-attributes' for Tramp files."
344 ;; We cannot call `file-attributes' for backward compatibility reasons.
345 ;; Its optional parameter ID-FORMAT is introduced with Emacs 22.
346 (cons x
(tramp-fish-handle-file-attributes
347 (if full x
(expand-file-name x directory
)) id-format
)))
348 (directory-files directory full match nosort
)))
350 (defun tramp-fish-handle-expand-file-name (name &optional dir
)
351 "Like `expand-file-name' for Tramp files."
352 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
353 (setq dir
(or dir default-directory
"/"))
354 ;; Unless NAME is absolute, concat DIR and NAME.
355 (unless (file-name-absolute-p name
)
356 (setq name
(concat (file-name-as-directory dir
) name
)))
357 ;; If NAME is not a Tramp file, run the real handler,
358 (if (or (tramp-completion-mode-p) (not (tramp-tramp-file-p name
)))
359 (tramp-drop-volume-letter
360 (tramp-run-real-handler 'expand-file-name
(list name nil
)))
362 (with-parsed-tramp-file-name name nil
363 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
364 (setq localname
(concat "~/" localname
)))
365 ;; Tilde expansion if necessary.
366 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
367 (let ((uname (match-string 1 localname
))
368 (fname (match-string 2 localname
)))
369 ;; We cannot apply "~user/", because this is not supported
370 ;; by the FISH protocol.
371 (unless (string-equal uname
"~")
373 v
'file-error
"Tilde expansion not supported for %s" name
))
375 (with-connection-property v uname
376 (tramp-fish-send-command-and-check v
"#PWD")
377 (with-current-buffer (tramp-get-buffer v
)
378 (goto-char (point-min))
379 (buffer-substring (point) (tramp-compat-line-end-position)))))
380 (setq localname
(concat uname fname
))))
381 ;; There might be a double slash, for example when "~/"
382 ;; expands to "/". Remove this.
383 (while (string-match "//" localname
)
384 (setq localname
(replace-match "/" t t localname
)))
385 ;; No tilde characters in file name, do normal
386 ;; expand-file-name (this does "/./" and "/../"). We bind
387 ;; `directory-sep-char' here for XEmacs on Windows, which
388 ;; would otherwise use backslash. `default-directory' is
389 ;; bound, because on Windows there would be problems with UNC
390 ;; shares or Cygwin mounts.
391 (let ((directory-sep-char ?
/)
392 (default-directory (tramp-compat-temporary-file-directory)))
393 (tramp-make-tramp-file-name
395 (tramp-drop-volume-letter
396 (tramp-run-real-handler
397 'expand-file-name
(list localname
))))))))
399 (defun tramp-fish-handle-file-attributes (filename &optional id-format
)
400 "Like `file-attributes' for Tramp files."
401 (with-parsed-tramp-file-name (expand-file-name filename
) nil
402 (with-file-property v localname
(format "file-attributes-%s" id-format
)
403 (cdr (car (tramp-fish-get-file-entries v localname nil
))))))
405 (defun tramp-fish-handle-file-directory-p (filename)
406 "Like `file-directory-p' for Tramp files."
407 (let ((attributes (file-attributes filename
)))
409 (or (string-match "d" (nth 8 attributes
))
410 (and (file-symlink-p filename
)
411 (with-parsed-tramp-file-name filename nil
413 (tramp-make-tramp-file-name
414 method user host
(nth 0 attributes
))))))
417 (defun tramp-fish-handle-file-exists-p (filename)
418 "Like `file-exists-p' for Tramp files."
419 (and (file-attributes filename
) t
))
421 (defun tramp-fish-handle-file-executable-p (filename)
422 "Like `file-executable-p' for Tramp files."
423 (with-parsed-tramp-file-name (expand-file-name filename
) nil
424 (with-file-property v localname
"file-executable-p"
425 (when (file-exists-p filename
)
426 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
428 (tramp-make-tramp-file-name
430 (tramp-get-connection-property v
"home-directory" nil
))))
431 (or (and (char-equal (aref mode-chars
3) ?x
)
432 (equal (nth 2 (file-attributes filename
))
433 (nth 2 (file-attributes home-directory
))))
434 (and (char-equal (aref mode-chars
6) ?x
)
435 (equal (nth 3 (file-attributes filename
))
436 (nth 3 (file-attributes home-directory
))))
437 (char-equal (aref mode-chars
9) ?x
)))))))
439 (defun tramp-fish-handle-file-readable-p (filename)
440 "Like `file-readable-p' for Tramp files."
441 (with-parsed-tramp-file-name (expand-file-name filename
) nil
442 (with-file-property v localname
"file-readable-p"
443 (when (file-exists-p filename
)
444 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
446 (tramp-make-tramp-file-name
448 (tramp-get-connection-property v
"home-directory" nil
))))
449 (or (and (char-equal (aref mode-chars
1) ?r
)
450 (equal (nth 2 (file-attributes filename
))
451 (nth 2 (file-attributes home-directory
))))
452 (and (char-equal (aref mode-chars
4) ?r
)
453 (equal (nth 3 (file-attributes filename
))
454 (nth 3 (file-attributes home-directory
))))
455 (char-equal (aref mode-chars
7) ?r
)))))))
457 (defun tramp-fish-handle-file-writable-p (filename)
458 "Like `file-writable-p' for Tramp files."
459 (with-parsed-tramp-file-name (expand-file-name filename
) nil
460 (with-file-property v localname
"file-writable-p"
461 (if (not (file-exists-p filename
))
462 ;; If file doesn't exist, check if directory is writable.
463 (and (file-directory-p (file-name-directory filename
))
464 (file-writable-p (file-name-directory filename
)))
465 ;; Existing files must be writable.
466 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
468 (tramp-make-tramp-file-name
470 (tramp-get-connection-property v
"home-directory" nil
))))
471 (or (and (char-equal (aref mode-chars
2) ?w
)
472 (equal (nth 2 (file-attributes filename
))
473 (nth 2 (file-attributes home-directory
))))
474 (and (char-equal (aref mode-chars
5) ?w
)
475 (equal (nth 3 (file-attributes filename
))
476 (nth 3 (file-attributes home-directory
))))
477 (char-equal (aref mode-chars
8) ?w
)))))))
479 (defun tramp-fish-handle-file-local-copy (filename)
480 "Like `file-local-copy' for Tramp files."
481 (with-parsed-tramp-file-name (expand-file-name filename
) nil
482 (unless (file-exists-p filename
)
485 "Cannot make local copy of non-existing file `%s'" filename
))
486 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
487 (tramp-message v
4 "Fetching %s to tmp file %s..." filename tmpfile
)
488 (when (tramp-fish-retrieve-data v
)
490 (with-current-buffer (tramp-get-buffer v
)
491 (write-region (point-min) (point-max) tmpfile
))
492 (tramp-message v
4 "Fetching %s to tmp file %s...done" filename tmpfile
)
495 ;; This function should return "foo/" for directories and "bar" for
497 (defun tramp-fish-handle-file-name-all-completions (filename directory
)
498 "Like `file-name-all-completions' for Tramp files."
501 (with-parsed-tramp-file-name (expand-file-name directory
) nil
502 (with-file-property v localname
"file-name-all-completions"
505 (with-file-property v localname
"file-entries"
506 (tramp-fish-get-file-entries v localname t
))))
510 (if (string-match "d" (nth 9 x
))
511 (file-name-as-directory (nth 0 x
))
515 (defun tramp-fish-handle-file-newer-than-file-p (file1 file2
)
516 "Like `file-newer-than-file-p' for Tramp files."
518 ((not (file-exists-p file1
)) nil
)
519 ((not (file-exists-p file2
)) t
)
520 (t (tramp-time-less-p (nth 5 (file-attributes file2
))
521 (nth 5 (file-attributes file1
))))))
523 (defun tramp-fish-handle-insert-directory
524 (filename switches
&optional wildcard full-directory-p
)
525 "Like `insert-directory' for Tramp files.
526 WILDCARD and FULL-DIRECTORY-P are not handled."
527 (setq filename
(expand-file-name filename
))
528 (when (file-directory-p filename
)
529 ;; This check is a little bit strange, but in `dired-add-entry'
530 ;; this function is called with a non-directory ...
531 (setq filename
(file-name-as-directory filename
)))
533 (with-parsed-tramp-file-name filename nil
534 (tramp-flush-file-property v localname
)
537 (with-file-property v localname
"file-entries"
538 (tramp-fish-get-file-entries v localname t
))))
545 (if (string-match "t" switches
)
547 (tramp-time-less-p (nth 6 y
) (nth 6 x
))
549 (string-lessp (nth 0 x
) (nth 0 y
))))))
556 "%10s %3d %-8s %-8s %8s %s %s%s\n"
563 (if (tramp-time-less-p
564 (tramp-time-subtract (current-time) (nth 6 x
))
569 (nth 0 x
) ; file name
570 (if (stringp (nth 1 x
)) (format " -> %s" (nth 1 x
)) "")))
575 (defun tramp-fish-handle-insert-file-contents
576 (filename &optional visit beg end replace
)
577 "Like `insert-file-contents' for Tramp files."
578 (barf-if-buffer-read-only)
580 (setq buffer-file-name
(expand-file-name filename
))
581 (set-visited-file-modtime)
582 (set-buffer-modified-p nil
))
584 (with-parsed-tramp-file-name filename nil
585 (if (not (file-exists-p filename
))
587 v
'file-error
"File %s not found on remote host" filename
)
589 (let ((point (point))
591 (tramp-message v
4 "Fetching file %s..." filename
)
592 (when (tramp-fish-retrieve-data v
)
595 (with-current-buffer (tramp-get-buffer v
)
596 (let ((beg (or beg
(point-min)))
597 (end (min (or end
(point-max)) (point-max))))
598 (setq size
(- end beg
))
599 (buffer-substring beg end
))))
601 (tramp-message v
4 "Fetching file %s...done" filename
)
603 (list (expand-file-name filename
) size
)))))
605 (defun tramp-fish-handle-make-directory (dir &optional parents
)
606 "Like `make-directory' for Tramp files."
607 (setq dir
(directory-file-name (expand-file-name dir
)))
608 (unless (file-name-absolute-p dir
)
609 (setq dir
(expand-file-name dir default-directory
)))
610 (with-parsed-tramp-file-name dir nil
612 (let ((ldir (file-name-directory dir
)))
613 ;; Make missing directory parts
614 (when (and parents
(not (file-directory-p ldir
)))
615 (make-directory ldir parents
))
617 (when (file-directory-p ldir
)
618 (make-directory-internal dir
))
619 (unless (file-directory-p dir
)
620 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
622 (defun tramp-fish-handle-make-directory-internal (directory)
623 "Like `make-directory-internal' for Tramp files."
624 (setq directory
(directory-file-name (expand-file-name directory
)))
625 (unless (file-name-absolute-p directory
)
626 (setq directory
(expand-file-name directory default-directory
)))
627 (when (file-directory-p (file-name-directory directory
))
628 (with-parsed-tramp-file-name directory nil
631 (tramp-fish-send-command-and-check v
(format "#MKD %s" localname
))
633 v
'file-error
"Couldn't make directory %s" directory
))))))
635 (defun tramp-fish-handle-make-symbolic-link
636 (filename linkname
&optional ok-if-already-exists
)
637 "Like `make-symbolic-link' for Tramp files.
638 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
639 the symlink. If LINKNAME is a Tramp file, only the localname component is
640 used as the target of the symlink.
642 If LINKNAME is a Tramp file and the localname component is relative, then
643 it is expanded first, before the localname component is taken. Note that
644 this can give surprising results if the user/host for the source and
645 target of the symlink differ."
646 (with-parsed-tramp-file-name linkname nil
647 ;; Do the 'confirm if exists' thing.
648 (when (file-exists-p linkname
)
650 (if (or (null ok-if-already-exists
) ; not allowed to exist
651 (and (numberp ok-if-already-exists
)
654 "File %s already exists; make it a link anyway? "
657 v
'file-already-exists
"File %s already exists" localname
)
658 (delete-file linkname
)))
660 ;; If FILENAME is a Tramp name, use just the localname component.
661 (when (tramp-tramp-file-p filename
)
662 (setq filename
(tramp-file-name-localname
663 (tramp-dissect-file-name (expand-file-name filename
)))))
665 ;; Right, they are on the same host, regardless of user, method, etc.
666 ;; We now make the link on the remote machine. This will occur as the user
667 ;; that FILENAME belongs to.
669 (tramp-fish-send-command-and-check
670 v
(format "#SYMLINK %s %s" filename localname
))
671 (tramp-error v
'file-error
"Error creating symbolic link %s" linkname
))))
673 (defun tramp-fish-handle-rename-file
674 (filename newname
&optional ok-if-already-exists
)
675 "Like `rename-file' for Tramp files."
676 (tramp-fish-do-copy-or-rename-file
677 'rename filename newname ok-if-already-exists t
))
679 (defun tramp-fish-handle-set-file-modes (filename mode
)
680 "Like `set-file-modes' for Tramp files."
681 (with-parsed-tramp-file-name filename nil
682 (tramp-flush-file-property v localname
)
683 (unless (tramp-fish-send-command-and-check
684 v
(format "#CHMOD %s %s"
685 (tramp-decimal-to-octal mode
)
686 (tramp-shell-quote-argument localname
)))
688 v
'file-error
"Error while changing file's mode %s" filename
))))
690 (defun tramp-fish-handle-set-file-times (filename &optional time
)
691 "Like `set-file-times' for Tramp files."
692 (with-parsed-tramp-file-name filename nil
693 (let ((time (if (or (null time
) (equal time
'(0 0))) (current-time) time
)))
695 "touch" nil nil nil
"-t"
696 (format-time-string "%Y%m%d%H%M.%S" time
)
697 (tramp-shell-quote-argument localname
))))))
699 (defun tramp-fish-handle-write-region
700 (start end filename
&optional append visit lockname confirm
)
701 "Like `write-region' for Tramp files."
702 (setq filename
(expand-file-name filename
))
703 (with-parsed-tramp-file-name filename nil
704 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
705 (when (and (not (featurep 'xemacs
))
706 confirm
(file-exists-p filename
))
707 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
709 (tramp-error v
'file-error
"File not overwritten")))
711 (tramp-flush-file-property v localname
)
714 (let ((tramp-fish-ok-prompt-regexp
716 tramp-fish-ok-prompt-regexp
"\\|"
717 tramp-fish-continue-prompt-regexp
)))
718 (tramp-fish-send-command
719 v
(format "%s %d %s\n### 100"
720 (if append
"#APPEND" "#STOR") (- end start
) localname
)))
722 ;; Send data, if there are any.
724 (tramp-fish-send-command v
(buffer-substring-no-properties start end
)))
727 (set-visited-file-modtime))))
729 (defun tramp-fish-handle-executable-find (command)
730 "Like `executable-find' for Tramp files."
732 (if (zerop (process-file "which" nil t nil command
))
734 (goto-char (point-min))
735 (buffer-substring (point-min) (tramp-compat-line-end-position))))))
737 (defun tramp-fish-handle-process-file
738 (program &optional infile destination display
&rest args
)
739 "Like `process-file' for Tramp files."
740 ;; The implementation is not complete yet.
741 (when (and (numberp destination
) (zerop destination
))
742 (error "Implementation does not handle immediate return"))
744 (with-parsed-tramp-file-name default-directory nil
745 (let (command input tmpinput output tmpoutput stderr tmpstderr
748 (setq command
(mapconcat 'tramp-shell-quote-argument
749 (cons program args
) " "))
752 (setq input
"/dev/null")
753 (setq infile
(expand-file-name infile
))
754 (if (tramp-equal-remote default-directory infile
)
755 ;; INFILE is on the same remote host.
756 (setq input
(with-parsed-tramp-file-name infile nil localname
))
757 ;; INFILE must be copied to remote host.
758 (setq input
(tramp-make-tramp-temp-file v
)
759 tmpinput
(tramp-make-tramp-file-name method user host input
))
760 (copy-file infile tmpinput t
)))
761 (when input
(setq command
(format "%s <%s" command input
)))
764 (setq output
(tramp-make-tramp-temp-file v
)
765 tmpoutput
(tramp-make-tramp-file-name method user host output
))
768 ((bufferp destination
)
769 (setq outbuf destination
))
771 ((stringp destination
)
772 (setq outbuf
(get-buffer-create destination
)))
773 ;; (REAL-DESTINATION ERROR-DESTINATION)
777 ((bufferp (car destination
))
778 (setq outbuf
(car destination
)))
779 ((stringp (car destination
))
780 (setq outbuf
(get-buffer-create (car destination
)))))
783 ((stringp (cadr destination
))
784 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
785 (if (tramp-equal-remote default-directory
(cadr destination
))
786 ;; stderr is on the same remote host.
787 (setq stderr
(with-parsed-tramp-file-name
788 (cadr destination
) nil localname
))
789 ;; stderr must be copied to remote host. The temporary
790 ;; file must be deleted after execution.
791 (setq stderr
(tramp-make-tramp-temp-file v
)
792 tmpstderr
(tramp-make-tramp-file-name
793 method user host stderr
))))
794 ;; stderr to be discarded
795 ((null (cadr destination
))
796 (setq stderr
"/dev/null"))))
799 (setq outbuf
(current-buffer))))
800 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
802 ;; Goto working directory.
804 (tramp-fish-send-command-and-check
805 v
(format "#CWD %s" (tramp-shell-quote-argument localname
)))
806 (tramp-error v
'file-error
"No such directory: %s" default-directory
))
807 ;; Send the command. It might not return in time, so we protect it.
810 (unless (tramp-fish-send-command-and-check
813 (tramp-shell-quote-argument command
) output
))
815 ;; Check return code.
818 (tramp-make-tramp-file-name method user host output
)))
820 (insert-file-contents tmpfile
)
821 (goto-char (point-max))
823 (looking-at "^###RESULT: \\([0-9]+\\)")
824 (setq ret
(string-to-number (match-string 1)))
825 (delete-region (point) (point-max))
826 (write-region (point-min) (point-max) tmpfile
))
827 ;; We should show the output anyway.
829 (with-current-buffer outbuf
(insert-file-contents tmpfile
))
830 (when display
(display-buffer outbuf
))))
831 ;; When the user did interrupt, we should do it also.
832 (error (setq ret
1)))
834 ;; Provide error file.
835 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
837 (when tmpinput
(delete-file tmpinput
))
838 (when tmpoutput
(delete-file tmpoutput
))
839 ;; Return exit status.
843 ;; Internal file name functions
845 (defun tramp-fish-do-copy-or-rename-file
846 (op filename newname
&optional ok-if-already-exists keep-date preserve-uid-gid
)
847 "Copy or rename a remote file.
848 OP must be `copy' or `rename' and indicates the operation to
849 perform. FILENAME specifies the file to copy or rename, NEWNAME
850 is the name of the new file (for copy) or the new name of the
851 file (for rename). OK-IF-ALREADY-EXISTS means don't barf if
852 NEWNAME exists already. KEEP-DATE means to make sure that
853 NEWNAME has the same timestamp as FILENAME.
855 This function is invoked by `tramp-fish-handle-copy-file' and
856 `tramp-fish-handle-rename-file'. It is an error if OP is neither
857 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
859 (unless (memq op
'(copy rename
))
860 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
861 (let ((t1 (tramp-tramp-file-p filename
))
862 (t2 (tramp-tramp-file-p newname
)))
864 (unless ok-if-already-exists
865 (when (and t2
(file-exists-p newname
))
866 (with-parsed-tramp-file-name newname nil
868 v
'file-already-exists
"File %s already exists" newname
))))
872 ;; Both are Tramp files.
875 ;; Shortcut: if method, host, user are the same for both
876 ;; files, we invoke `cp' or `mv' on the remote host
878 ((tramp-equal-remote filename newname
)
879 (tramp-fish-do-copy-or-rename-file-directly
880 op filename newname keep-date preserve-uid-gid
))
881 ;; No shortcut was possible. So we copy the
882 ;; file first. If the operation was `rename', we go
883 ;; back and delete the original file (if the copy was
884 ;; successful). The approach is simple-minded: we
885 ;; create a new buffer, insert the contents of the
886 ;; source file into it, then write out the buffer to
887 ;; the target file. The advantage is that it doesn't
888 ;; matter which filename handlers are used for the
889 ;; source and target file.
891 (tramp-do-copy-or-rename-file-via-buffer
892 op filename newname keep-date
))))
894 ;; One file is a Tramp file, the other one is local.
896 ;; Use the generic method via a Tramp buffer.
897 (tramp-do-copy-or-rename-file-via-buffer
898 op filename newname keep-date
))
901 ;; One of them must be a Tramp file.
902 (error "Tramp implementation says this cannot happen")))
903 ;; When newname did exist, we have wrong cached values.
905 (with-parsed-tramp-file-name newname nil
906 (tramp-flush-file-property v localname
)
907 (tramp-flush-file-property v
(file-name-directory localname
)))))))
909 (defun tramp-fish-do-copy-or-rename-file-directly
910 (op filename newname keep-date preserve-uid-gid
)
911 "Invokes `COPY' or `RENAME' on the remote system.
912 OP must be one of `copy' or `rename', indicating `cp' or `mv',
913 respectively. VEC specifies the connection. LOCALNAME1 and
914 LOCALNAME2 specify the two arguments of `cp' or `mv'. If
915 KEEP-DATE is non-nil, preserve the time stamp when copying.
916 PRESERVE-UID-GID is completely ignored."
917 (with-parsed-tramp-file-name filename v1
918 (with-parsed-tramp-file-name newname v2
919 (tramp-fish-send-command
922 (if (eq op
'copy
) "#COPY" "#RENAME")
923 (tramp-shell-quote-argument v1-localname
)
924 (tramp-shell-quote-argument v2-localname
)))))
925 ;; KEEP-DATE handling.
926 (when (and keep-date
(functionp 'set-file-times
))
927 (set-file-times newname
(nth 5 (file-attributes filename
))))
929 (set-file-modes newname
(tramp-default-file-modes filename
)))
931 (defun tramp-fish-get-file-entries (vec localname list
)
932 "Read entries returned by FISH server.
933 When LIST is true, a #LIST command will be sent, including all entries
934 of a directory. Otherwise, #STAT is sent for just one entry.
935 Result is a list of (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
936 SIZE MODE WEIRD INODE DEVICE)."
938 (with-current-buffer (tramp-get-buffer vec
)
939 ;; #LIST does not work properly with trailing "/", at least in
941 (when (string-match "/$" localname
)
942 (setq localname
(concat localname
".")))
944 (let ((command (format "%s %s" (if list
"#LIST" "#STAT") localname
))
945 buffer-read-only num res
)
948 (tramp-fish-send-command vec command
)
950 ;; Read number of entries
951 (goto-char (point-min))
953 (unless (integerp (setq num
(read (current-buffer)))) (error nil
))
954 (error (return nil
)))
956 (delete-region (point-min) (point))
959 (goto-char (point-min))
961 (unless (looking-at tramp-fish-continue-prompt-regexp
) (error nil
))
962 (error (return nil
)))
964 (delete-region (point-min) (point))
968 (let ((item (tramp-fish-read-file-entry)))
969 ;; Add inode and device.
972 (list (tramp-get-inode vec
)
973 (tramp-get-device vec
))))))
976 (goto-char (point-min))
978 (unless (looking-at tramp-fish-ok-prompt-regexp
) (error nil
))
981 "`%s' does not return a valid Lisp expression: `%s'"
982 command
(buffer-string))))
984 (delete-region (point-min) (point))
988 (defun tramp-fish-read-file-entry ()
989 "Parse entry in output buffer.
990 Result is the list (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
992 ;; We are called from `tramp-fish-get-file-entries', which sets the
994 (let (buffer-read-only localname link uid gid mtime size mode
)
998 ;; P<unix permissions> <owner>.<group>
999 ((looking-at "^P\\(.+\\)\\s-\\(.+\\)\\.\\(.+\\)$")
1000 (setq mode
(match-string 1))
1001 (setq uid
(match-string 2))
1002 (setq gid
(match-string 3))
1003 (when (string-match "^d" mode
) (setq link t
)))
1005 ((looking-at "^S\\([0-9]+\\)$")
1006 (setq size
(string-to-number (match-string 1))))
1007 ;; D<year> <month> <day> <hour> <minute> <second>[.1234]
1009 "^D\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\(\\S-+\\)$")
1012 (string-to-number (match-string 6))
1013 (string-to-number (match-string 5))
1014 (string-to-number (match-string 4))
1015 (string-to-number (match-string 3))
1016 (string-to-number (match-string 2))
1017 (string-to-number (match-string 1)))))
1018 ;; d<3-letters month name> <day> <year or HH:MM>
1019 ((looking-at "^d") nil
)
1020 ;; E<major-of-device>,<minor>
1021 ((looking-at "^E") nil
)
1023 ((looking-at "^:\\(.+\\)$")
1024 (setq localname
(match-string 1)))
1025 ;; L<filename symlink points to>
1026 ((looking-at "^L\\(.+\\)$")
1027 (setq link
(match-string 1)))
1029 ((looking-at "^M\\(.+\\)$") nil
)
1035 (delete-region (point-min) (point))))
1037 ;; delete trailing empty line
1039 (delete-region (point-min) (point))
1041 ;; Return entry in file-attributes format
1042 (list localname link -
1 uid gid
'(0 0) mtime
'(0 0) size mode nil
)))
1044 (defun tramp-fish-retrieve-data (vec)
1045 "Reads remote data for FISH protocol.
1046 The data are left in the connection buffer of VEC for further processing.
1047 Returns the size of the data."
1049 (with-current-buffer (tramp-get-buffer vec
)
1050 ;; The retrieved data might be in binary format, without
1051 ;; trailing newline. Therefore, the OK prompt might not start
1052 ;; at the beginning of a line.
1053 (let ((tramp-fish-ok-prompt-regexp "### 200\n")
1057 (tramp-fish-send-command
1058 vec
(format "#RETR %s" (tramp-file-name-localname vec
)))
1061 (goto-char (point-min))
1063 (unless (integerp (setq size
(read (current-buffer)))) (error nil
))
1064 (error (return nil
)))
1066 (delete-region (point-min) (point))
1069 (goto-char (point-min))
1071 (unless (looking-at tramp-fish-continue-prompt-regexp
) (error nil
))
1072 (error (return nil
)))
1074 (delete-region (point-min) (point))
1076 ;; The received data might contain the OK prompt already, so
1077 ;; there might be outstanding data.
1078 (while (/= (+ size
(length tramp-fish-ok-prompt-regexp
))
1079 (- (point-max) (point-min)))
1080 (tramp-wait-for-regexp
1081 (tramp-get-connection-process vec
) nil
1082 (concat tramp-fish-ok-prompt-regexp
"$")))
1085 (goto-char (+ (point-min) size
))
1087 (unless (looking-at tramp-fish-ok-prompt-regexp
) (error nil
))
1088 (error (return nil
)))
1089 (delete-region (+ (point-min) size
) (point-max))
1093 ;; Connection functions
1095 (defun tramp-fish-maybe-open-connection (vec)
1096 "Maybe open a connection VEC.
1097 Does not do anything if a connection is already open, but re-opens the
1098 connection if a previous connection has died for some reason."
1099 (let ((process-connection-type tramp-process-connection-type
)
1100 (p (get-buffer-process (tramp-get-buffer vec
))))
1102 ;; New connection must be opened.
1103 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
1105 ;; Set variables for computing the prompt for reading password.
1106 (setq tramp-current-method
(tramp-file-name-method vec
)
1107 tramp-current-user
(tramp-file-name-user vec
)
1108 tramp-current-host
(tramp-file-name-host vec
))
1110 ;; Start new process.
1111 (when (and p
(processp p
))
1113 (setenv "TERM" tramp-terminal-type
)
1114 (setenv "PS1" tramp-initial-end-of-output
)
1116 vec
3 "Opening connection for %s@%s using %s..."
1117 tramp-current-user tramp-current-host tramp-current-method
)
1119 (let* ((process-connection-type tramp-process-connection-type
)
1120 (inhibit-eol-conversion nil
)
1121 (coding-system-for-read 'binary
)
1122 (coding-system-for-write 'binary
)
1123 ;; This must be done in order to avoid our file name handler.
1124 (p (let ((default-directory
1125 (tramp-compat-temporary-file-directory)))
1127 (or (tramp-get-connection-property vec
"process-name" nil
)
1128 (tramp-buffer-name vec
))
1129 (tramp-get-connection-buffer vec
)
1131 (tramp-file-name-user vec
)
1132 (tramp-file-name-host vec
)))))
1133 (tramp-message vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1135 ;; Check whether process is alive.
1136 (tramp-set-process-query-on-exit-flag p nil
)
1138 (tramp-process-actions p vec tramp-actions-before-shell
60)
1139 (tramp-fish-send-command vec tramp-fish-start-fish-server-command
)
1142 "Found remote shell prompt on `%s'" (tramp-file-name-host vec
))))))
1144 (defun tramp-fish-send-command (vec command
)
1145 "Send the COMMAND to connection VEC."
1146 (tramp-fish-maybe-open-connection vec
)
1147 (tramp-message vec
6 "%s" command
)
1148 (tramp-send-string vec command
)
1149 (tramp-wait-for-regexp
1150 (tramp-get-connection-process vec
) nil
1151 (concat tramp-fish-ok-prompt-regexp
"\\|" tramp-fish-error-prompt-regexp
)))
1153 (defun tramp-fish-send-command-and-check (vec command
)
1154 "Send the COMMAND to connection VEC.
1155 Returns nil if there has been an error message."
1158 (tramp-fish-send-command vec command
)
1160 ;; Read return code.
1161 (with-current-buffer (tramp-get-buffer vec
)
1162 (goto-char (point-min))
1163 (looking-at tramp-fish-ok-prompt-regexp
)))
1165 (provide 'tramp-fish
)
1169 ;; * Evaluate the MIME information with #LIST or #STAT.
1172 ;; arch-tag: a66df7df-5f29-42a7-a921-643ceb29db49
1173 ;;;; tramp-fish.el ends here