Ticket #1897: Build breaks on ignored return values
[midnight-commander.git] / lib / vfs / mc-vfs / fish.c
blob2a657f887958f87be11ae2b87de682da40c58e84
1 /* Virtual File System: FISH implementation for transfering files over
2 shell connections.
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
10 Derived from ftpfs.c.
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. */
26 /**
27 * \file
28 * \brief Source: Virtual File System: FISH implementation for transfering files over
29 * shell connections
30 * \author Pavel Machek
31 * \author Michal Svec
32 * \date 1998, 2000
34 * Derived from ftpfs.c
35 * Read README.fish for protocol specification.
37 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
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 */
46 #include <config.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <pwd.h>
50 #include <grp.h>
51 #include <sys/time.h> /* gettimeofday() */
52 #include <stdlib.h>
53 #include <string.h>
55 #include "lib/global.h"
56 #include "lib/fs.h"
57 #include "lib/tty/tty.h" /* enable/disable interrupt key */
58 #include "lib/strescape.h"
59 #include "lib/unixcompat.h"
61 #include "src/wtools.h" /* message() */
62 #include "src/main.h" /* print_vfs_message */
63 #include "utilvfs.h"
64 #include "xdirentry.h"
65 #include "vfs.h"
66 #include "vfs-impl.h"
67 #include "gc.h" /* vfs_stamp_create */
68 #include "netutil.h"
69 #include "fish.h"
71 int fish_directory_timeout = 900;
73 #define DO_RESOLVE_SYMLINK 1
74 #define DO_OPEN 2
75 #define DO_FREE_RESOURCE 4
77 #define FISH_FLAG_COMPRESSED 1
78 #define FISH_FLAG_RSH 2
80 #define OPT_FLUSH 1
81 #define OPT_IGNORE_ERROR 2
84 * Reply codes.
86 #define PRELIM 1 /* positive preliminary */
87 #define COMPLETE 2 /* positive completion */
88 #define CONTINUE 3 /* positive intermediate */
89 #define TRANSIENT 4 /* transient negative completion */
90 #define ERROR 5 /* permanent negative completion */
92 /* command wait_flag: */
93 #define NONE 0x00
94 #define WAIT_REPLY 0x01
95 #define WANT_STRING 0x02
96 static char reply_str[80];
98 static struct vfs_class vfs_fish_ops;
100 static int
101 fish_command (struct vfs_class *me, struct vfs_s_super *super,
102 int wait_reply, const char *fmt, ...) __attribute__ ((format (__printf__, 4, 5)));
104 static int
105 fish_decode_reply (char *s, int was_garbage)
107 int code;
108 if (!sscanf (s, "%d", &code))
110 code = 500;
111 return 5;
113 if (code < 100)
114 return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
115 return code / 100;
118 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
119 static int
120 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
122 char answer[1024];
123 int was_garbage = 0;
125 for (;;)
127 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
129 if (string_buf)
130 *string_buf = 0;
131 return 4;
134 if (strncmp (answer, "### ", 4))
136 was_garbage = 1;
137 if (string_buf)
138 g_strlcpy (string_buf, answer, string_len);
140 else
141 return fish_decode_reply (answer + 4, was_garbage);
145 #define SUP super->u.fish
147 static int
148 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
150 va_list ap;
151 char *str;
152 int status;
153 FILE *logfile = MEDATA->logfile;
155 va_start (ap, fmt);
157 str = g_strdup_vprintf (fmt, ap);
158 va_end (ap);
160 if (logfile)
162 size_t ret;
163 ret = fwrite (str, strlen (str), 1, logfile);
164 ret = fflush (logfile);
167 tty_enable_interrupt_key ();
169 status = write (SUP.sockw, str, strlen (str));
170 g_free (str);
172 tty_disable_interrupt_key ();
173 if (status < 0)
174 return TRANSIENT;
176 if (wait_reply)
177 return fish_get_reply (me, SUP.sockr,
178 (wait_reply & WANT_STRING) ? reply_str :
179 NULL, sizeof (reply_str) - 1);
180 return COMPLETE;
183 static void
184 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
186 if ((SUP.sockw != -1) || (SUP.sockr != -1))
188 print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
189 fish_command (me, super, NONE, "#BYE\nexit\n");
190 close (SUP.sockw);
191 close (SUP.sockr);
192 SUP.sockw = SUP.sockr = -1;
194 g_free (SUP.host);
195 g_free (SUP.user);
196 g_free (SUP.cwdir);
197 g_free (SUP.password);
200 static void
201 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
203 int fileset1[2], fileset2[2];
204 int res;
206 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
207 vfs_die ("Cannot pipe(): %m.");
209 if ((res = fork ()))
211 if (res < 0)
212 vfs_die ("Cannot fork(): %m.");
213 /* We are the parent */
214 close (fileset1[0]);
215 SUP.sockw = fileset1[1];
216 close (fileset2[1]);
217 SUP.sockr = fileset2[0];
219 else
221 res = dup2 (fileset1[0], 0);
222 close (fileset1[0]);
223 close (fileset1[1]);
224 res = dup2 (fileset2[1], 1);
225 close (2);
226 /* stderr to /dev/null */
227 res = open ("/dev/null", O_WRONLY);
228 close (fileset2[0]);
229 close (fileset2[1]);
230 execvp (path, const_cast (char **, argv));
231 _exit (3);
235 /* The returned directory should always contain a trailing slash */
236 static char *
237 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
239 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
240 return g_strconcat (reply_str, "/", (char *) NULL);
241 ERRNOR (EIO, NULL);
244 static int
245 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
248 char gbuf[10];
249 const char *argv[10]; /* All of 10 is used now */
250 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
251 int i = 0;
253 argv[i++] = xsh;
254 if (SUP.flags == FISH_FLAG_COMPRESSED)
255 argv[i++] = "-C";
257 if (SUP.flags > FISH_FLAG_RSH)
259 argv[i++] = "-p";
260 g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
261 argv[i++] = gbuf;
265 * Add the user name to the ssh command line only if it was explicitly
266 * set in vfs URL. rsh/ssh will get current user by default
267 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
268 * option breaks it for some)
271 if (SUP.user)
273 argv[i++] = "-l";
274 argv[i++] = SUP.user;
276 else
278 /* The rest of the code assumes it to be a valid username */
279 SUP.user = vfs_get_local_username ();
282 argv[i++] = SUP.host;
283 argv[i++] = "echo FISH:; /bin/sh";
284 argv[i++] = NULL;
286 fish_pipeopen (super, xsh, argv);
289 char answer[2048];
290 print_vfs_message (_("fish: Waiting for initial line..."));
291 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
292 ERRNOR (E_PROTO, -1);
293 print_vfs_message ("%s", answer);
294 if (strstr (answer, "assword"))
296 /* Currently, this does not work. ssh reads passwords from
297 /dev/tty, not from stdin :-(. */
299 message (D_ERROR, MSG_ERROR,
300 _("Sorry, we cannot do password authenticated connections for now."));
301 ERRNOR (EPERM, -1);
302 if (!SUP.password)
304 char *p, *op;
305 p = g_strconcat (_(" fish: Password required for "), SUP.user, " ", (char *) NULL);
306 op = vfs_get_password (p);
307 g_free (p);
308 if (op == NULL)
309 ERRNOR (EPERM, -1);
310 SUP.password = op;
312 print_vfs_message (_("fish: Sending password..."));
315 size_t str_len;
316 str_len = strlen (SUP.password);
317 if ((write (SUP.sockw, SUP.password, str_len ) != (ssize_t) str_len)
318 || (write (SUP.sockw, "\n", 1) != 1))
320 ERRNOR(EIO, -1);
326 print_vfs_message (_("fish: Sending initial line..."));
328 * Run `start_fish_server'. If it doesn't exist - no problem,
329 * we'll talk directly to the shell.
331 if (fish_command
332 (me, super, WAIT_REPLY,
333 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
334 ERRNOR (E_PROTO, -1);
336 print_vfs_message (_("fish: Handshaking version..."));
337 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
338 ERRNOR (E_PROTO, -1);
340 /* Set up remote locale to C, otherwise dates cannot be recognized */
341 if (fish_command
342 (me, super, WAIT_REPLY,
343 "LANG=C; LC_ALL=C; LC_TIME=C\n"
344 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE)
345 ERRNOR (E_PROTO, -1);
347 print_vfs_message (_("fish: Setting up current directory..."));
348 SUP.cwdir = fish_getcwd (me, super);
349 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
350 #if 0
351 super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
352 #endif
353 super->name = g_strdup (PATH_SEP_STR);
355 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
356 return 0;
359 static int
360 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
361 const char *archive_name, char *op)
363 char *host, *user, *password, *p;
364 int flags;
366 (void) archive_name;
368 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
369 &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
371 g_free (p);
373 SUP.host = host;
374 SUP.user = user;
375 SUP.flags = flags;
376 if (!strncmp (op, "rsh:", 4))
377 SUP.flags = FISH_FLAG_RSH;
378 SUP.cwdir = NULL;
379 if (password)
380 SUP.password = password;
381 return fish_open_archive_int (me, super);
384 static int
385 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
386 const char *archive_name, char *op, void *cookie)
388 char *host, *user;
389 int flags;
390 int result;
392 (void) me;
393 (void) archive_name;
394 (void) cookie;
396 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
397 URL_NOSLASH | URL_USE_ANONYMOUS);
399 g_free (op);
401 if (user == NULL)
402 user = vfs_get_local_username ();
404 result = ((strcmp (host, SUP.host) == 0)
405 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
407 g_free (host);
408 g_free (user);
410 return result;
413 static int
414 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
416 struct vfs_s_super *super = dir->super;
417 char buffer[8192];
418 struct vfs_s_entry *ent = NULL;
419 FILE *logfile;
420 char *quoted_path;
421 int reply_code;
422 gchar *shell_commands;
424 #if 0
426 * Simple FISH debug interface :]
428 if (!(MEDATA->logfile))
430 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
432 #endif /* 0 */
434 logfile = MEDATA->logfile;
436 print_vfs_message (_("fish: Reading directory %s..."), remote_path);
438 gettimeofday (&dir->timestamp, NULL);
439 dir->timestamp.tv_sec += fish_directory_timeout;
440 quoted_path = strutils_shell_escape (remote_path);
441 /* *INDENT-OFF* */
442 shell_commands = g_strconcat (
443 "#LIST /%s\n"
444 "if `perl -v > /dev/null 2>&1` ; then\n"
445 "perl -e '\n"
446 "use strict;\n"
447 "use POSIX;\n"
448 "use Fcntl;\n"
449 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
450 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
451 "my $dirname = $ARGV[0];\n"
452 "if (opendir ( DIR, $dirname )) {\n"
453 "while( (my $filename = readdir(DIR))){\n"
454 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
455 "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
457 "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
458 "my $e_filename = $filename;\n"
459 "$e_filename =~ $strutils_shell_escape_regex;\n"
460 "if (S_ISLNK($mode) ) {\n"
461 "my $linkname = readlink (\"$dirname/$filename\");\n"
462 "$linkname =~ $strutils_shell_escape_regex;\n"
463 "\n"
464 "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
465 "d$mloctime\\n"
466 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
467 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
468 "} else {\n"
469 "printf(\"R%%o %%o $uid.$gid\\n"
470 "S$size\\n"
471 "d$mloctime\\n"
472 ":\\\"$e_filename\\\"\\n"
473 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
474 "}}\n"
476 "printf(\"### 200\\n\");\n"
477 "closedir(DIR);\n"
478 "} else {\n"
479 "printf(\"### 500\\n\");\n"
480 "}\n"
481 "exit 0\n"
482 "' /%s ||\n" /* ARGV[0] - path to browse */
483 " echo '### 500'\n" /* do not hang if perl is to eval it */
484 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
485 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
486 "LSOPT=\"-Qlan\";\n"
487 "ADD=0;\n"
488 "else\n"
489 "LSOPT=\"-lan\";\n"
490 "ADD=1;\n"
491 "fi\n"
492 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
493 "while read p l u g s m d y n n2 n3; do\n"
494 "if test \"$m\" = \"0\" ; then \n"
495 "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
496 "else\n"
497 "n=$n\" \"$n2\" \"$n3\n"
498 "fi\n"
499 "if [ $ADD = 0 ]; then\n"
500 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
501 "elif `sed --version >/dev/null 2>&1` ; then\n"
502 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
503 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
504 "else\n"
505 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
506 "fi\n"
507 "done )\n"
508 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
509 "while read p l u g a i m d y n n2 n3; do\n"
510 "if test \"$a\" = \"0\" ; then \n"
511 "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
512 "else\n"
513 "n=$n\" \"$n2\" \"$n3\n"
514 "fi\n"
515 "if [ $ADD = 0 ]; then\n"
516 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
517 "elif `sed --version >/dev/null 2>&1` ; then\n"
518 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
519 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
520 "else\n"
521 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
522 "fi\n"
523 "done)\n"
524 "echo '### 200'\n"
525 "else\n"
526 "echo '### 500'\n"
527 "fi\n"
529 (char *) NULL);
530 /* *INDENT-ON* */
532 fish_command (me, super, NONE, shell_commands,
533 quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
535 g_free (shell_commands);
536 g_free (quoted_path);
537 ent = vfs_s_generate_entry (me, NULL, dir, 0);
538 while (1)
540 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
541 if ((!res) || (res == EINTR))
543 vfs_s_free_entry (me, ent);
544 me->verrno = ECONNRESET;
545 goto error;
547 if (logfile)
549 fputs (buffer, logfile);
550 fputs ("\n", logfile);
551 fflush (logfile);
553 if (!strncmp (buffer, "### ", 4))
554 break;
555 if ((!buffer[0]))
557 if (ent->name)
559 vfs_s_insert_entry (me, dir, ent);
560 ent = vfs_s_generate_entry (me, NULL, dir, 0);
562 continue;
565 #define ST ent->ino->st
567 switch (buffer[0])
569 case ':':
571 char *temp;
572 char *data_start = buffer + 1;
573 char *filename = data_start;
574 char *linkname = data_start;
575 char *filename_bound = filename + strlen (filename);
576 char *linkname_bound = filename_bound;
577 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
578 break; /* We'll do "." and ".." ourselves */
580 if (S_ISLNK (ST.st_mode))
582 /* we expect: "escaped-name" -> "escaped-name"
583 // -> cannot occur in filenames,
584 // because it will be escaped to -\> */
586 if (*filename == '"')
587 ++filename;
589 linkname = strstr (filename, "\" -> \"");
590 if (!linkname)
592 /* broken client, or smth goes wrong */
593 linkname = filename_bound;
594 if (filename_bound > filename && *(filename_bound - 1) == '"')
595 --filename_bound; /* skip trailing " */
597 else
599 filename_bound = linkname;
600 linkname += 6; /* strlen ("\" -> \"") */
601 if (*(linkname_bound - 1) == '"')
602 --linkname_bound; /* skip trailing " */
605 ent->name = str_dup_range (filename, filename_bound);
606 temp = ent->name;
607 ent->name = strutils_shell_unescape (ent->name);
608 g_free (temp);
610 ent->ino->linkname = str_dup_range (linkname, linkname_bound);
611 temp = ent->ino->linkname;
612 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
613 g_free (temp);
615 else
617 /* we expect: "escaped-name" */
618 if (filename_bound - filename > 2)
621 there is at least 2 "
622 and we skip them
624 if (*filename == '"')
625 ++filename;
626 if (*(filename_bound - 1) == '"')
627 --filename_bound;
629 ent->name = str_dup_range (filename, filename_bound);
630 temp = ent->name;
631 ent->name = strutils_shell_unescape (ent->name);
632 g_free (temp);
634 break;
636 case 'S':
637 #ifdef HAVE_ATOLL
638 ST.st_size = (off_t) atoll (buffer + 1);
639 #else
640 ST.st_size = (off_t) atof (buffer + 1);
641 #endif
642 break;
643 case 'P':
645 size_t skipped;
646 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
647 break;
649 case 'R':
652 raw filemode:
653 we expect: Roctal-filemode octal-filetype uid.gid
655 size_t skipped;
656 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
657 break;
659 case 'd':
661 vfs_split_text (buffer + 1);
662 if (!vfs_parse_filedate (0, &ST.st_ctime))
663 break;
664 ST.st_atime = ST.st_mtime = ST.st_ctime;
666 break;
667 case 'D':
669 struct tm tim;
670 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
671 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
672 break;
673 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
675 break;
676 case 'E':
678 int maj, min;
679 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
680 break;
681 #ifdef HAVE_STRUCT_STAT_ST_RDEV
682 ST.st_rdev = makedev (maj, min);
683 #endif
688 vfs_s_free_entry (me, ent);
689 reply_code = fish_decode_reply (buffer + 4, 0);
690 if (reply_code == COMPLETE)
692 g_free (SUP.cwdir);
693 SUP.cwdir = g_strdup (remote_path);
694 print_vfs_message (_("%s: done."), me->name);
695 return 0;
697 else if (reply_code == ERROR)
699 me->verrno = EACCES;
701 else
703 me->verrno = E_REMOTE;
706 error:
707 print_vfs_message (_("%s: failure"), me->name);
708 return -1;
711 static int
712 fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
714 struct vfs_s_super *super = FH_SUPER;
715 int n, total;
716 char buffer[8192];
717 struct stat s;
718 int was_error = 0;
719 int h;
720 char *quoted_name;
722 h = open (localname, O_RDONLY);
724 if (h == -1)
725 ERRNOR (EIO, -1);
726 if (fstat (h, &s) < 0)
728 close (h);
729 ERRNOR (EIO, -1);
732 /* First, try this as stor:
734 * ( head -c number ) | ( cat > file; cat >/dev/null )
736 * If `head' is not present on the remote system, `dd' will be used.
737 * Unfortunately, we cannot trust most non-GNU `head' implementations
738 * even if `-c' options is supported. Therefore, we separate GNU head
739 * (and other modern heads?) using `-q' and `-' . This causes another
740 * implementations to fail (because of "incorrect options").
742 * Fallback is:
744 * rest=<number>
745 * while [ $rest -gt 0 ]
746 * do
747 * cnt=`expr \( $rest + 255 \) / 256`
748 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
749 * rest=`expr $rest - $n`
750 * done
752 * `dd' was not designed for full filling of input buffers,
753 * and does not report exact number of bytes (not blocks).
754 * Therefore a more complex shell script is needed.
756 * On some systems non-GNU head writes "Usage:" error report to stdout
757 * instead of stderr. It makes impossible the use of "head || dd"
758 * algorithm for file appending case, therefore just "dd" is used for it.
761 quoted_name = strutils_shell_escape (name);
762 print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
764 /* FIXME: File size is limited to ULONG_MAX */
765 if (!fh->u.fish.append)
766 /* *INDENT-OFF* */
767 n = fish_command (me, super, WAIT_REPLY,
768 "#STOR %lu /%s\n"
769 "echo '### 001'\n"
770 "file=/%s\n"
771 "res=`exec 3>&1\n"
772 "(\n"
773 "head -c %lu -q - || echo DD >&3\n"
774 ") 2>/dev/null | (\n"
775 "cat > $file\n"
776 "cat > /dev/null\n"
777 ")`; [ \"$res\" = DD ] && {\n"
778 "> \"$file\"\n"
779 "rest=%lu\n"
780 "while [ $rest -gt 0 ]\n"
781 "do\n"
782 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
783 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
784 " rest=`expr $rest - $n`\n"
785 "done\n"
786 "}; echo '### 200'\n",
787 (unsigned long) s.st_size, quoted_name,
788 quoted_name, (unsigned long) s.st_size, (unsigned long) s.st_size);
789 /* *INDENT-ON* */
790 else
791 /* *INDENT-OFF* */
792 n = fish_command (me, super, WAIT_REPLY,
793 "#STOR %lu /%s\n"
794 "echo '### 001'\n"
795 "{\n"
796 "file=/%s\n"
797 "rest=%lu\n"
798 "while [ $rest -gt 0 ]\n"
799 "do\n"
800 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
801 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
802 " rest=`expr $rest - $n`\n"
803 "done\n"
804 "}; echo '### 200'\n",
805 (unsigned long) s.st_size, quoted_name,
806 quoted_name, (unsigned long) s.st_size);
807 /* *INDENT-ON* */
809 if (n != PRELIM)
811 close (h);
812 ERRNOR (E_REMOTE, -1);
815 total = 0;
817 while (1)
819 int t;
820 while ((n = read (h, buffer, sizeof (buffer))) < 0)
822 if ((errno == EINTR) && tty_got_interrupt ())
823 continue;
824 print_vfs_message (_("fish: Local read failed, sending zeros"));
825 close (h);
826 h = open ("/dev/zero", O_RDONLY);
828 if (n == 0)
829 break;
830 if ((t = write (SUP.sockw, buffer, n)) != n)
832 if (t == -1)
834 me->verrno = errno;
836 else
838 me->verrno = EIO;
840 goto error_return;
842 tty_disable_interrupt_key ();
843 total += n;
844 print_vfs_message (_("fish: storing %s %d (%lu)"),
845 was_error ? _("zeros") : _("file"), total, (unsigned long) s.st_size);
847 close (h);
848 g_free (quoted_name);
849 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
850 ERRNOR (E_REMOTE, -1);
851 return 0;
852 error_return:
853 close (h);
854 fish_get_reply (me, SUP.sockr, NULL, 0);
855 g_free (quoted_name);
856 return -1;
859 static int
860 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
862 char *name;
863 char *quoted_name;
864 if (offset)
865 ERRNOR (E_NOTSUPP, 0);
866 name = vfs_s_fullpath (me, fh->ino);
867 if (name == NULL)
868 return 0;
869 quoted_name = strutils_shell_escape (name);
870 g_free (name);
871 fh->u.fish.append = 0;
874 * Check whether the remote file is readable by using `dd' to copy
875 * a single byte from the remote file to /dev/null. If `dd' completes
876 * with exit status of 0 use `cat' to send the file contents to the
877 * standard output (i.e. over the network).
879 /* *INDENT-OFF* */
880 offset = fish_command (me, FH_SUPER, WANT_STRING,
881 "#RETR /%s\n"
882 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
883 "then\n"
884 "ls -ln /%s 2>/dev/null | (\n"
885 "read p l u g s r\n"
886 "echo $s\n"
887 ")\n"
888 "echo '### 100'\n"
889 "cat /%s\n"
890 "echo '### 200'\n"
891 "else\n"
892 "echo '### 500'\n"
893 "fi\n",
894 quoted_name, quoted_name, quoted_name, quoted_name);
895 /* *INDENT-ON* */
896 g_free (quoted_name);
897 if (offset != PRELIM)
898 ERRNOR (E_REMOTE, 0);
899 fh->linear = LS_LINEAR_OPEN;
900 fh->u.fish.got = 0;
901 errno = 0;
902 #if SIZEOF_OFF_T == SIZEOF_LONG
903 fh->u.fish.total = strtol (reply_str, NULL, 10);
904 #else
905 fh->u.fish.total = strtoll (reply_str, NULL, 10);
906 #endif
907 if (errno != 0)
908 ERRNOR (E_REMOTE, 0);
909 return 1;
912 static void
913 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
915 struct vfs_s_super *super = FH_SUPER;
916 char buffer[8192];
917 int n;
919 print_vfs_message (_("Aborting transfer..."));
922 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
923 if (n)
925 if ((n = read (SUP.sockr, buffer, n)) < 0)
926 return;
927 fh->u.fish.got += n;
930 while (n);
932 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
933 print_vfs_message (_("Error reported after abort."));
934 else
935 print_vfs_message (_("Aborted transfer would be successful."));
938 static int
939 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
941 struct vfs_s_super *super = FH_SUPER;
942 int n = 0;
943 len = MIN (fh->u.fish.total - fh->u.fish.got, len);
944 tty_disable_interrupt_key ();
945 while (len && ((n = read (SUP.sockr, buf, len)) < 0))
947 if ((errno == EINTR) && !tty_got_interrupt ())
948 continue;
949 break;
951 tty_enable_interrupt_key ();
953 if (n > 0)
954 fh->u.fish.got += n;
955 if (n < 0)
956 fish_linear_abort (me, fh);
957 if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
958 ERRNOR (E_REMOTE, -1);
959 ERRNOR (errno, n);
962 static void
963 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
965 if (fh->u.fish.total != fh->u.fish.got)
966 fish_linear_abort (me, fh);
969 static int
970 fish_ctl (void *fh, int ctlop, void *arg)
972 (void) arg;
973 (void) fh;
974 (void) ctlop;
975 return 0;
976 #if 0
977 switch (ctlop)
979 case VFS_CTL_IS_NOTREADY:
981 int v;
983 if (!FH->linear)
984 vfs_die ("You may not do this");
985 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
986 return 0;
988 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
989 if (((v < 0) && (errno == EINTR)) || v == 0)
990 return 1;
991 return 0;
993 default:
994 return 0;
996 #endif
999 static int
1000 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1002 int r;
1004 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1005 vfs_stamp_create (&vfs_fish_ops, super);
1006 if (r != COMPLETE)
1007 ERRNOR (E_REMOTE, -1);
1008 if (flags & OPT_FLUSH)
1009 vfs_s_invalidate (me, super);
1010 return 0;
1013 #define PREFIX \
1014 char buf[BUF_LARGE]; \
1015 const char *crpath; \
1016 char *rpath, *mpath = g_strdup (path); \
1017 struct vfs_s_super *super; \
1018 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) \
1020 g_free (mpath); \
1021 return -1; \
1023 rpath = strutils_shell_escape(crpath); \
1024 g_free (mpath);
1026 #define POSTFIX(flags) \
1027 g_free (rpath); \
1028 return fish_send_command(me, super, buf, flags);
1030 static int
1031 fish_chmod (struct vfs_class *me, const char *path, int mode)
1033 PREFIX
1034 /* *INDENT-OFF* */
1035 g_snprintf (buf, sizeof (buf),
1036 "#CHMOD %4.4o /%s\n"
1037 "if chmod %4.4o /%s 2>/dev/null; then\n"
1038 "echo '### 000'\n"
1039 "else\n"
1040 "echo '### 500'\n"
1041 "fi\n",
1042 mode & 07777, rpath, mode & 07777, rpath);
1043 /* *INDENT-ON* */
1044 POSTFIX (OPT_FLUSH);
1047 #define FISH_OP(name, string) \
1048 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1050 char buf[BUF_LARGE]; \
1051 const char *crpath1, *crpath2; \
1052 char *rpath1, *rpath2, *mpath1, *mpath2; \
1053 struct vfs_s_super *super1, *super2; \
1054 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) \
1056 g_free (mpath1); \
1057 return -1; \
1059 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) \
1061 g_free (mpath1); \
1062 g_free (mpath2); \
1063 return -1; \
1065 rpath1 = strutils_shell_escape (crpath1); \
1066 g_free (mpath1); \
1067 rpath2 = strutils_shell_escape (crpath2); \
1068 g_free (mpath2); \
1069 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1070 g_free (rpath1); \
1071 g_free (rpath2); \
1072 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1075 /* *INDENT-OFF* */
1076 FISH_OP (rename,
1077 "#RENAME /%s /%s\n"
1078 "if mv /%s /%s 2>/dev/null; then\n"
1079 "echo '### 000'\n"
1080 "else\n"
1081 "echo '### 500'\n"
1082 "fi\n")
1084 FISH_OP (link,
1085 "#LINK /%s /%s\n"
1086 "if ln /%s /%s 2>/dev/null; then\n"
1087 "echo '### 000'\n"
1088 "else\n"
1089 "echo '### 500'\n"
1090 "fi\n")
1091 /* *INDENT-ON* */
1093 static int
1094 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1096 char *qsetto;
1097 PREFIX qsetto = strutils_shell_escape (setto);
1098 /* *INDENT-OFF* */
1099 g_snprintf (buf, sizeof (buf),
1100 "#SYMLINK %s /%s\n"
1101 "if ln -s %s /%s 2>/dev/null; then\n"
1102 "echo '### 000'\n"
1103 "else\n"
1104 "echo '### 500'\n"
1105 "fi\n",
1106 qsetto, rpath, qsetto, rpath);
1107 /* *INDENT-ON* */
1108 g_free (qsetto);
1109 POSTFIX (OPT_FLUSH);
1112 static int
1113 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
1115 char *sowner, *sgroup;
1116 struct passwd *pw;
1117 struct group *gr;
1119 if ((pw = getpwuid (owner)) == NULL)
1120 return 0;
1122 if ((gr = getgrgid (group)) == NULL)
1123 return 0;
1125 sowner = pw->pw_name;
1126 sgroup = gr->gr_name;
1128 PREFIX
1129 /* *INDENT-OFF* */
1130 g_snprintf (buf, sizeof (buf),
1131 "#CHOWN %s:%s /%s\n"
1132 "if chown %s:%s /%s 2>/dev/null; then\n"
1133 "echo '### 000'\n"
1134 "else\n"
1135 "echo '### 500'\n"
1136 "fi\n",
1137 sowner, sgroup, rpath, sowner, sgroup, rpath);
1138 /* *INDENT-ON* */
1139 fish_send_command (me, super, buf, OPT_FLUSH);
1140 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1141 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1142 POSTFIX (OPT_FLUSH)}
1145 static int
1146 fish_unlink (struct vfs_class *me, const char *path)
1148 PREFIX
1150 /* *INDENT-OFF* */
1151 g_snprintf (buf, sizeof (buf),
1152 "#DELE /%s\n"
1153 "if rm -f /%s 2>/dev/null; then\n"
1154 "echo '### 000'\n"
1155 "else\n"
1156 "echo '### 500'\n"
1157 "fi\n",
1158 rpath, rpath);
1159 /* *INDENT-ON* */
1161 POSTFIX (OPT_FLUSH);
1164 static int
1165 fish_exists (struct vfs_class *me, const char *path)
1167 PREFIX
1169 /* *INDENT-OFF* */
1170 g_snprintf (buf, sizeof (buf),
1171 "#ISEXISTS /%s\n"
1172 "ls -l /%s >/dev/null 2>/dev/null\n"
1173 "echo '### '$?\n",
1174 rpath, rpath);
1175 /* *INDENT-ON* */
1177 g_free (rpath);
1179 if (fish_send_command (me, super, buf, OPT_FLUSH) == 0)
1180 return 1;
1182 return 0;
1186 static int
1187 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1189 int ret_code;
1191 PREFIX (void) mode;
1193 /* *INDENT-OFF* */
1194 g_snprintf (buf, sizeof (buf),
1195 "#MKD /%s\n"
1196 "if mkdir /%s 2>/dev/null; then\n"
1197 "echo '### 000'\n"
1198 "else\n"
1199 "echo '### 500'\n"
1200 "fi\n",
1201 rpath, rpath);
1202 /* *INDENT-ON* */
1204 g_free (rpath);
1205 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1207 if (ret_code != 0)
1208 return ret_code;
1210 if (!fish_exists (me, path))
1212 ERRNOR (EACCES, -1);
1214 return 0;
1217 static int
1218 fish_rmdir (struct vfs_class *me, const char *path)
1220 PREFIX
1221 /* *INDENT-OFF* */
1222 g_snprintf (buf, sizeof (buf),
1223 "#RMD /%s\n"
1224 "if rmdir /%s 2>/dev/null; then\n"
1225 "echo '### 000'\n"
1226 "else\n"
1227 "echo '### 500'\n"
1228 "fi\n",
1229 rpath, rpath);
1230 /* *INDENT-ON* */
1231 POSTFIX (OPT_FLUSH);
1234 static int
1235 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
1237 (void) mode;
1239 fh->u.fish.append = 0;
1240 /* File will be written only, so no need to retrieve it */
1241 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1243 fh->u.fish.append = flags & O_APPEND;
1244 if (!fh->ino->localname)
1246 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1247 fh->ino->ent->name);
1248 if (tmp_handle == -1)
1249 return -1;
1250 close (tmp_handle);
1252 return 0;
1254 if (!fh->ino->localname)
1255 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1256 return -1;
1257 if (!fh->ino->localname)
1258 vfs_die ("retrieve_file failed to fill in localname");
1259 return 0;
1262 static void
1263 fish_fill_names (struct vfs_class *me, fill_names_f func)
1265 struct vfs_s_super *super = MEDATA->supers;
1266 char *name;
1268 char gbuf[10];
1270 while (super)
1272 const char *flags = "";
1273 switch (SUP.flags)
1275 case FISH_FLAG_RSH:
1276 flags = ":r";
1277 break;
1278 case FISH_FLAG_COMPRESSED:
1279 flags = ":C";
1280 break;
1281 default:
1282 if (SUP.flags > FISH_FLAG_RSH)
1284 break;
1285 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1286 flags = gbuf;
1288 break;
1291 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1292 (*func) (name);
1293 g_free (name);
1294 super = super->next;
1298 static void *
1299 fish_open (struct vfs_class *me, const char *file, int flags, int mode)
1302 sorry, i've places hack here
1303 cause fish don't able to open files with O_EXCL flag
1305 flags &= ~O_EXCL;
1306 return vfs_s_open (me, file, flags, mode);
1310 void
1311 init_fish (void)
1313 static struct vfs_s_subclass fish_subclass;
1315 tcp_init ();
1317 fish_subclass.flags = VFS_S_REMOTE;
1318 fish_subclass.archive_same = fish_archive_same;
1319 fish_subclass.open_archive = fish_open_archive;
1320 fish_subclass.free_archive = fish_free_archive;
1321 fish_subclass.fh_open = fish_fh_open;
1322 fish_subclass.dir_load = fish_dir_load;
1323 fish_subclass.file_store = fish_file_store;
1324 fish_subclass.linear_start = fish_linear_start;
1325 fish_subclass.linear_read = fish_linear_read;
1326 fish_subclass.linear_close = fish_linear_close;
1328 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1329 vfs_fish_ops.name = "fish";
1330 vfs_fish_ops.prefix = "sh:";
1331 vfs_fish_ops.fill_names = fish_fill_names;
1332 vfs_fish_ops.chmod = fish_chmod;
1333 vfs_fish_ops.chown = fish_chown;
1334 vfs_fish_ops.open = fish_open;
1335 vfs_fish_ops.symlink = fish_symlink;
1336 vfs_fish_ops.link = fish_link;
1337 vfs_fish_ops.unlink = fish_unlink;
1338 vfs_fish_ops.rename = fish_rename;
1339 vfs_fish_ops.mkdir = fish_mkdir;
1340 vfs_fish_ops.rmdir = fish_rmdir;
1341 vfs_fish_ops.ctl = fish_ctl;
1342 vfs_register_class (&vfs_fish_ops);