Ticket #2206: Add jump support to target line in some external editors
[midnight-commander.git] / src / vfs / fish / fish.c
blob6cac04d7b5e37de2ce0b1a1657bb6df043c43dcb
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/util.h" /* my_exit() */
70 #include "lib/mcconfig.h"
72 #include "src/execute.h" /* pre_exec, post_exec */
74 #include "lib/vfs/vfs.h"
75 #include "lib/vfs/utilvfs.h"
76 #include "lib/vfs/netutil.h"
77 #include "lib/vfs/xdirentry.h"
78 #include "lib/vfs/gc.h" /* vfs_stamp_create */
80 #include "fish.h"
81 #include "fishdef.h"
83 /*** global variables ****************************************************************************/
85 int fish_directory_timeout = 900;
87 /*** file scope macro definitions ****************************************************************/
89 #define DO_RESOLVE_SYMLINK 1
90 #define DO_OPEN 2
91 #define DO_FREE_RESOURCE 4
93 #define FISH_FLAG_COMPRESSED 1
94 #define FISH_FLAG_RSH 2
96 #define OPT_FLUSH 1
97 #define OPT_IGNORE_ERROR 2
100 * Reply codes.
102 #define PRELIM 1 /* positive preliminary */
103 #define COMPLETE 2 /* positive completion */
104 #define CONTINUE 3 /* positive intermediate */
105 #define TRANSIENT 4 /* transient negative completion */
106 #define ERROR 5 /* permanent negative completion */
108 /* command wait_flag: */
109 #define NONE 0x00
110 #define WAIT_REPLY 0x01
111 #define WANT_STRING 0x02
113 /* environment flags */
114 #define FISH_HAVE_HEAD 1
115 #define FISH_HAVE_SED 2
116 #define FISH_HAVE_AWK 4
117 #define FISH_HAVE_PERL 8
118 #define FISH_HAVE_LSQ 16
119 #define FISH_HAVE_DATE_MDYT 32
120 #define FISH_HAVE_TAIL 64
122 #define SUP ((fish_super_data_t *) super->data)
124 /*** file scope type declarations ****************************************************************/
126 typedef struct
128 int sockr;
129 int sockw;
130 char *scr_ls;
131 char *scr_chmod;
132 char *scr_utime;
133 char *scr_exists;
134 char *scr_mkdir;
135 char *scr_unlink;
136 char *scr_chown;
137 char *scr_rmdir;
138 char *scr_ln;
139 char *scr_mv;
140 char *scr_hardlink;
141 char *scr_get;
142 char *scr_send;
143 char *scr_append;
144 char *scr_info;
145 int host_flags;
146 char *scr_env;
147 } fish_super_data_t;
149 typedef struct
151 off_t got;
152 off_t total;
153 gboolean append;
154 } fish_fh_data_t;
156 /*** file scope variables ************************************************************************/
158 static char reply_str[80];
160 static struct vfs_class vfs_fish_ops;
162 /*** file scope functions ************************************************************************/
163 /* --------------------------------------------------------------------------------------------- */
165 static char *
166 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
168 char *scr_filename = NULL;
169 char *scr_content;
170 gsize scr_len = 0;
172 /* 1st: scan user directory */
173 scr_filename = g_build_path (PATH_SEP_STR, mc_config_get_data_path (), FISH_PREFIX, hostname,
174 script_name, (char *) NULL);
175 /* silent about user dir */
176 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
177 g_free (scr_filename);
178 /* 2nd: scan system dir */
179 if (scr_content == NULL)
181 scr_filename =
182 g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
183 g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
184 g_free (scr_filename);
187 if (scr_content != NULL)
188 return scr_content;
190 return g_strdup (def_content);
193 /* --------------------------------------------------------------------------------------------- */
195 static int
196 fish_decode_reply (char *s, gboolean was_garbage)
198 int code;
200 if (sscanf (s, "%d", &code) == 0)
202 code = 500;
203 return 5;
205 if (code < 100)
206 return was_garbage ? ERROR : (code == 0 ? COMPLETE : PRELIM);
207 return code / 100;
210 /* --------------------------------------------------------------------------------------------- */
211 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
213 static int
214 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
216 char answer[BUF_1K];
217 gboolean was_garbage = FALSE;
219 while (TRUE)
221 if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
223 if (string_buf != NULL)
224 *string_buf = '\0';
225 return 4;
228 if (strncmp (answer, "### ", 4) == 0)
229 return fish_decode_reply (answer + 4, was_garbage ? 1 : 0);
231 was_garbage = TRUE;
232 if (string_buf != NULL)
233 g_strlcpy (string_buf, answer, string_len);
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);
257 (void) ret;
260 tty_enable_interrupt_key ();
262 status = write (SUP->sockw, str, strlen (str));
263 g_free (str);
265 tty_disable_interrupt_key ();
266 if (status < 0)
267 return TRANSIENT;
269 if (wait_reply)
270 return fish_get_reply (me, SUP->sockr,
271 (wait_reply & WANT_STRING) ? reply_str :
272 NULL, sizeof (reply_str) - 1);
273 return COMPLETE;
276 /* --------------------------------------------------------------------------------------------- */
278 static void
279 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
281 if ((SUP->sockw != -1) || (SUP->sockr != -1))
283 vfs_print_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
284 fish_command (me, super, NONE, "#BYE\nexit\n");
285 close (SUP->sockw);
286 close (SUP->sockr);
287 SUP->sockw = SUP->sockr = -1;
289 g_free (SUP->scr_ls);
290 g_free (SUP->scr_exists);
291 g_free (SUP->scr_mkdir);
292 g_free (SUP->scr_unlink);
293 g_free (SUP->scr_chown);
294 g_free (SUP->scr_chmod);
295 g_free (SUP->scr_utime);
296 g_free (SUP->scr_rmdir);
297 g_free (SUP->scr_ln);
298 g_free (SUP->scr_mv);
299 g_free (SUP->scr_hardlink);
300 g_free (SUP->scr_get);
301 g_free (SUP->scr_send);
302 g_free (SUP->scr_append);
303 g_free (SUP->scr_info);
304 g_free (SUP->scr_env);
305 g_free (SUP);
306 super->data = NULL;
309 /* --------------------------------------------------------------------------------------------- */
311 static void
312 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
314 int fileset1[2], fileset2[2];
315 int res;
317 if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
318 vfs_die ("Cannot pipe(): %m.");
320 res = fork ();
322 if (res != 0)
324 if (res < 0)
325 vfs_die ("Cannot fork(): %m.");
326 /* We are the parent */
327 close (fileset1[0]);
328 SUP->sockw = fileset1[1];
329 close (fileset2[1]);
330 SUP->sockr = fileset2[0];
332 else
334 res = dup2 (fileset1[0], 0);
335 close (fileset1[0]);
336 close (fileset1[1]);
337 res = dup2 (fileset2[1], 1);
338 close (2);
339 /* stderr to /dev/null */
340 res = open ("/dev/null", O_WRONLY);
341 close (fileset2[0]);
342 close (fileset2[1]);
343 execvp (path, const_cast (char **, argv));
344 my_exit (3);
348 /* --------------------------------------------------------------------------------------------- */
350 static char *
351 fish_set_env (int flags)
353 GString *tmp;
355 tmp = g_string_sized_new (250);
356 g_string_assign (tmp, "");
358 if ((flags & FISH_HAVE_HEAD) != 0)
359 g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
361 if ((flags & FISH_HAVE_SED) != 0)
362 g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
364 if ((flags & FISH_HAVE_AWK) != 0)
365 g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
367 if ((flags & FISH_HAVE_PERL) != 0)
368 g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
370 if ((flags & FISH_HAVE_LSQ) != 0)
371 g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
373 if ((flags & FISH_HAVE_DATE_MDYT) != 0)
374 g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
376 if ((flags & FISH_HAVE_TAIL) != 0)
377 g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
379 return g_string_free (tmp, FALSE);
382 /* --------------------------------------------------------------------------------------------- */
384 static gboolean
385 fish_info (struct vfs_class *me, struct vfs_s_super *super)
387 char buffer[BUF_8K];
388 if (fish_command (me, super, NONE, SUP->scr_info) == COMPLETE)
390 while (TRUE)
392 int res;
394 res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
395 if ((res == 0) || (res == EINTR))
396 ERRNOR (ECONNRESET, FALSE);
397 if (strncmp (buffer, "### ", 4) == 0)
398 break;
399 SUP->host_flags = atol (buffer);
401 return TRUE;
403 ERRNOR (E_PROTO, FALSE);
407 /* --------------------------------------------------------------------------------------------- */
409 static void
410 fish_open_archive_pipeopen (struct vfs_s_super *super)
412 char gbuf[10];
413 const char *argv[10]; /* All of 10 is used now */
414 const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
415 int i = 0;
417 argv[i++] = xsh;
418 if (super->path_element->port == FISH_FLAG_COMPRESSED)
419 argv[i++] = "-C";
421 if (super->path_element->port > FISH_FLAG_RSH)
423 argv[i++] = "-p";
424 g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
425 argv[i++] = gbuf;
429 * Add the user name to the ssh command line only if it was explicitly
430 * set in vfs URL. rsh/ssh will get current user by default
431 * plus we can set convenient overrides in ~/.ssh/config (explicit -l
432 * option breaks it for some)
435 if (super->path_element->user != NULL)
437 argv[i++] = "-l";
438 argv[i++] = super->path_element->user;
440 else
442 /* The rest of the code assumes it to be a valid username */
443 super->path_element->user = vfs_get_local_username ();
446 argv[i++] = super->path_element->host;
447 argv[i++] = "echo FISH:; /bin/sh";
448 argv[i++] = NULL;
450 fish_pipeopen (super, xsh, argv);
453 /* --------------------------------------------------------------------------------------------- */
455 static gboolean
456 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
458 char answer[2048];
460 printf ("\n%s\n", _("fish: Waiting for initial line..."));
462 if (!vfs_s_get_line (me, SUP->sockr, answer, sizeof (answer), ':'))
463 return FALSE;
465 if (strstr (answer, "assword") != NULL)
467 /* Currently, this does not work. ssh reads passwords from
468 /dev/tty, not from stdin :-(. */
470 printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
472 return FALSE;
473 #if 0
474 if (super->path_element->password == NULL)
476 char *p, *op;
477 p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
478 op = vfs_get_password (p);
479 g_free (p);
480 if (op == NULL)
481 return FALSE;
482 super->path_element->password = op;
486 printf ("\n%s\n", _("fish: Sending password..."));
489 size_t str_len;
491 str_len = strlen (super->path_element->password);
492 if ((write (SUP.sockw, super->path_element->password, str_len) != (ssize_t) str_len)
493 || (write (SUP->sockw, "\n", 1) != 1))
494 return FALSE;
496 #endif
498 return TRUE;
501 /* --------------------------------------------------------------------------------------------- */
503 static int
504 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
506 gboolean ftalk;
507 /* hide panels */
508 pre_exec ();
510 /* open pipe */
511 fish_open_archive_pipeopen (super);
513 /* Start talk with ssh-server (password prompt, etc ) */
514 ftalk = fish_open_archive_talk (me, super);
516 /* show panels */
517 post_exec ();
519 if (!ftalk)
520 ERRNOR (E_PROTO, -1);
522 vfs_print_message (_("fish: Sending initial line..."));
524 * Run `start_fish_server'. If it doesn't exist - no problem,
525 * we'll talk directly to the shell.
528 if (fish_command
529 (me, super, WAIT_REPLY,
530 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") != COMPLETE)
531 ERRNOR (E_PROTO, -1);
533 vfs_print_message (_("fish: Handshaking version..."));
534 if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n") != COMPLETE)
535 ERRNOR (E_PROTO, -1);
537 /* Set up remote locale to C, otherwise dates cannot be recognized */
538 if (fish_command
539 (me, super, WAIT_REPLY,
540 "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n") != COMPLETE)
541 ERRNOR (E_PROTO, -1);
543 vfs_print_message (_("fish: Getting host info..."));
544 if (fish_info (me, super))
545 SUP->scr_env = fish_set_env (SUP->host_flags);
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 *filename_bound;
716 filename_bound = filename + strlen (filename);
718 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
719 break; /* We'll do "." and ".." ourselves */
721 if (S_ISLNK (ST.st_mode))
723 char *linkname;
724 char *linkname_bound;
725 /* we expect: "escaped-name" -> "escaped-name"
726 // -> cannot occur in filenames,
727 // because it will be escaped to -\> */
730 linkname_bound = filename_bound;
732 if (*filename == '"')
733 ++filename;
735 linkname = strstr (filename, "\" -> \"");
736 if (!linkname)
738 /* broken client, or smth goes wrong */
739 linkname = filename_bound;
740 if (filename_bound > filename && *(filename_bound - 1) == '"')
741 --filename_bound; /* skip trailing " */
743 else
745 filename_bound = linkname;
746 linkname += 6; /* strlen ("\" -> \"") */
747 if (*(linkname_bound - 1) == '"')
748 --linkname_bound; /* skip trailing " */
751 ent->name = g_strndup (filename, filename_bound - filename);
752 temp = ent->name;
753 ent->name = strutils_shell_unescape (ent->name);
754 g_free (temp);
756 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
757 temp = ent->ino->linkname;
758 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
759 g_free (temp);
761 else
763 /* we expect: "escaped-name" */
764 if (filename_bound - filename > 2)
767 there is at least 2 "
768 and we skip them
770 if (*filename == '"')
771 ++filename;
772 if (*(filename_bound - 1) == '"')
773 --filename_bound;
775 ent->name = g_strndup (filename, filename_bound - filename);
776 temp = ent->name;
777 ent->name = strutils_shell_unescape (ent->name);
778 g_free (temp);
780 break;
782 case 'S':
783 ST.st_size = (off_t) g_ascii_strtoll (buffer + 1, NULL, 10);
784 break;
785 case 'P':
787 size_t skipped;
788 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
789 break;
791 case 'R':
794 raw filemode:
795 we expect: Roctal-filemode octal-filetype uid.gid
797 size_t skipped;
798 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
799 break;
801 case 'd':
803 vfs_split_text (buffer + 1);
804 if (!vfs_parse_filedate (0, &ST.st_ctime))
805 break;
806 ST.st_atime = ST.st_mtime = ST.st_ctime;
808 break;
809 case 'D':
811 struct tm tim;
812 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
813 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
814 break;
815 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
817 break;
818 case 'E':
820 int maj, min;
821 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
822 break;
823 #ifdef HAVE_STRUCT_STAT_ST_RDEV
824 ST.st_rdev = makedev (maj, min);
825 #endif
830 vfs_s_free_entry (me, ent);
831 reply_code = fish_decode_reply (buffer + 4, 0);
832 if (reply_code == COMPLETE)
834 vfs_print_message (_("%s: done."), me->name);
835 return 0;
838 me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
840 error:
841 vfs_print_message (_("%s: failure"), me->name);
842 return -1;
845 /* --------------------------------------------------------------------------------------------- */
847 static int
848 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
850 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
851 gchar *shell_commands = NULL;
852 struct vfs_s_super *super = FH_SUPER;
853 int code;
854 off_t total;
855 char buffer[BUF_8K];
856 struct stat s;
857 int h;
858 char *quoted_name;
860 h = open (localname, O_RDONLY);
861 if (h == -1)
862 ERRNOR (EIO, -1);
863 if (fstat (h, &s) < 0)
865 close (h);
866 ERRNOR (EIO, -1);
869 /* First, try this as stor:
871 * ( head -c number ) | ( cat > file; cat >/dev/null )
873 * If `head' is not present on the remote system, `dd' will be used.
874 * Unfortunately, we cannot trust most non-GNU `head' implementations
875 * even if `-c' options is supported. Therefore, we separate GNU head
876 * (and other modern heads?) using `-q' and `-' . This causes another
877 * implementations to fail (because of "incorrect options").
879 * Fallback is:
881 * rest=<number>
882 * while [ $rest -gt 0 ]
883 * do
884 * cnt=`expr \( $rest + 255 \) / 256`
885 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
886 * rest=`expr $rest - $n`
887 * done
889 * `dd' was not designed for full filling of input buffers,
890 * and does not report exact number of bytes (not blocks).
891 * Therefore a more complex shell script is needed.
893 * On some systems non-GNU head writes "Usage:" error report to stdout
894 * instead of stderr. It makes impossible the use of "head || dd"
895 * algorithm for file appending case, therefore just "dd" is used for it.
898 quoted_name = strutils_shell_escape (name);
899 vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
901 /* FIXME: File size is limited to ULONG_MAX */
902 if (fish->append)
904 shell_commands =
905 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
906 SUP->scr_append, (char *) NULL);
908 code = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
909 (uintmax_t) s.st_size);
910 g_free (shell_commands);
912 else
914 shell_commands =
915 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
916 SUP->scr_send, (char *) NULL);
917 code = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
918 (uintmax_t) s.st_size);
919 g_free (shell_commands);
921 if (code != PRELIM)
923 close (h);
924 ERRNOR (E_REMOTE, -1);
927 total = 0;
929 while (TRUE)
931 ssize_t n, t;
933 while ((n = read (h, buffer, sizeof (buffer))) < 0)
935 if ((errno == EINTR) && tty_got_interrupt ())
936 continue;
937 vfs_print_message (_("fish: Local read failed, sending zeros"));
938 close (h);
939 h = open ("/dev/zero", O_RDONLY);
942 if (n == 0)
943 break;
945 t = write (SUP->sockw, buffer, n);
946 if (t != n)
948 if (t == -1)
949 me->verrno = errno;
950 else
951 me->verrno = EIO;
952 goto error_return;
954 tty_disable_interrupt_key ();
955 total += n;
956 vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _("fish: storing file"),
957 (uintmax_t) total, (uintmax_t) s.st_size);
959 close (h);
960 g_free (quoted_name);
962 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
963 ERRNOR (E_REMOTE, -1);
964 return 0;
966 error_return:
967 close (h);
968 fish_get_reply (me, SUP->sockr, NULL, 0);
969 g_free (quoted_name);
970 return -1;
973 /* --------------------------------------------------------------------------------------------- */
975 static int
976 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
978 fish_fh_data_t *fish;
979 gchar *shell_commands = NULL;
980 struct vfs_s_super *super = FH_SUPER;
981 char *name;
982 char *quoted_name;
984 if (fh->data == NULL)
985 fh->data = g_new0 (fish_fh_data_t, 1);
987 fish = (fish_fh_data_t *) fh->data;
989 name = vfs_s_fullpath (me, fh->ino);
990 if (name == NULL)
991 return 0;
992 quoted_name = strutils_shell_escape (name);
993 g_free (name);
994 fish->append = FALSE;
997 * Check whether the remote file is readable by using `dd' to copy
998 * a single byte from the remote file to /dev/null. If `dd' completes
999 * with exit status of 0 use `cat' to send the file contents to the
1000 * standard output (i.e. over the network).
1003 shell_commands =
1004 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
1005 SUP->scr_get, (char *) NULL);
1006 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
1007 g_free (shell_commands);
1008 g_free (quoted_name);
1009 if (offset != PRELIM)
1010 ERRNOR (E_REMOTE, 0);
1011 fh->linear = LS_LINEAR_OPEN;
1012 fish->got = 0;
1013 errno = 0;
1014 #if SIZEOF_OFF_T == SIZEOF_LONG
1015 fish->total = (off_t) strtol (reply_str, NULL, 10);
1016 #else
1017 fish->total = (off_t) g_ascii_strtoll (reply_str, NULL, 10);
1018 #endif
1019 if (errno != 0)
1020 ERRNOR (E_REMOTE, 0);
1021 return 1;
1024 /* --------------------------------------------------------------------------------------------- */
1026 static void
1027 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
1029 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1030 struct vfs_s_super *super = FH_SUPER;
1031 char buffer[BUF_8K];
1032 ssize_t n;
1034 vfs_print_message (_("Aborting transfer..."));
1038 n = MIN ((off_t) sizeof (buffer), (fish->total - fish->got));
1039 if (n != 0)
1041 n = read (SUP->sockr, buffer, n);
1042 if (n < 0)
1043 return;
1044 fish->got += n;
1047 while (n != 0);
1049 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1050 vfs_print_message (_("Error reported after abort."));
1051 else
1052 vfs_print_message (_("Aborted transfer would be successful."));
1055 /* --------------------------------------------------------------------------------------------- */
1057 static ssize_t
1058 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
1060 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1061 struct vfs_s_super *super = FH_SUPER;
1062 ssize_t n = 0;
1064 len = MIN ((size_t) (fish->total - fish->got), len);
1065 tty_disable_interrupt_key ();
1066 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1068 if ((errno == EINTR) && !tty_got_interrupt ())
1069 continue;
1070 break;
1072 tty_enable_interrupt_key ();
1074 if (n > 0)
1075 fish->got += n;
1076 else if (n < 0)
1077 fish_linear_abort (me, fh);
1078 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1079 ERRNOR (E_REMOTE, -1);
1080 ERRNOR (errno, n);
1083 /* --------------------------------------------------------------------------------------------- */
1085 static void
1086 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1088 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1090 if (fish->total != fish->got)
1091 fish_linear_abort (me, fh);
1094 /* --------------------------------------------------------------------------------------------- */
1096 static int
1097 fish_ctl (void *fh, int ctlop, void *arg)
1099 (void) arg;
1100 (void) fh;
1101 (void) ctlop;
1102 return 0;
1103 #if 0
1104 switch (ctlop)
1106 case VFS_CTL_IS_NOTREADY:
1108 int v;
1110 if (!FH->linear)
1111 vfs_die ("You may not do this");
1112 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1113 return 0;
1115 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1116 if (((v < 0) && (errno == EINTR)) || v == 0)
1117 return 1;
1118 return 0;
1120 default:
1121 return 0;
1123 #endif
1126 /* --------------------------------------------------------------------------------------------- */
1128 static int
1129 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1131 int r;
1133 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1134 vfs_stamp_create (&vfs_fish_ops, super);
1135 if (r != COMPLETE)
1136 ERRNOR (E_REMOTE, -1);
1137 if (flags & OPT_FLUSH)
1138 vfs_s_invalidate (me, super);
1139 return 0;
1142 /* --------------------------------------------------------------------------------------------- */
1144 static int
1145 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1147 gchar *shell_commands = NULL;
1148 char buf[BUF_LARGE];
1149 const char *crpath1, *crpath2;
1150 char *rpath1, *rpath2;
1151 struct vfs_s_super *super, *super2;
1152 const vfs_path_element_t *path_element;
1154 path_element = vfs_path_get_by_index (vpath1, -1);
1156 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1157 if (crpath1 == NULL)
1158 return -1;
1160 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1161 if (crpath2 == NULL)
1162 return -1;
1164 rpath1 = strutils_shell_escape (crpath1);
1165 rpath2 = strutils_shell_escape (crpath2);
1166 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1167 SUP->scr_mv, (char *) NULL);
1168 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1169 g_free (shell_commands);
1170 g_free (rpath1);
1171 g_free (rpath2);
1172 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1175 /* --------------------------------------------------------------------------------------------- */
1177 static int
1178 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1180 gchar *shell_commands = NULL;
1181 char buf[BUF_LARGE];
1182 const char *crpath1, *crpath2;
1183 char *rpath1, *rpath2;
1184 struct vfs_s_super *super, *super2;
1185 const vfs_path_element_t *path_element;
1187 path_element = vfs_path_get_by_index (vpath1, -1);
1189 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1190 if (crpath1 == NULL)
1191 return -1;
1193 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1194 if (crpath2 == NULL)
1195 return -1;
1197 rpath1 = strutils_shell_escape (crpath1);
1198 rpath2 = strutils_shell_escape (crpath2);
1199 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1200 SUP->scr_hardlink, (char *) NULL);
1201 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1202 g_free (shell_commands);
1203 g_free (rpath1);
1204 g_free (rpath2);
1205 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1209 /* --------------------------------------------------------------------------------------------- */
1211 static int
1212 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1214 char *qsetto;
1215 gchar *shell_commands = NULL;
1216 char buf[BUF_LARGE];
1217 const char *crpath;
1218 char *rpath;
1219 struct vfs_s_super *super;
1220 const vfs_path_element_t *path_element;
1222 path_element = vfs_path_get_by_index (vpath2, -1);
1224 crpath = vfs_s_get_path (vpath2, &super, 0);
1225 if (crpath == NULL)
1226 return -1;
1228 rpath = strutils_shell_escape (crpath);
1229 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1231 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1232 SUP->scr_ln, (char *) NULL);
1233 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1234 g_free (shell_commands);
1235 g_free (qsetto);
1236 g_free (rpath);
1237 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1240 /* --------------------------------------------------------------------------------------------- */
1242 static int
1243 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1245 gchar *shell_commands = NULL;
1246 char buf[BUF_LARGE];
1247 const char *crpath;
1248 char *rpath;
1249 struct vfs_s_super *super;
1250 const vfs_path_element_t *path_element;
1252 path_element = vfs_path_get_by_index (vpath, -1);
1254 crpath = vfs_s_get_path (vpath, &super, 0);
1255 if (crpath == NULL)
1256 return -1;
1257 rpath = strutils_shell_escape (crpath);
1259 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1260 SUP->scr_chmod, (char *) NULL);
1261 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (int) (mode & 07777));
1262 g_free (shell_commands);
1263 g_free (rpath);
1264 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1267 /* --------------------------------------------------------------------------------------------- */
1269 static int
1270 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1272 char *sowner, *sgroup;
1273 struct passwd *pw;
1274 struct group *gr;
1276 pw = getpwuid (owner);
1277 if (pw == NULL)
1278 return 0;
1280 gr = getgrgid (group);
1281 if (gr == NULL)
1282 return 0;
1284 sowner = pw->pw_name;
1285 sgroup = gr->gr_name;
1288 gchar *shell_commands = NULL;
1289 char buf[BUF_LARGE];
1290 const char *crpath;
1291 char *rpath;
1292 struct vfs_s_super *super;
1293 const vfs_path_element_t *path_element;
1295 path_element = vfs_path_get_by_index (vpath, -1);
1297 crpath = vfs_s_get_path (vpath, &super, 0);
1298 if (crpath == NULL)
1299 return -1;
1300 rpath = strutils_shell_escape (crpath);
1302 shell_commands = g_strconcat (SUP->scr_env,
1303 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1304 SUP->scr_chown, (char *) NULL);
1305 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1306 g_free (shell_commands);
1307 fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1308 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1309 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1310 g_free (rpath);
1311 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1315 /* --------------------------------------------------------------------------------------------- */
1317 static int
1318 fish_utime (const vfs_path_t * vpath, struct utimbuf *times)
1320 gchar *shell_commands = NULL;
1321 char utcmtime[16], utcatime[16];
1322 struct tm *gmt;
1324 char buf[BUF_LARGE];
1325 const char *crpath;
1326 char *rpath;
1327 struct vfs_s_super *super;
1328 const vfs_path_element_t *path_element;
1330 path_element = vfs_path_get_by_index (vpath, -1);
1332 crpath = vfs_s_get_path (vpath, &super, 0);
1333 if (crpath == NULL)
1334 return -1;
1335 rpath = strutils_shell_escape (crpath);
1337 gmt = gmtime (&times->modtime);
1338 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1339 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1340 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1342 gmt = gmtime (&times->actime);
1343 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1344 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1345 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1347 shell_commands =
1348 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
1349 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n", SUP->scr_utime, (char *) NULL);
1350 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (long) times->actime,
1351 (long) times->modtime, utcatime, utcmtime);
1352 g_free (shell_commands);
1353 g_free (rpath);
1354 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1357 /* --------------------------------------------------------------------------------------------- */
1359 static int
1360 fish_unlink (const vfs_path_t * vpath)
1362 gchar *shell_commands = NULL;
1364 char buf[BUF_LARGE];
1365 const char *crpath;
1366 char *rpath;
1367 struct vfs_s_super *super;
1368 const vfs_path_element_t *path_element;
1370 path_element = vfs_path_get_by_index (vpath, -1);
1372 crpath = vfs_s_get_path (vpath, &super, 0);
1373 if (crpath == NULL)
1374 return -1;
1375 rpath = strutils_shell_escape (crpath);
1377 shell_commands =
1378 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_unlink, (char *) NULL);
1379 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1380 g_free (shell_commands);
1381 g_free (rpath);
1382 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1385 /* --------------------------------------------------------------------------------------------- */
1387 static int
1388 fish_exists (const vfs_path_t * vpath)
1390 gchar *shell_commands = NULL;
1392 char buf[BUF_LARGE];
1393 const char *crpath;
1394 char *rpath;
1395 struct vfs_s_super *super;
1396 const vfs_path_element_t *path_element;
1398 path_element = vfs_path_get_by_index (vpath, -1);
1400 crpath = vfs_s_get_path (vpath, &super, 0);
1401 if (crpath == NULL)
1402 return -1;
1403 rpath = strutils_shell_escape (crpath);
1405 shell_commands =
1406 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_exists, (char *) NULL);
1407 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1408 g_free (shell_commands);
1409 g_free (rpath);
1411 return (fish_send_command (path_element->class, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1414 /* --------------------------------------------------------------------------------------------- */
1416 static int
1417 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1419 gchar *shell_commands = NULL;
1420 int ret_code;
1421 char buf[BUF_LARGE];
1422 const char *crpath;
1423 char *rpath;
1424 struct vfs_s_super *super;
1425 const vfs_path_element_t *path_element;
1427 (void) mode;
1429 path_element = vfs_path_get_by_index (vpath, -1);
1431 crpath = vfs_s_get_path (vpath, &super, 0);
1432 if (crpath == NULL)
1433 return -1;
1434 rpath = strutils_shell_escape (crpath);
1436 shell_commands =
1437 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_mkdir, (char *) NULL);
1438 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1439 g_free (shell_commands);
1441 g_free (rpath);
1442 ret_code = fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1444 if (ret_code != 0)
1445 return ret_code;
1447 if (!fish_exists (vpath))
1449 path_element->class->verrno = EACCES;
1450 return -1;
1452 return 0;
1455 /* --------------------------------------------------------------------------------------------- */
1457 static int
1458 fish_rmdir (const vfs_path_t * vpath)
1460 gchar *shell_commands = NULL;
1461 char buf[BUF_LARGE];
1462 const char *crpath;
1463 char *rpath;
1464 struct vfs_s_super *super;
1465 const vfs_path_element_t *path_element;
1467 path_element = vfs_path_get_by_index (vpath, -1);
1469 crpath = vfs_s_get_path (vpath, &super, 0);
1470 if (crpath == NULL)
1471 return -1;
1472 rpath = strutils_shell_escape (crpath);
1474 shell_commands =
1475 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_rmdir, (char *) NULL);
1476 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1477 g_free (shell_commands);
1478 g_free (rpath);
1479 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1482 /* --------------------------------------------------------------------------------------------- */
1484 static void
1485 fish_fh_free_data (vfs_file_handler_t * fh)
1487 if (fh != NULL)
1489 g_free (fh->data);
1490 fh->data = NULL;
1494 /* --------------------------------------------------------------------------------------------- */
1496 static int
1497 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1499 fish_fh_data_t *fish;
1501 (void) mode;
1503 fh->data = g_new0 (fish_fh_data_t, 1);
1504 fish = (fish_fh_data_t *) fh->data;
1506 /* File will be written only, so no need to retrieve it */
1507 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1509 /* user pressed the button [ Append ] in the "Copy" dialog */
1510 if ((flags & O_APPEND) != 0)
1511 fish->append = TRUE;
1513 if (!fh->ino->localname)
1515 vfs_path_t *vpath;
1516 int tmp_handle;
1518 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1519 if (tmp_handle == -1)
1521 vfs_path_free (vpath);
1522 goto fail;
1524 fh->ino->localname = vfs_path_to_str (vpath);
1525 vfs_path_free (vpath);
1526 close (tmp_handle);
1528 return 0;
1530 if (!fh->ino->localname && vfs_s_retrieve_file (me, fh->ino) == -1)
1531 goto fail;
1532 if (!fh->ino->localname)
1533 vfs_die ("retrieve_file failed to fill in localname");
1534 return 0;
1536 fail:
1537 fish_fh_free_data (fh);
1538 return -1;
1541 /* --------------------------------------------------------------------------------------------- */
1543 static void
1544 fish_fill_names (struct vfs_class *me, fill_names_f func)
1546 GList *iter;
1548 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1550 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1552 char *name;
1553 char gbuf[10];
1554 const char *flags = "";
1556 switch (super->path_element->port)
1558 case FISH_FLAG_RSH:
1559 flags = ":r";
1560 break;
1561 case FISH_FLAG_COMPRESSED:
1562 flags = ":C";
1563 break;
1564 default:
1565 if (super->path_element->port > FISH_FLAG_RSH)
1567 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1568 flags = gbuf;
1570 break;
1573 name =
1574 g_strconcat (vfs_fish_ops.prefix, VFS_PATH_URL_DELIMITER,
1575 super->path_element->user, "@", super->path_element->host, flags, "/",
1576 super->path_element->path, (char *) NULL);
1577 func (name);
1578 g_free (name);
1582 /* --------------------------------------------------------------------------------------------- */
1584 static void *
1585 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1588 sorry, i've places hack here
1589 cause fish don't able to open files with O_EXCL flag
1591 flags &= ~O_EXCL;
1592 return vfs_s_open (vpath, flags, mode);
1595 /* --------------------------------------------------------------------------------------------- */
1596 /*** public functions ****************************************************************************/
1597 /* --------------------------------------------------------------------------------------------- */
1599 void
1600 init_fish (void)
1602 static struct vfs_s_subclass fish_subclass;
1604 tcp_init ();
1606 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1607 fish_subclass.archive_same = fish_archive_same;
1608 fish_subclass.open_archive = fish_open_archive;
1609 fish_subclass.free_archive = fish_free_archive;
1610 fish_subclass.fh_open = fish_fh_open;
1611 fish_subclass.fh_free_data = fish_fh_free_data;
1612 fish_subclass.dir_load = fish_dir_load;
1613 fish_subclass.file_store = fish_file_store;
1614 fish_subclass.linear_start = fish_linear_start;
1615 fish_subclass.linear_read = fish_linear_read;
1616 fish_subclass.linear_close = fish_linear_close;
1618 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1619 vfs_fish_ops.name = "fish";
1620 vfs_fish_ops.prefix = "sh";
1621 vfs_fish_ops.fill_names = fish_fill_names;
1622 vfs_fish_ops.chmod = fish_chmod;
1623 vfs_fish_ops.chown = fish_chown;
1624 vfs_fish_ops.utime = fish_utime;
1625 vfs_fish_ops.open = fish_open;
1626 vfs_fish_ops.symlink = fish_symlink;
1627 vfs_fish_ops.link = fish_link;
1628 vfs_fish_ops.unlink = fish_unlink;
1629 vfs_fish_ops.rename = fish_rename;
1630 vfs_fish_ops.mkdir = fish_mkdir;
1631 vfs_fish_ops.rmdir = fish_rmdir;
1632 vfs_fish_ops.ctl = fish_ctl;
1633 vfs_register_class (&vfs_fish_ops);
1636 /* --------------------------------------------------------------------------------------------- */