Added new fnction for manipulate vpath objects:
[midnight-commander.git] / src / vfs / fish / fish.c
blob84de9540a891d73738cc716e0c262a6fe223929f
1 /*
2 Virtual File System: FISH implementation for transfering files over
3 shell connections.
5 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
6 2007, 2008, 2009, 2010, 2011
7 The Free Software Foundation, Inc.
9 Written by:
10 Pavel Machek, 1998
11 Michal Svec, 2000
12 Andrew Borodin <aborodin@vmail.ru>, 2010
13 Slava Zanko <slavazanko@gmail.com>, 2010
14 Ilia Maslakov <il.smind@gmail.com>, 2010
16 Derived from ftpfs.c.
18 This file is part of the Midnight Commander.
20 The Midnight Commander is free software: you can redistribute it
21 and/or modify it under the terms of the GNU General Public License as
22 published by the Free Software Foundation, either version 3 of the License,
23 or (at your option) any later version.
25 The Midnight Commander is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 GNU General Public License for more details.
30 You should have received a copy of the GNU General Public License
31 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 /**
35 * \file
36 * \brief Source: Virtual File System: FISH implementation for transfering files over
37 * shell connections
38 * \author Pavel Machek
39 * \author Michal Svec
40 * \date 1998, 2000
42 * Derived from ftpfs.c
43 * Read README.fish for protocol specification.
45 * Syntax of path is: \verbatim /#sh:user@host[:Cr]/path \endverbatim
46 * where C means you want compressed connection,
47 * and r means you want to use rsh
49 * Namespace: fish_vfs_ops exported.
52 /* Define this if your ssh can take -I option */
54 #include <config.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <pwd.h>
58 #include <grp.h>
59 #include <sys/time.h> /* gettimeofday() */
60 #include <stdlib.h>
61 #include <string.h>
62 #include <inttypes.h> /* uintmax_t */
64 #include "lib/global.h"
65 #include "lib/tty/tty.h" /* enable/disable interrupt key */
66 #include "lib/strescape.h"
67 #include "lib/unixcompat.h"
68 #include "lib/fileloc.h"
69 #include "lib/mcconfig.h"
71 #include "src/execute.h" /* pre_exec, post_exec */
73 #include "lib/vfs/vfs.h"
74 #include "lib/vfs/utilvfs.h"
75 #include "lib/vfs/netutil.h"
76 #include "lib/vfs/xdirentry.h"
77 #include "lib/vfs/gc.h" /* vfs_stamp_create */
79 #include "fish.h"
80 #include "fishdef.h"
82 /*** global variables ****************************************************************************/
84 int fish_directory_timeout = 900;
86 /*** file scope macro definitions ****************************************************************/
88 #define DO_RESOLVE_SYMLINK 1
89 #define DO_OPEN 2
90 #define DO_FREE_RESOURCE 4
92 #define FISH_FLAG_COMPRESSED 1
93 #define FISH_FLAG_RSH 2
95 #define OPT_FLUSH 1
96 #define OPT_IGNORE_ERROR 2
99 * Reply codes.
101 #define PRELIM 1 /* positive preliminary */
102 #define COMPLETE 2 /* positive completion */
103 #define CONTINUE 3 /* positive intermediate */
104 #define TRANSIENT 4 /* transient negative completion */
105 #define ERROR 5 /* permanent negative completion */
107 /* command wait_flag: */
108 #define NONE 0x00
109 #define WAIT_REPLY 0x01
110 #define WANT_STRING 0x02
112 /* environment flags */
113 #define FISH_HAVE_HEAD 1
114 #define FISH_HAVE_SED 2
115 #define FISH_HAVE_AWK 4
116 #define FISH_HAVE_PERL 8
117 #define FISH_HAVE_LSQ 16
118 #define FISH_HAVE_DATE_MDYT 32
119 #define FISH_HAVE_TAIL 64
121 #define SUP ((fish_super_data_t *) super->data)
123 /*** file scope type declarations ****************************************************************/
125 typedef struct
127 int sockr;
128 int sockw;
129 char *scr_ls;
130 char *scr_chmod;
131 char *scr_utime;
132 char *scr_exists;
133 char *scr_mkdir;
134 char *scr_unlink;
135 char *scr_chown;
136 char *scr_rmdir;
137 char *scr_ln;
138 char *scr_mv;
139 char *scr_hardlink;
140 char *scr_get;
141 char *scr_send;
142 char *scr_append;
143 char *scr_info;
144 int host_flags;
145 char *scr_env;
146 } fish_super_data_t;
148 typedef struct
150 off_t got;
151 off_t total;
152 gboolean append;
153 } fish_fh_data_t;
155 /*** file scope variables ************************************************************************/
157 static char reply_str[80];
159 static struct vfs_class vfs_fish_ops;
161 /*** file scope functions ************************************************************************/
162 /* --------------------------------------------------------------------------------------------- */
164 static char *
165 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
167 char *scr_filename = NULL;
168 char *scr_content;
169 gsize scr_len = 0;
171 /* 1st: scan user directory */
172 scr_filename = g_build_path (PATH_SEP_STR, mc_config_get_data_path (), FISH_PREFIX, hostname,
173 script_name, (char *) NULL);
174 /* silent about user dir */
175 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
176 g_free (scr_filename);
177 /* 2nd: scan system dir */
178 if (scr_content == NULL)
180 scr_filename =
181 g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
182 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
183 g_free (scr_filename);
186 if (scr_content != NULL)
187 return scr_content;
189 return g_strdup (def_content);
192 /* --------------------------------------------------------------------------------------------- */
194 static int
195 fish_decode_reply (char *s, int was_garbage)
197 int code;
198 if (!sscanf (s, "%d", &code))
200 code = 500;
201 return 5;
203 if (code < 100)
204 return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
205 return code / 100;
208 /* --------------------------------------------------------------------------------------------- */
209 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
211 static int
212 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
214 char answer[BUF_1K];
215 int was_garbage = 0;
217 for (;;)
219 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
221 if (string_buf)
222 *string_buf = 0;
223 return 4;
226 if (strncmp (answer, "### ", 4))
228 was_garbage = 1;
229 if (string_buf)
230 g_strlcpy (string_buf, answer, string_len);
232 else
233 return fish_decode_reply (answer + 4, was_garbage);
237 /* --------------------------------------------------------------------------------------------- */
239 static int
240 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
242 va_list ap;
243 char *str;
244 ssize_t status;
245 FILE *logfile = MEDATA->logfile;
247 va_start (ap, fmt);
249 str = g_strdup_vprintf (fmt, ap);
250 va_end (ap);
252 if (logfile)
254 size_t ret;
255 ret = fwrite (str, strlen (str), 1, logfile);
256 ret = fflush (logfile);
259 tty_enable_interrupt_key ();
261 status = write (SUP->sockw, str, strlen (str));
262 g_free (str);
264 tty_disable_interrupt_key ();
265 if (status < 0)
266 return TRANSIENT;
268 if (wait_reply)
269 return fish_get_reply (me, SUP->sockr,
270 (wait_reply & WANT_STRING) ? reply_str :
271 NULL, sizeof (reply_str) - 1);
272 return COMPLETE;
275 /* --------------------------------------------------------------------------------------------- */
277 static void
278 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
280 if ((SUP->sockw != -1) || (SUP->sockr != -1))
282 vfs_print_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
283 fish_command (me, super, NONE, "#BYE\nexit\n");
284 close (SUP->sockw);
285 close (SUP->sockr);
286 SUP->sockw = SUP->sockr = -1;
288 g_free (SUP->scr_ls);
289 g_free (SUP->scr_exists);
290 g_free (SUP->scr_mkdir);
291 g_free (SUP->scr_unlink);
292 g_free (SUP->scr_chown);
293 g_free (SUP->scr_chmod);
294 g_free (SUP->scr_utime);
295 g_free (SUP->scr_rmdir);
296 g_free (SUP->scr_ln);
297 g_free (SUP->scr_mv);
298 g_free (SUP->scr_hardlink);
299 g_free (SUP->scr_get);
300 g_free (SUP->scr_send);
301 g_free (SUP->scr_append);
302 g_free (SUP->scr_info);
303 g_free (SUP->scr_env);
304 g_free (SUP);
305 super->data = NULL;
308 /* --------------------------------------------------------------------------------------------- */
310 static void
311 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
313 int fileset1[2], fileset2[2];
314 int res;
316 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
317 vfs_die ("Cannot pipe(): %m.");
319 res = fork ();
321 if (res != 0)
323 if (res < 0)
324 vfs_die ("Cannot fork(): %m.");
325 /* We are the parent */
326 close (fileset1[0]);
327 SUP->sockw = fileset1[1];
328 close (fileset2[1]);
329 SUP->sockr = fileset2[0];
331 else
333 res = dup2 (fileset1[0], 0);
334 close (fileset1[0]);
335 close (fileset1[1]);
336 res = dup2 (fileset2[1], 1);
337 close (2);
338 /* stderr to /dev/null */
339 res = open ("/dev/null", O_WRONLY);
340 close (fileset2[0]);
341 close (fileset2[1]);
342 execvp (path, const_cast (char **, argv));
343 _exit (3);
347 /* --------------------------------------------------------------------------------------------- */
349 static char *
350 fish_set_env (int flags)
352 GString *tmp;
354 tmp = g_string_sized_new (250);
355 g_string_assign (tmp, "");
357 if ((flags & FISH_HAVE_HEAD) != 0)
358 g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
360 if ((flags & FISH_HAVE_SED) != 0)
361 g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
363 if ((flags & FISH_HAVE_AWK) != 0)
364 g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
366 if ((flags & FISH_HAVE_PERL) != 0)
367 g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
369 if ((flags & FISH_HAVE_LSQ) != 0)
370 g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
372 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
373 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
375 if ((flags & FISH_HAVE_TAIL) != 0)
376 g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
378 return g_string_free (tmp, FALSE);
381 /* --------------------------------------------------------------------------------------------- */
383 static gboolean
384 fish_info (struct vfs_class *me, struct vfs_s_super *super)
386 char buffer[BUF_8K];
387 if (fish_command (me, super, NONE, SUP->scr_info) == COMPLETE)
389 while (TRUE)
391 int res;
393 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
394 if ((res == 0) || (res == EINTR))
395 ERRNOR (ECONNRESET, FALSE);
396 if (strncmp (buffer, "### ", 4) == 0)
397 break;
398 SUP->host_flags = atol (buffer);
400 return TRUE;
402 ERRNOR (E_PROTO, FALSE);
406 /* --------------------------------------------------------------------------------------------- */
407 /* The returned directory should always contain a trailing slash */
409 static char *
410 fish_getcwd (struct vfs_class *me, struct vfs_s_super *super)
412 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
413 return g_strconcat (reply_str, "/", (char *) NULL);
414 ERRNOR (EIO, NULL);
418 /* --------------------------------------------------------------------------------------------- */
420 static void
421 fish_open_archive_pipeopen (struct vfs_s_super *super)
423 char gbuf[10];
424 const char *argv[10]; /* All of 10 is used now */
425 const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
426 int i = 0;
428 argv[i++] = xsh;
429 if (super->path_element->port == FISH_FLAG_COMPRESSED)
430 argv[i++] = "-C";
432 if (super->path_element->port > FISH_FLAG_RSH)
434 argv[i++] = "-p";
435 g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
436 argv[i++] = gbuf;
440 * Add the user name to the ssh command line only if it was explicitly
441 * set in vfs URL. rsh/ssh will get current user by default
442 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
443 * option breaks it for some)
446 if (super->path_element->user != NULL)
448 argv[i++] = "-l";
449 argv[i++] = super->path_element->user;
451 else
453 /* The rest of the code assumes it to be a valid username */
454 super->path_element->user = vfs_get_local_username ();
457 argv[i++] = super->path_element->host;
458 argv[i++] = "echo FISH:; /bin/sh";
459 argv[i++] = NULL;
461 fish_pipeopen (super, xsh, argv);
464 /* --------------------------------------------------------------------------------------------- */
466 static gboolean
467 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
469 char answer[2048];
471 printf ("\n%s\n", _("fish: Waiting for initial line..."));
473 if (!vfs_s_get_line (me, SUP->sockr, answer, sizeof (answer), ':'))
474 return FALSE;
476 if (strstr (answer, "assword") != NULL)
478 /* Currently, this does not work. ssh reads passwords from
479 /dev/tty, not from stdin :-(. */
481 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
483 return FALSE;
484 #if 0
485 if (super->path_element->password == NULL)
487 char *p, *op;
488 p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
489 op = vfs_get_password (p);
490 g_free (p);
491 if (op == NULL)
492 return FALSE;
493 super->path_element->password = op;
497 printf ("\n%s\n", _("fish: Sending password..."));
500 size_t str_len;
502 str_len = strlen (super->path_element->password);
503 if ((write (SUP.sockw, super->path_element->password, str_len) != (ssize_t) str_len)
504 || (write (SUP->sockw, "\n", 1) != 1))
505 return FALSE;
507 #endif
509 return TRUE;
512 /* --------------------------------------------------------------------------------------------- */
514 static int
515 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
517 gboolean ftalk;
518 /* hide panels */
519 pre_exec ();
521 /* open pipe */
522 fish_open_archive_pipeopen (super);
524 /* Start talk with ssh-server (password prompt, etc ) */
525 ftalk = fish_open_archive_talk (me, super);
527 /* show panels */
528 post_exec ();
530 if (!ftalk)
531 ERRNOR (E_PROTO, -1);
533 vfs_print_message (_("fish: Sending initial line..."));
535 * Run `start_fish_server'. If it doesn't exist - no problem,
536 * we'll talk directly to the shell.
539 if (fish_command
540 (me, super, WAIT_REPLY,
541 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
542 ERRNOR (E_PROTO, -1);
544 vfs_print_message (_("fish: Handshaking version..."));
545 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
546 ERRNOR (E_PROTO, -1);
548 /* Set up remote locale to C, otherwise dates cannot be recognized */
549 if (fish_command
550 (me, super, WAIT_REPLY,
551 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
552 ERRNOR (E_PROTO, -1);
554 vfs_print_message (_("fish: Getting host info..."));
555 if (fish_info (me, super))
556 SUP->scr_env = fish_set_env (SUP->host_flags);
558 vfs_print_message (_("fish: Setting up current directory..."));
559 super->path_element->path = fish_getcwd (me, super);
560 vfs_print_message (_("fish: Connected, home %s."), super->path_element->path);
561 #if 0
562 super->name =
563 g_strconcat ("/#sh:", super->path_element->user, "@", super->path_element->host, "/",
564 (char *) NULL);
565 #else
566 super->name = g_strdup (PATH_SEP_STR);
567 #endif
569 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
570 return 0;
573 /* --------------------------------------------------------------------------------------------- */
575 static int
576 fish_open_archive (struct vfs_s_super *super,
577 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
579 (void) vpath;
581 super->data = g_new0 (fish_super_data_t, 1);
582 super->path_element = vfs_path_element_clone (vpath_element);
584 if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
585 super->path_element->port = FISH_FLAG_RSH;
587 SUP->scr_ls =
588 fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
589 SUP->scr_exists =
590 fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
591 FISH_EXISTS_DEF_CONTENT);
592 SUP->scr_mkdir =
593 fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
594 FISH_MKDIR_DEF_CONTENT);
595 SUP->scr_unlink =
596 fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
597 FISH_UNLINK_DEF_CONTENT);
598 SUP->scr_chown =
599 fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
600 FISH_CHOWN_DEF_CONTENT);
601 SUP->scr_chmod =
602 fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
603 FISH_CHMOD_DEF_CONTENT);
604 SUP->scr_utime =
605 fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
606 FISH_UTIME_DEF_CONTENT);
607 SUP->scr_rmdir =
608 fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
609 FISH_RMDIR_DEF_CONTENT);
610 SUP->scr_ln =
611 fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
612 SUP->scr_mv =
613 fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
614 SUP->scr_hardlink =
615 fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
616 FISH_HARDLINK_DEF_CONTENT);
617 SUP->scr_get =
618 fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
619 SUP->scr_send =
620 fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
621 FISH_SEND_DEF_CONTENT);
622 SUP->scr_append =
623 fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
624 FISH_APPEND_DEF_CONTENT);
625 SUP->scr_info =
626 fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
627 FISH_INFO_DEF_CONTENT);
629 return fish_open_archive_int (vpath_element->class, super);
632 /* --------------------------------------------------------------------------------------------- */
634 static int
635 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
636 const vfs_path_t * vpath, void *cookie)
638 vfs_path_element_t *path_element;
639 int result;
641 (void) vpath;
642 (void) cookie;
644 path_element = vfs_path_element_clone (vpath_element);
646 if (path_element->user == NULL)
647 path_element->user = vfs_get_local_username ();
649 result = ((strcmp (path_element->host, super->path_element->host) == 0)
650 && (strcmp (path_element->user, super->path_element->user) == 0)
651 && (path_element->port == super->path_element->port)) ? 1 : 0;
653 vfs_path_element_free (path_element);
655 return result;
658 /* --------------------------------------------------------------------------------------------- */
660 static int
661 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
663 struct vfs_s_super *super = dir->super;
664 char buffer[8192];
665 struct vfs_s_entry *ent = NULL;
666 FILE *logfile;
667 char *quoted_path;
668 int reply_code;
669 gchar *shell_commands;
672 * Simple FISH debug interface :]
674 #if 0
675 if (!(MEDATA->logfile))
677 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
679 #endif
680 logfile = MEDATA->logfile;
682 vfs_print_message (_("fish: Reading directory %s..."), remote_path);
684 gettimeofday (&dir->timestamp, NULL);
685 dir->timestamp.tv_sec += fish_directory_timeout;
686 quoted_path = strutils_shell_escape (remote_path);
687 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_ls, (char *) NULL);
688 fish_command (me, super, NONE, shell_commands, quoted_path);
689 g_free (shell_commands);
690 g_free (quoted_path);
691 ent = vfs_s_generate_entry (me, NULL, dir, 0);
692 while (TRUE)
694 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
695 if ((!res) || (res == EINTR))
697 vfs_s_free_entry (me, ent);
698 me->verrno = ECONNRESET;
699 goto error;
701 if (logfile)
703 fputs (buffer, logfile);
704 fputs ("\n", logfile);
705 fflush (logfile);
707 if (!strncmp (buffer, "### ", 4))
708 break;
709 if ((!buffer[0]))
711 if (ent->name)
713 vfs_s_insert_entry (me, dir, ent);
714 ent = vfs_s_generate_entry (me, NULL, dir, 0);
716 continue;
719 #define ST ent->ino->st
721 switch (buffer[0])
723 case ':':
725 char *temp;
726 char *data_start = buffer + 1;
727 char *filename = data_start;
728 char *linkname = data_start;
729 char *filename_bound = filename + strlen (filename);
730 char *linkname_bound = filename_bound;
731 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
732 break; /* We'll do "." and ".." ourselves */
734 if (S_ISLNK (ST.st_mode))
736 /* we expect: "escaped-name" -> "escaped-name"
737 // -> cannot occur in filenames,
738 // because it will be escaped to -\> */
740 if (*filename == '"')
741 ++filename;
743 linkname = strstr (filename, "\" -> \"");
744 if (!linkname)
746 /* broken client, or smth goes wrong */
747 linkname = filename_bound;
748 if (filename_bound > filename && *(filename_bound - 1) == '"')
749 --filename_bound; /* skip trailing " */
751 else
753 filename_bound = linkname;
754 linkname += 6; /* strlen ("\" -> \"") */
755 if (*(linkname_bound - 1) == '"')
756 --linkname_bound; /* skip trailing " */
759 ent->name = g_strndup (filename, filename_bound - filename);
760 temp = ent->name;
761 ent->name = strutils_shell_unescape (ent->name);
762 g_free (temp);
764 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
765 temp = ent->ino->linkname;
766 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
767 g_free (temp);
769 else
771 /* we expect: "escaped-name" */
772 if (filename_bound - filename > 2)
775 there is at least 2 "
776 and we skip them
778 if (*filename == '"')
779 ++filename;
780 if (*(filename_bound - 1) == '"')
781 --filename_bound;
783 ent->name = g_strndup (filename, filename_bound - filename);
784 temp = ent->name;
785 ent->name = strutils_shell_unescape (ent->name);
786 g_free (temp);
788 break;
790 case 'S':
791 #ifdef HAVE_ATOLL
792 ST.st_size = (off_t) atoll (buffer + 1);
793 #else
794 ST.st_size = (off_t) atof (buffer + 1);
795 #endif
796 break;
797 case 'P':
799 size_t skipped;
800 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
801 break;
803 case 'R':
806 raw filemode:
807 we expect: Roctal-filemode octal-filetype uid.gid
809 size_t skipped;
810 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
811 break;
813 case 'd':
815 vfs_split_text (buffer + 1);
816 if (!vfs_parse_filedate (0, &ST.st_ctime))
817 break;
818 ST.st_atime = ST.st_mtime = ST.st_ctime;
820 break;
821 case 'D':
823 struct tm tim;
824 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
825 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
826 break;
827 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
829 break;
830 case 'E':
832 int maj, min;
833 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
834 break;
835 #ifdef HAVE_STRUCT_STAT_ST_RDEV
836 ST.st_rdev = makedev (maj, min);
837 #endif
842 vfs_s_free_entry (me, ent);
843 reply_code = fish_decode_reply (buffer + 4, 0);
844 if (reply_code == COMPLETE)
846 g_free (super->path_element->path);
847 super->path_element->path = g_strdup (remote_path);
848 vfs_print_message (_("%s: done."), me->name);
849 return 0;
852 me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
854 error:
855 vfs_print_message (_("%s: failure"), me->name);
856 return -1;
859 /* --------------------------------------------------------------------------------------------- */
861 static int
862 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
864 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
865 gchar *shell_commands = NULL;
866 struct vfs_s_super *super = FH_SUPER;
867 int n, total;
868 char buffer[8192];
869 struct stat s;
870 int was_error = 0;
871 int h;
872 char *quoted_name;
874 h = open (localname, O_RDONLY);
875 if (h == -1)
876 ERRNOR (EIO, -1);
877 if (fstat (h, &s) < 0)
879 close (h);
880 ERRNOR (EIO, -1);
883 /* First, try this as stor:
885 * ( head -c number ) | ( cat > file; cat >/dev/null )
887 * If `head' is not present on the remote system, `dd' will be used.
888 * Unfortunately, we cannot trust most non-GNU `head' implementations
889 * even if `-c' options is supported. Therefore, we separate GNU head
890 * (and other modern heads?) using `-q' and `-' . This causes another
891 * implementations to fail (because of "incorrect options").
893 * Fallback is:
895 * rest=<number>
896 * while [ $rest -gt 0 ]
897 * do
898 * cnt=`expr \( $rest + 255 \) / 256`
899 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
900 * rest=`expr $rest - $n`
901 * done
903 * `dd' was not designed for full filling of input buffers,
904 * and does not report exact number of bytes (not blocks).
905 * Therefore a more complex shell script is needed.
907 * On some systems non-GNU head writes "Usage:" error report to stdout
908 * instead of stderr. It makes impossible the use of "head || dd"
909 * algorithm for file appending case, therefore just "dd" is used for it.
912 quoted_name = strutils_shell_escape (name);
913 vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
915 /* FIXME: File size is limited to ULONG_MAX */
916 if (fish->append)
918 shell_commands =
919 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
920 SUP->scr_append, (char *) NULL);
922 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
923 (uintmax_t) s.st_size);
924 g_free (shell_commands);
926 else
928 shell_commands =
929 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
930 SUP->scr_send, (char *) NULL);
931 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
932 (uintmax_t) s.st_size);
933 g_free (shell_commands);
935 if (n != PRELIM)
937 close (h);
938 ERRNOR (E_REMOTE, -1);
941 total = 0;
943 while (TRUE)
945 int t;
946 while ((n = read (h, buffer, sizeof (buffer))) < 0)
948 if ((errno == EINTR) && tty_got_interrupt ())
949 continue;
950 vfs_print_message (_("fish: Local read failed, sending zeros"));
951 close (h);
952 h = open ("/dev/zero", O_RDONLY);
955 if (n == 0)
956 break;
958 t = write (SUP->sockw, buffer, n);
959 if (t != n)
961 if (t == -1)
962 me->verrno = errno;
963 else
964 me->verrno = EIO;
965 goto error_return;
967 tty_disable_interrupt_key ();
968 total += n;
969 vfs_print_message ("%s: %d/%" PRIuMAX,
970 was_error ? _("fish: storing zeros") : _("fish: storing file"),
971 total, (uintmax_t) s.st_size);
973 close (h);
974 g_free (quoted_name);
976 if ((fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE) || was_error)
977 ERRNOR (E_REMOTE, -1);
978 return 0;
980 error_return:
981 close (h);
982 fish_get_reply (me, SUP->sockr, NULL, 0);
983 g_free (quoted_name);
984 return -1;
987 /* --------------------------------------------------------------------------------------------- */
989 static int
990 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
992 fish_fh_data_t *fish;
993 gchar *shell_commands = NULL;
994 struct vfs_s_super *super = FH_SUPER;
995 char *name;
996 char *quoted_name;
998 if (fh->data == NULL)
999 fh->data = g_new0 (fish_fh_data_t, 1);
1001 fish = (fish_fh_data_t *) fh->data;
1003 name = vfs_s_fullpath (me, fh->ino);
1004 if (name == NULL)
1005 return 0;
1006 quoted_name = strutils_shell_escape (name);
1007 g_free (name);
1008 fish->append = FALSE;
1011 * Check whether the remote file is readable by using `dd' to copy
1012 * a single byte from the remote file to /dev/null. If `dd' completes
1013 * with exit status of 0 use `cat' to send the file contents to the
1014 * standard output (i.e. over the network).
1017 shell_commands =
1018 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
1019 SUP->scr_get, (char *) NULL);
1020 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
1021 g_free (shell_commands);
1022 g_free (quoted_name);
1023 if (offset != PRELIM)
1024 ERRNOR (E_REMOTE, 0);
1025 fh->linear = LS_LINEAR_OPEN;
1026 fish->got = 0;
1027 errno = 0;
1028 #if SIZEOF_OFF_T == SIZEOF_LONG
1029 fish->total = (off_t) strtol (reply_str, NULL, 10);
1030 #else
1031 fish->total = (off_t) strtoll (reply_str, NULL, 10);
1032 #endif
1033 if (errno != 0)
1034 ERRNOR (E_REMOTE, 0);
1035 return 1;
1038 /* --------------------------------------------------------------------------------------------- */
1040 static void
1041 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
1043 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1044 struct vfs_s_super *super = FH_SUPER;
1045 char buffer[BUF_8K];
1046 int n;
1048 vfs_print_message (_("Aborting transfer..."));
1052 n = MIN (sizeof (buffer), (size_t) (fish->total - fish->got));
1053 if (n != 0)
1055 n = read (SUP->sockr, buffer, n);
1056 if (n < 0)
1057 return;
1058 fish->got += n;
1061 while (n != 0);
1063 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1064 vfs_print_message (_("Error reported after abort."));
1065 else
1066 vfs_print_message (_("Aborted transfer would be successful."));
1069 /* --------------------------------------------------------------------------------------------- */
1071 static int
1072 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
1074 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1075 struct vfs_s_super *super = FH_SUPER;
1076 ssize_t n = 0;
1079 len = MIN ((size_t) (fish->total - fish->got), len);
1080 tty_disable_interrupt_key ();
1081 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1083 if ((errno == EINTR) && !tty_got_interrupt ())
1084 continue;
1085 break;
1087 tty_enable_interrupt_key ();
1089 if (n > 0)
1090 fish->got += n;
1091 else if (n < 0)
1092 fish_linear_abort (me, fh);
1093 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1094 ERRNOR (E_REMOTE, -1);
1095 ERRNOR (errno, n);
1098 /* --------------------------------------------------------------------------------------------- */
1100 static void
1101 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1103 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1105 if (fish->total != fish->got)
1106 fish_linear_abort (me, fh);
1109 /* --------------------------------------------------------------------------------------------- */
1111 static int
1112 fish_ctl (void *fh, int ctlop, void *arg)
1114 (void) arg;
1115 (void) fh;
1116 (void) ctlop;
1117 return 0;
1118 #if 0
1119 switch (ctlop)
1121 case VFS_CTL_IS_NOTREADY:
1123 int v;
1125 if (!FH->linear)
1126 vfs_die ("You may not do this");
1127 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1128 return 0;
1130 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1131 if (((v < 0) && (errno == EINTR)) || v == 0)
1132 return 1;
1133 return 0;
1135 default:
1136 return 0;
1138 #endif
1141 /* --------------------------------------------------------------------------------------------- */
1143 static int
1144 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1146 int r;
1148 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1149 vfs_stamp_create (&vfs_fish_ops, super);
1150 if (r != COMPLETE)
1151 ERRNOR (E_REMOTE, -1);
1152 if (flags & OPT_FLUSH)
1153 vfs_s_invalidate (me, super);
1154 return 0;
1157 /* --------------------------------------------------------------------------------------------- */
1159 static int
1160 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1162 gchar *shell_commands = NULL;
1163 char buf[BUF_LARGE];
1164 const char *crpath1, *crpath2;
1165 char *rpath1, *rpath2;
1166 struct vfs_s_super *super, *super2;
1167 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath1, -1);
1169 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1170 if (crpath1 == NULL)
1171 return -1;
1173 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1174 if (crpath2 == NULL)
1175 return -1;
1177 rpath1 = strutils_shell_escape (crpath1);
1178 rpath2 = strutils_shell_escape (crpath2);
1179 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1180 SUP->scr_mv, (char *) NULL);
1181 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1182 g_free (shell_commands);
1183 g_free (rpath1);
1184 g_free (rpath2);
1185 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1188 /* --------------------------------------------------------------------------------------------- */
1190 static int
1191 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1193 gchar *shell_commands = NULL;
1194 char buf[BUF_LARGE];
1195 const char *crpath1, *crpath2;
1196 char *rpath1, *rpath2;
1197 struct vfs_s_super *super, *super2;
1198 vfs_path_element_t *path_element;
1201 path_element = vfs_path_get_by_index (vpath1, -1);
1203 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1204 if (crpath1 == NULL)
1205 return -1;
1207 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1208 if (crpath2 == NULL)
1209 return -1;
1211 rpath1 = strutils_shell_escape (crpath1);
1212 rpath2 = strutils_shell_escape (crpath2);
1213 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1214 SUP->scr_hardlink, (char *) NULL);
1215 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1216 g_free (shell_commands);
1217 g_free (rpath1);
1218 g_free (rpath2);
1219 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1223 /* --------------------------------------------------------------------------------------------- */
1225 static int
1226 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1228 char *qsetto;
1229 gchar *shell_commands = NULL;
1230 char buf[BUF_LARGE];
1231 const char *crpath;
1232 char *rpath;
1233 struct vfs_s_super *super;
1234 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath2, -1);
1236 crpath = vfs_s_get_path (vpath2, &super, 0);
1237 if (crpath == NULL)
1238 return -1;
1240 rpath = strutils_shell_escape (crpath);
1241 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1243 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1244 SUP->scr_ln, (char *) NULL);
1245 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1246 g_free (shell_commands);
1247 g_free (qsetto);
1248 g_free (rpath);
1249 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1252 /* --------------------------------------------------------------------------------------------- */
1254 static int
1255 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1257 gchar *shell_commands = NULL;
1258 char buf[BUF_LARGE];
1259 const char *crpath;
1260 char *rpath;
1261 struct vfs_s_super *super;
1262 vfs_path_element_t *path_element;
1264 path_element = vfs_path_get_by_index (vpath, -1);
1266 crpath = vfs_s_get_path (vpath, &super, 0);
1267 if (crpath == NULL)
1268 return -1;
1269 rpath = strutils_shell_escape (crpath);
1271 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1272 SUP->scr_chmod, (char *) NULL);
1273 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (int) (mode & 07777));
1274 g_free (shell_commands);
1275 g_free (rpath);
1276 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1279 /* --------------------------------------------------------------------------------------------- */
1281 static int
1282 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1284 char *sowner, *sgroup;
1285 struct passwd *pw;
1286 struct group *gr;
1288 pw = getpwuid (owner);
1289 if (pw == NULL)
1290 return 0;
1292 gr = getgrgid (group);
1293 if (gr == NULL)
1294 return 0;
1296 sowner = pw->pw_name;
1297 sgroup = gr->gr_name;
1300 gchar *shell_commands = NULL;
1301 char buf[BUF_LARGE];
1302 const char *crpath;
1303 char *rpath;
1304 struct vfs_s_super *super;
1305 vfs_path_element_t *path_element;
1307 path_element = vfs_path_get_by_index (vpath, -1);
1310 crpath = vfs_s_get_path (vpath, &super, 0);
1311 if (crpath == NULL)
1312 return -1;
1313 rpath = strutils_shell_escape (crpath);
1315 shell_commands = g_strconcat (SUP->scr_env,
1316 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1317 SUP->scr_chown, (char *) NULL);
1318 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1319 g_free (shell_commands);
1320 fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1321 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1322 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1323 g_free (rpath);
1324 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1328 /* --------------------------------------------------------------------------------------------- */
1330 static int
1331 fish_utime (const vfs_path_t * vpath, struct utimbuf *times)
1333 gchar *shell_commands = NULL;
1334 char utcmtime[16], utcatime[16];
1335 struct tm *gmt;
1337 char buf[BUF_LARGE];
1338 const char *crpath;
1339 char *rpath;
1340 struct vfs_s_super *super;
1341 vfs_path_element_t *path_element;
1343 path_element = vfs_path_get_by_index (vpath, -1);
1344 crpath = vfs_s_get_path (vpath, &super, 0);
1345 if (crpath == NULL)
1346 return -1;
1347 rpath = strutils_shell_escape (crpath);
1349 gmt = gmtime (&times->modtime);
1350 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1351 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1352 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1354 gmt = gmtime (&times->actime);
1355 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1356 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1357 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1359 shell_commands =
1360 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
1361 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n", SUP->scr_utime, (char *) NULL);
1362 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (long) times->actime,
1363 (long) times->modtime, utcatime, utcmtime);
1364 g_free (shell_commands);
1365 g_free (rpath);
1366 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1369 /* --------------------------------------------------------------------------------------------- */
1371 static int
1372 fish_unlink (const vfs_path_t * vpath)
1374 gchar *shell_commands = NULL;
1376 char buf[BUF_LARGE];
1377 const char *crpath;
1378 char *rpath;
1379 struct vfs_s_super *super;
1380 vfs_path_element_t *path_element;
1382 path_element = vfs_path_get_by_index (vpath, -1);
1383 crpath = vfs_s_get_path (vpath, &super, 0);
1384 if (crpath == NULL)
1385 return -1;
1386 rpath = strutils_shell_escape (crpath);
1388 shell_commands =
1389 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_unlink, (char *) NULL);
1390 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1391 g_free (shell_commands);
1392 g_free (rpath);
1393 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1396 /* --------------------------------------------------------------------------------------------- */
1398 static int
1399 fish_exists (const vfs_path_t * vpath)
1401 gchar *shell_commands = NULL;
1403 char buf[BUF_LARGE];
1404 const char *crpath;
1405 char *rpath;
1406 struct vfs_s_super *super;
1407 vfs_path_element_t *path_element;
1409 path_element = vfs_path_get_by_index (vpath, -1);
1410 crpath = vfs_s_get_path (vpath, &super, 0);
1411 if (crpath == NULL)
1412 return -1;
1413 rpath = strutils_shell_escape (crpath);
1415 shell_commands =
1416 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_exists, (char *) NULL);
1417 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1418 g_free (shell_commands);
1419 g_free (rpath);
1421 return (fish_send_command (path_element->class, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1424 /* --------------------------------------------------------------------------------------------- */
1426 static int
1427 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1429 gchar *shell_commands = NULL;
1430 int ret_code;
1431 char buf[BUF_LARGE];
1432 const char *crpath;
1433 char *rpath;
1434 struct vfs_s_super *super;
1435 vfs_path_element_t *path_element;
1437 (void) mode;
1439 path_element = vfs_path_get_by_index (vpath, -1);
1441 crpath = vfs_s_get_path (vpath, &super, 0);
1442 if (crpath == NULL)
1443 return -1;
1444 rpath = strutils_shell_escape (crpath);
1446 shell_commands =
1447 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_mkdir, (char *) NULL);
1448 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1449 g_free (shell_commands);
1451 g_free (rpath);
1452 ret_code = fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1454 if (ret_code != 0)
1455 return ret_code;
1457 if (!fish_exists (vpath))
1459 path_element->class->verrno = EACCES;
1460 return -1;
1462 return 0;
1465 /* --------------------------------------------------------------------------------------------- */
1467 static int
1468 fish_rmdir (const vfs_path_t * vpath)
1470 gchar *shell_commands = NULL;
1471 char buf[BUF_LARGE];
1472 const char *crpath;
1473 char *rpath;
1474 struct vfs_s_super *super;
1475 vfs_path_element_t *path_element;
1477 path_element = vfs_path_get_by_index (vpath, -1);
1479 crpath = vfs_s_get_path (vpath, &super, 0);
1480 if (crpath == NULL)
1481 return -1;
1482 rpath = strutils_shell_escape (crpath);
1484 shell_commands =
1485 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_rmdir, (char *) NULL);
1486 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1487 g_free (shell_commands);
1488 g_free (rpath);
1489 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1492 /* --------------------------------------------------------------------------------------------- */
1494 static void
1495 fish_fh_free_data (vfs_file_handler_t * fh)
1497 if (fh != NULL)
1499 g_free (fh->data);
1500 fh->data = NULL;
1504 /* --------------------------------------------------------------------------------------------- */
1506 static int
1507 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1509 fish_fh_data_t *fish;
1511 (void) mode;
1513 fh->data = g_new0 (fish_fh_data_t, 1);
1514 fish = (fish_fh_data_t *) fh->data;
1516 /* File will be written only, so no need to retrieve it */
1517 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1519 /* user pressed the button [ Append ] in the "Copy" dialog */
1520 if ((flags & O_APPEND) != 0)
1521 fish->append = TRUE;
1523 if (!fh->ino->localname)
1525 vfs_path_t *vpath;
1526 int tmp_handle;
1528 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1529 if (tmp_handle == -1)
1531 vfs_path_free (vpath);
1532 goto fail;
1534 fh->ino->localname = vfs_path_to_str (vpath);
1535 vfs_path_free (vpath);
1536 close (tmp_handle);
1538 return 0;
1540 if (!fh->ino->localname && vfs_s_retrieve_file (me, fh->ino) == -1)
1541 goto fail;
1542 if (!fh->ino->localname)
1543 vfs_die ("retrieve_file failed to fill in localname");
1544 return 0;
1546 fail:
1547 fish_fh_free_data (fh);
1548 return -1;
1551 /* --------------------------------------------------------------------------------------------- */
1553 static void
1554 fish_fill_names (struct vfs_class *me, fill_names_f func)
1556 GList *iter;
1558 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1560 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1562 char *name;
1563 char gbuf[10];
1564 const char *flags = "";
1566 switch (super->path_element->port)
1568 case FISH_FLAG_RSH:
1569 flags = ":r";
1570 break;
1571 case FISH_FLAG_COMPRESSED:
1572 flags = ":C";
1573 break;
1574 default:
1575 if (super->path_element->port > FISH_FLAG_RSH)
1577 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1578 flags = gbuf;
1580 break;
1583 name =
1584 g_strconcat (vfs_fish_ops.prefix, VFS_PATH_URL_DELIMITER,
1585 super->path_element->user, "@", super->path_element->host, flags, "/",
1586 super->path_element->path, (char *) NULL);
1587 func (name);
1588 g_free (name);
1592 /* --------------------------------------------------------------------------------------------- */
1594 static void *
1595 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1598 sorry, i've places hack here
1599 cause fish don't able to open files with O_EXCL flag
1601 flags &= ~O_EXCL;
1602 return vfs_s_open (vpath, flags, mode);
1605 /* --------------------------------------------------------------------------------------------- */
1606 /*** public functions ****************************************************************************/
1607 /* --------------------------------------------------------------------------------------------- */
1609 void
1610 init_fish (void)
1612 static struct vfs_s_subclass fish_subclass;
1614 tcp_init ();
1616 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1617 fish_subclass.archive_same = fish_archive_same;
1618 fish_subclass.open_archive = fish_open_archive;
1619 fish_subclass.free_archive = fish_free_archive;
1620 fish_subclass.fh_open = fish_fh_open;
1621 fish_subclass.fh_free_data = fish_fh_free_data;
1622 fish_subclass.dir_load = fish_dir_load;
1623 fish_subclass.file_store = fish_file_store;
1624 fish_subclass.linear_start = fish_linear_start;
1625 fish_subclass.linear_read = fish_linear_read;
1626 fish_subclass.linear_close = fish_linear_close;
1628 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1629 vfs_fish_ops.name = "fish";
1630 vfs_fish_ops.prefix = "sh";
1631 vfs_fish_ops.fill_names = fish_fill_names;
1632 vfs_fish_ops.chmod = fish_chmod;
1633 vfs_fish_ops.chown = fish_chown;
1634 vfs_fish_ops.utime = fish_utime;
1635 vfs_fish_ops.open = fish_open;
1636 vfs_fish_ops.symlink = fish_symlink;
1637 vfs_fish_ops.link = fish_link;
1638 vfs_fish_ops.unlink = fish_unlink;
1639 vfs_fish_ops.rename = fish_rename;
1640 vfs_fish_ops.mkdir = fish_mkdir;
1641 vfs_fish_ops.rmdir = fish_rmdir;
1642 vfs_fish_ops.ctl = fish_ctl;
1643 vfs_register_class (&vfs_fish_ops);
1646 /* --------------------------------------------------------------------------------------------- */