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
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"
65 #include "src/filemanager/layout.h" /* print_vfs_message */
66 #include "src/execute.h" /* pre_exec, post_exec */
71 #include "xdirentry.h"
72 #include "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 super->u.fish
119 char buf[BUF_LARGE]; \
120 const char *crpath; \
121 char *rpath, *mpath; \
122 struct vfs_s_super *super; \
123 mpath = g_strdup (path); \
124 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
125 if (crpath == NULL) \
130 rpath = strutils_shell_escape (crpath); \
133 /*** file scope type declarations ****************************************************************/
135 /*** file scope variables ************************************************************************/
137 static char reply_str
[80];
139 static struct vfs_class vfs_fish_ops
;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
145 fish_load_script_from_file (const char *hostname
, const char *script_name
, const char *def_content
)
147 char *scr_filename
= NULL
;
151 /* 1st: scan user directory */
152 scr_filename
= g_build_path (PATH_SEP_STR
, home_dir
, MC_USERCONF_DIR
, FISH_PREFIX
, hostname
,
153 script_name
, (char *) NULL
);
154 /* silent about user dir */
155 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
156 g_free (scr_filename
);
157 /* 2nd: scan system dir */
158 if (scr_content
== NULL
)
161 g_build_path (PATH_SEP_STR
, LIBEXECDIR
, FISH_PREFIX
, script_name
, (char *) NULL
);
162 g_file_get_contents (scr_filename
, &scr_content
, &scr_len
, NULL
);
163 g_free (scr_filename
);
166 if (scr_content
!= NULL
)
169 return g_strdup (def_content
);
172 /* --------------------------------------------------------------------------------------------- */
175 fish_decode_reply (char *s
, int was_garbage
)
178 if (!sscanf (s
, "%d", &code
))
184 return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
188 /* --------------------------------------------------------------------------------------------- */
189 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
192 fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
199 if (!vfs_s_get_line (me
, sock
, answer
, sizeof (answer
), '\n'))
206 if (strncmp (answer
, "### ", 4))
210 g_strlcpy (string_buf
, answer
, string_len
);
213 return fish_decode_reply (answer
+ 4, was_garbage
);
217 /* --------------------------------------------------------------------------------------------- */
220 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
, int wait_reply
, const char *fmt
, ...)
225 FILE *logfile
= MEDATA
->logfile
;
229 str
= g_strdup_vprintf (fmt
, ap
);
235 ret
= fwrite (str
, strlen (str
), 1, logfile
);
236 ret
= fflush (logfile
);
239 tty_enable_interrupt_key ();
241 status
= write (SUP
.sockw
, str
, strlen (str
));
244 tty_disable_interrupt_key ();
249 return fish_get_reply (me
, SUP
.sockr
,
250 (wait_reply
& WANT_STRING
) ? reply_str
:
251 NULL
, sizeof (reply_str
) - 1);
255 /* --------------------------------------------------------------------------------------------- */
258 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
260 if ((SUP
.sockw
!= -1) || (SUP
.sockr
!= -1))
262 print_vfs_message (_("fish: Disconnecting from %s"), super
->name
? super
->name
: "???");
263 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
266 SUP
.sockw
= SUP
.sockr
= -1;
271 g_free (SUP
.password
);
273 g_free (SUP
.scr_exists
);
274 g_free (SUP
.scr_mkdir
);
275 g_free (SUP
.scr_unlink
);
276 g_free (SUP
.scr_chown
);
277 g_free (SUP
.scr_chmod
);
278 g_free (SUP
.scr_rmdir
);
281 g_free (SUP
.scr_hardlink
);
282 g_free (SUP
.scr_get
);
283 g_free (SUP
.scr_send
);
284 g_free (SUP
.scr_append
);
285 g_free (SUP
.scr_info
);
286 g_free (SUP
.scr_env
);
289 /* --------------------------------------------------------------------------------------------- */
292 fish_pipeopen (struct vfs_s_super
*super
, const char *path
, const char *argv
[])
294 int fileset1
[2], fileset2
[2];
297 if ((pipe (fileset1
) < 0) || (pipe (fileset2
) < 0))
298 vfs_die ("Cannot pipe(): %m.");
305 vfs_die ("Cannot fork(): %m.");
306 /* We are the parent */
308 SUP
.sockw
= fileset1
[1];
310 SUP
.sockr
= fileset2
[0];
314 res
= dup2 (fileset1
[0], 0);
317 res
= dup2 (fileset2
[1], 1);
319 /* stderr to /dev/null */
320 res
= open ("/dev/null", O_WRONLY
);
323 execvp (path
, const_cast (char **, argv
));
328 /* --------------------------------------------------------------------------------------------- */
331 fish_set_env (int flags
)
335 tmp
= g_string_sized_new (250);
336 g_string_assign (tmp
, "");
338 if ((flags
& FISH_HAVE_HEAD
) != 0)
339 g_string_append (tmp
, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
341 if ((flags
& FISH_HAVE_SED
) != 0)
342 g_string_append (tmp
, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
344 if ((flags
& FISH_HAVE_AWK
) != 0)
345 g_string_append (tmp
, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
347 if ((flags
& FISH_HAVE_PERL
) != 0)
348 g_string_append (tmp
, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
350 if ((flags
& FISH_HAVE_LSQ
) != 0)
351 g_string_append (tmp
, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
353 if ((flags
& FISH_HAVE_DATE_MDYT
) != 0)
354 g_string_append (tmp
, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
356 if ((flags
& FISH_HAVE_TAIL
) != 0)
357 g_string_append (tmp
, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
359 return g_string_free (tmp
, FALSE
);
362 /* --------------------------------------------------------------------------------------------- */
365 fish_info (struct vfs_class
*me
, struct vfs_s_super
*super
)
368 if (fish_command (me
, super
, NONE
, SUP
.scr_info
) == COMPLETE
)
373 res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
374 if ((!res
) || (res
== EINTR
))
375 ERRNOR (ECONNRESET
, FALSE
);
376 if (!strncmp (buffer
, "### ", 4))
378 SUP
.host_flags
= atol (buffer
);
382 ERRNOR (E_PROTO
, FALSE
);
386 /* --------------------------------------------------------------------------------------------- */
387 /* The returned directory should always contain a trailing slash */
390 fish_getcwd (struct vfs_class
*me
, struct vfs_s_super
*super
)
392 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
393 return g_strconcat (reply_str
, "/", (char *) NULL
);
398 /* --------------------------------------------------------------------------------------------- */
401 fish_open_archive_pipeopen (struct vfs_s_super
*super
)
404 const char *argv
[10]; /* All of 10 is used now */
405 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
409 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
412 if (SUP
.flags
> FISH_FLAG_RSH
)
415 g_snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
420 * Add the user name to the ssh command line only if it was explicitly
421 * set in vfs URL. rsh/ssh will get current user by default
422 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
423 * option breaks it for some)
429 argv
[i
++] = SUP
.user
;
433 /* The rest of the code assumes it to be a valid username */
434 SUP
.user
= vfs_get_local_username ();
437 argv
[i
++] = SUP
.host
;
438 argv
[i
++] = "echo FISH:; /bin/sh";
441 fish_pipeopen (super
, xsh
, argv
);
444 /* --------------------------------------------------------------------------------------------- */
447 fish_open_archive_talk (struct vfs_class
*me
, struct vfs_s_super
*super
)
451 printf ("\n%s\n", _("fish: Waiting for initial line..."));
453 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
456 if (strstr (answer
, "assword"))
458 /* Currently, this does not work. ssh reads passwords from
459 /dev/tty, not from stdin :-(. */
461 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
468 p
= g_strdup_printf (_("fish: Password is required for %s"), SUP
.user
);
469 op
= vfs_get_password (p
);
475 printf ("\n%s\n", _("fish: Sending password..."));
478 str_len
= strlen (SUP
.password
);
479 if ((write (SUP
.sockw
, SUP
.password
, str_len
) != (ssize_t
) str_len
)
480 || (write (SUP
.sockw
, "\n", 1) != 1))
490 /* --------------------------------------------------------------------------------------------- */
493 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
500 fish_open_archive_pipeopen (super
);
502 /* Start talk with ssh-server (password prompt, etc ) */
503 ftalk
= fish_open_archive_talk (me
, super
);
509 ERRNOR (E_PROTO
, -1);
511 print_vfs_message (_("fish: Sending initial line..."));
513 * Run `start_fish_server'. If it doesn't exist - no problem,
514 * we'll talk directly to the shell.
518 (me
, super
, WAIT_REPLY
,
519 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE
)
520 ERRNOR (E_PROTO
, -1);
522 print_vfs_message (_("fish: Handshaking version..."));
523 if (fish_command (me
, super
, WAIT_REPLY
, "#VER 0.0.3\necho '### 000'\n") != COMPLETE
)
524 ERRNOR (E_PROTO
, -1);
526 /* Set up remote locale to C, otherwise dates cannot be recognized */
528 (me
, super
, WAIT_REPLY
,
529 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE
)
530 ERRNOR (E_PROTO
, -1);
532 print_vfs_message (_("fish: Getting host info..."));
533 if (fish_info (me
, super
))
534 SUP
.scr_env
= fish_set_env (SUP
.host_flags
);
536 print_vfs_message (_("fish: Setting up current directory..."));
537 SUP
.cwdir
= fish_getcwd (me
, super
);
538 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
540 super
->name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
542 super
->name
= g_strdup (PATH_SEP_STR
);
544 super
->root
= vfs_s_new_inode (me
, super
, vfs_s_default_stat (me
, S_IFDIR
| 0755));
548 /* --------------------------------------------------------------------------------------------- */
551 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
552 const char *archive_name
, char *op
)
554 char *host
, *user
, *password
, *p
;
559 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
560 &password
, 0, URL_NOSLASH
| URL_USE_ANONYMOUS
);
567 if (!strncmp (op
, "rsh:", 4))
568 SUP
.flags
= FISH_FLAG_RSH
;
571 SUP
.password
= password
;
572 SUP
.scr_ls
= fish_load_script_from_file (host
, FISH_LS_FILE
, FISH_LS_DEF_CONTENT
);
573 SUP
.scr_exists
= fish_load_script_from_file (host
, FISH_EXISTS_FILE
, FISH_EXISTS_DEF_CONTENT
);
574 SUP
.scr_mkdir
= fish_load_script_from_file (host
, FISH_MKDIR_FILE
, FISH_MKDIR_DEF_CONTENT
);
575 SUP
.scr_unlink
= fish_load_script_from_file (host
, FISH_UNLINK_FILE
, FISH_UNLINK_DEF_CONTENT
);
576 SUP
.scr_chown
= fish_load_script_from_file (host
, FISH_CHOWN_FILE
, FISH_CHOWN_DEF_CONTENT
);
577 SUP
.scr_chmod
= fish_load_script_from_file (host
, FISH_CHMOD_FILE
, FISH_CHMOD_DEF_CONTENT
);
578 SUP
.scr_rmdir
= fish_load_script_from_file (host
, FISH_RMDIR_FILE
, FISH_RMDIR_DEF_CONTENT
);
579 SUP
.scr_ln
= fish_load_script_from_file (host
, FISH_LN_FILE
, FISH_LN_DEF_CONTENT
);
580 SUP
.scr_mv
= fish_load_script_from_file (host
, FISH_MV_FILE
, FISH_MV_DEF_CONTENT
);
582 fish_load_script_from_file (host
, FISH_HARDLINK_FILE
, FISH_HARDLINK_DEF_CONTENT
);
583 SUP
.scr_get
= fish_load_script_from_file (host
, FISH_GET_FILE
, FISH_GET_DEF_CONTENT
);
584 SUP
.scr_send
= fish_load_script_from_file (host
, FISH_SEND_FILE
, FISH_SEND_DEF_CONTENT
);
585 SUP
.scr_append
= fish_load_script_from_file (host
, FISH_APPEND_FILE
, FISH_APPEND_DEF_CONTENT
);
586 SUP
.scr_info
= fish_load_script_from_file (host
, FISH_INFO_FILE
, FISH_INFO_DEF_CONTENT
);
587 return fish_open_archive_int (me
, super
);
590 /* --------------------------------------------------------------------------------------------- */
593 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
594 const char *archive_name
, char *op
, void *cookie
)
604 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
605 URL_NOSLASH
| URL_USE_ANONYMOUS
);
610 user
= vfs_get_local_username ();
612 result
= ((strcmp (host
, SUP
.host
) == 0)
613 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
621 /* --------------------------------------------------------------------------------------------- */
624 fish_dir_load (struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
626 struct vfs_s_super
*super
= dir
->super
;
628 struct vfs_s_entry
*ent
= NULL
;
632 gchar
*shell_commands
;
635 * Simple FISH debug interface :]
638 if (!(MEDATA
->logfile
))
640 MEDATA
->logfile
= fopen ("/tmp/mc-FISH.sh", "w");
643 logfile
= MEDATA
->logfile
;
645 print_vfs_message (_("fish: Reading directory %s..."), remote_path
);
647 gettimeofday (&dir
->timestamp
, NULL
);
648 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
649 quoted_path
= strutils_shell_escape (remote_path
);
650 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_ls
, (char *) NULL
);
651 fish_command (me
, super
, NONE
, shell_commands
, quoted_path
);
652 g_free (shell_commands
);
653 g_free (quoted_path
);
654 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
657 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
658 if ((!res
) || (res
== EINTR
))
660 vfs_s_free_entry (me
, ent
);
661 me
->verrno
= ECONNRESET
;
666 fputs (buffer
, logfile
);
667 fputs ("\n", logfile
);
670 if (!strncmp (buffer
, "### ", 4))
676 vfs_s_insert_entry (me
, dir
, ent
);
677 ent
= vfs_s_generate_entry (me
, NULL
, dir
, 0);
682 #define ST ent->ino->st
689 char *data_start
= buffer
+ 1;
690 char *filename
= data_start
;
691 char *linkname
= data_start
;
692 char *filename_bound
= filename
+ strlen (filename
);
693 char *linkname_bound
= filename_bound
;
694 if (!strcmp (data_start
, "\".\"") || !strcmp (data_start
, "\"..\""))
695 break; /* We'll do "." and ".." ourselves */
697 if (S_ISLNK (ST
.st_mode
))
699 /* we expect: "escaped-name" -> "escaped-name"
700 // -> cannot occur in filenames,
701 // because it will be escaped to -\> */
703 if (*filename
== '"')
706 linkname
= strstr (filename
, "\" -> \"");
709 /* broken client, or smth goes wrong */
710 linkname
= filename_bound
;
711 if (filename_bound
> filename
&& *(filename_bound
- 1) == '"')
712 --filename_bound
; /* skip trailing " */
716 filename_bound
= linkname
;
717 linkname
+= 6; /* strlen ("\" -> \"") */
718 if (*(linkname_bound
- 1) == '"')
719 --linkname_bound
; /* skip trailing " */
722 ent
->name
= g_strndup (filename
, filename_bound
- filename
);
724 ent
->name
= strutils_shell_unescape (ent
->name
);
727 ent
->ino
->linkname
= g_strndup (linkname
, linkname_bound
- linkname
);
728 temp
= ent
->ino
->linkname
;
729 ent
->ino
->linkname
= strutils_shell_unescape (ent
->ino
->linkname
);
734 /* we expect: "escaped-name" */
735 if (filename_bound
- filename
> 2)
738 there is at least 2 "
741 if (*filename
== '"')
743 if (*(filename_bound
- 1) == '"')
746 ent
->name
= g_strndup (filename
, filename_bound
- filename
);
748 ent
->name
= strutils_shell_unescape (ent
->name
);
755 ST
.st_size
= (off_t
) atoll (buffer
+ 1);
757 ST
.st_size
= (off_t
) atof (buffer
+ 1);
763 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
770 we expect: Roctal-filemode octal-filetype uid.gid
773 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
778 vfs_split_text (buffer
+ 1);
779 if (!vfs_parse_filedate (0, &ST
.st_ctime
))
781 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
787 if (sscanf (buffer
+ 1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
788 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
790 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime (&tim
);
796 if (sscanf (buffer
+ 1, "%d,%d", &maj
, &min
) != 2)
798 #ifdef HAVE_STRUCT_STAT_ST_RDEV
799 ST
.st_rdev
= makedev (maj
, min
);
805 vfs_s_free_entry (me
, ent
);
806 reply_code
= fish_decode_reply (buffer
+ 4, 0);
807 if (reply_code
== COMPLETE
)
810 SUP
.cwdir
= g_strdup (remote_path
);
811 print_vfs_message (_("%s: done."), me
->name
);
814 else if (reply_code
== ERROR
)
820 me
->verrno
= E_REMOTE
;
824 print_vfs_message (_("%s: failure"), me
->name
);
828 /* --------------------------------------------------------------------------------------------- */
831 fish_file_store (struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
833 gchar
*shell_commands
= NULL
;
834 struct vfs_s_super
*super
= FH_SUPER
;
842 h
= open (localname
, O_RDONLY
);
846 if (fstat (h
, &s
) < 0)
852 /* First, try this as stor:
854 * ( head -c number ) | ( cat > file; cat >/dev/null )
856 * If `head' is not present on the remote system, `dd' will be used.
857 * Unfortunately, we cannot trust most non-GNU `head' implementations
858 * even if `-c' options is supported. Therefore, we separate GNU head
859 * (and other modern heads?) using `-q' and `-' . This causes another
860 * implementations to fail (because of "incorrect options").
865 * while [ $rest -gt 0 ]
867 * cnt=`expr \( $rest + 255 \) / 256`
868 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
869 * rest=`expr $rest - $n`
872 * `dd' was not designed for full filling of input buffers,
873 * and does not report exact number of bytes (not blocks).
874 * Therefore a more complex shell script is needed.
876 * On some systems non-GNU head writes "Usage:" error report to stdout
877 * instead of stderr. It makes impossible the use of "head || dd"
878 * algorithm for file appending case, therefore just "dd" is used for it.
881 quoted_name
= strutils_shell_escape (name
);
882 print_vfs_message (_("fish: store %s: sending command..."), quoted_name
);
884 /* FIXME: File size is limited to ULONG_MAX */
885 if (!fh
->u
.fish
.append
)
887 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX
";\n",
888 SUP
.scr_append
, (char *) NULL
);
889 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
,
890 (uintmax_t) s
.st_size
);
891 g_free (shell_commands
);
895 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX
";\n",
896 SUP
.scr_send
, (char *) NULL
);
897 n
= fish_command (me
, super
, WAIT_REPLY
, shell_commands
, quoted_name
,
898 (uintmax_t) s
.st_size
);
899 g_free (shell_commands
);
904 ERRNOR (E_REMOTE
, -1);
912 while ((n
= read (h
, buffer
, sizeof (buffer
))) < 0)
914 if ((errno
== EINTR
) && tty_got_interrupt ())
916 print_vfs_message (_("fish: Local read failed, sending zeros"));
918 h
= open ("/dev/zero", O_RDONLY
);
924 t
= write (SUP
.sockw
, buffer
, n
);
933 tty_disable_interrupt_key ();
935 print_vfs_message ("%s: %d/%" PRIuMAX
,
936 was_error
? _("fish: storing zeros") : _("fish: storing file"),
937 total
, (uintmax_t) s
.st_size
);
940 g_free (quoted_name
);
941 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
942 ERRNOR (E_REMOTE
, -1);
946 fish_get_reply (me
, SUP
.sockr
, NULL
, 0);
947 g_free (quoted_name
);
951 /* --------------------------------------------------------------------------------------------- */
954 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
956 gchar
*shell_commands
= NULL
;
957 struct vfs_s_super
*super
= FH_SUPER
;
961 name
= vfs_s_fullpath (me
, fh
->ino
);
964 quoted_name
= strutils_shell_escape (name
);
966 fh
->u
.fish
.append
= 0;
969 * Check whether the remote file is readable by using `dd' to copy
970 * a single byte from the remote file to /dev/null. If `dd' completes
971 * with exit status of 0 use `cat' to send the file contents to the
972 * standard output (i.e. over the network).
975 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX
";\n",
976 SUP
.scr_get
, (char *) NULL
);
977 offset
= fish_command (me
, super
, WANT_STRING
, shell_commands
, quoted_name
, (uintmax_t) offset
);
978 g_free (shell_commands
);
979 g_free (quoted_name
);
980 if (offset
!= PRELIM
)
981 ERRNOR (E_REMOTE
, 0);
982 fh
->linear
= LS_LINEAR_OPEN
;
985 #if SIZEOF_OFF_T == SIZEOF_LONG
986 fh
->u
.fish
.total
= (off_t
) strtol (reply_str
, NULL
, 10);
988 fh
->u
.fish
.total
= (off_t
) strtoll (reply_str
, NULL
, 10);
991 ERRNOR (E_REMOTE
, 0);
995 /* --------------------------------------------------------------------------------------------- */
998 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
1000 struct vfs_s_super
*super
= FH_SUPER
;
1004 print_vfs_message (_("Aborting transfer..."));
1007 n
= MIN (8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
1010 n
= read (SUP
.sockr
, buffer
, n
);
1013 fh
->u
.fish
.got
+= n
;
1018 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
1019 print_vfs_message (_("Error reported after abort."));
1021 print_vfs_message (_("Aborted transfer would be successful."));
1024 /* --------------------------------------------------------------------------------------------- */
1027 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, size_t len
)
1029 struct vfs_s_super
*super
= FH_SUPER
;
1031 len
= MIN ((size_t) (fh
->u
.fish
.total
- fh
->u
.fish
.got
), len
);
1032 tty_disable_interrupt_key ();
1033 while (len
!= 0 && ((n
= read (SUP
.sockr
, buf
, len
)) < 0))
1035 if ((errno
== EINTR
) && !tty_got_interrupt ())
1039 tty_enable_interrupt_key ();
1042 fh
->u
.fish
.got
+= n
;
1044 fish_linear_abort (me
, fh
);
1045 else if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
1046 ERRNOR (E_REMOTE
, -1);
1050 /* --------------------------------------------------------------------------------------------- */
1053 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
1055 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
1056 fish_linear_abort (me
, fh
);
1059 /* --------------------------------------------------------------------------------------------- */
1062 fish_ctl (void *fh
, int ctlop
, void *arg
)
1071 case VFS_CTL_IS_NOTREADY
:
1076 vfs_die ("You may not do this");
1077 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
1080 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
1081 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
1091 /* --------------------------------------------------------------------------------------------- */
1094 fish_send_command (struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
1098 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
1099 vfs_stamp_create (&vfs_fish_ops
, super
);
1101 ERRNOR (E_REMOTE
, -1);
1102 if (flags
& OPT_FLUSH
)
1103 vfs_s_invalidate (me
, super
);
1107 /* --------------------------------------------------------------------------------------------- */
1110 fish_rename (struct vfs_class
*me
, const char *path1
, const char *path2
)
1112 gchar
*shell_commands
= NULL
;
1113 char buf
[BUF_LARGE
];
1114 const char *crpath1
, *crpath2
;
1115 char *rpath1
, *rpath2
, *mpath1
, *mpath2
;
1116 struct vfs_s_super
*super
, *super2
;
1118 mpath1
= g_strdup (path1
);
1119 crpath1
= vfs_s_get_path_mangle (me
, mpath1
, &super
, 0);
1120 if (crpath1
== NULL
)
1125 mpath2
= g_strdup (path2
);
1126 crpath2
= vfs_s_get_path_mangle (me
, mpath2
, &super2
, 0);
1127 if (crpath2
== NULL
)
1133 rpath1
= strutils_shell_escape (crpath1
);
1135 rpath2
= strutils_shell_escape (crpath2
);
1137 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1138 SUP
.scr_mv
, (char *) NULL
);
1139 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1140 g_free (shell_commands
);
1143 return fish_send_command (me
, super2
, buf
, OPT_FLUSH
);
1146 /* --------------------------------------------------------------------------------------------- */
1149 fish_link (struct vfs_class
*me
, const char *path1
, const char *path2
)
1151 gchar
*shell_commands
= NULL
;
1152 char buf
[BUF_LARGE
];
1153 const char *crpath1
, *crpath2
;
1154 char *rpath1
, *rpath2
, *mpath1
, *mpath2
;
1155 struct vfs_s_super
*super
, *super2
;
1157 mpath1
= g_strdup (path1
);
1158 crpath1
= vfs_s_get_path_mangle (me
, mpath1
, &super
, 0);
1159 if (crpath1
== NULL
)
1164 mpath2
= g_strdup (path2
);
1165 crpath2
= vfs_s_get_path_mangle (me
, mpath2
, &super2
, 0);
1166 if (crpath2
== NULL
)
1172 rpath1
= strutils_shell_escape (crpath1
);
1174 rpath2
= strutils_shell_escape (crpath2
);
1176 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1177 SUP
.scr_hardlink
, (char *) NULL
);
1178 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath1
, rpath2
);
1179 g_free (shell_commands
);
1182 return fish_send_command (me
, super2
, buf
, OPT_FLUSH
);
1186 /* --------------------------------------------------------------------------------------------- */
1189 fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
1192 gchar
*shell_commands
= NULL
;
1193 char buf
[BUF_LARGE
];
1195 char *rpath
, *mpath
;
1196 struct vfs_s_super
*super
;
1198 mpath
= g_strdup (path
);
1199 crpath
= vfs_s_get_path_mangle (me
, mpath
, &super
, 0);
1205 rpath
= strutils_shell_escape (crpath
);
1208 qsetto
= strutils_shell_escape (setto
);
1209 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1210 SUP
.scr_ln
, (char *) NULL
);
1211 g_snprintf (buf
, sizeof (buf
), shell_commands
, qsetto
, rpath
);
1212 g_free (shell_commands
);
1215 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1218 /* --------------------------------------------------------------------------------------------- */
1221 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
1223 gchar
*shell_commands
= NULL
;
1227 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1228 SUP
.scr_chmod
, (char *) NULL
);
1229 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, mode
& 07777);
1230 g_free (shell_commands
);
1232 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1235 /* --------------------------------------------------------------------------------------------- */
1238 fish_chown (struct vfs_class
*me
, const char *path
, uid_t owner
, gid_t group
)
1240 char *sowner
, *sgroup
;
1244 pw
= getpwuid (owner
);
1248 gr
= getgrgid (group
);
1252 sowner
= pw
->pw_name
;
1253 sgroup
= gr
->gr_name
;
1255 gchar
*shell_commands
= NULL
;
1259 shell_commands
= g_strconcat (SUP
.scr_env
,
1260 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1261 SUP
.scr_chown
, (char *) NULL
);
1262 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
, sowner
, sgroup
);
1263 g_free (shell_commands
);
1264 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1265 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1266 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1268 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1272 /* --------------------------------------------------------------------------------------------- */
1275 fish_unlink (struct vfs_class
*me
, const char *path
)
1277 gchar
*shell_commands
= NULL
;
1282 g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_unlink
, (char *) NULL
);
1283 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1284 g_free (shell_commands
);
1286 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1289 /* --------------------------------------------------------------------------------------------- */
1292 fish_exists (struct vfs_class
*me
, const char *path
)
1294 gchar
*shell_commands
= NULL
;
1299 g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_exists
, (char *) NULL
);
1300 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1301 g_free (shell_commands
);
1304 return (fish_send_command (me
, super
, buf
, OPT_FLUSH
) == 0) ? 1 : 0;
1308 /* --------------------------------------------------------------------------------------------- */
1311 fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1313 gchar
*shell_commands
= NULL
;
1320 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_mkdir
, (char *) NULL
);
1321 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1322 g_free (shell_commands
);
1325 ret_code
= fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1330 if (!fish_exists (me
, path
))
1332 ERRNOR (EACCES
, -1);
1337 /* --------------------------------------------------------------------------------------------- */
1340 fish_rmdir (struct vfs_class
*me
, const char *path
)
1342 gchar
*shell_commands
= NULL
;
1346 shell_commands
= g_strconcat (SUP
.scr_env
, "FISH_FILENAME=%s;\n", SUP
.scr_rmdir
, (char *) NULL
);
1347 g_snprintf (buf
, sizeof (buf
), shell_commands
, rpath
);
1348 g_free (shell_commands
);
1350 return fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1353 /* --------------------------------------------------------------------------------------------- */
1356 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
, mode_t mode
)
1360 fh
->u
.fish
.append
= 0;
1361 /* File will be written only, so no need to retrieve it */
1362 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
)))
1364 fh
->u
.fish
.append
= flags
& O_APPEND
;
1365 if (!fh
->ino
->localname
)
1367 int tmp_handle
= vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1368 fh
->ino
->ent
->name
);
1369 if (tmp_handle
== -1)
1375 if (!fh
->ino
->localname
)
1376 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1378 if (!fh
->ino
->localname
)
1379 vfs_die ("retrieve_file failed to fill in localname");
1383 /* --------------------------------------------------------------------------------------------- */
1386 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1388 struct vfs_s_super
*super
= MEDATA
->supers
;
1395 const char *flags
= "";
1401 case FISH_FLAG_COMPRESSED
:
1405 if (SUP
.flags
> FISH_FLAG_RSH
)
1408 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1414 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
, "/", SUP
.cwdir
, (char *) NULL
);
1417 super
= super
->next
;
1421 /* --------------------------------------------------------------------------------------------- */
1424 fish_open (struct vfs_class
*me
, const char *file
, int flags
, mode_t mode
)
1427 sorry, i've places hack here
1428 cause fish don't able to open files with O_EXCL flag
1431 return vfs_s_open (me
, file
, flags
, mode
);
1434 /* --------------------------------------------------------------------------------------------- */
1435 /*** public functions ****************************************************************************/
1436 /* --------------------------------------------------------------------------------------------- */
1441 static struct vfs_s_subclass fish_subclass
;
1445 fish_subclass
.flags
= VFS_S_REMOTE
;
1446 fish_subclass
.archive_same
= fish_archive_same
;
1447 fish_subclass
.open_archive
= fish_open_archive
;
1448 fish_subclass
.free_archive
= fish_free_archive
;
1449 fish_subclass
.fh_open
= fish_fh_open
;
1450 fish_subclass
.dir_load
= fish_dir_load
;
1451 fish_subclass
.file_store
= fish_file_store
;
1452 fish_subclass
.linear_start
= fish_linear_start
;
1453 fish_subclass
.linear_read
= fish_linear_read
;
1454 fish_subclass
.linear_close
= fish_linear_close
;
1456 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1457 vfs_fish_ops
.name
= "fish";
1458 vfs_fish_ops
.prefix
= "sh:";
1459 vfs_fish_ops
.fill_names
= fish_fill_names
;
1460 vfs_fish_ops
.chmod
= fish_chmod
;
1461 vfs_fish_ops
.chown
= fish_chown
;
1462 vfs_fish_ops
.open
= fish_open
;
1463 vfs_fish_ops
.symlink
= fish_symlink
;
1464 vfs_fish_ops
.link
= fish_link
;
1465 vfs_fish_ops
.unlink
= fish_unlink
;
1466 vfs_fish_ops
.rename
= fish_rename
;
1467 vfs_fish_ops
.mkdir
= fish_mkdir
;
1468 vfs_fish_ops
.rmdir
= fish_rmdir
;
1469 vfs_fish_ops
.ctl
= fish_ctl
;
1470 vfs_register_class (&vfs_fish_ops
);
1473 /* --------------------------------------------------------------------------------------------- */