1 /* Virtual File System: FISH implementation for transfering files over
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7 Written by: 1998 Pavel Machek
8 Spaces fix: 2000 Michal Svec
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Library General Public License
17 as published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
31 * \brief Source: Virtual File System: FISH implementation for transfering files over
33 * \author Pavel Machek
37 * Derived from ftpfs.c
38 * Read README.fish for protocol specification.
40 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
41 * where C means you want compressed connection,
42 * and r means you want to use rsh
44 * Namespace: fish_vfs_ops exported.
47 /* Define this if your ssh can take -I option */
54 #include <sys/time.h> /* gettimeofday() */
57 #include <inttypes.h> /* uintmax_t */
59 #include "lib/global.h"
60 #include "lib/tty/tty.h" /* enable/disable interrupt key */
61 #include "lib/strescape.h"
62 #include "lib/unixcompat.h"
63 #include "lib/fileloc.h"
64 #include "lib/mcconfig.h"
66 #include "src/execute.h" /* pre_exec, post_exec */
68 #include "lib/vfs/vfs.h"
69 #include "lib/vfs/utilvfs.h"
70 #include "lib/vfs/netutil.h"
71 #include "lib/vfs/xdirentry.h"
72 #include "lib/vfs/gc.h" /* vfs_stamp_create */
77 /*** global variables ****************************************************************************/
79 int fish_directory_timeout
= 900;
81 /*** file scope macro definitions ****************************************************************/
83 #define DO_RESOLVE_SYMLINK 1
85 #define DO_FREE_RESOURCE 4
87 #define FISH_FLAG_COMPRESSED 1
88 #define FISH_FLAG_RSH 2
91 #define OPT_IGNORE_ERROR 2
96 #define PRELIM 1 /* positive preliminary */
97 #define COMPLETE 2 /* positive completion */
98 #define CONTINUE 3 /* positive intermediate */
99 #define TRANSIENT 4 /* transient negative completion */
100 #define ERROR 5 /* permanent negative completion */
102 /* command wait_flag: */
104 #define WAIT_REPLY 0x01
105 #define WANT_STRING 0x02
107 /* environment flags */
108 #define FISH_HAVE_HEAD 1
109 #define FISH_HAVE_SED 2
110 #define FISH_HAVE_AWK 4
111 #define FISH_HAVE_PERL 8
112 #define FISH_HAVE_LSQ 16
113 #define FISH_HAVE_DATE_MDYT 32
114 #define FISH_HAVE_TAIL 64
116 #define SUP ((fish_super_data_t *) super->data)
118 /*** file scope type declarations ****************************************************************/
149 /*** file scope variables ************************************************************************/
151 static char reply_str
[80];
153 static struct vfs_class vfs_fish_ops
;
155 /*** file scope functions ************************************************************************/
156 /* --------------------------------------------------------------------------------------------- */
159 fish_load_script_from_file (const char *hostname
, const char *script_name
, const char *def_content
)
161 char *scr_filename
= NULL
;
165 /* 1st: scan user directory */
166 scr_filename
= g_build_path (PATH_SEP_STR
, mc_config_get_data_path (), FISH_PREFIX
, hostname
,
167 script_name
, (char *) NULL
);
168 /* silent about user dir */
169 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
170 g_free (scr_filename
);
171 /* 2nd: scan system dir */
172 if (scr_content
== NULL
)
175 g_build_path (PATH_SEP_STR
, LIBEXECDIR
, FISH_PREFIX
, script_name
, (char *) NULL
);
176 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
177 g_free (scr_filename
);
180 if (scr_content
!= NULL
)
183 return g_strdup (def_content
);
186 /* --------------------------------------------------------------------------------------------- */
189 fish_decode_reply (char *s
, int was_garbage
)
192 if (!sscanf (s
, "%d", &code
))
198 return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
202 /* --------------------------------------------------------------------------------------------- */
203 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
206 fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
213 if (!vfs_s_get_line (me
, sock
, answer
, sizeof (answer
), '\n'))
220 if (strncmp (answer
, "### ", 4))
224 g_strlcpy (string_buf
, answer
, string_len
);
227 return fish_decode_reply (answer
+ 4, was_garbage
);
231 /* --------------------------------------------------------------------------------------------- */
234 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
, int wait_reply
, const char *fmt
, ...)
239 FILE *logfile
= MEDATA
->logfile
;
243 str
= g_strdup_vprintf (fmt
, ap
);
249 ret
= fwrite (str
, strlen (str
), 1, logfile
);
250 ret
= fflush (logfile
);
253 tty_enable_interrupt_key ();
255 status
= write (SUP
->sockw
, str
, strlen (str
));
258 tty_disable_interrupt_key ();
263 return fish_get_reply (me
, SUP
->sockr
,
264 (wait_reply
& WANT_STRING
) ? reply_str
:
265 NULL
, sizeof (reply_str
) - 1);
269 /* --------------------------------------------------------------------------------------------- */
272 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
274 if ((SUP
->sockw
!= -1) || (SUP
->sockr
!= -1))
276 vfs_print_message (_("fish: Disconnecting from %s"), super
->name
? super
->name
: "???");
277 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
280 SUP
->sockw
= SUP
->sockr
= -1;
282 g_free (SUP
->scr_ls
);
283 g_free (SUP
->scr_exists
);
284 g_free (SUP
->scr_mkdir
);
285 g_free (SUP
->scr_unlink
);
286 g_free (SUP
->scr_chown
);
287 g_free (SUP
->scr_chmod
);
288 g_free (SUP
->scr_rmdir
);
289 g_free (SUP
->scr_ln
);
290 g_free (SUP
->scr_mv
);
291 g_free (SUP
->scr_hardlink
);
292 g_free (SUP
->scr_get
);
293 g_free (SUP
->scr_send
);
294 g_free (SUP
->scr_append
);
295 g_free (SUP
->scr_info
);
296 g_free (SUP
->scr_env
);
301 /* --------------------------------------------------------------------------------------------- */
304 fish_pipeopen (struct vfs_s_super
*super
, const char *path
, const char *argv
[])
306 int fileset1
[2], fileset2
[2];
309 if ((pipe (fileset1
) < 0) || (pipe (fileset2
) < 0))
310 vfs_die ("Cannot pipe(): %m.");
317 vfs_die ("Cannot fork(): %m.");
318 /* We are the parent */
320 SUP
->sockw
= fileset1
[1];
322 SUP
->sockr
= fileset2
[0];
326 res
= dup2 (fileset1
[0], 0);
329 res
= dup2 (fileset2
[1], 1);
331 /* stderr to /dev/null */
332 res
= open ("/dev/null", O_WRONLY
);
335 execvp (path
, const_cast (char **, argv
));
340 /* --------------------------------------------------------------------------------------------- */
343 fish_set_env (int flags
)
347 tmp
= g_string_sized_new (250);
348 g_string_assign (tmp
, "");
350 if ((flags
& FISH_HAVE_HEAD
) != 0)
351 g_string_append (tmp
, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
353 if ((flags
& FISH_HAVE_SED
) != 0)
354 g_string_append (tmp
, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
356 if ((flags
& FISH_HAVE_AWK
) != 0)
357 g_string_append (tmp
, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
359 if ((flags
& FISH_HAVE_PERL
) != 0)
360 g_string_append (tmp
, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
362 if ((flags
& FISH_HAVE_LSQ
) != 0)
363 g_string_append (tmp
, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
365 if ((flags
& FISH_HAVE_DATE_MDYT
) != 0)
366 g_string_append (tmp
, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
368 if ((flags
& FISH_HAVE_TAIL
) != 0)
369 g_string_append (tmp
, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
371 return g_string_free (tmp
, FALSE
);
374 /* --------------------------------------------------------------------------------------------- */
377 fish_info (struct vfs_class
*me
, struct vfs_s_super
*super
)
380 if (fish_command (me
, super
, NONE
, SUP
->scr_info
) == COMPLETE
)
386 res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
->sockr
);
387 if ((res
== 0) || (res
== EINTR
))
388 ERRNOR (ECONNRESET
, FALSE
);
389 if (strncmp (buffer
, "### ", 4) == 0)
391 SUP
->host_flags
= atol (buffer
);
395 ERRNOR (E_PROTO
, FALSE
);
399 /* --------------------------------------------------------------------------------------------- */
400 /* The returned directory should always contain a trailing slash */
403 fish_getcwd (struct vfs_class
*me
, struct vfs_s_super
*super
)
405 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
406 return g_strconcat (reply_str
, "/", (char *) NULL
);
411 /* --------------------------------------------------------------------------------------------- */
414 fish_open_archive_pipeopen (struct vfs_s_super
*super
)
417 const char *argv
[10]; /* All of 10 is used now */
418 const char *xsh
= (super
->path_element
->port
== FISH_FLAG_RSH
? "rsh" : "ssh");
422 if (super
->path_element
->port
== FISH_FLAG_COMPRESSED
)
425 if (super
->path_element
->port
> FISH_FLAG_RSH
)
428 g_snprintf (gbuf
, sizeof (gbuf
), "%d", super
->path_element
->port
);
433 * Add the user name to the ssh command line only if it was explicitly
434 * set in vfs URL. rsh/ssh will get current user by default
435 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
436 * option breaks it for some)
439 if (super
->path_element
->user
!= NULL
)
442 argv
[i
++] = super
->path_element
->user
;
446 /* The rest of the code assumes it to be a valid username */
447 super
->path_element
->user
= vfs_get_local_username ();
450 argv
[i
++] = super
->path_element
->host
;
451 argv
[i
++] = "echo FISH:; /bin/sh";
454 fish_pipeopen (super
, xsh
, argv
);
457 /* --------------------------------------------------------------------------------------------- */
460 fish_open_archive_talk (struct vfs_class
*me
, struct vfs_s_super
*super
)
464 printf ("\n%s\n", _("fish: Waiting for initial line..."));
466 if (!vfs_s_get_line (me
, SUP
->sockr
, answer
, sizeof (answer
), ':'))
469 if (strstr (answer
, "assword") != NULL
)
471 /* Currently, this does not work. ssh reads passwords from
472 /dev/tty, not from stdin :-(. */
474 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
478 if (super
->path_element
->password
== NULL
)
481 p
= g_strdup_printf (_("fish: Password is required for %s"), super
->path_element
->user
);
482 op
= vfs_get_password (p
);
486 super
->path_element
->password
= op
;
490 printf ("\n%s\n", _("fish: Sending password..."));
495 str_len
= strlen (super
->path_element
->password
);
496 if ((write (SUP
.sockw
, super
->path_element
->password
, str_len
) != (ssize_t
) str_len
)
497 || (write (SUP
->sockw
, "\n", 1) != 1))
505 /* --------------------------------------------------------------------------------------------- */
508 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
515 fish_open_archive_pipeopen (super
);
517 /* Start talk with ssh-server (password prompt, etc ) */
518 ftalk
= fish_open_archive_talk (me
, super
);
524 ERRNOR (E_PROTO
, -1);
526 vfs_print_message (_("fish: Sending initial line..."));
528 * Run `start_fish_server'. If it doesn't exist - no problem,
529 * we'll talk directly to the shell.
533 (me
, super
, WAIT_REPLY
,
534 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE
)
535 ERRNOR (E_PROTO
, -1);
537 vfs_print_message (_("fish: Handshaking version..."));
538 if (fish_command (me
, super
, WAIT_REPLY
, "#VER 0.0.3\necho '### 000'\n") != COMPLETE
)
539 ERRNOR (E_PROTO
, -1);
541 /* Set up remote locale to C, otherwise dates cannot be recognized */
543 (me
, super
, WAIT_REPLY
,
544 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE
)
545 ERRNOR (E_PROTO
, -1);
547 vfs_print_message (_("fish: Getting host info..."));
548 if (fish_info (me
, super
))
549 SUP
->scr_env
= fish_set_env (SUP
->host_flags
);
551 vfs_print_message (_("fish: Setting up current directory..."));
552 super
->path_element
->path
= fish_getcwd (me
, super
);
553 vfs_print_message (_("fish: Connected, home %s."), super
->path_element
->path
);
556 g_strconcat ("/#sh:", super
->path_element
->user
, "@", super
->path_element
->host
, "/",
559 super
->name
= g_strdup (PATH_SEP_STR
);
562 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
566 /* --------------------------------------------------------------------------------------------- */
569 fish_open_archive (struct vfs_s_super
*super
,
570 const vfs_path_t
* vpath
, const vfs_path_element_t
* vpath_element
)
574 super
->data
= g_new0 (fish_super_data_t
, 1);
575 super
->path_element
= vfs_path_element_clone (vpath_element
);
577 if (strncmp (vpath_element
->vfs_prefix
, "rsh", 3) == 0)
578 super
->path_element
->port
= FISH_FLAG_RSH
;
581 fish_load_script_from_file (super
->path_element
->host
, FISH_LS_FILE
, FISH_LS_DEF_CONTENT
);
583 fish_load_script_from_file (super
->path_element
->host
, FISH_EXISTS_FILE
,
584 FISH_EXISTS_DEF_CONTENT
);
586 fish_load_script_from_file (super
->path_element
->host
, FISH_MKDIR_FILE
,
587 FISH_MKDIR_DEF_CONTENT
);
589 fish_load_script_from_file (super
->path_element
->host
, FISH_UNLINK_FILE
,
590 FISH_UNLINK_DEF_CONTENT
);
592 fish_load_script_from_file (super
->path_element
->host
, FISH_CHOWN_FILE
,
593 FISH_CHOWN_DEF_CONTENT
);
595 fish_load_script_from_file (super
->path_element
->host
, FISH_CHMOD_FILE
,
596 FISH_CHMOD_DEF_CONTENT
);
598 fish_load_script_from_file (super
->path_element
->host
, FISH_RMDIR_FILE
,
599 FISH_RMDIR_DEF_CONTENT
);
601 fish_load_script_from_file (super
->path_element
->host
, FISH_LN_FILE
, FISH_LN_DEF_CONTENT
);
603 fish_load_script_from_file (super
->path_element
->host
, FISH_MV_FILE
, FISH_MV_DEF_CONTENT
);
605 fish_load_script_from_file (super
->path_element
->host
, FISH_HARDLINK_FILE
,
606 FISH_HARDLINK_DEF_CONTENT
);
608 fish_load_script_from_file (super
->path_element
->host
, FISH_GET_FILE
, FISH_GET_DEF_CONTENT
);
610 fish_load_script_from_file (super
->path_element
->host
, FISH_SEND_FILE
,
611 FISH_SEND_DEF_CONTENT
);
613 fish_load_script_from_file (super
->path_element
->host
, FISH_APPEND_FILE
,
614 FISH_APPEND_DEF_CONTENT
);
616 fish_load_script_from_file (super
->path_element
->host
, FISH_INFO_FILE
,
617 FISH_INFO_DEF_CONTENT
);
619 return fish_open_archive_int (vpath_element
->class, super
);
622 /* --------------------------------------------------------------------------------------------- */
625 fish_archive_same (const vfs_path_element_t
* vpath_element
, struct vfs_s_super
*super
,
626 const vfs_path_t
* vpath
, void *cookie
)
628 vfs_path_element_t
*path_element
;
634 path_element
= vfs_path_element_clone (vpath_element
);
636 if (path_element
->user
== NULL
)
637 path_element
->user
= vfs_get_local_username ();
639 result
= ((strcmp (path_element
->host
, super
->path_element
->host
) == 0)
640 && (strcmp (path_element
->user
, super
->path_element
->user
) == 0)
641 && (path_element
->port
== super
->path_element
->port
)) ? 1 : 0;
643 vfs_path_element_free (path_element
);
648 /* --------------------------------------------------------------------------------------------- */
651 fish_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
653 struct vfs_s_super
*super
= dir
->super
;
655 struct vfs_s_entry
*ent
= NULL
;
659 gchar
*shell_commands
;
662 * Simple FISH debug interface :]
665 if (!(MEDATA
->logfile
))
667 MEDATA
->logfile
= fopen ("/tmp/mc-FISH.sh", "w");
670 logfile
= MEDATA
->logfile
;
672 vfs_print_message (_("fish: Reading directory %s..."), remote_path
);
674 gettimeofday (&dir
->timestamp
, NULL
);
675 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
676 quoted_path
= strutils_shell_escape (remote_path
);
677 shell_commands
= g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s;\n", SUP
->scr_ls
, (char *) NULL
);
678 fish_command (me
, super
, NONE
, shell_commands
, quoted_path
);
679 g_free (shell_commands
);
680 g_free (quoted_path
);
681 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
684 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
->sockr
);
685 if ((!res
) || (res
== EINTR
))
687 vfs_s_free_entry (me
, ent
);
688 me
->verrno
= ECONNRESET
;
693 fputs (buffer
, logfile
);
694 fputs ("\n", logfile
);
697 if (!strncmp (buffer
, "### ", 4))
703 vfs_s_insert_entry (me
, dir
, ent
);
704 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
709 #define ST ent->ino->st
716 char *data_start
= buffer
+ 1;
717 char *filename
= data_start
;
718 char *linkname
= data_start
;
719 char *filename_bound
= filename
+ strlen (filename
);
720 char *linkname_bound
= filename_bound
;
721 if (!strcmp (data_start
, "\".\"") || !strcmp (data_start
, "\"..\""))
722 break; /* We'll do "." and ".." ourselves */
724 if (S_ISLNK (ST
.st_mode
))
726 /* we expect: "escaped-name" -> "escaped-name"
727 // -> cannot occur in filenames,
728 // because it will be escaped to -\> */
730 if (*filename
== '"')
733 linkname
= strstr (filename
, "\" -> \"");
736 /* broken client, or smth goes wrong */
737 linkname
= filename_bound
;
738 if (filename_bound
> filename
&& *(filename_bound
- 1) == '"')
739 --filename_bound
; /* skip trailing " */
743 filename_bound
= linkname
;
744 linkname
+= 6; /* strlen ("\" -> \"") */
745 if (*(linkname_bound
- 1) == '"')
746 --linkname_bound
; /* skip trailing " */
749 ent
->name
= g_strndup (filename
, filename_bound
- filename
);
751 ent
->name
= strutils_shell_unescape (ent
->name
);
754 ent
->ino
->linkname
= g_strndup (linkname
, linkname_bound
- linkname
);
755 temp
= ent
->ino
->linkname
;
756 ent
->ino
->linkname
= strutils_shell_unescape (ent
->ino
->linkname
);
761 /* we expect: "escaped-name" */
762 if (filename_bound
- filename
> 2)
765 there is at least 2 "
768 if (*filename
== '"')
770 if (*(filename_bound
- 1) == '"')
773 ent
->name
= g_strndup (filename
, filename_bound
- filename
);
775 ent
->name
= strutils_shell_unescape (ent
->name
);
782 ST
.st_size
= (off_t
) atoll (buffer
+ 1);
784 ST
.st_size
= (off_t
) atof (buffer
+ 1);
790 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
797 we expect: Roctal-filemode octal-filetype uid.gid
800 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
805 vfs_split_text (buffer
+ 1);
806 if (!vfs_parse_filedate (0, &ST
.st_ctime
))
808 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
814 if (sscanf (buffer
+ 1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
815 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
817 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime (&tim
);
823 if (sscanf (buffer
+ 1, "%d,%d", &maj
, &min
) != 2)
825 #ifdef HAVE_STRUCT_STAT_ST_RDEV
826 ST
.st_rdev
= makedev (maj
, min
);
832 vfs_s_free_entry (me
, ent
);
833 reply_code
= fish_decode_reply (buffer
+ 4, 0);
834 if (reply_code
== COMPLETE
)
836 g_free (super
->path_element
->path
);
837 super
->path_element
->path
= g_strdup (remote_path
);
838 vfs_print_message (_("%s: done."), me
->name
);
842 me
->verrno
= reply_code
== ERROR
? EACCES
: E_REMOTE
;
845 vfs_print_message (_("%s: failure"), me
->name
);
849 /* --------------------------------------------------------------------------------------------- */
852 fish_file_store (struct vfs_class
*me
, vfs_file_handler_t
* fh
, char *name
, char *localname
)
854 fish_fh_data_t
*fish
= (fish_fh_data_t
*) fh
->data
;
855 gchar
*shell_commands
= NULL
;
856 struct vfs_s_super
*super
= FH_SUPER
;
864 h
= open (localname
, O_RDONLY
);
867 if (fstat (h
, &s
) < 0)
873 /* First, try this as stor:
875 * ( head -c number ) | ( cat > file; cat >/dev/null )
877 * If `head' is not present on the remote system, `dd' will be used.
878 * Unfortunately, we cannot trust most non-GNU `head' implementations
879 * even if `-c' options is supported. Therefore, we separate GNU head
880 * (and other modern heads?) using `-q' and `-' . This causes another
881 * implementations to fail (because of "incorrect options").
886 * while [ $rest -gt 0 ]
888 * cnt=`expr \( $rest + 255 \) / 256`
889 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
890 * rest=`expr $rest - $n`
893 * `dd' was not designed for full filling of input buffers,
894 * and does not report exact number of bytes (not blocks).
895 * Therefore a more complex shell script is needed.
897 * On some systems non-GNU head writes "Usage:" error report to stdout
898 * instead of stderr. It makes impossible the use of "head || dd"
899 * algorithm for file appending case, therefore just "dd" is used for it.
902 quoted_name
= strutils_shell_escape (name
);
903 vfs_print_message (_("fish: store %s: sending command..."), quoted_name
);
905 /* FIXME: File size is limited to ULONG_MAX */
909 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX
";\n",
910 SUP
->scr_append
, (char *) NULL
);
912 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
,
913 (uintmax_t) s
.st_size
);
914 g_free (shell_commands
);
919 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX
";\n",
920 SUP
->scr_send
, (char *) NULL
);
921 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
,
922 (uintmax_t) s
.st_size
);
923 g_free (shell_commands
);
928 ERRNOR (E_REMOTE
, -1);
936 while ((n
= read (h
, buffer
, sizeof (buffer
))) < 0)
938 if ((errno
== EINTR
) && tty_got_interrupt ())
940 vfs_print_message (_("fish: Local read failed, sending zeros"));
942 h
= open ("/dev/zero", O_RDONLY
);
948 t
= write (SUP
->sockw
, buffer
, n
);
957 tty_disable_interrupt_key ();
959 vfs_print_message ("%s: %d/%" PRIuMAX
,
960 was_error
? _("fish: storing zeros") : _("fish: storing file"),
961 total
, (uintmax_t) s
.st_size
);
964 g_free (quoted_name
);
966 if ((fish_get_reply (me
, SUP
->sockr
, NULL
, 0) != COMPLETE
) || was_error
)
967 ERRNOR (E_REMOTE
, -1);
972 fish_get_reply (me
, SUP
->sockr
, NULL
, 0);
973 g_free (quoted_name
);
977 /* --------------------------------------------------------------------------------------------- */
980 fish_linear_start (struct vfs_class
*me
, vfs_file_handler_t
* fh
, off_t offset
)
982 fish_fh_data_t
*fish
;
983 gchar
*shell_commands
= NULL
;
984 struct vfs_s_super
*super
= FH_SUPER
;
988 if (fh
->data
== NULL
)
989 fh
->data
= g_new0 (fish_fh_data_t
, 1);
991 fish
= (fish_fh_data_t
*) fh
->data
;
993 name
= vfs_s_fullpath (me
, fh
->ino
);
996 quoted_name
= strutils_shell_escape (name
);
1001 * Check whether the remote file is readable by using `dd' to copy
1002 * a single byte from the remote file to /dev/null. If `dd' completes
1003 * with exit status of 0 use `cat' to send the file contents to the
1004 * standard output (i.e. over the network).
1008 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX
";\n",
1009 SUP
->scr_get
, (char *) NULL
);
1010 offset
= fish_command (me
, super
, WANT_STRING
, shell_commands
, quoted_name
, (uintmax_t) offset
);
1011 g_free (shell_commands
);
1012 g_free (quoted_name
);
1013 if (offset
!= PRELIM
)
1014 ERRNOR (E_REMOTE
, 0);
1015 fh
->linear
= LS_LINEAR_OPEN
;
1018 #if SIZEOF_OFF_T == SIZEOF_LONG
1019 fish
->total
= (off_t
) strtol (reply_str
, NULL
, 10);
1021 fish
->total
= (off_t
) strtoll (reply_str
, NULL
, 10);
1024 ERRNOR (E_REMOTE
, 0);
1028 /* --------------------------------------------------------------------------------------------- */
1031 fish_linear_abort (struct vfs_class
*me
, vfs_file_handler_t
* fh
)
1033 fish_fh_data_t
*fish
= (fish_fh_data_t
*) fh
->data
;
1034 struct vfs_s_super
*super
= FH_SUPER
;
1035 char buffer
[BUF_8K
];
1038 vfs_print_message (_("Aborting transfer..."));
1042 n
= MIN (sizeof (buffer
), (size_t) (fish
->total
- fish
->got
));
1045 n
= read (SUP
->sockr
, buffer
, n
);
1053 if (fish_get_reply (me
, SUP
->sockr
, NULL
, 0) != COMPLETE
)
1054 vfs_print_message (_("Error reported after abort."));
1056 vfs_print_message (_("Aborted transfer would be successful."));
1059 /* --------------------------------------------------------------------------------------------- */
1062 fish_linear_read (struct vfs_class
*me
, vfs_file_handler_t
* fh
, void *buf
, size_t len
)
1064 fish_fh_data_t
*fish
= (fish_fh_data_t
*) fh
->data
;
1065 struct vfs_s_super
*super
= FH_SUPER
;
1069 len
= MIN ((size_t) (fish
->total
- fish
->got
), len
);
1070 tty_disable_interrupt_key ();
1071 while (len
!= 0 && ((n
= read (SUP
->sockr
, buf
, len
)) < 0))
1073 if ((errno
== EINTR
) && !tty_got_interrupt ())
1077 tty_enable_interrupt_key ();
1082 fish_linear_abort (me
, fh
);
1083 else if (fish_get_reply (me
, SUP
->sockr
, NULL
, 0) != COMPLETE
)
1084 ERRNOR (E_REMOTE
, -1);
1088 /* --------------------------------------------------------------------------------------------- */
1091 fish_linear_close (struct vfs_class
*me
, vfs_file_handler_t
* fh
)
1093 fish_fh_data_t
*fish
= (fish_fh_data_t
*) fh
->data
;
1095 if (fish
->total
!= fish
->got
)
1096 fish_linear_abort (me
, fh
);
1099 /* --------------------------------------------------------------------------------------------- */
1102 fish_ctl (void *fh
, int ctlop
, void *arg
)
1111 case VFS_CTL_IS_NOTREADY
:
1116 vfs_die ("You may not do this");
1117 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
1120 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
1121 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
1131 /* --------------------------------------------------------------------------------------------- */
1134 fish_send_command (struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
1138 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
1139 vfs_stamp_create (&vfs_fish_ops
, super
);
1141 ERRNOR (E_REMOTE
, -1);
1142 if (flags
& OPT_FLUSH
)
1143 vfs_s_invalidate (me
, super
);
1147 /* --------------------------------------------------------------------------------------------- */
1150 fish_rename (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1152 gchar
*shell_commands
= NULL
;
1153 char buf
[BUF_LARGE
];
1154 const char *crpath1
, *crpath2
;
1155 char *rpath1
, *rpath2
;
1156 struct vfs_s_super
*super
, *super2
;
1157 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath1
, -1);
1159 crpath1
= vfs_s_get_path (vpath1
, &super
, 0);
1160 if (crpath1
== NULL
)
1163 crpath2
= vfs_s_get_path (vpath2
, &super2
, 0);
1164 if (crpath2
== NULL
)
1167 rpath1
= strutils_shell_escape (crpath1
);
1168 rpath2
= strutils_shell_escape (crpath2
);
1169 shell_commands
= g_strconcat (SUP
->scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1170 SUP
->scr_mv
, (char *) NULL
);
1171 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1172 g_free (shell_commands
);
1175 return fish_send_command (path_element
->class, super2
, buf
, OPT_FLUSH
);
1178 /* --------------------------------------------------------------------------------------------- */
1181 fish_link (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1183 gchar
*shell_commands
= NULL
;
1184 char buf
[BUF_LARGE
];
1185 const char *crpath1
, *crpath2
;
1186 char *rpath1
, *rpath2
;
1187 struct vfs_s_super
*super
, *super2
;
1188 vfs_path_element_t
*path_element
;
1191 path_element
= vfs_path_get_by_index (vpath1
, -1);
1193 crpath1
= vfs_s_get_path (vpath1
, &super
, 0);
1194 if (crpath1
== NULL
)
1197 crpath2
= vfs_s_get_path (vpath2
, &super2
, 0);
1198 if (crpath2
== NULL
)
1201 rpath1
= strutils_shell_escape (crpath1
);
1202 rpath2
= strutils_shell_escape (crpath2
);
1203 shell_commands
= g_strconcat (SUP
->scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1204 SUP
->scr_hardlink
, (char *) NULL
);
1205 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1206 g_free (shell_commands
);
1209 return fish_send_command (path_element
->class, super2
, buf
, OPT_FLUSH
);
1213 /* --------------------------------------------------------------------------------------------- */
1216 fish_symlink (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1219 gchar
*shell_commands
= NULL
;
1220 char buf
[BUF_LARGE
];
1222 char *rpath
, *str_path
;
1223 struct vfs_s_super
*super
;
1224 vfs_path_element_t
*path_element
= vfs_path_get_by_index (vpath1
, -1);
1226 crpath
= vfs_s_get_path (vpath2
, &super
, 0);
1229 rpath
= strutils_shell_escape (crpath
);
1231 str_path
= vfs_path_to_str (vpath1
);
1232 qsetto
= strutils_shell_escape (str_path
);
1235 shell_commands
= g_strconcat (SUP
->scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1236 SUP
->scr_ln
, (char *) NULL
);
1237 g_snprintf (buf
, sizeof (buf
), shell_commands
, qsetto
, rpath
);
1238 g_free (shell_commands
);
1241 return fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1244 /* --------------------------------------------------------------------------------------------- */
1247 fish_chmod (const vfs_path_t
* vpath
, mode_t mode
)
1249 gchar
*shell_commands
= NULL
;
1250 char buf
[BUF_LARGE
];
1253 struct vfs_s_super
*super
;
1254 vfs_path_element_t
*path_element
;
1256 path_element
= vfs_path_get_by_index (vpath
, -1);
1258 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1261 rpath
= strutils_shell_escape (crpath
);
1263 shell_commands
= g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1264 SUP
->scr_chmod
, (char *) NULL
);
1265 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, (int) (mode
& 07777));
1266 g_free (shell_commands
);
1268 return fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1271 /* --------------------------------------------------------------------------------------------- */
1274 fish_chown (const vfs_path_t
* vpath
, uid_t owner
, gid_t group
)
1276 char *sowner
, *sgroup
;
1280 pw
= getpwuid (owner
);
1284 gr
= getgrgid (group
);
1288 sowner
= pw
->pw_name
;
1289 sgroup
= gr
->gr_name
;
1292 gchar
*shell_commands
= NULL
;
1293 char buf
[BUF_LARGE
];
1296 struct vfs_s_super
*super
;
1297 vfs_path_element_t
*path_element
;
1299 path_element
= vfs_path_get_by_index (vpath
, -1);
1302 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1305 rpath
= strutils_shell_escape (crpath
);
1307 shell_commands
= g_strconcat (SUP
->scr_env
,
1308 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1309 SUP
->scr_chown
, (char *) NULL
);
1310 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, sowner
, sgroup
);
1311 g_free (shell_commands
);
1312 fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1313 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1314 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1316 return fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1320 /* --------------------------------------------------------------------------------------------- */
1323 fish_unlink (const vfs_path_t
* vpath
)
1325 gchar
*shell_commands
= NULL
;
1327 char buf
[BUF_LARGE
];
1330 struct vfs_s_super
*super
;
1331 vfs_path_element_t
*path_element
;
1333 path_element
= vfs_path_get_by_index (vpath
, -1);
1334 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1337 rpath
= strutils_shell_escape (crpath
);
1340 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s;\n", SUP
->scr_unlink
, (char *) NULL
);
1341 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1342 g_free (shell_commands
);
1344 return fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1347 /* --------------------------------------------------------------------------------------------- */
1350 fish_exists (const vfs_path_t
* vpath
)
1352 gchar
*shell_commands
= NULL
;
1354 char buf
[BUF_LARGE
];
1357 struct vfs_s_super
*super
;
1358 vfs_path_element_t
*path_element
;
1360 path_element
= vfs_path_get_by_index (vpath
, -1);
1361 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1364 rpath
= strutils_shell_escape (crpath
);
1367 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s;\n", SUP
->scr_exists
, (char *) NULL
);
1368 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1369 g_free (shell_commands
);
1372 return (fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
) == 0) ? 1 : 0;
1375 /* --------------------------------------------------------------------------------------------- */
1378 fish_mkdir (const vfs_path_t
* vpath
, mode_t mode
)
1380 gchar
*shell_commands
= NULL
;
1382 char buf
[BUF_LARGE
];
1385 struct vfs_s_super
*super
;
1386 vfs_path_element_t
*path_element
;
1390 path_element
= vfs_path_get_by_index (vpath
, -1);
1392 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1395 rpath
= strutils_shell_escape (crpath
);
1398 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s;\n", SUP
->scr_mkdir
, (char *) NULL
);
1399 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1400 g_free (shell_commands
);
1403 ret_code
= fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1408 if (!fish_exists (vpath
))
1410 path_element
->class->verrno
= EACCES
;
1416 /* --------------------------------------------------------------------------------------------- */
1419 fish_rmdir (const vfs_path_t
* vpath
)
1421 gchar
*shell_commands
= NULL
;
1422 char buf
[BUF_LARGE
];
1425 struct vfs_s_super
*super
;
1426 vfs_path_element_t
*path_element
;
1428 path_element
= vfs_path_get_by_index (vpath
, -1);
1430 crpath
= vfs_s_get_path (vpath
, &super
, 0);
1433 rpath
= strutils_shell_escape (crpath
);
1436 g_strconcat (SUP
->scr_env
, "FISH_FILENAME=%s;\n", SUP
->scr_rmdir
, (char *) NULL
);
1437 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1438 g_free (shell_commands
);
1440 return fish_send_command (path_element
->class, super
, buf
, OPT_FLUSH
);
1443 /* --------------------------------------------------------------------------------------------- */
1446 fish_fh_open (struct vfs_class
*me
, vfs_file_handler_t
* fh
, int flags
, mode_t mode
)
1448 fish_fh_data_t
*fish
;
1452 fh
->data
= g_new0 (fish_fh_data_t
, 1);
1453 fish
= (fish_fh_data_t
*) fh
->data
;
1455 /* File will be written only, so no need to retrieve it */
1456 if (((flags
& O_WRONLY
) == O_WRONLY
) && ((flags
& (O_RDONLY
| O_RDWR
)) == 0))
1458 fish
->append
= flags
& O_APPEND
;
1459 if (!fh
->ino
->localname
)
1461 int tmp_handle
= vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1462 fh
->ino
->ent
->name
);
1463 if (tmp_handle
== -1)
1469 if (!fh
->ino
->localname
&& vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1471 if (!fh
->ino
->localname
)
1472 vfs_die ("retrieve_file failed to fill in localname");
1480 /* --------------------------------------------------------------------------------------------- */
1483 fish_fh_close (struct vfs_class
*me
, vfs_file_handler_t
* fh
)
1491 /* --------------------------------------------------------------------------------------------- */
1494 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1498 for (iter
= MEDATA
->supers
; iter
!= NULL
; iter
= g_list_next (iter
))
1500 const struct vfs_s_super
*super
= (const struct vfs_s_super
*) iter
->data
;
1504 const char *flags
= "";
1506 switch (super
->path_element
->port
)
1511 case FISH_FLAG_COMPRESSED
:
1515 if (super
->path_element
->port
> FISH_FLAG_RSH
)
1517 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", super
->path_element
->port
);
1524 g_strconcat (vfs_fish_ops
.prefix
, VFS_PATH_URL_DELIMITER
,
1525 super
->path_element
->user
, "@", super
->path_element
->host
, flags
, "/",
1526 super
->path_element
->path
, (char *) NULL
);
1532 /* --------------------------------------------------------------------------------------------- */
1535 fish_open (const vfs_path_t
* vpath
, int flags
, mode_t mode
)
1538 sorry, i've places hack here
1539 cause fish don't able to open files with O_EXCL flag
1542 return vfs_s_open (vpath
, flags
, mode
);
1545 /* --------------------------------------------------------------------------------------------- */
1546 /*** public functions ****************************************************************************/
1547 /* --------------------------------------------------------------------------------------------- */
1552 static struct vfs_s_subclass fish_subclass
;
1556 fish_subclass
.flags
= VFS_S_REMOTE
;
1557 fish_subclass
.archive_same
= fish_archive_same
;
1558 fish_subclass
.open_archive
= fish_open_archive
;
1559 fish_subclass
.free_archive
= fish_free_archive
;
1560 fish_subclass
.fh_open
= fish_fh_open
;
1561 fish_subclass
.fh_close
= fish_fh_close
;
1562 fish_subclass
.dir_load
= fish_dir_load
;
1563 fish_subclass
.file_store
= fish_file_store
;
1564 fish_subclass
.linear_start
= fish_linear_start
;
1565 fish_subclass
.linear_read
= fish_linear_read
;
1566 fish_subclass
.linear_close
= fish_linear_close
;
1568 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1569 vfs_fish_ops
.name
= "fish";
1570 vfs_fish_ops
.prefix
= "sh";
1571 vfs_fish_ops
.fill_names
= fish_fill_names
;
1572 vfs_fish_ops
.chmod
= fish_chmod
;
1573 vfs_fish_ops
.chown
= fish_chown
;
1574 vfs_fish_ops
.open
= fish_open
;
1575 vfs_fish_ops
.symlink
= fish_symlink
;
1576 vfs_fish_ops
.link
= fish_link
;
1577 vfs_fish_ops
.unlink
= fish_unlink
;
1578 vfs_fish_ops
.rename
= fish_rename
;
1579 vfs_fish_ops
.mkdir
= fish_mkdir
;
1580 vfs_fish_ops
.rmdir
= fish_rmdir
;
1581 vfs_fish_ops
.ctl
= fish_ctl
;
1582 vfs_register_class (&vfs_fish_ops
);
1585 /* --------------------------------------------------------------------------------------------- */