Ticket #2779: Active VFS directories list contain incorrect current path
[midnight-commander.git] / src / vfs / fish / fish.c
blobf18d9ff5390e257c105e19294d1a87281d6f2d5a
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 /* --------------------------------------------------------------------------------------------- */
408 static void
409 fish_open_archive_pipeopen (struct vfs_s_super *super)
411 char gbuf[10];
412 const char *argv[10]; /* All of 10 is used now */
413 const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
414 int i = 0;
416 argv[i++] = xsh;
417 if (super->path_element->port == FISH_FLAG_COMPRESSED)
418 argv[i++] = "-C";
420 if (super->path_element->port > FISH_FLAG_RSH)
422 argv[i++] = "-p";
423 g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
424 argv[i++] = gbuf;
428 * Add the user name to the ssh command line only if it was explicitly
429 * set in vfs URL. rsh/ssh will get current user by default
430 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
431 * option breaks it for some)
434 if (super->path_element->user != NULL)
436 argv[i++] = "-l";
437 argv[i++] = super->path_element->user;
439 else
441 /* The rest of the code assumes it to be a valid username */
442 super->path_element->user = vfs_get_local_username ();
445 argv[i++] = super->path_element->host;
446 argv[i++] = "echo FISH:; /bin/sh";
447 argv[i++] = NULL;
449 fish_pipeopen (super, xsh, argv);
452 /* --------------------------------------------------------------------------------------------- */
454 static gboolean
455 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
457 char answer[2048];
459 printf ("\n%s\n", _("fish: Waiting for initial line..."));
461 if (!vfs_s_get_line (me, SUP->sockr, answer, sizeof (answer), ':'))
462 return FALSE;
464 if (strstr (answer, "assword") != NULL)
466 /* Currently, this does not work. ssh reads passwords from
467 /dev/tty, not from stdin :-(. */
469 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
471 return FALSE;
472 #if 0
473 if (super->path_element->password == NULL)
475 char *p, *op;
476 p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
477 op = vfs_get_password (p);
478 g_free (p);
479 if (op == NULL)
480 return FALSE;
481 super->path_element->password = op;
485 printf ("\n%s\n", _("fish: Sending password..."));
488 size_t str_len;
490 str_len = strlen (super->path_element->password);
491 if ((write (SUP.sockw, super->path_element->password, str_len) != (ssize_t) str_len)
492 || (write (SUP->sockw, "\n", 1) != 1))
493 return FALSE;
495 #endif
497 return TRUE;
500 /* --------------------------------------------------------------------------------------------- */
502 static int
503 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
505 gboolean ftalk;
506 /* hide panels */
507 pre_exec ();
509 /* open pipe */
510 fish_open_archive_pipeopen (super);
512 /* Start talk with ssh-server (password prompt, etc ) */
513 ftalk = fish_open_archive_talk (me, super);
515 /* show panels */
516 post_exec ();
518 if (!ftalk)
519 ERRNOR (E_PROTO, -1);
521 vfs_print_message (_("fish: Sending initial line..."));
523 * Run `start_fish_server'. If it doesn't exist - no problem,
524 * we'll talk directly to the shell.
527 if (fish_command
528 (me, super, WAIT_REPLY,
529 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
530 ERRNOR (E_PROTO, -1);
532 vfs_print_message (_("fish: Handshaking version..."));
533 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
534 ERRNOR (E_PROTO, -1);
536 /* Set up remote locale to C, otherwise dates cannot be recognized */
537 if (fish_command
538 (me, super, WAIT_REPLY,
539 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
540 ERRNOR (E_PROTO, -1);
542 vfs_print_message (_("fish: Getting host info..."));
543 if (fish_info (me, super))
544 SUP->scr_env = fish_set_env (SUP->host_flags);
546 vfs_print_message (_("fish: Connected, home %s."), super->path_element->path);
547 #if 0
548 super->name =
549 g_strconcat ("/#sh:", super->path_element->user, "@", super->path_element->host, "/",
550 (char *) NULL);
551 #else
552 super->name = g_strdup (PATH_SEP_STR);
553 #endif
555 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
556 return 0;
559 /* --------------------------------------------------------------------------------------------- */
561 static int
562 fish_open_archive (struct vfs_s_super *super,
563 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
565 (void) vpath;
567 super->data = g_new0 (fish_super_data_t, 1);
568 super->path_element = vfs_path_element_clone (vpath_element);
570 if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
571 super->path_element->port = FISH_FLAG_RSH;
573 SUP->scr_ls =
574 fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
575 SUP->scr_exists =
576 fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
577 FISH_EXISTS_DEF_CONTENT);
578 SUP->scr_mkdir =
579 fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
580 FISH_MKDIR_DEF_CONTENT);
581 SUP->scr_unlink =
582 fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
583 FISH_UNLINK_DEF_CONTENT);
584 SUP->scr_chown =
585 fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
586 FISH_CHOWN_DEF_CONTENT);
587 SUP->scr_chmod =
588 fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
589 FISH_CHMOD_DEF_CONTENT);
590 SUP->scr_utime =
591 fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
592 FISH_UTIME_DEF_CONTENT);
593 SUP->scr_rmdir =
594 fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
595 FISH_RMDIR_DEF_CONTENT);
596 SUP->scr_ln =
597 fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
598 SUP->scr_mv =
599 fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
600 SUP->scr_hardlink =
601 fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
602 FISH_HARDLINK_DEF_CONTENT);
603 SUP->scr_get =
604 fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
605 SUP->scr_send =
606 fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
607 FISH_SEND_DEF_CONTENT);
608 SUP->scr_append =
609 fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
610 FISH_APPEND_DEF_CONTENT);
611 SUP->scr_info =
612 fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
613 FISH_INFO_DEF_CONTENT);
615 return fish_open_archive_int (vpath_element->class, super);
618 /* --------------------------------------------------------------------------------------------- */
620 static int
621 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
622 const vfs_path_t * vpath, void *cookie)
624 vfs_path_element_t *path_element;
625 int result;
627 (void) vpath;
628 (void) cookie;
630 path_element = vfs_path_element_clone (vpath_element);
632 if (path_element->user == NULL)
633 path_element->user = vfs_get_local_username ();
635 result = ((strcmp (path_element->host, super->path_element->host) == 0)
636 && (strcmp (path_element->user, super->path_element->user) == 0)
637 && (path_element->port == super->path_element->port)) ? 1 : 0;
639 vfs_path_element_free (path_element);
641 return result;
644 /* --------------------------------------------------------------------------------------------- */
646 static int
647 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
649 struct vfs_s_super *super = dir->super;
650 char buffer[8192];
651 struct vfs_s_entry *ent = NULL;
652 FILE *logfile;
653 char *quoted_path;
654 int reply_code;
655 gchar *shell_commands;
658 * Simple FISH debug interface :]
660 #if 0
661 if (!(MEDATA->logfile))
663 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
665 #endif
666 logfile = MEDATA->logfile;
668 vfs_print_message (_("fish: Reading directory %s..."), remote_path);
670 gettimeofday (&dir->timestamp, NULL);
671 dir->timestamp.tv_sec += fish_directory_timeout;
672 quoted_path = strutils_shell_escape (remote_path);
673 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_ls, (char *) NULL);
674 fish_command (me, super, NONE, shell_commands, quoted_path);
675 g_free (shell_commands);
676 g_free (quoted_path);
677 ent = vfs_s_generate_entry (me, NULL, dir, 0);
678 while (TRUE)
680 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
681 if ((!res) || (res == EINTR))
683 vfs_s_free_entry (me, ent);
684 me->verrno = ECONNRESET;
685 goto error;
687 if (logfile)
689 fputs (buffer, logfile);
690 fputs ("\n", logfile);
691 fflush (logfile);
693 if (!strncmp (buffer, "### ", 4))
694 break;
695 if ((!buffer[0]))
697 if (ent->name)
699 vfs_s_insert_entry (me, dir, ent);
700 ent = vfs_s_generate_entry (me, NULL, dir, 0);
702 continue;
705 #define ST ent->ino->st
707 switch (buffer[0])
709 case ':':
711 char *temp;
712 char *data_start = buffer + 1;
713 char *filename = data_start;
714 char *linkname = data_start;
715 char *filename_bound = filename + strlen (filename);
716 char *linkname_bound = filename_bound;
717 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
718 break; /* We'll do "." and ".." ourselves */
720 if (S_ISLNK (ST.st_mode))
722 /* we expect: "escaped-name" -> "escaped-name"
723 // -> cannot occur in filenames,
724 // because it will be escaped to -\> */
726 if (*filename == '"')
727 ++filename;
729 linkname = strstr (filename, "\" -> \"");
730 if (!linkname)
732 /* broken client, or smth goes wrong */
733 linkname = filename_bound;
734 if (filename_bound > filename && *(filename_bound - 1) == '"')
735 --filename_bound; /* skip trailing " */
737 else
739 filename_bound = linkname;
740 linkname += 6; /* strlen ("\" -> \"") */
741 if (*(linkname_bound - 1) == '"')
742 --linkname_bound; /* skip trailing " */
745 ent->name = g_strndup (filename, filename_bound - filename);
746 temp = ent->name;
747 ent->name = strutils_shell_unescape (ent->name);
748 g_free (temp);
750 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
751 temp = ent->ino->linkname;
752 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
753 g_free (temp);
755 else
757 /* we expect: "escaped-name" */
758 if (filename_bound - filename > 2)
761 there is at least 2 "
762 and we skip them
764 if (*filename == '"')
765 ++filename;
766 if (*(filename_bound - 1) == '"')
767 --filename_bound;
769 ent->name = g_strndup (filename, filename_bound - filename);
770 temp = ent->name;
771 ent->name = strutils_shell_unescape (ent->name);
772 g_free (temp);
774 break;
776 case 'S':
777 #ifdef HAVE_ATOLL
778 ST.st_size = (off_t) atoll (buffer + 1);
779 #else
780 ST.st_size = (off_t) atof (buffer + 1);
781 #endif
782 break;
783 case 'P':
785 size_t skipped;
786 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
787 break;
789 case 'R':
792 raw filemode:
793 we expect: Roctal-filemode octal-filetype uid.gid
795 size_t skipped;
796 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
797 break;
799 case 'd':
801 vfs_split_text (buffer + 1);
802 if (!vfs_parse_filedate (0, &ST.st_ctime))
803 break;
804 ST.st_atime = ST.st_mtime = ST.st_ctime;
806 break;
807 case 'D':
809 struct tm tim;
810 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
811 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
812 break;
813 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
815 break;
816 case 'E':
818 int maj, min;
819 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
820 break;
821 #ifdef HAVE_STRUCT_STAT_ST_RDEV
822 ST.st_rdev = makedev (maj, min);
823 #endif
828 vfs_s_free_entry (me, ent);
829 reply_code = fish_decode_reply (buffer + 4, 0);
830 if (reply_code == COMPLETE)
832 vfs_print_message (_("%s: done."), me->name);
833 return 0;
836 me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
838 error:
839 vfs_print_message (_("%s: failure"), me->name);
840 return -1;
843 /* --------------------------------------------------------------------------------------------- */
845 static int
846 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
848 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
849 gchar *shell_commands = NULL;
850 struct vfs_s_super *super = FH_SUPER;
851 int n, total;
852 char buffer[8192];
853 struct stat s;
854 int was_error = 0;
855 int h;
856 char *quoted_name;
858 h = open (localname, O_RDONLY);
859 if (h == -1)
860 ERRNOR (EIO, -1);
861 if (fstat (h, &s) < 0)
863 close (h);
864 ERRNOR (EIO, -1);
867 /* First, try this as stor:
869 * ( head -c number ) | ( cat > file; cat >/dev/null )
871 * If `head' is not present on the remote system, `dd' will be used.
872 * Unfortunately, we cannot trust most non-GNU `head' implementations
873 * even if `-c' options is supported. Therefore, we separate GNU head
874 * (and other modern heads?) using `-q' and `-' . This causes another
875 * implementations to fail (because of "incorrect options").
877 * Fallback is:
879 * rest=<number>
880 * while [ $rest -gt 0 ]
881 * do
882 * cnt=`expr \( $rest + 255 \) / 256`
883 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
884 * rest=`expr $rest - $n`
885 * done
887 * `dd' was not designed for full filling of input buffers,
888 * and does not report exact number of bytes (not blocks).
889 * Therefore a more complex shell script is needed.
891 * On some systems non-GNU head writes "Usage:" error report to stdout
892 * instead of stderr. It makes impossible the use of "head || dd"
893 * algorithm for file appending case, therefore just "dd" is used for it.
896 quoted_name = strutils_shell_escape (name);
897 vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
899 /* FIXME: File size is limited to ULONG_MAX */
900 if (fish->append)
902 shell_commands =
903 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
904 SUP->scr_append, (char *) NULL);
906 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
907 (uintmax_t) s.st_size);
908 g_free (shell_commands);
910 else
912 shell_commands =
913 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
914 SUP->scr_send, (char *) NULL);
915 n = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
916 (uintmax_t) s.st_size);
917 g_free (shell_commands);
919 if (n != PRELIM)
921 close (h);
922 ERRNOR (E_REMOTE, -1);
925 total = 0;
927 while (TRUE)
929 int t;
930 while ((n = read (h, buffer, sizeof (buffer))) < 0)
932 if ((errno == EINTR) && tty_got_interrupt ())
933 continue;
934 vfs_print_message (_("fish: Local read failed, sending zeros"));
935 close (h);
936 h = open ("/dev/zero", O_RDONLY);
939 if (n == 0)
940 break;
942 t = write (SUP->sockw, buffer, n);
943 if (t != n)
945 if (t == -1)
946 me->verrno = errno;
947 else
948 me->verrno = EIO;
949 goto error_return;
951 tty_disable_interrupt_key ();
952 total += n;
953 vfs_print_message ("%s: %d/%" PRIuMAX,
954 was_error ? _("fish: storing zeros") : _("fish: storing file"),
955 total, (uintmax_t) s.st_size);
957 close (h);
958 g_free (quoted_name);
960 if ((fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE) || was_error)
961 ERRNOR (E_REMOTE, -1);
962 return 0;
964 error_return:
965 close (h);
966 fish_get_reply (me, SUP->sockr, NULL, 0);
967 g_free (quoted_name);
968 return -1;
971 /* --------------------------------------------------------------------------------------------- */
973 static int
974 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
976 fish_fh_data_t *fish;
977 gchar *shell_commands = NULL;
978 struct vfs_s_super *super = FH_SUPER;
979 char *name;
980 char *quoted_name;
982 if (fh->data == NULL)
983 fh->data = g_new0 (fish_fh_data_t, 1);
985 fish = (fish_fh_data_t *) fh->data;
987 name = vfs_s_fullpath (me, fh->ino);
988 if (name == NULL)
989 return 0;
990 quoted_name = strutils_shell_escape (name);
991 g_free (name);
992 fish->append = FALSE;
995 * Check whether the remote file is readable by using `dd' to copy
996 * a single byte from the remote file to /dev/null. If `dd' completes
997 * with exit status of 0 use `cat' to send the file contents to the
998 * standard output (i.e. over the network).
1001 shell_commands =
1002 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
1003 SUP->scr_get, (char *) NULL);
1004 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
1005 g_free (shell_commands);
1006 g_free (quoted_name);
1007 if (offset != PRELIM)
1008 ERRNOR (E_REMOTE, 0);
1009 fh->linear = LS_LINEAR_OPEN;
1010 fish->got = 0;
1011 errno = 0;
1012 #if SIZEOF_OFF_T == SIZEOF_LONG
1013 fish->total = (off_t) strtol (reply_str, NULL, 10);
1014 #else
1015 fish->total = (off_t) strtoll (reply_str, NULL, 10);
1016 #endif
1017 if (errno != 0)
1018 ERRNOR (E_REMOTE, 0);
1019 return 1;
1022 /* --------------------------------------------------------------------------------------------- */
1024 static void
1025 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
1027 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1028 struct vfs_s_super *super = FH_SUPER;
1029 char buffer[BUF_8K];
1030 int n;
1032 vfs_print_message (_("Aborting transfer..."));
1036 n = MIN (sizeof (buffer), (size_t) (fish->total - fish->got));
1037 if (n != 0)
1039 n = read (SUP->sockr, buffer, n);
1040 if (n < 0)
1041 return;
1042 fish->got += n;
1045 while (n != 0);
1047 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1048 vfs_print_message (_("Error reported after abort."));
1049 else
1050 vfs_print_message (_("Aborted transfer would be successful."));
1053 /* --------------------------------------------------------------------------------------------- */
1055 static int
1056 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
1058 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1059 struct vfs_s_super *super = FH_SUPER;
1060 ssize_t n = 0;
1063 len = MIN ((size_t) (fish->total - fish->got), len);
1064 tty_disable_interrupt_key ();
1065 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1067 if ((errno == EINTR) && !tty_got_interrupt ())
1068 continue;
1069 break;
1071 tty_enable_interrupt_key ();
1073 if (n > 0)
1074 fish->got += n;
1075 else if (n < 0)
1076 fish_linear_abort (me, fh);
1077 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1078 ERRNOR (E_REMOTE, -1);
1079 ERRNOR (errno, n);
1082 /* --------------------------------------------------------------------------------------------- */
1084 static void
1085 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1087 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1089 if (fish->total != fish->got)
1090 fish_linear_abort (me, fh);
1093 /* --------------------------------------------------------------------------------------------- */
1095 static int
1096 fish_ctl (void *fh, int ctlop, void *arg)
1098 (void) arg;
1099 (void) fh;
1100 (void) ctlop;
1101 return 0;
1102 #if 0
1103 switch (ctlop)
1105 case VFS_CTL_IS_NOTREADY:
1107 int v;
1109 if (!FH->linear)
1110 vfs_die ("You may not do this");
1111 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1112 return 0;
1114 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1115 if (((v < 0) && (errno == EINTR)) || v == 0)
1116 return 1;
1117 return 0;
1119 default:
1120 return 0;
1122 #endif
1125 /* --------------------------------------------------------------------------------------------- */
1127 static int
1128 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1130 int r;
1132 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1133 vfs_stamp_create (&vfs_fish_ops, super);
1134 if (r != COMPLETE)
1135 ERRNOR (E_REMOTE, -1);
1136 if (flags & OPT_FLUSH)
1137 vfs_s_invalidate (me, super);
1138 return 0;
1141 /* --------------------------------------------------------------------------------------------- */
1143 static int
1144 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1146 gchar *shell_commands = NULL;
1147 char buf[BUF_LARGE];
1148 const char *crpath1, *crpath2;
1149 char *rpath1, *rpath2;
1150 struct vfs_s_super *super, *super2;
1151 const vfs_path_element_t *path_element;
1153 path_element = vfs_path_get_by_index (vpath1, -1);
1155 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1156 if (crpath1 == NULL)
1157 return -1;
1159 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1160 if (crpath2 == NULL)
1161 return -1;
1163 rpath1 = strutils_shell_escape (crpath1);
1164 rpath2 = strutils_shell_escape (crpath2);
1165 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1166 SUP->scr_mv, (char *) NULL);
1167 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1168 g_free (shell_commands);
1169 g_free (rpath1);
1170 g_free (rpath2);
1171 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1174 /* --------------------------------------------------------------------------------------------- */
1176 static int
1177 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1179 gchar *shell_commands = NULL;
1180 char buf[BUF_LARGE];
1181 const char *crpath1, *crpath2;
1182 char *rpath1, *rpath2;
1183 struct vfs_s_super *super, *super2;
1184 const vfs_path_element_t *path_element;
1186 path_element = vfs_path_get_by_index (vpath1, -1);
1188 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1189 if (crpath1 == NULL)
1190 return -1;
1192 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1193 if (crpath2 == NULL)
1194 return -1;
1196 rpath1 = strutils_shell_escape (crpath1);
1197 rpath2 = strutils_shell_escape (crpath2);
1198 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1199 SUP->scr_hardlink, (char *) NULL);
1200 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1201 g_free (shell_commands);
1202 g_free (rpath1);
1203 g_free (rpath2);
1204 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1208 /* --------------------------------------------------------------------------------------------- */
1210 static int
1211 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1213 char *qsetto;
1214 gchar *shell_commands = NULL;
1215 char buf[BUF_LARGE];
1216 const char *crpath;
1217 char *rpath;
1218 struct vfs_s_super *super;
1219 const vfs_path_element_t *path_element;
1221 path_element = vfs_path_get_by_index (vpath2, -1);
1223 crpath = vfs_s_get_path (vpath2, &super, 0);
1224 if (crpath == NULL)
1225 return -1;
1227 rpath = strutils_shell_escape (crpath);
1228 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1230 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1231 SUP->scr_ln, (char *) NULL);
1232 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1233 g_free (shell_commands);
1234 g_free (qsetto);
1235 g_free (rpath);
1236 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1239 /* --------------------------------------------------------------------------------------------- */
1241 static int
1242 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1244 gchar *shell_commands = NULL;
1245 char buf[BUF_LARGE];
1246 const char *crpath;
1247 char *rpath;
1248 struct vfs_s_super *super;
1249 const vfs_path_element_t *path_element;
1251 path_element = vfs_path_get_by_index (vpath, -1);
1253 crpath = vfs_s_get_path (vpath, &super, 0);
1254 if (crpath == NULL)
1255 return -1;
1256 rpath = strutils_shell_escape (crpath);
1258 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1259 SUP->scr_chmod, (char *) NULL);
1260 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (int) (mode & 07777));
1261 g_free (shell_commands);
1262 g_free (rpath);
1263 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1266 /* --------------------------------------------------------------------------------------------- */
1268 static int
1269 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1271 char *sowner, *sgroup;
1272 struct passwd *pw;
1273 struct group *gr;
1275 pw = getpwuid (owner);
1276 if (pw == NULL)
1277 return 0;
1279 gr = getgrgid (group);
1280 if (gr == NULL)
1281 return 0;
1283 sowner = pw->pw_name;
1284 sgroup = gr->gr_name;
1287 gchar *shell_commands = NULL;
1288 char buf[BUF_LARGE];
1289 const char *crpath;
1290 char *rpath;
1291 struct vfs_s_super *super;
1292 const vfs_path_element_t *path_element;
1294 path_element = vfs_path_get_by_index (vpath, -1);
1296 crpath = vfs_s_get_path (vpath, &super, 0);
1297 if (crpath == NULL)
1298 return -1;
1299 rpath = strutils_shell_escape (crpath);
1301 shell_commands = g_strconcat (SUP->scr_env,
1302 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1303 SUP->scr_chown, (char *) NULL);
1304 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1305 g_free (shell_commands);
1306 fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1307 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1308 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1309 g_free (rpath);
1310 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1314 /* --------------------------------------------------------------------------------------------- */
1316 static int
1317 fish_utime (const vfs_path_t * vpath, struct utimbuf *times)
1319 gchar *shell_commands = NULL;
1320 char utcmtime[16], utcatime[16];
1321 struct tm *gmt;
1323 char buf[BUF_LARGE];
1324 const char *crpath;
1325 char *rpath;
1326 struct vfs_s_super *super;
1327 const vfs_path_element_t *path_element;
1329 path_element = vfs_path_get_by_index (vpath, -1);
1331 crpath = vfs_s_get_path (vpath, &super, 0);
1332 if (crpath == NULL)
1333 return -1;
1334 rpath = strutils_shell_escape (crpath);
1336 gmt = gmtime (&times->modtime);
1337 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1338 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1339 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1341 gmt = gmtime (&times->actime);
1342 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1343 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1344 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1346 shell_commands =
1347 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
1348 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n", SUP->scr_utime, (char *) NULL);
1349 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (long) times->actime,
1350 (long) times->modtime, utcatime, utcmtime);
1351 g_free (shell_commands);
1352 g_free (rpath);
1353 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1356 /* --------------------------------------------------------------------------------------------- */
1358 static int
1359 fish_unlink (const vfs_path_t * vpath)
1361 gchar *shell_commands = NULL;
1363 char buf[BUF_LARGE];
1364 const char *crpath;
1365 char *rpath;
1366 struct vfs_s_super *super;
1367 const vfs_path_element_t *path_element;
1369 path_element = vfs_path_get_by_index (vpath, -1);
1371 crpath = vfs_s_get_path (vpath, &super, 0);
1372 if (crpath == NULL)
1373 return -1;
1374 rpath = strutils_shell_escape (crpath);
1376 shell_commands =
1377 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_unlink, (char *) NULL);
1378 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1379 g_free (shell_commands);
1380 g_free (rpath);
1381 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1384 /* --------------------------------------------------------------------------------------------- */
1386 static int
1387 fish_exists (const vfs_path_t * vpath)
1389 gchar *shell_commands = NULL;
1391 char buf[BUF_LARGE];
1392 const char *crpath;
1393 char *rpath;
1394 struct vfs_s_super *super;
1395 const vfs_path_element_t *path_element;
1397 path_element = vfs_path_get_by_index (vpath, -1);
1399 crpath = vfs_s_get_path (vpath, &super, 0);
1400 if (crpath == NULL)
1401 return -1;
1402 rpath = strutils_shell_escape (crpath);
1404 shell_commands =
1405 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_exists, (char *) NULL);
1406 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1407 g_free (shell_commands);
1408 g_free (rpath);
1410 return (fish_send_command (path_element->class, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1413 /* --------------------------------------------------------------------------------------------- */
1415 static int
1416 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1418 gchar *shell_commands = NULL;
1419 int ret_code;
1420 char buf[BUF_LARGE];
1421 const char *crpath;
1422 char *rpath;
1423 struct vfs_s_super *super;
1424 const vfs_path_element_t *path_element;
1426 (void) mode;
1428 path_element = vfs_path_get_by_index (vpath, -1);
1430 crpath = vfs_s_get_path (vpath, &super, 0);
1431 if (crpath == NULL)
1432 return -1;
1433 rpath = strutils_shell_escape (crpath);
1435 shell_commands =
1436 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_mkdir, (char *) NULL);
1437 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1438 g_free (shell_commands);
1440 g_free (rpath);
1441 ret_code = fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1443 if (ret_code != 0)
1444 return ret_code;
1446 if (!fish_exists (vpath))
1448 path_element->class->verrno = EACCES;
1449 return -1;
1451 return 0;
1454 /* --------------------------------------------------------------------------------------------- */
1456 static int
1457 fish_rmdir (const vfs_path_t * vpath)
1459 gchar *shell_commands = NULL;
1460 char buf[BUF_LARGE];
1461 const char *crpath;
1462 char *rpath;
1463 struct vfs_s_super *super;
1464 const vfs_path_element_t *path_element;
1466 path_element = vfs_path_get_by_index (vpath, -1);
1468 crpath = vfs_s_get_path (vpath, &super, 0);
1469 if (crpath == NULL)
1470 return -1;
1471 rpath = strutils_shell_escape (crpath);
1473 shell_commands =
1474 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_rmdir, (char *) NULL);
1475 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1476 g_free (shell_commands);
1477 g_free (rpath);
1478 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1481 /* --------------------------------------------------------------------------------------------- */
1483 static void
1484 fish_fh_free_data (vfs_file_handler_t * fh)
1486 if (fh != NULL)
1488 g_free (fh->data);
1489 fh->data = NULL;
1493 /* --------------------------------------------------------------------------------------------- */
1495 static int
1496 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1498 fish_fh_data_t *fish;
1500 (void) mode;
1502 fh->data = g_new0 (fish_fh_data_t, 1);
1503 fish = (fish_fh_data_t *) fh->data;
1505 /* File will be written only, so no need to retrieve it */
1506 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1508 /* user pressed the button [ Append ] in the "Copy" dialog */
1509 if ((flags & O_APPEND) != 0)
1510 fish->append = TRUE;
1512 if (!fh->ino->localname)
1514 vfs_path_t *vpath;
1515 int tmp_handle;
1517 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1518 if (tmp_handle == -1)
1520 vfs_path_free (vpath);
1521 goto fail;
1523 fh->ino->localname = vfs_path_to_str (vpath);
1524 vfs_path_free (vpath);
1525 close (tmp_handle);
1527 return 0;
1529 if (!fh->ino->localname && vfs_s_retrieve_file (me, fh->ino) == -1)
1530 goto fail;
1531 if (!fh->ino->localname)
1532 vfs_die ("retrieve_file failed to fill in localname");
1533 return 0;
1535 fail:
1536 fish_fh_free_data (fh);
1537 return -1;
1540 /* --------------------------------------------------------------------------------------------- */
1542 static void
1543 fish_fill_names (struct vfs_class *me, fill_names_f func)
1545 GList *iter;
1547 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1549 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1551 char *name;
1552 char gbuf[10];
1553 const char *flags = "";
1555 switch (super->path_element->port)
1557 case FISH_FLAG_RSH:
1558 flags = ":r";
1559 break;
1560 case FISH_FLAG_COMPRESSED:
1561 flags = ":C";
1562 break;
1563 default:
1564 if (super->path_element->port > FISH_FLAG_RSH)
1566 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1567 flags = gbuf;
1569 break;
1571 name = vfs_path_element_build_pretty_path_str (super->path_element);
1572 func (name);
1573 g_free (name);
1577 /* --------------------------------------------------------------------------------------------- */
1579 static void *
1580 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1583 sorry, i've places hack here
1584 cause fish don't able to open files with O_EXCL flag
1586 flags &= ~O_EXCL;
1587 return vfs_s_open (vpath, flags, mode);
1590 /* --------------------------------------------------------------------------------------------- */
1591 /*** public functions ****************************************************************************/
1592 /* --------------------------------------------------------------------------------------------- */
1594 void
1595 init_fish (void)
1597 static struct vfs_s_subclass fish_subclass;
1599 tcp_init ();
1601 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1602 fish_subclass.archive_same = fish_archive_same;
1603 fish_subclass.open_archive = fish_open_archive;
1604 fish_subclass.free_archive = fish_free_archive;
1605 fish_subclass.fh_open = fish_fh_open;
1606 fish_subclass.fh_free_data = fish_fh_free_data;
1607 fish_subclass.dir_load = fish_dir_load;
1608 fish_subclass.file_store = fish_file_store;
1609 fish_subclass.linear_start = fish_linear_start;
1610 fish_subclass.linear_read = fish_linear_read;
1611 fish_subclass.linear_close = fish_linear_close;
1613 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1614 vfs_fish_ops.name = "fish";
1615 vfs_fish_ops.prefix = "sh";
1616 vfs_fish_ops.fill_names = fish_fill_names;
1617 vfs_fish_ops.chmod = fish_chmod;
1618 vfs_fish_ops.chown = fish_chown;
1619 vfs_fish_ops.utime = fish_utime;
1620 vfs_fish_ops.open = fish_open;
1621 vfs_fish_ops.symlink = fish_symlink;
1622 vfs_fish_ops.link = fish_link;
1623 vfs_fish_ops.unlink = fish_unlink;
1624 vfs_fish_ops.rename = fish_rename;
1625 vfs_fish_ops.mkdir = fish_mkdir;
1626 vfs_fish_ops.rmdir = fish_rmdir;
1627 vfs_fish_ops.ctl = fish_ctl;
1628 vfs_register_class (&vfs_fish_ops);
1631 /* --------------------------------------------------------------------------------------------- */