1 /* Virtual File System: FISH implementation for transfering files over
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007 Free Software Foundation, Inc.
7 Written by: 1998 Pavel Machek
8 Spaces fix: 2000 Michal Svec
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public License
14 as published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Library General Public License for more details.
22 You should have received a copy of the GNU Library General Public
23 License along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 * \brief Source: Virtual File System: FISH implementation for transfering files over
30 * \author Pavel Machek
34 * Derived from ftpfs.c
35 * Read README.fish for protocol specification.
37 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
38 * where C means you want compressed connection,
39 * and r means you want to use rsh
41 * Namespace: fish_vfs_ops exported.
44 /* Define this if your ssh can take -I option */
51 #include <sys/time.h> /* gettimeofday() */
55 #include "lib/global.h"
57 #include "lib/tty/tty.h" /* enable/disable interrupt key */
58 #include "lib/strescape.h"
59 #include "lib/unixcompat.h"
61 #include "src/wtools.h" /* message() */
62 #include "src/main.h" /* print_vfs_message */
64 #include "xdirentry.h"
67 #include "gc.h" /* vfs_stamp_create */
71 int fish_directory_timeout
= 900;
73 #define DO_RESOLVE_SYMLINK 1
75 #define DO_FREE_RESOURCE 4
77 #define FISH_FLAG_COMPRESSED 1
78 #define FISH_FLAG_RSH 2
81 #define OPT_IGNORE_ERROR 2
86 #define PRELIM 1 /* positive preliminary */
87 #define COMPLETE 2 /* positive completion */
88 #define CONTINUE 3 /* positive intermediate */
89 #define TRANSIENT 4 /* transient negative completion */
90 #define ERROR 5 /* permanent negative completion */
92 /* command wait_flag: */
94 #define WAIT_REPLY 0x01
95 #define WANT_STRING 0x02
96 static char reply_str
[80];
98 static struct vfs_class vfs_fish_ops
;
101 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
,
102 int wait_reply
, const char *fmt
, ...) __attribute__ ((format (__printf__
, 4, 5)));
105 fish_decode_reply (char *s
, int was_garbage
)
108 if (!sscanf (s
, "%d", &code
))
114 return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
118 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
120 fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
127 if (!vfs_s_get_line (me
, sock
, answer
, sizeof (answer
), '\n'))
134 if (strncmp (answer
, "### ", 4))
138 g_strlcpy (string_buf
, answer
, string_len
);
141 return fish_decode_reply (answer
+ 4, was_garbage
);
145 #define SUP super->u.fish
148 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
, int wait_reply
, const char *fmt
, ...)
153 FILE *logfile
= MEDATA
->logfile
;
157 str
= g_strdup_vprintf (fmt
, ap
);
163 ret
= fwrite (str
, strlen (str
), 1, logfile
);
164 ret
= fflush (logfile
);
167 tty_enable_interrupt_key ();
169 status
= write (SUP
.sockw
, str
, strlen (str
));
172 tty_disable_interrupt_key ();
177 return fish_get_reply (me
, SUP
.sockr
,
178 (wait_reply
& WANT_STRING
) ? reply_str
:
179 NULL
, sizeof (reply_str
) - 1);
184 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
186 if ((SUP
.sockw
!= -1) || (SUP
.sockr
!= -1))
188 print_vfs_message (_("fish: Disconnecting from %s"), super
->name
? super
->name
: "???");
189 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
192 SUP
.sockw
= SUP
.sockr
= -1;
197 g_free (SUP
.password
);
201 fish_pipeopen (struct vfs_s_super
*super
, const char *path
, const char *argv
[])
203 int fileset1
[2], fileset2
[2];
206 if ((pipe (fileset1
) < 0) || (pipe (fileset2
) < 0))
207 vfs_die ("Cannot pipe(): %m.");
212 vfs_die ("Cannot fork(): %m.");
213 /* We are the parent */
215 SUP
.sockw
= fileset1
[1];
217 SUP
.sockr
= fileset2
[0];
221 res
= dup2 (fileset1
[0], 0);
224 res
= dup2 (fileset2
[1], 1);
226 /* stderr to /dev/null */
227 res
= open ("/dev/null", O_WRONLY
);
230 execvp (path
, const_cast (char **, argv
));
235 /* The returned directory should always contain a trailing slash */
237 fish_getcwd (struct vfs_class
*me
, struct vfs_s_super
*super
)
239 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
240 return g_strconcat (reply_str
, "/", (char *) NULL
);
245 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
249 const char *argv
[10]; /* All of 10 is used now */
250 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
254 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
257 if (SUP
.flags
> FISH_FLAG_RSH
)
260 g_snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
265 * Add the user name to the ssh command line only if it was explicitly
266 * set in vfs URL. rsh/ssh will get current user by default
267 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
268 * option breaks it for some)
274 argv
[i
++] = SUP
.user
;
278 /* The rest of the code assumes it to be a valid username */
279 SUP
.user
= vfs_get_local_username ();
282 argv
[i
++] = SUP
.host
;
283 argv
[i
++] = "echo FISH:; /bin/sh";
286 fish_pipeopen (super
, xsh
, argv
);
290 print_vfs_message (_("fish: Waiting for initial line..."));
291 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
292 ERRNOR (E_PROTO
, -1);
293 print_vfs_message ("%s", answer
);
294 if (strstr (answer
, "assword"))
296 /* Currently, this does not work. ssh reads passwords from
297 /dev/tty, not from stdin :-(. */
299 message (D_ERROR
, MSG_ERROR
,
300 _("Sorry, we cannot do password authenticated connections for now."));
305 p
= g_strconcat (_(" fish: Password required for "), SUP
.user
, " ", (char *) NULL
);
306 op
= vfs_get_password (p
);
312 print_vfs_message (_("fish: Sending password..."));
316 str_len
= strlen (SUP
.password
);
317 if ((write (SUP
.sockw
, SUP
.password
, str_len
) != (ssize_t
) str_len
)
318 || (write (SUP
.sockw
, "\n", 1) != 1))
326 print_vfs_message (_("fish: Sending initial line..."));
328 * Run `start_fish_server'. If it doesn't exist - no problem,
329 * we'll talk directly to the shell.
332 (me
, super
, WAIT_REPLY
,
333 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE
)
334 ERRNOR (E_PROTO
, -1);
336 print_vfs_message (_("fish: Handshaking version..."));
337 if (fish_command (me
, super
, WAIT_REPLY
, "#VER 0.0.0\necho '### 000'\n") != COMPLETE
)
338 ERRNOR (E_PROTO
, -1);
340 /* Set up remote locale to C, otherwise dates cannot be recognized */
342 (me
, super
, WAIT_REPLY
,
343 "LANG=C; LC_ALL=C; LC_TIME=C\n"
344 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE
)
345 ERRNOR (E_PROTO
, -1);
347 print_vfs_message (_("fish: Setting up current directory..."));
348 SUP
.cwdir
= fish_getcwd (me
, super
);
349 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
351 super
->name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
353 super
->name
= g_strdup (PATH_SEP_STR
);
355 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
360 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
361 const char *archive_name
, char *op
)
363 char *host
, *user
, *password
, *p
;
368 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
369 &password
, 0, URL_NOSLASH
| URL_USE_ANONYMOUS
);
376 if (!strncmp (op
, "rsh:", 4))
377 SUP
.flags
= FISH_FLAG_RSH
;
380 SUP
.password
= password
;
381 return fish_open_archive_int (me
, super
);
385 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
386 const char *archive_name
, char *op
, void *cookie
)
396 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
397 URL_NOSLASH
| URL_USE_ANONYMOUS
);
402 user
= vfs_get_local_username ();
404 result
= ((strcmp (host
, SUP
.host
) == 0)
405 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
414 fish_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
416 struct vfs_s_super
*super
= dir
->super
;
418 struct vfs_s_entry
*ent
= NULL
;
422 gchar
*shell_commands
;
426 * Simple FISH debug interface :]
428 if (!(MEDATA
->logfile
))
430 MEDATA
->logfile
= fopen ("/tmp/mc-FISH.sh", "w");
434 logfile
= MEDATA
->logfile
;
436 print_vfs_message (_("fish: Reading directory %s..."), remote_path
);
438 gettimeofday (&dir
->timestamp
, NULL
);
439 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
440 quoted_path
= strutils_shell_escape (remote_path
);
442 shell_commands
= g_strconcat (
444 "if `perl -v > /dev/null 2>&1` ; then\n"
449 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
450 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
451 "my $dirname = $ARGV[0];\n"
452 "if (opendir ( DIR, $dirname )) {\n"
453 "while( (my $filename = readdir(DIR))){\n"
454 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
455 "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
457 "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
458 "my $e_filename = $filename;\n"
459 "$e_filename =~ $strutils_shell_escape_regex;\n"
460 "if (S_ISLNK($mode) ) {\n"
461 "my $linkname = readlink (\"$dirname/$filename\");\n"
462 "$linkname =~ $strutils_shell_escape_regex;\n"
464 "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
466 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
467 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
469 "printf(\"R%%o %%o $uid.$gid\\n"
472 ":\\\"$e_filename\\\"\\n"
473 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
476 "printf(\"### 200\\n\");\n"
479 "printf(\"### 500\\n\");\n"
482 "' /%s ||\n" /* ARGV[0] - path to browse */
483 " echo '### 500'\n" /* do not hang if perl is to eval it */
484 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
485 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
492 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
493 "while read p l u g s m d y n n2 n3; do\n"
494 "if test \"$m\" = \"0\" ; then \n"
495 "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
497 "n=$n\" \"$n2\" \"$n3\n"
499 "if [ $ADD = 0 ]; then\n"
500 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
501 "elif `sed --version >/dev/null 2>&1` ; then\n"
502 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
503 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
505 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
508 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
509 "while read p l u g a i m d y n n2 n3; do\n"
510 "if test \"$a\" = \"0\" ; then \n"
511 "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
513 "n=$n\" \"$n2\" \"$n3\n"
515 "if [ $ADD = 0 ]; then\n"
516 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
517 "elif `sed --version >/dev/null 2>&1` ; then\n"
518 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
519 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
521 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
532 fish_command (me
, super
, NONE
, shell_commands
,
533 quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
);
535 g_free (shell_commands
);
536 g_free (quoted_path
);
537 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
540 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
541 if ((!res
) || (res
== EINTR
))
543 vfs_s_free_entry (me
, ent
);
544 me
->verrno
= ECONNRESET
;
549 fputs (buffer
, logfile
);
550 fputs ("\n", logfile
);
553 if (!strncmp (buffer
, "### ", 4))
559 vfs_s_insert_entry (me
, dir
, ent
);
560 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
565 #define ST ent->ino->st
572 char *data_start
= buffer
+ 1;
573 char *filename
= data_start
;
574 char *linkname
= data_start
;
575 char *filename_bound
= filename
+ strlen (filename
);
576 char *linkname_bound
= filename_bound
;
577 if (!strcmp (data_start
, "\".\"") || !strcmp (data_start
, "\"..\""))
578 break; /* We'll do "." and ".." ourselves */
580 if (S_ISLNK (ST
.st_mode
))
582 /* we expect: "escaped-name" -> "escaped-name"
583 // -> cannot occur in filenames,
584 // because it will be escaped to -\> */
586 if (*filename
== '"')
589 linkname
= strstr (filename
, "\" -> \"");
592 /* broken client, or smth goes wrong */
593 linkname
= filename_bound
;
594 if (filename_bound
> filename
&& *(filename_bound
- 1) == '"')
595 --filename_bound
; /* skip trailing " */
599 filename_bound
= linkname
;
600 linkname
+= 6; /* strlen ("\" -> \"") */
601 if (*(linkname_bound
- 1) == '"')
602 --linkname_bound
; /* skip trailing " */
605 ent
->name
= str_dup_range (filename
, filename_bound
);
607 ent
->name
= strutils_shell_unescape (ent
->name
);
610 ent
->ino
->linkname
= str_dup_range (linkname
, linkname_bound
);
611 temp
= ent
->ino
->linkname
;
612 ent
->ino
->linkname
= strutils_shell_unescape (ent
->ino
->linkname
);
617 /* we expect: "escaped-name" */
618 if (filename_bound
- filename
> 2)
621 there is at least 2 "
624 if (*filename
== '"')
626 if (*(filename_bound
- 1) == '"')
629 ent
->name
= str_dup_range (filename
, filename_bound
);
631 ent
->name
= strutils_shell_unescape (ent
->name
);
638 ST
.st_size
= (off_t
) atoll (buffer
+ 1);
640 ST
.st_size
= (off_t
) atof (buffer
+ 1);
646 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
653 we expect: Roctal-filemode octal-filetype uid.gid
656 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
661 vfs_split_text (buffer
+ 1);
662 if (!vfs_parse_filedate (0, &ST
.st_ctime
))
664 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
670 if (sscanf (buffer
+ 1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
671 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
673 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime (&tim
);
679 if (sscanf (buffer
+ 1, "%d,%d", &maj
, &min
) != 2)
681 #ifdef HAVE_STRUCT_STAT_ST_RDEV
682 ST
.st_rdev
= makedev (maj
, min
);
688 vfs_s_free_entry (me
, ent
);
689 reply_code
= fish_decode_reply (buffer
+ 4, 0);
690 if (reply_code
== COMPLETE
)
693 SUP
.cwdir
= g_strdup (remote_path
);
694 print_vfs_message (_("%s: done."), me
->name
);
697 else if (reply_code
== ERROR
)
703 me
->verrno
= E_REMOTE
;
707 print_vfs_message (_("%s: failure"), me
->name
);
712 fish_file_store (struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
714 struct vfs_s_super
*super
= FH_SUPER
;
722 h
= open (localname
, O_RDONLY
);
726 if (fstat (h
, &s
) < 0)
732 /* First, try this as stor:
734 * ( head -c number ) | ( cat > file; cat >/dev/null )
736 * If `head' is not present on the remote system, `dd' will be used.
737 * Unfortunately, we cannot trust most non-GNU `head' implementations
738 * even if `-c' options is supported. Therefore, we separate GNU head
739 * (and other modern heads?) using `-q' and `-' . This causes another
740 * implementations to fail (because of "incorrect options").
745 * while [ $rest -gt 0 ]
747 * cnt=`expr \( $rest + 255 \) / 256`
748 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
749 * rest=`expr $rest - $n`
752 * `dd' was not designed for full filling of input buffers,
753 * and does not report exact number of bytes (not blocks).
754 * Therefore a more complex shell script is needed.
756 * On some systems non-GNU head writes "Usage:" error report to stdout
757 * instead of stderr. It makes impossible the use of "head || dd"
758 * algorithm for file appending case, therefore just "dd" is used for it.
761 quoted_name
= strutils_shell_escape (name
);
762 print_vfs_message (_("fish: store %s: sending command..."), quoted_name
);
764 /* FIXME: File size is limited to ULONG_MAX */
765 if (!fh
->u
.fish
.append
)
768 n
= fish_command (me
, super
, WAIT_REPLY
,
774 "head -c %lu -q - || echo DD >&3\n"
775 ") 2>/dev/null | (\n"
778 ")`; [ \"$res\" = DD ] && {\n"
781 "while [ $rest -gt 0 ]\n"
783 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
784 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
785 " rest=`expr $rest - $n`\n"
787 "}; echo '### 200'\n",
788 (unsigned long) s
.st_size
, quoted_name
,
789 quoted_name
, (unsigned long) s
.st_size
, (unsigned long) s
.st_size
);
795 n
= fish_command (me
, super
, WAIT_REPLY
,
801 "while [ $rest -gt 0 ]\n"
803 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
804 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
805 " rest=`expr $rest - $n`\n"
807 "}; echo '### 200'\n",
808 (unsigned long) s
.st_size
, quoted_name
,
809 quoted_name
, (unsigned long) s
.st_size
);
815 ERRNOR (E_REMOTE
, -1);
823 while ((n
= read (h
, buffer
, sizeof (buffer
))) < 0)
825 if ((errno
== EINTR
) && tty_got_interrupt ())
827 print_vfs_message (_("fish: Local read failed, sending zeros"));
829 h
= open ("/dev/zero", O_RDONLY
);
833 if ((t
= write (SUP
.sockw
, buffer
, n
)) != n
)
845 tty_disable_interrupt_key ();
847 print_vfs_message (_("fish: storing %s %d (%lu)"),
848 was_error
? _("zeros") : _("file"), total
, (unsigned long) s
.st_size
);
851 g_free (quoted_name
);
852 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
853 ERRNOR (E_REMOTE
, -1);
857 fish_get_reply (me
, SUP
.sockr
, NULL
, 0);
858 g_free (quoted_name
);
863 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
868 ERRNOR (E_NOTSUPP
, 0);
869 name
= vfs_s_fullpath (me
, fh
->ino
);
872 quoted_name
= strutils_shell_escape (name
);
874 fh
->u
.fish
.append
= 0;
877 * Check whether the remote file is readable by using `dd' to copy
878 * a single byte from the remote file to /dev/null. If `dd' completes
879 * with exit status of 0 use `cat' to send the file contents to the
880 * standard output (i.e. over the network).
883 offset
= fish_command (me
, FH_SUPER
, WANT_STRING
,
885 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
887 "ls -ln /%s 2>/dev/null | (\n"
897 quoted_name
, quoted_name
, quoted_name
, quoted_name
);
899 g_free (quoted_name
);
900 if (offset
!= PRELIM
)
901 ERRNOR (E_REMOTE
, 0);
902 fh
->linear
= LS_LINEAR_OPEN
;
905 #if SIZEOF_OFF_T == SIZEOF_LONG
906 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
908 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
911 ERRNOR (E_REMOTE
, 0);
916 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
918 struct vfs_s_super
*super
= FH_SUPER
;
922 print_vfs_message (_("Aborting transfer..."));
925 n
= MIN (8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
928 if ((n
= read (SUP
.sockr
, buffer
, n
)) < 0)
935 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
936 print_vfs_message (_("Error reported after abort."));
938 print_vfs_message (_("Aborted transfer would be successful."));
942 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
944 struct vfs_s_super
*super
= FH_SUPER
;
946 len
= MIN (fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
947 tty_disable_interrupt_key ();
948 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
)) < 0))
950 if ((errno
== EINTR
) && !tty_got_interrupt ())
954 tty_enable_interrupt_key ();
959 fish_linear_abort (me
, fh
);
960 if ((!n
) && ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)))
961 ERRNOR (E_REMOTE
, -1);
966 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
968 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
969 fish_linear_abort (me
, fh
);
973 fish_ctl (void *fh
, int ctlop
, void *arg
)
982 case VFS_CTL_IS_NOTREADY
:
987 vfs_die ("You may not do this");
988 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
991 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
992 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
1003 fish_send_command (struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
1007 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
1008 vfs_stamp_create (&vfs_fish_ops
, super
);
1010 ERRNOR (E_REMOTE
, -1);
1011 if (flags
& OPT_FLUSH
)
1012 vfs_s_invalidate (me
, super
);
1017 char buf[BUF_LARGE]; \
1018 const char *crpath; \
1019 char *rpath, *mpath = g_strdup (path); \
1020 struct vfs_s_super *super; \
1021 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) \
1026 rpath = strutils_shell_escape(crpath); \
1029 #define POSTFIX(flags) \
1031 return fish_send_command(me, super, buf, flags);
1034 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1038 g_snprintf (buf
, sizeof (buf
),
1039 "#CHMOD %4.4o /%s\n"
1040 "if chmod %4.4o /%s 2>/dev/null; then\n"
1045 mode
& 07777, rpath
, mode
& 07777, rpath
);
1046 POSTFIX (OPT_FLUSH
);
1051 #define FISH_OP(name, string) \
1052 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1054 char buf[BUF_LARGE]; \
1055 const char *crpath1, *crpath2; \
1056 char *rpath1, *rpath2, *mpath1, *mpath2; \
1057 struct vfs_s_super *super1, *super2; \
1058 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) \
1063 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) \
1069 rpath1 = strutils_shell_escape (crpath1); \
1071 rpath2 = strutils_shell_escape (crpath2); \
1073 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1076 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1081 "if mv /%s /%s 2>/dev/null; then\n"
1089 "if ln /%s /%s 2>/dev/null; then\n"
1097 fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
1100 PREFIX qsetto
= strutils_shell_escape (setto
);
1102 g_snprintf (buf
, sizeof (buf
),
1104 "if ln -s %s /%s 2>/dev/null; then\n"
1109 qsetto
, rpath
, qsetto
, rpath
);
1112 POSTFIX (OPT_FLUSH
);
1116 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
1118 char *sowner
, *sgroup
;
1122 if ((pw
= getpwuid (owner
)) == NULL
)
1125 if ((gr
= getgrgid (group
)) == NULL
)
1128 sowner
= pw
->pw_name
;
1129 sgroup
= gr
->gr_name
;
1133 g_snprintf (buf
, sizeof (buf
),
1134 "#CHOWN %s:%s /%s\n"
1135 "if chown %s:%s /%s 2>/dev/null; then\n"
1140 sowner
, sgroup
, rpath
, sowner
, sgroup
, rpath
);
1142 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1143 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1144 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1145 POSTFIX (OPT_FLUSH
)}
1149 fish_unlink (struct vfs_class
*me
, const char *path
)
1154 g_snprintf (buf
, sizeof (buf
),
1156 "if rm -f /%s 2>/dev/null; then\n"
1164 POSTFIX (OPT_FLUSH
);
1168 fish_exists (struct vfs_class
*me
, const char *path
)
1173 g_snprintf (buf
, sizeof (buf
),
1175 "ls -l /%s >/dev/null 2>/dev/null\n"
1182 if (fish_send_command (me
, super
, buf
, OPT_FLUSH
) == 0)
1190 fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1197 g_snprintf (buf
, sizeof (buf
),
1199 "if mkdir /%s 2>/dev/null; then\n"
1208 ret_code
= fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1213 if (!fish_exists (me
, path
))
1215 ERRNOR (EACCES
, -1);
1221 fish_rmdir (struct vfs_class
*me
, const char *path
)
1225 g_snprintf (buf
, sizeof (buf
),
1227 "if rmdir /%s 2>/dev/null; then\n"
1234 POSTFIX (OPT_FLUSH
);
1238 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
, int mode
)
1242 fh
->u
.fish
.append
= 0;
1243 /* File will be written only, so no need to retrieve it */
1244 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
)))
1246 fh
->u
.fish
.append
= flags
& O_APPEND
;
1247 if (!fh
->ino
->localname
)
1249 int tmp_handle
= vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1250 fh
->ino
->ent
->name
);
1251 if (tmp_handle
== -1)
1257 if (!fh
->ino
->localname
)
1258 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1260 if (!fh
->ino
->localname
)
1261 vfs_die ("retrieve_file failed to fill in localname");
1266 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1268 struct vfs_s_super
*super
= MEDATA
->supers
;
1275 const char *flags
= "";
1281 case FISH_FLAG_COMPRESSED
:
1285 if (SUP
.flags
> FISH_FLAG_RSH
)
1288 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1294 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
, "/", SUP
.cwdir
, (char *) NULL
);
1297 super
= super
->next
;
1302 fish_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1305 sorry, i've places hack here
1306 cause fish don't able to open files with O_EXCL flag
1309 return vfs_s_open (me
, file
, flags
, mode
);
1316 static struct vfs_s_subclass fish_subclass
;
1320 fish_subclass
.flags
= VFS_S_REMOTE
;
1321 fish_subclass
.archive_same
= fish_archive_same
;
1322 fish_subclass
.open_archive
= fish_open_archive
;
1323 fish_subclass
.free_archive
= fish_free_archive
;
1324 fish_subclass
.fh_open
= fish_fh_open
;
1325 fish_subclass
.dir_load
= fish_dir_load
;
1326 fish_subclass
.file_store
= fish_file_store
;
1327 fish_subclass
.linear_start
= fish_linear_start
;
1328 fish_subclass
.linear_read
= fish_linear_read
;
1329 fish_subclass
.linear_close
= fish_linear_close
;
1331 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1332 vfs_fish_ops
.name
= "fish";
1333 vfs_fish_ops
.prefix
= "sh:";
1334 vfs_fish_ops
.fill_names
= fish_fill_names
;
1335 vfs_fish_ops
.chmod
= fish_chmod
;
1336 vfs_fish_ops
.chown
= fish_chown
;
1337 vfs_fish_ops
.open
= fish_open
;
1338 vfs_fish_ops
.symlink
= fish_symlink
;
1339 vfs_fish_ops
.link
= fish_link
;
1340 vfs_fish_ops
.unlink
= fish_unlink
;
1341 vfs_fish_ops
.rename
= fish_rename
;
1342 vfs_fish_ops
.mkdir
= fish_mkdir
;
1343 vfs_fish_ops
.rmdir
= fish_rmdir
;
1344 vfs_fish_ops
.ctl
= fish_ctl
;
1345 vfs_register_class (&vfs_fish_ops
);