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;
174 mhl_mem_free (SUP
.host
);
175 mhl_mem_free (SUP
.user
);
176 mhl_mem_free (SUP
.cwdir
);
177 mhl_mem_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
)
223 const char *argv
[10]; /* All of 10 is used now */
224 const char *xsh
= (SUP
.flags
== FISH_FLAG_RSH
? "rsh" : "ssh");
228 if (SUP
.flags
== FISH_FLAG_COMPRESSED
)
231 if (SUP
.flags
> FISH_FLAG_RSH
)
234 snprintf (gbuf
, sizeof (gbuf
), "%d", SUP
.flags
);
239 argv
[i
++] = SUP
.user
;
240 argv
[i
++] = SUP
.host
;
241 argv
[i
++] = "echo FISH:; /bin/sh";
244 fish_pipeopen (super
, xsh
, argv
);
248 print_vfs_message (_("fish: Waiting for initial line..."));
249 if (!vfs_s_get_line (me
, SUP
.sockr
, answer
, sizeof (answer
), ':'))
250 ERRNOR (E_PROTO
, -1);
251 print_vfs_message ("%s", answer
);
252 if (strstr (answer
, "assword")) {
254 /* Currently, this does not work. ssh reads passwords from
255 /dev/tty, not from stdin :-(. */
257 message (D_ERROR
, MSG_ERROR
,
259 ("Sorry, we cannot do password authenticated connections for now."));
263 p
= g_strconcat (_(" fish: Password required for "),
264 SUP
.user
, " ", (char *) NULL
);
265 op
= vfs_get_password (p
);
271 print_vfs_message (_("fish: Sending password..."));
272 write (SUP
.sockw
, SUP
.password
, strlen (SUP
.password
));
273 write (SUP
.sockw
, "\n", 1);
277 print_vfs_message (_("fish: Sending initial line..."));
279 * Run `start_fish_server'. If it doesn't exist - no problem,
280 * we'll talk directly to the shell.
283 (me
, super
, WAIT_REPLY
,
284 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
286 ERRNOR (E_PROTO
, -1);
288 print_vfs_message (_("fish: Handshaking version..."));
290 (me
, super
, WAIT_REPLY
,
291 "#VER 0.0.0\necho '### 000'\n") != COMPLETE
)
292 ERRNOR (E_PROTO
, -1);
294 /* Set up remote locale to C, otherwise dates cannot be recognized */
296 (me
, super
, WAIT_REPLY
,
297 "LANG=C; LC_ALL=C; LC_TIME=C\n"
298 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
300 ERRNOR (E_PROTO
, -1);
302 print_vfs_message (_("fish: Setting up current directory..."));
303 SUP
.cwdir
= fish_getcwd (me
, super
);
304 print_vfs_message (_("fish: Connected, home %s."), SUP
.cwdir
);
307 g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, "/", (char *) NULL
);
309 super
->name
= mhl_str_dup (PATH_SEP_STR
);
312 vfs_s_new_inode (me
, super
,
313 vfs_s_default_stat (me
, S_IFDIR
| 0755));
318 fish_open_archive (struct vfs_class
*me
, struct vfs_s_super
*super
,
319 const char *archive_name
, char *op
)
321 char *host
, *user
, *password
, *p
;
326 p
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
,
327 &password
, 0, URL_NOSLASH
);
334 if (!strncmp (op
, "rsh:", 4))
335 SUP
.flags
= FISH_FLAG_RSH
;
338 SUP
.password
= password
;
339 return fish_open_archive_int (me
, super
);
343 fish_archive_same (struct vfs_class
*me
, struct vfs_s_super
*super
,
344 const char *archive_name
, char *op
, void *cookie
)
353 op
= vfs_split_url (strchr (op
, ':') + 1, &host
, &user
, &flags
, 0, 0,
358 flags
= ((strcmp (host
, SUP
.host
) == 0)
359 && (strcmp (user
, SUP
.user
) == 0) && (flags
== SUP
.flags
));
367 fish_dir_load(struct vfs_class
*me
, struct vfs_s_inode
*dir
, char *remote_path
)
369 struct vfs_s_super
*super
= dir
->super
;
371 struct vfs_s_entry
*ent
= NULL
;
373 SHELL_ESCAPED_STR quoted_path
;
378 * Simple FISH debug interface :]
380 if (!(MEDATA
->logfile
))
382 MEDATA
->logfile
= fopen("/tmp/mc-FISH.sh", "w");
386 logfile
= MEDATA
->logfile
;
388 print_vfs_message(_("fish: Reading directory %s..."), remote_path
);
390 gettimeofday(&dir
->timestamp
, NULL
);
391 dir
->timestamp
.tv_sec
+= fish_directory_timeout
;
392 quoted_path
= mhl_shell_escape_dup (remote_path
);
393 fish_command (me
, super
, NONE
,
395 "if `perl -v > /dev/null 2>&1` ; then\n"
400 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
401 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
402 "my $dirname = $ARGV[0];\n"
403 "if (opendir ( DIR, $dirname )) {\n"
404 "while( (my $filename = readdir(DIR))){\n"
405 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
406 "my $mloctime= scalar localtime $mtime;\n"
407 "my $shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
408 "my $e_filename = $filename;\n"
409 "$e_filename =~ $shell_escape_regex;\n"
410 "if (S_ISLNK($mode) ) {\n"
411 "my $linkname = readlink (\"$dirname/$filename\");\n"
412 "$linkname =~ $shell_escape_regex;\n"
414 "printf(\"R%%o %%o $uid.$gid\\n"
417 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
418 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
420 "printf(\"R%%o %%o $uid.$gid\\n"
423 ":\\\"$e_filename\\\"\\n"
424 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
426 "printf(\"### 200\\n\");\n"
429 "printf(\"### 500\\n\");\n"
432 "' /%s ||\n" /* ARGV[0] - path to browse */
433 " echo '### 500'\n" /* do not hang if perl is to eval it */
434 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
435 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
442 "ls $LSOPT \"/%s\" 2>/dev/null | grep '^[^cbt]' | (\n"
443 "while read p l u g s m d y n; do\n"
444 "if [ $ADD = 0 ]; then\n"
445 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
446 "elif `sed --version >/dev/null 2>&1` ; then\n"
447 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
448 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
450 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
453 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
454 "while read p l u g a i m d y n; do\n"
455 "if [ $ADD = 0 ]; then\n"
456 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
457 "elif `sed --version >/dev/null 2>&1` ; then\n"
458 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
459 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
461 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
468 quoted_path
.s
, quoted_path
.s
, quoted_path
.s
, quoted_path
.s
, quoted_path
.s
, quoted_path
.s
);
469 mhl_mem_free (quoted_path
.s
);
470 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
472 int res
= vfs_s_get_line_interruptible (me
, buffer
, sizeof (buffer
), SUP
.sockr
);
473 if ((!res
) || (res
== EINTR
)) {
474 vfs_s_free_entry(me
, ent
);
475 me
->verrno
= ECONNRESET
;
479 fputs (buffer
, logfile
);
480 fputs ("\n", logfile
);
483 if (!strncmp(buffer
, "### ", 4))
487 vfs_s_insert_entry(me
, dir
, ent
);
488 ent
= vfs_s_generate_entry(me
, NULL
, dir
, 0);
493 #define ST ent->ino->st
497 char *data_start
= buffer
+1;
498 char *filename
= data_start
;
499 char *linkname
= data_start
;
500 char *filename_bound
= filename
+ strlen(filename
);
501 char *linkname_bound
= filename_bound
;
502 if (!strcmp(data_start
, "\".\"") || !strcmp(data_start
, "\"..\""))
503 break; /* We'll do "." and ".." ourselves */
505 if (S_ISLNK(ST
.st_mode
)) {
506 // we expect: "escaped-name" -> "escaped-name"
507 // -> cannot occur in filenames,
508 // because it will be escaped to -\>
510 if (*filename
== '"')
513 linkname
= strstr(filename
, "\" -> \"");
516 /* broken client, or smth goes wrong */
517 linkname
= filename_bound
;
518 if (filename_bound
> filename
519 && *(filename_bound
- 1) == '"')
520 --filename_bound
; // skip trailing "
524 filename_bound
= linkname
;
525 linkname
+= 6; // strlen ("\" -> \"")
526 if (*(linkname_bound
- 1) == '"')
527 --linkname_bound
; // skip trailing "
530 ent
->name
= mhl_str_dup_range(filename
, filename_bound
);
531 mhl_shell_unescape_buf(ent
->name
);
533 ent
->ino
->linkname
= mhl_str_dup_range(linkname
, linkname_bound
);
534 mhl_shell_unescape_buf(ent
->ino
->linkname
);
536 // we expect: "escaped-name"
537 if (filename_bound
- filename
> 2)
539 // there is at least 2 "
541 if (*filename
== '"')
543 if (*(filename_bound
- 1) == '"')
547 ent
->name
= mhl_str_dup_range(filename
, filename_bound
);
548 mhl_shell_unescape_buf(ent
->name
);
554 ST
.st_size
= (off_t
) atoll (buffer
+1);
556 ST
.st_size
= (off_t
) atof (buffer
+1);
561 vfs_parse_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
566 // we expect: Roctal-filemode octal-filetype uid.gid
568 vfs_parse_raw_filemode (buffer
+ 1, &skipped
, &ST
.st_mode
);
572 vfs_split_text(buffer
+1);
573 if (!vfs_parse_filedate(0, &ST
.st_ctime
))
575 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
;
580 if (sscanf(buffer
+1, "%d %d %d %d %d %d", &tim
.tm_year
, &tim
.tm_mon
,
581 &tim
.tm_mday
, &tim
.tm_hour
, &tim
.tm_min
, &tim
.tm_sec
) != 6)
583 ST
.st_atime
= ST
.st_mtime
= ST
.st_ctime
= mktime(&tim
);
588 if (sscanf(buffer
+1, "%d,%d", &maj
, &min
) != 2)
590 #ifdef HAVE_STRUCT_STAT_ST_RDEV
591 ST
.st_rdev
= makedev (maj
, min
);
597 vfs_s_free_entry (me
, ent
);
598 reply_code
= fish_decode_reply(buffer
+ 4, 0);
599 if (reply_code
== COMPLETE
) {
600 mhl_mem_free (SUP
.cwdir
);
601 SUP
.cwdir
= mhl_str_dup (remote_path
);
602 print_vfs_message (_("%s: done."), me
->name
);
604 } else if (reply_code
== ERROR
) {
607 me
->verrno
= E_REMOTE
;
611 print_vfs_message (_("%s: failure"), me
->name
);
616 fish_file_store(struct vfs_class
*me
, struct vfs_s_fh
*fh
, char *name
, char *localname
)
618 struct vfs_s_super
*super
= FH_SUPER
;
624 SHELL_ESCAPED_STR quoted_name
;
626 h
= open (localname
, O_RDONLY
);
630 if (fstat(h
, &s
)<0) {
635 /* First, try this as stor:
637 * ( head -c number ) | ( cat > file; cat >/dev/null )
639 * If `head' is not present on the remote system, `dd' will be used.
640 * Unfortunately, we cannot trust most non-GNU `head' implementations
641 * even if `-c' options is supported. Therefore, we separate GNU head
642 * (and other modern heads?) using `-q' and `-' . This causes another
643 * implementations to fail (because of "incorrect options").
648 * while [ $rest -gt 0 ]
650 * cnt=`expr \( $rest + 255 \) / 256`
651 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
652 * rest=`expr $rest - $n`
655 * `dd' was not designed for full filling of input buffers,
656 * and does not report exact number of bytes (not blocks).
657 * Therefore a more complex shell script is needed.
659 * On some systems non-GNU head writes "Usage:" error report to stdout
660 * instead of stderr. It makes impossible the use of "head || dd"
661 * algorithm for file appending case, therefore just "dd" is used for it.
664 quoted_name
= mhl_shell_escape_dup(name
);
665 print_vfs_message(_("fish: store %s: sending command..."), quoted_name
.s
);
667 /* FIXME: File size is limited to ULONG_MAX */
668 if (!fh
->u
.fish
.append
)
669 n
= fish_command (me
, super
, WAIT_REPLY
,
675 "head -c %lu -q - || echo DD >&3\n"
676 ") 2>/dev/null | (\n"
679 ")`; [ \"$res\" = DD ] && {\n"
682 "while [ $rest -gt 0 ]\n"
684 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
685 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
686 " rest=`expr $rest - $n`\n"
688 "}; echo '### 200'\n",
689 (unsigned long) s
.st_size
, quoted_name
.s
,
690 quoted_name
.s
, (unsigned long) s
.st_size
,
691 (unsigned long) s
.st_size
);
693 n
= fish_command (me
, super
, WAIT_REPLY
,
699 "while [ $rest -gt 0 ]\n"
701 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
702 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
703 " rest=`expr $rest - $n`\n"
705 "}; echo '### 200'\n",
706 (unsigned long) s
.st_size
, quoted_name
.s
,
707 quoted_name
.s
, (unsigned long) s
.st_size
);
711 ERRNOR(E_REMOTE
, -1);
717 while ((n
= read(h
, buffer
, sizeof(buffer
))) < 0) {
718 if ((errno
== EINTR
) && got_interrupt())
720 print_vfs_message(_("fish: Local read failed, sending zeros") );
722 h
= open( "/dev/zero", O_RDONLY
);
726 if ((t
= write (SUP
.sockw
, buffer
, n
)) != n
) {
734 disable_interrupt_key();
736 print_vfs_message(_("fish: storing %s %d (%lu)"),
737 was_error
? _("zeros") : _("file"), total
,
738 (unsigned long) s
.st_size
);
741 mhl_mem_free(quoted_name
.s
);
742 if ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
) || was_error
)
743 ERRNOR (E_REMOTE
, -1);
747 fish_get_reply(me
, SUP
.sockr
, NULL
, 0);
748 mhl_mem_free(quoted_name
.s
);
753 fish_linear_start (struct vfs_class
*me
, struct vfs_s_fh
*fh
, off_t offset
)
756 SHELL_ESCAPED_STR quoted_name
;
758 ERRNOR (E_NOTSUPP
, 0);
759 name
= vfs_s_fullpath (me
, fh
->ino
);
762 quoted_name
= mhl_shell_escape_dup(name
);
763 fh
->u
.fish
.append
= 0;
766 * Check whether the remote file is readable by using `dd' to copy
767 * a single byte from the remote file to /dev/null. If `dd' completes
768 * with exit status of 0 use `cat' to send the file contents to the
769 * standard output (i.e. over the network).
771 offset
= fish_command (me
, FH_SUPER
, WANT_STRING
,
773 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
775 "ls -ln /%s 2>/dev/null | (\n"
785 quoted_name
.s
, quoted_name
.s
, quoted_name
.s
, quoted_name
.s
);
786 mhl_mem_free (quoted_name
.s
);
787 if (offset
!= PRELIM
) ERRNOR (E_REMOTE
, 0);
788 fh
->linear
= LS_LINEAR_OPEN
;
791 #if SIZEOF_OFF_T == SIZEOF_LONG
792 fh
->u
.fish
.total
= strtol (reply_str
, NULL
, 10);
794 fh
->u
.fish
.total
= strtoll (reply_str
, NULL
, 10);
797 ERRNOR (E_REMOTE
, 0);
802 fish_linear_abort (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
804 struct vfs_s_super
*super
= FH_SUPER
;
808 print_vfs_message( _("Aborting transfer...") );
810 n
= MIN(8192, fh
->u
.fish
.total
- fh
->u
.fish
.got
);
812 if ((n
= read(SUP
.sockr
, buffer
, n
)) < 0)
818 if (fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)
819 print_vfs_message( _("Error reported after abort.") );
821 print_vfs_message( _("Aborted transfer would be successful.") );
825 fish_linear_read (struct vfs_class
*me
, struct vfs_s_fh
*fh
, void *buf
, int len
)
827 struct vfs_s_super
*super
= FH_SUPER
;
829 len
= MIN( fh
->u
.fish
.total
- fh
->u
.fish
.got
, len
);
830 disable_interrupt_key();
831 while (len
&& ((n
= read (SUP
.sockr
, buf
, len
))<0)) {
832 if ((errno
== EINTR
) && !got_interrupt())
836 enable_interrupt_key();
838 if (n
>0) fh
->u
.fish
.got
+= n
;
839 if (n
<0) fish_linear_abort(me
, fh
);
840 if ((!n
) && ((fish_get_reply (me
, SUP
.sockr
, NULL
, 0) != COMPLETE
)))
841 ERRNOR (E_REMOTE
, -1);
846 fish_linear_close (struct vfs_class
*me
, struct vfs_s_fh
*fh
)
848 if (fh
->u
.fish
.total
!= fh
->u
.fish
.got
)
849 fish_linear_abort(me
, fh
);
853 fish_ctl (void *fh
, int ctlop
, void *arg
)
859 case VFS_CTL_IS_NOTREADY
:
864 vfs_die ("You may not do this");
865 if (FH
->linear
== LS_LINEAR_CLOSED
|| FH
->linear
== LS_LINEAR_PREOPEN
)
868 v
= vfs_s_select_on_two (FH_SUPER
->u
.fish
.sockr
, 0);
869 if (((v
< 0) && (errno
== EINTR
)) || v
== 0)
880 fish_send_command(struct vfs_class
*me
, struct vfs_s_super
*super
, const char *cmd
, int flags
)
884 r
= fish_command (me
, super
, WAIT_REPLY
, "%s", cmd
);
885 vfs_stamp_create (&vfs_fish_ops
, super
);
886 if (r
!= COMPLETE
) ERRNOR (E_REMOTE
, -1);
887 if (flags
& OPT_FLUSH
)
888 vfs_s_invalidate(me
, super
);
893 char buf[BUF_LARGE]; \
894 const char *crpath; \
895 char *mpath = mhl_str_dup (path); \
896 SHELL_ESCAPED_STR rpath; \
897 struct vfs_s_super *super; \
898 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
899 mhl_mem_free (mpath); \
902 rpath = mhl_shell_escape_dup(crpath); \
903 mhl_mem_free (mpath);
905 #define POSTFIX(flags) \
906 mhl_mem_free (rpath.s); \
907 return fish_send_command(me, super, buf, flags);
910 fish_chmod (struct vfs_class
*me
, const char *path
, int mode
)
913 snprintf(buf
, sizeof(buf
), "#CHMOD %4.4o /%s\n"
914 "chmod %4.4o /%s 2>/dev/null\n"
916 mode
& 07777, rpath
.s
,
917 mode
& 07777, rpath
.s
);
921 #define FISH_OP(name, string) \
922 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
924 char buf[BUF_LARGE]; \
925 const char *crpath1, *crpath2; \
926 char *mpath1, *mpath2; \
927 struct vfs_s_super *super1, *super2; \
928 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = mhl_str_dup(path1), &super1, 0))) { \
929 mhl_mem_free (mpath1); \
932 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = mhl_str_dup(path2), &super2, 0))) { \
933 mhl_mem_free (mpath1); \
934 mhl_mem_free (mpath2); \
936 SHELL_ESCAPED_STR rpath1 = mhl_shell_escape_dup (crpath1); \
937 mhl_mem_free (mpath1); \
938 SHELL_ESCAPED_STR rpath2 = mhl_shell_escape_dup (crpath2); \
939 mhl_mem_free (mpath2); \
940 snprintf(buf, sizeof(buf), string "\n", rpath1.s, rpath2.s, rpath1.s, rpath2.s); \
941 mhl_mem_free (rpath1.s); \
942 mhl_mem_free (rpath2.s); \
943 return fish_send_command(me, super2, buf, OPT_FLUSH); \
946 FISH_OP(rename
, "#RENAME /%s /%s\n"
947 "mv /%s /%s 2>/dev/null\n"
949 FISH_OP(link
, "#LINK /%s /%s\n"
950 "ln /%s /%s 2>/dev/null\n"
953 static int fish_symlink (struct vfs_class
*me
, const char *setto
, const char *path
)
955 SHELL_ESCAPED_STR qsetto
;
957 qsetto
= mhl_shell_escape_dup (setto
);
958 snprintf(buf
, sizeof(buf
),
960 "ln -s %s /%s 2>/dev/null\n"
962 qsetto
.s
, rpath
.s
, qsetto
.s
, rpath
.s
);
963 mhl_mem_free (qsetto
.s
);
968 fish_chown (struct vfs_class
*me
, const char *path
, int owner
, int group
)
970 char *sowner
, *sgroup
;
974 if ((pw
= getpwuid (owner
)) == NULL
)
977 if ((gr
= getgrgid (group
)) == NULL
)
980 sowner
= pw
->pw_name
;
981 sgroup
= gr
->gr_name
;
984 snprintf (buf
, sizeof(buf
),
986 "chown %s /%s 2>/dev/null\n"
990 fish_send_command (me
, super
, buf
, OPT_FLUSH
);
991 /* FIXME: what should we report if chgrp succeeds but chown fails? */
992 snprintf (buf
, sizeof(buf
),
993 "#CHGRP /%s \"/%s\"\n"
994 "chgrp %s \"/%s\" 2>/dev/null\n"
998 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1003 static int fish_unlink (struct vfs_class
*me
, const char *path
)
1006 snprintf(buf
, sizeof(buf
),
1008 "rm -f /%s 2>/dev/null\n"
1014 static int fish_mkdir (struct vfs_class
*me
, const char *path
, mode_t mode
)
1020 snprintf(buf
, sizeof(buf
),
1022 "mkdir /%s 2>/dev/null\n"
1028 static int fish_rmdir (struct vfs_class
*me
, const char *path
)
1031 snprintf(buf
, sizeof(buf
),
1033 "rmdir /%s 2>/dev/null\n"
1040 fish_fh_open (struct vfs_class
*me
, struct vfs_s_fh
*fh
, int flags
,
1045 fh
->u
.fish
.append
= 0;
1046 /* File will be written only, so no need to retrieve it */
1047 if (((flags
& O_WRONLY
) == O_WRONLY
) && !(flags
& (O_RDONLY
| O_RDWR
))) {
1048 fh
->u
.fish
.append
= flags
& O_APPEND
;
1049 if (!fh
->ino
->localname
) {
1051 vfs_mkstemps (&fh
->ino
->localname
, me
->name
,
1052 fh
->ino
->ent
->name
);
1053 if (tmp_handle
== -1)
1059 if (!fh
->ino
->localname
)
1060 if (vfs_s_retrieve_file (me
, fh
->ino
) == -1)
1062 if (!fh
->ino
->localname
)
1063 vfs_die ("retrieve_file failed to fill in localname");
1068 fish_fill_names (struct vfs_class
*me
, fill_names_f func
)
1070 struct vfs_s_super
*super
= MEDATA
->supers
;
1077 const char *flags
= "";
1083 case FISH_FLAG_COMPRESSED
:
1087 if (SUP
.flags
> FISH_FLAG_RSH
)
1090 snprintf (gbuf
, sizeof (gbuf
), ":%d", SUP
.flags
);
1096 name
= g_strconcat ("/#sh:", SUP
.user
, "@", SUP
.host
, flags
,
1097 "/", SUP
.cwdir
, (char *) NULL
);
1099 mhl_mem_free (name
);
1100 super
= super
->next
;
1107 static struct vfs_s_subclass fish_subclass
;
1109 fish_subclass
.flags
= VFS_S_REMOTE
;
1110 fish_subclass
.archive_same
= fish_archive_same
;
1111 fish_subclass
.open_archive
= fish_open_archive
;
1112 fish_subclass
.free_archive
= fish_free_archive
;
1113 fish_subclass
.fh_open
= fish_fh_open
;
1114 fish_subclass
.dir_load
= fish_dir_load
;
1115 fish_subclass
.file_store
= fish_file_store
;
1116 fish_subclass
.linear_start
= fish_linear_start
;
1117 fish_subclass
.linear_read
= fish_linear_read
;
1118 fish_subclass
.linear_close
= fish_linear_close
;
1120 vfs_s_init_class (&vfs_fish_ops
, &fish_subclass
);
1121 vfs_fish_ops
.name
= "fish";
1122 vfs_fish_ops
.prefix
= "sh:";
1123 vfs_fish_ops
.fill_names
= fish_fill_names
;
1124 vfs_fish_ops
.chmod
= fish_chmod
;
1125 vfs_fish_ops
.chown
= fish_chown
;
1126 vfs_fish_ops
.symlink
= fish_symlink
;
1127 vfs_fish_ops
.link
= fish_link
;
1128 vfs_fish_ops
.unlink
= fish_unlink
;
1129 vfs_fish_ops
.rename
= fish_rename
;
1130 vfs_fish_ops
.mkdir
= fish_mkdir
;
1131 vfs_fish_ops
.rmdir
= fish_rmdir
;
1132 vfs_fish_ops
.ctl
= fish_ctl
;
1133 vfs_register_class (&vfs_fish_ops
);