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. */
27 * Read README.fish for protocol specification.
29 * Syntax of path is: /#sh:user@host[:Cr]/path
30 * where C means you want compressed connection,
31 * and r means you want to use rsh
33 * Namespace: fish_vfs_ops exported.
36 /* Define this if your ssh can take -I option */
42 #include <mhl/string.h>
44 #include "../src/global.h"
45 #include "../src/tty.h" /* enable/disable interrupt key */
46 #include "../src/wtools.h" /* message() */
47 #include "../src/main.h" /* print_vfs_message */
49 #include "xdirentry.h"
52 #include "gc.h" /* vfs_stamp_create */
54 #include "../src/unixcompat.h"
56 #include "../mhl/memory.h"
57 #include "../mhl/string.h"
58 #include "../mhl/escape.h"
60 int fish_directory_timeout
= 900;
62 #define DO_RESOLVE_SYMLINK 1
64 #define DO_FREE_RESOURCE 4
66 #define FISH_FLAG_COMPRESSED 1
67 #define FISH_FLAG_RSH 2
70 #define OPT_IGNORE_ERROR 2
75 #define PRELIM 1 /* positive preliminary */
76 #define COMPLETE 2 /* positive completion */
77 #define CONTINUE 3 /* positive intermediate */
78 #define TRANSIENT 4 /* transient negative completion */
79 #define ERROR 5 /* permanent negative completion */
81 /* command wait_flag: */
83 #define WAIT_REPLY 0x01
84 #define WANT_STRING 0x02
85 static char reply_str
[80];
87 static struct vfs_class vfs_fish_ops
;
90 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
,
91 int wait_reply
, const char *fmt
, ...)
92 __attribute__ ((format (__printf__
, 4, 5)));
94 static int fish_decode_reply (char *s
, int was_garbage
)
97 if (!sscanf(s
, "%d", &code
)) {
101 if (code
<100) return was_garbage
? ERROR
: (!code
? COMPLETE
: PRELIM
);
105 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
106 static int fish_get_reply (struct vfs_class
*me
, int sock
, char *string_buf
, int string_len
)
112 if (!vfs_s_get_line(me
, sock
, answer
, sizeof(answer
), '\n')) {
118 if (strncmp(answer
, "### ", 4)) {
121 g_strlcpy(string_buf
, answer
, string_len
);
122 } else return fish_decode_reply(answer
+4, was_garbage
);
126 #define SUP super->u.fish
129 fish_command (struct vfs_class
*me
, struct vfs_s_super
*super
,
130 int wait_reply
, const char *fmt
, ...)
135 FILE *logfile
= MEDATA
->logfile
;
139 str
= g_strdup_vprintf (fmt
, ap
);
143 fwrite (str
, strlen (str
), 1, logfile
);
147 enable_interrupt_key ();
149 status
= write (SUP
.sockw
, str
, strlen (str
));
152 disable_interrupt_key ();
157 return fish_get_reply (me
, SUP
.sockr
,
158 (wait_reply
& WANT_STRING
) ? reply_str
:
159 NULL
, sizeof (reply_str
) - 1);
164 fish_free_archive (struct vfs_class
*me
, struct vfs_s_super
*super
)
166 if ((SUP
.sockw
!= -1) || (SUP
.sockr
!= -1)) {
167 print_vfs_message (_("fish: Disconnecting from %s"),
168 super
->name
? super
->name
: "???");
169 fish_command (me
, super
, NONE
, "#BYE\nexit\n");
172 SUP
.sockw
= SUP
.sockr
= -1;
177 g_free (SUP
.password
);
181 fish_pipeopen(struct vfs_s_super
*super
, const char *path
, const char *argv
[])
183 int fileset1
[2], fileset2
[2];
186 if ((pipe(fileset1
)<0) || (pipe(fileset2
)<0))
187 vfs_die("Cannot pipe(): %m.");
189 if ((res
= fork())) {
190 if (res
<0) vfs_die("Cannot fork(): %m.");
191 /* We are the parent */
193 SUP
.sockw
= fileset1
[1];
195 SUP
.sockr
= fileset2
[0];
199 close(fileset1
[0]); close(fileset1
[1]);
202 /* stderr to /dev/null */
203 open ("/dev/null", O_WRONLY
);
204 close(fileset2
[0]); close(fileset2
[1]);
205 execvp(path
, const_cast(char **, argv
));
210 /* The returned directory should always contain a trailing slash */
211 static char *fish_getcwd(struct vfs_class
*me
, struct vfs_s_super
*super
)
213 if (fish_command (me
, super
, WANT_STRING
, "#PWD\npwd; echo '### 200'\n") == COMPLETE
)
214 return g_strconcat (reply_str
, "/", (char *) NULL
);
219 fish_open_archive_int (struct vfs_class
*me
, struct vfs_s_super
*super
)
222 const char *argv
[10];
223 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
227 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
230 argv
[i
++] = SUP
.user
;
231 argv
[i
++] = SUP
.host
;
232 argv
[i
++] = "echo FISH:; /bin/sh";
235 fish_pipeopen (super
, xsh
, argv
);
239 print_vfs_message (_("fish: Waiting for initial line..."));
240 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
241 ERRNOR (E_PROTO
, -1);
242 print_vfs_message ("%s", answer
);
243 if (strstr (answer
, "assword")) {
245 /* Currently, this does not work. ssh reads passwords from
246 /dev/tty, not from stdin :-(. */
248 message (D_ERROR
, MSG_ERROR
,
250 ("Sorry, we cannot do password authenticated connections for now."));
254 p
= g_strconcat (_(" fish: Password required for "),
255 SUP
.user
, " ", (char *) NULL
);
256 op
= vfs_get_password (p
);
262 print_vfs_message (_("fish: Sending password..."));
263 write (SUP
.sockw
, SUP
.password
, strlen (SUP
.password
));
264 write (SUP
.sockw
, "\n", 1);
268 print_vfs_message (_("fish: Sending initial line..."));
270 * Run `start_fish_server'. If it doesn't exist - no problem,
271 * we'll talk directly to the shell.
274 (me
, super
, WAIT_REPLY
,
275 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
277 ERRNOR (E_PROTO
, -1);
279 print_vfs_message (_("fish: Handshaking version..."));
281 (me
, super
, WAIT_REPLY
,
282 "#VER 0.0.0\necho '### 000'\n") != COMPLETE
)
283 ERRNOR (E_PROTO
, -1);
285 /* Set up remote locale to C, otherwise dates cannot be recognized */
287 (me
, super
, WAIT_REPLY
,
288 "LANG=C; LC_ALL=C; LC_TIME=C\n"
289 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
291 ERRNOR (E_PROTO
, -1);
293 print_vfs_message (_("fish: Setting up current directory..."));
294 SUP
.cwdir
= fish_getcwd (me
, super
);
295 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
298 g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
300 super
->name
= mhl_str_dup (PATH_SEP_STR
);
303 vfs_s_new_inode (me
, super
,
304 vfs_s_default_stat (me
, S_IFDIR
| 0755));
309 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
310 const char *archive_name
, char *op
)
312 char *host
, *user
, *password
, *p
;
317 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
318 &password
, 0, URL_NOSLASH
);
325 if (!strncmp (op
, "rsh:", 4))
326 SUP
.flags
|= FISH_FLAG_RSH
;
329 SUP
.password
= password
;
330 return fish_open_archive_int (me
, super
);
334 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
335 const char *archive_name
, char *op
, void *cookie
)
344 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
349 flags
= ((strcmp (host
, SUP
.host
) == 0)
350 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
358 fish_dir_load(struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
360 struct vfs_s_super
*super
= dir
->super
;
362 struct vfs_s_entry
*ent
= NULL
;
369 * Simple FISH debug interface :]
371 if (!(MEDATA
->logfile
))
373 MEDATA
->logfile
= fopen("/tmp/mc-FISH.sh", "w");
377 logfile
= MEDATA
->logfile
;
379 print_vfs_message(_("fish: Reading directory %s..."), remote_path
);
381 gettimeofday(&dir
->timestamp
, NULL
);
382 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
383 quoted_path
= mhl_shell_escape_dup (remote_path
);
384 fish_command (me
, super
, NONE
,
386 "if `perl -v > /dev/null 2>&1` ; then\n"
391 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
392 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
393 "my $dirname = $ARGV[0];\n"
394 "if (opendir ( DIR, $dirname )) {\n"
395 "while( (my $filename = readdir(DIR))){\n"
396 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
397 "my $mloctime= scalar localtime $mtime;\n"
398 "my $shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
399 "my $e_filename = $filename;\n"
400 "$e_filename =~ $shell_escape_regex;\n"
401 "if (S_ISLNK($mode) ) {\n"
402 "my $linkname = readlink (\"$dirname/$filename\");\n"
403 "$linkname =~ $shell_escape_regex;\n"
405 "printf(\"R%%o %%o $uid.$gid\\n"
408 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
409 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
411 "printf(\"R%%o %%o $uid.$gid\\n"
414 ":\\\"$e_filename\\\"\\n"
415 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
417 "printf(\"### 200\\n\");\n"
420 "printf(\"### 500\\n\");\n"
423 "' /%s ||\n" /* ARGV[0] - path to browse */
424 " echo '### 500'\n" /* do not hang if perl is to eval it */
425 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
426 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
433 "ls $LSOPT \"/%s\" 2>/dev/null | grep '^[^cbt]' | (\n"
434 "while read p l u g s m d y n; do\n"
435 "if [ $ADD = 0 ]; then\n"
436 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
437 "elif `sed --version >/dev/null 2>&1` ; then\n"
438 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
439 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
441 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
444 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
445 "while read p l u g a i m d y n; do\n"
446 "if [ $ADD = 0 ]; then\n"
447 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
448 "elif `sed --version >/dev/null 2>&1` ; then\n"
449 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
450 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
452 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
459 quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
, quoted_path
);
460 mhl_mem_free (quoted_path
);
461 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
463 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
464 if ((!res
) || (res
== EINTR
)) {
465 vfs_s_free_entry(me
, ent
);
466 me
->verrno
= ECONNRESET
;
470 fputs (buffer
, logfile
);
471 fputs ("\n", logfile
);
474 if (!strncmp(buffer
, "### ", 4))
478 vfs_s_insert_entry(me
, dir
, ent
);
479 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
484 #define ST ent->ino->st
488 char *data_start
= buffer
+1;
489 char *filename
= data_start
;
490 char *linkname
= data_start
;
491 char *filename_bound
= filename
+ strlen(filename
);
492 char *linkname_bound
= filename_bound
;
493 if (!strcmp(data_start
, "\".\"") || !strcmp(data_start
, "\"..\""))
494 break; /* We'll do "." and ".." ourselves */
496 if (S_ISLNK(ST
.st_mode
)) {
497 // we expect: "escaped-name" -> "escaped-name"
498 // -> cannot occur in filenames,
499 // because it will be escaped to -\>
501 if (*filename
== '"')
504 linkname
= strstr(filename
, "\" -> \"");
507 /* broken client, or smth goes wrong */
508 linkname
= filename_bound
;
509 if (filename_bound
> filename
510 && *(filename_bound
- 1) == '"')
511 --filename_bound
; // skip trailing "
515 filename_bound
= linkname
;
516 linkname
+= 6; // strlen ("\" -> \"")
517 if (*(linkname_bound
- 1) == '"')
518 --linkname_bound
; // skip trailing "
521 ent
->name
= mhl_str_dup_range(filename
, filename_bound
);
522 mhl_shell_unescape_buf(ent
->name
);
524 ent
->ino
->linkname
= mhl_str_dup_range(linkname
, linkname_bound
);
525 mhl_shell_unescape_buf(ent
->ino
->linkname
);
527 // we expect: "escaped-name"
528 if (filename_bound
- filename
> 2)
530 // there is at least 2 "
532 if (*filename
== '"')
534 if (*(filename_bound
- 1) == '"')
538 ent
->name
= mhl_str_dup_range(filename
, filename_bound
);
539 mhl_shell_unescape_buf(ent
->name
);
545 ST
.st_size
= (off_t
) atoll (buffer
+1);
547 ST
.st_size
= (off_t
) atof (buffer
+1);
552 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
557 // we expect: Roctal-filemode octal-filetype uid.gid
559 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
563 vfs_split_text(buffer
+1);
564 if (!vfs_parse_filedate(0, &ST
.st_ctime
))
566 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
571 if (sscanf(buffer
+1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
572 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
574 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime(&tim
);
579 if (sscanf(buffer
+1, "%d,%d", &maj
, &min
) != 2)
581 #ifdef HAVE_STRUCT_STAT_ST_RDEV
582 ST
.st_rdev
= makedev (maj
, min
);
588 vfs_s_free_entry (me
, ent
);
589 reply_code
= fish_decode_reply(buffer
+ 4, 0);
590 if (reply_code
== COMPLETE
) {
592 SUP
.cwdir
= mhl_str_dup (remote_path
);
593 print_vfs_message (_("%s: done."), me
->name
);
595 } else if (reply_code
== ERROR
) {
598 me
->verrno
= E_REMOTE
;
602 print_vfs_message (_("%s: failure"), me
->name
);
607 fish_file_store(struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
609 struct vfs_s_super
*super
= FH_SUPER
;
617 h
= open (localname
, O_RDONLY
);
621 if (fstat(h
, &s
)<0) {
626 /* First, try this as stor:
628 * ( head -c number ) | ( cat > file; cat >/dev/null )
630 * If `head' is not present on the remote system, `dd' will be used.
631 * Unfortunately, we cannot trust most non-GNU `head' implementations
632 * even if `-c' options is supported. Therefore, we separate GNU head
633 * (and other modern heads?) using `-q' and `-' . This causes another
634 * implementations to fail (because of "incorrect options").
639 * while [ $rest -gt 0 ]
641 * cnt=`expr \( $rest + 255 \) / 256`
642 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
643 * rest=`expr $rest - $n`
646 * `dd' was not designed for full filling of input buffers,
647 * and does not report exact number of bytes (not blocks).
648 * Therefore a more complex shell script is needed.
650 * On some systems non-GNU head writes "Usage:" error report to stdout
651 * instead of stderr. It makes impossible the use of "head || dd"
652 * algorithm for file appending case, therefore just "dd" is used for it.
655 quoted_name
= mhl_shell_escape_dup(name
);
656 print_vfs_message(_("fish: store %s: sending command..."), quoted_name
);
658 /* FIXME: File size is limited to ULONG_MAX */
659 if (!fh
->u
.fish
.append
)
660 n
= fish_command (me
, super
, WAIT_REPLY
,
666 "head -c %lu -q - || echo DD >&3\n"
667 ") 2>/dev/null | (\n"
670 ")`; [ \"$res\" = DD ] && {\n"
673 "while [ $rest -gt 0 ]\n"
675 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
676 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
677 " rest=`expr $rest - $n`\n"
679 "}; echo '### 200'\n",
680 (unsigned long) s
.st_size
, quoted_name
,
681 quoted_name
, (unsigned long) s
.st_size
,
682 (unsigned long) s
.st_size
);
684 n
= fish_command (me
, super
, WAIT_REPLY
,
690 "while [ $rest -gt 0 ]\n"
692 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
693 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
694 " rest=`expr $rest - $n`\n"
696 "}; echo '### 200'\n",
697 (unsigned long) s
.st_size
, quoted_name
,
698 quoted_name
, (unsigned long) s
.st_size
);
702 ERRNOR(E_REMOTE
, -1);
708 while ((n
= read(h
, buffer
, sizeof(buffer
))) < 0) {
709 if ((errno
== EINTR
) && got_interrupt())
711 print_vfs_message(_("fish: Local read failed, sending zeros") );
713 h
= open( "/dev/zero", O_RDONLY
);
717 if ((t
= write (SUP
.sockw
, buffer
, n
)) != n
) {
725 disable_interrupt_key();
727 print_vfs_message(_("fish: storing %s %d (%lu)"),
728 was_error
? _("zeros") : _("file"), total
,
729 (unsigned long) s
.st_size
);
732 mhl_mem_free(quoted_name
);
733 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
734 ERRNOR (E_REMOTE
, -1);
738 fish_get_reply(me
, SUP
.sockr
, NULL
, 0);
739 mhl_mem_free(quoted_name
);
744 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
749 ERRNOR (E_NOTSUPP
, 0);
750 name
= vfs_s_fullpath (me
, fh
->ino
);
753 quoted_name
= mhl_shell_escape_dup(name
);
754 fh
->u
.fish
.append
= 0;
757 * Check whether the remote file is readable by using `dd' to copy
758 * a single byte from the remote file to /dev/null. If `dd' completes
759 * with exit status of 0 use `cat' to send the file contents to the
760 * standard output (i.e. over the network).
762 offset
= fish_command (me
, FH_SUPER
, WANT_STRING
,
764 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
766 "ls -ln /%s 2>/dev/null | (\n"
776 quoted_name
, quoted_name
, quoted_name
, quoted_name
);
777 g_free (quoted_name
);
778 if (offset
!= PRELIM
) ERRNOR (E_REMOTE
, 0);
779 fh
->linear
= LS_LINEAR_OPEN
;
782 #if SIZEOF_OFF_T == SIZEOF_LONG
783 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
785 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
788 ERRNOR (E_REMOTE
, 0);
793 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
795 struct vfs_s_super
*super
= FH_SUPER
;
799 print_vfs_message( _("Aborting transfer...") );
801 n
= MIN(8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
803 if ((n
= read(SUP
.sockr
, buffer
, n
)) < 0)
809 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
810 print_vfs_message( _("Error reported after abort.") );
812 print_vfs_message( _("Aborted transfer would be successful.") );
816 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
818 struct vfs_s_super
*super
= FH_SUPER
;
820 len
= MIN( fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
821 disable_interrupt_key();
822 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
))<0)) {
823 if ((errno
== EINTR
) && !got_interrupt())
827 enable_interrupt_key();
829 if (n
>0) fh
->u
.fish
.got
+= n
;
830 if (n
<0) fish_linear_abort(me
, fh
);
831 if ((!n
) && ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)))
832 ERRNOR (E_REMOTE
, -1);
837 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
839 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
840 fish_linear_abort(me
, fh
);
844 fish_ctl (void *fh
, int ctlop
, void *arg
)
850 case VFS_CTL_IS_NOTREADY
:
855 vfs_die ("You may not do this");
856 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
859 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
860 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
871 fish_send_command(struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
875 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
876 vfs_stamp_create (&vfs_fish_ops
, super
);
877 if (r
!= COMPLETE
) ERRNOR (E_REMOTE
, -1);
878 if (flags
& OPT_FLUSH
)
879 vfs_s_invalidate(me
, super
);
884 char buf[BUF_LARGE]; \
885 const char *crpath; \
886 char *rpath, *mpath = mhl_str_dup (path); \
887 struct vfs_s_super *super; \
888 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
892 rpath = mhl_shell_escape_dup(crpath); \
895 #define POSTFIX(flags) \
897 return fish_send_command(me, super, buf, flags);
900 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
903 g_snprintf(buf
, sizeof(buf
), "#CHMOD %4.4o /%s\n"
904 "chmod %4.4o /%s 2>/dev/null\n"
907 mode
& 07777, rpath
);
911 #define FISH_OP(name, string) \
912 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
914 char buf[BUF_LARGE]; \
915 const char *crpath1, *crpath2; \
916 char *rpath1, *rpath2, *mpath1, *mpath2; \
917 struct vfs_s_super *super1, *super2; \
918 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = mhl_str_dup(path1), &super1, 0))) { \
922 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = mhl_str_dup(path2), &super2, 0))) { \
927 rpath1 = mhl_shell_escape_dup (crpath1); \
929 rpath2 = mhl_shell_escape_dup (crpath2); \
931 g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
932 mhl_mem_free (rpath1); \
933 mhl_mem_free (rpath2); \
934 return fish_send_command(me, super2, buf, OPT_FLUSH); \
937 FISH_OP(rename
, "#RENAME /%s /%s\n"
938 "mv /%s /%s 2>/dev/null\n"
940 FISH_OP(link
, "#LINK /%s /%s\n"
941 "ln /%s /%s 2>/dev/null\n"
944 static int fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
948 qsetto
= mhl_shell_escape_dup (setto
);
949 g_snprintf(buf
, sizeof(buf
),
951 "ln -s %s /%s 2>/dev/null\n"
953 qsetto
, rpath
, qsetto
, rpath
);
954 mhl_mem_free (qsetto
);
959 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
961 char *sowner
, *sgroup
;
965 if ((pw
= getpwuid (owner
)) == NULL
)
968 if ((gr
= getgrgid (group
)) == NULL
)
971 sowner
= pw
->pw_name
;
972 sgroup
= gr
->gr_name
;
975 g_snprintf (buf
, sizeof(buf
),
977 "chown %s /%s 2>/dev/null\n"
981 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
982 /* FIXME: what should we report if chgrp succeeds but chown fails? */
983 g_snprintf (buf
, sizeof(buf
),
984 "#CHGRP /%s \"/%s\"\n"
985 "chgrp %s \"/%s\" 2>/dev/null\n"
989 /* fish_send_command(me, super, buf, OPT_FLUSH); */
994 static int fish_unlink (struct vfs_class
*me
, const char *path
)
997 g_snprintf(buf
, sizeof(buf
),
999 "rm -f /%s 2>/dev/null\n"
1005 static int fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1011 g_snprintf(buf
, sizeof(buf
),
1013 "mkdir /%s 2>/dev/null\n"
1019 static int fish_rmdir (struct vfs_class
*me
, const char *path
)
1022 g_snprintf(buf
, sizeof(buf
),
1024 "rmdir /%s 2>/dev/null\n"
1031 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
,
1036 fh
->u
.fish
.append
= 0;
1037 /* File will be written only, so no need to retrieve it */
1038 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
))) {
1039 fh
->u
.fish
.append
= flags
& O_APPEND
;
1040 if (!fh
->ino
->localname
) {
1042 vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1043 fh
->ino
->ent
->name
);
1044 if (tmp_handle
== -1)
1050 if (!fh
->ino
->localname
)
1051 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1053 if (!fh
->ino
->localname
)
1054 vfs_die ("retrieve_file failed to fill in localname");
1059 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1061 struct vfs_s_super
*super
= MEDATA
->supers
;
1066 switch (SUP
.flags
& (FISH_FLAG_RSH
| FISH_FLAG_COMPRESSED
)) {
1070 case FISH_FLAG_COMPRESSED
:
1073 case FISH_FLAG_RSH
| FISH_FLAG_COMPRESSED
:
1081 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
,
1082 "/", SUP
.cwdir
, (char *) NULL
);
1085 super
= super
->next
;
1092 static struct vfs_s_subclass fish_subclass
;
1094 fish_subclass
.flags
= VFS_S_REMOTE
;
1095 fish_subclass
.archive_same
= fish_archive_same
;
1096 fish_subclass
.open_archive
= fish_open_archive
;
1097 fish_subclass
.free_archive
= fish_free_archive
;
1098 fish_subclass
.fh_open
= fish_fh_open
;
1099 fish_subclass
.dir_load
= fish_dir_load
;
1100 fish_subclass
.file_store
= fish_file_store
;
1101 fish_subclass
.linear_start
= fish_linear_start
;
1102 fish_subclass
.linear_read
= fish_linear_read
;
1103 fish_subclass
.linear_close
= fish_linear_close
;
1105 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1106 vfs_fish_ops
.name
= "fish";
1107 vfs_fish_ops
.prefix
= "sh:";
1108 vfs_fish_ops
.fill_names
= fish_fill_names
;
1109 vfs_fish_ops
.chmod
= fish_chmod
;
1110 vfs_fish_ops
.chown
= fish_chown
;
1111 vfs_fish_ops
.symlink
= fish_symlink
;
1112 vfs_fish_ops
.link
= fish_link
;
1113 vfs_fish_ops
.unlink
= fish_unlink
;
1114 vfs_fish_ops
.rename
= fish_rename
;
1115 vfs_fish_ops
.mkdir
= fish_mkdir
;
1116 vfs_fish_ops
.rmdir
= fish_rmdir
;
1117 vfs_fish_ops
.ctl
= fish_ctl
;
1118 vfs_register_class (&vfs_fish_ops
);