1 /* Virtual File System: FISH implementation for transfering files over
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007 Free Software Foundation, Inc.
7 Written by: 1998 Pavel Machek
8 Spaces fix: 2000 Michal Svec
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public License
14 as published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Library General Public License for more details.
22 You should have received a copy of the GNU Library General Public
23 License along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 * \brief Source: Virtual File System: FISH implementation for transfering files over
30 * \author Pavel Machek
34 * Derived from ftpfs.c
35 * Read README.fish for protocol specification.
37 * Syntax of path is: /#sh:user@host[:Cr]/path
38 * where C means you want compressed connection,
39 * and r means you want to use rsh
41 * Namespace: fish_vfs_ops exported.
44 /* Define this if your ssh can take -I option */
51 #include <sys/time.h> /* gettimeofday() */
55 #include "../src/global.h"
57 #include "../src/tty/tty.h" /* enable/disable interrupt key */
59 #include "../src/wtools.h" /* message() */
60 #include "../src/main.h" /* print_vfs_message */
61 #include "../src/util.h"
62 #include "../src/strescape.h"
63 #include "../src/unixcompat.h"
64 #include "../src/fs.h"
66 #include "xdirentry.h"
69 #include "gc.h" /* vfs_stamp_create */
73 int fish_directory_timeout
= 900;
75 #define DO_RESOLVE_SYMLINK 1
77 #define DO_FREE_RESOURCE 4
79 #define FISH_FLAG_COMPRESSED 1
80 #define FISH_FLAG_RSH 2
83 #define OPT_IGNORE_ERROR 2
88 #define PRELIM 1 /* positive preliminary */
89 #define COMPLETE 2 /* positive completion */
90 #define CONTINUE 3 /* positive intermediate */
91 #define TRANSIENT 4 /* transient negative completion */
92 #define ERROR 5 /* permanent negative completion */
94 /* command wait_flag: */
96 #define WAIT_REPLY 0x01
97 #define WANT_STRING 0x02
98 static char reply_str
[80];
100 static struct vfs_class vfs_fish_ops
;
103 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
,
104 int wait_reply
, const char *fmt
, ...)
105 __attribute__ ((format (__printf__
, 4, 5)));
107 static int fish_decode_reply (char *s
, int was_garbage
)
110 if (!sscanf(s
, "%d", &code
)) {
114 if (code
<100) return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
118 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
119 static int fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
125 if (!vfs_s_get_line(me
, sock
, answer
, sizeof(answer
), '\n')) {
131 if (strncmp(answer
, "### ", 4)) {
134 g_strlcpy(string_buf
, answer
, string_len
);
135 } else return fish_decode_reply(answer
+4, was_garbage
);
139 #define SUP super->u.fish
142 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
,
143 int wait_reply
, const char *fmt
, ...)
148 FILE *logfile
= MEDATA
->logfile
;
152 str
= g_strdup_vprintf (fmt
, ap
);
156 fwrite (str
, strlen (str
), 1, logfile
);
160 tty_enable_interrupt_key ();
162 status
= write (SUP
.sockw
, str
, strlen (str
));
165 tty_disable_interrupt_key ();
170 return fish_get_reply (me
, SUP
.sockr
,
171 (wait_reply
& WANT_STRING
) ? reply_str
:
172 NULL
, sizeof (reply_str
) - 1);
177 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
179 if ((SUP
.sockw
!= -1) || (SUP
.sockr
!= -1)) {
180 print_vfs_message (_("fish: Disconnecting from %s"),
181 super
->name
? super
->name
: "???");
182 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
185 SUP
.sockw
= SUP
.sockr
= -1;
190 g_free (SUP
.password
);
194 fish_pipeopen(struct vfs_s_super
*super
, const char *path
, const char *argv
[])
196 int fileset1
[2], fileset2
[2];
199 if ((pipe(fileset1
)<0) || (pipe(fileset2
)<0))
200 vfs_die("Cannot pipe(): %m.");
202 if ((res
= fork())) {
203 if (res
<0) vfs_die("Cannot fork(): %m.");
204 /* We are the parent */
206 SUP
.sockw
= fileset1
[1];
208 SUP
.sockr
= fileset2
[0];
212 close(fileset1
[0]); close(fileset1
[1]);
215 /* stderr to /dev/null */
216 open ("/dev/null", O_WRONLY
);
217 close(fileset2
[0]); close(fileset2
[1]);
218 execvp(path
, const_cast(char **, argv
));
223 /* The returned directory should always contain a trailing slash */
224 static char *fish_getcwd(struct vfs_class
*me
, struct vfs_s_super
*super
)
226 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
227 return g_strconcat (reply_str
, "/", (char *) NULL
);
232 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
236 const char *argv
[10]; /* All of 10 is used now */
237 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
241 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
244 if (SUP
.flags
> FISH_FLAG_RSH
)
247 g_snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
252 argv
[i
++] = SUP
.user
;
253 argv
[i
++] = SUP
.host
;
254 argv
[i
++] = "echo FISH:; /bin/sh";
257 fish_pipeopen (super
, xsh
, argv
);
261 print_vfs_message (_("fish: Waiting for initial line..."));
262 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
263 ERRNOR (E_PROTO
, -1);
264 print_vfs_message ("%s", answer
);
265 if (strstr (answer
, "assword")) {
267 /* Currently, this does not work. ssh reads passwords from
268 /dev/tty, not from stdin :-(. */
270 message (D_ERROR
, MSG_ERROR
,
272 ("Sorry, we cannot do password authenticated connections for now."));
276 p
= g_strconcat (_(" fish: Password required for "),
277 SUP
.user
, " ", (char *) NULL
);
278 op
= vfs_get_password (p
);
284 print_vfs_message (_("fish: Sending password..."));
285 write (SUP
.sockw
, SUP
.password
, strlen (SUP
.password
));
286 write (SUP
.sockw
, "\n", 1);
290 print_vfs_message (_("fish: Sending initial line..."));
292 * Run `start_fish_server'. If it doesn't exist - no problem,
293 * we'll talk directly to the shell.
296 (me
, super
, WAIT_REPLY
,
297 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
299 ERRNOR (E_PROTO
, -1);
301 print_vfs_message (_("fish: Handshaking version..."));
303 (me
, super
, WAIT_REPLY
,
304 "#VER 0.0.0\necho '### 000'\n") != COMPLETE
)
305 ERRNOR (E_PROTO
, -1);
307 /* Set up remote locale to C, otherwise dates cannot be recognized */
309 (me
, super
, WAIT_REPLY
,
310 "LANG=C; LC_ALL=C; LC_TIME=C\n"
311 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
313 ERRNOR (E_PROTO
, -1);
315 print_vfs_message (_("fish: Setting up current directory..."));
316 SUP
.cwdir
= fish_getcwd (me
, super
);
317 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
320 g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
322 super
->name
= g_strdup (PATH_SEP_STR
);
325 vfs_s_new_inode (me
, super
,
326 vfs_s_default_stat (me
, S_IFDIR
| 0755));
331 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
332 const char *archive_name
, char *op
)
334 char *host
, *user
, *password
, *p
;
339 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
340 &password
, 0, URL_NOSLASH
);
347 if (!strncmp (op
, "rsh:", 4))
348 SUP
.flags
= FISH_FLAG_RSH
;
351 SUP
.password
= password
;
352 return fish_open_archive_int (me
, super
);
356 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
357 const char *archive_name
, char *op
, void *cookie
)
366 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
371 flags
= ((strcmp (host
, SUP
.host
) == 0)
372 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
380 fish_dir_load(struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
382 struct vfs_s_super
*super
= dir
->super
;
384 struct vfs_s_entry
*ent
= NULL
;
388 gchar
*shell_commands
;
392 * Simple FISH debug interface :]
394 if (!(MEDATA
->logfile
))
396 MEDATA
->logfile
= fopen("/tmp/mc-FISH.sh", "w");
400 logfile
= MEDATA
->logfile
;
402 print_vfs_message(_("fish: Reading directory %s..."), remote_path
);
404 gettimeofday(&dir
->timestamp
, NULL
);
405 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
406 quoted_path
= strutils_shell_escape (remote_path
);
407 shell_commands
= g_strconcat(
409 "if `perl -v > /dev/null 2>&1` ; then\n"
414 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
415 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
416 "my $dirname = $ARGV[0];\n"
417 "if (opendir ( DIR, $dirname )) {\n"
418 "while( (my $filename = readdir(DIR))){\n"
419 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
420 "my $mloctime= scalar localtime $mtime;\n"
422 "my $strutils_shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
423 "my $e_filename = $filename;\n"
424 "$e_filename =~ $strutils_shell_escape_regex;\n"
425 "if (S_ISLNK($mode) ) {\n"
426 "my $linkname = readlink (\"$dirname/$filename\");\n"
427 "$linkname =~ $strutils_shell_escape_regex;\n"
429 "printf(\"R%%o %%o $uid.$gid\\n"
432 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
433 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
435 "printf(\"R%%o %%o $uid.$gid\\n"
438 ":\\\"$e_filename\\\"\\n"
439 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
442 "printf(\"### 200\\n\");\n"
445 "printf(\"### 500\\n\");\n"
448 "' /%s ||\n" /* ARGV[0] - path to browse */
449 " echo '### 500'\n" /* do not hang if perl is to eval it */
450 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
451 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
458 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
459 "while read p l u g s m d y n; do\n"
460 "if [ $ADD = 0 ]; then\n"
461 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
462 "elif `sed --version >/dev/null 2>&1` ; then\n"
463 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
465 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
467 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
470 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
471 "while read p l u g a i m d y n; do\n"
472 "if [ $ADD = 0 ]; then\n"
473 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
474 "elif `sed --version >/dev/null 2>&1` ; then\n"
475 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
476 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
478 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
489 fish_command (me
, super
, NONE
, shell_commands
,
490 quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
);
492 g_free(shell_commands
);
493 g_free (quoted_path
);
494 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
496 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
497 if ((!res
) || (res
== EINTR
)) {
498 vfs_s_free_entry(me
, ent
);
499 me
->verrno
= ECONNRESET
;
503 fputs (buffer
, logfile
);
504 fputs ("\n", logfile
);
507 if (!strncmp(buffer
, "### ", 4))
511 vfs_s_insert_entry(me
, dir
, ent
);
512 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
517 #define ST ent->ino->st
522 char *data_start
= buffer
+1;
523 char *filename
= data_start
;
524 char *linkname
= data_start
;
525 char *filename_bound
= filename
+ strlen(filename
);
526 char *linkname_bound
= filename_bound
;
527 if (!strcmp(data_start
, "\".\"") || !strcmp(data_start
, "\"..\""))
528 break; /* We'll do "." and ".." ourselves */
530 if (S_ISLNK(ST
.st_mode
)) {
531 /* we expect: "escaped-name" -> "escaped-name"
532 // -> cannot occur in filenames,
533 // because it will be escaped to -\> */
535 if (*filename
== '"')
538 linkname
= strstr(filename
, "\" -> \"");
541 /* broken client, or smth goes wrong */
542 linkname
= filename_bound
;
543 if (filename_bound
> filename
544 && *(filename_bound
- 1) == '"')
545 --filename_bound
; /* skip trailing " */
549 filename_bound
= linkname
;
550 linkname
+= 6; /* strlen ("\" -> \"") */
551 if (*(linkname_bound
- 1) == '"')
552 --linkname_bound
; /* skip trailing " */
555 ent
->name
= str_dup_range(filename
, filename_bound
);
557 ent
->name
= strutils_shell_unescape(ent
->name
);
560 ent
->ino
->linkname
= str_dup_range(linkname
, linkname_bound
);
561 temp
= ent
->ino
->linkname
;
562 ent
->ino
->linkname
= strutils_shell_unescape(ent
->ino
->linkname
);
565 /* we expect: "escaped-name" */
566 if (filename_bound
- filename
> 2)
569 there is at least 2 "
572 if (*filename
== '"')
574 if (*(filename_bound
- 1) == '"')
577 ent
->name
= str_dup_range(filename
, filename_bound
);
579 ent
->name
= strutils_shell_unescape(ent
->name
);
586 ST
.st_size
= (off_t
) atoll (buffer
+1);
588 ST
.st_size
= (off_t
) atof (buffer
+1);
593 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
599 we expect: Roctal-filemode octal-filetype uid.gid
602 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
606 vfs_split_text(buffer
+1);
607 if (!vfs_parse_filedate(0, &ST
.st_ctime
))
609 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
614 if (sscanf(buffer
+1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
615 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
617 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime(&tim
);
622 if (sscanf(buffer
+1, "%d,%d", &maj
, &min
) != 2)
624 #ifdef HAVE_STRUCT_STAT_ST_RDEV
625 ST
.st_rdev
= makedev (maj
, min
);
631 vfs_s_free_entry (me
, ent
);
632 reply_code
= fish_decode_reply(buffer
+ 4, 0);
633 if (reply_code
== COMPLETE
) {
635 SUP
.cwdir
= g_strdup (remote_path
);
636 print_vfs_message (_("%s: done."), me
->name
);
638 } else if (reply_code
== ERROR
) {
641 me
->verrno
= E_REMOTE
;
645 print_vfs_message (_("%s: failure"), me
->name
);
650 fish_file_store(struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
652 struct vfs_s_super
*super
= FH_SUPER
;
660 h
= open (localname
, O_RDONLY
);
664 if (fstat(h
, &s
)<0) {
669 /* First, try this as stor:
671 * ( head -c number ) | ( cat > file; cat >/dev/null )
673 * If `head' is not present on the remote system, `dd' will be used.
674 * Unfortunately, we cannot trust most non-GNU `head' implementations
675 * even if `-c' options is supported. Therefore, we separate GNU head
676 * (and other modern heads?) using `-q' and `-' . This causes another
677 * implementations to fail (because of "incorrect options").
682 * while [ $rest -gt 0 ]
684 * cnt=`expr \( $rest + 255 \) / 256`
685 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
686 * rest=`expr $rest - $n`
689 * `dd' was not designed for full filling of input buffers,
690 * and does not report exact number of bytes (not blocks).
691 * Therefore a more complex shell script is needed.
693 * On some systems non-GNU head writes "Usage:" error report to stdout
694 * instead of stderr. It makes impossible the use of "head || dd"
695 * algorithm for file appending case, therefore just "dd" is used for it.
698 quoted_name
= strutils_shell_escape(name
);
699 print_vfs_message(_("fish: store %s: sending command..."), quoted_name
);
701 /* FIXME: File size is limited to ULONG_MAX */
702 if (!fh
->u
.fish
.append
)
703 n
= fish_command (me
, super
, WAIT_REPLY
,
709 "head -c %lu -q - || echo DD >&3\n"
710 ") 2>/dev/null | (\n"
713 ")`; [ \"$res\" = DD ] && {\n"
716 "while [ $rest -gt 0 ]\n"
718 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
719 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
720 " rest=`expr $rest - $n`\n"
722 "}; echo '### 200'\n",
723 (unsigned long) s
.st_size
, quoted_name
,
724 quoted_name
, (unsigned long) s
.st_size
,
725 (unsigned long) s
.st_size
);
727 n
= fish_command (me
, super
, WAIT_REPLY
,
733 "while [ $rest -gt 0 ]\n"
735 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
736 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
737 " rest=`expr $rest - $n`\n"
739 "}; echo '### 200'\n",
740 (unsigned long) s
.st_size
, quoted_name
,
741 quoted_name
, (unsigned long) s
.st_size
);
745 ERRNOR(E_REMOTE
, -1);
751 while ((n
= read(h
, buffer
, sizeof(buffer
))) < 0) {
752 if ((errno
== EINTR
) && tty_got_interrupt ())
754 print_vfs_message(_("fish: Local read failed, sending zeros") );
756 h
= open( "/dev/zero", O_RDONLY
);
760 if ((t
= write (SUP
.sockw
, buffer
, n
)) != n
) {
768 tty_disable_interrupt_key ();
770 print_vfs_message(_("fish: storing %s %d (%lu)"),
771 was_error
? _("zeros") : _("file"), total
,
772 (unsigned long) s
.st_size
);
776 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
777 ERRNOR (E_REMOTE
, -1);
781 fish_get_reply(me
, SUP
.sockr
, NULL
, 0);
787 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
792 ERRNOR (E_NOTSUPP
, 0);
793 name
= vfs_s_fullpath (me
, fh
->ino
);
796 quoted_name
= strutils_shell_escape(name
);
797 fh
->u
.fish
.append
= 0;
800 * Check whether the remote file is readable by using `dd' to copy
801 * a single byte from the remote file to /dev/null. If `dd' completes
802 * with exit status of 0 use `cat' to send the file contents to the
803 * standard output (i.e. over the network).
805 offset
= fish_command (me
, FH_SUPER
, WANT_STRING
,
807 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
809 "ls -ln /%s 2>/dev/null | (\n"
819 quoted_name
, quoted_name
, quoted_name
, quoted_name
);
820 g_free (quoted_name
);
821 if (offset
!= PRELIM
) ERRNOR (E_REMOTE
, 0);
822 fh
->linear
= LS_LINEAR_OPEN
;
825 #if SIZEOF_OFF_T == SIZEOF_LONG
826 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
828 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
831 ERRNOR (E_REMOTE
, 0);
836 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
838 struct vfs_s_super
*super
= FH_SUPER
;
842 print_vfs_message( _("Aborting transfer...") );
844 n
= MIN(8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
846 if ((n
= read(SUP
.sockr
, buffer
, n
)) < 0)
852 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
853 print_vfs_message( _("Error reported after abort.") );
855 print_vfs_message( _("Aborted transfer would be successful.") );
859 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
861 struct vfs_s_super
*super
= FH_SUPER
;
863 len
= MIN( fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
864 tty_disable_interrupt_key ();
865 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
))<0)) {
866 if ((errno
== EINTR
) && !tty_got_interrupt ())
870 tty_enable_interrupt_key();
872 if (n
>0) fh
->u
.fish
.got
+= n
;
873 if (n
<0) fish_linear_abort(me
, fh
);
874 if ((!n
) && ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)))
875 ERRNOR (E_REMOTE
, -1);
880 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
882 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
883 fish_linear_abort(me
, fh
);
887 fish_ctl (void *fh
, int ctlop
, void *arg
)
895 case VFS_CTL_IS_NOTREADY
:
900 vfs_die ("You may not do this");
901 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
904 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
905 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
916 fish_send_command(struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
920 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
921 vfs_stamp_create (&vfs_fish_ops
, super
);
922 if (r
!= COMPLETE
) ERRNOR (E_REMOTE
, -1);
923 if (flags
& OPT_FLUSH
)
924 vfs_s_invalidate(me
, super
);
929 char buf[BUF_LARGE]; \
930 const char *crpath; \
931 char *rpath, *mpath = g_strdup (path); \
932 struct vfs_s_super *super; \
933 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
937 rpath = strutils_shell_escape(crpath); \
940 #define POSTFIX(flags) \
942 return fish_send_command(me, super, buf, flags);
945 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
948 g_snprintf(buf
, sizeof(buf
), "#CHMOD %4.4o /%s\n"
949 "if chmod %4.4o /%s 2>/dev/null; then\n"
955 mode
& 07777, rpath
);
959 #define FISH_OP(name, string) \
960 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
962 char buf[BUF_LARGE]; \
963 const char *crpath1, *crpath2; \
964 char *rpath1, *rpath2, *mpath1, *mpath2; \
965 struct vfs_s_super *super1, *super2; \
966 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) { \
970 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) { \
975 rpath1 = strutils_shell_escape (crpath1); \
977 rpath2 = strutils_shell_escape (crpath2); \
979 g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
982 return fish_send_command(me, super2, buf, OPT_FLUSH); \
985 FISH_OP(rename
, "#RENAME /%s /%s\n"
986 "if mv /%s /%s 2>/dev/null; then\n"
991 FISH_OP(link
, "#LINK /%s /%s\n"
992 "if ln /%s /%s 2>/dev/null; then\n"
998 static int fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
1002 qsetto
= strutils_shell_escape (setto
);
1003 g_snprintf(buf
, sizeof(buf
),
1005 "if ln -s %s /%s 2>/dev/null; then\n"
1010 qsetto
, rpath
, qsetto
, rpath
);
1016 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
1018 char *sowner
, *sgroup
;
1022 if ((pw
= getpwuid (owner
)) == NULL
)
1025 if ((gr
= getgrgid (group
)) == NULL
)
1028 sowner
= pw
->pw_name
;
1029 sgroup
= gr
->gr_name
;
1032 g_snprintf (buf
, sizeof(buf
),
1033 "#CHOWN %s:%s /%s\n"
1034 "if chown %s:%s /%s 2>/dev/null; then\n"
1039 sowner
, sgroup
, rpath
,
1040 sowner
, sgroup
, rpath
);
1041 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
1042 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1043 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1048 static int fish_unlink (struct vfs_class
*me
, const char *path
)
1051 g_snprintf(buf
, sizeof(buf
),
1053 "if rm -f /%s 2>/dev/null; then\n"
1062 static int fish_exists (struct vfs_class
*me
, const char *path
)
1066 g_snprintf(buf
, sizeof(buf
),
1068 "ls -l /%s >/dev/null 2>/dev/null\n"
1074 if ( fish_send_command(me
, super
, buf
, OPT_FLUSH
) == 0 )
1081 static int fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1089 g_snprintf(buf
, sizeof(buf
),
1091 "if mkdir /%s 2>/dev/null; then\n"
1099 ret_code
= fish_send_command(me
, super
, buf
, OPT_FLUSH
);
1101 if ( ret_code
!= 0 )
1104 if ( ! fish_exists (me
, path
) ){
1105 ERRNOR (EACCES
, -1);
1110 static int fish_rmdir (struct vfs_class
*me
, const char *path
)
1113 g_snprintf(buf
, sizeof(buf
),
1115 "if rmdir /%s 2>/dev/null; then\n"
1125 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
,
1130 fh
->u
.fish
.append
= 0;
1131 /* File will be written only, so no need to retrieve it */
1132 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
))) {
1133 fh
->u
.fish
.append
= flags
& O_APPEND
;
1134 if (!fh
->ino
->localname
) {
1136 vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1137 fh
->ino
->ent
->name
);
1138 if (tmp_handle
== -1)
1144 if (!fh
->ino
->localname
)
1145 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1147 if (!fh
->ino
->localname
)
1148 vfs_die ("retrieve_file failed to fill in localname");
1153 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1155 struct vfs_s_super
*super
= MEDATA
->supers
;
1162 const char *flags
= "";
1168 case FISH_FLAG_COMPRESSED
:
1172 if (SUP
.flags
> FISH_FLAG_RSH
)
1175 g_snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1181 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
,
1182 "/", SUP
.cwdir
, (char *) NULL
);
1185 super
= super
->next
;
1190 fish_open (struct vfs_class
*me
, const char *file
, int flags
, int mode
)
1193 sorry, i've places hack here
1194 cause fish don't able to open files with O_EXCL flag
1197 return vfs_s_open (me
, file
, flags
, mode
);
1204 static struct vfs_s_subclass fish_subclass
;
1206 fish_subclass
.flags
= VFS_S_REMOTE
;
1207 fish_subclass
.archive_same
= fish_archive_same
;
1208 fish_subclass
.open_archive
= fish_open_archive
;
1209 fish_subclass
.free_archive
= fish_free_archive
;
1210 fish_subclass
.fh_open
= fish_fh_open
;
1211 fish_subclass
.dir_load
= fish_dir_load
;
1212 fish_subclass
.file_store
= fish_file_store
;
1213 fish_subclass
.linear_start
= fish_linear_start
;
1214 fish_subclass
.linear_read
= fish_linear_read
;
1215 fish_subclass
.linear_close
= fish_linear_close
;
1217 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1218 vfs_fish_ops
.name
= "fish";
1219 vfs_fish_ops
.prefix
= "sh:";
1220 vfs_fish_ops
.fill_names
= fish_fill_names
;
1221 vfs_fish_ops
.chmod
= fish_chmod
;
1222 vfs_fish_ops
.chown
= fish_chown
;
1223 vfs_fish_ops
.open
= fish_open
;
1224 vfs_fish_ops
.symlink
= fish_symlink
;
1225 vfs_fish_ops
.link
= fish_link
;
1226 vfs_fish_ops
.unlink
= fish_unlink
;
1227 vfs_fish_ops
.rename
= fish_rename
;
1228 vfs_fish_ops
.mkdir
= fish_mkdir
;
1229 vfs_fish_ops
.rmdir
= fish_rmdir
;
1230 vfs_fish_ops
.ctl
= fish_ctl
;
1231 vfs_register_class (&vfs_fish_ops
);