Applied MC indentation policy.
[midnight-commander.git] / lib / vfs / mc-vfs / fish.c
blob966317f3060dfd5d032686a85375bf699c82a2d5
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 fwrite (str, strlen (str), 1, logfile);
163 fflush (logfile);
166 tty_enable_interrupt_key ();
168 status = write (SUP.sockw, str, strlen (str));
169 g_free (str);
171 tty_disable_interrupt_key ();
172 if (status < 0)
173 return TRANSIENT;
175 if (wait_reply)
176 return fish_get_reply (me, SUP.sockr,
177 (wait_reply & WANT_STRING) ? reply_str :
178 NULL, sizeof (reply_str) - 1);
179 return COMPLETE;
182 static void
183 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
185 if ((SUP.sockw != -1) || (SUP.sockr != -1))
187 print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
188 fish_command (me, super, NONE, "#BYE\nexit\n");
189 close (SUP.sockw);
190 close (SUP.sockr);
191 SUP.sockw = SUP.sockr = -1;
193 g_free (SUP.host);
194 g_free (SUP.user);
195 g_free (SUP.cwdir);
196 g_free (SUP.password);
199 static void
200 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
202 int fileset1[2], fileset2[2];
203 int res;
205 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
206 vfs_die ("Cannot pipe(): %m.");
208 if ((res = fork ()))
210 if (res < 0)
211 vfs_die ("Cannot fork(): %m.");
212 /* We are the parent */
213 close (fileset1[0]);
214 SUP.sockw = fileset1[1];
215 close (fileset2[1]);
216 SUP.sockr = fileset2[0];
218 else
220 close (0);
221 dup (fileset1[0]);
222 close (fileset1[0]);
223 close (fileset1[1]);
224 close (1);
225 close (2);
226 dup (fileset2[1]);
227 /* stderr to /dev/null */
228 open ("/dev/null", O_WRONLY);
229 close (fileset2[0]);
230 close (fileset2[1]);
231 execvp (path, const_cast (char **, argv));
232 _exit (3);
236 /* The returned directory should always contain a trailing slash */
237 static char *
238 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
240 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
241 return g_strconcat (reply_str, "/", (char *) NULL);
242 ERRNOR (EIO, NULL);
245 static int
246 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
249 char gbuf[10];
250 const char *argv[10]; /* All of 10 is used now */
251 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
252 int i = 0;
254 argv[i++] = xsh;
255 if (SUP.flags == FISH_FLAG_COMPRESSED)
256 argv[i++] = "-C";
258 if (SUP.flags > FISH_FLAG_RSH)
260 argv[i++] = "-p";
261 g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
262 argv[i++] = gbuf;
266 * Add the user name to the ssh command line only if it was explicitly
267 * set in vfs URL. rsh/ssh will get current user by default
268 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
269 * option breaks it for some)
272 if (SUP.user)
274 argv[i++] = "-l";
275 argv[i++] = SUP.user;
277 else
279 /* The rest of the code assumes it to be a valid username */
280 SUP.user = vfs_get_local_username ();
283 argv[i++] = SUP.host;
284 argv[i++] = "echo FISH:; /bin/sh";
285 argv[i++] = NULL;
287 fish_pipeopen (super, xsh, argv);
290 char answer[2048];
291 print_vfs_message (_("fish: Waiting for initial line..."));
292 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
293 ERRNOR (E_PROTO, -1);
294 print_vfs_message ("%s", answer);
295 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..."));
315 write (SUP.sockw, SUP.password, strlen (SUP.password));
316 write (SUP.sockw, "\n", 1);
320 print_vfs_message (_("fish: Sending initial line..."));
322 * Run `start_fish_server'. If it doesn't exist - no problem,
323 * we'll talk directly to the shell.
325 if (fish_command
326 (me, super, WAIT_REPLY,
327 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
328 ERRNOR (E_PROTO, -1);
330 print_vfs_message (_("fish: Handshaking version..."));
331 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
332 ERRNOR (E_PROTO, -1);
334 /* Set up remote locale to C, otherwise dates cannot be recognized */
335 if (fish_command
336 (me, super, WAIT_REPLY,
337 "LANG=C; LC_ALL=C; LC_TIME=C\n"
338 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n") != COMPLETE)
339 ERRNOR (E_PROTO, -1);
341 print_vfs_message (_("fish: Setting up current directory..."));
342 SUP.cwdir = fish_getcwd (me, super);
343 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
344 #if 0
345 super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
346 #endif
347 super->name = g_strdup (PATH_SEP_STR);
349 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
350 return 0;
353 static int
354 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
355 const char *archive_name, char *op)
357 char *host, *user, *password, *p;
358 int flags;
360 (void) archive_name;
362 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
363 &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
365 g_free (p);
367 SUP.host = host;
368 SUP.user = user;
369 SUP.flags = flags;
370 if (!strncmp (op, "rsh:", 4))
371 SUP.flags = FISH_FLAG_RSH;
372 SUP.cwdir = NULL;
373 if (password)
374 SUP.password = password;
375 return fish_open_archive_int (me, super);
378 static int
379 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
380 const char *archive_name, char *op, void *cookie)
382 char *host, *user;
383 int flags;
384 int result;
386 (void) me;
387 (void) archive_name;
388 (void) cookie;
390 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
391 URL_NOSLASH | URL_USE_ANONYMOUS);
393 g_free (op);
395 if (user == NULL)
396 user = vfs_get_local_username ();
398 result = ((strcmp (host, SUP.host) == 0)
399 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
401 g_free (host);
402 g_free (user);
404 return result;
407 static int
408 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
410 struct vfs_s_super *super = dir->super;
411 char buffer[8192];
412 struct vfs_s_entry *ent = NULL;
413 FILE *logfile;
414 char *quoted_path;
415 int reply_code;
416 gchar *shell_commands;
418 #if 0
420 * Simple FISH debug interface :]
422 if (!(MEDATA->logfile))
424 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
426 #endif /* 0 */
428 logfile = MEDATA->logfile;
430 print_vfs_message (_("fish: Reading directory %s..."), remote_path);
432 gettimeofday (&dir->timestamp, NULL);
433 dir->timestamp.tv_sec += fish_directory_timeout;
434 quoted_path = strutils_shell_escape (remote_path);
435 /* *INDENT-OFF* */
436 shell_commands = g_strconcat (
437 "#LIST /%s\n"
438 "if `perl -v > /dev/null 2>&1` ; then\n"
439 "perl -e '\n"
440 "use strict;\n"
441 "use POSIX;\n"
442 "use Fcntl;\n"
443 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
444 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
445 "my $dirname = $ARGV[0];\n"
446 "if (opendir ( DIR, $dirname )) {\n"
447 "while( (my $filename = readdir(DIR))){\n"
448 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
449 "my $mloctime= strftime(\"%%m-%%d-%%Y %%H:%%M\", localtime $mtime);\n"
451 "my $strutils_shell_escape_regex = s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
452 "my $e_filename = $filename;\n"
453 "$e_filename =~ $strutils_shell_escape_regex;\n"
454 "if (S_ISLNK($mode) ) {\n"
455 "my $linkname = readlink (\"$dirname/$filename\");\n"
456 "$linkname =~ $strutils_shell_escape_regex;\n"
457 "\n"
458 "printf(\"R%%o %%o $uid.$gid\\n" "S$size\\n"
459 "d$mloctime\\n"
460 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
461 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
462 "} else {\n"
463 "printf(\"R%%o %%o $uid.$gid\\n"
464 "S$size\\n"
465 "d$mloctime\\n"
466 ":\\\"$e_filename\\\"\\n"
467 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
468 "}}\n"
470 "printf(\"### 200\\n\");\n"
471 "closedir(DIR);\n"
472 "} else {\n"
473 "printf(\"### 500\\n\");\n"
474 "}\n"
475 "exit 0\n"
476 "' /%s ||\n" /* ARGV[0] - path to browse */
477 " echo '### 500'\n" /* do not hang if perl is to eval it */
478 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
479 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
480 "LSOPT=\"-Qlan\";\n"
481 "ADD=0;\n"
482 "else\n"
483 "LSOPT=\"-lan\";\n"
484 "ADD=1;\n"
485 "fi\n"
486 "ls $LSOPT /%s 2>/dev/null | grep '^[^cbt]' | (\n"
487 "while read p l u g s m d y n n2 n3; do\n"
488 "if test \"$m\" = \"0\" ; then \n"
489 "s=$d; m=$y; d=$n y=$n2; n=$n3\n"
490 "else\n"
491 "n=$n\" \"$n2\" \"$n3\n"
492 "fi\n"
493 "if [ $ADD = 0 ]; then\n"
494 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
495 "elif `sed --version >/dev/null 2>&1` ; then\n"
496 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n",
497 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
498 "else\n"
499 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
500 "fi\n"
501 "done )\n"
502 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
503 "while read p l u g a i m d y n n2 n3; do\n"
504 "if test \"$a\" = \"0\" ; then \n"
505 "a=$m; i=$d; m=$y; d=$n y=$n2; n=$n3\n"
506 "else\n"
507 "n=$n\" \"$n2\" \"$n3\n"
508 "fi\n"
509 "if [ $ADD = 0 ]; then\n"
510 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
511 "elif `sed --version >/dev/null 2>&1` ; then\n"
512 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
513 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
514 "else\n"
515 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
516 "fi\n"
517 "done)\n"
518 "echo '### 200'\n"
519 "else\n"
520 "echo '### 500'\n"
521 "fi\n"
523 (char *) NULL);
524 /* *INDENT-ON* */
526 fish_command (me, super, NONE, shell_commands,
527 quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
529 g_free (shell_commands);
530 g_free (quoted_path);
531 ent = vfs_s_generate_entry (me, NULL, dir, 0);
532 while (1)
534 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
535 if ((!res) || (res == EINTR))
537 vfs_s_free_entry (me, ent);
538 me->verrno = ECONNRESET;
539 goto error;
541 if (logfile)
543 fputs (buffer, logfile);
544 fputs ("\n", logfile);
545 fflush (logfile);
547 if (!strncmp (buffer, "### ", 4))
548 break;
549 if ((!buffer[0]))
551 if (ent->name)
553 vfs_s_insert_entry (me, dir, ent);
554 ent = vfs_s_generate_entry (me, NULL, dir, 0);
556 continue;
559 #define ST ent->ino->st
561 switch (buffer[0])
563 case ':':
565 char *temp;
566 char *data_start = buffer + 1;
567 char *filename = data_start;
568 char *linkname = data_start;
569 char *filename_bound = filename + strlen (filename);
570 char *linkname_bound = filename_bound;
571 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
572 break; /* We'll do "." and ".." ourselves */
574 if (S_ISLNK (ST.st_mode))
576 /* we expect: "escaped-name" -> "escaped-name"
577 // -> cannot occur in filenames,
578 // because it will be escaped to -\> */
580 if (*filename == '"')
581 ++filename;
583 linkname = strstr (filename, "\" -> \"");
584 if (!linkname)
586 /* broken client, or smth goes wrong */
587 linkname = filename_bound;
588 if (filename_bound > filename && *(filename_bound - 1) == '"')
589 --filename_bound; /* skip trailing " */
591 else
593 filename_bound = linkname;
594 linkname += 6; /* strlen ("\" -> \"") */
595 if (*(linkname_bound - 1) == '"')
596 --linkname_bound; /* skip trailing " */
599 ent->name = str_dup_range (filename, filename_bound);
600 temp = ent->name;
601 ent->name = strutils_shell_unescape (ent->name);
602 g_free (temp);
604 ent->ino->linkname = str_dup_range (linkname, linkname_bound);
605 temp = ent->ino->linkname;
606 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
607 g_free (temp);
609 else
611 /* we expect: "escaped-name" */
612 if (filename_bound - filename > 2)
615 there is at least 2 "
616 and we skip them
618 if (*filename == '"')
619 ++filename;
620 if (*(filename_bound - 1) == '"')
621 --filename_bound;
623 ent->name = str_dup_range (filename, filename_bound);
624 temp = ent->name;
625 ent->name = strutils_shell_unescape (ent->name);
626 g_free (temp);
628 break;
630 case 'S':
631 #ifdef HAVE_ATOLL
632 ST.st_size = (off_t) atoll (buffer + 1);
633 #else
634 ST.st_size = (off_t) atof (buffer + 1);
635 #endif
636 break;
637 case 'P':
639 size_t skipped;
640 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
641 break;
643 case 'R':
646 raw filemode:
647 we expect: Roctal-filemode octal-filetype uid.gid
649 size_t skipped;
650 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
651 break;
653 case 'd':
655 vfs_split_text (buffer + 1);
656 if (!vfs_parse_filedate (0, &ST.st_ctime))
657 break;
658 ST.st_atime = ST.st_mtime = ST.st_ctime;
660 break;
661 case 'D':
663 struct tm tim;
664 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
665 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
666 break;
667 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
669 break;
670 case 'E':
672 int maj, min;
673 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
674 break;
675 #ifdef HAVE_STRUCT_STAT_ST_RDEV
676 ST.st_rdev = makedev (maj, min);
677 #endif
682 vfs_s_free_entry (me, ent);
683 reply_code = fish_decode_reply (buffer + 4, 0);
684 if (reply_code == COMPLETE)
686 g_free (SUP.cwdir);
687 SUP.cwdir = g_strdup (remote_path);
688 print_vfs_message (_("%s: done."), me->name);
689 return 0;
691 else if (reply_code == ERROR)
693 me->verrno = EACCES;
695 else
697 me->verrno = E_REMOTE;
700 error:
701 print_vfs_message (_("%s: failure"), me->name);
702 return -1;
705 static int
706 fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
708 struct vfs_s_super *super = FH_SUPER;
709 int n, total;
710 char buffer[8192];
711 struct stat s;
712 int was_error = 0;
713 int h;
714 char *quoted_name;
716 h = open (localname, O_RDONLY);
718 if (h == -1)
719 ERRNOR (EIO, -1);
720 if (fstat (h, &s) < 0)
722 close (h);
723 ERRNOR (EIO, -1);
726 /* First, try this as stor:
728 * ( head -c number ) | ( cat > file; cat >/dev/null )
730 * If `head' is not present on the remote system, `dd' will be used.
731 * Unfortunately, we cannot trust most non-GNU `head' implementations
732 * even if `-c' options is supported. Therefore, we separate GNU head
733 * (and other modern heads?) using `-q' and `-' . This causes another
734 * implementations to fail (because of "incorrect options").
736 * Fallback is:
738 * rest=<number>
739 * while [ $rest -gt 0 ]
740 * do
741 * cnt=`expr \( $rest + 255 \) / 256`
742 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
743 * rest=`expr $rest - $n`
744 * done
746 * `dd' was not designed for full filling of input buffers,
747 * and does not report exact number of bytes (not blocks).
748 * Therefore a more complex shell script is needed.
750 * On some systems non-GNU head writes "Usage:" error report to stdout
751 * instead of stderr. It makes impossible the use of "head || dd"
752 * algorithm for file appending case, therefore just "dd" is used for it.
755 quoted_name = strutils_shell_escape (name);
756 print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
758 /* FIXME: File size is limited to ULONG_MAX */
759 if (!fh->u.fish.append)
760 /* *INDENT-OFF* */
761 n = fish_command (me, super, WAIT_REPLY,
762 "#STOR %lu /%s\n"
763 "echo '### 001'\n"
764 "file=/%s\n"
765 "res=`exec 3>&1\n"
766 "(\n"
767 "head -c %lu -q - || echo DD >&3\n"
768 ") 2>/dev/null | (\n"
769 "cat > $file\n"
770 "cat > /dev/null\n"
771 ")`; [ \"$res\" = DD ] && {\n"
772 "> \"$file\"\n"
773 "rest=%lu\n"
774 "while [ $rest -gt 0 ]\n"
775 "do\n"
776 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
777 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
778 " rest=`expr $rest - $n`\n"
779 "done\n"
780 "}; echo '### 200'\n",
781 (unsigned long) s.st_size, quoted_name,
782 quoted_name, (unsigned long) s.st_size, (unsigned long) s.st_size);
783 /* *INDENT-ON* */
784 else
785 /* *INDENT-OFF* */
786 n = fish_command (me, super, WAIT_REPLY,
787 "#STOR %lu /%s\n"
788 "echo '### 001'\n"
789 "{\n"
790 "file=/%s\n"
791 "rest=%lu\n"
792 "while [ $rest -gt 0 ]\n"
793 "do\n"
794 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
795 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
796 " rest=`expr $rest - $n`\n"
797 "done\n"
798 "}; echo '### 200'\n",
799 (unsigned long) s.st_size, quoted_name,
800 quoted_name, (unsigned long) s.st_size);
801 /* *INDENT-ON* */
803 if (n != PRELIM)
805 close (h);
806 ERRNOR (E_REMOTE, -1);
809 total = 0;
811 while (1)
813 int t;
814 while ((n = read (h, buffer, sizeof (buffer))) < 0)
816 if ((errno == EINTR) && tty_got_interrupt ())
817 continue;
818 print_vfs_message (_("fish: Local read failed, sending zeros"));
819 close (h);
820 h = open ("/dev/zero", O_RDONLY);
822 if (n == 0)
823 break;
824 if ((t = write (SUP.sockw, buffer, n)) != n)
826 if (t == -1)
828 me->verrno = errno;
830 else
832 me->verrno = EIO;
834 goto error_return;
836 tty_disable_interrupt_key ();
837 total += n;
838 print_vfs_message (_("fish: storing %s %d (%lu)"),
839 was_error ? _("zeros") : _("file"), total, (unsigned long) s.st_size);
841 close (h);
842 g_free (quoted_name);
843 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
844 ERRNOR (E_REMOTE, -1);
845 return 0;
846 error_return:
847 close (h);
848 fish_get_reply (me, SUP.sockr, NULL, 0);
849 g_free (quoted_name);
850 return -1;
853 static int
854 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
856 char *name;
857 char *quoted_name;
858 if (offset)
859 ERRNOR (E_NOTSUPP, 0);
860 name = vfs_s_fullpath (me, fh->ino);
861 if (name == NULL)
862 return 0;
863 quoted_name = strutils_shell_escape (name);
864 g_free (name);
865 fh->u.fish.append = 0;
868 * Check whether the remote file is readable by using `dd' to copy
869 * a single byte from the remote file to /dev/null. If `dd' completes
870 * with exit status of 0 use `cat' to send the file contents to the
871 * standard output (i.e. over the network).
873 /* *INDENT-OFF* */
874 offset = fish_command (me, FH_SUPER, WANT_STRING,
875 "#RETR /%s\n"
876 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
877 "then\n"
878 "ls -ln /%s 2>/dev/null | (\n"
879 "read p l u g s r\n"
880 "echo $s\n"
881 ")\n"
882 "echo '### 100'\n"
883 "cat /%s\n"
884 "echo '### 200'\n"
885 "else\n"
886 "echo '### 500'\n"
887 "fi\n",
888 quoted_name, quoted_name, quoted_name, quoted_name);
889 /* *INDENT-ON* */
890 g_free (quoted_name);
891 if (offset != PRELIM)
892 ERRNOR (E_REMOTE, 0);
893 fh->linear = LS_LINEAR_OPEN;
894 fh->u.fish.got = 0;
895 errno = 0;
896 #if SIZEOF_OFF_T == SIZEOF_LONG
897 fh->u.fish.total = strtol (reply_str, NULL, 10);
898 #else
899 fh->u.fish.total = strtoll (reply_str, NULL, 10);
900 #endif
901 if (errno != 0)
902 ERRNOR (E_REMOTE, 0);
903 return 1;
906 static void
907 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
909 struct vfs_s_super *super = FH_SUPER;
910 char buffer[8192];
911 int n;
913 print_vfs_message (_("Aborting transfer..."));
916 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
917 if (n)
919 if ((n = read (SUP.sockr, buffer, n)) < 0)
920 return;
921 fh->u.fish.got += n;
924 while (n);
926 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
927 print_vfs_message (_("Error reported after abort."));
928 else
929 print_vfs_message (_("Aborted transfer would be successful."));
932 static int
933 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
935 struct vfs_s_super *super = FH_SUPER;
936 int n = 0;
937 len = MIN (fh->u.fish.total - fh->u.fish.got, len);
938 tty_disable_interrupt_key ();
939 while (len && ((n = read (SUP.sockr, buf, len)) < 0))
941 if ((errno == EINTR) && !tty_got_interrupt ())
942 continue;
943 break;
945 tty_enable_interrupt_key ();
947 if (n > 0)
948 fh->u.fish.got += n;
949 if (n < 0)
950 fish_linear_abort (me, fh);
951 if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
952 ERRNOR (E_REMOTE, -1);
953 ERRNOR (errno, n);
956 static void
957 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
959 if (fh->u.fish.total != fh->u.fish.got)
960 fish_linear_abort (me, fh);
963 static int
964 fish_ctl (void *fh, int ctlop, void *arg)
966 (void) arg;
967 (void) fh;
968 (void) ctlop;
969 return 0;
970 #if 0
971 switch (ctlop)
973 case VFS_CTL_IS_NOTREADY:
975 int v;
977 if (!FH->linear)
978 vfs_die ("You may not do this");
979 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
980 return 0;
982 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
983 if (((v < 0) && (errno == EINTR)) || v == 0)
984 return 1;
985 return 0;
987 default:
988 return 0;
990 #endif
993 static int
994 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
996 int r;
998 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
999 vfs_stamp_create (&vfs_fish_ops, super);
1000 if (r != COMPLETE)
1001 ERRNOR (E_REMOTE, -1);
1002 if (flags & OPT_FLUSH)
1003 vfs_s_invalidate (me, super);
1004 return 0;
1007 #define PREFIX \
1008 char buf[BUF_LARGE]; \
1009 const char *crpath; \
1010 char *rpath, *mpath = g_strdup (path); \
1011 struct vfs_s_super *super; \
1012 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) \
1014 g_free (mpath); \
1015 return -1; \
1017 rpath = strutils_shell_escape(crpath); \
1018 g_free (mpath);
1020 #define POSTFIX(flags) \
1021 g_free (rpath); \
1022 return fish_send_command(me, super, buf, flags);
1024 static int
1025 fish_chmod (struct vfs_class *me, const char *path, int mode)
1027 PREFIX
1028 /* *INDENT-OFF* */
1029 g_snprintf (buf, sizeof (buf),
1030 "#CHMOD %4.4o /%s\n"
1031 "if chmod %4.4o /%s 2>/dev/null; then\n"
1032 "echo '### 000'\n"
1033 "else\n"
1034 "echo '### 500'\n"
1035 "fi\n",
1036 mode & 07777, rpath, mode & 07777, rpath);
1037 /* *INDENT-ON* */
1038 POSTFIX (OPT_FLUSH);
1041 #define FISH_OP(name, string) \
1042 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
1044 char buf[BUF_LARGE]; \
1045 const char *crpath1, *crpath2; \
1046 char *rpath1, *rpath2, *mpath1, *mpath2; \
1047 struct vfs_s_super *super1, *super2; \
1048 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) \
1050 g_free (mpath1); \
1051 return -1; \
1053 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) \
1055 g_free (mpath1); \
1056 g_free (mpath2); \
1057 return -1; \
1059 rpath1 = strutils_shell_escape (crpath1); \
1060 g_free (mpath1); \
1061 rpath2 = strutils_shell_escape (crpath2); \
1062 g_free (mpath2); \
1063 g_snprintf (buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
1064 g_free (rpath1); \
1065 g_free (rpath2); \
1066 return fish_send_command(me, super2, buf, OPT_FLUSH); \
1069 /* *INDENT-OFF* */
1070 FISH_OP (rename,
1071 "#RENAME /%s /%s\n"
1072 "if mv /%s /%s 2>/dev/null; then\n"
1073 "echo '### 000'\n"
1074 "else\n"
1075 "echo '### 500'\n"
1076 "fi\n")
1078 FISH_OP (link,
1079 "#LINK /%s /%s\n"
1080 "if ln /%s /%s 2>/dev/null; then\n"
1081 "echo '### 000'\n"
1082 "else\n"
1083 "echo '### 500'\n"
1084 "fi\n")
1085 /* *INDENT-ON* */
1087 static int
1088 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1090 char *qsetto;
1091 PREFIX qsetto = strutils_shell_escape (setto);
1092 /* *INDENT-OFF* */
1093 g_snprintf (buf, sizeof (buf),
1094 "#SYMLINK %s /%s\n"
1095 "if ln -s %s /%s 2>/dev/null; then\n"
1096 "echo '### 000'\n"
1097 "else\n"
1098 "echo '### 500'\n"
1099 "fi\n",
1100 qsetto, rpath, qsetto, rpath);
1101 /* *INDENT-ON* */
1102 g_free (qsetto);
1103 POSTFIX (OPT_FLUSH);
1106 static int
1107 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
1109 char *sowner, *sgroup;
1110 struct passwd *pw;
1111 struct group *gr;
1113 if ((pw = getpwuid (owner)) == NULL)
1114 return 0;
1116 if ((gr = getgrgid (group)) == NULL)
1117 return 0;
1119 sowner = pw->pw_name;
1120 sgroup = gr->gr_name;
1122 PREFIX
1123 /* *INDENT-OFF* */
1124 g_snprintf (buf, sizeof (buf),
1125 "#CHOWN %s:%s /%s\n"
1126 "if chown %s:%s /%s 2>/dev/null; then\n"
1127 "echo '### 000'\n"
1128 "else\n"
1129 "echo '### 500'\n"
1130 "fi\n",
1131 sowner, sgroup, rpath, sowner, sgroup, rpath);
1132 /* *INDENT-ON* */
1133 fish_send_command (me, super, buf, OPT_FLUSH);
1134 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1135 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1136 POSTFIX (OPT_FLUSH)}
1139 static int
1140 fish_unlink (struct vfs_class *me, const char *path)
1142 PREFIX
1144 /* *INDENT-OFF* */
1145 g_snprintf (buf, sizeof (buf),
1146 "#DELE /%s\n"
1147 "if rm -f /%s 2>/dev/null; then\n"
1148 "echo '### 000'\n"
1149 "else\n"
1150 "echo '### 500'\n"
1151 "fi\n",
1152 rpath, rpath);
1153 /* *INDENT-ON* */
1155 POSTFIX (OPT_FLUSH);
1158 static int
1159 fish_exists (struct vfs_class *me, const char *path)
1161 PREFIX
1163 /* *INDENT-OFF* */
1164 g_snprintf (buf, sizeof (buf),
1165 "#ISEXISTS /%s\n"
1166 "ls -l /%s >/dev/null 2>/dev/null\n"
1167 "echo '### '$?\n",
1168 rpath, rpath);
1169 /* *INDENT-ON* */
1171 g_free (rpath);
1173 if (fish_send_command (me, super, buf, OPT_FLUSH) == 0)
1174 return 1;
1176 return 0;
1180 static int
1181 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1183 int ret_code;
1185 PREFIX (void) mode;
1187 /* *INDENT-OFF* */
1188 g_snprintf (buf, sizeof (buf),
1189 "#MKD /%s\n"
1190 "if mkdir /%s 2>/dev/null; then\n"
1191 "echo '### 000'\n"
1192 "else\n"
1193 "echo '### 500'\n"
1194 "fi\n",
1195 rpath, rpath);
1196 /* *INDENT-ON* */
1198 g_free (rpath);
1199 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1201 if (ret_code != 0)
1202 return ret_code;
1204 if (!fish_exists (me, path))
1206 ERRNOR (EACCES, -1);
1208 return 0;
1211 static int
1212 fish_rmdir (struct vfs_class *me, const char *path)
1214 PREFIX
1215 /* *INDENT-OFF* */
1216 g_snprintf (buf, sizeof (buf),
1217 "#RMD /%s\n"
1218 "if rmdir /%s 2>/dev/null; then\n"
1219 "echo '### 000'\n"
1220 "else\n"
1221 "echo '### 500'\n"
1222 "fi\n",
1223 rpath, rpath);
1224 /* *INDENT-ON* */
1225 POSTFIX (OPT_FLUSH);
1228 static int
1229 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
1231 (void) mode;
1233 fh->u.fish.append = 0;
1234 /* File will be written only, so no need to retrieve it */
1235 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1237 fh->u.fish.append = flags & O_APPEND;
1238 if (!fh->ino->localname)
1240 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1241 fh->ino->ent->name);
1242 if (tmp_handle == -1)
1243 return -1;
1244 close (tmp_handle);
1246 return 0;
1248 if (!fh->ino->localname)
1249 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1250 return -1;
1251 if (!fh->ino->localname)
1252 vfs_die ("retrieve_file failed to fill in localname");
1253 return 0;
1256 static void
1257 fish_fill_names (struct vfs_class *me, fill_names_f func)
1259 struct vfs_s_super *super = MEDATA->supers;
1260 char *name;
1262 char gbuf[10];
1264 while (super)
1266 const char *flags = "";
1267 switch (SUP.flags)
1269 case FISH_FLAG_RSH:
1270 flags = ":r";
1271 break;
1272 case FISH_FLAG_COMPRESSED:
1273 flags = ":C";
1274 break;
1275 default:
1276 if (SUP.flags > FISH_FLAG_RSH)
1278 break;
1279 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1280 flags = gbuf;
1282 break;
1285 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1286 (*func) (name);
1287 g_free (name);
1288 super = super->next;
1292 static void *
1293 fish_open (struct vfs_class *me, const char *file, int flags, int mode)
1296 sorry, i've places hack here
1297 cause fish don't able to open files with O_EXCL flag
1299 flags &= ~O_EXCL;
1300 return vfs_s_open (me, file, flags, mode);
1304 void
1305 init_fish (void)
1307 static struct vfs_s_subclass fish_subclass;
1309 tcp_init ();
1311 fish_subclass.flags = VFS_S_REMOTE;
1312 fish_subclass.archive_same = fish_archive_same;
1313 fish_subclass.open_archive = fish_open_archive;
1314 fish_subclass.free_archive = fish_free_archive;
1315 fish_subclass.fh_open = fish_fh_open;
1316 fish_subclass.dir_load = fish_dir_load;
1317 fish_subclass.file_store = fish_file_store;
1318 fish_subclass.linear_start = fish_linear_start;
1319 fish_subclass.linear_read = fish_linear_read;
1320 fish_subclass.linear_close = fish_linear_close;
1322 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1323 vfs_fish_ops.name = "fish";
1324 vfs_fish_ops.prefix = "sh:";
1325 vfs_fish_ops.fill_names = fish_fill_names;
1326 vfs_fish_ops.chmod = fish_chmod;
1327 vfs_fish_ops.chown = fish_chown;
1328 vfs_fish_ops.open = fish_open;
1329 vfs_fish_ops.symlink = fish_symlink;
1330 vfs_fish_ops.link = fish_link;
1331 vfs_fish_ops.unlink = fish_unlink;
1332 vfs_fish_ops.rename = fish_rename;
1333 vfs_fish_ops.mkdir = fish_mkdir;
1334 vfs_fish_ops.rmdir = fish_rmdir;
1335 vfs_fish_ops.ctl = fish_ctl;
1336 vfs_register_class (&vfs_fish_ops);