(fish_file_store): type accuracy to avoid counter overflow while copiyng huge files.
[midnight-commander.git] / src / vfs / fish / fish.c
blob3eacf4d3d4c3c2302684b888269416df5576df07
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 #if 0
547 super->name =
548 g_strconcat ("sh://", super->path_element->user, "@", super->path_element->host, "/",
549 (char *) NULL);
550 #else
551 super->name = g_strdup (PATH_SEP_STR);
552 #endif
554 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat (me, S_IFDIR | 0755));
555 return 0;
558 /* --------------------------------------------------------------------------------------------- */
560 static int
561 fish_open_archive (struct vfs_s_super *super,
562 const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
564 (void) vpath;
566 super->data = g_new0 (fish_super_data_t, 1);
567 super->path_element = vfs_path_element_clone (vpath_element);
569 if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
570 super->path_element->port = FISH_FLAG_RSH;
572 SUP->scr_ls =
573 fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
574 SUP->scr_exists =
575 fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
576 FISH_EXISTS_DEF_CONTENT);
577 SUP->scr_mkdir =
578 fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
579 FISH_MKDIR_DEF_CONTENT);
580 SUP->scr_unlink =
581 fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
582 FISH_UNLINK_DEF_CONTENT);
583 SUP->scr_chown =
584 fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
585 FISH_CHOWN_DEF_CONTENT);
586 SUP->scr_chmod =
587 fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
588 FISH_CHMOD_DEF_CONTENT);
589 SUP->scr_utime =
590 fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
591 FISH_UTIME_DEF_CONTENT);
592 SUP->scr_rmdir =
593 fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
594 FISH_RMDIR_DEF_CONTENT);
595 SUP->scr_ln =
596 fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
597 SUP->scr_mv =
598 fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
599 SUP->scr_hardlink =
600 fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
601 FISH_HARDLINK_DEF_CONTENT);
602 SUP->scr_get =
603 fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
604 SUP->scr_send =
605 fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
606 FISH_SEND_DEF_CONTENT);
607 SUP->scr_append =
608 fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
609 FISH_APPEND_DEF_CONTENT);
610 SUP->scr_info =
611 fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
612 FISH_INFO_DEF_CONTENT);
614 return fish_open_archive_int (vpath_element->class, super);
617 /* --------------------------------------------------------------------------------------------- */
619 static int
620 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
621 const vfs_path_t * vpath, void *cookie)
623 vfs_path_element_t *path_element;
624 int result;
626 (void) vpath;
627 (void) cookie;
629 path_element = vfs_path_element_clone (vpath_element);
631 if (path_element->user == NULL)
632 path_element->user = vfs_get_local_username ();
634 result = ((strcmp (path_element->host, super->path_element->host) == 0)
635 && (strcmp (path_element->user, super->path_element->user) == 0)
636 && (path_element->port == super->path_element->port)) ? 1 : 0;
638 vfs_path_element_free (path_element);
640 return result;
643 /* --------------------------------------------------------------------------------------------- */
645 static int
646 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
648 struct vfs_s_super *super = dir->super;
649 char buffer[8192];
650 struct vfs_s_entry *ent = NULL;
651 FILE *logfile;
652 char *quoted_path;
653 int reply_code;
654 gchar *shell_commands;
657 * Simple FISH debug interface :]
659 #if 0
660 if (!(MEDATA->logfile))
662 MEDATA->logfile = fopen ("/tmp/mc-FISH.sh", "w");
664 #endif
665 logfile = MEDATA->logfile;
667 vfs_print_message (_("fish: Reading directory %s..."), remote_path);
669 gettimeofday (&dir->timestamp, NULL);
670 dir->timestamp.tv_sec += fish_directory_timeout;
671 quoted_path = strutils_shell_escape (remote_path);
672 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_ls, (char *) NULL);
673 fish_command (me, super, NONE, shell_commands, quoted_path);
674 g_free (shell_commands);
675 g_free (quoted_path);
676 ent = vfs_s_generate_entry (me, NULL, dir, 0);
677 while (TRUE)
679 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP->sockr);
680 if ((!res) || (res == EINTR))
682 vfs_s_free_entry (me, ent);
683 me->verrno = ECONNRESET;
684 goto error;
686 if (logfile)
688 fputs (buffer, logfile);
689 fputs ("\n", logfile);
690 fflush (logfile);
692 if (!strncmp (buffer, "### ", 4))
693 break;
694 if ((!buffer[0]))
696 if (ent->name)
698 vfs_s_insert_entry (me, dir, ent);
699 ent = vfs_s_generate_entry (me, NULL, dir, 0);
701 continue;
704 #define ST ent->ino->st
706 switch (buffer[0])
708 case ':':
710 char *temp;
711 char *data_start = buffer + 1;
712 char *filename = data_start;
713 char *filename_bound;
715 filename_bound = filename + strlen (filename);
717 if (!strcmp (data_start, "\".\"") || !strcmp (data_start, "\"..\""))
718 break; /* We'll do "." and ".." ourselves */
720 if (S_ISLNK (ST.st_mode))
722 char *linkname;
723 char *linkname_bound;
724 /* we expect: "escaped-name" -> "escaped-name"
725 // -> cannot occur in filenames,
726 // because it will be escaped to -\> */
729 linkname_bound = filename_bound;
731 if (*filename == '"')
732 ++filename;
734 linkname = strstr (filename, "\" -> \"");
735 if (!linkname)
737 /* broken client, or smth goes wrong */
738 linkname = filename_bound;
739 if (filename_bound > filename && *(filename_bound - 1) == '"')
740 --filename_bound; /* skip trailing " */
742 else
744 filename_bound = linkname;
745 linkname += 6; /* strlen ("\" -> \"") */
746 if (*(linkname_bound - 1) == '"')
747 --linkname_bound; /* skip trailing " */
750 ent->name = g_strndup (filename, filename_bound - filename);
751 temp = ent->name;
752 ent->name = strutils_shell_unescape (ent->name);
753 g_free (temp);
755 ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
756 temp = ent->ino->linkname;
757 ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
758 g_free (temp);
760 else
762 /* we expect: "escaped-name" */
763 if (filename_bound - filename > 2)
766 there is at least 2 "
767 and we skip them
769 if (*filename == '"')
770 ++filename;
771 if (*(filename_bound - 1) == '"')
772 --filename_bound;
774 ent->name = g_strndup (filename, filename_bound - filename);
775 temp = ent->name;
776 ent->name = strutils_shell_unescape (ent->name);
777 g_free (temp);
779 break;
781 case 'S':
782 #ifdef HAVE_ATOLL
783 ST.st_size = (off_t) atoll (buffer + 1);
784 #else
785 ST.st_size = (off_t) atof (buffer + 1);
786 #endif
787 break;
788 case 'P':
790 size_t skipped;
791 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
792 break;
794 case 'R':
797 raw filemode:
798 we expect: Roctal-filemode octal-filetype uid.gid
800 size_t skipped;
801 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
802 break;
804 case 'd':
806 vfs_split_text (buffer + 1);
807 if (!vfs_parse_filedate (0, &ST.st_ctime))
808 break;
809 ST.st_atime = ST.st_mtime = ST.st_ctime;
811 break;
812 case 'D':
814 struct tm tim;
815 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
816 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
817 break;
818 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
820 break;
821 case 'E':
823 int maj, min;
824 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
825 break;
826 #ifdef HAVE_STRUCT_STAT_ST_RDEV
827 ST.st_rdev = makedev (maj, min);
828 #endif
833 vfs_s_free_entry (me, ent);
834 reply_code = fish_decode_reply (buffer + 4, 0);
835 if (reply_code == COMPLETE)
837 vfs_print_message (_("%s: done."), me->name);
838 return 0;
841 me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
843 error:
844 vfs_print_message (_("%s: failure"), me->name);
845 return -1;
848 /* --------------------------------------------------------------------------------------------- */
850 static int
851 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
853 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
854 gchar *shell_commands = NULL;
855 struct vfs_s_super *super = FH_SUPER;
856 int code;
857 off_t total;
858 char buffer[BUF_8K];
859 struct stat s;
860 gboolean was_error = FALSE;
861 int h;
862 char *quoted_name;
864 h = open (localname, O_RDONLY);
865 if (h == -1)
866 ERRNOR (EIO, -1);
867 if (fstat (h, &s) < 0)
869 close (h);
870 ERRNOR (EIO, -1);
873 /* First, try this as stor:
875 * ( head -c number ) | ( cat > file; cat >/dev/null )
877 * If `head' is not present on the remote system, `dd' will be used.
878 * Unfortunately, we cannot trust most non-GNU `head' implementations
879 * even if `-c' options is supported. Therefore, we separate GNU head
880 * (and other modern heads?) using `-q' and `-' . This causes another
881 * implementations to fail (because of "incorrect options").
883 * Fallback is:
885 * rest=<number>
886 * while [ $rest -gt 0 ]
887 * do
888 * cnt=`expr \( $rest + 255 \) / 256`
889 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
890 * rest=`expr $rest - $n`
891 * done
893 * `dd' was not designed for full filling of input buffers,
894 * and does not report exact number of bytes (not blocks).
895 * Therefore a more complex shell script is needed.
897 * On some systems non-GNU head writes "Usage:" error report to stdout
898 * instead of stderr. It makes impossible the use of "head || dd"
899 * algorithm for file appending case, therefore just "dd" is used for it.
902 quoted_name = strutils_shell_escape (name);
903 vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
905 /* FIXME: File size is limited to ULONG_MAX */
906 if (fish->append)
908 shell_commands =
909 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
910 SUP->scr_append, (char *) NULL);
912 code = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
913 (uintmax_t) s.st_size);
914 g_free (shell_commands);
916 else
918 shell_commands =
919 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n",
920 SUP->scr_send, (char *) NULL);
921 code = fish_command (me, super, WAIT_REPLY, shell_commands, quoted_name,
922 (uintmax_t) s.st_size);
923 g_free (shell_commands);
925 if (code != PRELIM)
927 close (h);
928 ERRNOR (E_REMOTE, -1);
931 total = 0;
933 while (TRUE)
935 ssize_t n, t;
937 while ((n = read (h, buffer, sizeof (buffer))) < 0)
939 if ((errno == EINTR) && tty_got_interrupt ())
940 continue;
941 vfs_print_message (_("fish: Local read failed, sending zeros"));
942 close (h);
943 h = open ("/dev/zero", O_RDONLY);
946 if (n == 0)
947 break;
949 t = write (SUP->sockw, buffer, n);
950 if (t != n)
952 if (t == -1)
953 me->verrno = errno;
954 else
955 me->verrno = EIO;
956 goto error_return;
958 tty_disable_interrupt_key ();
959 total += n;
960 vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX,
961 was_error ? _("fish: storing zeros") : _("fish: storing file"),
962 (uintmax_t) total, (uintmax_t) s.st_size);
964 close (h);
965 g_free (quoted_name);
967 if ((fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE) || was_error)
968 ERRNOR (E_REMOTE, -1);
969 return 0;
971 error_return:
972 close (h);
973 fish_get_reply (me, SUP->sockr, NULL, 0);
974 g_free (quoted_name);
975 return -1;
978 /* --------------------------------------------------------------------------------------------- */
980 static int
981 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
983 fish_fh_data_t *fish;
984 gchar *shell_commands = NULL;
985 struct vfs_s_super *super = FH_SUPER;
986 char *name;
987 char *quoted_name;
989 if (fh->data == NULL)
990 fh->data = g_new0 (fish_fh_data_t, 1);
992 fish = (fish_fh_data_t *) fh->data;
994 name = vfs_s_fullpath (me, fh->ino);
995 if (name == NULL)
996 return 0;
997 quoted_name = strutils_shell_escape (name);
998 g_free (name);
999 fish->append = FALSE;
1002 * Check whether the remote file is readable by using `dd' to copy
1003 * a single byte from the remote file to /dev/null. If `dd' completes
1004 * with exit status of 0 use `cat' to send the file contents to the
1005 * standard output (i.e. over the network).
1008 shell_commands =
1009 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n",
1010 SUP->scr_get, (char *) NULL);
1011 offset = fish_command (me, super, WANT_STRING, shell_commands, quoted_name, (uintmax_t) offset);
1012 g_free (shell_commands);
1013 g_free (quoted_name);
1014 if (offset != PRELIM)
1015 ERRNOR (E_REMOTE, 0);
1016 fh->linear = LS_LINEAR_OPEN;
1017 fish->got = 0;
1018 errno = 0;
1019 #if SIZEOF_OFF_T == SIZEOF_LONG
1020 fish->total = (off_t) strtol (reply_str, NULL, 10);
1021 #else
1022 fish->total = (off_t) strtoll (reply_str, NULL, 10);
1023 #endif
1024 if (errno != 0)
1025 ERRNOR (E_REMOTE, 0);
1026 return 1;
1029 /* --------------------------------------------------------------------------------------------- */
1031 static void
1032 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
1034 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1035 struct vfs_s_super *super = FH_SUPER;
1036 char buffer[BUF_8K];
1037 int n;
1039 vfs_print_message (_("Aborting transfer..."));
1043 n = MIN (sizeof (buffer), (size_t) (fish->total - fish->got));
1044 if (n != 0)
1046 n = read (SUP->sockr, buffer, n);
1047 if (n < 0)
1048 return;
1049 fish->got += n;
1052 while (n != 0);
1054 if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1055 vfs_print_message (_("Error reported after abort."));
1056 else
1057 vfs_print_message (_("Aborted transfer would be successful."));
1060 /* --------------------------------------------------------------------------------------------- */
1062 static int
1063 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
1065 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1066 struct vfs_s_super *super = FH_SUPER;
1067 ssize_t n = 0;
1070 len = MIN ((size_t) (fish->total - fish->got), len);
1071 tty_disable_interrupt_key ();
1072 while (len != 0 && ((n = read (SUP->sockr, buf, len)) < 0))
1074 if ((errno == EINTR) && !tty_got_interrupt ())
1075 continue;
1076 break;
1078 tty_enable_interrupt_key ();
1080 if (n > 0)
1081 fish->got += n;
1082 else if (n < 0)
1083 fish_linear_abort (me, fh);
1084 else if (fish_get_reply (me, SUP->sockr, NULL, 0) != COMPLETE)
1085 ERRNOR (E_REMOTE, -1);
1086 ERRNOR (errno, n);
1089 /* --------------------------------------------------------------------------------------------- */
1091 static void
1092 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
1094 fish_fh_data_t *fish = (fish_fh_data_t *) fh->data;
1096 if (fish->total != fish->got)
1097 fish_linear_abort (me, fh);
1100 /* --------------------------------------------------------------------------------------------- */
1102 static int
1103 fish_ctl (void *fh, int ctlop, void *arg)
1105 (void) arg;
1106 (void) fh;
1107 (void) ctlop;
1108 return 0;
1109 #if 0
1110 switch (ctlop)
1112 case VFS_CTL_IS_NOTREADY:
1114 int v;
1116 if (!FH->linear)
1117 vfs_die ("You may not do this");
1118 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
1119 return 0;
1121 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
1122 if (((v < 0) && (errno == EINTR)) || v == 0)
1123 return 1;
1124 return 0;
1126 default:
1127 return 0;
1129 #endif
1132 /* --------------------------------------------------------------------------------------------- */
1134 static int
1135 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
1137 int r;
1139 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
1140 vfs_stamp_create (&vfs_fish_ops, super);
1141 if (r != COMPLETE)
1142 ERRNOR (E_REMOTE, -1);
1143 if (flags & OPT_FLUSH)
1144 vfs_s_invalidate (me, super);
1145 return 0;
1148 /* --------------------------------------------------------------------------------------------- */
1150 static int
1151 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1153 gchar *shell_commands = NULL;
1154 char buf[BUF_LARGE];
1155 const char *crpath1, *crpath2;
1156 char *rpath1, *rpath2;
1157 struct vfs_s_super *super, *super2;
1158 const vfs_path_element_t *path_element;
1160 path_element = vfs_path_get_by_index (vpath1, -1);
1162 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1163 if (crpath1 == NULL)
1164 return -1;
1166 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1167 if (crpath2 == NULL)
1168 return -1;
1170 rpath1 = strutils_shell_escape (crpath1);
1171 rpath2 = strutils_shell_escape (crpath2);
1172 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1173 SUP->scr_mv, (char *) NULL);
1174 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1175 g_free (shell_commands);
1176 g_free (rpath1);
1177 g_free (rpath2);
1178 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1181 /* --------------------------------------------------------------------------------------------- */
1183 static int
1184 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1186 gchar *shell_commands = NULL;
1187 char buf[BUF_LARGE];
1188 const char *crpath1, *crpath2;
1189 char *rpath1, *rpath2;
1190 struct vfs_s_super *super, *super2;
1191 const vfs_path_element_t *path_element;
1193 path_element = vfs_path_get_by_index (vpath1, -1);
1195 crpath1 = vfs_s_get_path (vpath1, &super, 0);
1196 if (crpath1 == NULL)
1197 return -1;
1199 crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1200 if (crpath2 == NULL)
1201 return -1;
1203 rpath1 = strutils_shell_escape (crpath1);
1204 rpath2 = strutils_shell_escape (crpath2);
1205 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1206 SUP->scr_hardlink, (char *) NULL);
1207 g_snprintf (buf, sizeof (buf), shell_commands, rpath1, rpath2);
1208 g_free (shell_commands);
1209 g_free (rpath1);
1210 g_free (rpath2);
1211 return fish_send_command (path_element->class, super2, buf, OPT_FLUSH);
1215 /* --------------------------------------------------------------------------------------------- */
1217 static int
1218 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1220 char *qsetto;
1221 gchar *shell_commands = NULL;
1222 char buf[BUF_LARGE];
1223 const char *crpath;
1224 char *rpath;
1225 struct vfs_s_super *super;
1226 const vfs_path_element_t *path_element;
1228 path_element = vfs_path_get_by_index (vpath2, -1);
1230 crpath = vfs_s_get_path (vpath2, &super, 0);
1231 if (crpath == NULL)
1232 return -1;
1234 rpath = strutils_shell_escape (crpath);
1235 qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1237 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILEFROM=%s FISH_FILETO=%s;\n",
1238 SUP->scr_ln, (char *) NULL);
1239 g_snprintf (buf, sizeof (buf), shell_commands, qsetto, rpath);
1240 g_free (shell_commands);
1241 g_free (qsetto);
1242 g_free (rpath);
1243 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1246 /* --------------------------------------------------------------------------------------------- */
1248 static int
1249 fish_chmod (const vfs_path_t * vpath, mode_t mode)
1251 gchar *shell_commands = NULL;
1252 char buf[BUF_LARGE];
1253 const char *crpath;
1254 char *rpath;
1255 struct vfs_s_super *super;
1256 const vfs_path_element_t *path_element;
1258 path_element = vfs_path_get_by_index (vpath, -1);
1260 crpath = vfs_s_get_path (vpath, &super, 0);
1261 if (crpath == NULL)
1262 return -1;
1263 rpath = strutils_shell_escape (crpath);
1265 shell_commands = g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n",
1266 SUP->scr_chmod, (char *) NULL);
1267 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (int) (mode & 07777));
1268 g_free (shell_commands);
1269 g_free (rpath);
1270 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1273 /* --------------------------------------------------------------------------------------------- */
1275 static int
1276 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
1278 char *sowner, *sgroup;
1279 struct passwd *pw;
1280 struct group *gr;
1282 pw = getpwuid (owner);
1283 if (pw == NULL)
1284 return 0;
1286 gr = getgrgid (group);
1287 if (gr == NULL)
1288 return 0;
1290 sowner = pw->pw_name;
1291 sgroup = gr->gr_name;
1294 gchar *shell_commands = NULL;
1295 char buf[BUF_LARGE];
1296 const char *crpath;
1297 char *rpath;
1298 struct vfs_s_super *super;
1299 const vfs_path_element_t *path_element;
1301 path_element = vfs_path_get_by_index (vpath, -1);
1303 crpath = vfs_s_get_path (vpath, &super, 0);
1304 if (crpath == NULL)
1305 return -1;
1306 rpath = strutils_shell_escape (crpath);
1308 shell_commands = g_strconcat (SUP->scr_env,
1309 "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n",
1310 SUP->scr_chown, (char *) NULL);
1311 g_snprintf (buf, sizeof (buf), shell_commands, rpath, sowner, sgroup);
1312 g_free (shell_commands);
1313 fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1314 /* FIXME: what should we report if chgrp succeeds but chown fails? */
1315 /* fish_send_command(me, super, buf, OPT_FLUSH); */
1316 g_free (rpath);
1317 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1321 /* --------------------------------------------------------------------------------------------- */
1323 static int
1324 fish_utime (const vfs_path_t * vpath, struct utimbuf *times)
1326 gchar *shell_commands = NULL;
1327 char utcmtime[16], utcatime[16];
1328 struct tm *gmt;
1330 char buf[BUF_LARGE];
1331 const char *crpath;
1332 char *rpath;
1333 struct vfs_s_super *super;
1334 const vfs_path_element_t *path_element;
1336 path_element = vfs_path_get_by_index (vpath, -1);
1338 crpath = vfs_s_get_path (vpath, &super, 0);
1339 if (crpath == NULL)
1340 return -1;
1341 rpath = strutils_shell_escape (crpath);
1343 gmt = gmtime (&times->modtime);
1344 g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1345 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1346 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1348 gmt = gmtime (&times->actime);
1349 g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1350 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1351 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1353 shell_commands =
1354 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld ",
1355 "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s;\n", SUP->scr_utime, (char *) NULL);
1356 g_snprintf (buf, sizeof (buf), shell_commands, rpath, (long) times->actime,
1357 (long) times->modtime, utcatime, utcmtime);
1358 g_free (shell_commands);
1359 g_free (rpath);
1360 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1363 /* --------------------------------------------------------------------------------------------- */
1365 static int
1366 fish_unlink (const vfs_path_t * vpath)
1368 gchar *shell_commands = NULL;
1370 char buf[BUF_LARGE];
1371 const char *crpath;
1372 char *rpath;
1373 struct vfs_s_super *super;
1374 const vfs_path_element_t *path_element;
1376 path_element = vfs_path_get_by_index (vpath, -1);
1378 crpath = vfs_s_get_path (vpath, &super, 0);
1379 if (crpath == NULL)
1380 return -1;
1381 rpath = strutils_shell_escape (crpath);
1383 shell_commands =
1384 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_unlink, (char *) NULL);
1385 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1386 g_free (shell_commands);
1387 g_free (rpath);
1388 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1391 /* --------------------------------------------------------------------------------------------- */
1393 static int
1394 fish_exists (const vfs_path_t * vpath)
1396 gchar *shell_commands = NULL;
1398 char buf[BUF_LARGE];
1399 const char *crpath;
1400 char *rpath;
1401 struct vfs_s_super *super;
1402 const vfs_path_element_t *path_element;
1404 path_element = vfs_path_get_by_index (vpath, -1);
1406 crpath = vfs_s_get_path (vpath, &super, 0);
1407 if (crpath == NULL)
1408 return -1;
1409 rpath = strutils_shell_escape (crpath);
1411 shell_commands =
1412 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_exists, (char *) NULL);
1413 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1414 g_free (shell_commands);
1415 g_free (rpath);
1417 return (fish_send_command (path_element->class, super, buf, OPT_FLUSH) == 0) ? 1 : 0;
1420 /* --------------------------------------------------------------------------------------------- */
1422 static int
1423 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
1425 gchar *shell_commands = NULL;
1426 int ret_code;
1427 char buf[BUF_LARGE];
1428 const char *crpath;
1429 char *rpath;
1430 struct vfs_s_super *super;
1431 const vfs_path_element_t *path_element;
1433 (void) mode;
1435 path_element = vfs_path_get_by_index (vpath, -1);
1437 crpath = vfs_s_get_path (vpath, &super, 0);
1438 if (crpath == NULL)
1439 return -1;
1440 rpath = strutils_shell_escape (crpath);
1442 shell_commands =
1443 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_mkdir, (char *) NULL);
1444 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1445 g_free (shell_commands);
1447 g_free (rpath);
1448 ret_code = fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1450 if (ret_code != 0)
1451 return ret_code;
1453 if (!fish_exists (vpath))
1455 path_element->class->verrno = EACCES;
1456 return -1;
1458 return 0;
1461 /* --------------------------------------------------------------------------------------------- */
1463 static int
1464 fish_rmdir (const vfs_path_t * vpath)
1466 gchar *shell_commands = NULL;
1467 char buf[BUF_LARGE];
1468 const char *crpath;
1469 char *rpath;
1470 struct vfs_s_super *super;
1471 const vfs_path_element_t *path_element;
1473 path_element = vfs_path_get_by_index (vpath, -1);
1475 crpath = vfs_s_get_path (vpath, &super, 0);
1476 if (crpath == NULL)
1477 return -1;
1478 rpath = strutils_shell_escape (crpath);
1480 shell_commands =
1481 g_strconcat (SUP->scr_env, "FISH_FILENAME=%s;\n", SUP->scr_rmdir, (char *) NULL);
1482 g_snprintf (buf, sizeof (buf), shell_commands, rpath);
1483 g_free (shell_commands);
1484 g_free (rpath);
1485 return fish_send_command (path_element->class, super, buf, OPT_FLUSH);
1488 /* --------------------------------------------------------------------------------------------- */
1490 static void
1491 fish_fh_free_data (vfs_file_handler_t * fh)
1493 if (fh != NULL)
1495 g_free (fh->data);
1496 fh->data = NULL;
1500 /* --------------------------------------------------------------------------------------------- */
1502 static int
1503 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
1505 fish_fh_data_t *fish;
1507 (void) mode;
1509 fh->data = g_new0 (fish_fh_data_t, 1);
1510 fish = (fish_fh_data_t *) fh->data;
1512 /* File will be written only, so no need to retrieve it */
1513 if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1515 /* user pressed the button [ Append ] in the "Copy" dialog */
1516 if ((flags & O_APPEND) != 0)
1517 fish->append = TRUE;
1519 if (!fh->ino->localname)
1521 vfs_path_t *vpath;
1522 int tmp_handle;
1524 tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1525 if (tmp_handle == -1)
1527 vfs_path_free (vpath);
1528 goto fail;
1530 fh->ino->localname = vfs_path_to_str (vpath);
1531 vfs_path_free (vpath);
1532 close (tmp_handle);
1534 return 0;
1536 if (!fh->ino->localname && vfs_s_retrieve_file (me, fh->ino) == -1)
1537 goto fail;
1538 if (!fh->ino->localname)
1539 vfs_die ("retrieve_file failed to fill in localname");
1540 return 0;
1542 fail:
1543 fish_fh_free_data (fh);
1544 return -1;
1547 /* --------------------------------------------------------------------------------------------- */
1549 static void
1550 fish_fill_names (struct vfs_class *me, fill_names_f func)
1552 GList *iter;
1554 for (iter = MEDATA->supers; iter != NULL; iter = g_list_next (iter))
1556 const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1558 char *name;
1559 char gbuf[10];
1560 const char *flags = "";
1562 switch (super->path_element->port)
1564 case FISH_FLAG_RSH:
1565 flags = ":r";
1566 break;
1567 case FISH_FLAG_COMPRESSED:
1568 flags = ":C";
1569 break;
1570 default:
1571 if (super->path_element->port > FISH_FLAG_RSH)
1573 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1574 flags = gbuf;
1576 break;
1579 name =
1580 g_strconcat (vfs_fish_ops.prefix, VFS_PATH_URL_DELIMITER,
1581 super->path_element->user, "@", super->path_element->host, flags, "/",
1582 super->path_element->path, (char *) NULL);
1583 func (name);
1584 g_free (name);
1588 /* --------------------------------------------------------------------------------------------- */
1590 static void *
1591 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
1594 sorry, i've places hack here
1595 cause fish don't able to open files with O_EXCL flag
1597 flags &= ~O_EXCL;
1598 return vfs_s_open (vpath, flags, mode);
1601 /* --------------------------------------------------------------------------------------------- */
1602 /*** public functions ****************************************************************************/
1603 /* --------------------------------------------------------------------------------------------- */
1605 void
1606 init_fish (void)
1608 static struct vfs_s_subclass fish_subclass;
1610 tcp_init ();
1612 fish_subclass.flags = VFS_S_REMOTE | VFS_S_USETMP;
1613 fish_subclass.archive_same = fish_archive_same;
1614 fish_subclass.open_archive = fish_open_archive;
1615 fish_subclass.free_archive = fish_free_archive;
1616 fish_subclass.fh_open = fish_fh_open;
1617 fish_subclass.fh_free_data = fish_fh_free_data;
1618 fish_subclass.dir_load = fish_dir_load;
1619 fish_subclass.file_store = fish_file_store;
1620 fish_subclass.linear_start = fish_linear_start;
1621 fish_subclass.linear_read = fish_linear_read;
1622 fish_subclass.linear_close = fish_linear_close;
1624 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1625 vfs_fish_ops.name = "fish";
1626 vfs_fish_ops.prefix = "sh";
1627 vfs_fish_ops.fill_names = fish_fill_names;
1628 vfs_fish_ops.chmod = fish_chmod;
1629 vfs_fish_ops.chown = fish_chown;
1630 vfs_fish_ops.utime = fish_utime;
1631 vfs_fish_ops.open = fish_open;
1632 vfs_fish_ops.symlink = fish_symlink;
1633 vfs_fish_ops.link = fish_link;
1634 vfs_fish_ops.unlink = fish_unlink;
1635 vfs_fish_ops.rename = fish_rename;
1636 vfs_fish_ops.mkdir = fish_mkdir;
1637 vfs_fish_ops.rmdir = fish_rmdir;
1638 vfs_fish_ops.ctl = fish_ctl;
1639 vfs_register_class (&vfs_fish_ops);
1642 /* --------------------------------------------------------------------------------------------- */