Final Indentation of all touched files
[midnight-commander.git] / lib / vfs / mc-vfs / fish.c
blob707e79820dfe7fa2538fa0238c7abe131c71c543
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)
767 /* *INDENT-OFF* */
768 n = fish_command (me, super, WAIT_REPLY,
769 "#STOR %lu /%s\n"
770 "echo '### 001'\n"
771 "file=/%s\n"
772 "res=`exec 3>&1\n"
773 "(\n"
774 "head -c %lu -q - || echo DD >&3\n"
775 ") 2>/dev/null | (\n"
776 "cat > $file\n"
777 "cat > /dev/null\n"
778 ")`; [ \"$res\" = DD ] && {\n"
779 "> \"$file\"\n"
780 "rest=%lu\n"
781 "while [ $rest -gt 0 ]\n"
782 "do\n"
783 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
784 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
785 " rest=`expr $rest - $n`\n"
786 "done\n"
787 "}; echo '### 200'\n",
788 (unsigned long) s.st_size, quoted_name,
789 quoted_name, (unsigned long) s.st_size, (unsigned long) s.st_size);
790 /* *INDENT-ON* */
792 else
794 /* *INDENT-OFF* */
795 n = fish_command (me, super, WAIT_REPLY,
796 "#STOR %lu /%s\n"
797 "echo '### 001'\n"
798 "{\n"
799 "file=/%s\n"
800 "rest=%lu\n"
801 "while [ $rest -gt 0 ]\n"
802 "do\n"
803 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
804 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
805 " rest=`expr $rest - $n`\n"
806 "done\n"
807 "}; echo '### 200'\n",
808 (unsigned long) s.st_size, quoted_name,
809 quoted_name, (unsigned long) s.st_size);
810 /* *INDENT-ON* */
812 if (n != PRELIM)
814 close (h);
815 ERRNOR (E_REMOTE, -1);
818 total = 0;
820 while (1)
822 int t;
823 while ((n = read (h, buffer, sizeof (buffer))) < 0)
825 if ((errno == EINTR) && tty_got_interrupt ())
826 continue;
827 print_vfs_message (_("fish: Local read failed, sending zeros"));
828 close (h);
829 h = open ("/dev/zero", O_RDONLY);
831 if (n == 0)
832 break;
833 if ((t = write (SUP.sockw, buffer, n)) != n)
835 if (t == -1)
837 me->verrno = errno;
839 else
841 me->verrno = EIO;
843 goto error_return;
845 tty_disable_interrupt_key ();
846 total += n;
847 print_vfs_message (_("fish: storing %s %d (%lu)"),
848 was_error ? _("zeros") : _("file"), total, (unsigned long) s.st_size);
850 close (h);
851 g_free (quoted_name);
852 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
853 ERRNOR (E_REMOTE, -1);
854 return 0;
855 error_return:
856 close (h);
857 fish_get_reply (me, SUP.sockr, NULL, 0);
858 g_free (quoted_name);
859 return -1;
862 static int
863 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
865 char *name;
866 char *quoted_name;
867 if (offset)
868 ERRNOR (E_NOTSUPP, 0);
869 name = vfs_s_fullpath (me, fh->ino);
870 if (name == NULL)
871 return 0;
872 quoted_name = strutils_shell_escape (name);
873 g_free (name);
874 fh->u.fish.append = 0;
877 * Check whether the remote file is readable by using `dd' to copy
878 * a single byte from the remote file to /dev/null. If `dd' completes
879 * with exit status of 0 use `cat' to send the file contents to the
880 * standard output (i.e. over the network).
882 /* *INDENT-OFF* */
883 offset = fish_command (me, FH_SUPER, WANT_STRING,
884 "#RETR /%s\n"
885 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
886 "then\n"
887 "ls -ln /%s 2>/dev/null | (\n"
888 "read p l u g s r\n"
889 "echo $s\n"
890 ")\n"
891 "echo '### 100'\n"
892 "cat /%s\n"
893 "echo '### 200'\n"
894 "else\n"
895 "echo '### 500'\n"
896 "fi\n",
897 quoted_name, quoted_name, quoted_name, quoted_name);
898 /* *INDENT-ON* */
899 g_free (quoted_name);
900 if (offset != PRELIM)
901 ERRNOR (E_REMOTE, 0);
902 fh->linear = LS_LINEAR_OPEN;
903 fh->u.fish.got = 0;
904 errno = 0;
905 #if SIZEOF_OFF_T == SIZEOF_LONG
906 fh->u.fish.total = strtol (reply_str, NULL, 10);
907 #else
908 fh->u.fish.total = strtoll (reply_str, NULL, 10);
909 #endif
910 if (errno != 0)
911 ERRNOR (E_REMOTE, 0);
912 return 1;
915 static void
916 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
918 struct vfs_s_super *super = FH_SUPER;
919 char buffer[8192];
920 int n;
922 print_vfs_message (_("Aborting transfer..."));
925 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
926 if (n)
928 if ((n = read (SUP.sockr, buffer, n)) < 0)
929 return;
930 fh->u.fish.got += n;
933 while (n);
935 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
936 print_vfs_message (_("Error reported after abort."));
937 else
938 print_vfs_message (_("Aborted transfer would be successful."));
941 static int
942 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
944 struct vfs_s_super *super = FH_SUPER;
945 int n = 0;
946 len = MIN (fh->u.fish.total - fh->u.fish.got, len);
947 tty_disable_interrupt_key ();
948 while (len && ((n = read (SUP.sockr, buf, len)) < 0))
950 if ((errno == EINTR) && !tty_got_interrupt ())
951 continue;
952 break;
954 tty_enable_interrupt_key ();
956 if (n > 0)
957 fh->u.fish.got += n;
958 if (n < 0)
959 fish_linear_abort (me, fh);
960 if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
961 ERRNOR (E_REMOTE, -1);
962 ERRNOR (errno, n);
965 static void
966 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
968 if (fh->u.fish.total != fh->u.fish.got)
969 fish_linear_abort (me, fh);
972 static int
973 fish_ctl (void *fh, int ctlop, void *arg)
975 (void) arg;
976 (void) fh;
977 (void) ctlop;
978 return 0;
979 #if 0
980 switch (ctlop)
982 case VFS_CTL_IS_NOTREADY:
984 int v;
986 if (!FH->linear)
987 vfs_die ("You may not do this");
988 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
989 return 0;
991 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
992 if (((v < 0) && (errno == EINTR)) || v == 0)
993 return 1;
994 return 0;
996 default:
997 return 0;
999 #endif
1002 static int
1003 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1005 int r;
1007 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1008 vfs_stamp_create (&vfs_fish_ops, super);
1009 if (r != COMPLETE)
1010 ERRNOR (E_REMOTE, -1);
1011 if (flags & OPT_FLUSH)
1012 vfs_s_invalidate (me, super);
1013 return 0;
1016 #define PREFIX \
1017 char buf[BUF_LARGE]; \
1018 const char *crpath; \
1019 char *rpath, *mpath = g_strdup (path); \
1020 struct vfs_s_super *super; \
1021 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) \
1023 g_free (mpath); \
1024 return -1; \
1026 rpath = strutils_shell_escape(crpath); \
1027 g_free (mpath);
1029 #define POSTFIX(flags) \
1030 g_free (rpath); \
1031 return fish_send_command(me, super, buf, flags);
1033 static int
1034 fish_chmod (struct vfs_class *me, const char *path, int mode)
1036 /* *INDENT-OFF* */
1037 PREFIX
1038 g_snprintf (buf, sizeof (buf),
1039 "#CHMOD %4.4o /%s\n"
1040 "if chmod %4.4o /%s 2>/dev/null; then\n"
1041 "echo '### 000'\n"
1042 "else\n"
1043 "echo '### 500'\n"
1044 "fi\n",
1045 mode & 07777, rpath, mode & 07777, rpath);
1046 POSTFIX (OPT_FLUSH);
1047 /* *INDENT-ON* */
1050 /* *INDENT-OFF* */
1051 #define FISH_OP(name, string) \
1052 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1054 char buf[BUF_LARGE]; \
1055 const char *crpath1, *crpath2; \
1056 char *rpath1, *rpath2, *mpath1, *mpath2; \
1057 struct vfs_s_super *super1, *super2; \
1058 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) \
1060 g_free (mpath1); \
1061 return -1; \
1063 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) \
1065 g_free (mpath1); \
1066 g_free (mpath2); \
1067 return -1; \
1069 rpath1 = strutils_shell_escape (crpath1); \
1070 g_free (mpath1); \
1071 rpath2 = strutils_shell_escape (crpath2); \
1072 g_free (mpath2); \
1073 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1074 g_free (rpath1); \
1075 g_free (rpath2); \
1076 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1079 FISH_OP (rename,
1080 "#RENAME /%s /%s\n"
1081 "if mv /%s /%s 2>/dev/null; then\n"
1082 "echo '### 000'\n"
1083 "else\n"
1084 "echo '### 500'\n"
1085 "fi\n")
1087 FISH_OP (link,
1088 "#LINK /%s /%s\n"
1089 "if ln /%s /%s 2>/dev/null; then\n"
1090 "echo '### 000'\n"
1091 "else\n"
1092 "echo '### 500'\n"
1093 "fi\n")
1094 /* *INDENT-ON* */
1096 static int
1097 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1099 char *qsetto;
1100 PREFIX qsetto = strutils_shell_escape (setto);
1101 /* *INDENT-OFF* */
1102 g_snprintf (buf, sizeof (buf),
1103 "#SYMLINK %s /%s\n"
1104 "if ln -s %s /%s 2>/dev/null; then\n"
1105 "echo '### 000'\n"
1106 "else\n"
1107 "echo '### 500'\n"
1108 "fi\n",
1109 qsetto, rpath, qsetto, rpath);
1110 /* *INDENT-ON* */
1111 g_free (qsetto);
1112 POSTFIX (OPT_FLUSH);
1115 static int
1116 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
1118 char *sowner, *sgroup;
1119 struct passwd *pw;
1120 struct group *gr;
1122 if ((pw = getpwuid (owner)) == NULL)
1123 return 0;
1125 if ((gr = getgrgid (group)) == NULL)
1126 return 0;
1128 sowner = pw->pw_name;
1129 sgroup = gr->gr_name;
1131 /* *INDENT-OFF* */
1132 PREFIX
1133 g_snprintf (buf, sizeof (buf),
1134 "#CHOWN %s:%s /%s\n"
1135 "if chown %s:%s /%s 2>/dev/null; then\n"
1136 "echo '### 000'\n"
1137 "else\n"
1138 "echo '### 500'\n"
1139 "fi\n",
1140 sowner, sgroup, rpath, sowner, sgroup, rpath);
1141 /* *INDENT-ON* */
1142 fish_send_command (me, super, buf, OPT_FLUSH);
1143 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1144 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1145 POSTFIX (OPT_FLUSH)}
1148 static int
1149 fish_unlink (struct vfs_class *me, const char *path)
1151 /* *INDENT-OFF* */
1152 PREFIX
1154 g_snprintf (buf, sizeof (buf),
1155 "#DELE /%s\n"
1156 "if rm -f /%s 2>/dev/null; then\n"
1157 "echo '### 000'\n"
1158 "else\n"
1159 "echo '### 500'\n"
1160 "fi\n",
1161 rpath, rpath);
1162 /* *INDENT-ON* */
1164 POSTFIX (OPT_FLUSH);
1167 static int
1168 fish_exists (struct vfs_class *me, const char *path)
1170 /* *INDENT-OFF* */
1171 PREFIX
1173 g_snprintf (buf, sizeof (buf),
1174 "#ISEXISTS /%s\n"
1175 "ls -l /%s >/dev/null 2>/dev/null\n"
1176 "echo '### '$?\n",
1177 rpath, rpath);
1178 /* *INDENT-ON* */
1180 g_free (rpath);
1182 if (fish_send_command (me, super, buf, OPT_FLUSH) == 0)
1183 return 1;
1185 return 0;
1189 static int
1190 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1192 int ret_code;
1194 /* *INDENT-OFF* */
1195 PREFIX (void) mode;
1197 g_snprintf (buf, sizeof (buf),
1198 "#MKD /%s\n"
1199 "if mkdir /%s 2>/dev/null; then\n"
1200 "echo '### 000'\n"
1201 "else\n"
1202 "echo '### 500'\n"
1203 "fi\n",
1204 rpath, rpath);
1205 /* *INDENT-ON* */
1207 g_free (rpath);
1208 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1210 if (ret_code != 0)
1211 return ret_code;
1213 if (!fish_exists (me, path))
1215 ERRNOR (EACCES, -1);
1217 return 0;
1220 static int
1221 fish_rmdir (struct vfs_class *me, const char *path)
1223 /* *INDENT-OFF* */
1224 PREFIX
1225 g_snprintf (buf, sizeof (buf),
1226 "#RMD /%s\n"
1227 "if rmdir /%s 2>/dev/null; then\n"
1228 "echo '### 000'\n"
1229 "else\n"
1230 "echo '### 500'\n"
1231 "fi\n",
1232 rpath, rpath);
1233 /* *INDENT-ON* */
1234 POSTFIX (OPT_FLUSH);
1237 static int
1238 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
1240 (void) mode;
1242 fh->u.fish.append = 0;
1243 /* File will be written only, so no need to retrieve it */
1244 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1246 fh->u.fish.append = flags & O_APPEND;
1247 if (!fh->ino->localname)
1249 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1250 fh->ino->ent->name);
1251 if (tmp_handle == -1)
1252 return -1;
1253 close (tmp_handle);
1255 return 0;
1257 if (!fh->ino->localname)
1258 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1259 return -1;
1260 if (!fh->ino->localname)
1261 vfs_die ("retrieve_file failed to fill in localname");
1262 return 0;
1265 static void
1266 fish_fill_names (struct vfs_class *me, fill_names_f func)
1268 struct vfs_s_super *super = MEDATA->supers;
1269 char *name;
1271 char gbuf[10];
1273 while (super)
1275 const char *flags = "";
1276 switch (SUP.flags)
1278 case FISH_FLAG_RSH:
1279 flags = ":r";
1280 break;
1281 case FISH_FLAG_COMPRESSED:
1282 flags = ":C";
1283 break;
1284 default:
1285 if (SUP.flags > FISH_FLAG_RSH)
1287 break;
1288 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1289 flags = gbuf;
1291 break;
1294 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1295 (*func) (name);
1296 g_free (name);
1297 super = super->next;
1301 static void *
1302 fish_open (struct vfs_class *me, const char *file, int flags, int mode)
1305 sorry, i've places hack here
1306 cause fish don't able to open files with O_EXCL flag
1308 flags &= ~O_EXCL;
1309 return vfs_s_open (me, file, flags, mode);
1313 void
1314 init_fish (void)
1316 static struct vfs_s_subclass fish_subclass;
1318 tcp_init ();
1320 fish_subclass.flags = VFS_S_REMOTE;
1321 fish_subclass.archive_same = fish_archive_same;
1322 fish_subclass.open_archive = fish_open_archive;
1323 fish_subclass.free_archive = fish_free_archive;
1324 fish_subclass.fh_open = fish_fh_open;
1325 fish_subclass.dir_load = fish_dir_load;
1326 fish_subclass.file_store = fish_file_store;
1327 fish_subclass.linear_start = fish_linear_start;
1328 fish_subclass.linear_read = fish_linear_read;
1329 fish_subclass.linear_close = fish_linear_close;
1331 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1332 vfs_fish_ops.name = "fish";
1333 vfs_fish_ops.prefix = "sh:";
1334 vfs_fish_ops.fill_names = fish_fill_names;
1335 vfs_fish_ops.chmod = fish_chmod;
1336 vfs_fish_ops.chown = fish_chown;
1337 vfs_fish_ops.open = fish_open;
1338 vfs_fish_ops.symlink = fish_symlink;
1339 vfs_fish_ops.link = fish_link;
1340 vfs_fish_ops.unlink = fish_unlink;
1341 vfs_fish_ops.rename = fish_rename;
1342 vfs_fish_ops.mkdir = fish_mkdir;
1343 vfs_fish_ops.rmdir = fish_rmdir;
1344 vfs_fish_ops.ctl = fish_ctl;
1345 vfs_register_class (&vfs_fish_ops);