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
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Library General Public License
16 as published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Library General Public License for more details.
24 You should have received a copy of the GNU Library General Public
25 License along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
30 * \brief Source: Virtual File System: FISH implementation for transfering files over
32 * \author Pavel Machek
36 * Derived from ftpfs.c
37 * Read README.fish for protocol specification.
39 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
40 * where C means you want compressed connection,
41 * and r means you want to use rsh
43 * Namespace: fish_vfs_ops exported.
46 /* Define this if your ssh can take -I option */
53 #include <sys/time.h> /* gettimeofday() */
57 #include "lib/global.h"
59 #include "lib/tty/tty.h" /* enable/disable interrupt key */
60 #include "lib/strescape.h"
61 #include "lib/unixcompat.h"
62 #include "lib/fileloc.h"
64 #include "src/wtools.h" /* message() */
65 #include "src/main.h" /* print_vfs_message */
67 #include "xdirentry.h"
70 #include "gc.h" /* vfs_stamp_create */
75 int fish_directory_timeout
= 900;
77 #define DO_RESOLVE_SYMLINK 1
79 #define DO_FREE_RESOURCE 4
81 #define FISH_FLAG_COMPRESSED 1
82 #define FISH_FLAG_RSH 2
85 #define OPT_IGNORE_ERROR 2
90 #define PRELIM 1 /* positive preliminary */
91 #define COMPLETE 2 /* positive completion */
92 #define CONTINUE 3 /* positive intermediate */
93 #define TRANSIENT 4 /* transient negative completion */
94 #define ERROR 5 /* permanent negative completion */
96 /* command wait_flag: */
98 #define WAIT_REPLY 0x01
99 #define WANT_STRING 0x02
101 /* environment flags */
102 #define FISH_HAVE_HEAD 1
103 #define FISH_HAVE_SED 2
104 #define FISH_HAVE_AWK 4
105 #define FISH_HAVE_PERL 8
106 #define FISH_HAVE_LSQ 16
107 #define FISH_HAVE_DATE_MDYT 32
109 static char reply_str
[80];
111 static struct vfs_class vfs_fish_ops
;
115 fish_load_script_from_file (const char *hostname
, const char *script_name
, const char *def_content
)
117 char *scr_filename
= NULL
;
121 /* 1st: scan user directory */
122 scr_filename
= g_build_path (PATH_SEP_STR
, home_dir
, MC_USERCONF_DIR
, FISH_PREFIX
, hostname
,
123 script_name
, (char *) NULL
);
124 /* silent about user dir */
125 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
126 g_free (scr_filename
);
127 /* 2nd: scan system dir */
128 if (scr_content
== NULL
)
130 g_free (scr_content
);
131 scr_filename
= g_build_path (PATH_SEP_STR
, LIBEXECDIR
, FISH_PREFIX
, script_name
, (char *) NULL
);
132 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
133 g_free (scr_filename
);
136 if (scr_content
!= NULL
)
139 g_free (scr_content
);
140 return g_strdup (def_content
);
144 fish_decode_reply (char *s
, int was_garbage
)
147 if (!sscanf (s
, "%d", &code
))
153 return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
157 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
159 fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
166 if (!vfs_s_get_line (me
, sock
, answer
, sizeof (answer
), '\n'))
173 if (strncmp (answer
, "### ", 4))
177 g_strlcpy (string_buf
, answer
, string_len
);
180 return fish_decode_reply (answer
+ 4, was_garbage
);
184 #define SUP super->u.fish
187 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
, int wait_reply
, const char *fmt
, ...)
192 FILE *logfile
= MEDATA
->logfile
;
196 str
= g_strdup_vprintf (fmt
, ap
);
202 ret
= fwrite (str
, strlen (str
), 1, logfile
);
203 ret
= fflush (logfile
);
206 tty_enable_interrupt_key ();
208 status
= write (SUP
.sockw
, str
, strlen (str
));
211 tty_disable_interrupt_key ();
216 return fish_get_reply (me
, SUP
.sockr
,
217 (wait_reply
& WANT_STRING
) ? reply_str
:
218 NULL
, sizeof (reply_str
) - 1);
223 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
225 if ((SUP
.sockw
!= -1) || (SUP
.sockr
!= -1))
227 print_vfs_message (_("fish: Disconnecting from %s"), super
->name
? super
->name
: "???");
228 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
231 SUP
.sockw
= SUP
.sockr
= -1;
236 g_free (SUP
.password
);
238 g_free (SUP
.scr_exists
);
239 g_free (SUP
.scr_mkdir
);
240 g_free (SUP
.scr_unlink
);
241 g_free (SUP
.scr_chown
);
242 g_free (SUP
.scr_chmod
);
243 g_free (SUP
.scr_rmdir
);
246 g_free (SUP
.scr_hardlink
);
247 g_free (SUP
.scr_get
);
248 g_free (SUP
.scr_send
);
249 g_free (SUP
.scr_append
);
250 g_free (SUP
.scr_info
);
251 g_free (SUP
.scr_env
);
255 fish_pipeopen (struct vfs_s_super
*super
, const char *path
, const char *argv
[])
257 int fileset1
[2], fileset2
[2];
260 if ((pipe (fileset1
) < 0) || (pipe (fileset2
) < 0))
261 vfs_die ("Cannot pipe(): %m.");
268 vfs_die ("Cannot fork(): %m.");
269 /* We are the parent */
271 SUP
.sockw
= fileset1
[1];
273 SUP
.sockr
= fileset2
[0];
277 res
= dup2 (fileset1
[0], 0);
280 res
= dup2 (fileset2
[1], 1);
282 /* stderr to /dev/null */
283 res
= open ("/dev/null", O_WRONLY
);
286 execvp (path
, const_cast (char **, argv
));
292 fish_set_env (int flags
)
296 tmp
= g_string_sized_new (128);
297 g_string_assign (tmp
, "export ");
299 if ((flags
& FISH_HAVE_HEAD
) != 0)
300 g_string_append (tmp
, "FISH_HAVE_HEAD=1 ");
302 if ((flags
& FISH_HAVE_SED
) != 0)
303 g_string_append (tmp
, "FISH_HAVE_SED=1 ");
305 if ((flags
& FISH_HAVE_AWK
) != 0)
306 g_string_append (tmp
, "FISH_HAVE_AWK=1 ");
308 if ((flags
& FISH_HAVE_PERL
) != 0)
309 g_string_append (tmp
, "FISH_HAVE_PERL=1 ");
311 if ((flags
& FISH_HAVE_LSQ
) != 0)
312 g_string_append (tmp
, "FISH_HAVE_LSQ=1 ");
314 if ((flags
& FISH_HAVE_DATE_MDYT
) != 0)
315 g_string_append (tmp
, "FISH_HAVE_DATE_MDYT=1 ");
317 return g_string_free (tmp
, FALSE
);
321 fish_info (struct vfs_class
*me
, struct vfs_s_super
*super
)
324 if (fish_command (me
, super
, NONE
, SUP
.scr_info
) == COMPLETE
)
329 res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
330 if ((!res
) || (res
== EINTR
))
331 ERRNOR (ECONNRESET
, FALSE
);
332 if (!strncmp (buffer
, "### ", 4))
334 SUP
.host_flags
= atol (buffer
);
338 ERRNOR (E_PROTO
, FALSE
);
342 /* The returned directory should always contain a trailing slash */
344 fish_getcwd (struct vfs_class
*me
, struct vfs_s_super
*super
)
346 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
347 return g_strconcat (reply_str
, "/", (char *) NULL
);
352 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
356 const char *argv
[10]; /* All of 10 is used now */
357 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
361 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
364 if (SUP
.flags
> FISH_FLAG_RSH
)
367 g_snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
372 * Add the user name to the ssh command line only if it was explicitly
373 * set in vfs URL. rsh/ssh will get current user by default
374 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
375 * option breaks it for some)
381 argv
[i
++] = SUP
.user
;
385 /* The rest of the code assumes it to be a valid username */
386 SUP
.user
= vfs_get_local_username ();
389 argv
[i
++] = SUP
.host
;
390 argv
[i
++] = "echo FISH:; /bin/sh";
393 fish_pipeopen (super
, xsh
, argv
);
397 print_vfs_message (_("fish: Waiting for initial line..."));
398 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
399 ERRNOR (E_PROTO
, -1);
400 print_vfs_message ("%s", answer
);
401 if (strstr (answer
, "assword"))
403 /* Currently, this does not work. ssh reads passwords from
404 /dev/tty, not from stdin :-(. */
406 message (D_ERROR
, MSG_ERROR
,
407 _("Sorry, we cannot do password authenticated connections for now."));
412 p
= g_strdup_printf (_("fish: Password is required for %s"), SUP
.user
);
413 op
= vfs_get_password (p
);
420 print_vfs_message (_("fish: Sending password..."));
424 str_len
= strlen (SUP
.password
);
425 if ((write (SUP
.sockw
, SUP
.password
, str_len
) != (ssize_t
) str_len
)
426 || (write (SUP
.sockw
, "\n", 1) != 1))
434 print_vfs_message (_("fish: Sending initial line..."));
436 * Run `start_fish_server'. If it doesn't exist - no problem,
437 * we'll talk directly to the shell.
440 (me
, super
, WAIT_REPLY
,
441 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE
)
442 ERRNOR (E_PROTO
, -1);
444 print_vfs_message (_("fish: Handshaking version..."));
445 if (fish_command (me
, super
, WAIT_REPLY
, "#VER 0.0.3\necho '### 000'\n") != COMPLETE
)
446 ERRNOR (E_PROTO
, -1);
448 /* Set up remote locale to C, otherwise dates cannot be recognized */
450 (me
, super
, WAIT_REPLY
,
451 "export LANG=C LC_ALL=C LC_TIME=C\n"
452 "echo '### 200'\n") != COMPLETE
)
453 ERRNOR (E_PROTO
, -1);
455 print_vfs_message (_("fish: Getting host info..."));
456 if (fish_info (me
, super
))
457 SUP
.scr_env
= fish_set_env (SUP
.host_flags
);
459 print_vfs_message (_("fish: Setting up current directory..."));
460 SUP
.cwdir
= fish_getcwd (me
, super
);
461 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
463 super
->name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
465 super
->name
= g_strdup (PATH_SEP_STR
);
467 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
472 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
473 const char *archive_name
, char *op
)
475 char *host
, *user
, *password
, *p
;
480 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
481 &password
, 0, URL_NOSLASH
| URL_USE_ANONYMOUS
);
488 if (!strncmp (op
, "rsh:", 4))
489 SUP
.flags
= FISH_FLAG_RSH
;
492 SUP
.password
= password
;
493 SUP
.scr_ls
= fish_load_script_from_file (host
, FISH_LS_FILE
, FISH_LS_DEF_CONTENT
);
494 SUP
.scr_exists
= fish_load_script_from_file (host
, FISH_EXISTS_FILE
, FISH_EXISTS_FILE
);
495 SUP
.scr_mkdir
= fish_load_script_from_file (host
, FISH_MKDIR_FILE
, FISH_MKDIR_FILE
);
496 SUP
.scr_unlink
= fish_load_script_from_file (host
, FISH_UNLINK_FILE
, FISH_UNLINK_FILE
);
497 SUP
.scr_chown
= fish_load_script_from_file (host
, FISH_CHOWN_FILE
, FISH_CHOWN_FILE
);
498 SUP
.scr_chmod
= fish_load_script_from_file (host
, FISH_CHMOD_FILE
, FISH_CHMOD_FILE
);
499 SUP
.scr_rmdir
= fish_load_script_from_file (host
, FISH_RMDIR_FILE
, FISH_RMDIR_FILE
);
500 SUP
.scr_ln
= fish_load_script_from_file (host
, FISH_LN_FILE
, FISH_LN_FILE
);
501 SUP
.scr_mv
= fish_load_script_from_file (host
, FISH_MV_FILE
, FISH_MV_FILE
);
502 SUP
.scr_hardlink
= fish_load_script_from_file (host
, FISH_HARDLINK_FILE
, FISH_HARDLINK_FILE
);
503 SUP
.scr_get
= fish_load_script_from_file (host
, FISH_GET_FILE
, FISH_GET_FILE
);
504 SUP
.scr_send
= fish_load_script_from_file (host
, FISH_SEND_FILE
,FISH_SEND_FILE
);
505 SUP
.scr_append
= fish_load_script_from_file (host
, FISH_APPEND_FILE
, FISH_APPEND_FILE
);
506 SUP
.scr_info
= fish_load_script_from_file (host
, FISH_INFO_FILE
, FISH_INFO_FILE
);
507 return fish_open_archive_int (me
, super
);
511 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
512 const char *archive_name
, char *op
, void *cookie
)
522 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
523 URL_NOSLASH
| URL_USE_ANONYMOUS
);
528 user
= vfs_get_local_username ();
530 result
= ((strcmp (host
, SUP
.host
) == 0)
531 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
540 fish_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
542 struct vfs_s_super
*super
= dir
->super
;
544 struct vfs_s_entry
*ent
= NULL
;
548 gchar
*shell_commands
;
551 * Simple FISH debug interface :]
554 if (!(MEDATA
->logfile
))
556 MEDATA
->logfile
= fopen ("/tmp/mc-FISH.sh", "w");
559 logfile
= MEDATA
->logfile
;
561 print_vfs_message (_("fish: Reading directory %s..."), remote_path
);
563 gettimeofday (&dir
->timestamp
, NULL
);
564 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
565 quoted_path
= strutils_shell_escape (remote_path
);
566 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_ls
, (char *) NULL
);
567 fish_command (me
, super
, NONE
, shell_commands
, quoted_path
);
568 g_free (shell_commands
);
569 g_free (quoted_path
);
570 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
573 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
574 if ((!res
) || (res
== EINTR
))
576 vfs_s_free_entry (me
, ent
);
577 me
->verrno
= ECONNRESET
;
582 fputs (buffer
, logfile
);
583 fputs ("\n", logfile
);
586 if (!strncmp (buffer
, "### ", 4))
592 vfs_s_insert_entry (me
, dir
, ent
);
593 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
598 #define ST ent->ino->st
605 char *data_start
= buffer
+ 1;
606 char *filename
= data_start
;
607 char *linkname
= data_start
;
608 char *filename_bound
= filename
+ strlen (filename
);
609 char *linkname_bound
= filename_bound
;
610 if (!strcmp (data_start
, "\".\"") || !strcmp (data_start
, "\"..\""))
611 break; /* We'll do "." and ".." ourselves */
613 if (S_ISLNK (ST
.st_mode
))
615 /* we expect: "escaped-name" -> "escaped-name"
616 // -> cannot occur in filenames,
617 // because it will be escaped to -\> */
619 if (*filename
== '"')
622 linkname
= strstr (filename
, "\" -> \"");
625 /* broken client, or smth goes wrong */
626 linkname
= filename_bound
;
627 if (filename_bound
> filename
&& *(filename_bound
- 1) == '"')
628 --filename_bound
; /* skip trailing " */
632 filename_bound
= linkname
;
633 linkname
+= 6; /* strlen ("\" -> \"") */
634 if (*(linkname_bound
- 1) == '"')
635 --linkname_bound
; /* skip trailing " */
638 ent
->name
= str_dup_range (filename
, filename_bound
);
640 ent
->name
= strutils_shell_unescape (ent
->name
);
643 ent
->ino
->linkname
= str_dup_range (linkname
, linkname_bound
);
644 temp
= ent
->ino
->linkname
;
645 ent
->ino
->linkname
= strutils_shell_unescape (ent
->ino
->linkname
);
650 /* we expect: "escaped-name" */
651 if (filename_bound
- filename
> 2)
654 there is at least 2 "
657 if (*filename
== '"')
659 if (*(filename_bound
- 1) == '"')
662 ent
->name
= str_dup_range (filename
, filename_bound
);
664 ent
->name
= strutils_shell_unescape (ent
->name
);
671 ST
.st_size
= (off_t
) atoll (buffer
+ 1);
673 ST
.st_size
= (off_t
) atof (buffer
+ 1);
679 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
686 we expect: Roctal-filemode octal-filetype uid.gid
689 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
694 vfs_split_text (buffer
+ 1);
695 if (!vfs_parse_filedate (0, &ST
.st_ctime
))
697 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
703 if (sscanf (buffer
+ 1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
704 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
706 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime (&tim
);
712 if (sscanf (buffer
+ 1, "%d,%d", &maj
, &min
) != 2)
714 #ifdef HAVE_STRUCT_STAT_ST_RDEV
715 ST
.st_rdev
= makedev (maj
, min
);
721 vfs_s_free_entry (me
, ent
);
722 reply_code
= fish_decode_reply (buffer
+ 4, 0);
723 if (reply_code
== COMPLETE
)
726 SUP
.cwdir
= g_strdup (remote_path
);
727 print_vfs_message (_("%s: done."), me
->name
);
730 else if (reply_code
== ERROR
)
736 me
->verrno
= E_REMOTE
;
740 print_vfs_message (_("%s: failure"), me
->name
);
745 fish_file_store (struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
747 gchar
*shell_commands
= NULL
;
748 struct vfs_s_super
*super
= FH_SUPER
;
756 h
= open (localname
, O_RDONLY
);
760 if (fstat (h
, &s
) < 0)
766 /* First, try this as stor:
768 * ( head -c number ) | ( cat > file; cat >/dev/null )
770 * If `head' is not present on the remote system, `dd' will be used.
771 * Unfortunately, we cannot trust most non-GNU `head' implementations
772 * even if `-c' options is supported. Therefore, we separate GNU head
773 * (and other modern heads?) using `-q' and `-' . This causes another
774 * implementations to fail (because of "incorrect options").
779 * while [ $rest -gt 0 ]
781 * cnt=`expr \( $rest + 255 \) / 256`
782 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
783 * rest=`expr $rest - $n`
786 * `dd' was not designed for full filling of input buffers,
787 * and does not report exact number of bytes (not blocks).
788 * Therefore a more complex shell script is needed.
790 * On some systems non-GNU head writes "Usage:" error report to stdout
791 * instead of stderr. It makes impossible the use of "head || dd"
792 * algorithm for file appending case, therefore just "dd" is used for it.
795 quoted_name
= strutils_shell_escape (name
);
796 print_vfs_message (_("fish: store %s: sending command..."), quoted_name
);
798 /* FIXME: File size is limited to ULONG_MAX */
799 if (!fh
->u
.fish
.append
)
801 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%lu;\n",
802 SUP
.scr_append
, (char *) NULL
);
803 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
, (unsigned long) s
.st_size
);
804 g_free (shell_commands
);
808 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%lu;\n",
809 SUP
.scr_send
, (char *) NULL
);
810 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
, (unsigned long) s
.st_size
);
811 g_free (shell_commands
);
816 ERRNOR (E_REMOTE
, -1);
824 while ((n
= read (h
, buffer
, sizeof (buffer
))) < 0)
826 if ((errno
== EINTR
) && tty_got_interrupt ())
828 print_vfs_message (_("fish: Local read failed, sending zeros"));
830 h
= open ("/dev/zero", O_RDONLY
);
836 t
= write (SUP
.sockw
, buffer
, 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
)
865 gchar
*shell_commands
= NULL
;
866 struct vfs_s_super
*super
= FH_SUPER
;
870 ERRNOR (E_NOTSUPP
, 0);
871 name
= vfs_s_fullpath (me
, fh
->ino
);
874 quoted_name
= strutils_shell_escape (name
);
876 fh
->u
.fish
.append
= 0;
879 * Check whether the remote file is readable by using `dd' to copy
880 * a single byte from the remote file to /dev/null. If `dd' completes
881 * with exit status of 0 use `cat' to send the file contents to the
882 * standard output (i.e. over the network).
885 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_get
, (char *) NULL
);
886 offset
= fish_command (me
, super
, WANT_STRING
, shell_commands
, quoted_name
);
887 g_free (shell_commands
);
888 g_free (quoted_name
);
889 if (offset
!= PRELIM
)
890 ERRNOR (E_REMOTE
, 0);
891 fh
->linear
= LS_LINEAR_OPEN
;
894 #if SIZEOF_OFF_T == SIZEOF_LONG
895 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
897 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
900 ERRNOR (E_REMOTE
, 0);
905 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
907 struct vfs_s_super
*super
= FH_SUPER
;
911 print_vfs_message (_("Aborting transfer..."));
914 n
= MIN (8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
917 n
= read (SUP
.sockr
, buffer
, n
);
925 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
926 print_vfs_message (_("Error reported after abort."));
928 print_vfs_message (_("Aborted transfer would be successful."));
932 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
934 struct vfs_s_super
*super
= FH_SUPER
;
936 len
= MIN (fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
937 tty_disable_interrupt_key ();
938 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
)) < 0))
940 if ((errno
== EINTR
) && !tty_got_interrupt ())
944 tty_enable_interrupt_key ();
949 fish_linear_abort (me
, fh
);
950 else if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
951 ERRNOR (E_REMOTE
, -1);
956 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
958 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
959 fish_linear_abort (me
, fh
);
963 fish_ctl (void *fh
, int ctlop
, void *arg
)
972 case VFS_CTL_IS_NOTREADY
:
977 vfs_die ("You may not do this");
978 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
981 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
982 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
993 fish_send_command (struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
997 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
998 vfs_stamp_create (&vfs_fish_ops
, super
);
1000 ERRNOR (E_REMOTE
, -1);
1001 if (flags
& OPT_FLUSH
)
1002 vfs_s_invalidate (me
, super
);
1007 char buf[BUF_LARGE]; \
1008 const char *crpath; \
1009 char *rpath, *mpath = g_strdup (path); \
1010 struct vfs_s_super *super; \
1011 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
1012 if (crpath == NULL) \
1017 rpath = strutils_shell_escape (crpath); \
1021 fish_rename (struct vfs_class
*me
, const char *path1
, const char *path2
)
1023 gchar
*shell_commands
= NULL
;
1024 char buf
[BUF_LARGE
];
1025 const char *crpath1
, *crpath2
;
1026 char *rpath1
, *rpath2
, *mpath1
, *mpath2
;
1027 struct vfs_s_super
*super
, *super2
;
1028 crpath1
= vfs_s_get_path_mangle (me
, mpath1
= g_strdup(path1
), &super
, 0);
1029 if (crpath1
== NULL
)
1034 crpath2
= vfs_s_get_path_mangle (me
, mpath2
= g_strdup(path2
), &super2
, 0);
1035 if (crpath2
== NULL
)
1041 rpath1
= strutils_shell_escape (crpath1
);
1043 rpath2
= strutils_shell_escape (crpath2
);
1045 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1046 SUP
.scr_mv
, (char *) NULL
);
1047 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1048 g_free (shell_commands
);
1051 return fish_send_command(me
, super2
, buf
, OPT_FLUSH
);
1055 fish_link (struct vfs_class
*me
, const char *path1
, const char *path2
)
1057 gchar
*shell_commands
= NULL
;
1058 char buf
[BUF_LARGE
];
1059 const char *crpath1
, *crpath2
;
1060 char *rpath1
, *rpath2
, *mpath1
, *mpath2
;
1061 struct vfs_s_super
*super
, *super2
;
1062 crpath1
= vfs_s_get_path_mangle (me
, mpath1
= g_strdup(path1
), &super
, 0);
1063 if (crpath1
== NULL
)
1068 crpath2
= vfs_s_get_path_mangle (me
, mpath2
= g_strdup(path2
), &super2
, 0);
1069 if (crpath2
== NULL
)
1075 rpath1
= strutils_shell_escape (crpath1
);
1077 rpath2
= strutils_shell_escape (crpath2
);
1079 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1080 SUP
.scr_hardlink
, (char *) NULL
);
1081 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1082 g_free (shell_commands
);
1085 return fish_send_command(me
, super2
, buf
, OPT_FLUSH
);
1089 fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
1092 gchar
*shell_commands
= NULL
;
1093 char buf
[BUF_LARGE
];
1095 char *rpath
, *mpath
= g_strdup (path
);
1096 struct vfs_s_super
*super
;
1097 crpath
= vfs_s_get_path_mangle (me
, mpath
, &super
, 0);
1103 rpath
= strutils_shell_escape (crpath
);
1106 qsetto
= strutils_shell_escape (setto
);
1107 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1108 SUP
.scr_ln
, (char *) NULL
);
1109 g_snprintf (buf
, sizeof (buf
), shell_commands
, qsetto
, rpath
);
1110 g_free (shell_commands
);
1113 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1117 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1119 gchar
*shell_commands
= NULL
;
1122 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1123 SUP
.scr_chmod
, (char *) NULL
);
1124 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, mode
& 07777);
1125 g_free (shell_commands
);
1127 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1131 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
1133 char *sowner
, *sgroup
;
1137 pw
= getpwuid (owner
);
1141 gr
= getgrgid (group
);
1145 sowner
= pw
->pw_name
;
1146 sgroup
= gr
->gr_name
;
1148 gchar
*shell_commands
= NULL
;
1152 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1153 SUP
.scr_chown
, (char *) NULL
);
1154 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, sowner
, sgroup
);
1155 g_free (shell_commands
);
1156 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1157 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1158 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1160 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1165 fish_unlink (struct vfs_class
*me
, const char *path
)
1167 gchar
*shell_commands
= NULL
;
1170 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_unlink
, (char *) NULL
);
1171 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1172 g_free (shell_commands
);
1174 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1178 fish_exists (struct vfs_class
*me
, const char *path
)
1180 gchar
*shell_commands
= NULL
;
1183 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_exists
, (char *) NULL
);
1184 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1185 g_free (shell_commands
);
1188 return (fish_send_command (me
, super
, buf
, OPT_FLUSH
) == 0) ? 1 : 0;
1193 fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1195 gchar
*shell_commands
= NULL
;
1200 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_mkdir
, (char *) NULL
);
1201 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1202 g_free (shell_commands
);
1205 ret_code
= fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1210 if (!fish_exists (me
, path
))
1212 ERRNOR (EACCES
, -1);
1218 fish_rmdir (struct vfs_class
*me
, const char *path
)
1220 gchar
*shell_commands
= NULL
;
1223 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_rmdir
, (char *) NULL
);
1224 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1225 g_free (shell_commands
);
1227 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1231 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
, int mode
)
1235 fh
->u
.fish
.append
= 0;
1236 /* File will be written only, so no need to retrieve it */
1237 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
)))
1239 fh
->u
.fish
.append
= flags
& O_APPEND
;
1240 if (!fh
->ino
->localname
)
1242 int tmp_handle
= vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1243 fh
->ino
->ent
->name
);
1244 if (tmp_handle
== -1)
1250 if (!fh
->ino
->localname
)
1251 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1253 if (!fh
->ino
->localname
)
1254 vfs_die ("retrieve_file failed to fill in localname");
1259 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1261 struct vfs_s_super
*super
= MEDATA
->supers
;
1268 const char *flags
= "";
1274 case FISH_FLAG_COMPRESSED
:
1278 if (SUP
.flags
> FISH_FLAG_RSH
)
1281 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1287 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
, "/", SUP
.cwdir
, (char *) NULL
);
1290 super
= super
->next
;
1295 fish_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1298 sorry, i've places hack here
1299 cause fish don't able to open files with O_EXCL flag
1302 return vfs_s_open (me
, file
, flags
, mode
);
1309 static struct vfs_s_subclass fish_subclass
;
1313 fish_subclass
.flags
= VFS_S_REMOTE
;
1314 fish_subclass
.archive_same
= fish_archive_same
;
1315 fish_subclass
.open_archive
= fish_open_archive
;
1316 fish_subclass
.free_archive
= fish_free_archive
;
1317 fish_subclass
.fh_open
= fish_fh_open
;
1318 fish_subclass
.dir_load
= fish_dir_load
;
1319 fish_subclass
.file_store
= fish_file_store
;
1320 fish_subclass
.linear_start
= fish_linear_start
;
1321 fish_subclass
.linear_read
= fish_linear_read
;
1322 fish_subclass
.linear_close
= fish_linear_close
;
1324 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1325 vfs_fish_ops
.name
= "fish";
1326 vfs_fish_ops
.prefix
= "sh:";
1327 vfs_fish_ops
.fill_names
= fish_fill_names
;
1328 vfs_fish_ops
.chmod
= fish_chmod
;
1329 vfs_fish_ops
.chown
= fish_chown
;
1330 vfs_fish_ops
.open
= fish_open
;
1331 vfs_fish_ops
.symlink
= fish_symlink
;
1332 vfs_fish_ops
.link
= fish_link
;
1333 vfs_fish_ops
.unlink
= fish_unlink
;
1334 vfs_fish_ops
.rename
= fish_rename
;
1335 vfs_fish_ops
.mkdir
= fish_mkdir
;
1336 vfs_fish_ops
.rmdir
= fish_rmdir
;
1337 vfs_fish_ops
.ctl
= fish_ctl
;
1338 vfs_register_class (&vfs_fish_ops
);