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.");
214 vfs_die ("Cannot fork(): %m.");
215 /* We are the parent */
217 SUP
.sockw
= fileset1
[1];
219 SUP
.sockr
= fileset2
[0];
223 res
= dup2 (fileset1
[0], 0);
226 res
= dup2 (fileset2
[1], 1);
228 /* stderr to /dev/null */
229 res
= open ("/dev/null", O_WRONLY
);
232 execvp (path
, const_cast (char **, argv
));
237 /* The returned directory should always contain a trailing slash */
239 fish_getcwd (struct vfs_class
*me
, struct vfs_s_super
*super
)
241 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
242 return g_strconcat (reply_str
, "/", (char *) NULL
);
247 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
251 const char *argv
[10]; /* All of 10 is used now */
252 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
256 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
259 if (SUP
.flags
> FISH_FLAG_RSH
)
262 g_snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
267 * Add the user name to the ssh command line only if it was explicitly
268 * set in vfs URL. rsh/ssh will get current user by default
269 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
270 * option breaks it for some)
276 argv
[i
++] = SUP
.user
;
280 /* The rest of the code assumes it to be a valid username */
281 SUP
.user
= vfs_get_local_username ();
284 argv
[i
++] = SUP
.host
;
285 argv
[i
++] = "echo FISH:; /bin/sh";
288 fish_pipeopen (super
, xsh
, argv
);
292 print_vfs_message (_("fish: Waiting for initial line..."));
293 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
294 ERRNOR (E_PROTO
, -1);
295 print_vfs_message ("%s", answer
);
296 if (strstr (answer
, "assword"))
298 /* Currently, this does not work. ssh reads passwords from
299 /dev/tty, not from stdin :-(. */
301 message (D_ERROR
, MSG_ERROR
,
302 _("Sorry, we cannot do password authenticated connections for now."));
307 p
= g_strconcat (_(" fish: Password required for "), SUP
.user
, " ", (char *) NULL
);
308 op
= vfs_get_password (p
);
314 print_vfs_message (_("fish: Sending password..."));
318 str_len
= strlen (SUP
.password
);
319 if ((write (SUP
.sockw
, SUP
.password
, str_len
) != (ssize_t
) str_len
)
320 || (write (SUP
.sockw
, "\n", 1) != 1))
328 print_vfs_message (_("fish: Sending initial line..."));
330 * Run `start_fish_server'. If it doesn't exist - no problem,
331 * we'll talk directly to the shell.
334 (me
, super
, WAIT_REPLY
,
335 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE
)
336 ERRNOR (E_PROTO
, -1);
338 print_vfs_message (_("fish: Handshaking version..."));
339 if (fish_command (me
, super
, WAIT_REPLY
, "#VER 0.0.0\necho '### 000'\n") != COMPLETE
)
340 ERRNOR (E_PROTO
, -1);
342 /* Set up remote locale to C, otherwise dates cannot be recognized */
344 (me
, super
, WAIT_REPLY
,
345 "LANG=C; LC_ALL=C; LC_TIME=C\n"
346 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE
)
347 ERRNOR (E_PROTO
, -1);
349 print_vfs_message (_("fish: Setting up current directory..."));
350 SUP
.cwdir
= fish_getcwd (me
, super
);
351 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
353 super
->name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
355 super
->name
= g_strdup (PATH_SEP_STR
);
357 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
362 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
363 const char *archive_name
, char *op
)
365 char *host
, *user
, *password
, *p
;
370 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
371 &password
, 0, URL_NOSLASH
| URL_USE_ANONYMOUS
);
378 if (!strncmp (op
, "rsh:", 4))
379 SUP
.flags
= FISH_FLAG_RSH
;
382 SUP
.password
= password
;
383 return fish_open_archive_int (me
, super
);
387 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
388 const char *archive_name
, char *op
, void *cookie
)
398 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
399 URL_NOSLASH
| URL_USE_ANONYMOUS
);
404 user
= vfs_get_local_username ();
406 result
= ((strcmp (host
, SUP
.host
) == 0)
407 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
416 fish_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
418 struct vfs_s_super
*super
= dir
->super
;
420 struct vfs_s_entry
*ent
= NULL
;
424 gchar
*shell_commands
;
428 * Simple FISH debug interface :]
430 if (!(MEDATA
->logfile
))
432 MEDATA
->logfile
= fopen ("/tmp/mc-FISH.sh", "w");
436 logfile
= MEDATA
->logfile
;
438 print_vfs_message (_("fish: Reading directory %s..."), remote_path
);
440 gettimeofday (&dir
->timestamp
, NULL
);
441 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
442 quoted_path
= strutils_shell_escape (remote_path
);
444 shell_commands
= g_strconcat (
446 "if `perl -v > /dev/null 2>&1` ; then\n"
451 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
452 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
453 "my $dirname = $ARGV[0];\n"
454 "if (opendir ( DIR, $dirname )) {\n"
455 "while( (my $filename = readdir(DIR))){\n"
456 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
457 "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
459 "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
460 "my $e_filename = $filename;\n"
461 "$e_filename =~ $strutils_shell_escape_regex;\n"
462 "if (S_ISLNK($mode) ) {\n"
463 "my $linkname = readlink (\"$dirname/$filename\");\n"
464 "$linkname =~ $strutils_shell_escape_regex;\n"
466 "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
468 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
469 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
471 "printf(\"R%%o %%o $uid.$gid\\n"
474 ":\\\"$e_filename\\\"\\n"
475 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
478 "printf(\"### 200\\n\");\n"
481 "printf(\"### 500\\n\");\n"
484 "' /%s ||\n" /* ARGV[0] - path to browse */
485 " echo '### 500'\n" /* do not hang if perl is to eval it */
486 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
487 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
494 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
495 "while read p l u g s m d y n n2 n3; do\n"
496 "if test \"$m\" = \"0\" ; then \n"
497 "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
499 "n=$n\" \"$n2\" \"$n3\n"
501 "if [ $ADD = 0 ]; then\n"
502 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
503 "elif `sed --version >/dev/null 2>&1` ; then\n"
504 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
505 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
507 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
510 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
511 "while read p l u g a i m d y n n2 n3; do\n"
512 "if test \"$a\" = \"0\" ; then \n"
513 "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
515 "n=$n\" \"$n2\" \"$n3\n"
517 "if [ $ADD = 0 ]; then\n"
518 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
519 "elif `sed --version >/dev/null 2>&1` ; then\n"
520 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
521 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
523 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
534 fish_command (me
, super
, NONE
, shell_commands
,
535 quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
);
537 g_free (shell_commands
);
538 g_free (quoted_path
);
539 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
542 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
543 if ((!res
) || (res
== EINTR
))
545 vfs_s_free_entry (me
, ent
);
546 me
->verrno
= ECONNRESET
;
551 fputs (buffer
, logfile
);
552 fputs ("\n", logfile
);
555 if (!strncmp (buffer
, "### ", 4))
561 vfs_s_insert_entry (me
, dir
, ent
);
562 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
567 #define ST ent->ino->st
574 char *data_start
= buffer
+ 1;
575 char *filename
= data_start
;
576 char *linkname
= data_start
;
577 char *filename_bound
= filename
+ strlen (filename
);
578 char *linkname_bound
= filename_bound
;
579 if (!strcmp (data_start
, "\".\"") || !strcmp (data_start
, "\"..\""))
580 break; /* We'll do "." and ".." ourselves */
582 if (S_ISLNK (ST
.st_mode
))
584 /* we expect: "escaped-name" -> "escaped-name"
585 // -> cannot occur in filenames,
586 // because it will be escaped to -\> */
588 if (*filename
== '"')
591 linkname
= strstr (filename
, "\" -> \"");
594 /* broken client, or smth goes wrong */
595 linkname
= filename_bound
;
596 if (filename_bound
> filename
&& *(filename_bound
- 1) == '"')
597 --filename_bound
; /* skip trailing " */
601 filename_bound
= linkname
;
602 linkname
+= 6; /* strlen ("\" -> \"") */
603 if (*(linkname_bound
- 1) == '"')
604 --linkname_bound
; /* skip trailing " */
607 ent
->name
= str_dup_range (filename
, filename_bound
);
609 ent
->name
= strutils_shell_unescape (ent
->name
);
612 ent
->ino
->linkname
= str_dup_range (linkname
, linkname_bound
);
613 temp
= ent
->ino
->linkname
;
614 ent
->ino
->linkname
= strutils_shell_unescape (ent
->ino
->linkname
);
619 /* we expect: "escaped-name" */
620 if (filename_bound
- filename
> 2)
623 there is at least 2 "
626 if (*filename
== '"')
628 if (*(filename_bound
- 1) == '"')
631 ent
->name
= str_dup_range (filename
, filename_bound
);
633 ent
->name
= strutils_shell_unescape (ent
->name
);
640 ST
.st_size
= (off_t
) atoll (buffer
+ 1);
642 ST
.st_size
= (off_t
) atof (buffer
+ 1);
648 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
655 we expect: Roctal-filemode octal-filetype uid.gid
658 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
663 vfs_split_text (buffer
+ 1);
664 if (!vfs_parse_filedate (0, &ST
.st_ctime
))
666 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
672 if (sscanf (buffer
+ 1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
673 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
675 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime (&tim
);
681 if (sscanf (buffer
+ 1, "%d,%d", &maj
, &min
) != 2)
683 #ifdef HAVE_STRUCT_STAT_ST_RDEV
684 ST
.st_rdev
= makedev (maj
, min
);
690 vfs_s_free_entry (me
, ent
);
691 reply_code
= fish_decode_reply (buffer
+ 4, 0);
692 if (reply_code
== COMPLETE
)
695 SUP
.cwdir
= g_strdup (remote_path
);
696 print_vfs_message (_("%s: done."), me
->name
);
699 else if (reply_code
== ERROR
)
705 me
->verrno
= E_REMOTE
;
709 print_vfs_message (_("%s: failure"), me
->name
);
714 fish_file_store (struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
716 struct vfs_s_super
*super
= FH_SUPER
;
724 h
= open (localname
, O_RDONLY
);
728 if (fstat (h
, &s
) < 0)
734 /* First, try this as stor:
736 * ( head -c number ) | ( cat > file; cat >/dev/null )
738 * If `head' is not present on the remote system, `dd' will be used.
739 * Unfortunately, we cannot trust most non-GNU `head' implementations
740 * even if `-c' options is supported. Therefore, we separate GNU head
741 * (and other modern heads?) using `-q' and `-' . This causes another
742 * implementations to fail (because of "incorrect options").
747 * while [ $rest -gt 0 ]
749 * cnt=`expr \( $rest + 255 \) / 256`
750 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
751 * rest=`expr $rest - $n`
754 * `dd' was not designed for full filling of input buffers,
755 * and does not report exact number of bytes (not blocks).
756 * Therefore a more complex shell script is needed.
758 * On some systems non-GNU head writes "Usage:" error report to stdout
759 * instead of stderr. It makes impossible the use of "head || dd"
760 * algorithm for file appending case, therefore just "dd" is used for it.
763 quoted_name
= strutils_shell_escape (name
);
764 print_vfs_message (_("fish: store %s: sending command..."), quoted_name
);
766 /* FIXME: File size is limited to ULONG_MAX */
767 if (!fh
->u
.fish
.append
)
770 n
= fish_command (me
, super
, WAIT_REPLY
,
776 "head -c %lu -q - || echo DD >&3\n"
777 ") 2>/dev/null | (\n"
780 ")`; [ \"$res\" = DD ] && {\n"
783 "while [ $rest -gt 0 ]\n"
785 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
786 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
787 " rest=`expr $rest - $n`\n"
789 "}; echo '### 200'\n",
790 (unsigned long) s
.st_size
, quoted_name
,
791 quoted_name
, (unsigned long) s
.st_size
, (unsigned long) s
.st_size
);
797 n
= fish_command (me
, super
, WAIT_REPLY
,
803 "while [ $rest -gt 0 ]\n"
805 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
806 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
807 " rest=`expr $rest - $n`\n"
809 "}; echo '### 200'\n",
810 (unsigned long) s
.st_size
, quoted_name
,
811 quoted_name
, (unsigned long) s
.st_size
);
817 ERRNOR (E_REMOTE
, -1);
825 while ((n
= read (h
, buffer
, sizeof (buffer
))) < 0)
827 if ((errno
== EINTR
) && tty_got_interrupt ())
829 print_vfs_message (_("fish: Local read failed, sending zeros"));
831 h
= open ("/dev/zero", O_RDONLY
);
837 t
= write (SUP
.sockw
, buffer
, n
);
846 tty_disable_interrupt_key ();
848 print_vfs_message (_("fish: storing %s %d (%lu)"),
849 was_error
? _("zeros") : _("file"), total
, (unsigned long) s
.st_size
);
852 g_free (quoted_name
);
853 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
854 ERRNOR (E_REMOTE
, -1);
858 fish_get_reply (me
, SUP
.sockr
, NULL
, 0);
859 g_free (quoted_name
);
864 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
869 ERRNOR (E_NOTSUPP
, 0);
870 name
= vfs_s_fullpath (me
, fh
->ino
);
873 quoted_name
= strutils_shell_escape (name
);
875 fh
->u
.fish
.append
= 0;
878 * Check whether the remote file is readable by using `dd' to copy
879 * a single byte from the remote file to /dev/null. If `dd' completes
880 * with exit status of 0 use `cat' to send the file contents to the
881 * standard output (i.e. over the network).
884 offset
= fish_command (me
, FH_SUPER
, WANT_STRING
,
886 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
888 "ls -ln /%s 2>/dev/null | (\n"
898 quoted_name
, quoted_name
, quoted_name
, quoted_name
);
900 g_free (quoted_name
);
901 if (offset
!= PRELIM
)
902 ERRNOR (E_REMOTE
, 0);
903 fh
->linear
= LS_LINEAR_OPEN
;
906 #if SIZEOF_OFF_T == SIZEOF_LONG
907 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
909 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
912 ERRNOR (E_REMOTE
, 0);
917 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
919 struct vfs_s_super
*super
= FH_SUPER
;
923 print_vfs_message (_("Aborting transfer..."));
926 n
= MIN (8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
929 n
= read (SUP
.sockr
, buffer
, n
);
937 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
938 print_vfs_message (_("Error reported after abort."));
940 print_vfs_message (_("Aborted transfer would be successful."));
944 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
946 struct vfs_s_super
*super
= FH_SUPER
;
948 len
= MIN (fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
949 tty_disable_interrupt_key ();
950 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
)) < 0))
952 if ((errno
== EINTR
) && !tty_got_interrupt ())
956 tty_enable_interrupt_key ();
961 fish_linear_abort (me
, fh
);
962 else if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
963 ERRNOR (E_REMOTE
, -1);
968 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
970 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
971 fish_linear_abort (me
, fh
);
975 fish_ctl (void *fh
, int ctlop
, void *arg
)
984 case VFS_CTL_IS_NOTREADY
:
989 vfs_die ("You may not do this");
990 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
993 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
994 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
1005 fish_send_command (struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
1009 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
1010 vfs_stamp_create (&vfs_fish_ops
, super
);
1012 ERRNOR (E_REMOTE
, -1);
1013 if (flags
& OPT_FLUSH
)
1014 vfs_s_invalidate (me
, super
);
1019 char buf[BUF_LARGE]; \
1020 const char *crpath; \
1021 char *rpath, *mpath = g_strdup (path); \
1022 struct vfs_s_super *super; \
1023 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
1024 if (crpath == NULL) \
1029 rpath = strutils_shell_escape (crpath); \
1032 #define POSTFIX(flags) \
1034 return fish_send_command (me, super, buf, flags);
1037 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1041 g_snprintf (buf
, sizeof (buf
),
1042 "#CHMOD %4.4o /%s\n"
1043 "if chmod %4.4o /%s 2>/dev/null; then\n"
1048 mode
& 07777, rpath
, mode
& 07777, rpath
);
1049 POSTFIX (OPT_FLUSH
);
1054 #define FISH_OP(name, string) \
1055 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1057 char buf[BUF_LARGE]; \
1058 const char *crpath1, *crpath2; \
1059 char *rpath1, *rpath2, *mpath1, *mpath2; \
1060 struct vfs_s_super *super1, *super2; \
1061 crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0); \
1062 if (crpath1 == NULL) \
1067 crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0); \
1068 if (crpath2 == NULL) \
1074 rpath1 = strutils_shell_escape (crpath1); \
1076 rpath2 = strutils_shell_escape (crpath2); \
1078 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1081 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1086 "if mv /%s /%s 2>/dev/null; then\n"
1094 "if ln /%s /%s 2>/dev/null; then\n"
1102 fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
1105 PREFIX qsetto
= strutils_shell_escape (setto
);
1107 g_snprintf (buf
, sizeof (buf
),
1109 "if ln -s %s /%s 2>/dev/null; then\n"
1114 qsetto
, rpath
, qsetto
, rpath
);
1117 POSTFIX (OPT_FLUSH
);
1121 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
1123 char *sowner
, *sgroup
;
1127 pw
= getpwuid (owner
);
1131 gr
= getgrgid (group
);
1135 sowner
= pw
->pw_name
;
1136 sgroup
= gr
->gr_name
;
1140 g_snprintf (buf
, sizeof (buf
),
1141 "#CHOWN %s:%s /%s\n"
1142 "if chown %s:%s /%s 2>/dev/null; then\n"
1147 sowner
, sgroup
, rpath
, sowner
, sgroup
, rpath
);
1149 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1150 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1151 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1152 POSTFIX (OPT_FLUSH
)}
1156 fish_unlink (struct vfs_class
*me
, const char *path
)
1161 g_snprintf (buf
, sizeof (buf
),
1163 "if rm -f /%s 2>/dev/null; then\n"
1171 POSTFIX (OPT_FLUSH
);
1175 fish_exists (struct vfs_class
*me
, const char *path
)
1180 g_snprintf (buf
, sizeof (buf
),
1182 "ls -l /%s >/dev/null 2>/dev/null\n"
1189 return (fish_send_command (me
, super
, buf
, OPT_FLUSH
) == 0) ? 1 : 0;
1194 fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1201 g_snprintf (buf
, sizeof (buf
),
1203 "if mkdir /%s 2>/dev/null; then\n"
1212 ret_code
= fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1217 if (!fish_exists (me
, path
))
1219 ERRNOR (EACCES
, -1);
1225 fish_rmdir (struct vfs_class
*me
, const char *path
)
1229 g_snprintf (buf
, sizeof (buf
),
1231 "if rmdir /%s 2>/dev/null; then\n"
1238 POSTFIX (OPT_FLUSH
);
1242 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
, int mode
)
1246 fh
->u
.fish
.append
= 0;
1247 /* File will be written only, so no need to retrieve it */
1248 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
)))
1250 fh
->u
.fish
.append
= flags
& O_APPEND
;
1251 if (!fh
->ino
->localname
)
1253 int tmp_handle
= vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1254 fh
->ino
->ent
->name
);
1255 if (tmp_handle
== -1)
1261 if (!fh
->ino
->localname
)
1262 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1264 if (!fh
->ino
->localname
)
1265 vfs_die ("retrieve_file failed to fill in localname");
1270 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1272 struct vfs_s_super
*super
= MEDATA
->supers
;
1279 const char *flags
= "";
1285 case FISH_FLAG_COMPRESSED
:
1289 if (SUP
.flags
> FISH_FLAG_RSH
)
1292 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1298 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
, "/", SUP
.cwdir
, (char *) NULL
);
1301 super
= super
->next
;
1306 fish_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1309 sorry, i've places hack here
1310 cause fish don't able to open files with O_EXCL flag
1313 return vfs_s_open (me
, file
, flags
, mode
);
1320 static struct vfs_s_subclass fish_subclass
;
1324 fish_subclass
.flags
= VFS_S_REMOTE
;
1325 fish_subclass
.archive_same
= fish_archive_same
;
1326 fish_subclass
.open_archive
= fish_open_archive
;
1327 fish_subclass
.free_archive
= fish_free_archive
;
1328 fish_subclass
.fh_open
= fish_fh_open
;
1329 fish_subclass
.dir_load
= fish_dir_load
;
1330 fish_subclass
.file_store
= fish_file_store
;
1331 fish_subclass
.linear_start
= fish_linear_start
;
1332 fish_subclass
.linear_read
= fish_linear_read
;
1333 fish_subclass
.linear_close
= fish_linear_close
;
1335 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1336 vfs_fish_ops
.name
= "fish";
1337 vfs_fish_ops
.prefix
= "sh:";
1338 vfs_fish_ops
.fill_names
= fish_fill_names
;
1339 vfs_fish_ops
.chmod
= fish_chmod
;
1340 vfs_fish_ops
.chown
= fish_chown
;
1341 vfs_fish_ops
.open
= fish_open
;
1342 vfs_fish_ops
.symlink
= fish_symlink
;
1343 vfs_fish_ops
.link
= fish_link
;
1344 vfs_fish_ops
.unlink
= fish_unlink
;
1345 vfs_fish_ops
.rename
= fish_rename
;
1346 vfs_fish_ops
.mkdir
= fish_mkdir
;
1347 vfs_fish_ops
.rmdir
= fish_rmdir
;
1348 vfs_fish_ops
.ctl
= fish_ctl
;
1349 vfs_register_class (&vfs_fish_ops
);