Revert "VFS: make VFS-specific file handler class the derived one from vfs_file_handl...
[midnight-commander.git] / src / vfs / fish / fish.c
blobae87c91104b235734ff95fadf573c9bd0c03f097
1 /*
2 Virtual File System: FISH implementation for transfering files over
3 shell connections.
5 Copyright (C) 1998-2018
6 Free Software Foundation, Inc.
8 Written by:
9 Pavel Machek, 1998
10 Michal Svec, 2000
11 Andrew Borodin <aborodin@vmail.ru>, 2010
12 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
13 Ilia Maslakov <il.smind@gmail.com>, 2010
15 Derived from ftpfs.c.
17 This file is part of the Midnight Commander.
19 The Midnight Commander is free software: you can redistribute it
20 and/or modify it under the terms of the GNU General Public License as
21 published by the Free Software Foundation, either version 3 of the License,
22 or (at your option) any later version.
24 The Midnight Commander is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 GNU General Public License for more details.
29 You should have received a copy of the GNU General Public License
30 along with this program. If not, see <http://www.gnu.org/licenses/>.
33 /**
34 * \file
35 * \brief Source: Virtual File System: FISH implementation for transfering files over
36 * shell connections
37 * \author Pavel Machek
38 * \author Michal Svec
39 * \date 1998, 2000
41 * Derived from ftpfs.c
42 * Read README.fish for protocol specification.
44 * Syntax of path is: \verbatim sh://user@host[:Cr]/path \endverbatim
45 * where C means you want compressed connection,
46 * and r means you want to use rsh
48 * Namespace: fish_vfs_ops exported.
51 /* Define this if your ssh can take -I option */
53 #include <config.h>
54 #include <errno.h>
55 #include <pwd.h>
56 #include <grp.h>
57 #include <sys/time.h> /* gettimeofday() */
58 #include <stdlib.h>
59 #include <string.h>
60 #include <inttypes.h> /* uintmax_t */
62 #include "lib/global.h"
63 #include "lib/tty/tty.h" /* enable/disable interrupt key */
64 #include "lib/strescape.h"
65 #include "lib/unixcompat.h"
66 #include "lib/fileloc.h"
67 #include "lib/util.h" /* my_exit() */
68 #include "lib/mcconfig.h"
70 #include "src/execute.h" /* pre_exec, post_exec */
72 #include "lib/vfs/vfs.h"
73 #include "lib/vfs/utilvfs.h"
74 #include "lib/vfs/netutil.h"
75 #include "lib/vfs/xdirentry.h"
76 #include "lib/vfs/gc.h" /* vfs_stamp_create */
78 #include "fish.h"
79 #include "fishdef.h"
81 /*** global variables ****************************************************************************/
83 int fish_directory_timeout = 900;
85 /*** file scope macro definitions ****************************************************************/
87 #define DO_RESOLVE_SYMLINK 1
88 #define DO_OPEN 2
89 #define DO_FREE_RESOURCE 4
91 #define FISH_FLAG_COMPRESSED 1
92 #define FISH_FLAG_RSH 2
94 #define OPT_FLUSH 1
95 #define OPT_IGNORE_ERROR 2
98 * Reply codes.
100 #define PRELIM 1 /* positive preliminary */
101 #define COMPLETE 2 /* positive completion */
102 #define CONTINUE 3 /* positive intermediate */
103 #define TRANSIENT 4 /* transient negative completion */
104 #define ERROR 5 /* permanent negative completion */
106 /* command wait_flag: */
107 #define NONE 0x00
108 #define WAIT_REPLY 0x01
109 #define WANT_STRING 0x02
111 /* environment flags */
112 #define FISH_HAVE_HEAD 1
113 #define FISH_HAVE_SED 2
114 #define FISH_HAVE_AWK 4
115 #define FISH_HAVE_PERL 8
116 #define FISH_HAVE_LSQ 16
117 #define FISH_HAVE_DATE_MDYT 32
118 #define FISH_HAVE_TAIL 64
120 #define SUP ((fish_super_data_t *) super)
122 /*** file scope type declarations ****************************************************************/
124 typedef struct
126 struct vfs_s_super base; /* base class */
128 int sockr;
129 int sockw;
130 char *scr_ls;
131 char *scr_chmod;
132 char *scr_utime;
133 char *scr_exists;
134 char *scr_mkdir;
135 char *scr_unlink;
136 char *scr_chown;
137 char *scr_rmdir;
138 char *scr_ln;
139 char *scr_mv;
140 char *scr_hardlink;
141 char *scr_get;
142 char *scr_send;
143 char *scr_append;
144 char *scr_info;
145 int host_flags;
146 char *scr_env;
147 } fish_super_data_t;
149 typedef struct
151 off_t got;
152 off_t total;
153 gboolean append;
154 } fish_fh_data_t;
156 /*** file scope variables ************************************************************************/
158 static char reply_str[80];
160 static struct vfs_s_subclass fish_subclass;
161 static struct vfs_class *vfs_fish_ops = (struct vfs_class *) &fish_subclass;
163 /* --------------------------------------------------------------------------------------------- */
164 /*** file scope functions ************************************************************************/
165 /* --------------------------------------------------------------------------------------------- */
167 static void
168 fish_set_blksize (struct stat *s)
170 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
171 /* redefine block size */
172 s->st_blksize = 64 * 1024; /* FIXME */
173 #endif
176 /* --------------------------------------------------------------------------------------------- */
178 static struct stat *
179 fish_default_stat (struct vfs_class *me)
181 struct stat *s;
183 s = vfs_s_default_stat (me, S_IFDIR | 0755);
184 fish_set_blksize (s);
185 vfs_adjust_stat (s);
187 return s;
190 /* --------------------------------------------------------------------------------------------- */
192 static char *
193 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
195 char *scr_filename = NULL;
196 char *scr_content;
197 gsize scr_len = 0;
199 /* 1st: scan user directory */
200 scr_filename = g_build_path (PATH_SEP_STR, mc_config_get_data_path (), FISH_PREFIX, hostname,
201 script_name, (char *) NULL);
202 /* silent about user dir */
203 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
204 g_free (scr_filename);
205 /* 2nd: scan system dir */
206 if (scr_content == NULL)
208 scr_filename =
209 g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
210 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
211 g_free (scr_filename);
214 if (scr_content != NULL)
215 return scr_content;
217 return g_strdup (def_content);
220 /* --------------------------------------------------------------------------------------------- */
222 static int
223 fish_decode_reply (char *s, gboolean was_garbage)
225 int code;
227 /* cppcheck-suppress invalidscanf */
228 if (sscanf (s, "%d", &code) == 0)
230 code = 500;
231 return 5;
233 if (code < 100)
234 return was_garbage ? ERROR : (code == 0 ? COMPLETE : PRELIM);
235 return code / 100;
238 /* --------------------------------------------------------------------------------------------- */
239 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
241 static int
242 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
244 char answer[BUF_1K];
245 gboolean was_garbage = FALSE;
247 while (TRUE)
249 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
251 if (string_buf != NULL)
252 *string_buf = '\0';
253 return 4;
256 if (strncmp (answer, "### ", 4) == 0)
257 return fish_decode_reply (answer + 4, was_garbage ? 1 : 0);
259 was_garbage = TRUE;
260 if (string_buf != NULL)
261 g_strlcpy (string_buf, answer, string_len);
265 /* --------------------------------------------------------------------------------------------- */
267 static int
268 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *cmd,
269 size_t cmd_len)
271 ssize_t status;
272 FILE *logfile = MEDATA->logfile;
274 if (cmd_len == (size_t) (-1))
275 cmd_len = strlen (cmd);
277 if (logfile != NULL)
279 size_t ret;
281 ret = fwrite (cmd, cmd_len, 1, logfile);
282 ret = fflush (logfile);
283 (void) ret;
286 tty_enable_interrupt_key ();
287 status = write (SUP->sockw, cmd, cmd_len);
288 tty_disable_interrupt_key ();
290 if (status < 0)
291 return TRANSIENT;
293 if (wait_reply)
294 return fish_get_reply (me, SUP->sockr,
295 (wait_reply & WANT_STRING) != 0 ? reply_str :
296 NULL, sizeof (reply_str) - 1);
297 return COMPLETE;
300 /* --------------------------------------------------------------------------------------------- */
302 static int
303 G_GNUC_PRINTF (5, 0)
304 fish_command_va (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
305 const char *vars, va_list ap)
307 int r;
308 GString *command;
310 command = g_string_new (SUP->scr_env);
311 g_string_append_vprintf (command, vars, ap);
312 g_string_append (command, scr);
313 r = fish_command (me, super, wait_reply, command->str, command->len);
314 g_string_free (command, TRUE);
316 return r;
319 /* --------------------------------------------------------------------------------------------- */
321 static int
322 G_GNUC_PRINTF (5, 6)
323 fish_command_v (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
324 const char *vars, ...)
326 int r;
327 va_list ap;
329 va_start (ap, vars);
330 r = fish_command_va (me, super, wait_reply, scr, vars, ap);
331 va_end (ap);
333 return r;
336 /* --------------------------------------------------------------------------------------------- */
338 static int
339 G_GNUC_PRINTF (5, 6)
340 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, int flags, const char *scr,
341 const char *vars, ...)
343 int r;
344 va_list ap;
346 va_start (ap, vars);
347 r = fish_command_va (me, super, WAIT_REPLY, scr, vars, ap);
348 va_end (ap);
349 vfs_stamp_create (vfs_fish_ops, super);
351 if (r != COMPLETE)
352 ERRNOR (E_REMOTE, -1);
353 if ((flags & OPT_FLUSH) != 0)
354 vfs_s_invalidate (me, super);
356 return 0;
359 /* --------------------------------------------------------------------------------------------- */
361 static struct vfs_s_super *
362 fish_new_archive (struct vfs_class *me)
364 fish_super_data_t *arch;
366 arch = g_new0 (fish_super_data_t, 1);
367 arch->base.me = me;
369 return VFS_SUPER (arch);
372 /* --------------------------------------------------------------------------------------------- */
374 static void
375 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
377 if ((SUP->sockw != -1) || (SUP->sockr != -1))
379 vfs_print_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
380 fish_command (me, super, NONE, "#BYE\nexit\n", -1);
381 close (SUP->sockw);
382 close (SUP->sockr);
383 SUP->sockw = SUP->sockr = -1;
385 g_free (SUP->scr_ls);
386 g_free (SUP->scr_exists);
387 g_free (SUP->scr_mkdir);
388 g_free (SUP->scr_unlink);
389 g_free (SUP->scr_chown);
390 g_free (SUP->scr_chmod);
391 g_free (SUP->scr_utime);
392 g_free (SUP->scr_rmdir);
393 g_free (SUP->scr_ln);
394 g_free (SUP->scr_mv);
395 g_free (SUP->scr_hardlink);
396 g_free (SUP->scr_get);
397 g_free (SUP->scr_send);
398 g_free (SUP->scr_append);
399 g_free (SUP->scr_info);
400 g_free (SUP->scr_env);
403 /* --------------------------------------------------------------------------------------------- */
405 static void
406 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
408 int fileset1[2], fileset2[2];
409 int res;
411 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
412 vfs_die ("Cannot pipe(): %m.");
414 res = fork ();
416 if (res != 0)
418 if (res < 0)
419 vfs_die ("Cannot fork(): %m.");
420 /* We are the parent */
421 close (fileset1[0]);
422 SUP->sockw = fileset1[1];
423 close (fileset2[1]);
424 SUP->sockr = fileset2[0];
426 else
428 res = dup2 (fileset1[0], STDIN_FILENO);
429 close (fileset1[0]);
430 close (fileset1[1]);
431 res = dup2 (fileset2[1], STDOUT_FILENO);
432 close (STDERR_FILENO);
433 /* stderr to /dev/null */
434 res = open ("/dev/null", O_WRONLY);
435 close (fileset2[0]);
436 close (fileset2[1]);
437 execvp (path, (char **) argv);
438 my_exit (3);
442 /* --------------------------------------------------------------------------------------------- */
444 static char *
445 fish_set_env (int flags)
447 GString *tmp;
449 tmp = g_string_sized_new (250);
450 g_string_assign (tmp, "");
452 if ((flags & FISH_HAVE_HEAD) != 0)
453 g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
455 if ((flags & FISH_HAVE_SED) != 0)
456 g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
458 if ((flags & FISH_HAVE_AWK) != 0)
459 g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
461 if ((flags & FISH_HAVE_PERL) != 0)
462 g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
464 if ((flags & FISH_HAVE_LSQ) != 0)
465 g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
467 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
468 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
470 if ((flags & FISH_HAVE_TAIL) != 0)
471 g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
473 return g_string_free (tmp, FALSE);
476 /* --------------------------------------------------------------------------------------------- */
478 static gboolean
479 fish_info (struct vfs_class *me, struct vfs_s_super *super)
481 if (fish_command (me, super, NONE, SUP->scr_info, -1) == COMPLETE)
483 while (TRUE)
485 int res;
486 char buffer[BUF_8K];
488 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
489 if ((res == 0) || (res == EINTR))
490 ERRNOR (ECONNRESET, FALSE);
491 if (strncmp (buffer, "### ", 4) == 0)
492 break;
493 SUP->host_flags = atol (buffer);
495 return TRUE;
497 ERRNOR (E_PROTO, FALSE);
500 /* --------------------------------------------------------------------------------------------- */
502 static void
503 fish_open_archive_pipeopen (struct vfs_s_super *super)
505 char gbuf[10];
506 const char *argv[10]; /* All of 10 is used now */
507 const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
508 int i = 0;
510 argv[i++] = xsh;
511 if (super->path_element->port == FISH_FLAG_COMPRESSED)
512 argv[i++] = "-C";
514 if (super->path_element->port > FISH_FLAG_RSH)
516 argv[i++] = "-p";
517 g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
518 argv[i++] = gbuf;
522 * Add the user name to the ssh command line only if it was explicitly
523 * set in vfs URL. rsh/ssh will get current user by default
524 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
525 * option breaks it for some)
528 if (super->path_element->user != NULL)
530 argv[i++] = "-l";
531 argv[i++] = super->path_element->user;
533 else
535 /* The rest of the code assumes it to be a valid username */
536 super->path_element->user = vfs_get_local_username ();
539 argv[i++] = super->path_element->host;
540 argv[i++] = "echo FISH:; /bin/sh";
541 argv[i++] = NULL;
543 fish_pipeopen (super, xsh, argv);
546 /* --------------------------------------------------------------------------------------------- */
548 static gboolean
549 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
551 char answer[2048];
553 printf ("\n%s\n", _("fish: Waiting for initial line..."));
555 if (vfs_s_get_line (me, SUP->sockr, answer, sizeof (answer), ':') == 0)
556 return FALSE;
558 if (strstr (answer, "assword") != NULL)
560 /* Currently, this does not work. ssh reads passwords from
561 /dev/tty, not from stdin :-(. */
563 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
565 return FALSE;
566 #if 0
567 if (super->path_element->password == NULL)
569 char *p, *op;
571 p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
572 op = vfs_get_password (p);
573 g_free (p);
574 if (op == NULL)
575 return FALSE;
576 super->path_element->password = op;
579 printf ("\n%s\n", _("fish: Sending password..."));
582 size_t str_len;
584 str_len = strlen (super->path_element->password);
585 if ((write (SUP.sockw, super->path_element->password, str_len) != (ssize_t) str_len)
586 || (write (SUP->sockw, "\n", 1) != 1))
587 return FALSE;
589 #endif
591 return TRUE;
594 /* --------------------------------------------------------------------------------------------- */
596 static int
597 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
599 gboolean ftalk;
601 /* hide panels */
602 pre_exec ();
604 /* open pipe */
605 fish_open_archive_pipeopen (super);
607 /* Start talk with ssh-server (password prompt, etc ) */
608 ftalk = fish_open_archive_talk (me, super);
610 /* show panels */
611 post_exec ();
613 if (!ftalk)
614 ERRNOR (E_PROTO, -1);
616 vfs_print_message ("%s", _("fish: Sending initial line..."));
618 * Run 'start_fish_server'. If it doesn't exist - no problem,
619 * we'll talk directly to the shell.
622 if (fish_command
623 (me, super, WAIT_REPLY, "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n",
624 -1) != COMPLETE)
625 ERRNOR (E_PROTO, -1);
627 vfs_print_message ("%s", _("fish: Handshaking version..."));
628 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n", -1) != COMPLETE)
629 ERRNOR (E_PROTO, -1);
631 /* Set up remote locale to C, otherwise dates cannot be recognized */
632 if (fish_command
633 (me, super, WAIT_REPLY,
634 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n",
635 -1) != COMPLETE)
636 ERRNOR (E_PROTO, -1);
638 vfs_print_message ("%s", _("fish: Getting host info..."));
639 if (fish_info (me, super))
640 SUP->scr_env = fish_set_env (SUP->host_flags);
642 #if 0
643 super->name =
644 g_strconcat ("sh://", super->path_element->user, "@", super->path_element->host,
645 PATH_SEP_STR, (char *) NULL);
646 #else
647 super->name = g_strdup (PATH_SEP_STR);
648 #endif
650 super->root = vfs_s_new_inode (me, super, fish_default_stat (me));
652 return 0;
655 /* --------------------------------------------------------------------------------------------- */
657 static int
658 fish_open_archive (struct vfs_s_super *super,
659 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
661 (void) vpath;
663 super->path_element = vfs_path_element_clone (vpath_element);
665 if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
666 super->path_element->port = FISH_FLAG_RSH;
668 SUP->scr_ls =
669 fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
670 SUP->scr_exists =
671 fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
672 FISH_EXISTS_DEF_CONTENT);
673 SUP->scr_mkdir =
674 fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
675 FISH_MKDIR_DEF_CONTENT);
676 SUP->scr_unlink =
677 fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
678 FISH_UNLINK_DEF_CONTENT);
679 SUP->scr_chown =
680 fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
681 FISH_CHOWN_DEF_CONTENT);
682 SUP->scr_chmod =
683 fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
684 FISH_CHMOD_DEF_CONTENT);
685 SUP->scr_utime =
686 fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
687 FISH_UTIME_DEF_CONTENT);
688 SUP->scr_rmdir =
689 fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
690 FISH_RMDIR_DEF_CONTENT);
691 SUP->scr_ln =
692 fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
693 SUP->scr_mv =
694 fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
695 SUP->scr_hardlink =
696 fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
697 FISH_HARDLINK_DEF_CONTENT);
698 SUP->scr_get =
699 fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
700 SUP->scr_send =
701 fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
702 FISH_SEND_DEF_CONTENT);
703 SUP->scr_append =
704 fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
705 FISH_APPEND_DEF_CONTENT);
706 SUP->scr_info =
707 fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
708 FISH_INFO_DEF_CONTENT);
710 return fish_open_archive_int (vpath_element->class, super);
713 /* --------------------------------------------------------------------------------------------- */
715 static int
716 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
717 const vfs_path_t * vpath, void *cookie)
719 vfs_path_element_t *path_element;
720 int result;
722 (void) vpath;
723 (void) cookie;
725 path_element = vfs_path_element_clone (vpath_element);
727 if (path_element->user == NULL)
728 path_element->user = vfs_get_local_username ();
730 result = ((strcmp (path_element->host, super->path_element->host) == 0)
731 && (strcmp (path_element->user, super->path_element->user) == 0)
732 && (path_element->port == super->path_element->port)) ? 1 : 0;
734 vfs_path_element_free (path_element);
736 return result;
739 /* --------------------------------------------------------------------------------------------- */
741 static int
742 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
744 struct vfs_s_super *super = dir->super;
745 char buffer[BUF_8K] = "\0";
746 struct vfs_s_entry *ent = NULL;
747 FILE *logfile;
748 char *quoted_path;
749 int reply_code;
752 * Simple FISH debug interface :]
754 #if 0
755 if (MEDATA->logfile == NULL)
756 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
757 #endif
758 logfile = MEDATA->logfile;
760 vfs_print_message (_("fish: Reading directory %s..."), remote_path);
762 gettimeofday (&dir->timestamp, NULL);
763 dir->timestamp.tv_sec += fish_directory_timeout;
765 quoted_path = strutils_shell_escape (remote_path);
766 (void) fish_command_v (me, super, NONE, SUP->scr_ls, "FISH_FILENAME=%s;\n", quoted_path);
767 g_free (quoted_path);
769 ent = vfs_s_generate_entry (me, NULL, dir, 0);
771 while (TRUE)
773 int res;
775 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
777 if ((res == 0) || (res == EINTR))
779 vfs_s_free_entry (me, ent);
780 me->verrno = ECONNRESET;
781 goto error;
783 if (logfile != NULL)
785 fputs (buffer, logfile);
786 fputs ("\n", logfile);
787 fflush (logfile);
789 if (strncmp (buffer, "### ", 4) == 0)
790 break;
791 if (buffer[0] == '\0')
793 if (ent->name != NULL)
795 vfs_s_insert_entry (me, dir, ent);
796 ent = vfs_s_generate_entry (me, NULL, dir, 0);
798 continue;
801 #define ST ent->ino->st
803 switch (buffer[0])
805 case ':':
807 char *temp;
808 char *data_start = buffer + 1;
809 char *filename = data_start;
810 char *filename_bound;
812 filename_bound = filename + strlen (filename);
814 if (strcmp (data_start, "\".\"") == 0 || strcmp (data_start, "\"..\"") == 0)
815 break; /* We'll do "." and ".." ourselves */
817 if (S_ISLNK (ST.st_mode))
819 char *linkname;
820 char *linkname_bound;
821 /* we expect: "escaped-name" -> "escaped-name"
822 // -> cannot occur in filenames,
823 // because it will be escaped to -\> */
826 linkname_bound = filename_bound;
828 if (*filename == '"')
829 ++filename;
831 linkname = strstr (filename, "\" -> \"");
832 if (linkname == NULL)
834 /* broken client, or smth goes wrong */
835 linkname = filename_bound;
836 if (filename_bound > filename && *(filename_bound - 1) == '"')
837 --filename_bound; /* skip trailing " */
839 else
841 filename_bound = linkname;
842 linkname += 6; /* strlen ("\" -> \"") */
843 if (*(linkname_bound - 1) == '"')
844 --linkname_bound; /* skip trailing " */
847 ent->name = g_strndup (filename, filename_bound - filename);
848 temp = ent->name;
849 ent->name = strutils_shell_unescape (ent->name);
850 g_free (temp);
852 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
853 temp = ent->ino->linkname;
854 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
855 g_free (temp);
857 else
859 /* we expect: "escaped-name" */
860 if (filename_bound - filename > 2)
863 there is at least 2 "
864 and we skip them
866 if (*filename == '"')
867 ++filename;
868 if (*(filename_bound - 1) == '"')
869 --filename_bound;
871 ent->name = g_strndup (filename, filename_bound - filename);
872 temp = ent->name;
873 ent->name = strutils_shell_unescape (ent->name);
874 g_free (temp);
876 break;
878 case 'S':
879 ST.st_size = (off_t) g_ascii_strtoll (buffer + 1, NULL, 10);
880 break;
881 case 'P':
883 size_t skipped;
885 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
886 break;
888 case 'R':
891 raw filemode:
892 we expect: Roctal-filemode octal-filetype uid.gid
894 size_t skipped;
896 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
897 break;
899 case 'd':
901 vfs_split_text (buffer + 1);
902 if (vfs_parse_filedate (0, &ST.st_ctime) == 0)
903 break;
904 ST.st_atime = ST.st_mtime = ST.st_ctime;
905 #ifdef HAVE_STRUCT_STAT_ST_MTIM
906 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
907 #endif
909 break;
910 case 'D':
912 struct tm tim;
914 /* cppcheck-suppress invalidscanf */
915 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
916 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
917 break;
918 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
919 #ifdef HAVE_STRUCT_STAT_ST_MTIM
920 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
921 #endif
923 break;
924 case 'E':
926 int maj, min;
928 /* cppcheck-suppress invalidscanf */
929 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
930 break;
931 #ifdef HAVE_STRUCT_STAT_ST_RDEV
932 ST.st_rdev = makedev (maj, min);
933 #endif
935 default:
936 break;
940 vfs_s_free_entry (me, ent);
941 reply_code = fish_decode_reply (buffer + 4, 0);
942 if (reply_code == COMPLETE)
944 vfs_print_message (_("%s: done."), me->name);
945 return 0;
948 me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
950 error:
951 vfs_print_message (_("%s: failure"), me->name);
952 return -1;
955 /* --------------------------------------------------------------------------------------------- */
957 static int
958 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
960 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
961 struct vfs_s_super *super = FH_SUPER;
962 int code;
963 off_t total = 0;
964 char buffer[BUF_8K];
965 struct stat s;
966 int h;
967 char *quoted_name;
969 h = open (localname, O_RDONLY);
970 if (h == -1)
971 ERRNOR (EIO, -1);
972 if (fstat (h, &s) < 0)
974 close (h);
975 ERRNOR (EIO, -1);
978 /* First, try this as stor:
980 * ( head -c number ) | ( cat > file; cat >/dev/null )
982 * If 'head' is not present on the remote system, 'dd' will be used.
983 * Unfortunately, we cannot trust most non-GNU 'head' implementations
984 * even if '-c' options is supported. Therefore, we separate GNU head
985 * (and other modern heads?) using '-q' and '-' . This causes another
986 * implementations to fail (because of "incorrect options").
988 * Fallback is:
990 * rest=<number>
991 * while [ $rest -gt 0 ]
992 * do
993 * cnt=`expr \( $rest + 255 \) / 256`
994 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
995 * rest=`expr $rest - $n`
996 * done
998 * 'dd' was not designed for full filling of input buffers,
999 * and does not report exact number of bytes (not blocks).
1000 * Therefore a more complex shell script is needed.
1002 * On some systems non-GNU head writes "Usage:" error report to stdout
1003 * instead of stderr. It makes impossible the use of "head || dd"
1004 * algorithm for file appending case, therefore just "dd" is used for it.
1007 quoted_name = strutils_shell_escape (name);
1008 vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
1010 /* FIXME: File size is limited to ULONG_MAX */
1011 code = fish_command_v (me, super, WAIT_REPLY, fish->append ? SUP->scr_append : SUP->scr_send,
1012 "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n", quoted_name,
1013 (uintmax_t) s.st_size);
1014 g_free (quoted_name);
1016 if (code != PRELIM)
1018 close (h);
1019 ERRNOR (E_REMOTE, -1);
1022 while (TRUE)
1024 ssize_t n, t;
1026 while ((n = read (h, buffer, sizeof (buffer))) < 0)
1028 if ((errno == EINTR) && tty_got_interrupt ())
1029 continue;
1030 vfs_print_message ("%s", _("fish: Local read failed, sending zeros"));
1031 close (h);
1032 h = open ("/dev/zero", O_RDONLY);
1035 if (n == 0)
1036 break;
1038 t = write (SUP->sockw, buffer, n);
1039 if (t != n)
1041 if (t == -1)
1042 me->verrno = errno;
1043 else
1044 me->verrno = EIO;
1045 goto error_return;
1047 tty_disable_interrupt_key ();
1048 total += n;
1049 vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _("fish: storing file"),
1050 (uintmax_t) total, (uintmax_t) s.st_size);
1052 close (h);
1054 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1055 ERRNOR (E_REMOTE, -1);
1056 return 0;
1058 error_return:
1059 close (h);
1060 fish_get_reply (me, SUP->sockr, NULL, 0);
1061 return -1;
1064 /* --------------------------------------------------------------------------------------------- */
1066 static int
1067 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
1069 fish_fh_data_t *fish;
1070 struct vfs_s_super *super = FH_SUPER;
1071 char *name;
1072 char *quoted_name;
1074 if (fh->data == NULL)
1075 fh->data = g_new0 (fish_fh_data_t, 1);
1077 fish = (fish_fh_data_t *) fh->data;
1079 name = vfs_s_fullpath (me, fh->ino);
1080 if (name == NULL)
1081 return 0;
1082 quoted_name = strutils_shell_escape (name);
1083 g_free (name);
1084 fish->append = FALSE;
1087 * Check whether the remote file is readable by using 'dd' to copy
1088 * a single byte from the remote file to /dev/null. If 'dd' completes
1089 * with exit status of 0 use 'cat' to send the file contents to the
1090 * standard output (i.e. over the network).
1093 offset =
1094 fish_command_v (me, super, WANT_STRING, SUP->scr_get,
1095 "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n", quoted_name,
1096 (uintmax_t) offset);
1097 g_free (quoted_name);
1099 if (offset != PRELIM)
1100 ERRNOR (E_REMOTE, 0);
1101 fh->linear = LS_LINEAR_OPEN;
1102 fish->got = 0;
1103 errno = 0;
1104 #if SIZEOF_OFF_T == SIZEOF_LONG
1105 fish->total = (off_t) strtol (reply_str, NULL, 10);
1106 #else
1107 fish->total = (off_t) g_ascii_strtoll (reply_str, NULL, 10);
1108 #endif
1109 if (errno != 0)
1110 ERRNOR (E_REMOTE, 0);
1111 return 1;
1114 /* --------------------------------------------------------------------------------------------- */
1116 static void
1117 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
1119 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1120 struct vfs_s_super *super = FH_SUPER;
1121 char buffer[BUF_8K];
1122 ssize_t n;
1124 vfs_print_message ("%s", _("Aborting transfer..."));
1128 n = MIN ((off_t) sizeof (buffer), (fish->total - fish->got));
1129 if (n != 0)
1131 n = read (SUP->sockr, buffer, n);
1132 if (n < 0)
1133 return;
1134 fish->got += n;
1137 while (n != 0);
1139 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1140 vfs_print_message ("%s", _("Error reported after abort."));
1141 else
1142 vfs_print_message ("%s", _("Aborted transfer would be successful."));
1145 /* --------------------------------------------------------------------------------------------- */
1147 static ssize_t
1148 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
1150 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1151 struct vfs_s_super *super = FH_SUPER;
1152 ssize_t n = 0;
1154 len = MIN ((size_t) (fish->total - fish->got), len);
1155 tty_disable_interrupt_key ();
1156 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1158 if ((errno == EINTR) && !tty_got_interrupt ())
1159 continue;
1160 break;
1162 tty_enable_interrupt_key ();
1164 if (n > 0)
1165 fish->got += n;
1166 else if (n < 0)
1167 fish_linear_abort (me, fh);
1168 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1169 ERRNOR (E_REMOTE, -1);
1170 ERRNOR (errno, n);
1173 /* --------------------------------------------------------------------------------------------- */
1175 static void
1176 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1178 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1180 if (fish->total != fish->got)
1181 fish_linear_abort (me, fh);
1184 /* --------------------------------------------------------------------------------------------- */
1186 static int
1187 fish_ctl (void *fh, int ctlop, void *arg)
1189 (void) arg;
1190 (void) fh;
1191 (void) ctlop;
1193 return 0;
1195 #if 0
1196 switch (ctlop)
1198 case VFS_CTL_IS_NOTREADY:
1200 int v;
1202 if (FH->linear == LS_NOT_LINEAR)
1203 vfs_die ("You may not do this");
1204 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1205 return 0;
1207 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1209 return (((v < 0) && (errno == EINTR)) || v == 0) ? 1 : 0;
1211 default:
1212 return 0;
1214 #endif
1217 /* --------------------------------------------------------------------------------------------- */
1219 static int
1220 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1222 const char *crpath1, *crpath2;
1223 char *rpath1, *rpath2;
1224 struct vfs_s_super *super, *super2;
1225 const vfs_path_element_t *path_element;
1226 int ret;
1228 path_element = vfs_path_get_by_index (vpath1, -1);
1230 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1231 if (crpath1 == NULL)
1232 return -1;
1234 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1235 if (crpath2 == NULL)
1236 return -1;
1238 rpath1 = strutils_shell_escape (crpath1);
1239 rpath2 = strutils_shell_escape (crpath2);
1241 ret =
1242 fish_send_command (path_element->class, super2, OPT_FLUSH, SUP->scr_mv,
1243 "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1245 g_free (rpath1);
1246 g_free (rpath2);
1248 return ret;
1251 /* --------------------------------------------------------------------------------------------- */
1253 static int
1254 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1256 const char *crpath1, *crpath2;
1257 char *rpath1, *rpath2;
1258 struct vfs_s_super *super, *super2;
1259 const vfs_path_element_t *path_element;
1260 int ret;
1262 path_element = vfs_path_get_by_index (vpath1, -1);
1264 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1265 if (crpath1 == NULL)
1266 return -1;
1268 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1269 if (crpath2 == NULL)
1270 return -1;
1272 rpath1 = strutils_shell_escape (crpath1);
1273 rpath2 = strutils_shell_escape (crpath2);
1275 ret =
1276 fish_send_command (path_element->class, super2, OPT_FLUSH, SUP->scr_hardlink,
1277 "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1279 g_free (rpath1);
1280 g_free (rpath2);
1282 return ret;
1285 /* --------------------------------------------------------------------------------------------- */
1287 static int
1288 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1290 char *qsetto;
1291 const char *crpath;
1292 char *rpath;
1293 struct vfs_s_super *super;
1294 const vfs_path_element_t *path_element;
1295 int ret;
1297 path_element = vfs_path_get_by_index (vpath2, -1);
1299 crpath = vfs_s_get_path (vpath2, &super, 0);
1300 if (crpath == NULL)
1301 return -1;
1303 rpath = strutils_shell_escape (crpath);
1304 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1306 ret =
1307 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_ln,
1308 "FISH_FILEFROM=%s FISH_FILETO=%s;\n", qsetto, rpath);
1310 g_free (qsetto);
1311 g_free (rpath);
1313 return ret;
1316 /* --------------------------------------------------------------------------------------------- */
1318 static int
1319 fish_stat (const vfs_path_t * vpath, struct stat *buf)
1321 int ret;
1323 ret = vfs_s_stat (vpath, buf);
1324 fish_set_blksize (buf);
1325 return ret;
1328 /* --------------------------------------------------------------------------------------------- */
1330 static int
1331 fish_lstat (const vfs_path_t * vpath, struct stat *buf)
1333 int ret;
1335 ret = vfs_s_lstat (vpath, buf);
1336 fish_set_blksize (buf);
1337 return ret;
1340 /* --------------------------------------------------------------------------------------------- */
1342 static int
1343 fish_fstat (void *vfs_info, struct stat *buf)
1345 int ret;
1347 ret = vfs_s_fstat (vfs_info, buf);
1348 fish_set_blksize (buf);
1349 return ret;
1352 /* --------------------------------------------------------------------------------------------- */
1354 static int
1355 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1357 const char *crpath;
1358 char *rpath;
1359 struct vfs_s_super *super;
1360 const vfs_path_element_t *path_element;
1361 int ret;
1363 path_element = vfs_path_get_by_index (vpath, -1);
1365 crpath = vfs_s_get_path (vpath, &super, 0);
1366 if (crpath == NULL)
1367 return -1;
1369 rpath = strutils_shell_escape (crpath);
1371 ret =
1372 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_chmod,
1373 "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n", rpath,
1374 (unsigned int) (mode & 07777));
1376 g_free (rpath);
1378 return ret;;
1381 /* --------------------------------------------------------------------------------------------- */
1383 static int
1384 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1386 char *sowner, *sgroup;
1387 struct passwd *pw;
1388 struct group *gr;
1389 const char *crpath;
1390 char *rpath;
1391 struct vfs_s_super *super;
1392 const vfs_path_element_t *path_element;
1393 int ret;
1395 pw = getpwuid (owner);
1396 if (pw == NULL)
1397 return 0;
1399 gr = getgrgid (group);
1400 if (gr == NULL)
1401 return 0;
1403 sowner = pw->pw_name;
1404 sgroup = gr->gr_name;
1406 path_element = vfs_path_get_by_index (vpath, -1);
1408 crpath = vfs_s_get_path (vpath, &super, 0);
1409 if (crpath == NULL)
1410 return -1;
1412 rpath = strutils_shell_escape (crpath);
1414 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1415 ret =
1416 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_chown,
1417 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n", rpath, sowner,
1418 sgroup);
1420 g_free (rpath);
1422 return ret;
1425 /* --------------------------------------------------------------------------------------------- */
1427 static void
1428 fish_get_atime (mc_timesbuf_t * times, time_t * sec, long *nsec)
1430 #ifdef HAVE_UTIMENSAT
1431 *sec = (*times)[0].tv_sec;
1432 *nsec = (*times)[0].tv_nsec;
1433 #else
1434 *sec = times->actime;
1435 *nsec = 0;
1436 #endif
1439 /* --------------------------------------------------------------------------------------------- */
1441 static void
1442 fish_get_mtime (mc_timesbuf_t * times, time_t * sec, long *nsec)
1444 #ifdef HAVE_UTIMENSAT
1445 *sec = (*times)[1].tv_sec;
1446 *nsec = (*times)[1].tv_nsec;
1447 #else
1448 *sec = times->modtime;
1449 *nsec = 0;
1450 #endif
1453 /* --------------------------------------------------------------------------------------------- */
1455 static int
1456 fish_utime (const vfs_path_t * vpath, mc_timesbuf_t * times)
1458 char utcatime[16], utcmtime[16];
1459 char utcatime_w_nsec[30], utcmtime_w_nsec[30];
1460 time_t atime, mtime;
1461 long atime_nsec, mtime_nsec;
1462 struct tm *gmt;
1463 const char *crpath;
1464 char *rpath;
1465 struct vfs_s_super *super;
1466 const vfs_path_element_t *path_element;
1467 int ret;
1469 path_element = vfs_path_get_by_index (vpath, -1);
1471 crpath = vfs_s_get_path (vpath, &super, 0);
1472 if (crpath == NULL)
1473 return -1;
1475 rpath = strutils_shell_escape (crpath);
1477 fish_get_atime (times, &atime, &atime_nsec);
1478 gmt = gmtime (&atime);
1479 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1480 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1481 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1482 g_snprintf (utcatime_w_nsec, sizeof (utcatime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1483 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1484 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, atime_nsec);
1486 fish_get_mtime (times, &mtime, &mtime_nsec);
1487 gmt = gmtime (&mtime);
1488 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1489 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1490 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1491 g_snprintf (utcmtime_w_nsec, sizeof (utcmtime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1492 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1493 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, mtime_nsec);
1495 ret = fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_utime,
1496 "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld "
1497 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s FISH_TOUCHATIME_W_NSEC=\"%s\" "
1498 "FISH_TOUCHMTIME_W_NSEC=\"%s\";\n", rpath, (long) atime, (long) mtime,
1499 utcatime, utcmtime, utcatime_w_nsec, utcmtime_w_nsec);
1501 g_free (rpath);
1503 return ret;
1506 /* --------------------------------------------------------------------------------------------- */
1508 static int
1509 fish_unlink (const vfs_path_t * vpath)
1511 const char *crpath;
1512 char *rpath;
1513 struct vfs_s_super *super;
1514 const vfs_path_element_t *path_element;
1515 int ret;
1517 path_element = vfs_path_get_by_index (vpath, -1);
1519 crpath = vfs_s_get_path (vpath, &super, 0);
1520 if (crpath == NULL)
1521 return -1;
1523 rpath = strutils_shell_escape (crpath);
1525 ret =
1526 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_unlink,
1527 "FISH_FILENAME=%s;\n", rpath);
1529 g_free (rpath);
1531 return ret;
1534 /* --------------------------------------------------------------------------------------------- */
1536 static int
1537 fish_exists (const vfs_path_t * vpath)
1539 const char *crpath;
1540 char *rpath;
1541 struct vfs_s_super *super;
1542 const vfs_path_element_t *path_element;
1543 int ret;
1545 path_element = vfs_path_get_by_index (vpath, -1);
1547 crpath = vfs_s_get_path (vpath, &super, 0);
1548 if (crpath == NULL)
1549 return -1;
1551 rpath = strutils_shell_escape (crpath);
1553 ret =
1554 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_exists,
1555 "FISH_FILENAME=%s;\n", rpath);
1557 g_free (rpath);
1559 return (ret == 0 ? 1 : 0);
1562 /* --------------------------------------------------------------------------------------------- */
1564 static int
1565 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1567 const char *crpath;
1568 char *rpath;
1569 struct vfs_s_super *super;
1570 const vfs_path_element_t *path_element;
1571 int ret;
1573 (void) mode;
1575 path_element = vfs_path_get_by_index (vpath, -1);
1577 crpath = vfs_s_get_path (vpath, &super, 0);
1578 if (crpath == NULL)
1579 return -1;
1581 rpath = strutils_shell_escape (crpath);
1583 ret =
1584 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_mkdir,
1585 "FISH_FILENAME=%s;\n", rpath);
1586 g_free (rpath);
1588 if (ret != 0)
1589 return ret;
1591 if (fish_exists (vpath) == 0)
1593 path_element->class->verrno = EACCES;
1594 return -1;
1596 return 0;
1599 /* --------------------------------------------------------------------------------------------- */
1601 static int
1602 fish_rmdir (const vfs_path_t * vpath)
1604 const char *crpath;
1605 char *rpath;
1606 struct vfs_s_super *super;
1607 const vfs_path_element_t *path_element;
1608 int ret;
1610 path_element = vfs_path_get_by_index (vpath, -1);
1612 crpath = vfs_s_get_path (vpath, &super, 0);
1613 if (crpath == NULL)
1614 return -1;
1616 rpath = strutils_shell_escape (crpath);
1618 ret =
1619 fish_send_command (path_element->class, super, OPT_FLUSH, SUP->scr_rmdir,
1620 "FISH_FILENAME=%s;\n", rpath);
1622 g_free (rpath);
1624 return ret;
1627 /* --------------------------------------------------------------------------------------------- */
1629 static void
1630 fish_fh_free_data (vfs_file_handler_t * fh)
1632 if (fh != NULL)
1633 MC_PTR_FREE (fh->data);
1636 /* --------------------------------------------------------------------------------------------- */
1638 static int
1639 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1641 fish_fh_data_t *fish;
1643 (void) mode;
1645 fh->data = g_new0 (fish_fh_data_t, 1);
1646 fish = (fish_fh_data_t *) fh->data;
1648 /* File will be written only, so no need to retrieve it */
1649 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1651 /* user pressed the button [ Append ] in the "Copy" dialog */
1652 if ((flags & O_APPEND) != 0)
1653 fish->append = TRUE;
1655 if (fh->ino->localname == NULL)
1657 vfs_path_t *vpath;
1658 int tmp_handle;
1660 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1661 if (tmp_handle == -1)
1663 vfs_path_free (vpath);
1664 goto fail;
1666 fh->ino->localname = g_strdup (vfs_path_as_str (vpath));
1667 vfs_path_free (vpath);
1668 close (tmp_handle);
1670 return 0;
1672 if (fh->ino->localname == NULL && vfs_s_retrieve_file (me, fh->ino) == -1)
1673 goto fail;
1674 if (fh->ino->localname == NULL)
1675 vfs_die ("retrieve_file failed to fill in localname");
1676 return 0;
1678 fail:
1679 fish_fh_free_data (fh);
1680 return -1;
1683 /* --------------------------------------------------------------------------------------------- */
1685 static void
1686 fish_fill_names (struct vfs_class *me, fill_names_f func)
1688 GList *iter;
1690 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1692 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1694 char *name;
1695 char gbuf[10];
1696 const char *flags = "";
1698 switch (super->path_element->port)
1700 case FISH_FLAG_RSH:
1701 flags = ":r";
1702 break;
1703 case FISH_FLAG_COMPRESSED:
1704 flags = ":C";
1705 break;
1706 default:
1707 if (super->path_element->port > FISH_FLAG_RSH)
1709 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1710 flags = gbuf;
1712 break;
1715 name =
1716 g_strconcat (vfs_fish_ops->prefix, VFS_PATH_URL_DELIMITER,
1717 super->path_element->user, "@", super->path_element->host, flags,
1718 PATH_SEP_STR, super->path_element->path, (char *) NULL);
1719 func (name);
1720 g_free (name);
1724 /* --------------------------------------------------------------------------------------------- */
1726 static void *
1727 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1730 sorry, i've places hack here
1731 cause fish don't able to open files with O_EXCL flag
1733 flags &= ~O_EXCL;
1734 return vfs_s_open (vpath, flags, mode);
1737 /* --------------------------------------------------------------------------------------------- */
1738 /*** public functions ****************************************************************************/
1739 /* --------------------------------------------------------------------------------------------- */
1741 void
1742 init_fish (void)
1744 tcp_init ();
1746 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1747 fish_subclass.archive_same = fish_archive_same;
1748 fish_subclass.new_archive = fish_new_archive;
1749 fish_subclass.open_archive = fish_open_archive;
1750 fish_subclass.free_archive = fish_free_archive;
1751 fish_subclass.fh_open = fish_fh_open;
1752 fish_subclass.fh_free_data = fish_fh_free_data;
1753 fish_subclass.dir_load = fish_dir_load;
1754 fish_subclass.file_store = fish_file_store;
1755 fish_subclass.linear_start = fish_linear_start;
1756 fish_subclass.linear_read = fish_linear_read;
1757 fish_subclass.linear_close = fish_linear_close;
1759 vfs_s_init_class (&fish_subclass);
1760 vfs_fish_ops->name = "fish";
1761 vfs_fish_ops->prefix = "sh";
1762 vfs_fish_ops->fill_names = fish_fill_names;
1763 vfs_fish_ops->stat = fish_stat;
1764 vfs_fish_ops->lstat = fish_lstat;
1765 vfs_fish_ops->fstat = fish_fstat;
1766 vfs_fish_ops->chmod = fish_chmod;
1767 vfs_fish_ops->chown = fish_chown;
1768 vfs_fish_ops->utime = fish_utime;
1769 vfs_fish_ops->open = fish_open;
1770 vfs_fish_ops->symlink = fish_symlink;
1771 vfs_fish_ops->link = fish_link;
1772 vfs_fish_ops->unlink = fish_unlink;
1773 vfs_fish_ops->rename = fish_rename;
1774 vfs_fish_ops->mkdir = fish_mkdir;
1775 vfs_fish_ops->rmdir = fish_rmdir;
1776 vfs_fish_ops->ctl = fish_ctl;
1777 vfs_register_class (vfs_fish_ops);
1780 /* --------------------------------------------------------------------------------------------- */