5c84175f4f88c29f7a53b8b0cc909c58362b3d37
[midnight-commander.git] / src / vfs / fish / fish.c
blob5c84175f4f88c29f7a53b8b0cc909c58362b3d37
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, gboolean was_garbage)
197 int code;
199 if (sscanf (s, "%d", &code) == 0)
201 code = 500;
202 return 5;
204 if (code < 100)
205 return was_garbage ? ERROR : (code == 0 ? COMPLETE : PRELIM);
206 return code / 100;
209 /* --------------------------------------------------------------------------------------------- */
210 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
212 static int
213 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
215 char answer[BUF_1K];
216 gboolean was_garbage = FALSE;
218 while (TRUE)
220 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
222 if (string_buf != NULL)
223 *string_buf = '\0';
224 return 4;
227 if (strncmp (answer, "### ", 4) == 0)
228 return fish_decode_reply (answer + 4, was_garbage ? 1 : 0);
230 was_garbage = TRUE;
231 if (string_buf != NULL)
232 g_strlcpy (string_buf, answer, string_len);
236 /* --------------------------------------------------------------------------------------------- */
238 static int
239 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *fmt, ...)
241 va_list ap;
242 char *str;
243 ssize_t status;
244 FILE *logfile = MEDATA->logfile;
246 va_start (ap, fmt);
248 str = g_strdup_vprintf (fmt, ap);
249 va_end (ap);
251 if (logfile)
253 size_t ret;
254 ret = fwrite (str, strlen (str), 1, logfile);
255 ret = fflush (logfile);
258 tty_enable_interrupt_key ();
260 status = write (SUP->sockw, str, strlen (str));
261 g_free (str);
263 tty_disable_interrupt_key ();
264 if (status < 0)
265 return TRANSIENT;
267 if (wait_reply)
268 return fish_get_reply (me, SUP->sockr,
269 (wait_reply & WANT_STRING) ? reply_str :
270 NULL, sizeof (reply_str) - 1);
271 return COMPLETE;
274 /* --------------------------------------------------------------------------------------------- */
276 static void
277 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
279 if ((SUP->sockw != -1) || (SUP->sockr != -1))
281 vfs_print_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
282 fish_command (me, super, NONE, "#BYE\nexit\n");
283 close (SUP->sockw);
284 close (SUP->sockr);
285 SUP->sockw = SUP->sockr = -1;
287 g_free (SUP->scr_ls);
288 g_free (SUP->scr_exists);
289 g_free (SUP->scr_mkdir);
290 g_free (SUP->scr_unlink);
291 g_free (SUP->scr_chown);
292 g_free (SUP->scr_chmod);
293 g_free (SUP->scr_utime);
294 g_free (SUP->scr_rmdir);
295 g_free (SUP->scr_ln);
296 g_free (SUP->scr_mv);
297 g_free (SUP->scr_hardlink);
298 g_free (SUP->scr_get);
299 g_free (SUP->scr_send);
300 g_free (SUP->scr_append);
301 g_free (SUP->scr_info);
302 g_free (SUP->scr_env);
303 g_free (SUP);
304 super->data = NULL;
307 /* --------------------------------------------------------------------------------------------- */
309 static void
310 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
312 int fileset1[2], fileset2[2];
313 int res;
315 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
316 vfs_die ("Cannot pipe(): %m.");
318 res = fork ();
320 if (res != 0)
322 if (res < 0)
323 vfs_die ("Cannot fork(): %m.");
324 /* We are the parent */
325 close (fileset1[0]);
326 SUP->sockw = fileset1[1];
327 close (fileset2[1]);
328 SUP->sockr = fileset2[0];
330 else
332 res = dup2 (fileset1[0], 0);
333 close (fileset1[0]);
334 close (fileset1[1]);
335 res = dup2 (fileset2[1], 1);
336 close (2);
337 /* stderr to /dev/null */
338 res = open ("/dev/null", O_WRONLY);
339 close (fileset2[0]);
340 close (fileset2[1]);
341 execvp (path, const_cast (char **, argv));
342 _exit (3);
346 /* --------------------------------------------------------------------------------------------- */
348 static char *
349 fish_set_env (int flags)
351 GString *tmp;
353 tmp = g_string_sized_new (250);
354 g_string_assign (tmp, "");
356 if ((flags & FISH_HAVE_HEAD) != 0)
357 g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
359 if ((flags & FISH_HAVE_SED) != 0)
360 g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
362 if ((flags & FISH_HAVE_AWK) != 0)
363 g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
365 if ((flags & FISH_HAVE_PERL) != 0)
366 g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
368 if ((flags & FISH_HAVE_LSQ) != 0)
369 g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
371 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
372 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
374 if ((flags & FISH_HAVE_TAIL) != 0)
375 g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
377 return g_string_free (tmp, FALSE);
380 /* --------------------------------------------------------------------------------------------- */
382 static gboolean
383 fish_info (struct vfs_class *me, struct vfs_s_super *super)
385 char buffer[BUF_8K];
386 if (fish_command (me, super, NONE, SUP->scr_info) == COMPLETE)
388 while (TRUE)
390 int res;
392 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
393 if ((res == 0) || (res == EINTR))
394 ERRNOR (ECONNRESET, FALSE);
395 if (strncmp (buffer, "### ", 4) == 0)
396 break;
397 SUP->host_flags = atol (buffer);
399 return TRUE;
401 ERRNOR (E_PROTO, FALSE);
405 /* --------------------------------------------------------------------------------------------- */
407 static void
408 fish_open_archive_pipeopen (struct vfs_s_super *super)
410 char gbuf[10];
411 const char *argv[10]; /* All of 10 is used now */
412 const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
413 int i = 0;
415 argv[i++] = xsh;
416 if (super->path_element->port == FISH_FLAG_COMPRESSED)
417 argv[i++] = "-C";
419 if (super->path_element->port > FISH_FLAG_RSH)
421 argv[i++] = "-p";
422 g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
423 argv[i++] = gbuf;
427 * Add the user name to the ssh command line only if it was explicitly
428 * set in vfs URL. rsh/ssh will get current user by default
429 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
430 * option breaks it for some)
433 if (super->path_element->user != NULL)
435 argv[i++] = "-l";
436 argv[i++] = super->path_element->user;
438 else
440 /* The rest of the code assumes it to be a valid username */
441 super->path_element->user = vfs_get_local_username ();
444 argv[i++] = super->path_element->host;
445 argv[i++] = "echo FISH:; /bin/sh";
446 argv[i++] = NULL;
448 fish_pipeopen (super, xsh, argv);
451 /* --------------------------------------------------------------------------------------------- */
453 static gboolean
454 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
456 char answer[2048];
458 printf ("\n%s\n", _("fish: Waiting for initial line..."));
460 if (!vfs_s_get_line (me, SUP->sockr, answer, sizeof (answer), ':'))
461 return FALSE;
463 if (strstr (answer, "assword") != NULL)
465 /* Currently, this does not work. ssh reads passwords from
466 /dev/tty, not from stdin :-(. */
468 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
470 return FALSE;
471 #if 0
472 if (super->path_element->password == NULL)
474 char *p, *op;
475 p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
476 op = vfs_get_password (p);
477 g_free (p);
478 if (op == NULL)
479 return FALSE;
480 super->path_element->password = op;
484 printf ("\n%s\n", _("fish: Sending password..."));
487 size_t str_len;
489 str_len = strlen (super->path_element->password);
490 if ((write (SUP.sockw, super->path_element->password, str_len) != (ssize_t) str_len)
491 || (write (SUP->sockw, "\n", 1) != 1))
492 return FALSE;
494 #endif
496 return TRUE;
499 /* --------------------------------------------------------------------------------------------- */
501 static int
502 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
504 gboolean ftalk;
505 /* hide panels */
506 pre_exec ();
508 /* open pipe */
509 fish_open_archive_pipeopen (super);
511 /* Start talk with ssh-server (password prompt, etc ) */
512 ftalk = fish_open_archive_talk (me, super);
514 /* show panels */
515 post_exec ();
517 if (!ftalk)
518 ERRNOR (E_PROTO, -1);
520 vfs_print_message (_("fish: Sending initial line..."));
522 * Run `start_fish_server'. If it doesn't exist - no problem,
523 * we'll talk directly to the shell.
526 if (fish_command
527 (me, super, WAIT_REPLY,
528 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
529 ERRNOR (E_PROTO, -1);
531 vfs_print_message (_("fish: Handshaking version..."));
532 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
533 ERRNOR (E_PROTO, -1);
535 /* Set up remote locale to C, otherwise dates cannot be recognized */
536 if (fish_command
537 (me, super, WAIT_REPLY,
538 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
539 ERRNOR (E_PROTO, -1);
541 vfs_print_message (_("fish: Getting host info..."));
542 if (fish_info (me, super))
543 SUP->scr_env = fish_set_env (SUP->host_flags);
545 #if 0
546 super->name =
547 g_strconcat ("sh://", super->path_element->user, "@", super->path_element->host, "/",
548 (char *) NULL);
549 #else
550 super->name = g_strdup (PATH_SEP_STR);
551 #endif
553 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
554 return 0;
557 /* --------------------------------------------------------------------------------------------- */
559 static int
560 fish_open_archive (struct vfs_s_super *super,
561 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
563 (void) vpath;
565 super->data = g_new0 (fish_super_data_t, 1);
566 super->path_element = vfs_path_element_clone (vpath_element);
568 if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
569 super->path_element->port = FISH_FLAG_RSH;
571 SUP->scr_ls =
572 fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
573 SUP->scr_exists =
574 fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
575 FISH_EXISTS_DEF_CONTENT);
576 SUP->scr_mkdir =
577 fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
578 FISH_MKDIR_DEF_CONTENT);
579 SUP->scr_unlink =
580 fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
581 FISH_UNLINK_DEF_CONTENT);
582 SUP->scr_chown =
583 fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
584 FISH_CHOWN_DEF_CONTENT);
585 SUP->scr_chmod =
586 fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
587 FISH_CHMOD_DEF_CONTENT);
588 SUP->scr_utime =
589 fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
590 FISH_UTIME_DEF_CONTENT);
591 SUP->scr_rmdir =
592 fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
593 FISH_RMDIR_DEF_CONTENT);
594 SUP->scr_ln =
595 fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
596 SUP->scr_mv =
597 fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
598 SUP->scr_hardlink =
599 fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
600 FISH_HARDLINK_DEF_CONTENT);
601 SUP->scr_get =
602 fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
603 SUP->scr_send =
604 fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
605 FISH_SEND_DEF_CONTENT);
606 SUP->scr_append =
607 fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
608 FISH_APPEND_DEF_CONTENT);
609 SUP->scr_info =
610 fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
611 FISH_INFO_DEF_CONTENT);
613 return fish_open_archive_int (vpath_element->class, super);
616 /* --------------------------------------------------------------------------------------------- */
618 static int
619 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
620 const vfs_path_t * vpath, void *cookie)
622 vfs_path_element_t *path_element;
623 int result;
625 (void) vpath;
626 (void) cookie;
628 path_element = vfs_path_element_clone (vpath_element);
630 if (path_element->user == NULL)
631 path_element->user = vfs_get_local_username ();
633 result = ((strcmp (path_element->host, super->path_element->host) == 0)
634 && (strcmp (path_element->user, super->path_element->user) == 0)
635 && (path_element->port == super->path_element->port)) ? 1 : 0;
637 vfs_path_element_free (path_element);
639 return result;
642 /* --------------------------------------------------------------------------------------------- */
644 static int
645 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
647 struct vfs_s_super *super = dir->super;
648 char buffer[8192];
649 struct vfs_s_entry *ent = NULL;
650 FILE *logfile;
651 char *quoted_path;
652 int reply_code;
653 gchar *shell_commands;
656 * Simple FISH debug interface :]
658 #if 0
659 if (!(MEDATA->logfile))
661 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
663 #endif
664 logfile = MEDATA->logfile;
666 vfs_print_message (_("fish: Reading directory %s..."), remote_path);
668 gettimeofday (&dir->timestamp, NULL);
669 dir->timestamp.tv_sec += fish_directory_timeout;
670 quoted_path = strutils_shell_escape (remote_path);
671 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_ls, (char *) NULL);
672 fish_command (me, super, NONE, shell_commands, quoted_path);
673 g_free (shell_commands);
674 g_free (quoted_path);
675 ent = vfs_s_generate_entry (me, NULL, dir, 0);
676 while (TRUE)
678 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
679 if ((!res) || (res == EINTR))
681 vfs_s_free_entry (me, ent);
682 me->verrno = ECONNRESET;
683 goto error;
685 if (logfile)
687 fputs (buffer, logfile);
688 fputs ("\n", logfile);
689 fflush (logfile);
691 if (!strncmp (buffer, "### ", 4))
692 break;
693 if ((!buffer[0]))
695 if (ent->name)
697 vfs_s_insert_entry (me, dir, ent);
698 ent = vfs_s_generate_entry (me, NULL, dir, 0);
700 continue;
703 #define ST ent->ino->st
705 switch (buffer[0])
707 case ':':
709 char *temp;
710 char *data_start = buffer + 1;
711 char *filename = data_start;
712 char *filename_bound;
714 filename_bound = filename + strlen (filename);
716 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
717 break; /* We'll do "." and ".." ourselves */
719 if (S_ISLNK (ST.st_mode))
721 char *linkname;
722 char *linkname_bound;
723 /* we expect: "escaped-name" -> "escaped-name"
724 // -> cannot occur in filenames,
725 // because it will be escaped to -\> */
728 linkname_bound = filename_bound;
730 if (*filename == '"')
731 ++filename;
733 linkname = strstr (filename, "\" -> \"");
734 if (!linkname)
736 /* broken client, or smth goes wrong */
737 linkname = filename_bound;
738 if (filename_bound > filename && *(filename_bound - 1) == '"')
739 --filename_bound; /* skip trailing " */
741 else
743 filename_bound = linkname;
744 linkname += 6; /* strlen ("\" -> \"") */
745 if (*(linkname_bound - 1) == '"')
746 --linkname_bound; /* skip trailing " */
749 ent->name = g_strndup (filename, filename_bound - filename);
750 temp = ent->name;
751 ent->name = strutils_shell_unescape (ent->name);
752 g_free (temp);
754 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
755 temp = ent->ino->linkname;
756 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
757 g_free (temp);
759 else
761 /* we expect: "escaped-name" */
762 if (filename_bound - filename > 2)
765 there is at least 2 "
766 and we skip them
768 if (*filename == '"')
769 ++filename;
770 if (*(filename_bound - 1) == '"')
771 --filename_bound;
773 ent->name = g_strndup (filename, filename_bound - filename);
774 temp = ent->name;
775 ent->name = strutils_shell_unescape (ent->name);
776 g_free (temp);
778 break;
780 case 'S':
781 ST.st_size = (off_t) g_ascii_strtoll (buffer + 1, NULL, 10);
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 code;
852 off_t total;
853 char buffer[BUF_8K];
854 struct stat s;
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 code = 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 code = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
916 (uintmax_t) s.st_size);
917 g_free (shell_commands);
919 if (code != PRELIM)
921 close (h);
922 ERRNOR (E_REMOTE, -1);
925 total = 0;
927 while (TRUE)
929 ssize_t n, t;
931 while ((n = read (h, buffer, sizeof (buffer))) < 0)
933 if ((errno == EINTR) && tty_got_interrupt ())
934 continue;
935 vfs_print_message (_("fish: Local read failed, sending zeros"));
936 close (h);
937 h = open ("/dev/zero", O_RDONLY);
940 if (n == 0)
941 break;
943 t = write (SUP->sockw, buffer, n);
944 if (t != n)
946 if (t == -1)
947 me->verrno = errno;
948 else
949 me->verrno = EIO;
950 goto error_return;
952 tty_disable_interrupt_key ();
953 total += n;
954 vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _("fish: storing file"),
955 (uintmax_t) 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)
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) g_ascii_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 ssize_t n;
1032 vfs_print_message (_("Aborting transfer..."));
1036 n = MIN ((off_t) sizeof (buffer), (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 ssize_t
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;
1062 len = MIN ((size_t) (fish->total - fish->got), len);
1063 tty_disable_interrupt_key ();
1064 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1066 if ((errno == EINTR) && !tty_got_interrupt ())
1067 continue;
1068 break;
1070 tty_enable_interrupt_key ();
1072 if (n > 0)
1073 fish->got += n;
1074 else if (n < 0)
1075 fish_linear_abort (me, fh);
1076 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1077 ERRNOR (E_REMOTE, -1);
1078 ERRNOR (errno, n);
1081 /* --------------------------------------------------------------------------------------------- */
1083 static void
1084 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1086 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1088 if (fish->total != fish->got)
1089 fish_linear_abort (me, fh);
1092 /* --------------------------------------------------------------------------------------------- */
1094 static int
1095 fish_ctl (void *fh, int ctlop, void *arg)
1097 (void) arg;
1098 (void) fh;
1099 (void) ctlop;
1100 return 0;
1101 #if 0
1102 switch (ctlop)
1104 case VFS_CTL_IS_NOTREADY:
1106 int v;
1108 if (!FH->linear)
1109 vfs_die ("You may not do this");
1110 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1111 return 0;
1113 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1114 if (((v < 0) && (errno == EINTR)) || v == 0)
1115 return 1;
1116 return 0;
1118 default:
1119 return 0;
1121 #endif
1124 /* --------------------------------------------------------------------------------------------- */
1126 static int
1127 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1129 int r;
1131 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1132 vfs_stamp_create (&vfs_fish_ops, super);
1133 if (r != COMPLETE)
1134 ERRNOR (E_REMOTE, -1);
1135 if (flags & OPT_FLUSH)
1136 vfs_s_invalidate (me, super);
1137 return 0;
1140 /* --------------------------------------------------------------------------------------------- */
1142 static int
1143 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1145 gchar *shell_commands = NULL;
1146 char buf[BUF_LARGE];
1147 const char *crpath1, *crpath2;
1148 char *rpath1, *rpath2;
1149 struct vfs_s_super *super, *super2;
1150 const vfs_path_element_t *path_element;
1152 path_element = vfs_path_get_by_index (vpath1, -1);
1154 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1155 if (crpath1 == NULL)
1156 return -1;
1158 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1159 if (crpath2 == NULL)
1160 return -1;
1162 rpath1 = strutils_shell_escape (crpath1);
1163 rpath2 = strutils_shell_escape (crpath2);
1164 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1165 SUP->scr_mv, (char *) NULL);
1166 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1167 g_free (shell_commands);
1168 g_free (rpath1);
1169 g_free (rpath2);
1170 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1173 /* --------------------------------------------------------------------------------------------- */
1175 static int
1176 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1178 gchar *shell_commands = NULL;
1179 char buf[BUF_LARGE];
1180 const char *crpath1, *crpath2;
1181 char *rpath1, *rpath2;
1182 struct vfs_s_super *super, *super2;
1183 const vfs_path_element_t *path_element;
1185 path_element = vfs_path_get_by_index (vpath1, -1);
1187 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1188 if (crpath1 == NULL)
1189 return -1;
1191 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1192 if (crpath2 == NULL)
1193 return -1;
1195 rpath1 = strutils_shell_escape (crpath1);
1196 rpath2 = strutils_shell_escape (crpath2);
1197 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1198 SUP->scr_hardlink, (char *) NULL);
1199 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1200 g_free (shell_commands);
1201 g_free (rpath1);
1202 g_free (rpath2);
1203 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1207 /* --------------------------------------------------------------------------------------------- */
1209 static int
1210 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1212 char *qsetto;
1213 gchar *shell_commands = NULL;
1214 char buf[BUF_LARGE];
1215 const char *crpath;
1216 char *rpath;
1217 struct vfs_s_super *super;
1218 const vfs_path_element_t *path_element;
1220 path_element = vfs_path_get_by_index (vpath2, -1);
1222 crpath = vfs_s_get_path (vpath2, &super, 0);
1223 if (crpath == NULL)
1224 return -1;
1226 rpath = strutils_shell_escape (crpath);
1227 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1229 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1230 SUP->scr_ln, (char *) NULL);
1231 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1232 g_free (shell_commands);
1233 g_free (qsetto);
1234 g_free (rpath);
1235 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1238 /* --------------------------------------------------------------------------------------------- */
1240 static int
1241 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1243 gchar *shell_commands = NULL;
1244 char buf[BUF_LARGE];
1245 const char *crpath;
1246 char *rpath;
1247 struct vfs_s_super *super;
1248 const vfs_path_element_t *path_element;
1250 path_element = vfs_path_get_by_index (vpath, -1);
1252 crpath = vfs_s_get_path (vpath, &super, 0);
1253 if (crpath == NULL)
1254 return -1;
1255 rpath = strutils_shell_escape (crpath);
1257 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1258 SUP->scr_chmod, (char *) NULL);
1259 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (int) (mode & 07777));
1260 g_free (shell_commands);
1261 g_free (rpath);
1262 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1265 /* --------------------------------------------------------------------------------------------- */
1267 static int
1268 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1270 char *sowner, *sgroup;
1271 struct passwd *pw;
1272 struct group *gr;
1274 pw = getpwuid (owner);
1275 if (pw == NULL)
1276 return 0;
1278 gr = getgrgid (group);
1279 if (gr == NULL)
1280 return 0;
1282 sowner = pw->pw_name;
1283 sgroup = gr->gr_name;
1286 gchar *shell_commands = NULL;
1287 char buf[BUF_LARGE];
1288 const char *crpath;
1289 char *rpath;
1290 struct vfs_s_super *super;
1291 const vfs_path_element_t *path_element;
1293 path_element = vfs_path_get_by_index (vpath, -1);
1295 crpath = vfs_s_get_path (vpath, &super, 0);
1296 if (crpath == NULL)
1297 return -1;
1298 rpath = strutils_shell_escape (crpath);
1300 shell_commands = g_strconcat (SUP->scr_env,
1301 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1302 SUP->scr_chown, (char *) NULL);
1303 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1304 g_free (shell_commands);
1305 fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1306 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1307 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1308 g_free (rpath);
1309 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1313 /* --------------------------------------------------------------------------------------------- */
1315 static int
1316 fish_utime (const vfs_path_t * vpath, struct utimbuf *times)
1318 gchar *shell_commands = NULL;
1319 char utcmtime[16], utcatime[16];
1320 struct tm *gmt;
1322 char buf[BUF_LARGE];
1323 const char *crpath;
1324 char *rpath;
1325 struct vfs_s_super *super;
1326 const vfs_path_element_t *path_element;
1328 path_element = vfs_path_get_by_index (vpath, -1);
1330 crpath = vfs_s_get_path (vpath, &super, 0);
1331 if (crpath == NULL)
1332 return -1;
1333 rpath = strutils_shell_escape (crpath);
1335 gmt = gmtime (&times->modtime);
1336 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1337 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1338 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1340 gmt = gmtime (&times->actime);
1341 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1342 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1343 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1345 shell_commands =
1346 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
1347 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n", SUP->scr_utime, (char *) NULL);
1348 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (long) times->actime,
1349 (long) times->modtime, utcatime, utcmtime);
1350 g_free (shell_commands);
1351 g_free (rpath);
1352 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1355 /* --------------------------------------------------------------------------------------------- */
1357 static int
1358 fish_unlink (const vfs_path_t * vpath)
1360 gchar *shell_commands = NULL;
1362 char buf[BUF_LARGE];
1363 const char *crpath;
1364 char *rpath;
1365 struct vfs_s_super *super;
1366 const vfs_path_element_t *path_element;
1368 path_element = vfs_path_get_by_index (vpath, -1);
1370 crpath = vfs_s_get_path (vpath, &super, 0);
1371 if (crpath == NULL)
1372 return -1;
1373 rpath = strutils_shell_escape (crpath);
1375 shell_commands =
1376 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_unlink, (char *) NULL);
1377 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1378 g_free (shell_commands);
1379 g_free (rpath);
1380 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1383 /* --------------------------------------------------------------------------------------------- */
1385 static int
1386 fish_exists (const vfs_path_t * vpath)
1388 gchar *shell_commands = NULL;
1390 char buf[BUF_LARGE];
1391 const char *crpath;
1392 char *rpath;
1393 struct vfs_s_super *super;
1394 const vfs_path_element_t *path_element;
1396 path_element = vfs_path_get_by_index (vpath, -1);
1398 crpath = vfs_s_get_path (vpath, &super, 0);
1399 if (crpath == NULL)
1400 return -1;
1401 rpath = strutils_shell_escape (crpath);
1403 shell_commands =
1404 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_exists, (char *) NULL);
1405 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1406 g_free (shell_commands);
1407 g_free (rpath);
1409 return (fish_send_command (path_element->class, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1412 /* --------------------------------------------------------------------------------------------- */
1414 static int
1415 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1417 gchar *shell_commands = NULL;
1418 int ret_code;
1419 char buf[BUF_LARGE];
1420 const char *crpath;
1421 char *rpath;
1422 struct vfs_s_super *super;
1423 const vfs_path_element_t *path_element;
1425 (void) mode;
1427 path_element = vfs_path_get_by_index (vpath, -1);
1429 crpath = vfs_s_get_path (vpath, &super, 0);
1430 if (crpath == NULL)
1431 return -1;
1432 rpath = strutils_shell_escape (crpath);
1434 shell_commands =
1435 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_mkdir, (char *) NULL);
1436 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1437 g_free (shell_commands);
1439 g_free (rpath);
1440 ret_code = fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1442 if (ret_code != 0)
1443 return ret_code;
1445 if (!fish_exists (vpath))
1447 path_element->class->verrno = EACCES;
1448 return -1;
1450 return 0;
1453 /* --------------------------------------------------------------------------------------------- */
1455 static int
1456 fish_rmdir (const vfs_path_t * vpath)
1458 gchar *shell_commands = NULL;
1459 char buf[BUF_LARGE];
1460 const char *crpath;
1461 char *rpath;
1462 struct vfs_s_super *super;
1463 const vfs_path_element_t *path_element;
1465 path_element = vfs_path_get_by_index (vpath, -1);
1467 crpath = vfs_s_get_path (vpath, &super, 0);
1468 if (crpath == NULL)
1469 return -1;
1470 rpath = strutils_shell_escape (crpath);
1472 shell_commands =
1473 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_rmdir, (char *) NULL);
1474 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1475 g_free (shell_commands);
1476 g_free (rpath);
1477 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1480 /* --------------------------------------------------------------------------------------------- */
1482 static void
1483 fish_fh_free_data (vfs_file_handler_t * fh)
1485 if (fh != NULL)
1487 g_free (fh->data);
1488 fh->data = NULL;
1492 /* --------------------------------------------------------------------------------------------- */
1494 static int
1495 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1497 fish_fh_data_t *fish;
1499 (void) mode;
1501 fh->data = g_new0 (fish_fh_data_t, 1);
1502 fish = (fish_fh_data_t *) fh->data;
1504 /* File will be written only, so no need to retrieve it */
1505 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1507 /* user pressed the button [ Append ] in the "Copy" dialog */
1508 if ((flags & O_APPEND) != 0)
1509 fish->append = TRUE;
1511 if (!fh->ino->localname)
1513 vfs_path_t *vpath;
1514 int tmp_handle;
1516 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1517 if (tmp_handle == -1)
1519 vfs_path_free (vpath);
1520 goto fail;
1522 fh->ino->localname = vfs_path_to_str (vpath);
1523 vfs_path_free (vpath);
1524 close (tmp_handle);
1526 return 0;
1528 if (!fh->ino->localname && vfs_s_retrieve_file (me, fh->ino) == -1)
1529 goto fail;
1530 if (!fh->ino->localname)
1531 vfs_die ("retrieve_file failed to fill in localname");
1532 return 0;
1534 fail:
1535 fish_fh_free_data (fh);
1536 return -1;
1539 /* --------------------------------------------------------------------------------------------- */
1541 static void
1542 fish_fill_names (struct vfs_class *me, fill_names_f func)
1544 GList *iter;
1546 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1548 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1550 char *name;
1551 char gbuf[10];
1552 const char *flags = "";
1554 switch (super->path_element->port)
1556 case FISH_FLAG_RSH:
1557 flags = ":r";
1558 break;
1559 case FISH_FLAG_COMPRESSED:
1560 flags = ":C";
1561 break;
1562 default:
1563 if (super->path_element->port > FISH_FLAG_RSH)
1565 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1566 flags = gbuf;
1568 break;
1571 name =
1572 g_strconcat (vfs_fish_ops.prefix, VFS_PATH_URL_DELIMITER,
1573 super->path_element->user, "@", super->path_element->host, flags, "/",
1574 super->path_element->path, (char *) NULL);
1575 func (name);
1576 g_free (name);
1580 /* --------------------------------------------------------------------------------------------- */
1582 static void *
1583 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1586 sorry, i've places hack here
1587 cause fish don't able to open files with O_EXCL flag
1589 flags &= ~O_EXCL;
1590 return vfs_s_open (vpath, flags, mode);
1593 /* --------------------------------------------------------------------------------------------- */
1594 /*** public functions ****************************************************************************/
1595 /* --------------------------------------------------------------------------------------------- */
1597 void
1598 init_fish (void)
1600 static struct vfs_s_subclass fish_subclass;
1602 tcp_init ();
1604 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1605 fish_subclass.archive_same = fish_archive_same;
1606 fish_subclass.open_archive = fish_open_archive;
1607 fish_subclass.free_archive = fish_free_archive;
1608 fish_subclass.fh_open = fish_fh_open;
1609 fish_subclass.fh_free_data = fish_fh_free_data;
1610 fish_subclass.dir_load = fish_dir_load;
1611 fish_subclass.file_store = fish_file_store;
1612 fish_subclass.linear_start = fish_linear_start;
1613 fish_subclass.linear_read = fish_linear_read;
1614 fish_subclass.linear_close = fish_linear_close;
1616 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1617 vfs_fish_ops.name = "fish";
1618 vfs_fish_ops.prefix = "sh";
1619 vfs_fish_ops.fill_names = fish_fill_names;
1620 vfs_fish_ops.chmod = fish_chmod;
1621 vfs_fish_ops.chown = fish_chown;
1622 vfs_fish_ops.utime = fish_utime;
1623 vfs_fish_ops.open = fish_open;
1624 vfs_fish_ops.symlink = fish_symlink;
1625 vfs_fish_ops.link = fish_link;
1626 vfs_fish_ops.unlink = fish_unlink;
1627 vfs_fish_ops.rename = fish_rename;
1628 vfs_fish_ops.mkdir = fish_mkdir;
1629 vfs_fish_ops.rmdir = fish_rmdir;
1630 vfs_fish_ops.ctl = fish_ctl;
1631 vfs_register_class (&vfs_fish_ops);
1634 /* --------------------------------------------------------------------------------------------- */