Ticket #2097: clean up before 4.7.2 release.
[midnight-commander.git] / lib / vfs / mc-vfs / fish.c
blobb1b172dde9b87ced34d279b4d9bc16c4b4e343f1
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 res = fork ();
211 if (res != 0)
213 if (res < 0)
214 vfs_die ("Cannot fork(): %m.");
215 /* We are the parent */
216 close (fileset1[0]);
217 SUP.sockw = fileset1[1];
218 close (fileset2[1]);
219 SUP.sockr = fileset2[0];
221 else
223 res = dup2 (fileset1[0], 0);
224 close (fileset1[0]);
225 close (fileset1[1]);
226 res = dup2 (fileset2[1], 1);
227 close (2);
228 /* stderr to /dev/null */
229 res = open ("/dev/null", O_WRONLY);
230 close (fileset2[0]);
231 close (fileset2[1]);
232 execvp (path, const_cast (char **, argv));
233 _exit (3);
237 /* The returned directory should always contain a trailing slash */
238 static char *
239 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
241 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
242 return g_strconcat (reply_str, "/", (char *) NULL);
243 ERRNOR (EIO, NULL);
246 static int
247 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
250 char gbuf[10];
251 const char *argv[10]; /* All of 10 is used now */
252 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
253 int i = 0;
255 argv[i++] = xsh;
256 if (SUP.flags == FISH_FLAG_COMPRESSED)
257 argv[i++] = "-C";
259 if (SUP.flags > FISH_FLAG_RSH)
261 argv[i++] = "-p";
262 g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
263 argv[i++] = gbuf;
267 * Add the user name to the ssh command line only if it was explicitly
268 * set in vfs URL. rsh/ssh will get current user by default
269 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
270 * option breaks it for some)
273 if (SUP.user)
275 argv[i++] = "-l";
276 argv[i++] = SUP.user;
278 else
280 /* The rest of the code assumes it to be a valid username */
281 SUP.user = vfs_get_local_username ();
284 argv[i++] = SUP.host;
285 argv[i++] = "echo FISH:; /bin/sh";
286 argv[i++] = NULL;
288 fish_pipeopen (super, xsh, argv);
291 char answer[2048];
292 print_vfs_message (_("fish: Waiting for initial line..."));
293 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
294 ERRNOR (E_PROTO, -1);
295 print_vfs_message ("%s", answer);
296 if (strstr (answer, "assword"))
298 /* Currently, this does not work. ssh reads passwords from
299 /dev/tty, not from stdin :-(. */
301 message (D_ERROR, MSG_ERROR,
302 _("Sorry, we cannot do password authenticated connections for now."));
303 ERRNOR (EPERM, -1);
304 if (!SUP.password)
306 char *p, *op;
307 p = g_strconcat (_(" fish: Password required for "), SUP.user, " ", (char *) NULL);
308 op = vfs_get_password (p);
309 g_free (p);
310 if (op == NULL)
311 ERRNOR (EPERM, -1);
312 SUP.password = op;
314 print_vfs_message (_("fish: Sending password..."));
317 size_t str_len;
318 str_len = strlen (SUP.password);
319 if ((write (SUP.sockw, SUP.password, str_len) != (ssize_t) str_len)
320 || (write (SUP.sockw, "\n", 1) != 1))
322 ERRNOR (EIO, -1);
328 print_vfs_message (_("fish: Sending initial line..."));
330 * Run `start_fish_server'. If it doesn't exist - no problem,
331 * we'll talk directly to the shell.
333 if (fish_command
334 (me, super, WAIT_REPLY,
335 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
336 ERRNOR (E_PROTO, -1);
338 print_vfs_message (_("fish: Handshaking version..."));
339 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
340 ERRNOR (E_PROTO, -1);
342 /* Set up remote locale to C, otherwise dates cannot be recognized */
343 if (fish_command
344 (me, super, WAIT_REPLY,
345 "LANG=C; LC_ALL=C; LC_TIME=C\n"
346 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE)
347 ERRNOR (E_PROTO, -1);
349 print_vfs_message (_("fish: Setting up current directory..."));
350 SUP.cwdir = fish_getcwd (me, super);
351 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
352 #if 0
353 super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
354 #endif
355 super->name = g_strdup (PATH_SEP_STR);
357 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
358 return 0;
361 static int
362 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
363 const char *archive_name, char *op)
365 char *host, *user, *password, *p;
366 int flags;
368 (void) archive_name;
370 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
371 &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
373 g_free (p);
375 SUP.host = host;
376 SUP.user = user;
377 SUP.flags = flags;
378 if (!strncmp (op, "rsh:", 4))
379 SUP.flags = FISH_FLAG_RSH;
380 SUP.cwdir = NULL;
381 if (password)
382 SUP.password = password;
383 return fish_open_archive_int (me, super);
386 static int
387 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
388 const char *archive_name, char *op, void *cookie)
390 char *host, *user;
391 int flags;
392 int result;
394 (void) me;
395 (void) archive_name;
396 (void) cookie;
398 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
399 URL_NOSLASH | URL_USE_ANONYMOUS);
401 g_free (op);
403 if (user == NULL)
404 user = vfs_get_local_username ();
406 result = ((strcmp (host, SUP.host) == 0)
407 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
409 g_free (host);
410 g_free (user);
412 return result;
415 static int
416 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
418 struct vfs_s_super *super = dir->super;
419 char buffer[8192];
420 struct vfs_s_entry *ent = NULL;
421 FILE *logfile;
422 char *quoted_path;
423 int reply_code;
424 gchar *shell_commands;
426 #if 0
428 * Simple FISH debug interface :]
430 if (!(MEDATA->logfile))
432 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
434 #endif /* 0 */
436 logfile = MEDATA->logfile;
438 print_vfs_message (_("fish: Reading directory %s..."), remote_path);
440 gettimeofday (&dir->timestamp, NULL);
441 dir->timestamp.tv_sec += fish_directory_timeout;
442 quoted_path = strutils_shell_escape (remote_path);
443 /* *INDENT-OFF* */
444 shell_commands = g_strconcat (
445 "#LIST /%s\n"
446 "if `perl -v > /dev/null 2>&1` ; then\n"
447 "perl -e '\n"
448 "use strict;\n"
449 "use POSIX;\n"
450 "use Fcntl;\n"
451 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
452 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
453 "my $dirname = $ARGV[0];\n"
454 "if (opendir ( DIR, $dirname )) {\n"
455 "while( (my $filename = readdir(DIR))){\n"
456 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
457 "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
459 "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
460 "my $e_filename = $filename;\n"
461 "$e_filename =~ $strutils_shell_escape_regex;\n"
462 "if (S_ISLNK($mode) ) {\n"
463 "my $linkname = readlink (\"$dirname/$filename\");\n"
464 "$linkname =~ $strutils_shell_escape_regex;\n"
465 "\n"
466 "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
467 "d$mloctime\\n"
468 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
469 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
470 "} else {\n"
471 "printf(\"R%%o %%o $uid.$gid\\n"
472 "S$size\\n"
473 "d$mloctime\\n"
474 ":\\\"$e_filename\\\"\\n"
475 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
476 "}}\n"
478 "printf(\"### 200\\n\");\n"
479 "closedir(DIR);\n"
480 "} else {\n"
481 "printf(\"### 500\\n\");\n"
482 "}\n"
483 "exit 0\n"
484 "' /%s ||\n" /* ARGV[0] - path to browse */
485 " echo '### 500'\n" /* do not hang if perl is to eval it */
486 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
487 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
488 "LSOPT=\"-Qlan\";\n"
489 "ADD=0;\n"
490 "else\n"
491 "LSOPT=\"-lan\";\n"
492 "ADD=1;\n"
493 "fi\n"
494 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
495 "while read p l u g s m d y n n2 n3; do\n"
496 "if test \"$m\" = \"0\" ; then \n"
497 "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
498 "else\n"
499 "n=$n\" \"$n2\" \"$n3\n"
500 "fi\n"
501 "if [ $ADD = 0 ]; then\n"
502 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
503 "elif `sed --version >/dev/null 2>&1` ; then\n"
504 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
505 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
506 "else\n"
507 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
508 "fi\n"
509 "done )\n"
510 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
511 "while read p l u g a i m d y n n2 n3; do\n"
512 "if test \"$a\" = \"0\" ; then \n"
513 "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
514 "else\n"
515 "n=$n\" \"$n2\" \"$n3\n"
516 "fi\n"
517 "if [ $ADD = 0 ]; then\n"
518 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
519 "elif `sed --version >/dev/null 2>&1` ; then\n"
520 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
521 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
522 "else\n"
523 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
524 "fi\n"
525 "done)\n"
526 "echo '### 200'\n"
527 "else\n"
528 "echo '### 500'\n"
529 "fi\n"
531 (char *) NULL);
532 /* *INDENT-ON* */
534 fish_command (me, super, NONE, shell_commands,
535 quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
537 g_free (shell_commands);
538 g_free (quoted_path);
539 ent = vfs_s_generate_entry (me, NULL, dir, 0);
540 while (1)
542 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
543 if ((!res) || (res == EINTR))
545 vfs_s_free_entry (me, ent);
546 me->verrno = ECONNRESET;
547 goto error;
549 if (logfile)
551 fputs (buffer, logfile);
552 fputs ("\n", logfile);
553 fflush (logfile);
555 if (!strncmp (buffer, "### ", 4))
556 break;
557 if ((!buffer[0]))
559 if (ent->name)
561 vfs_s_insert_entry (me, dir, ent);
562 ent = vfs_s_generate_entry (me, NULL, dir, 0);
564 continue;
567 #define ST ent->ino->st
569 switch (buffer[0])
571 case ':':
573 char *temp;
574 char *data_start = buffer + 1;
575 char *filename = data_start;
576 char *linkname = data_start;
577 char *filename_bound = filename + strlen (filename);
578 char *linkname_bound = filename_bound;
579 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
580 break; /* We'll do "." and ".." ourselves */
582 if (S_ISLNK (ST.st_mode))
584 /* we expect: "escaped-name" -> "escaped-name"
585 // -> cannot occur in filenames,
586 // because it will be escaped to -\> */
588 if (*filename == '"')
589 ++filename;
591 linkname = strstr (filename, "\" -> \"");
592 if (!linkname)
594 /* broken client, or smth goes wrong */
595 linkname = filename_bound;
596 if (filename_bound > filename && *(filename_bound - 1) == '"')
597 --filename_bound; /* skip trailing " */
599 else
601 filename_bound = linkname;
602 linkname += 6; /* strlen ("\" -> \"") */
603 if (*(linkname_bound - 1) == '"')
604 --linkname_bound; /* skip trailing " */
607 ent->name = str_dup_range (filename, filename_bound);
608 temp = ent->name;
609 ent->name = strutils_shell_unescape (ent->name);
610 g_free (temp);
612 ent->ino->linkname = str_dup_range (linkname, linkname_bound);
613 temp = ent->ino->linkname;
614 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
615 g_free (temp);
617 else
619 /* we expect: "escaped-name" */
620 if (filename_bound - filename > 2)
623 there is at least 2 "
624 and we skip them
626 if (*filename == '"')
627 ++filename;
628 if (*(filename_bound - 1) == '"')
629 --filename_bound;
631 ent->name = str_dup_range (filename, filename_bound);
632 temp = ent->name;
633 ent->name = strutils_shell_unescape (ent->name);
634 g_free (temp);
636 break;
638 case 'S':
639 #ifdef HAVE_ATOLL
640 ST.st_size = (off_t) atoll (buffer + 1);
641 #else
642 ST.st_size = (off_t) atof (buffer + 1);
643 #endif
644 break;
645 case 'P':
647 size_t skipped;
648 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
649 break;
651 case 'R':
654 raw filemode:
655 we expect: Roctal-filemode octal-filetype uid.gid
657 size_t skipped;
658 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
659 break;
661 case 'd':
663 vfs_split_text (buffer + 1);
664 if (!vfs_parse_filedate (0, &ST.st_ctime))
665 break;
666 ST.st_atime = ST.st_mtime = ST.st_ctime;
668 break;
669 case 'D':
671 struct tm tim;
672 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
673 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
674 break;
675 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
677 break;
678 case 'E':
680 int maj, min;
681 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
682 break;
683 #ifdef HAVE_STRUCT_STAT_ST_RDEV
684 ST.st_rdev = makedev (maj, min);
685 #endif
690 vfs_s_free_entry (me, ent);
691 reply_code = fish_decode_reply (buffer + 4, 0);
692 if (reply_code == COMPLETE)
694 g_free (SUP.cwdir);
695 SUP.cwdir = g_strdup (remote_path);
696 print_vfs_message (_("%s: done."), me->name);
697 return 0;
699 else if (reply_code == ERROR)
701 me->verrno = EACCES;
703 else
705 me->verrno = E_REMOTE;
708 error:
709 print_vfs_message (_("%s: failure"), me->name);
710 return -1;
713 static int
714 fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
716 struct vfs_s_super *super = FH_SUPER;
717 int n, total;
718 char buffer[8192];
719 struct stat s;
720 int was_error = 0;
721 int h;
722 char *quoted_name;
724 h = open (localname, O_RDONLY);
726 if (h == -1)
727 ERRNOR (EIO, -1);
728 if (fstat (h, &s) < 0)
730 close (h);
731 ERRNOR (EIO, -1);
734 /* First, try this as stor:
736 * ( head -c number ) | ( cat > file; cat >/dev/null )
738 * If `head' is not present on the remote system, `dd' will be used.
739 * Unfortunately, we cannot trust most non-GNU `head' implementations
740 * even if `-c' options is supported. Therefore, we separate GNU head
741 * (and other modern heads?) using `-q' and `-' . This causes another
742 * implementations to fail (because of "incorrect options").
744 * Fallback is:
746 * rest=<number>
747 * while [ $rest -gt 0 ]
748 * do
749 * cnt=`expr \( $rest + 255 \) / 256`
750 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
751 * rest=`expr $rest - $n`
752 * done
754 * `dd' was not designed for full filling of input buffers,
755 * and does not report exact number of bytes (not blocks).
756 * Therefore a more complex shell script is needed.
758 * On some systems non-GNU head writes "Usage:" error report to stdout
759 * instead of stderr. It makes impossible the use of "head || dd"
760 * algorithm for file appending case, therefore just "dd" is used for it.
763 quoted_name = strutils_shell_escape (name);
764 print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
766 /* FIXME: File size is limited to ULONG_MAX */
767 if (!fh->u.fish.append)
769 /* *INDENT-OFF* */
770 n = fish_command (me, super, WAIT_REPLY,
771 "#STOR %lu /%s\n"
772 "echo '### 001'\n"
773 "file=/%s\n"
774 "res=`exec 3>&1\n"
775 "(\n"
776 "head -c %lu -q - || echo DD >&3\n"
777 ") 2>/dev/null | (\n"
778 "cat > $file\n"
779 "cat > /dev/null\n"
780 ")`; [ \"$res\" = DD ] && {\n"
781 "> \"$file\"\n"
782 "rest=%lu\n"
783 "while [ $rest -gt 0 ]\n"
784 "do\n"
785 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
786 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
787 " rest=`expr $rest - $n`\n"
788 "done\n"
789 "}; echo '### 200'\n",
790 (unsigned long) s.st_size, quoted_name,
791 quoted_name, (unsigned long) s.st_size, (unsigned long) s.st_size);
792 /* *INDENT-ON* */
794 else
796 /* *INDENT-OFF* */
797 n = fish_command (me, super, WAIT_REPLY,
798 "#STOR %lu /%s\n"
799 "echo '### 001'\n"
800 "{\n"
801 "file=/%s\n"
802 "rest=%lu\n"
803 "while [ $rest -gt 0 ]\n"
804 "do\n"
805 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
806 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
807 " rest=`expr $rest - $n`\n"
808 "done\n"
809 "}; echo '### 200'\n",
810 (unsigned long) s.st_size, quoted_name,
811 quoted_name, (unsigned long) s.st_size);
812 /* *INDENT-ON* */
814 if (n != PRELIM)
816 close (h);
817 ERRNOR (E_REMOTE, -1);
820 total = 0;
822 while (1)
824 int t;
825 while ((n = read (h, buffer, sizeof (buffer))) < 0)
827 if ((errno == EINTR) && tty_got_interrupt ())
828 continue;
829 print_vfs_message (_("fish: Local read failed, sending zeros"));
830 close (h);
831 h = open ("/dev/zero", O_RDONLY);
834 if (n == 0)
835 break;
837 t = write (SUP.sockw, buffer, n);
838 if (t != n)
840 if (t == -1)
841 me->verrno = errno;
842 else
843 me->verrno = EIO;
844 goto error_return;
846 tty_disable_interrupt_key ();
847 total += n;
848 print_vfs_message (_("fish: storing %s %d (%lu)"),
849 was_error ? _("zeros") : _("file"), total, (unsigned long) s.st_size);
851 close (h);
852 g_free (quoted_name);
853 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
854 ERRNOR (E_REMOTE, -1);
855 return 0;
856 error_return:
857 close (h);
858 fish_get_reply (me, SUP.sockr, NULL, 0);
859 g_free (quoted_name);
860 return -1;
863 static int
864 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
866 char *name;
867 char *quoted_name;
868 if (offset)
869 ERRNOR (E_NOTSUPP, 0);
870 name = vfs_s_fullpath (me, fh->ino);
871 if (name == NULL)
872 return 0;
873 quoted_name = strutils_shell_escape (name);
874 g_free (name);
875 fh->u.fish.append = 0;
878 * Check whether the remote file is readable by using `dd' to copy
879 * a single byte from the remote file to /dev/null. If `dd' completes
880 * with exit status of 0 use `cat' to send the file contents to the
881 * standard output (i.e. over the network).
883 /* *INDENT-OFF* */
884 offset = fish_command (me, FH_SUPER, WANT_STRING,
885 "#RETR /%s\n"
886 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
887 "then\n"
888 "ls -ln /%s 2>/dev/null | (\n"
889 "read p l u g s r\n"
890 "echo $s\n"
891 ")\n"
892 "echo '### 100'\n"
893 "cat /%s\n"
894 "echo '### 200'\n"
895 "else\n"
896 "echo '### 500'\n"
897 "fi\n",
898 quoted_name, quoted_name, quoted_name, quoted_name);
899 /* *INDENT-ON* */
900 g_free (quoted_name);
901 if (offset != PRELIM)
902 ERRNOR (E_REMOTE, 0);
903 fh->linear = LS_LINEAR_OPEN;
904 fh->u.fish.got = 0;
905 errno = 0;
906 #if SIZEOF_OFF_T == SIZEOF_LONG
907 fh->u.fish.total = strtol (reply_str, NULL, 10);
908 #else
909 fh->u.fish.total = strtoll (reply_str, NULL, 10);
910 #endif
911 if (errno != 0)
912 ERRNOR (E_REMOTE, 0);
913 return 1;
916 static void
917 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
919 struct vfs_s_super *super = FH_SUPER;
920 char buffer[8192];
921 int n;
923 print_vfs_message (_("Aborting transfer..."));
926 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
927 if (n != 0)
929 n = read (SUP.sockr, buffer, n);
930 if (n < 0)
931 return;
932 fh->u.fish.got += n;
935 while (n != 0);
937 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
938 print_vfs_message (_("Error reported after abort."));
939 else
940 print_vfs_message (_("Aborted transfer would be successful."));
943 static int
944 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
946 struct vfs_s_super *super = FH_SUPER;
947 int n = 0;
948 len = MIN (fh->u.fish.total - fh->u.fish.got, len);
949 tty_disable_interrupt_key ();
950 while (len && ((n = read (SUP.sockr, buf, len)) < 0))
952 if ((errno == EINTR) && !tty_got_interrupt ())
953 continue;
954 break;
956 tty_enable_interrupt_key ();
958 if (n > 0)
959 fh->u.fish.got += n;
960 else if (n < 0)
961 fish_linear_abort (me, fh);
962 else if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
963 ERRNOR (E_REMOTE, -1);
964 ERRNOR (errno, n);
967 static void
968 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
970 if (fh->u.fish.total != fh->u.fish.got)
971 fish_linear_abort (me, fh);
974 static int
975 fish_ctl (void *fh, int ctlop, void *arg)
977 (void) arg;
978 (void) fh;
979 (void) ctlop;
980 return 0;
981 #if 0
982 switch (ctlop)
984 case VFS_CTL_IS_NOTREADY:
986 int v;
988 if (!FH->linear)
989 vfs_die ("You may not do this");
990 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
991 return 0;
993 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
994 if (((v < 0) && (errno == EINTR)) || v == 0)
995 return 1;
996 return 0;
998 default:
999 return 0;
1001 #endif
1004 static int
1005 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1007 int r;
1009 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1010 vfs_stamp_create (&vfs_fish_ops, super);
1011 if (r != COMPLETE)
1012 ERRNOR (E_REMOTE, -1);
1013 if (flags & OPT_FLUSH)
1014 vfs_s_invalidate (me, super);
1015 return 0;
1018 #define PREFIX \
1019 char buf[BUF_LARGE]; \
1020 const char *crpath; \
1021 char *rpath, *mpath = g_strdup (path); \
1022 struct vfs_s_super *super; \
1023 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
1024 if (crpath == NULL) \
1026 g_free (mpath); \
1027 return -1; \
1029 rpath = strutils_shell_escape (crpath); \
1030 g_free (mpath);
1032 #define POSTFIX(flags) \
1033 g_free (rpath); \
1034 return fish_send_command (me, super, buf, flags);
1036 static int
1037 fish_chmod (struct vfs_class *me, const char *path, int mode)
1039 /* *INDENT-OFF* */
1040 PREFIX
1041 g_snprintf (buf, sizeof (buf),
1042 "#CHMOD %4.4o /%s\n"
1043 "if chmod %4.4o /%s 2>/dev/null; then\n"
1044 "echo '### 000'\n"
1045 "else\n"
1046 "echo '### 500'\n"
1047 "fi\n",
1048 mode & 07777, rpath, mode & 07777, rpath);
1049 POSTFIX (OPT_FLUSH);
1050 /* *INDENT-ON* */
1053 /* *INDENT-OFF* */
1054 #define FISH_OP(name, string) \
1055 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1057 char buf[BUF_LARGE]; \
1058 const char *crpath1, *crpath2; \
1059 char *rpath1, *rpath2, *mpath1, *mpath2; \
1060 struct vfs_s_super *super1, *super2; \
1061 crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0); \
1062 if (crpath1 == NULL) \
1064 g_free (mpath1); \
1065 return -1; \
1067 crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0); \
1068 if (crpath2 == NULL) \
1070 g_free (mpath1); \
1071 g_free (mpath2); \
1072 return -1; \
1074 rpath1 = strutils_shell_escape (crpath1); \
1075 g_free (mpath1); \
1076 rpath2 = strutils_shell_escape (crpath2); \
1077 g_free (mpath2); \
1078 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1079 g_free (rpath1); \
1080 g_free (rpath2); \
1081 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1084 FISH_OP (rename,
1085 "#RENAME /%s /%s\n"
1086 "if mv /%s /%s 2>/dev/null; then\n"
1087 "echo '### 000'\n"
1088 "else\n"
1089 "echo '### 500'\n"
1090 "fi\n")
1092 FISH_OP (link,
1093 "#LINK /%s /%s\n"
1094 "if ln /%s /%s 2>/dev/null; then\n"
1095 "echo '### 000'\n"
1096 "else\n"
1097 "echo '### 500'\n"
1098 "fi\n")
1099 /* *INDENT-ON* */
1101 static int
1102 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1104 char *qsetto;
1105 PREFIX qsetto = strutils_shell_escape (setto);
1106 /* *INDENT-OFF* */
1107 g_snprintf (buf, sizeof (buf),
1108 "#SYMLINK %s /%s\n"
1109 "if ln -s %s /%s 2>/dev/null; then\n"
1110 "echo '### 000'\n"
1111 "else\n"
1112 "echo '### 500'\n"
1113 "fi\n",
1114 qsetto, rpath, qsetto, rpath);
1115 /* *INDENT-ON* */
1116 g_free (qsetto);
1117 POSTFIX (OPT_FLUSH);
1120 static int
1121 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
1123 char *sowner, *sgroup;
1124 struct passwd *pw;
1125 struct group *gr;
1127 pw = getpwuid (owner);
1128 if (pw == NULL)
1129 return 0;
1131 gr = getgrgid (group);
1132 if (gr == NULL)
1133 return 0;
1135 sowner = pw->pw_name;
1136 sgroup = gr->gr_name;
1138 /* *INDENT-OFF* */
1139 PREFIX
1140 g_snprintf (buf, sizeof (buf),
1141 "#CHOWN %s:%s /%s\n"
1142 "if chown %s:%s /%s 2>/dev/null; then\n"
1143 "echo '### 000'\n"
1144 "else\n"
1145 "echo '### 500'\n"
1146 "fi\n",
1147 sowner, sgroup, rpath, sowner, sgroup, rpath);
1148 /* *INDENT-ON* */
1149 fish_send_command (me, super, buf, OPT_FLUSH);
1150 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1151 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1152 POSTFIX (OPT_FLUSH)}
1155 static int
1156 fish_unlink (struct vfs_class *me, const char *path)
1158 /* *INDENT-OFF* */
1159 PREFIX
1161 g_snprintf (buf, sizeof (buf),
1162 "#DELE /%s\n"
1163 "if rm -f /%s 2>/dev/null; then\n"
1164 "echo '### 000'\n"
1165 "else\n"
1166 "echo '### 500'\n"
1167 "fi\n",
1168 rpath, rpath);
1169 /* *INDENT-ON* */
1171 POSTFIX (OPT_FLUSH);
1174 static int
1175 fish_exists (struct vfs_class *me, const char *path)
1177 /* *INDENT-OFF* */
1178 PREFIX
1180 g_snprintf (buf, sizeof (buf),
1181 "#ISEXISTS /%s\n"
1182 "ls -l /%s >/dev/null 2>/dev/null\n"
1183 "echo '### '$?\n",
1184 rpath, rpath);
1185 /* *INDENT-ON* */
1187 g_free (rpath);
1189 return (fish_send_command (me, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1193 static int
1194 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1196 int ret_code;
1198 /* *INDENT-OFF* */
1199 PREFIX (void) mode;
1201 g_snprintf (buf, sizeof (buf),
1202 "#MKD /%s\n"
1203 "if mkdir /%s 2>/dev/null; then\n"
1204 "echo '### 000'\n"
1205 "else\n"
1206 "echo '### 500'\n"
1207 "fi\n",
1208 rpath, rpath);
1209 /* *INDENT-ON* */
1211 g_free (rpath);
1212 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1214 if (ret_code != 0)
1215 return ret_code;
1217 if (!fish_exists (me, path))
1219 ERRNOR (EACCES, -1);
1221 return 0;
1224 static int
1225 fish_rmdir (struct vfs_class *me, const char *path)
1227 /* *INDENT-OFF* */
1228 PREFIX
1229 g_snprintf (buf, sizeof (buf),
1230 "#RMD /%s\n"
1231 "if rmdir /%s 2>/dev/null; then\n"
1232 "echo '### 000'\n"
1233 "else\n"
1234 "echo '### 500'\n"
1235 "fi\n",
1236 rpath, rpath);
1237 /* *INDENT-ON* */
1238 POSTFIX (OPT_FLUSH);
1241 static int
1242 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
1244 (void) mode;
1246 fh->u.fish.append = 0;
1247 /* File will be written only, so no need to retrieve it */
1248 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1250 fh->u.fish.append = flags & O_APPEND;
1251 if (!fh->ino->localname)
1253 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1254 fh->ino->ent->name);
1255 if (tmp_handle == -1)
1256 return -1;
1257 close (tmp_handle);
1259 return 0;
1261 if (!fh->ino->localname)
1262 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1263 return -1;
1264 if (!fh->ino->localname)
1265 vfs_die ("retrieve_file failed to fill in localname");
1266 return 0;
1269 static void
1270 fish_fill_names (struct vfs_class *me, fill_names_f func)
1272 struct vfs_s_super *super = MEDATA->supers;
1273 char *name;
1275 char gbuf[10];
1277 while (super)
1279 const char *flags = "";
1280 switch (SUP.flags)
1282 case FISH_FLAG_RSH:
1283 flags = ":r";
1284 break;
1285 case FISH_FLAG_COMPRESSED:
1286 flags = ":C";
1287 break;
1288 default:
1289 if (SUP.flags > FISH_FLAG_RSH)
1291 break;
1292 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1293 flags = gbuf;
1295 break;
1298 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1299 (*func) (name);
1300 g_free (name);
1301 super = super->next;
1305 static void *
1306 fish_open (struct vfs_class *me, const char *file, int flags, int mode)
1309 sorry, i've places hack here
1310 cause fish don't able to open files with O_EXCL flag
1312 flags &= ~O_EXCL;
1313 return vfs_s_open (me, file, flags, mode);
1317 void
1318 init_fish (void)
1320 static struct vfs_s_subclass fish_subclass;
1322 tcp_init ();
1324 fish_subclass.flags = VFS_S_REMOTE;
1325 fish_subclass.archive_same = fish_archive_same;
1326 fish_subclass.open_archive = fish_open_archive;
1327 fish_subclass.free_archive = fish_free_archive;
1328 fish_subclass.fh_open = fish_fh_open;
1329 fish_subclass.dir_load = fish_dir_load;
1330 fish_subclass.file_store = fish_file_store;
1331 fish_subclass.linear_start = fish_linear_start;
1332 fish_subclass.linear_read = fish_linear_read;
1333 fish_subclass.linear_close = fish_linear_close;
1335 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1336 vfs_fish_ops.name = "fish";
1337 vfs_fish_ops.prefix = "sh:";
1338 vfs_fish_ops.fill_names = fish_fill_names;
1339 vfs_fish_ops.chmod = fish_chmod;
1340 vfs_fish_ops.chown = fish_chown;
1341 vfs_fish_ops.open = fish_open;
1342 vfs_fish_ops.symlink = fish_symlink;
1343 vfs_fish_ops.link = fish_link;
1344 vfs_fish_ops.unlink = fish_unlink;
1345 vfs_fish_ops.rename = fish_rename;
1346 vfs_fish_ops.mkdir = fish_mkdir;
1347 vfs_fish_ops.rmdir = fish_rmdir;
1348 vfs_fish_ops.ctl = fish_ctl;
1349 vfs_register_class (&vfs_fish_ops);