1 ;;; tramp-fish.el --- Tramp access functions for FISH protocol
3 ;; Copyright (C) 2006, 2007, 2008 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' performed by default handler
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)
316 "Like `delete-directory' for Tramp files."
317 (when (file-exists-p directory
)
318 (with-parsed-tramp-file-name
319 (directory-file-name (expand-file-name directory
)) nil
320 (tramp-flush-directory-property v localname
)
321 (tramp-fish-send-command-and-check v
(format "#RMD %s" localname
)))))
323 (defun tramp-fish-handle-delete-file (filename)
324 "Like `delete-file' for Tramp files."
325 (when (file-exists-p filename
)
326 (with-parsed-tramp-file-name (expand-file-name filename
) nil
327 (tramp-flush-file-property v localname
)
328 (tramp-fish-send-command-and-check v
(format "#DELE %s" localname
)))))
330 (defun tramp-fish-handle-directory-files-and-attributes
331 (directory &optional full match nosort id-format
)
332 "Like `directory-files-and-attributes' for Tramp files."
335 ;; We cannot call `file-attributes' for backward compatibility reasons.
336 ;; Its optional parameter ID-FORMAT is introduced with Emacs 22.
337 (cons x
(tramp-fish-handle-file-attributes
338 (if full x
(expand-file-name x directory
)) id-format
)))
339 (directory-files directory full match nosort
)))
341 (defun tramp-fish-handle-expand-file-name (name &optional dir
)
342 "Like `expand-file-name' for Tramp files."
343 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
344 (setq dir
(or dir default-directory
"/"))
345 ;; Unless NAME is absolute, concat DIR and NAME.
346 (unless (file-name-absolute-p name
)
347 (setq name
(concat (file-name-as-directory dir
) name
)))
348 ;; If NAME is not a Tramp file, run the real handler,
349 (if (or (tramp-completion-mode-p) (not (tramp-tramp-file-p name
)))
350 (tramp-drop-volume-letter
351 (tramp-run-real-handler 'expand-file-name
(list name nil
)))
353 (with-parsed-tramp-file-name name nil
354 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
355 (setq localname
(concat "~/" localname
)))
356 ;; Tilde expansion if necessary.
357 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
358 (let ((uname (match-string 1 localname
))
359 (fname (match-string 2 localname
)))
360 ;; We cannot apply "~user/", because this is not supported
361 ;; by the FISH protocol.
362 (unless (string-equal uname
"~")
364 v
'file-error
"Tilde expansion not supported for %s" name
))
366 (with-connection-property v uname
367 (tramp-fish-send-command-and-check v
"#PWD")
368 (with-current-buffer (tramp-get-buffer v
)
369 (goto-char (point-min))
370 (buffer-substring (point) (tramp-compat-line-end-position)))))
371 (setq localname
(concat uname fname
))))
372 ;; There might be a double slash, for example when "~/"
373 ;; expands to "/". Remove this.
374 (while (string-match "//" localname
)
375 (setq localname
(replace-match "/" t t localname
)))
376 ;; No tilde characters in file name, do normal
377 ;; expand-file-name (this does "/./" and "/../"). We bind
378 ;; `directory-sep-char' here for XEmacs on Windows, which
379 ;; would otherwise use backslash. `default-directory' is
380 ;; bound, because on Windows there would be problems with UNC
381 ;; shares or Cygwin mounts.
382 (let ((directory-sep-char ?
/)
383 (default-directory (tramp-compat-temporary-file-directory)))
384 (tramp-make-tramp-file-name
386 (tramp-drop-volume-letter
387 (tramp-run-real-handler
388 'expand-file-name
(list localname
))))))))
390 (defun tramp-fish-handle-file-attributes (filename &optional id-format
)
391 "Like `file-attributes' for Tramp files."
392 (with-parsed-tramp-file-name (expand-file-name filename
) nil
393 (with-file-property v localname
(format "file-attributes-%s" id-format
)
394 (cdr (car (tramp-fish-get-file-entries v localname nil
))))))
396 (defun tramp-fish-handle-file-directory-p (filename)
397 "Like `file-directory-p' for Tramp files."
398 (let ((attributes (file-attributes filename
)))
400 (or (string-match "d" (nth 8 attributes
))
401 (and (file-symlink-p filename
)
402 (with-parsed-tramp-file-name filename nil
404 (tramp-make-tramp-file-name
405 method user host
(nth 0 attributes
))))))
408 (defun tramp-fish-handle-file-exists-p (filename)
409 "Like `file-exists-p' for Tramp files."
410 (and (file-attributes filename
) t
))
412 (defun tramp-fish-handle-file-executable-p (filename)
413 "Like `file-executable-p' for Tramp files."
414 (with-parsed-tramp-file-name (expand-file-name filename
) nil
415 (with-file-property v localname
"file-executable-p"
416 (when (file-exists-p filename
)
417 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
419 (tramp-make-tramp-file-name
421 (tramp-get-connection-property v
"home-directory" nil
))))
422 (or (and (char-equal (aref mode-chars
3) ?x
)
423 (equal (nth 2 (file-attributes filename
))
424 (nth 2 (file-attributes home-directory
))))
425 (and (char-equal (aref mode-chars
6) ?x
)
426 (equal (nth 3 (file-attributes filename
))
427 (nth 3 (file-attributes home-directory
))))
428 (char-equal (aref mode-chars
9) ?x
)))))))
430 (defun tramp-fish-handle-file-readable-p (filename)
431 "Like `file-readable-p' for Tramp files."
432 (with-parsed-tramp-file-name (expand-file-name filename
) nil
433 (with-file-property v localname
"file-readable-p"
434 (when (file-exists-p filename
)
435 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
437 (tramp-make-tramp-file-name
439 (tramp-get-connection-property v
"home-directory" nil
))))
440 (or (and (char-equal (aref mode-chars
1) ?r
)
441 (equal (nth 2 (file-attributes filename
))
442 (nth 2 (file-attributes home-directory
))))
443 (and (char-equal (aref mode-chars
4) ?r
)
444 (equal (nth 3 (file-attributes filename
))
445 (nth 3 (file-attributes home-directory
))))
446 (char-equal (aref mode-chars
7) ?r
)))))))
448 (defun tramp-fish-handle-file-writable-p (filename)
449 "Like `file-writable-p' for Tramp files."
450 (with-parsed-tramp-file-name (expand-file-name filename
) nil
451 (with-file-property v localname
"file-writable-p"
452 (if (not (file-exists-p filename
))
453 ;; If file doesn't exist, check if directory is writable.
454 (and (file-directory-p (file-name-directory filename
))
455 (file-writable-p (file-name-directory filename
)))
456 ;; Existing files must be writable.
457 (let ((mode-chars (string-to-vector (nth 8 (file-attributes filename
))))
459 (tramp-make-tramp-file-name
461 (tramp-get-connection-property v
"home-directory" nil
))))
462 (or (and (char-equal (aref mode-chars
2) ?w
)
463 (equal (nth 2 (file-attributes filename
))
464 (nth 2 (file-attributes home-directory
))))
465 (and (char-equal (aref mode-chars
5) ?w
)
466 (equal (nth 3 (file-attributes filename
))
467 (nth 3 (file-attributes home-directory
))))
468 (char-equal (aref mode-chars
8) ?w
)))))))
470 (defun tramp-fish-handle-file-local-copy (filename)
471 "Like `file-local-copy' for Tramp files."
472 (with-parsed-tramp-file-name (expand-file-name filename
) nil
473 (unless (file-exists-p filename
)
476 "Cannot make local copy of non-existing file `%s'" filename
))
477 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
478 (tramp-message v
4 "Fetching %s to tmp file %s..." filename tmpfile
)
479 (when (tramp-fish-retrieve-data v
)
481 (with-current-buffer (tramp-get-buffer v
)
482 (write-region (point-min) (point-max) tmpfile
))
483 (tramp-message v
4 "Fetching %s to tmp file %s...done" filename tmpfile
)
486 ;; This function should return "foo/" for directories and "bar" for
488 (defun tramp-fish-handle-file-name-all-completions (filename directory
)
489 "Like `file-name-all-completions' for Tramp files."
492 (with-parsed-tramp-file-name (expand-file-name directory
) nil
493 (with-file-property v localname
"file-name-all-completions"
496 (with-file-property v localname
"file-entries"
497 (tramp-fish-get-file-entries v localname t
))))
501 (if (string-match "d" (nth 9 x
))
502 (file-name-as-directory (nth 0 x
))
506 (defun tramp-fish-handle-file-newer-than-file-p (file1 file2
)
507 "Like `file-newer-than-file-p' for Tramp files."
509 ((not (file-exists-p file1
)) nil
)
510 ((not (file-exists-p file2
)) t
)
511 (t (tramp-time-less-p (nth 5 (file-attributes file2
))
512 (nth 5 (file-attributes file1
))))))
514 (defun tramp-fish-handle-insert-directory
515 (filename switches
&optional wildcard full-directory-p
)
516 "Like `insert-directory' for Tramp files.
517 WILDCARD and FULL-DIRECTORY-P are not handled."
518 (setq filename
(expand-file-name filename
))
519 (when (file-directory-p filename
)
520 ;; This check is a little bit strange, but in `dired-add-entry'
521 ;; this function is called with a non-directory ...
522 (setq filename
(file-name-as-directory filename
)))
524 (with-parsed-tramp-file-name filename nil
525 (tramp-flush-file-property v localname
)
528 (with-file-property v localname
"file-entries"
529 (tramp-fish-get-file-entries v localname t
))))
536 (if (string-match "t" switches
)
538 (tramp-time-less-p (nth 6 y
) (nth 6 x
))
540 (string-lessp (nth 0 x
) (nth 0 y
))))))
547 "%10s %3d %-8s %-8s %8s %s %s%s\n"
554 (if (tramp-time-less-p
555 (tramp-time-subtract (current-time) (nth 6 x
))
560 (nth 0 x
) ; file name
561 (if (stringp (nth 1 x
)) (format " -> %s" (nth 1 x
)) "")))
566 (defun tramp-fish-handle-insert-file-contents
567 (filename &optional visit beg end replace
)
568 "Like `insert-file-contents' for Tramp files."
569 (barf-if-buffer-read-only)
571 (setq buffer-file-name
(expand-file-name filename
))
572 (set-visited-file-modtime)
573 (set-buffer-modified-p nil
))
575 (with-parsed-tramp-file-name filename nil
576 (if (not (file-exists-p filename
))
578 v
'file-error
"File %s not found on remote host" filename
)
580 (let ((point (point))
582 (tramp-message v
4 "Fetching file %s..." filename
)
583 (when (tramp-fish-retrieve-data v
)
586 (with-current-buffer (tramp-get-buffer v
)
587 (let ((beg (or beg
(point-min)))
588 (end (min (or end
(point-max)) (point-max))))
589 (setq size
(- end beg
))
590 (buffer-substring beg end
))))
592 (tramp-message v
4 "Fetching file %s...done" filename
)
594 (list (expand-file-name filename
) size
)))))
596 (defun tramp-fish-handle-make-directory (dir &optional parents
)
597 "Like `make-directory' for Tramp files."
598 (setq dir
(directory-file-name (expand-file-name dir
)))
599 (unless (file-name-absolute-p dir
)
600 (setq dir
(expand-file-name dir default-directory
)))
601 (with-parsed-tramp-file-name dir nil
603 (let ((ldir (file-name-directory dir
)))
604 ;; Make missing directory parts
605 (when (and parents
(not (file-directory-p ldir
)))
606 (make-directory ldir parents
))
608 (when (file-directory-p ldir
)
609 (make-directory-internal dir
))
610 (unless (file-directory-p dir
)
611 (tramp-error v
'file-error
"Couldn't make directory %s" dir
))))))
613 (defun tramp-fish-handle-make-directory-internal (directory)
614 "Like `make-directory-internal' for Tramp files."
615 (setq directory
(directory-file-name (expand-file-name directory
)))
616 (unless (file-name-absolute-p directory
)
617 (setq directory
(expand-file-name directory default-directory
)))
618 (when (file-directory-p (file-name-directory directory
))
619 (with-parsed-tramp-file-name directory nil
622 (tramp-fish-send-command-and-check v
(format "#MKD %s" localname
))
624 v
'file-error
"Couldn't make directory %s" directory
))))))
626 (defun tramp-fish-handle-make-symbolic-link
627 (filename linkname
&optional ok-if-already-exists
)
628 "Like `make-symbolic-link' for Tramp files.
629 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
630 the symlink. If LINKNAME is a Tramp file, only the localname component is
631 used as the target of the symlink.
633 If LINKNAME is a Tramp file and the localname component is relative, then
634 it is expanded first, before the localname component is taken. Note that
635 this can give surprising results if the user/host for the source and
636 target of the symlink differ."
637 (with-parsed-tramp-file-name linkname nil
638 ;; Do the 'confirm if exists' thing.
639 (when (file-exists-p linkname
)
641 (if (or (null ok-if-already-exists
) ; not allowed to exist
642 (and (numberp ok-if-already-exists
)
645 "File %s already exists; make it a link anyway? "
648 v
'file-already-exists
"File %s already exists" localname
)
649 (delete-file linkname
)))
651 ;; If FILENAME is a Tramp name, use just the localname component.
652 (when (tramp-tramp-file-p filename
)
653 (setq filename
(tramp-file-name-localname
654 (tramp-dissect-file-name (expand-file-name filename
)))))
656 ;; Right, they are on the same host, regardless of user, method, etc.
657 ;; We now make the link on the remote machine. This will occur as the user
658 ;; that FILENAME belongs to.
660 (tramp-fish-send-command-and-check
661 v
(format "#SYMLINK %s %s" filename localname
))
662 (tramp-error v
'file-error
"Error creating symbolic link %s" linkname
))))
664 (defun tramp-fish-handle-rename-file
665 (filename newname
&optional ok-if-already-exists
)
666 "Like `rename-file' for Tramp files."
667 (tramp-fish-do-copy-or-rename-file
668 'rename filename newname ok-if-already-exists t
))
670 (defun tramp-fish-handle-set-file-modes (filename mode
)
671 "Like `set-file-modes' for Tramp files."
672 (with-parsed-tramp-file-name filename nil
673 (tramp-flush-file-property v localname
)
674 (unless (tramp-fish-send-command-and-check
675 v
(format "#CHMOD %s %s"
676 (tramp-decimal-to-octal mode
)
677 (tramp-shell-quote-argument localname
)))
679 v
'file-error
"Error while changing file's mode %s" filename
))))
681 (defun tramp-fish-handle-set-file-times (filename &optional time
)
682 "Like `set-file-times' for Tramp files."
683 (with-parsed-tramp-file-name filename nil
684 (let ((time (if (or (null time
) (equal time
'(0 0))) (current-time) time
)))
686 "touch" nil nil nil
"-t"
687 (format-time-string "%Y%m%d%H%M.%S" time
)
688 (tramp-shell-quote-argument localname
))))))
690 (defun tramp-fish-handle-write-region
691 (start end filename
&optional append visit lockname confirm
)
692 "Like `write-region' for Tramp files."
693 (setq filename
(expand-file-name filename
))
694 (with-parsed-tramp-file-name filename nil
695 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
696 (when (and (not (featurep 'xemacs
))
697 confirm
(file-exists-p filename
))
698 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
700 (tramp-error v
'file-error
"File not overwritten")))
702 (tramp-flush-file-property v localname
)
705 (let ((tramp-fish-ok-prompt-regexp
707 tramp-fish-ok-prompt-regexp
"\\|"
708 tramp-fish-continue-prompt-regexp
)))
709 (tramp-fish-send-command
710 v
(format "%s %d %s\n### 100"
711 (if append
"#APPEND" "#STOR") (- end start
) localname
)))
713 ;; Send data, if there are any.
715 (tramp-fish-send-command v
(buffer-substring-no-properties start end
)))
718 (set-visited-file-modtime))))
720 (defun tramp-fish-handle-executable-find (command)
721 "Like `executable-find' for Tramp files."
723 (if (zerop (process-file "which" nil t nil command
))
725 (goto-char (point-min))
726 (buffer-substring (point-min) (tramp-compat-line-end-position))))))
728 (defun tramp-fish-handle-process-file
729 (program &optional infile destination display
&rest args
)
730 "Like `process-file' for Tramp files."
731 ;; The implementation is not complete yet.
732 (when (and (numberp destination
) (zerop destination
))
733 (error "Implementation does not handle immediate return"))
735 (with-parsed-tramp-file-name default-directory nil
736 (let (command input tmpinput output tmpoutput stderr tmpstderr
739 (setq command
(mapconcat 'tramp-shell-quote-argument
740 (cons program args
) " "))
743 (setq input
"/dev/null")
744 (setq infile
(expand-file-name infile
))
745 (if (tramp-equal-remote default-directory infile
)
746 ;; INFILE is on the same remote host.
747 (setq input
(with-parsed-tramp-file-name infile nil localname
))
748 ;; INFILE must be copied to remote host.
749 (setq input
(tramp-make-tramp-temp-file v
)
750 tmpinput
(tramp-make-tramp-file-name method user host input
))
751 (copy-file infile tmpinput t
)))
752 (when input
(setq command
(format "%s <%s" command input
)))
755 (setq output
(tramp-make-tramp-temp-file v
)
756 tmpoutput
(tramp-make-tramp-file-name method user host output
))
759 ((bufferp destination
)
760 (setq outbuf destination
))
762 ((stringp destination
)
763 (setq outbuf
(get-buffer-create destination
)))
764 ;; (REAL-DESTINATION ERROR-DESTINATION)
768 ((bufferp (car destination
))
769 (setq outbuf
(car destination
)))
770 ((stringp (car destination
))
771 (setq outbuf
(get-buffer-create (car destination
)))))
774 ((stringp (cadr destination
))
775 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
776 (if (tramp-equal-remote default-directory
(cadr destination
))
777 ;; stderr is on the same remote host.
778 (setq stderr
(with-parsed-tramp-file-name
779 (cadr destination
) nil localname
))
780 ;; stderr must be copied to remote host. The temporary
781 ;; file must be deleted after execution.
782 (setq stderr
(tramp-make-tramp-temp-file v
)
783 tmpstderr
(tramp-make-tramp-file-name
784 method user host stderr
))))
785 ;; stderr to be discarded
786 ((null (cadr destination
))
787 (setq stderr
"/dev/null"))))
790 (setq outbuf
(current-buffer))))
791 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
793 ;; Goto working directory.
795 (tramp-fish-send-command-and-check
796 v
(format "#CWD %s" (tramp-shell-quote-argument localname
)))
797 (tramp-error v
'file-error
"No such directory: %s" default-directory
))
798 ;; Send the command. It might not return in time, so we protect it.
801 (unless (tramp-fish-send-command-and-check
804 (tramp-shell-quote-argument command
) output
))
806 ;; Check return code.
809 (tramp-make-tramp-file-name method user host output
)))
811 (insert-file-contents tmpfile
)
812 (goto-char (point-max))
814 (looking-at "^###RESULT: \\([0-9]+\\)")
815 (setq ret
(string-to-number (match-string 1)))
816 (delete-region (point) (point-max))
817 (write-region (point-min) (point-max) tmpfile
))
818 ;; We should show the output anyway.
820 (with-current-buffer outbuf
(insert-file-contents tmpfile
))
821 (when display
(display-buffer outbuf
))))
822 ;; When the user did interrupt, we should do it also.
823 (error (setq ret
1)))
825 ;; Provide error file.
826 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
828 (when tmpinput
(delete-file tmpinput
))
829 (when tmpoutput
(delete-file tmpoutput
))
830 ;; Return exit status.
834 ;; Internal file name functions
836 (defun tramp-fish-do-copy-or-rename-file
837 (op filename newname
&optional ok-if-already-exists keep-date preserve-uid-gid
)
838 "Copy or rename a remote file.
839 OP must be `copy' or `rename' and indicates the operation to
840 perform. FILENAME specifies the file to copy or rename, NEWNAME
841 is the name of the new file (for copy) or the new name of the
842 file (for rename). OK-IF-ALREADY-EXISTS means don't barf if
843 NEWNAME exists already. KEEP-DATE means to make sure that
844 NEWNAME has the same timestamp as FILENAME.
846 This function is invoked by `tramp-fish-handle-copy-file' and
847 `tramp-fish-handle-rename-file'. It is an error if OP is neither
848 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
850 (unless (memq op
'(copy rename
))
851 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
852 (let ((t1 (tramp-tramp-file-p filename
))
853 (t2 (tramp-tramp-file-p newname
)))
855 (unless ok-if-already-exists
856 (when (and t2
(file-exists-p newname
))
857 (with-parsed-tramp-file-name newname nil
859 v
'file-already-exists
"File %s already exists" newname
))))
863 ;; Both are Tramp files.
866 ;; Shortcut: if method, host, user are the same for both
867 ;; files, we invoke `cp' or `mv' on the remote host
869 ((tramp-equal-remote filename newname
)
870 (tramp-fish-do-copy-or-rename-file-directly
871 op filename newname keep-date preserve-uid-gid
))
872 ;; No shortcut was possible. So we copy the
873 ;; file first. If the operation was `rename', we go
874 ;; back and delete the original file (if the copy was
875 ;; successful). The approach is simple-minded: we
876 ;; create a new buffer, insert the contents of the
877 ;; source file into it, then write out the buffer to
878 ;; the target file. The advantage is that it doesn't
879 ;; matter which filename handlers are used for the
880 ;; source and target file.
882 (tramp-do-copy-or-rename-file-via-buffer
883 op filename newname keep-date
))))
885 ;; One file is a Tramp file, the other one is local.
887 ;; Use the generic method via a Tramp buffer.
888 (tramp-do-copy-or-rename-file-via-buffer
889 op filename newname keep-date
))
892 ;; One of them must be a Tramp file.
893 (error "Tramp implementation says this cannot happen")))
894 ;; When newname did exist, we have wrong cached values.
896 (with-parsed-tramp-file-name newname nil
897 (tramp-flush-file-property v localname
)
898 (tramp-flush-file-property v
(file-name-directory localname
)))))))
900 (defun tramp-fish-do-copy-or-rename-file-directly
901 (op filename newname keep-date preserve-uid-gid
)
902 "Invokes `COPY' or `RENAME' on the remote system.
903 OP must be one of `copy' or `rename', indicating `cp' or `mv',
904 respectively. VEC specifies the connection. LOCALNAME1 and
905 LOCALNAME2 specify the two arguments of `cp' or `mv'. If
906 KEEP-DATE is non-nil, preserve the time stamp when copying.
907 PRESERVE-UID-GID is completely ignored."
908 (with-parsed-tramp-file-name filename v1
909 (with-parsed-tramp-file-name newname v2
910 (tramp-fish-send-command
913 (if (eq op
'copy
) "#COPY" "#RENAME")
914 (tramp-shell-quote-argument v1-localname
)
915 (tramp-shell-quote-argument v2-localname
)))))
916 ;; KEEP-DATE handling.
917 (when (and keep-date
(functionp 'set-file-times
))
918 (set-file-times newname
(nth 5 (file-attributes filename
))))
920 (set-file-modes newname
(file-modes filename
)))
922 (defun tramp-fish-get-file-entries (vec localname list
)
923 "Read entries returned by FISH server.
924 When LIST is true, a #LIST command will be sent, including all entries
925 of a directory. Otherwise, #STAT is sent for just one entry.
926 Result is a list of (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
927 SIZE MODE WEIRD INODE DEVICE)."
929 (with-current-buffer (tramp-get-buffer vec
)
930 ;; #LIST does not work properly with trailing "/", at least in
932 (when (string-match "/$" localname
)
933 (setq localname
(concat localname
".")))
935 (let ((command (format "%s %s" (if list
"#LIST" "#STAT") localname
))
936 buffer-read-only num res
)
939 (tramp-fish-send-command vec command
)
941 ;; Read number of entries
942 (goto-char (point-min))
944 (unless (integerp (setq num
(read (current-buffer)))) (error nil
))
945 (error (return nil
)))
947 (delete-region (point-min) (point))
950 (goto-char (point-min))
952 (unless (looking-at tramp-fish-continue-prompt-regexp
) (error nil
))
953 (error (return nil
)))
955 (delete-region (point-min) (point))
959 (let ((item (tramp-fish-read-file-entry)))
960 ;; Add inode and device.
963 (list (tramp-get-inode vec
)
964 (tramp-get-device vec
))))))
967 (goto-char (point-min))
969 (unless (looking-at tramp-fish-ok-prompt-regexp
) (error nil
))
972 "`%s' does not return a valid Lisp expression: `%s'"
973 command
(buffer-string))))
975 (delete-region (point-min) (point))
979 (defun tramp-fish-read-file-entry ()
980 "Parse entry in output buffer.
981 Result is the list (LOCALNAME LINK COUNT UID GID ATIME MTIME CTIME
983 ;; We are called from `tramp-fish-get-file-entries', which sets the
985 (let (buffer-read-only localname link uid gid mtime size mode
)
989 ;; P<unix permissions> <owner>.<group>
990 ((looking-at "^P\\(.+\\)\\s-\\(.+\\)\\.\\(.+\\)$")
991 (setq mode
(match-string 1))
992 (setq uid
(match-string 2))
993 (setq gid
(match-string 3))
994 (when (string-match "^d" mode
) (setq link t
)))
996 ((looking-at "^S\\([0-9]+\\)$")
997 (setq size
(string-to-number (match-string 1))))
998 ;; D<year> <month> <day> <hour> <minute> <second>[.1234]
1000 "^D\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\([0-9]+\\)\\s-\\(\\S-+\\)$")
1003 (string-to-number (match-string 6))
1004 (string-to-number (match-string 5))
1005 (string-to-number (match-string 4))
1006 (string-to-number (match-string 3))
1007 (string-to-number (match-string 2))
1008 (string-to-number (match-string 1)))))
1009 ;; d<3-letters month name> <day> <year or HH:MM>
1010 ((looking-at "^d") nil
)
1011 ;; E<major-of-device>,<minor>
1012 ((looking-at "^E") nil
)
1014 ((looking-at "^:\\(.+\\)$")
1015 (setq localname
(match-string 1)))
1016 ;; L<filename symlink points to>
1017 ((looking-at "^L\\(.+\\)$")
1018 (setq link
(match-string 1)))
1020 ((looking-at "^M\\(.+\\)$") nil
)
1026 (delete-region (point-min) (point))))
1028 ;; delete trailing empty line
1030 (delete-region (point-min) (point))
1032 ;; Return entry in file-attributes format
1033 (list localname link -
1 uid gid
'(0 0) mtime
'(0 0) size mode nil
)))
1035 (defun tramp-fish-retrieve-data (vec)
1036 "Reads remote data for FISH protocol.
1037 The data are left in the connection buffer of VEC for further processing.
1038 Returns the size of the data."
1040 (with-current-buffer (tramp-get-buffer vec
)
1041 ;; The retrieved data might be in binary format, without
1042 ;; trailing newline. Therefore, the OK prompt might not start
1043 ;; at the beginning of a line.
1044 (let ((tramp-fish-ok-prompt-regexp "### 200\n")
1048 (tramp-fish-send-command
1049 vec
(format "#RETR %s" (tramp-file-name-localname vec
)))
1052 (goto-char (point-min))
1054 (unless (integerp (setq size
(read (current-buffer)))) (error nil
))
1055 (error (return nil
)))
1057 (delete-region (point-min) (point))
1060 (goto-char (point-min))
1062 (unless (looking-at tramp-fish-continue-prompt-regexp
) (error nil
))
1063 (error (return nil
)))
1065 (delete-region (point-min) (point))
1067 ;; The received data might contain the OK prompt already, so
1068 ;; there might be outstanding data.
1069 (while (/= (+ size
(length tramp-fish-ok-prompt-regexp
))
1070 (- (point-max) (point-min)))
1071 (tramp-wait-for-regexp
1072 (tramp-get-connection-process vec
) nil
1073 (concat tramp-fish-ok-prompt-regexp
"$")))
1076 (goto-char (+ (point-min) size
))
1078 (unless (looking-at tramp-fish-ok-prompt-regexp
) (error nil
))
1079 (error (return nil
)))
1080 (delete-region (+ (point-min) size
) (point-max))
1084 ;; Connection functions
1086 (defun tramp-fish-maybe-open-connection (vec)
1087 "Maybe open a connection VEC.
1088 Does not do anything if a connection is already open, but re-opens the
1089 connection if a previous connection has died for some reason."
1090 (let ((process-connection-type tramp-process-connection-type
)
1091 (p (get-buffer-process (tramp-get-buffer vec
))))
1093 ;; New connection must be opened.
1094 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
1096 ;; Set variables for computing the prompt for reading password.
1097 (setq tramp-current-method
(tramp-file-name-method vec
)
1098 tramp-current-user
(tramp-file-name-user vec
)
1099 tramp-current-host
(tramp-file-name-host vec
))
1101 ;; Start new process.
1102 (when (and p
(processp p
))
1104 (setenv "TERM" tramp-terminal-type
)
1107 vec
3 "Opening connection for %s@%s using %s..."
1108 tramp-current-user tramp-current-host tramp-current-method
)
1110 (let* ((process-connection-type tramp-process-connection-type
)
1111 (inhibit-eol-conversion nil
)
1112 (coding-system-for-read 'binary
)
1113 (coding-system-for-write 'binary
)
1114 ;; This must be done in order to avoid our file name handler.
1115 (p (let ((default-directory
1116 (tramp-compat-temporary-file-directory)))
1118 (or (tramp-get-connection-property vec
"process-name" nil
)
1119 (tramp-buffer-name vec
))
1120 (tramp-get-connection-buffer vec
)
1122 (tramp-file-name-user vec
)
1123 (tramp-file-name-host vec
)))))
1124 (tramp-message vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1126 ;; Check whether process is alive.
1127 (tramp-set-process-query-on-exit-flag p nil
)
1129 (tramp-process-actions p vec tramp-actions-before-shell
60)
1130 (tramp-fish-send-command vec tramp-fish-start-fish-server-command
)
1133 "Found remote shell prompt on `%s'" (tramp-file-name-host vec
))))))
1135 (defun tramp-fish-send-command (vec command
)
1136 "Send the COMMAND to connection VEC."
1137 (tramp-fish-maybe-open-connection vec
)
1138 (tramp-message vec
6 "%s" command
)
1139 (tramp-send-string vec command
)
1140 (tramp-wait-for-regexp
1141 (tramp-get-connection-process vec
) nil
1142 (concat tramp-fish-ok-prompt-regexp
"\\|" tramp-fish-error-prompt-regexp
)))
1144 (defun tramp-fish-send-command-and-check (vec command
)
1145 "Send the COMMAND to connection VEC.
1146 Returns nil if there has been an error message."
1149 (tramp-fish-send-command vec command
)
1151 ;; Read return code.
1152 (with-current-buffer (tramp-get-buffer vec
)
1153 (goto-char (point-min))
1154 (looking-at tramp-fish-ok-prompt-regexp
)))
1156 (provide 'tramp-fish
)
1160 ;; * Evaluate the MIME information with #LIST or #STAT.
1163 ;; arch-tag: a66df7df-5f29-42a7-a921-643ceb29db49
1164 ;;;; tramp-fish.el ends here