Ticket #2242 (improved FISH)
[midnight-commander.git] / lib / vfs / mc-vfs / fish.c
blob3abe293b03587acf442ab7c65807d7a5d60eff70
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
9 2010 Andrew Borobin
10 2010 Ilia Maslakov
12 Derived from ftpfs.c.
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU Library General Public License
16 as published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Library General Public License for more details.
24 You should have received a copy of the GNU Library General Public
25 License along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
28 /**
29 * \file
30 * \brief Source: Virtual File System: FISH implementation for transfering files over
31 * shell connections
32 * \author Pavel Machek
33 * \author Michal Svec
34 * \date 1998, 2000
36 * Derived from ftpfs.c
37 * Read README.fish for protocol specification.
39 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
40 * where C means you want compressed connection,
41 * and r means you want to use rsh
43 * Namespace: fish_vfs_ops exported.
46 /* Define this if your ssh can take -I option */
48 #include <config.h>
49 #include <errno.h>
50 #include <fcntl.h>
51 #include <pwd.h>
52 #include <grp.h>
53 #include <sys/time.h> /* gettimeofday() */
54 #include <stdlib.h>
55 #include <string.h>
57 #include "lib/global.h"
58 #include "lib/fs.h"
59 #include "lib/tty/tty.h" /* enable/disable interrupt key */
60 #include "lib/strescape.h"
61 #include "lib/unixcompat.h"
62 #include "lib/fileloc.h"
64 #include "src/wtools.h" /* message() */
65 #include "src/main.h" /* print_vfs_message */
66 #include "utilvfs.h"
67 #include "xdirentry.h"
68 #include "vfs.h"
69 #include "vfs-impl.h"
70 #include "gc.h" /* vfs_stamp_create */
71 #include "netutil.h"
72 #include "fish.h"
73 #include "fishdef.h"
75 int fish_directory_timeout = 900;
77 #define DO_RESOLVE_SYMLINK 1
78 #define DO_OPEN 2
79 #define DO_FREE_RESOURCE 4
81 #define FISH_FLAG_COMPRESSED 1
82 #define FISH_FLAG_RSH 2
84 #define OPT_FLUSH 1
85 #define OPT_IGNORE_ERROR 2
88 * Reply codes.
90 #define PRELIM 1 /* positive preliminary */
91 #define COMPLETE 2 /* positive completion */
92 #define CONTINUE 3 /* positive intermediate */
93 #define TRANSIENT 4 /* transient negative completion */
94 #define ERROR 5 /* permanent negative completion */
96 /* command wait_flag: */
97 #define NONE 0x00
98 #define WAIT_REPLY 0x01
99 #define WANT_STRING 0x02
101 /* environment flags */
102 #define FISH_HAVE_HEAD 1
103 #define FISH_HAVE_SED 2
104 #define FISH_HAVE_AWK 4
105 #define FISH_HAVE_PERL 8
106 #define FISH_HAVE_LSQ 16
107 #define FISH_HAVE_DATE_MDYT 32
109 static char reply_str[80];
111 static struct vfs_class vfs_fish_ops;
114 static char *
115 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
117 char *scr_filename = NULL;
118 char *scr_content;
119 gsize scr_len = 0;
121 /* 1st: scan user directory */
122 scr_filename = g_build_path (PATH_SEP_STR, home_dir, MC_USERCONF_DIR, FISH_PREFIX, hostname,
123 script_name, (char *) NULL);
124 /* silent about user dir */
125 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
126 g_free (scr_filename);
127 /* 2nd: scan system dir */
128 if (scr_content == NULL)
130 g_free (scr_content);
131 scr_filename = g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
132 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
133 g_free (scr_filename);
136 if (scr_content != NULL)
137 return scr_content;
139 g_free (scr_content);
140 return g_strdup (def_content);
143 static int
144 fish_decode_reply (char *s, int was_garbage)
146 int code;
147 if (!sscanf (s, "%d", &code))
149 code = 500;
150 return 5;
152 if (code < 100)
153 return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
154 return code / 100;
157 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
158 static int
159 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
161 char answer[1024];
162 int was_garbage = 0;
164 for (;;)
166 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
168 if (string_buf)
169 *string_buf = 0;
170 return 4;
173 if (strncmp (answer, "### ", 4))
175 was_garbage = 1;
176 if (string_buf)
177 g_strlcpy (string_buf, answer, string_len);
179 else
180 return fish_decode_reply (answer + 4, was_garbage);
184 #define SUP super->u.fish
186 static int
187 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
189 va_list ap;
190 char *str;
191 int status;
192 FILE *logfile = MEDATA->logfile;
194 va_start (ap, fmt);
196 str = g_strdup_vprintf (fmt, ap);
197 va_end (ap);
199 if (logfile)
201 size_t ret;
202 ret = fwrite (str, strlen (str), 1, logfile);
203 ret = fflush (logfile);
206 tty_enable_interrupt_key ();
208 status = write (SUP.sockw, str, strlen (str));
209 g_free (str);
211 tty_disable_interrupt_key ();
212 if (status < 0)
213 return TRANSIENT;
215 if (wait_reply)
216 return fish_get_reply (me, SUP.sockr,
217 (wait_reply & WANT_STRING) ? reply_str :
218 NULL, sizeof (reply_str) - 1);
219 return COMPLETE;
222 static void
223 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
225 if ((SUP.sockw != -1) || (SUP.sockr != -1))
227 print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
228 fish_command (me, super, NONE, "#BYE\nexit\n");
229 close (SUP.sockw);
230 close (SUP.sockr);
231 SUP.sockw = SUP.sockr = -1;
233 g_free (SUP.host);
234 g_free (SUP.user);
235 g_free (SUP.cwdir);
236 g_free (SUP.password);
237 g_free (SUP.scr_ls);
238 g_free (SUP.scr_exists);
239 g_free (SUP.scr_mkdir);
240 g_free (SUP.scr_unlink);
241 g_free (SUP.scr_chown);
242 g_free (SUP.scr_chmod);
243 g_free (SUP.scr_rmdir);
244 g_free (SUP.scr_ln);
245 g_free (SUP.scr_mv);
246 g_free (SUP.scr_hardlink);
247 g_free (SUP.scr_get);
248 g_free (SUP.scr_send);
249 g_free (SUP.scr_append);
250 g_free (SUP.scr_info);
251 g_free (SUP.scr_env);
254 static void
255 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
257 int fileset1[2], fileset2[2];
258 int res;
260 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
261 vfs_die ("Cannot pipe(): %m.");
263 res = fork ();
265 if (res != 0)
267 if (res < 0)
268 vfs_die ("Cannot fork(): %m.");
269 /* We are the parent */
270 close (fileset1[0]);
271 SUP.sockw = fileset1[1];
272 close (fileset2[1]);
273 SUP.sockr = fileset2[0];
275 else
277 res = dup2 (fileset1[0], 0);
278 close (fileset1[0]);
279 close (fileset1[1]);
280 res = dup2 (fileset2[1], 1);
281 close (2);
282 /* stderr to /dev/null */
283 res = open ("/dev/null", O_WRONLY);
284 close (fileset2[0]);
285 close (fileset2[1]);
286 execvp (path, const_cast (char **, argv));
287 _exit (3);
291 static char *
292 fish_set_env (int flags)
294 GString *tmp;
296 tmp = g_string_sized_new (128);
297 g_string_assign (tmp, "export ");
299 if ((flags & FISH_HAVE_HEAD) != 0)
300 g_string_append (tmp, "FISH_HAVE_HEAD=1 ");
302 if ((flags & FISH_HAVE_SED) != 0)
303 g_string_append (tmp, "FISH_HAVE_SED=1 ");
305 if ((flags & FISH_HAVE_AWK) != 0)
306 g_string_append (tmp, "FISH_HAVE_AWK=1 ");
308 if ((flags & FISH_HAVE_PERL) != 0)
309 g_string_append (tmp, "FISH_HAVE_PERL=1 ");
311 if ((flags & FISH_HAVE_LSQ) != 0)
312 g_string_append (tmp, "FISH_HAVE_LSQ=1 ");
314 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
315 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 ");
317 return g_string_free (tmp, FALSE);
320 static gboolean
321 fish_info (struct vfs_class *me, struct vfs_s_super *super)
323 char buffer[8192];
324 if (fish_command (me, super, NONE, SUP.scr_info) == COMPLETE)
326 while (1)
328 int res;
329 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
330 if ((!res) || (res == EINTR))
331 ERRNOR (ECONNRESET, FALSE);
332 if (!strncmp (buffer, "### ", 4))
333 break;
334 SUP.host_flags = atol (buffer);
336 return TRUE;
338 ERRNOR (E_PROTO, FALSE);
342 /* The returned directory should always contain a trailing slash */
343 static char *
344 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
346 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
347 return g_strconcat (reply_str, "/", (char *) NULL);
348 ERRNOR (EIO, NULL);
351 static int
352 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
355 char gbuf[10];
356 const char *argv[10]; /* All of 10 is used now */
357 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
358 int i = 0;
360 argv[i++] = xsh;
361 if (SUP.flags == FISH_FLAG_COMPRESSED)
362 argv[i++] = "-C";
364 if (SUP.flags > FISH_FLAG_RSH)
366 argv[i++] = "-p";
367 g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
368 argv[i++] = gbuf;
372 * Add the user name to the ssh command line only if it was explicitly
373 * set in vfs URL. rsh/ssh will get current user by default
374 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
375 * option breaks it for some)
378 if (SUP.user)
380 argv[i++] = "-l";
381 argv[i++] = SUP.user;
383 else
385 /* The rest of the code assumes it to be a valid username */
386 SUP.user = vfs_get_local_username ();
389 argv[i++] = SUP.host;
390 argv[i++] = "echo FISH:; /bin/sh";
391 argv[i++] = NULL;
393 fish_pipeopen (super, xsh, argv);
396 char answer[2048];
397 print_vfs_message (_("fish: Waiting for initial line..."));
398 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
399 ERRNOR (E_PROTO, -1);
400 print_vfs_message ("%s", answer);
401 if (strstr (answer, "assword"))
403 /* Currently, this does not work. ssh reads passwords from
404 /dev/tty, not from stdin :-(. */
406 message (D_ERROR, MSG_ERROR,
407 _("Sorry, we cannot do password authenticated connections for now."));
408 ERRNOR (EPERM, -1);
409 if (!SUP.password)
411 char *p, *op;
412 p = g_strdup_printf (_("fish: Password is required for %s"), SUP.user);
413 op = vfs_get_password (p);
414 g_free (p);
415 if (op == NULL)
416 ERRNOR (EPERM, -1);
417 SUP.password = op;
420 print_vfs_message (_("fish: Sending password..."));
423 size_t str_len;
424 str_len = strlen (SUP.password);
425 if ((write (SUP.sockw, SUP.password, str_len) != (ssize_t) str_len)
426 || (write (SUP.sockw, "\n", 1) != 1))
428 ERRNOR (EIO, -1);
434 print_vfs_message (_("fish: Sending initial line..."));
436 * Run `start_fish_server'. If it doesn't exist - no problem,
437 * we'll talk directly to the shell.
439 if (fish_command
440 (me, super, WAIT_REPLY,
441 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
442 ERRNOR (E_PROTO, -1);
444 print_vfs_message (_("fish: Handshaking version..."));
445 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
446 ERRNOR (E_PROTO, -1);
448 /* Set up remote locale to C, otherwise dates cannot be recognized */
449 if (fish_command
450 (me, super, WAIT_REPLY,
451 "export LANG=C LC_ALL=C LC_TIME=C\n"
452 "echo '### 200'\n") != COMPLETE)
453 ERRNOR (E_PROTO, -1);
455 print_vfs_message (_("fish: Getting host info..."));
456 if (fish_info (me, super))
457 SUP.scr_env = fish_set_env (SUP.host_flags);
459 print_vfs_message (_("fish: Setting up current directory..."));
460 SUP.cwdir = fish_getcwd (me, super);
461 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
462 #if 0
463 super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
464 #endif
465 super->name = g_strdup (PATH_SEP_STR);
467 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
468 return 0;
471 static int
472 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
473 const char *archive_name, char *op)
475 char *host, *user, *password, *p;
476 int flags;
478 (void) archive_name;
480 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
481 &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
483 g_free (p);
485 SUP.host = host;
486 SUP.user = user;
487 SUP.flags = flags;
488 if (!strncmp (op, "rsh:", 4))
489 SUP.flags = FISH_FLAG_RSH;
490 SUP.cwdir = NULL;
491 if (password)
492 SUP.password = password;
493 SUP.scr_ls = fish_load_script_from_file (host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
494 SUP.scr_exists = fish_load_script_from_file (host, FISH_EXISTS_FILE, FISH_EXISTS_FILE);
495 SUP.scr_mkdir = fish_load_script_from_file (host, FISH_MKDIR_FILE, FISH_MKDIR_FILE);
496 SUP.scr_unlink = fish_load_script_from_file (host, FISH_UNLINK_FILE, FISH_UNLINK_FILE);
497 SUP.scr_chown = fish_load_script_from_file (host, FISH_CHOWN_FILE, FISH_CHOWN_FILE);
498 SUP.scr_chmod = fish_load_script_from_file (host, FISH_CHMOD_FILE, FISH_CHMOD_FILE);
499 SUP.scr_rmdir = fish_load_script_from_file (host, FISH_RMDIR_FILE, FISH_RMDIR_FILE);
500 SUP.scr_ln = fish_load_script_from_file (host, FISH_LN_FILE, FISH_LN_FILE);
501 SUP.scr_mv = fish_load_script_from_file (host, FISH_MV_FILE, FISH_MV_FILE);
502 SUP.scr_hardlink = fish_load_script_from_file (host, FISH_HARDLINK_FILE, FISH_HARDLINK_FILE);
503 SUP.scr_get = fish_load_script_from_file (host, FISH_GET_FILE, FISH_GET_FILE);
504 SUP.scr_send = fish_load_script_from_file (host, FISH_SEND_FILE,FISH_SEND_FILE);
505 SUP.scr_append = fish_load_script_from_file (host, FISH_APPEND_FILE, FISH_APPEND_FILE);
506 SUP.scr_info = fish_load_script_from_file (host, FISH_INFO_FILE, FISH_INFO_FILE);
507 return fish_open_archive_int (me, super);
510 static int
511 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
512 const char *archive_name, char *op, void *cookie)
514 char *host, *user;
515 int flags;
516 int result;
518 (void) me;
519 (void) archive_name;
520 (void) cookie;
522 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
523 URL_NOSLASH | URL_USE_ANONYMOUS);
525 g_free (op);
527 if (user == NULL)
528 user = vfs_get_local_username ();
530 result = ((strcmp (host, SUP.host) == 0)
531 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
533 g_free (host);
534 g_free (user);
536 return result;
539 static int
540 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
542 struct vfs_s_super *super = dir->super;
543 char buffer[8192];
544 struct vfs_s_entry *ent = NULL;
545 FILE *logfile;
546 char *quoted_path;
547 int reply_code;
548 gchar *shell_commands;
551 * Simple FISH debug interface :]
553 #if 0
554 if (!(MEDATA->logfile))
556 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
558 #endif
559 logfile = MEDATA->logfile;
561 print_vfs_message (_("fish: Reading directory %s..."), remote_path);
563 gettimeofday (&dir->timestamp, NULL);
564 dir->timestamp.tv_sec += fish_directory_timeout;
565 quoted_path = strutils_shell_escape (remote_path);
566 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_ls, (char *) NULL);
567 fish_command (me, super, NONE, shell_commands, quoted_path);
568 g_free (shell_commands);
569 g_free (quoted_path);
570 ent = vfs_s_generate_entry (me, NULL, dir, 0);
571 while (1)
573 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
574 if ((!res) || (res == EINTR))
576 vfs_s_free_entry (me, ent);
577 me->verrno = ECONNRESET;
578 goto error;
580 if (logfile)
582 fputs (buffer, logfile);
583 fputs ("\n", logfile);
584 fflush (logfile);
586 if (!strncmp (buffer, "### ", 4))
587 break;
588 if ((!buffer[0]))
590 if (ent->name)
592 vfs_s_insert_entry (me, dir, ent);
593 ent = vfs_s_generate_entry (me, NULL, dir, 0);
595 continue;
598 #define ST ent->ino->st
600 switch (buffer[0])
602 case ':':
604 char *temp;
605 char *data_start = buffer + 1;
606 char *filename = data_start;
607 char *linkname = data_start;
608 char *filename_bound = filename + strlen (filename);
609 char *linkname_bound = filename_bound;
610 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
611 break; /* We'll do "." and ".." ourselves */
613 if (S_ISLNK (ST.st_mode))
615 /* we expect: "escaped-name" -> "escaped-name"
616 // -> cannot occur in filenames,
617 // because it will be escaped to -\> */
619 if (*filename == '"')
620 ++filename;
622 linkname = strstr (filename, "\" -> \"");
623 if (!linkname)
625 /* broken client, or smth goes wrong */
626 linkname = filename_bound;
627 if (filename_bound > filename && *(filename_bound - 1) == '"')
628 --filename_bound; /* skip trailing " */
630 else
632 filename_bound = linkname;
633 linkname += 6; /* strlen ("\" -> \"") */
634 if (*(linkname_bound - 1) == '"')
635 --linkname_bound; /* skip trailing " */
638 ent->name = str_dup_range (filename, filename_bound);
639 temp = ent->name;
640 ent->name = strutils_shell_unescape (ent->name);
641 g_free (temp);
643 ent->ino->linkname = str_dup_range (linkname, linkname_bound);
644 temp = ent->ino->linkname;
645 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
646 g_free (temp);
648 else
650 /* we expect: "escaped-name" */
651 if (filename_bound - filename > 2)
654 there is at least 2 "
655 and we skip them
657 if (*filename == '"')
658 ++filename;
659 if (*(filename_bound - 1) == '"')
660 --filename_bound;
662 ent->name = str_dup_range (filename, filename_bound);
663 temp = ent->name;
664 ent->name = strutils_shell_unescape (ent->name);
665 g_free (temp);
667 break;
669 case 'S':
670 #ifdef HAVE_ATOLL
671 ST.st_size = (off_t) atoll (buffer + 1);
672 #else
673 ST.st_size = (off_t) atof (buffer + 1);
674 #endif
675 break;
676 case 'P':
678 size_t skipped;
679 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
680 break;
682 case 'R':
685 raw filemode:
686 we expect: Roctal-filemode octal-filetype uid.gid
688 size_t skipped;
689 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
690 break;
692 case 'd':
694 vfs_split_text (buffer + 1);
695 if (!vfs_parse_filedate (0, &ST.st_ctime))
696 break;
697 ST.st_atime = ST.st_mtime = ST.st_ctime;
699 break;
700 case 'D':
702 struct tm tim;
703 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
704 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
705 break;
706 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
708 break;
709 case 'E':
711 int maj, min;
712 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
713 break;
714 #ifdef HAVE_STRUCT_STAT_ST_RDEV
715 ST.st_rdev = makedev (maj, min);
716 #endif
721 vfs_s_free_entry (me, ent);
722 reply_code = fish_decode_reply (buffer + 4, 0);
723 if (reply_code == COMPLETE)
725 g_free (SUP.cwdir);
726 SUP.cwdir = g_strdup (remote_path);
727 print_vfs_message (_("%s: done."), me->name);
728 return 0;
730 else if (reply_code == ERROR)
732 me->verrno = EACCES;
734 else
736 me->verrno = E_REMOTE;
739 error:
740 print_vfs_message (_("%s: failure"), me->name);
741 return -1;
744 static int
745 fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
747 gchar *shell_commands = NULL;
748 struct vfs_s_super *super = FH_SUPER;
749 int n, total;
750 char buffer[8192];
751 struct stat s;
752 int was_error = 0;
753 int h;
754 char *quoted_name;
756 h = open (localname, O_RDONLY);
758 if (h == -1)
759 ERRNOR (EIO, -1);
760 if (fstat (h, &s) < 0)
762 close (h);
763 ERRNOR (EIO, -1);
766 /* First, try this as stor:
768 * ( head -c number ) | ( cat > file; cat >/dev/null )
770 * If `head' is not present on the remote system, `dd' will be used.
771 * Unfortunately, we cannot trust most non-GNU `head' implementations
772 * even if `-c' options is supported. Therefore, we separate GNU head
773 * (and other modern heads?) using `-q' and `-' . This causes another
774 * implementations to fail (because of "incorrect options").
776 * Fallback is:
778 * rest=<number>
779 * while [ $rest -gt 0 ]
780 * do
781 * cnt=`expr \( $rest + 255 \) / 256`
782 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
783 * rest=`expr $rest - $n`
784 * done
786 * `dd' was not designed for full filling of input buffers,
787 * and does not report exact number of bytes (not blocks).
788 * Therefore a more complex shell script is needed.
790 * On some systems non-GNU head writes "Usage:" error report to stdout
791 * instead of stderr. It makes impossible the use of "head || dd"
792 * algorithm for file appending case, therefore just "dd" is used for it.
795 quoted_name = strutils_shell_escape (name);
796 print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
798 /* FIXME: File size is limited to ULONG_MAX */
799 if (!fh->u.fish.append)
801 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%lu;\n",
802 SUP.scr_append, (char *) NULL);
803 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name, (unsigned long) s.st_size);
804 g_free (shell_commands);
806 else
808 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%lu;\n",
809 SUP.scr_send, (char *) NULL);
810 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name, (unsigned long) s.st_size);
811 g_free (shell_commands);
813 if (n != PRELIM)
815 close (h);
816 ERRNOR (E_REMOTE, -1);
819 total = 0;
821 while (1)
823 int t;
824 while ((n = read (h, buffer, sizeof (buffer))) < 0)
826 if ((errno == EINTR) && tty_got_interrupt ())
827 continue;
828 print_vfs_message (_("fish: Local read failed, sending zeros"));
829 close (h);
830 h = open ("/dev/zero", O_RDONLY);
833 if (n == 0)
834 break;
836 t = write (SUP.sockw, buffer, n);
837 if (t != n)
839 if (t == -1)
840 me->verrno = errno;
841 else
842 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 gchar *shell_commands = NULL;
866 struct vfs_s_super *super = FH_SUPER;
867 char *name;
868 char *quoted_name;
869 if (offset)
870 ERRNOR (E_NOTSUPP, 0);
871 name = vfs_s_fullpath (me, fh->ino);
872 if (name == NULL)
873 return 0;
874 quoted_name = strutils_shell_escape (name);
875 g_free (name);
876 fh->u.fish.append = 0;
879 * Check whether the remote file is readable by using `dd' to copy
880 * a single byte from the remote file to /dev/null. If `dd' completes
881 * with exit status of 0 use `cat' to send the file contents to the
882 * standard output (i.e. over the network).
885 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_get, (char *) NULL);
886 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name);
887 g_free (shell_commands);
888 g_free (quoted_name);
889 if (offset != PRELIM)
890 ERRNOR (E_REMOTE, 0);
891 fh->linear = LS_LINEAR_OPEN;
892 fh->u.fish.got = 0;
893 errno = 0;
894 #if SIZEOF_OFF_T == SIZEOF_LONG
895 fh->u.fish.total = strtol (reply_str, NULL, 10);
896 #else
897 fh->u.fish.total = strtoll (reply_str, NULL, 10);
898 #endif
899 if (errno != 0)
900 ERRNOR (E_REMOTE, 0);
901 return 1;
904 static void
905 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
907 struct vfs_s_super *super = FH_SUPER;
908 char buffer[8192];
909 int n;
911 print_vfs_message (_("Aborting transfer..."));
914 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
915 if (n != 0)
917 n = read (SUP.sockr, buffer, n);
918 if (n < 0)
919 return;
920 fh->u.fish.got += n;
923 while (n != 0);
925 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
926 print_vfs_message (_("Error reported after abort."));
927 else
928 print_vfs_message (_("Aborted transfer would be successful."));
931 static int
932 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
934 struct vfs_s_super *super = FH_SUPER;
935 int n = 0;
936 len = MIN (fh->u.fish.total - fh->u.fish.got, len);
937 tty_disable_interrupt_key ();
938 while (len && ((n = read (SUP.sockr, buf, len)) < 0))
940 if ((errno == EINTR) && !tty_got_interrupt ())
941 continue;
942 break;
944 tty_enable_interrupt_key ();
946 if (n > 0)
947 fh->u.fish.got += n;
948 else if (n < 0)
949 fish_linear_abort (me, fh);
950 else if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
951 ERRNOR (E_REMOTE, -1);
952 ERRNOR (errno, n);
955 static void
956 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
958 if (fh->u.fish.total != fh->u.fish.got)
959 fish_linear_abort (me, fh);
962 static int
963 fish_ctl (void *fh, int ctlop, void *arg)
965 (void) arg;
966 (void) fh;
967 (void) ctlop;
968 return 0;
969 #if 0
970 switch (ctlop)
972 case VFS_CTL_IS_NOTREADY:
974 int v;
976 if (!FH->linear)
977 vfs_die ("You may not do this");
978 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
979 return 0;
981 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
982 if (((v < 0) && (errno == EINTR)) || v == 0)
983 return 1;
984 return 0;
986 default:
987 return 0;
989 #endif
992 static int
993 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
995 int r;
997 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
998 vfs_stamp_create (&vfs_fish_ops, super);
999 if (r != COMPLETE)
1000 ERRNOR (E_REMOTE, -1);
1001 if (flags & OPT_FLUSH)
1002 vfs_s_invalidate (me, super);
1003 return 0;
1006 #define PREFIX \
1007 char buf[BUF_LARGE]; \
1008 const char *crpath; \
1009 char *rpath, *mpath = g_strdup (path); \
1010 struct vfs_s_super *super; \
1011 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
1012 if (crpath == NULL) \
1014 g_free (mpath); \
1015 return -1; \
1017 rpath = strutils_shell_escape (crpath); \
1018 g_free (mpath);
1020 static int
1021 fish_rename (struct vfs_class *me, const char *path1, const char *path2)
1023 gchar *shell_commands = NULL;
1024 char buf[BUF_LARGE];
1025 const char *crpath1, *crpath2;
1026 char *rpath1, *rpath2, *mpath1, *mpath2;
1027 struct vfs_s_super *super, *super2;
1028 crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super, 0);
1029 if (crpath1 == NULL)
1031 g_free (mpath1);
1032 return -1;
1034 crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0);
1035 if (crpath2 == NULL)
1037 g_free (mpath1);
1038 g_free (mpath2);
1039 return -1;
1041 rpath1 = strutils_shell_escape (crpath1);
1042 g_free (mpath1);
1043 rpath2 = strutils_shell_escape (crpath2);
1044 g_free (mpath2);
1045 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1046 SUP.scr_mv, (char *) NULL);
1047 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1048 g_free (shell_commands);
1049 g_free (rpath1);
1050 g_free (rpath2);
1051 return fish_send_command(me, super2, buf, OPT_FLUSH);
1054 static int
1055 fish_link (struct vfs_class *me, const char *path1, const char *path2)
1057 gchar *shell_commands = NULL;
1058 char buf[BUF_LARGE];
1059 const char *crpath1, *crpath2;
1060 char *rpath1, *rpath2, *mpath1, *mpath2;
1061 struct vfs_s_super *super, *super2;
1062 crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super, 0);
1063 if (crpath1 == NULL)
1065 g_free (mpath1);
1066 return -1;
1068 crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0);
1069 if (crpath2 == NULL)
1071 g_free (mpath1);
1072 g_free (mpath2);
1073 return -1;
1075 rpath1 = strutils_shell_escape (crpath1);
1076 g_free (mpath1);
1077 rpath2 = strutils_shell_escape (crpath2);
1078 g_free (mpath2);
1079 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1080 SUP.scr_hardlink, (char *) NULL);
1081 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1082 g_free (shell_commands);
1083 g_free (rpath1);
1084 g_free (rpath2);
1085 return fish_send_command(me, super2, buf, OPT_FLUSH);
1088 static int
1089 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1091 char *qsetto;
1092 gchar *shell_commands = NULL;
1093 char buf[BUF_LARGE];
1094 const char *crpath;
1095 char *rpath, *mpath = g_strdup (path);
1096 struct vfs_s_super *super;
1097 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0);
1098 if (crpath == NULL)
1100 g_free (mpath);
1101 return -1;
1103 rpath = strutils_shell_escape (crpath);
1104 g_free (mpath);
1106 qsetto = strutils_shell_escape (setto);
1107 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1108 SUP.scr_ln, (char *) NULL);
1109 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1110 g_free (shell_commands);
1111 g_free (qsetto);
1112 g_free (rpath);
1113 return fish_send_command (me, super, buf, OPT_FLUSH);
1116 static int
1117 fish_chmod (struct vfs_class *me, const char *path, int mode)
1119 gchar *shell_commands = NULL;
1120 PREFIX
1122 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1123 SUP.scr_chmod, (char *) NULL);
1124 g_snprintf (buf, sizeof (buf), shell_commands, rpath, mode & 07777);
1125 g_free (shell_commands);
1126 g_free (rpath);
1127 return fish_send_command (me, super, buf, OPT_FLUSH);
1130 static int
1131 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
1133 char *sowner, *sgroup;
1134 struct passwd *pw;
1135 struct group *gr;
1137 pw = getpwuid (owner);
1138 if (pw == NULL)
1139 return 0;
1141 gr = getgrgid (group);
1142 if (gr == NULL)
1143 return 0;
1145 sowner = pw->pw_name;
1146 sgroup = gr->gr_name;
1148 gchar *shell_commands = NULL;
1150 PREFIX
1152 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1153 SUP.scr_chown, (char *) NULL);
1154 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1155 g_free (shell_commands);
1156 fish_send_command (me, super, buf, OPT_FLUSH);
1157 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1158 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1159 g_free (rpath);
1160 return fish_send_command (me, super, buf, OPT_FLUSH);
1164 static int
1165 fish_unlink (struct vfs_class *me, const char *path)
1167 gchar *shell_commands = NULL;
1168 PREFIX
1170 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_unlink, (char *) NULL);
1171 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1172 g_free (shell_commands);
1173 g_free (rpath);
1174 return fish_send_command (me, super, buf, OPT_FLUSH);
1177 static int
1178 fish_exists (struct vfs_class *me, const char *path)
1180 gchar *shell_commands = NULL;
1181 PREFIX
1183 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_exists, (char *) NULL);
1184 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1185 g_free (shell_commands);
1186 g_free (rpath);
1188 return (fish_send_command (me, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1192 static int
1193 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1195 gchar *shell_commands = NULL;
1196 int ret_code;
1198 PREFIX (void) mode;
1200 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_mkdir, (char *) NULL);
1201 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1202 g_free (shell_commands);
1204 g_free (rpath);
1205 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1207 if (ret_code != 0)
1208 return ret_code;
1210 if (!fish_exists (me, path))
1212 ERRNOR (EACCES, -1);
1214 return 0;
1217 static int
1218 fish_rmdir (struct vfs_class *me, const char *path)
1220 gchar *shell_commands = NULL;
1221 PREFIX
1223 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_rmdir, (char *) NULL);
1224 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1225 g_free (shell_commands);
1226 g_free (rpath);
1227 return fish_send_command (me, super, buf, OPT_FLUSH);
1230 static int
1231 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, int mode)
1233 (void) mode;
1235 fh->u.fish.append = 0;
1236 /* File will be written only, so no need to retrieve it */
1237 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1239 fh->u.fish.append = flags & O_APPEND;
1240 if (!fh->ino->localname)
1242 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1243 fh->ino->ent->name);
1244 if (tmp_handle == -1)
1245 return -1;
1246 close (tmp_handle);
1248 return 0;
1250 if (!fh->ino->localname)
1251 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1252 return -1;
1253 if (!fh->ino->localname)
1254 vfs_die ("retrieve_file failed to fill in localname");
1255 return 0;
1258 static void
1259 fish_fill_names (struct vfs_class *me, fill_names_f func)
1261 struct vfs_s_super *super = MEDATA->supers;
1262 char *name;
1264 char gbuf[10];
1266 while (super)
1268 const char *flags = "";
1269 switch (SUP.flags)
1271 case FISH_FLAG_RSH:
1272 flags = ":r";
1273 break;
1274 case FISH_FLAG_COMPRESSED:
1275 flags = ":C";
1276 break;
1277 default:
1278 if (SUP.flags > FISH_FLAG_RSH)
1280 break;
1281 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1282 flags = gbuf;
1284 break;
1287 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1288 (*func) (name);
1289 g_free (name);
1290 super = super->next;
1294 static void *
1295 fish_open (struct vfs_class *me, const char *file, int flags, int mode)
1298 sorry, i've places hack here
1299 cause fish don't able to open files with O_EXCL flag
1301 flags &= ~O_EXCL;
1302 return vfs_s_open (me, file, flags, mode);
1306 void
1307 init_fish (void)
1309 static struct vfs_s_subclass fish_subclass;
1311 tcp_init ();
1313 fish_subclass.flags = VFS_S_REMOTE;
1314 fish_subclass.archive_same = fish_archive_same;
1315 fish_subclass.open_archive = fish_open_archive;
1316 fish_subclass.free_archive = fish_free_archive;
1317 fish_subclass.fh_open = fish_fh_open;
1318 fish_subclass.dir_load = fish_dir_load;
1319 fish_subclass.file_store = fish_file_store;
1320 fish_subclass.linear_start = fish_linear_start;
1321 fish_subclass.linear_read = fish_linear_read;
1322 fish_subclass.linear_close = fish_linear_close;
1324 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1325 vfs_fish_ops.name = "fish";
1326 vfs_fish_ops.prefix = "sh:";
1327 vfs_fish_ops.fill_names = fish_fill_names;
1328 vfs_fish_ops.chmod = fish_chmod;
1329 vfs_fish_ops.chown = fish_chown;
1330 vfs_fish_ops.open = fish_open;
1331 vfs_fish_ops.symlink = fish_symlink;
1332 vfs_fish_ops.link = fish_link;
1333 vfs_fish_ops.unlink = fish_unlink;
1334 vfs_fish_ops.rename = fish_rename;
1335 vfs_fish_ops.mkdir = fish_mkdir;
1336 vfs_fish_ops.rmdir = fish_rmdir;
1337 vfs_fish_ops.ctl = fish_ctl;
1338 vfs_register_class (&vfs_fish_ops);