Ticket #2447: code cleanup before 4.7.5 release.
[pantumic.git] / lib / vfs / mc-vfs / fish.c
blob25a64211d996c3abe9dee5c285564fd04f86e702
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 Borodin
10 2010 Slava Zanko
11 2010 Ilia Maslakov
13 Derived from ftpfs.c.
15 This program is free software; you can redistribute it and/or
16 modify it under the terms of the GNU Library General Public License
17 as published by the Free Software Foundation; either version 2 of
18 the License, or (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU Library General Public License for more details.
25 You should have received a copy of the GNU Library General Public
26 License along with this program; if not, write to the Free Software
27 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 /**
30 * \file
31 * \brief Source: Virtual File System: FISH implementation for transfering files over
32 * shell connections
33 * \author Pavel Machek
34 * \author Michal Svec
35 * \date 1998, 2000
37 * Derived from ftpfs.c
38 * Read README.fish for protocol specification.
40 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
41 * where C means you want compressed connection,
42 * and r means you want to use rsh
44 * Namespace: fish_vfs_ops exported.
47 /* Define this if your ssh can take -I option */
49 #include <config.h>
50 #include <errno.h>
51 #include <fcntl.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/time.h> /* gettimeofday() */
55 #include <stdlib.h>
56 #include <string.h>
57 #include <inttypes.h> /* uintmax_t */
59 #include "lib/global.h"
60 #include "lib/tty/tty.h" /* enable/disable interrupt key */
61 #include "lib/strescape.h"
62 #include "lib/unixcompat.h"
63 #include "lib/fileloc.h"
65 #include "src/filemanager/layout.h" /* print_vfs_message */
66 #include "src/execute.h" /* pre_exec, post_exec */
68 #include "vfs-impl.h"
69 #include "utilvfs.h"
70 #include "netutil.h"
71 #include "xdirentry.h"
72 #include "gc.h" /* vfs_stamp_create */
74 #include "fish.h"
75 #include "fishdef.h"
77 /*** global variables ****************************************************************************/
79 int fish_directory_timeout = 900;
81 /*** file scope macro definitions ****************************************************************/
83 #define DO_RESOLVE_SYMLINK 1
84 #define DO_OPEN 2
85 #define DO_FREE_RESOURCE 4
87 #define FISH_FLAG_COMPRESSED 1
88 #define FISH_FLAG_RSH 2
90 #define OPT_FLUSH 1
91 #define OPT_IGNORE_ERROR 2
94 * Reply codes.
96 #define PRELIM 1 /* positive preliminary */
97 #define COMPLETE 2 /* positive completion */
98 #define CONTINUE 3 /* positive intermediate */
99 #define TRANSIENT 4 /* transient negative completion */
100 #define ERROR 5 /* permanent negative completion */
102 /* command wait_flag: */
103 #define NONE 0x00
104 #define WAIT_REPLY 0x01
105 #define WANT_STRING 0x02
107 /* environment flags */
108 #define FISH_HAVE_HEAD 1
109 #define FISH_HAVE_SED 2
110 #define FISH_HAVE_AWK 4
111 #define FISH_HAVE_PERL 8
112 #define FISH_HAVE_LSQ 16
113 #define FISH_HAVE_DATE_MDYT 32
114 #define FISH_HAVE_TAIL 64
116 #define SUP super->u.fish
118 #define PREFIX \
119 char buf[BUF_LARGE]; \
120 const char *crpath; \
121 char *rpath, *mpath; \
122 struct vfs_s_super *super; \
123 mpath = g_strdup (path); \
124 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0); \
125 if (crpath == NULL) \
127 g_free (mpath); \
128 return -1; \
130 rpath = strutils_shell_escape (crpath); \
131 g_free (mpath)
133 /*** file scope type declarations ****************************************************************/
135 /*** file scope variables ************************************************************************/
137 static char reply_str[80];
139 static struct vfs_class vfs_fish_ops;
141 /*** file scope functions ************************************************************************/
142 /* --------------------------------------------------------------------------------------------- */
144 static char *
145 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
147 char *scr_filename = NULL;
148 char *scr_content;
149 gsize scr_len = 0;
151 /* 1st: scan user directory */
152 scr_filename = g_build_path (PATH_SEP_STR, home_dir, MC_USERCONF_DIR, FISH_PREFIX, hostname,
153 script_name, (char *) NULL);
154 /* silent about user dir */
155 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
156 g_free (scr_filename);
157 /* 2nd: scan system dir */
158 if (scr_content == NULL)
160 scr_filename =
161 g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
162 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
163 g_free (scr_filename);
166 if (scr_content != NULL)
167 return scr_content;
169 return g_strdup (def_content);
172 /* --------------------------------------------------------------------------------------------- */
174 static int
175 fish_decode_reply (char *s, int was_garbage)
177 int code;
178 if (!sscanf (s, "%d", &code))
180 code = 500;
181 return 5;
183 if (code < 100)
184 return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
185 return code / 100;
188 /* --------------------------------------------------------------------------------------------- */
189 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
191 static int
192 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
194 char answer[1024];
195 int was_garbage = 0;
197 for (;;)
199 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
201 if (string_buf)
202 *string_buf = 0;
203 return 4;
206 if (strncmp (answer, "### ", 4))
208 was_garbage = 1;
209 if (string_buf)
210 g_strlcpy (string_buf, answer, string_len);
212 else
213 return fish_decode_reply (answer + 4, was_garbage);
217 /* --------------------------------------------------------------------------------------------- */
219 static int
220 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
222 va_list ap;
223 char *str;
224 int status;
225 FILE *logfile = MEDATA->logfile;
227 va_start (ap, fmt);
229 str = g_strdup_vprintf (fmt, ap);
230 va_end (ap);
232 if (logfile)
234 size_t ret;
235 ret = fwrite (str, strlen (str), 1, logfile);
236 ret = fflush (logfile);
239 tty_enable_interrupt_key ();
241 status = write (SUP.sockw, str, strlen (str));
242 g_free (str);
244 tty_disable_interrupt_key ();
245 if (status < 0)
246 return TRANSIENT;
248 if (wait_reply)
249 return fish_get_reply (me, SUP.sockr,
250 (wait_reply & WANT_STRING) ? reply_str :
251 NULL, sizeof (reply_str) - 1);
252 return COMPLETE;
255 /* --------------------------------------------------------------------------------------------- */
257 static void
258 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
260 if ((SUP.sockw != -1) || (SUP.sockr != -1))
262 print_vfs_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
263 fish_command (me, super, NONE, "#BYE\nexit\n");
264 close (SUP.sockw);
265 close (SUP.sockr);
266 SUP.sockw = SUP.sockr = -1;
268 g_free (SUP.host);
269 g_free (SUP.user);
270 g_free (SUP.cwdir);
271 g_free (SUP.password);
272 g_free (SUP.scr_ls);
273 g_free (SUP.scr_exists);
274 g_free (SUP.scr_mkdir);
275 g_free (SUP.scr_unlink);
276 g_free (SUP.scr_chown);
277 g_free (SUP.scr_chmod);
278 g_free (SUP.scr_rmdir);
279 g_free (SUP.scr_ln);
280 g_free (SUP.scr_mv);
281 g_free (SUP.scr_hardlink);
282 g_free (SUP.scr_get);
283 g_free (SUP.scr_send);
284 g_free (SUP.scr_append);
285 g_free (SUP.scr_info);
286 g_free (SUP.scr_env);
289 /* --------------------------------------------------------------------------------------------- */
291 static void
292 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
294 int fileset1[2], fileset2[2];
295 int res;
297 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
298 vfs_die ("Cannot pipe(): %m.");
300 res = fork ();
302 if (res != 0)
304 if (res < 0)
305 vfs_die ("Cannot fork(): %m.");
306 /* We are the parent */
307 close (fileset1[0]);
308 SUP.sockw = fileset1[1];
309 close (fileset2[1]);
310 SUP.sockr = fileset2[0];
312 else
314 res = dup2 (fileset1[0], 0);
315 close (fileset1[0]);
316 close (fileset1[1]);
317 res = dup2 (fileset2[1], 1);
318 close (2);
319 /* stderr to /dev/null */
320 res = open ("/dev/null", O_WRONLY);
321 close (fileset2[0]);
322 close (fileset2[1]);
323 execvp (path, const_cast (char **, argv));
324 _exit (3);
328 /* --------------------------------------------------------------------------------------------- */
330 static char *
331 fish_set_env (int flags)
333 GString *tmp;
335 tmp = g_string_sized_new (250);
336 g_string_assign (tmp, "");
338 if ((flags & FISH_HAVE_HEAD) != 0)
339 g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
341 if ((flags & FISH_HAVE_SED) != 0)
342 g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
344 if ((flags & FISH_HAVE_AWK) != 0)
345 g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
347 if ((flags & FISH_HAVE_PERL) != 0)
348 g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
350 if ((flags & FISH_HAVE_LSQ) != 0)
351 g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
353 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
354 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
356 if ((flags & FISH_HAVE_TAIL) != 0)
357 g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
359 return g_string_free (tmp, FALSE);
362 /* --------------------------------------------------------------------------------------------- */
364 static gboolean
365 fish_info (struct vfs_class *me, struct vfs_s_super *super)
367 char buffer[8192];
368 if (fish_command (me, super, NONE, SUP.scr_info) == COMPLETE)
370 while (1)
372 int res;
373 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
374 if ((!res) || (res == EINTR))
375 ERRNOR (ECONNRESET, FALSE);
376 if (!strncmp (buffer, "### ", 4))
377 break;
378 SUP.host_flags = atol (buffer);
380 return TRUE;
382 ERRNOR (E_PROTO, FALSE);
386 /* --------------------------------------------------------------------------------------------- */
387 /* The returned directory should always contain a trailing slash */
389 static char *
390 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
392 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
393 return g_strconcat (reply_str, "/", (char *) NULL);
394 ERRNOR (EIO, NULL);
398 /* --------------------------------------------------------------------------------------------- */
400 static void
401 fish_open_archive_pipeopen (struct vfs_s_super *super)
403 char gbuf[10];
404 const char *argv[10]; /* All of 10 is used now */
405 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
406 int i = 0;
408 argv[i++] = xsh;
409 if (SUP.flags == FISH_FLAG_COMPRESSED)
410 argv[i++] = "-C";
412 if (SUP.flags > FISH_FLAG_RSH)
414 argv[i++] = "-p";
415 g_snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
416 argv[i++] = gbuf;
420 * Add the user name to the ssh command line only if it was explicitly
421 * set in vfs URL. rsh/ssh will get current user by default
422 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
423 * option breaks it for some)
426 if (SUP.user)
428 argv[i++] = "-l";
429 argv[i++] = SUP.user;
431 else
433 /* The rest of the code assumes it to be a valid username */
434 SUP.user = vfs_get_local_username ();
437 argv[i++] = SUP.host;
438 argv[i++] = "echo FISH:; /bin/sh";
439 argv[i++] = NULL;
441 fish_pipeopen (super, xsh, argv);
444 /* --------------------------------------------------------------------------------------------- */
446 static gboolean
447 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
449 char answer[2048];
451 printf ("\n%s\n", _("fish: Waiting for initial line..."));
453 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
454 return FALSE;
456 if (strstr (answer, "assword"))
458 /* Currently, this does not work. ssh reads passwords from
459 /dev/tty, not from stdin :-(. */
461 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
463 return FALSE;
464 #if 0
465 if (!SUP.password)
467 char *p, *op;
468 p = g_strdup_printf (_("fish: Password is required for %s"), SUP.user);
469 op = vfs_get_password (p);
470 g_free (p);
471 if (op == NULL)
472 return FALSE;
473 SUP.password = op;
475 printf ("\n%s\n", _("fish: Sending password..."));
477 size_t str_len;
478 str_len = strlen (SUP.password);
479 if ((write (SUP.sockw, SUP.password, str_len) != (ssize_t) str_len)
480 || (write (SUP.sockw, "\n", 1) != 1))
482 return FALSE;
485 #endif
487 return TRUE;
490 /* --------------------------------------------------------------------------------------------- */
492 static int
493 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
495 gboolean ftalk;
496 /* hide panels */
497 pre_exec ();
499 /* open pipe */
500 fish_open_archive_pipeopen (super);
502 /* Start talk with ssh-server (password prompt, etc ) */
503 ftalk = fish_open_archive_talk (me, super);
505 /* show panels */
506 post_exec ();
508 if (!ftalk)
509 ERRNOR (E_PROTO, -1);
511 print_vfs_message (_("fish: Sending initial line..."));
513 * Run `start_fish_server'. If it doesn't exist - no problem,
514 * we'll talk directly to the shell.
517 if (fish_command
518 (me, super, WAIT_REPLY,
519 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
520 ERRNOR (E_PROTO, -1);
522 print_vfs_message (_("fish: Handshaking version..."));
523 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
524 ERRNOR (E_PROTO, -1);
526 /* Set up remote locale to C, otherwise dates cannot be recognized */
527 if (fish_command
528 (me, super, WAIT_REPLY,
529 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
530 ERRNOR (E_PROTO, -1);
532 print_vfs_message (_("fish: Getting host info..."));
533 if (fish_info (me, super))
534 SUP.scr_env = fish_set_env (SUP.host_flags);
536 print_vfs_message (_("fish: Setting up current directory..."));
537 SUP.cwdir = fish_getcwd (me, super);
538 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
539 #if 0
540 super->name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
541 #endif
542 super->name = g_strdup (PATH_SEP_STR);
544 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
545 return 0;
548 /* --------------------------------------------------------------------------------------------- */
550 static int
551 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
552 const char *archive_name, char *op)
554 char *host, *user, *password, *p;
555 int flags;
557 (void) archive_name;
559 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
560 &password, 0, URL_NOSLASH | URL_USE_ANONYMOUS);
562 g_free (p);
564 SUP.host = host;
565 SUP.user = user;
566 SUP.flags = flags;
567 if (!strncmp (op, "rsh:", 4))
568 SUP.flags = FISH_FLAG_RSH;
569 SUP.cwdir = NULL;
570 if (password)
571 SUP.password = password;
572 SUP.scr_ls = fish_load_script_from_file (host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
573 SUP.scr_exists = fish_load_script_from_file (host, FISH_EXISTS_FILE, FISH_EXISTS_DEF_CONTENT);
574 SUP.scr_mkdir = fish_load_script_from_file (host, FISH_MKDIR_FILE, FISH_MKDIR_DEF_CONTENT);
575 SUP.scr_unlink = fish_load_script_from_file (host, FISH_UNLINK_FILE, FISH_UNLINK_DEF_CONTENT);
576 SUP.scr_chown = fish_load_script_from_file (host, FISH_CHOWN_FILE, FISH_CHOWN_DEF_CONTENT);
577 SUP.scr_chmod = fish_load_script_from_file (host, FISH_CHMOD_FILE, FISH_CHMOD_DEF_CONTENT);
578 SUP.scr_rmdir = fish_load_script_from_file (host, FISH_RMDIR_FILE, FISH_RMDIR_DEF_CONTENT);
579 SUP.scr_ln = fish_load_script_from_file (host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
580 SUP.scr_mv = fish_load_script_from_file (host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
581 SUP.scr_hardlink =
582 fish_load_script_from_file (host, FISH_HARDLINK_FILE, FISH_HARDLINK_DEF_CONTENT);
583 SUP.scr_get = fish_load_script_from_file (host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
584 SUP.scr_send = fish_load_script_from_file (host, FISH_SEND_FILE, FISH_SEND_DEF_CONTENT);
585 SUP.scr_append = fish_load_script_from_file (host, FISH_APPEND_FILE, FISH_APPEND_DEF_CONTENT);
586 SUP.scr_info = fish_load_script_from_file (host, FISH_INFO_FILE, FISH_INFO_DEF_CONTENT);
587 return fish_open_archive_int (me, super);
590 /* --------------------------------------------------------------------------------------------- */
592 static int
593 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
594 const char *archive_name, char *op, void *cookie)
596 char *host, *user;
597 int flags;
598 int result;
600 (void) me;
601 (void) archive_name;
602 (void) cookie;
604 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
605 URL_NOSLASH | URL_USE_ANONYMOUS);
607 g_free (op);
609 if (user == NULL)
610 user = vfs_get_local_username ();
612 result = ((strcmp (host, SUP.host) == 0)
613 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
615 g_free (host);
616 g_free (user);
618 return result;
621 /* --------------------------------------------------------------------------------------------- */
623 static int
624 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
626 struct vfs_s_super *super = dir->super;
627 char buffer[8192];
628 struct vfs_s_entry *ent = NULL;
629 FILE *logfile;
630 char *quoted_path;
631 int reply_code;
632 gchar *shell_commands;
635 * Simple FISH debug interface :]
637 #if 0
638 if (!(MEDATA->logfile))
640 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
642 #endif
643 logfile = MEDATA->logfile;
645 print_vfs_message (_("fish: Reading directory %s..."), remote_path);
647 gettimeofday (&dir->timestamp, NULL);
648 dir->timestamp.tv_sec += fish_directory_timeout;
649 quoted_path = strutils_shell_escape (remote_path);
650 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_ls, (char *) NULL);
651 fish_command (me, super, NONE, shell_commands, quoted_path);
652 g_free (shell_commands);
653 g_free (quoted_path);
654 ent = vfs_s_generate_entry (me, NULL, dir, 0);
655 while (1)
657 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
658 if ((!res) || (res == EINTR))
660 vfs_s_free_entry (me, ent);
661 me->verrno = ECONNRESET;
662 goto error;
664 if (logfile)
666 fputs (buffer, logfile);
667 fputs ("\n", logfile);
668 fflush (logfile);
670 if (!strncmp (buffer, "### ", 4))
671 break;
672 if ((!buffer[0]))
674 if (ent->name)
676 vfs_s_insert_entry (me, dir, ent);
677 ent = vfs_s_generate_entry (me, NULL, dir, 0);
679 continue;
682 #define ST ent->ino->st
684 switch (buffer[0])
686 case ':':
688 char *temp;
689 char *data_start = buffer + 1;
690 char *filename = data_start;
691 char *linkname = data_start;
692 char *filename_bound = filename + strlen (filename);
693 char *linkname_bound = filename_bound;
694 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
695 break; /* We'll do "." and ".." ourselves */
697 if (S_ISLNK (ST.st_mode))
699 /* we expect: "escaped-name" -> "escaped-name"
700 // -> cannot occur in filenames,
701 // because it will be escaped to -\> */
703 if (*filename == '"')
704 ++filename;
706 linkname = strstr (filename, "\" -> \"");
707 if (!linkname)
709 /* broken client, or smth goes wrong */
710 linkname = filename_bound;
711 if (filename_bound > filename && *(filename_bound - 1) == '"')
712 --filename_bound; /* skip trailing " */
714 else
716 filename_bound = linkname;
717 linkname += 6; /* strlen ("\" -> \"") */
718 if (*(linkname_bound - 1) == '"')
719 --linkname_bound; /* skip trailing " */
722 ent->name = g_strndup (filename, filename_bound - filename);
723 temp = ent->name;
724 ent->name = strutils_shell_unescape (ent->name);
725 g_free (temp);
727 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
728 temp = ent->ino->linkname;
729 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
730 g_free (temp);
732 else
734 /* we expect: "escaped-name" */
735 if (filename_bound - filename > 2)
738 there is at least 2 "
739 and we skip them
741 if (*filename == '"')
742 ++filename;
743 if (*(filename_bound - 1) == '"')
744 --filename_bound;
746 ent->name = g_strndup (filename, filename_bound - filename);
747 temp = ent->name;
748 ent->name = strutils_shell_unescape (ent->name);
749 g_free (temp);
751 break;
753 case 'S':
754 #ifdef HAVE_ATOLL
755 ST.st_size = (off_t) atoll (buffer + 1);
756 #else
757 ST.st_size = (off_t) atof (buffer + 1);
758 #endif
759 break;
760 case 'P':
762 size_t skipped;
763 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
764 break;
766 case 'R':
769 raw filemode:
770 we expect: Roctal-filemode octal-filetype uid.gid
772 size_t skipped;
773 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
774 break;
776 case 'd':
778 vfs_split_text (buffer + 1);
779 if (!vfs_parse_filedate (0, &ST.st_ctime))
780 break;
781 ST.st_atime = ST.st_mtime = ST.st_ctime;
783 break;
784 case 'D':
786 struct tm tim;
787 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
788 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
789 break;
790 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
792 break;
793 case 'E':
795 int maj, min;
796 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
797 break;
798 #ifdef HAVE_STRUCT_STAT_ST_RDEV
799 ST.st_rdev = makedev (maj, min);
800 #endif
805 vfs_s_free_entry (me, ent);
806 reply_code = fish_decode_reply (buffer + 4, 0);
807 if (reply_code == COMPLETE)
809 g_free (SUP.cwdir);
810 SUP.cwdir = g_strdup (remote_path);
811 print_vfs_message (_("%s: done."), me->name);
812 return 0;
814 else if (reply_code == ERROR)
816 me->verrno = EACCES;
818 else
820 me->verrno = E_REMOTE;
823 error:
824 print_vfs_message (_("%s: failure"), me->name);
825 return -1;
828 /* --------------------------------------------------------------------------------------------- */
830 static int
831 fish_file_store (struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
833 gchar *shell_commands = NULL;
834 struct vfs_s_super *super = FH_SUPER;
835 int n, total;
836 char buffer[8192];
837 struct stat s;
838 int was_error = 0;
839 int h;
840 char *quoted_name;
842 h = open (localname, O_RDONLY);
844 if (h == -1)
845 ERRNOR (EIO, -1);
846 if (fstat (h, &s) < 0)
848 close (h);
849 ERRNOR (EIO, -1);
852 /* First, try this as stor:
854 * ( head -c number ) | ( cat > file; cat >/dev/null )
856 * If `head' is not present on the remote system, `dd' will be used.
857 * Unfortunately, we cannot trust most non-GNU `head' implementations
858 * even if `-c' options is supported. Therefore, we separate GNU head
859 * (and other modern heads?) using `-q' and `-' . This causes another
860 * implementations to fail (because of "incorrect options").
862 * Fallback is:
864 * rest=<number>
865 * while [ $rest -gt 0 ]
866 * do
867 * cnt=`expr \( $rest + 255 \) / 256`
868 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
869 * rest=`expr $rest - $n`
870 * done
872 * `dd' was not designed for full filling of input buffers,
873 * and does not report exact number of bytes (not blocks).
874 * Therefore a more complex shell script is needed.
876 * On some systems non-GNU head writes "Usage:" error report to stdout
877 * instead of stderr. It makes impossible the use of "head || dd"
878 * algorithm for file appending case, therefore just "dd" is used for it.
881 quoted_name = strutils_shell_escape (name);
882 print_vfs_message (_("fish: store %s: sending command..."), quoted_name);
884 /* FIXME: File size is limited to ULONG_MAX */
885 if (!fh->u.fish.append)
887 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
888 SUP.scr_append, (char *) NULL);
889 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
890 (uintmax_t) s.st_size);
891 g_free (shell_commands);
893 else
895 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
896 SUP.scr_send, (char *) NULL);
897 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
898 (uintmax_t) s.st_size);
899 g_free (shell_commands);
901 if (n != PRELIM)
903 close (h);
904 ERRNOR (E_REMOTE, -1);
907 total = 0;
909 while (1)
911 int t;
912 while ((n = read (h, buffer, sizeof (buffer))) < 0)
914 if ((errno == EINTR) && tty_got_interrupt ())
915 continue;
916 print_vfs_message (_("fish: Local read failed, sending zeros"));
917 close (h);
918 h = open ("/dev/zero", O_RDONLY);
921 if (n == 0)
922 break;
924 t = write (SUP.sockw, buffer, n);
925 if (t != n)
927 if (t == -1)
928 me->verrno = errno;
929 else
930 me->verrno = EIO;
931 goto error_return;
933 tty_disable_interrupt_key ();
934 total += n;
935 print_vfs_message ("%s: %d/%" PRIuMAX,
936 was_error ? _("fish: storing zeros") : _("fish: storing file"),
937 total, (uintmax_t) s.st_size);
939 close (h);
940 g_free (quoted_name);
941 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
942 ERRNOR (E_REMOTE, -1);
943 return 0;
944 error_return:
945 close (h);
946 fish_get_reply (me, SUP.sockr, NULL, 0);
947 g_free (quoted_name);
948 return -1;
951 /* --------------------------------------------------------------------------------------------- */
953 static int
954 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
956 gchar *shell_commands = NULL;
957 struct vfs_s_super *super = FH_SUPER;
958 char *name;
959 char *quoted_name;
961 name = vfs_s_fullpath (me, fh->ino);
962 if (name == NULL)
963 return 0;
964 quoted_name = strutils_shell_escape (name);
965 g_free (name);
966 fh->u.fish.append = 0;
969 * Check whether the remote file is readable by using `dd' to copy
970 * a single byte from the remote file to /dev/null. If `dd' completes
971 * with exit status of 0 use `cat' to send the file contents to the
972 * standard output (i.e. over the network).
975 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
976 SUP.scr_get, (char *) NULL);
977 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
978 g_free (shell_commands);
979 g_free (quoted_name);
980 if (offset != PRELIM)
981 ERRNOR (E_REMOTE, 0);
982 fh->linear = LS_LINEAR_OPEN;
983 fh->u.fish.got = 0;
984 errno = 0;
985 #if SIZEOF_OFF_T == SIZEOF_LONG
986 fh->u.fish.total = (off_t) strtol (reply_str, NULL, 10);
987 #else
988 fh->u.fish.total = (off_t) strtoll (reply_str, NULL, 10);
989 #endif
990 if (errno != 0)
991 ERRNOR (E_REMOTE, 0);
992 return 1;
995 /* --------------------------------------------------------------------------------------------- */
997 static void
998 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
1000 struct vfs_s_super *super = FH_SUPER;
1001 char buffer[8192];
1002 int n;
1004 print_vfs_message (_("Aborting transfer..."));
1007 n = MIN (8192, fh->u.fish.total - fh->u.fish.got);
1008 if (n != 0)
1010 n = read (SUP.sockr, buffer, n);
1011 if (n < 0)
1012 return;
1013 fh->u.fish.got += n;
1016 while (n != 0);
1018 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
1019 print_vfs_message (_("Error reported after abort."));
1020 else
1021 print_vfs_message (_("Aborted transfer would be successful."));
1024 /* --------------------------------------------------------------------------------------------- */
1026 static int
1027 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, size_t len)
1029 struct vfs_s_super *super = FH_SUPER;
1030 ssize_t n = 0;
1031 len = MIN ((size_t) (fh->u.fish.total - fh->u.fish.got), len);
1032 tty_disable_interrupt_key ();
1033 while (len != 0 && ((n = read (SUP.sockr, buf, len)) < 0))
1035 if ((errno == EINTR) && !tty_got_interrupt ())
1036 continue;
1037 break;
1039 tty_enable_interrupt_key ();
1041 if (n > 0)
1042 fh->u.fish.got += n;
1043 else if (n < 0)
1044 fish_linear_abort (me, fh);
1045 else if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
1046 ERRNOR (E_REMOTE, -1);
1047 ERRNOR (errno, n);
1050 /* --------------------------------------------------------------------------------------------- */
1052 static void
1053 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
1055 if (fh->u.fish.total != fh->u.fish.got)
1056 fish_linear_abort (me, fh);
1059 /* --------------------------------------------------------------------------------------------- */
1061 static int
1062 fish_ctl (void *fh, int ctlop, void *arg)
1064 (void) arg;
1065 (void) fh;
1066 (void) ctlop;
1067 return 0;
1068 #if 0
1069 switch (ctlop)
1071 case VFS_CTL_IS_NOTREADY:
1073 int v;
1075 if (!FH->linear)
1076 vfs_die ("You may not do this");
1077 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1078 return 0;
1080 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1081 if (((v < 0) && (errno == EINTR)) || v == 0)
1082 return 1;
1083 return 0;
1085 default:
1086 return 0;
1088 #endif
1091 /* --------------------------------------------------------------------------------------------- */
1093 static int
1094 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1096 int r;
1098 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1099 vfs_stamp_create (&vfs_fish_ops, super);
1100 if (r != COMPLETE)
1101 ERRNOR (E_REMOTE, -1);
1102 if (flags & OPT_FLUSH)
1103 vfs_s_invalidate (me, super);
1104 return 0;
1107 /* --------------------------------------------------------------------------------------------- */
1109 static int
1110 fish_rename (struct vfs_class *me, const char *path1, const char *path2)
1112 gchar *shell_commands = NULL;
1113 char buf[BUF_LARGE];
1114 const char *crpath1, *crpath2;
1115 char *rpath1, *rpath2, *mpath1, *mpath2;
1116 struct vfs_s_super *super, *super2;
1118 mpath1 = g_strdup (path1);
1119 crpath1 = vfs_s_get_path_mangle (me, mpath1, &super, 0);
1120 if (crpath1 == NULL)
1122 g_free (mpath1);
1123 return -1;
1125 mpath2 = g_strdup (path2);
1126 crpath2 = vfs_s_get_path_mangle (me, mpath2, &super2, 0);
1127 if (crpath2 == NULL)
1129 g_free (mpath1);
1130 g_free (mpath2);
1131 return -1;
1133 rpath1 = strutils_shell_escape (crpath1);
1134 g_free (mpath1);
1135 rpath2 = strutils_shell_escape (crpath2);
1136 g_free (mpath2);
1137 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1138 SUP.scr_mv, (char *) NULL);
1139 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1140 g_free (shell_commands);
1141 g_free (rpath1);
1142 g_free (rpath2);
1143 return fish_send_command (me, super2, buf, OPT_FLUSH);
1146 /* --------------------------------------------------------------------------------------------- */
1148 static int
1149 fish_link (struct vfs_class *me, const char *path1, const char *path2)
1151 gchar *shell_commands = NULL;
1152 char buf[BUF_LARGE];
1153 const char *crpath1, *crpath2;
1154 char *rpath1, *rpath2, *mpath1, *mpath2;
1155 struct vfs_s_super *super, *super2;
1157 mpath1 = g_strdup (path1);
1158 crpath1 = vfs_s_get_path_mangle (me, mpath1, &super, 0);
1159 if (crpath1 == NULL)
1161 g_free (mpath1);
1162 return -1;
1164 mpath2 = g_strdup (path2);
1165 crpath2 = vfs_s_get_path_mangle (me, mpath2, &super2, 0);
1166 if (crpath2 == NULL)
1168 g_free (mpath1);
1169 g_free (mpath2);
1170 return -1;
1172 rpath1 = strutils_shell_escape (crpath1);
1173 g_free (mpath1);
1174 rpath2 = strutils_shell_escape (crpath2);
1175 g_free (mpath2);
1176 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1177 SUP.scr_hardlink, (char *) NULL);
1178 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1179 g_free (shell_commands);
1180 g_free (rpath1);
1181 g_free (rpath2);
1182 return fish_send_command (me, super2, buf, OPT_FLUSH);
1186 /* --------------------------------------------------------------------------------------------- */
1188 static int
1189 fish_symlink (struct vfs_class *me, const char *setto, const char *path)
1191 char *qsetto;
1192 gchar *shell_commands = NULL;
1193 char buf[BUF_LARGE];
1194 const char *crpath;
1195 char *rpath, *mpath;
1196 struct vfs_s_super *super;
1198 mpath = g_strdup (path);
1199 crpath = vfs_s_get_path_mangle (me, mpath, &super, 0);
1200 if (crpath == NULL)
1202 g_free (mpath);
1203 return -1;
1205 rpath = strutils_shell_escape (crpath);
1206 g_free (mpath);
1208 qsetto = strutils_shell_escape (setto);
1209 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1210 SUP.scr_ln, (char *) NULL);
1211 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1212 g_free (shell_commands);
1213 g_free (qsetto);
1214 g_free (rpath);
1215 return fish_send_command (me, super, buf, OPT_FLUSH);
1218 /* --------------------------------------------------------------------------------------------- */
1220 static int
1221 fish_chmod (struct vfs_class *me, const char *path, int mode)
1223 gchar *shell_commands = NULL;
1225 PREFIX;
1227 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1228 SUP.scr_chmod, (char *) NULL);
1229 g_snprintf (buf, sizeof (buf), shell_commands, rpath, mode & 07777);
1230 g_free (shell_commands);
1231 g_free (rpath);
1232 return fish_send_command (me, super, buf, OPT_FLUSH);
1235 /* --------------------------------------------------------------------------------------------- */
1237 static int
1238 fish_chown (struct vfs_class *me, const char *path, uid_t owner, gid_t group)
1240 char *sowner, *sgroup;
1241 struct passwd *pw;
1242 struct group *gr;
1244 pw = getpwuid (owner);
1245 if (pw == NULL)
1246 return 0;
1248 gr = getgrgid (group);
1249 if (gr == NULL)
1250 return 0;
1252 sowner = pw->pw_name;
1253 sgroup = gr->gr_name;
1255 gchar *shell_commands = NULL;
1257 PREFIX;
1259 shell_commands = g_strconcat (SUP.scr_env,
1260 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1261 SUP.scr_chown, (char *) NULL);
1262 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1263 g_free (shell_commands);
1264 fish_send_command (me, super, buf, OPT_FLUSH);
1265 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1266 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1267 g_free (rpath);
1268 return fish_send_command (me, super, buf, OPT_FLUSH);
1272 /* --------------------------------------------------------------------------------------------- */
1274 static int
1275 fish_unlink (struct vfs_class *me, const char *path)
1277 gchar *shell_commands = NULL;
1279 PREFIX;
1281 shell_commands =
1282 g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_unlink, (char *) NULL);
1283 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1284 g_free (shell_commands);
1285 g_free (rpath);
1286 return fish_send_command (me, super, buf, OPT_FLUSH);
1289 /* --------------------------------------------------------------------------------------------- */
1291 static int
1292 fish_exists (struct vfs_class *me, const char *path)
1294 gchar *shell_commands = NULL;
1296 PREFIX;
1298 shell_commands =
1299 g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_exists, (char *) NULL);
1300 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1301 g_free (shell_commands);
1302 g_free (rpath);
1304 return (fish_send_command (me, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1308 /* --------------------------------------------------------------------------------------------- */
1310 static int
1311 fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1313 gchar *shell_commands = NULL;
1314 int ret_code;
1316 PREFIX;
1318 (void) mode;
1320 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_mkdir, (char *) NULL);
1321 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1322 g_free (shell_commands);
1324 g_free (rpath);
1325 ret_code = fish_send_command (me, super, buf, OPT_FLUSH);
1327 if (ret_code != 0)
1328 return ret_code;
1330 if (!fish_exists (me, path))
1332 ERRNOR (EACCES, -1);
1334 return 0;
1337 /* --------------------------------------------------------------------------------------------- */
1339 static int
1340 fish_rmdir (struct vfs_class *me, const char *path)
1342 gchar *shell_commands = NULL;
1344 PREFIX;
1346 shell_commands = g_strconcat (SUP.scr_env, "FISH_FILENAME=%s;\n", SUP.scr_rmdir, (char *) NULL);
1347 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1348 g_free (shell_commands);
1349 g_free (rpath);
1350 return fish_send_command (me, super, buf, OPT_FLUSH);
1353 /* --------------------------------------------------------------------------------------------- */
1355 static int
1356 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags, mode_t mode)
1358 (void) mode;
1360 fh->u.fish.append = 0;
1361 /* File will be written only, so no need to retrieve it */
1362 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR)))
1364 fh->u.fish.append = flags & O_APPEND;
1365 if (!fh->ino->localname)
1367 int tmp_handle = vfs_mkstemps (&fh->ino->localname, me->name,
1368 fh->ino->ent->name);
1369 if (tmp_handle == -1)
1370 return -1;
1371 close (tmp_handle);
1373 return 0;
1375 if (!fh->ino->localname)
1376 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1377 return -1;
1378 if (!fh->ino->localname)
1379 vfs_die ("retrieve_file failed to fill in localname");
1380 return 0;
1383 /* --------------------------------------------------------------------------------------------- */
1385 static void
1386 fish_fill_names (struct vfs_class *me, fill_names_f func)
1388 struct vfs_s_super *super = MEDATA->supers;
1389 char *name;
1391 char gbuf[10];
1393 while (super)
1395 const char *flags = "";
1396 switch (SUP.flags)
1398 case FISH_FLAG_RSH:
1399 flags = ":r";
1400 break;
1401 case FISH_FLAG_COMPRESSED:
1402 flags = ":C";
1403 break;
1404 default:
1405 if (SUP.flags > FISH_FLAG_RSH)
1407 break;
1408 g_snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1409 flags = gbuf;
1411 break;
1414 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags, "/", SUP.cwdir, (char *) NULL);
1415 (*func) (name);
1416 g_free (name);
1417 super = super->next;
1421 /* --------------------------------------------------------------------------------------------- */
1423 static void *
1424 fish_open (struct vfs_class *me, const char *file, int flags, mode_t mode)
1427 sorry, i've places hack here
1428 cause fish don't able to open files with O_EXCL flag
1430 flags &= ~O_EXCL;
1431 return vfs_s_open (me, file, flags, mode);
1434 /* --------------------------------------------------------------------------------------------- */
1435 /*** public functions ****************************************************************************/
1436 /* --------------------------------------------------------------------------------------------- */
1438 void
1439 init_fish (void)
1441 static struct vfs_s_subclass fish_subclass;
1443 tcp_init ();
1445 fish_subclass.flags = VFS_S_REMOTE;
1446 fish_subclass.archive_same = fish_archive_same;
1447 fish_subclass.open_archive = fish_open_archive;
1448 fish_subclass.free_archive = fish_free_archive;
1449 fish_subclass.fh_open = fish_fh_open;
1450 fish_subclass.dir_load = fish_dir_load;
1451 fish_subclass.file_store = fish_file_store;
1452 fish_subclass.linear_start = fish_linear_start;
1453 fish_subclass.linear_read = fish_linear_read;
1454 fish_subclass.linear_close = fish_linear_close;
1456 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1457 vfs_fish_ops.name = "fish";
1458 vfs_fish_ops.prefix = "sh:";
1459 vfs_fish_ops.fill_names = fish_fill_names;
1460 vfs_fish_ops.chmod = fish_chmod;
1461 vfs_fish_ops.chown = fish_chown;
1462 vfs_fish_ops.open = fish_open;
1463 vfs_fish_ops.symlink = fish_symlink;
1464 vfs_fish_ops.link = fish_link;
1465 vfs_fish_ops.unlink = fish_unlink;
1466 vfs_fish_ops.rename = fish_rename;
1467 vfs_fish_ops.mkdir = fish_mkdir;
1468 vfs_fish_ops.rmdir = fish_rmdir;
1469 vfs_fish_ops.ctl = fish_ctl;
1470 vfs_register_class (&vfs_fish_ops);
1473 /* --------------------------------------------------------------------------------------------- */