root/src/vfs/fish/fish.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. fish_set_blksize
  2. fish_default_stat
  3. fish_load_script_from_file
  4. fish_decode_reply
  5. fish_get_reply
  6. fish_command
  7. G_GNUC_PRINTF
  8. G_GNUC_PRINTF
  9. G_GNUC_PRINTF
  10. fish_new_archive
  11. fish_free_archive
  12. fish_pipeopen
  13. fish_set_env
  14. fish_info
  15. fish_open_archive_pipeopen
  16. fish_open_archive_talk
  17. fish_open_archive_int
  18. fish_open_archive
  19. fish_archive_same
  20. fish_dir_load
  21. fish_file_store
  22. fish_linear_start
  23. fish_linear_abort
  24. fish_linear_read
  25. fish_linear_close
  26. fish_ctl
  27. fish_rename
  28. fish_link
  29. fish_symlink
  30. fish_stat
  31. fish_lstat
  32. fish_fstat
  33. fish_chmod
  34. fish_chown
  35. fish_get_atime
  36. fish_get_mtime
  37. fish_utime
  38. fish_unlink
  39. fish_exists
  40. fish_mkdir
  41. fish_rmdir
  42. fish_fh_new
  43. fish_fh_open
  44. fish_fill_names
  45. fish_open
  46. vfs_init_fish

   1 /*
   2    Virtual File System: FISH implementation for transfering files over
   3    shell connections.
   4 
   5    Copyright (C) 1998-2019
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Pavel Machek, 1998
  10    Michal Svec, 2000
  11    Andrew Borodin <aborodin@vmail.ru>, 2010
  12    Slava Zanko <slavazanko@gmail.com>, 2010, 2013
  13    Ilia Maslakov <il.smind@gmail.com>, 2010
  14 
  15    Derived from ftpfs.c.
  16 
  17    This file is part of the Midnight Commander.
  18 
  19    The Midnight Commander is free software: you can redistribute it
  20    and/or modify it under the terms of the GNU General Public License as
  21    published by the Free Software Foundation, either version 3 of the License,
  22    or (at your option) any later version.
  23 
  24    The Midnight Commander is distributed in the hope that it will be useful,
  25    but WITHOUT ANY WARRANTY; without even the implied warranty of
  26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  27    GNU General Public License for more details.
  28 
  29    You should have received a copy of the GNU General Public License
  30    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  31  */
  32 
  33 /**
  34  * \file
  35  * \brief Source: Virtual File System: FISH implementation for transfering files over
  36  * shell connections
  37  * \author Pavel Machek
  38  * \author Michal Svec
  39  * \date 1998, 2000
  40  *
  41  * Derived from ftpfs.c
  42  * Read README.fish for protocol specification.
  43  *
  44  * Syntax of path is: \verbatim sh://user@host[:Cr]/path \endverbatim
  45  *      where C means you want compressed connection,
  46  *      and r means you want to use rsh
  47  *
  48  * Namespace: fish_vfs_ops exported.
  49  */
  50 
  51 /* Define this if your ssh can take -I option */
  52 
  53 #include <config.h>
  54 #include <errno.h>
  55 #include <pwd.h>
  56 #include <grp.h>
  57 #include <sys/time.h>           /* gettimeofday() */
  58 #include <stdlib.h>
  59 #include <string.h>
  60 #include <inttypes.h>           /* uintmax_t */
  61 
  62 #include "lib/global.h"
  63 #include "lib/tty/tty.h"        /* enable/disable interrupt key */
  64 #include "lib/strescape.h"
  65 #include "lib/unixcompat.h"
  66 #include "lib/fileloc.h"
  67 #include "lib/util.h"           /* my_exit() */
  68 #include "lib/mcconfig.h"
  69 
  70 #include "src/execute.h"        /* pre_exec, post_exec */
  71 
  72 #include "lib/vfs/vfs.h"
  73 #include "lib/vfs/utilvfs.h"
  74 #include "lib/vfs/netutil.h"
  75 #include "lib/vfs/xdirentry.h"
  76 #include "lib/vfs/gc.h"         /* vfs_stamp_create */
  77 
  78 #include "fish.h"
  79 #include "fishdef.h"
  80 
  81 /*** global variables ****************************************************************************/
  82 
  83 int fish_directory_timeout = 900;
  84 
  85 /*** file scope macro definitions ****************************************************************/
  86 
  87 #define DO_RESOLVE_SYMLINK 1
  88 #define DO_OPEN            2
  89 #define DO_FREE_RESOURCE   4
  90 
  91 #define FISH_FLAG_COMPRESSED 1
  92 #define FISH_FLAG_RSH        2
  93 
  94 #define OPT_FLUSH        1
  95 #define OPT_IGNORE_ERROR 2
  96 
  97 /*
  98  * Reply codes.
  99  */
 100 #define PRELIM          1       /* positive preliminary */
 101 #define COMPLETE        2       /* positive completion */
 102 #define CONTINUE        3       /* positive intermediate */
 103 #define TRANSIENT       4       /* transient negative completion */
 104 #define ERROR           5       /* permanent negative completion */
 105 
 106 /* command wait_flag: */
 107 #define NONE        0x00
 108 #define WAIT_REPLY  0x01
 109 #define WANT_STRING 0x02
 110 
 111 /* environment flags */
 112 #define FISH_HAVE_HEAD         1
 113 #define FISH_HAVE_SED          2
 114 #define FISH_HAVE_AWK          4
 115 #define FISH_HAVE_PERL         8
 116 #define FISH_HAVE_LSQ         16
 117 #define FISH_HAVE_DATE_MDYT   32
 118 #define FISH_HAVE_TAIL        64
 119 
 120 #define FISH_SUPER(super) ((fish_super_t *) (super))
 121 #define FISH_FILE_HANDLER(fh) ((fish_file_handler_t *) fh)
 122 
 123 /*** file scope type declarations ****************************************************************/
 124 
 125 typedef struct
 126 {
 127     struct vfs_s_super base;    /* base class */
 128 
 129     int sockr;
 130     int sockw;
 131     char *scr_ls;
 132     char *scr_chmod;
 133     char *scr_utime;
 134     char *scr_exists;
 135     char *scr_mkdir;
 136     char *scr_unlink;
 137     char *scr_chown;
 138     char *scr_rmdir;
 139     char *scr_ln;
 140     char *scr_mv;
 141     char *scr_hardlink;
 142     char *scr_get;
 143     char *scr_send;
 144     char *scr_append;
 145     char *scr_info;
 146     int host_flags;
 147     char *scr_env;
 148 } fish_super_t;
 149 
 150 typedef struct
 151 {
 152     vfs_file_handler_t base;    /* base class */
 153 
 154     off_t got;
 155     off_t total;
 156     gboolean append;
 157 } fish_file_handler_t;
 158 
 159 /*** file scope variables ************************************************************************/
 160 
 161 static char reply_str[80];
 162 
 163 static struct vfs_s_subclass fish_subclass;
 164 static struct vfs_class *vfs_fish_ops = VFS_CLASS (&fish_subclass);
 165 
 166 /* --------------------------------------------------------------------------------------------- */
 167 /*** file scope functions ************************************************************************/
 168 /* --------------------------------------------------------------------------------------------- */
 169 
 170 static void
 171 fish_set_blksize (struct stat *s)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
 174     /* redefine block size */
 175     s->st_blksize = 64 * 1024;  /* FIXME */
 176 #endif
 177 }
 178 
 179 /* --------------------------------------------------------------------------------------------- */
 180 
 181 static struct stat *
 182 fish_default_stat (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 183 {
 184     struct stat *s;
 185 
 186     s = vfs_s_default_stat (me, S_IFDIR | 0755);
 187     fish_set_blksize (s);
 188     vfs_adjust_stat (s);
 189 
 190     return s;
 191 }
 192 
 193 /* --------------------------------------------------------------------------------------------- */
 194 
 195 static char *
 196 fish_load_script_from_file (const char *hostname, const char *script_name, const char *def_content)
     /* [previous][next][first][last][top][bottom][index][help]  */
 197 {
 198     char *scr_filename = NULL;
 199     char *scr_content;
 200     gsize scr_len = 0;
 201 
 202     /* 1st: scan user directory */
 203     scr_filename = g_build_path (PATH_SEP_STR, mc_config_get_data_path (), FISH_PREFIX, hostname,
 204                                  script_name, (char *) NULL);
 205     /* silent about user dir */
 206     g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
 207     g_free (scr_filename);
 208     /* 2nd: scan system dir */
 209     if (scr_content == NULL)
 210     {
 211         scr_filename =
 212             g_build_path (PATH_SEP_STR, LIBEXECDIR, FISH_PREFIX, script_name, (char *) NULL);
 213         g_file_get_contents (scr_filename, &scr_content, &scr_len, NULL);
 214         g_free (scr_filename);
 215     }
 216 
 217     if (scr_content != NULL)
 218         return scr_content;
 219 
 220     return g_strdup (def_content);
 221 }
 222 
 223 /* --------------------------------------------------------------------------------------------- */
 224 
 225 static int
 226 fish_decode_reply (char *s, gboolean was_garbage)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     int code;
 229 
 230     /* cppcheck-suppress invalidscanf */
 231     if (sscanf (s, "%d", &code) == 0)
 232     {
 233         code = 500;
 234         return 5;
 235     }
 236     if (code < 100)
 237         return was_garbage ? ERROR : (code == 0 ? COMPLETE : PRELIM);
 238     return code / 100;
 239 }
 240 
 241 /* --------------------------------------------------------------------------------------------- */
 242 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
 243 
 244 static int
 245 fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 246 {
 247     char answer[BUF_1K];
 248     gboolean was_garbage = FALSE;
 249 
 250     while (TRUE)
 251     {
 252         if (!vfs_s_get_line (me, sock, answer, sizeof (answer), '\n'))
 253         {
 254             if (string_buf != NULL)
 255                 *string_buf = '\0';
 256             return 4;
 257         }
 258 
 259         if (strncmp (answer, "### ", 4) == 0)
 260             return fish_decode_reply (answer + 4, was_garbage ? 1 : 0);
 261 
 262         was_garbage = TRUE;
 263         if (string_buf != NULL)
 264             g_strlcpy (string_buf, answer, string_len);
 265     }
 266 }
 267 
 268 /* --------------------------------------------------------------------------------------------- */
 269 
 270 static int
 271 fish_command (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *cmd,
     /* [previous][next][first][last][top][bottom][index][help]  */
 272               size_t cmd_len)
 273 {
 274     ssize_t status;
 275     FILE *logfile = me->logfile;
 276 
 277     if (cmd_len == (size_t) (-1))
 278         cmd_len = strlen (cmd);
 279 
 280     if (logfile != NULL)
 281     {
 282         size_t ret;
 283 
 284         ret = fwrite (cmd, cmd_len, 1, logfile);
 285         ret = fflush (logfile);
 286         (void) ret;
 287     }
 288 
 289     tty_enable_interrupt_key ();
 290     status = write (FISH_SUPER (super)->sockw, cmd, cmd_len);
 291     tty_disable_interrupt_key ();
 292 
 293     if (status < 0)
 294         return TRANSIENT;
 295 
 296     if (wait_reply)
 297         return fish_get_reply (me, FISH_SUPER (super)->sockr,
 298                                (wait_reply & WANT_STRING) != 0 ? reply_str :
 299                                NULL, sizeof (reply_str) - 1);
 300     return COMPLETE;
 301 }
 302 
 303 /* --------------------------------------------------------------------------------------------- */
 304 
 305 static int
 306 G_GNUC_PRINTF (5, 0)
     /* [previous][next][first][last][top][bottom][index][help]  */
 307 fish_command_va (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
 308                  const char *vars, va_list ap)
 309 {
 310     int r;
 311     GString *command;
 312 
 313     command = g_string_new (FISH_SUPER (super)->scr_env);
 314     g_string_append_vprintf (command, vars, ap);
 315     g_string_append (command, scr);
 316     r = fish_command (me, super, wait_reply, command->str, command->len);
 317     g_string_free (command, TRUE);
 318 
 319     return r;
 320 }
 321 
 322 /* --------------------------------------------------------------------------------------------- */
 323 
 324 static int
 325 G_GNUC_PRINTF (5, 6)
     /* [previous][next][first][last][top][bottom][index][help]  */
 326 fish_command_v (struct vfs_class *me, struct vfs_s_super *super, int wait_reply, const char *scr,
 327                 const char *vars, ...)
 328 {
 329     int r;
 330     va_list ap;
 331 
 332     va_start (ap, vars);
 333     r = fish_command_va (me, super, wait_reply, scr, vars, ap);
 334     va_end (ap);
 335 
 336     return r;
 337 }
 338 
 339 /* --------------------------------------------------------------------------------------------- */
 340 
 341 static int
 342 G_GNUC_PRINTF (5, 6)
     /* [previous][next][first][last][top][bottom][index][help]  */
 343 fish_send_command (struct vfs_class *me, struct vfs_s_super *super, int flags, const char *scr,
 344                    const char *vars, ...)
 345 {
 346     int r;
 347     va_list ap;
 348 
 349     va_start (ap, vars);
 350     r = fish_command_va (me, super, WAIT_REPLY, scr, vars, ap);
 351     va_end (ap);
 352     vfs_stamp_create (vfs_fish_ops, super);
 353 
 354     if (r != COMPLETE)
 355         ERRNOR (E_REMOTE, -1);
 356     if ((flags & OPT_FLUSH) != 0)
 357         vfs_s_invalidate (me, super);
 358 
 359     return 0;
 360 }
 361 
 362 /* --------------------------------------------------------------------------------------------- */
 363 
 364 static struct vfs_s_super *
 365 fish_new_archive (struct vfs_class *me)
     /* [previous][next][first][last][top][bottom][index][help]  */
 366 {
 367     fish_super_t *arch;
 368 
 369     arch = g_new0 (fish_super_t, 1);
 370     arch->base.me = me;
 371 
 372     return VFS_SUPER (arch);
 373 }
 374 
 375 /* --------------------------------------------------------------------------------------------- */
 376 
 377 static void
 378 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 379 {
 380     fish_super_t *fish_super = FISH_SUPER (super);
 381 
 382     if ((fish_super->sockw != -1) || (fish_super->sockr != -1))
 383     {
 384         vfs_print_message (_("fish: Disconnecting from %s"), super->name ? super->name : "???");
 385         fish_command (me, super, NONE, "#BYE\nexit\n", -1);
 386         close (fish_super->sockw);
 387         close (fish_super->sockr);
 388         fish_super->sockw = fish_super->sockr = -1;
 389     }
 390     g_free (fish_super->scr_ls);
 391     g_free (fish_super->scr_exists);
 392     g_free (fish_super->scr_mkdir);
 393     g_free (fish_super->scr_unlink);
 394     g_free (fish_super->scr_chown);
 395     g_free (fish_super->scr_chmod);
 396     g_free (fish_super->scr_utime);
 397     g_free (fish_super->scr_rmdir);
 398     g_free (fish_super->scr_ln);
 399     g_free (fish_super->scr_mv);
 400     g_free (fish_super->scr_hardlink);
 401     g_free (fish_super->scr_get);
 402     g_free (fish_super->scr_send);
 403     g_free (fish_super->scr_append);
 404     g_free (fish_super->scr_info);
 405     g_free (fish_super->scr_env);
 406 }
 407 
 408 /* --------------------------------------------------------------------------------------------- */
 409 
 410 static void
 411 fish_pipeopen (struct vfs_s_super *super, const char *path, const char *argv[])
     /* [previous][next][first][last][top][bottom][index][help]  */
 412 {
 413     int fileset1[2], fileset2[2];
 414     int res;
 415 
 416     if ((pipe (fileset1) < 0) || (pipe (fileset2) < 0))
 417         vfs_die ("Cannot pipe(): %m.");
 418 
 419     res = fork ();
 420 
 421     if (res != 0)
 422     {
 423         if (res < 0)
 424             vfs_die ("Cannot fork(): %m.");
 425         /* We are the parent */
 426         close (fileset1[0]);
 427         FISH_SUPER (super)->sockw = fileset1[1];
 428         close (fileset2[1]);
 429         FISH_SUPER (super)->sockr = fileset2[0];
 430     }
 431     else
 432     {
 433         res = dup2 (fileset1[0], STDIN_FILENO);
 434         close (fileset1[0]);
 435         close (fileset1[1]);
 436         res = dup2 (fileset2[1], STDOUT_FILENO);
 437         close (STDERR_FILENO);
 438         /* stderr to /dev/null */
 439         res = open ("/dev/null", O_WRONLY);
 440         close (fileset2[0]);
 441         close (fileset2[1]);
 442         execvp (path, (char **) argv);
 443         my_exit (3);
 444     }
 445 }
 446 
 447 /* --------------------------------------------------------------------------------------------- */
 448 
 449 static char *
 450 fish_set_env (int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 451 {
 452     GString *tmp;
 453 
 454     tmp = g_string_sized_new (250);
 455 
 456     if ((flags & FISH_HAVE_HEAD) != 0)
 457         g_string_append (tmp, "FISH_HAVE_HEAD=1 export FISH_HAVE_HEAD; ");
 458 
 459     if ((flags & FISH_HAVE_SED) != 0)
 460         g_string_append (tmp, "FISH_HAVE_SED=1 export FISH_HAVE_SED; ");
 461 
 462     if ((flags & FISH_HAVE_AWK) != 0)
 463         g_string_append (tmp, "FISH_HAVE_AWK=1 export FISH_HAVE_AWK; ");
 464 
 465     if ((flags & FISH_HAVE_PERL) != 0)
 466         g_string_append (tmp, "FISH_HAVE_PERL=1 export FISH_HAVE_PERL; ");
 467 
 468     if ((flags & FISH_HAVE_LSQ) != 0)
 469         g_string_append (tmp, "FISH_HAVE_LSQ=1 export FISH_HAVE_LSQ; ");
 470 
 471     if ((flags & FISH_HAVE_DATE_MDYT) != 0)
 472         g_string_append (tmp, "FISH_HAVE_DATE_MDYT=1 export FISH_HAVE_DATE_MDYT; ");
 473 
 474     if ((flags & FISH_HAVE_TAIL) != 0)
 475         g_string_append (tmp, "FISH_HAVE_TAIL=1 export FISH_HAVE_TAIL; ");
 476 
 477     return g_string_free (tmp, FALSE);
 478 }
 479 
 480 /* --------------------------------------------------------------------------------------------- */
 481 
 482 static gboolean
 483 fish_info (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 484 {
 485     fish_super_t *fish_super = FISH_SUPER (super);
 486 
 487     if (fish_command (me, super, NONE, fish_super->scr_info, -1) == COMPLETE)
 488     {
 489         while (TRUE)
 490         {
 491             int res;
 492             char buffer[BUF_8K];
 493 
 494             res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), fish_super->sockr);
 495             if ((res == 0) || (res == EINTR))
 496                 ERRNOR (ECONNRESET, FALSE);
 497             if (strncmp (buffer, "### ", 4) == 0)
 498                 break;
 499             fish_super->host_flags = atol (buffer);
 500         }
 501         return TRUE;
 502     }
 503     ERRNOR (E_PROTO, FALSE);
 504 }
 505 
 506 /* --------------------------------------------------------------------------------------------- */
 507 
 508 static void
 509 fish_open_archive_pipeopen (struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 510 {
 511     char gbuf[10];
 512     const char *argv[10];       /* All of 10 is used now */
 513     const char *xsh = (super->path_element->port == FISH_FLAG_RSH ? "rsh" : "ssh");
 514     int i = 0;
 515 
 516     argv[i++] = xsh;
 517     if (super->path_element->port == FISH_FLAG_COMPRESSED)
 518         argv[i++] = "-C";
 519 
 520     if (super->path_element->port > FISH_FLAG_RSH)
 521     {
 522         argv[i++] = "-p";
 523         g_snprintf (gbuf, sizeof (gbuf), "%d", super->path_element->port);
 524         argv[i++] = gbuf;
 525     }
 526 
 527     /*
 528      * Add the user name to the ssh command line only if it was explicitly
 529      * set in vfs URL. rsh/ssh will get current user by default
 530      * plus we can set convenient overrides in  ~/.ssh/config (explicit -l
 531      * option breaks it for some)
 532      */
 533 
 534     if (super->path_element->user != NULL)
 535     {
 536         argv[i++] = "-l";
 537         argv[i++] = super->path_element->user;
 538     }
 539     else
 540     {
 541         /* The rest of the code assumes it to be a valid username */
 542         super->path_element->user = vfs_get_local_username ();
 543     }
 544 
 545     argv[i++] = super->path_element->host;
 546     argv[i++] = "echo FISH:; /bin/sh";
 547     argv[i++] = NULL;
 548 
 549     fish_pipeopen (super, xsh, argv);
 550 }
 551 
 552 /* --------------------------------------------------------------------------------------------- */
 553 
 554 static gboolean
 555 fish_open_archive_talk (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 556 {
 557     fish_super_t *fish_super = FISH_SUPER (super);
 558     char answer[2048];
 559 
 560     printf ("\n%s\n", _("fish: Waiting for initial line..."));
 561 
 562     if (vfs_s_get_line (me, fish_super->sockr, answer, sizeof (answer), ':') == 0)
 563         return FALSE;
 564 
 565     if (strstr (answer, "assword") != NULL)
 566     {
 567         /* Currently, this does not work. ssh reads passwords from
 568            /dev/tty, not from stdin :-(. */
 569 
 570         printf ("\n%s\n", _("Sorry, we cannot do password authenticated connections for now."));
 571 
 572         return FALSE;
 573 #if 0
 574         if (super->path_element->password == NULL)
 575         {
 576             char *p, *op;
 577 
 578             p = g_strdup_printf (_("fish: Password is required for %s"), super->path_element->user);
 579             op = vfs_get_password (p);
 580             g_free (p);
 581             if (op == NULL)
 582                 return FALSE;
 583             super->path_element->password = op;
 584         }
 585 
 586         printf ("\n%s\n", _("fish: Sending password..."));
 587 
 588         {
 589             size_t str_len;
 590 
 591             str_len = strlen (super->path_element->password);
 592             if ((write (fish_super.sockw, super->path_element->password, str_len) !=
 593                  (ssize_t) str_len) || (write (fish_super->sockw, "\n", 1) != 1))
 594                 return FALSE;
 595         }
 596 #endif
 597     }
 598     return TRUE;
 599 }
 600 
 601 /* --------------------------------------------------------------------------------------------- */
 602 
 603 static int
 604 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
     /* [previous][next][first][last][top][bottom][index][help]  */
 605 {
 606     gboolean ftalk;
 607 
 608     /* hide panels */
 609     pre_exec ();
 610 
 611     /* open pipe */
 612     fish_open_archive_pipeopen (super);
 613 
 614     /* Start talk with ssh-server (password prompt, etc ) */
 615     ftalk = fish_open_archive_talk (me, super);
 616 
 617     /* show panels */
 618     post_exec ();
 619 
 620     if (!ftalk)
 621         ERRNOR (E_PROTO, -1);
 622 
 623     vfs_print_message ("%s", _("fish: Sending initial line..."));
 624     /*
 625      * Run 'start_fish_server'. If it doesn't exist - no problem,
 626      * we'll talk directly to the shell.
 627      */
 628 
 629     if (fish_command
 630         (me, super, WAIT_REPLY, "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n",
 631          -1) != COMPLETE)
 632         ERRNOR (E_PROTO, -1);
 633 
 634     vfs_print_message ("%s", _("fish: Handshaking version..."));
 635     if (fish_command (me, super, WAIT_REPLY, "#VER 0.0.3\necho '### 000'\n", -1) != COMPLETE)
 636         ERRNOR (E_PROTO, -1);
 637 
 638     /* Set up remote locale to C, otherwise dates cannot be recognized */
 639     if (fish_command
 640         (me, super, WAIT_REPLY,
 641          "LANG=C LC_ALL=C LC_TIME=C; export LANG LC_ALL LC_TIME;\n" "echo '### 200'\n",
 642          -1) != COMPLETE)
 643         ERRNOR (E_PROTO, -1);
 644 
 645     vfs_print_message ("%s", _("fish: Getting host info..."));
 646     if (fish_info (me, super))
 647         FISH_SUPER (super)->scr_env = fish_set_env (FISH_SUPER (super)->host_flags);
 648 
 649 #if 0
 650     super->name =
 651         g_strconcat ("sh://", super->path_element->user, "@", super->path_element->host,
 652                      PATH_SEP_STR, (char *) NULL);
 653 #else
 654     super->name = g_strdup (PATH_SEP_STR);
 655 #endif
 656 
 657     super->root = vfs_s_new_inode (me, super, fish_default_stat (me));
 658 
 659     return 0;
 660 }
 661 
 662 /* --------------------------------------------------------------------------------------------- */
 663 
 664 static int
 665 fish_open_archive (struct vfs_s_super *super,
     /* [previous][next][first][last][top][bottom][index][help]  */
 666                    const vfs_path_t * vpath, const vfs_path_element_t * vpath_element)
 667 {
 668     fish_super_t *fish_super = FISH_SUPER (super);
 669 
 670     (void) vpath;
 671 
 672     super->path_element = vfs_path_element_clone (vpath_element);
 673 
 674     if (strncmp (vpath_element->vfs_prefix, "rsh", 3) == 0)
 675         super->path_element->port = FISH_FLAG_RSH;
 676 
 677     fish_super->scr_ls =
 678         fish_load_script_from_file (super->path_element->host, FISH_LS_FILE, FISH_LS_DEF_CONTENT);
 679     fish_super->scr_exists =
 680         fish_load_script_from_file (super->path_element->host, FISH_EXISTS_FILE,
 681                                     FISH_EXISTS_DEF_CONTENT);
 682     fish_super->scr_mkdir =
 683         fish_load_script_from_file (super->path_element->host, FISH_MKDIR_FILE,
 684                                     FISH_MKDIR_DEF_CONTENT);
 685     fish_super->scr_unlink =
 686         fish_load_script_from_file (super->path_element->host, FISH_UNLINK_FILE,
 687                                     FISH_UNLINK_DEF_CONTENT);
 688     fish_super->scr_chown =
 689         fish_load_script_from_file (super->path_element->host, FISH_CHOWN_FILE,
 690                                     FISH_CHOWN_DEF_CONTENT);
 691     fish_super->scr_chmod =
 692         fish_load_script_from_file (super->path_element->host, FISH_CHMOD_FILE,
 693                                     FISH_CHMOD_DEF_CONTENT);
 694     fish_super->scr_utime =
 695         fish_load_script_from_file (super->path_element->host, FISH_UTIME_FILE,
 696                                     FISH_UTIME_DEF_CONTENT);
 697     fish_super->scr_rmdir =
 698         fish_load_script_from_file (super->path_element->host, FISH_RMDIR_FILE,
 699                                     FISH_RMDIR_DEF_CONTENT);
 700     fish_super->scr_ln =
 701         fish_load_script_from_file (super->path_element->host, FISH_LN_FILE, FISH_LN_DEF_CONTENT);
 702     fish_super->scr_mv =
 703         fish_load_script_from_file (super->path_element->host, FISH_MV_FILE, FISH_MV_DEF_CONTENT);
 704     fish_super->scr_hardlink =
 705         fish_load_script_from_file (super->path_element->host, FISH_HARDLINK_FILE,
 706                                     FISH_HARDLINK_DEF_CONTENT);
 707     fish_super->scr_get =
 708         fish_load_script_from_file (super->path_element->host, FISH_GET_FILE, FISH_GET_DEF_CONTENT);
 709     fish_super->scr_send =
 710         fish_load_script_from_file (super->path_element->host, FISH_SEND_FILE,
 711                                     FISH_SEND_DEF_CONTENT);
 712     fish_super->scr_append =
 713         fish_load_script_from_file (super->path_element->host, FISH_APPEND_FILE,
 714                                     FISH_APPEND_DEF_CONTENT);
 715     fish_super->scr_info =
 716         fish_load_script_from_file (super->path_element->host, FISH_INFO_FILE,
 717                                     FISH_INFO_DEF_CONTENT);
 718 
 719     return fish_open_archive_int (vpath_element->class, super);
 720 }
 721 
 722 /* --------------------------------------------------------------------------------------------- */
 723 
 724 static int
 725 fish_archive_same (const vfs_path_element_t * vpath_element, struct vfs_s_super *super,
     /* [previous][next][first][last][top][bottom][index][help]  */
 726                    const vfs_path_t * vpath, void *cookie)
 727 {
 728     vfs_path_element_t *path_element;
 729     int result;
 730 
 731     (void) vpath;
 732     (void) cookie;
 733 
 734     path_element = vfs_path_element_clone (vpath_element);
 735 
 736     if (path_element->user == NULL)
 737         path_element->user = vfs_get_local_username ();
 738 
 739     result = ((strcmp (path_element->host, super->path_element->host) == 0)
 740               && (strcmp (path_element->user, super->path_element->user) == 0)
 741               && (path_element->port == super->path_element->port)) ? 1 : 0;
 742 
 743     vfs_path_element_free (path_element);
 744 
 745     return result;
 746 }
 747 
 748 /* --------------------------------------------------------------------------------------------- */
 749 
 750 static int
 751 fish_dir_load (struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
     /* [previous][next][first][last][top][bottom][index][help]  */
 752 {
 753     struct vfs_s_super *super = dir->super;
 754     char buffer[BUF_8K] = "\0";
 755     struct vfs_s_entry *ent = NULL;
 756     char *quoted_path;
 757     int reply_code;
 758 
 759     /*
 760      * Simple FISH debug interface :]
 761      */
 762 #if 0
 763     if (me->logfile == NULL)
 764         me->logfile = fopen ("/tmp/mc-FISH.sh", "w");
 765 #endif
 766 
 767     vfs_print_message (_("fish: Reading directory %s..."), remote_path);
 768 
 769     gettimeofday (&dir->timestamp, NULL);
 770     dir->timestamp.tv_sec += fish_directory_timeout;
 771 
 772     quoted_path = strutils_shell_escape (remote_path);
 773     (void) fish_command_v (me, super, NONE, FISH_SUPER (super)->scr_ls, "FISH_FILENAME=%s;\n",
 774                            quoted_path);
 775     g_free (quoted_path);
 776 
 777     ent = vfs_s_generate_entry (me, NULL, dir, 0);
 778 
 779     while (TRUE)
 780     {
 781         int res;
 782 
 783         res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), FISH_SUPER (super)->sockr);
 784 
 785         if ((res == 0) || (res == EINTR))
 786         {
 787             vfs_s_free_entry (me, ent);
 788             me->verrno = ECONNRESET;
 789             goto error;
 790         }
 791         if (me->logfile != NULL)
 792         {
 793             fputs (buffer, me->logfile);
 794             fputs ("\n", me->logfile);
 795             fflush (me->logfile);
 796         }
 797         if (strncmp (buffer, "### ", 4) == 0)
 798             break;
 799         if (buffer[0] == '\0')
 800         {
 801             if (ent->name != NULL)
 802             {
 803                 vfs_s_insert_entry (me, dir, ent);
 804                 ent = vfs_s_generate_entry (me, NULL, dir, 0);
 805             }
 806             continue;
 807         }
 808 
 809 #define ST ent->ino->st
 810 
 811         switch (buffer[0])
 812         {
 813         case ':':
 814             {
 815                 char *temp;
 816                 char *data_start = buffer + 1;
 817                 char *filename = data_start;
 818                 char *filename_bound;
 819 
 820                 filename_bound = filename + strlen (filename);
 821 
 822                 if (strcmp (data_start, "\".\"") == 0 || strcmp (data_start, "\"..\"") == 0)
 823                     break;      /* We'll do "." and ".." ourselves */
 824 
 825                 if (S_ISLNK (ST.st_mode))
 826                 {
 827                     char *linkname;
 828                     char *linkname_bound;
 829                     /* we expect: "escaped-name" -> "escaped-name"
 830                        //     -> cannot occur in filenames,
 831                        //     because it will be escaped to -\> */
 832 
 833 
 834                     linkname_bound = filename_bound;
 835 
 836                     if (*filename == '"')
 837                         ++filename;
 838 
 839                     linkname = strstr (filename, "\" -> \"");
 840                     if (linkname == NULL)
 841                     {
 842                         /* broken client, or smth goes wrong */
 843                         linkname = filename_bound;
 844                         if (filename_bound > filename && *(filename_bound - 1) == '"')
 845                             --filename_bound;   /* skip trailing " */
 846                     }
 847                     else
 848                     {
 849                         filename_bound = linkname;
 850                         linkname += 6;  /* strlen ("\" -> \"") */
 851                         if (*(linkname_bound - 1) == '"')
 852                             --linkname_bound;   /* skip trailing " */
 853                     }
 854 
 855                     ent->name = g_strndup (filename, filename_bound - filename);
 856                     temp = ent->name;
 857                     ent->name = strutils_shell_unescape (ent->name);
 858                     g_free (temp);
 859 
 860                     ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
 861                     temp = ent->ino->linkname;
 862                     ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
 863                     g_free (temp);
 864                 }
 865                 else
 866                 {
 867                     /* we expect: "escaped-name" */
 868                     if (filename_bound - filename > 2)
 869                     {
 870                         /*
 871                            there is at least 2 "
 872                            and we skip them
 873                          */
 874                         if (*filename == '"')
 875                             ++filename;
 876                         if (*(filename_bound - 1) == '"')
 877                             --filename_bound;
 878                     }
 879                     ent->name = g_strndup (filename, filename_bound - filename);
 880                     temp = ent->name;
 881                     ent->name = strutils_shell_unescape (ent->name);
 882                     g_free (temp);
 883                 }
 884                 break;
 885             }
 886         case 'S':
 887             ST.st_size = (off_t) g_ascii_strtoll (buffer + 1, NULL, 10);
 888             break;
 889         case 'P':
 890             {
 891                 size_t skipped;
 892 
 893                 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
 894                 break;
 895             }
 896         case 'R':
 897             {
 898                 /*
 899                    raw filemode:
 900                    we expect: Roctal-filemode octal-filetype uid.gid
 901                  */
 902                 size_t skipped;
 903 
 904                 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
 905                 break;
 906             }
 907         case 'd':
 908             {
 909                 vfs_split_text (buffer + 1);
 910                 if (vfs_parse_filedate (0, &ST.st_ctime) == 0)
 911                     break;
 912                 ST.st_atime = ST.st_mtime = ST.st_ctime;
 913 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 914                 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
 915 #endif
 916             }
 917             break;
 918         case 'D':
 919             {
 920                 struct tm tim;
 921 
 922                 /* cppcheck-suppress invalidscanf */
 923                 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
 924                             &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
 925                     break;
 926                 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
 927 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 928                 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
 929 #endif
 930             }
 931             break;
 932         case 'E':
 933             {
 934                 int maj, min;
 935 
 936                 /* cppcheck-suppress invalidscanf */
 937                 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
 938                     break;
 939 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 940                 ST.st_rdev = makedev (maj, min);
 941 #endif
 942             }
 943         default:
 944             break;
 945         }
 946     }
 947 
 948     vfs_s_free_entry (me, ent);
 949     reply_code = fish_decode_reply (buffer + 4, 0);
 950     if (reply_code == COMPLETE)
 951     {
 952         vfs_print_message (_("%s: done."), me->name);
 953         return 0;
 954     }
 955 
 956     me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
 957 
 958   error:
 959     vfs_print_message (_("%s: failure"), me->name);
 960     return -1;
 961 }
 962 
 963 /* --------------------------------------------------------------------------------------------- */
 964 
 965 static int
 966 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
     /* [previous][next][first][last][top][bottom][index][help]  */
 967 {
 968     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
 969     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
 970     fish_super_t *fish_super = FISH_SUPER (super);
 971     int code;
 972     off_t total = 0;
 973     char buffer[BUF_8K];
 974     struct stat s;
 975     int h;
 976     char *quoted_name;
 977 
 978     h = open (localname, O_RDONLY);
 979     if (h == -1)
 980         ERRNOR (EIO, -1);
 981     if (fstat (h, &s) < 0)
 982     {
 983         close (h);
 984         ERRNOR (EIO, -1);
 985     }
 986 
 987     /* First, try this as stor:
 988      *
 989      *     ( head -c number ) | ( cat > file; cat >/dev/null )
 990      *
 991      *  If 'head' is not present on the remote system, 'dd' will be used.
 992      * Unfortunately, we cannot trust most non-GNU 'head' implementations
 993      * even if '-c' options is supported. Therefore, we separate GNU head
 994      * (and other modern heads?) using '-q' and '-' . This causes another
 995      * implementations to fail (because of "incorrect options").
 996      *
 997      *  Fallback is:
 998      *
 999      *     rest=<number>
1000      *     while [ $rest -gt 0 ]
1001      *     do
1002      *        cnt=`expr \( $rest + 255 \) / 256`
1003      *        n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
1004      *        rest=`expr $rest - $n`
1005      *     done
1006      *
1007      *  'dd' was not designed for full filling of input buffers,
1008      *  and does not report exact number of bytes (not blocks).
1009      *  Therefore a more complex shell script is needed.
1010      *
1011      *   On some systems non-GNU head writes "Usage:" error report to stdout
1012      *  instead of stderr. It makes impossible the use of "head || dd"
1013      *  algorithm for file appending case, therefore just "dd" is used for it.
1014      */
1015 
1016     quoted_name = strutils_shell_escape (name);
1017     vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
1018 
1019     /* FIXME: File size is limited to ULONG_MAX */
1020     code =
1021         fish_command_v (me, super, WAIT_REPLY,
1022                         fish->append ? fish_super->scr_append : fish_super->scr_send,
1023                         "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n", quoted_name,
1024                         (uintmax_t) s.st_size);
1025     g_free (quoted_name);
1026 
1027     if (code != PRELIM)
1028     {
1029         close (h);
1030         ERRNOR (E_REMOTE, -1);
1031     }
1032 
1033     while (TRUE)
1034     {
1035         ssize_t n, t;
1036 
1037         while ((n = read (h, buffer, sizeof (buffer))) < 0)
1038         {
1039             if ((errno == EINTR) && tty_got_interrupt ())
1040                 continue;
1041             vfs_print_message ("%s", _("fish: Local read failed, sending zeros"));
1042             close (h);
1043             h = open ("/dev/zero", O_RDONLY);
1044         }
1045 
1046         if (n == 0)
1047             break;
1048 
1049         t = write (fish_super->sockw, buffer, n);
1050         if (t != n)
1051         {
1052             if (t == -1)
1053                 me->verrno = errno;
1054             else
1055                 me->verrno = EIO;
1056             goto error_return;
1057         }
1058         tty_disable_interrupt_key ();
1059         total += n;
1060         vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _("fish: storing file"),
1061                            (uintmax_t) total, (uintmax_t) s.st_size);
1062     }
1063     close (h);
1064 
1065     if (fish_get_reply (me, fish_super->sockr, NULL, 0) != COMPLETE)
1066         ERRNOR (E_REMOTE, -1);
1067     return 0;
1068 
1069   error_return:
1070     close (h);
1071     fish_get_reply (me, fish_super->sockr, NULL, 0);
1072     return -1;
1073 }
1074 
1075 /* --------------------------------------------------------------------------------------------- */
1076 
1077 static int
1078 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
1079 {
1080     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1081     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1082     char *name;
1083     char *quoted_name;
1084 
1085     name = vfs_s_fullpath (me, fh->ino);
1086     if (name == NULL)
1087         return 0;
1088     quoted_name = strutils_shell_escape (name);
1089     g_free (name);
1090     fish->append = FALSE;
1091 
1092     /*
1093      * Check whether the remote file is readable by using 'dd' to copy 
1094      * a single byte from the remote file to /dev/null. If 'dd' completes
1095      * with exit status of 0 use 'cat' to send the file contents to the
1096      * standard output (i.e. over the network).
1097      */
1098 
1099     offset =
1100         fish_command_v (me, super, WANT_STRING, FISH_SUPER (super)->scr_get,
1101                         "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n", quoted_name,
1102                         (uintmax_t) offset);
1103     g_free (quoted_name);
1104 
1105     if (offset != PRELIM)
1106         ERRNOR (E_REMOTE, 0);
1107     fh->linear = LS_LINEAR_OPEN;
1108     fish->got = 0;
1109     errno = 0;
1110 #if SIZEOF_OFF_T == SIZEOF_LONG
1111     fish->total = (off_t) strtol (reply_str, NULL, 10);
1112 #else
1113     fish->total = (off_t) g_ascii_strtoll (reply_str, NULL, 10);
1114 #endif
1115     if (errno != 0)
1116         ERRNOR (E_REMOTE, 0);
1117     return 1;
1118 }
1119 
1120 /* --------------------------------------------------------------------------------------------- */
1121 
1122 static void
1123 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1124 {
1125     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1126     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1127     char buffer[BUF_8K];
1128     ssize_t n;
1129 
1130     vfs_print_message ("%s", _("Aborting transfer..."));
1131 
1132     do
1133     {
1134         n = MIN ((off_t) sizeof (buffer), (fish->total - fish->got));
1135         if (n != 0)
1136         {
1137             n = read (FISH_SUPER (super)->sockr, buffer, n);
1138             if (n < 0)
1139                 return;
1140             fish->got += n;
1141         }
1142     }
1143     while (n != 0);
1144 
1145     if (fish_get_reply (me, FISH_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1146         vfs_print_message ("%s", _("Error reported after abort."));
1147     else
1148         vfs_print_message ("%s", _("Aborted transfer would be successful."));
1149 }
1150 
1151 /* --------------------------------------------------------------------------------------------- */
1152 
1153 static ssize_t
1154 fish_linear_read (struct vfs_class *me, vfs_file_handler_t * fh, void *buf, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
1155 {
1156     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1157     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1158     ssize_t n = 0;
1159 
1160     len = MIN ((size_t) (fish->total - fish->got), len);
1161     tty_disable_interrupt_key ();
1162     while (len != 0 && ((n = read (FISH_SUPER (super)->sockr, buf, len)) < 0))
1163     {
1164         if ((errno == EINTR) && !tty_got_interrupt ())
1165             continue;
1166         break;
1167     }
1168     tty_enable_interrupt_key ();
1169 
1170     if (n > 0)
1171         fish->got += n;
1172     else if (n < 0)
1173         fish_linear_abort (me, fh);
1174     else if (fish_get_reply (me, FISH_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1175         ERRNOR (E_REMOTE, -1);
1176     ERRNOR (errno, n);
1177 }
1178 
1179 /* --------------------------------------------------------------------------------------------- */
1180 
1181 static void
1182 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1183 {
1184     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1185 
1186     if (fish->total != fish->got)
1187         fish_linear_abort (me, fh);
1188 }
1189 
1190 /* --------------------------------------------------------------------------------------------- */
1191 
1192 static int
1193 fish_ctl (void *fh, int ctlop, void *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
1194 {
1195     (void) arg;
1196     (void) fh;
1197     (void) ctlop;
1198 
1199     return 0;
1200 
1201 #if 0
1202     switch (ctlop)
1203     {
1204     case VFS_CTL_IS_NOTREADY:
1205         {
1206             vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1207             int v;
1208 
1209             if (file->linear == LS_NOT_LINEAR)
1210                 vfs_die ("You may not do this");
1211             if (file->linear == LS_LINEAR_CLOSED || file->linear == LS_LINEAR_PREOPEN)
1212                 return 0;
1213 
1214             v = vfs_s_select_on_two (VFS_FILE_HANDLER_SUPER (fh)->u.fish.sockr, 0);
1215 
1216             return (((v < 0) && (errno == EINTR)) || v == 0) ? 1 : 0;
1217         }
1218     default:
1219         return 0;
1220     }
1221 #endif
1222 }
1223 
1224 /* --------------------------------------------------------------------------------------------- */
1225 
1226 static int
1227 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1228 {
1229     const char *crpath1, *crpath2;
1230     char *rpath1, *rpath2;
1231     struct vfs_s_super *super, *super2;
1232     const vfs_path_element_t *path_element;
1233     int ret;
1234 
1235     path_element = vfs_path_get_by_index (vpath1, -1);
1236 
1237     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1238     if (crpath1 == NULL)
1239         return -1;
1240 
1241     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1242     if (crpath2 == NULL)
1243         return -1;
1244 
1245     rpath1 = strutils_shell_escape (crpath1);
1246     rpath2 = strutils_shell_escape (crpath2);
1247 
1248     ret =
1249         fish_send_command (path_element->class, super2, OPT_FLUSH, FISH_SUPER (super)->scr_mv,
1250                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1251 
1252     g_free (rpath1);
1253     g_free (rpath2);
1254 
1255     return ret;
1256 }
1257 
1258 /* --------------------------------------------------------------------------------------------- */
1259 
1260 static int
1261 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1262 {
1263     const char *crpath1, *crpath2;
1264     char *rpath1, *rpath2;
1265     struct vfs_s_super *super, *super2;
1266     const vfs_path_element_t *path_element;
1267     int ret;
1268 
1269     path_element = vfs_path_get_by_index (vpath1, -1);
1270 
1271     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1272     if (crpath1 == NULL)
1273         return -1;
1274 
1275     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1276     if (crpath2 == NULL)
1277         return -1;
1278 
1279     rpath1 = strutils_shell_escape (crpath1);
1280     rpath2 = strutils_shell_escape (crpath2);
1281 
1282     ret =
1283         fish_send_command (path_element->class, super2, OPT_FLUSH, FISH_SUPER (super)->scr_hardlink,
1284                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1285 
1286     g_free (rpath1);
1287     g_free (rpath2);
1288 
1289     return ret;
1290 }
1291 
1292 /* --------------------------------------------------------------------------------------------- */
1293 
1294 static int
1295 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1296 {
1297     char *qsetto;
1298     const char *crpath;
1299     char *rpath;
1300     struct vfs_s_super *super;
1301     const vfs_path_element_t *path_element;
1302     int ret;
1303 
1304     path_element = vfs_path_get_by_index (vpath2, -1);
1305 
1306     crpath = vfs_s_get_path (vpath2, &super, 0);
1307     if (crpath == NULL)
1308         return -1;
1309 
1310     rpath = strutils_shell_escape (crpath);
1311     qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1312 
1313     ret =
1314         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_ln,
1315                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", qsetto, rpath);
1316 
1317     g_free (qsetto);
1318     g_free (rpath);
1319 
1320     return ret;
1321 }
1322 
1323 /* --------------------------------------------------------------------------------------------- */
1324 
1325 static int
1326 fish_stat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1327 {
1328     int ret;
1329 
1330     ret = vfs_s_stat (vpath, buf);
1331     fish_set_blksize (buf);
1332     return ret;
1333 }
1334 
1335 /* --------------------------------------------------------------------------------------------- */
1336 
1337 static int
1338 fish_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1339 {
1340     int ret;
1341 
1342     ret = vfs_s_lstat (vpath, buf);
1343     fish_set_blksize (buf);
1344     return ret;
1345 }
1346 
1347 /* --------------------------------------------------------------------------------------------- */
1348 
1349 static int
1350 fish_fstat (void *vfs_info, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1351 {
1352     int ret;
1353 
1354     ret = vfs_s_fstat (vfs_info, buf);
1355     fish_set_blksize (buf);
1356     return ret;
1357 }
1358 
1359 /* --------------------------------------------------------------------------------------------- */
1360 
1361 static int
1362 fish_chmod (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1363 {
1364     const char *crpath;
1365     char *rpath;
1366     struct vfs_s_super *super;
1367     const vfs_path_element_t *path_element;
1368     int ret;
1369 
1370     path_element = vfs_path_get_by_index (vpath, -1);
1371 
1372     crpath = vfs_s_get_path (vpath, &super, 0);
1373     if (crpath == NULL)
1374         return -1;
1375 
1376     rpath = strutils_shell_escape (crpath);
1377 
1378     ret =
1379         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_chmod,
1380                            "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n", rpath,
1381                            (unsigned int) (mode & 07777));
1382 
1383     g_free (rpath);
1384 
1385     return ret;;
1386 }
1387 
1388 /* --------------------------------------------------------------------------------------------- */
1389 
1390 static int
1391 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
1392 {
1393     char *sowner, *sgroup;
1394     struct passwd *pw;
1395     struct group *gr;
1396     const char *crpath;
1397     char *rpath;
1398     struct vfs_s_super *super;
1399     const vfs_path_element_t *path_element;
1400     int ret;
1401 
1402     pw = getpwuid (owner);
1403     if (pw == NULL)
1404         return 0;
1405 
1406     gr = getgrgid (group);
1407     if (gr == NULL)
1408         return 0;
1409 
1410     sowner = pw->pw_name;
1411     sgroup = gr->gr_name;
1412 
1413     path_element = vfs_path_get_by_index (vpath, -1);
1414 
1415     crpath = vfs_s_get_path (vpath, &super, 0);
1416     if (crpath == NULL)
1417         return -1;
1418 
1419     rpath = strutils_shell_escape (crpath);
1420 
1421     /* FIXME: what should we report if chgrp succeeds but chown fails? */
1422     ret =
1423         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_chown,
1424                            "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n", rpath, sowner,
1425                            sgroup);
1426 
1427     g_free (rpath);
1428 
1429     return ret;
1430 }
1431 
1432 /* --------------------------------------------------------------------------------------------- */
1433 
1434 static void
1435 fish_get_atime (mc_timesbuf_t * times, time_t * sec, long *nsec)
     /* [previous][next][first][last][top][bottom][index][help]  */
1436 {
1437 #ifdef HAVE_UTIMENSAT
1438     *sec = (*times)[0].tv_sec;
1439     *nsec = (*times)[0].tv_nsec;
1440 #else
1441     *sec = times->actime;
1442     *nsec = 0;
1443 #endif
1444 }
1445 
1446 /* --------------------------------------------------------------------------------------------- */
1447 
1448 static void
1449 fish_get_mtime (mc_timesbuf_t * times, time_t * sec, long *nsec)
     /* [previous][next][first][last][top][bottom][index][help]  */
1450 {
1451 #ifdef HAVE_UTIMENSAT
1452     *sec = (*times)[1].tv_sec;
1453     *nsec = (*times)[1].tv_nsec;
1454 #else
1455     *sec = times->modtime;
1456     *nsec = 0;
1457 #endif
1458 }
1459 
1460 /* --------------------------------------------------------------------------------------------- */
1461 
1462 static int
1463 fish_utime (const vfs_path_t * vpath, mc_timesbuf_t * times)
     /* [previous][next][first][last][top][bottom][index][help]  */
1464 {
1465     char utcatime[16], utcmtime[16];
1466     char utcatime_w_nsec[30], utcmtime_w_nsec[30];
1467     time_t atime, mtime;
1468     long atime_nsec, mtime_nsec;
1469     struct tm *gmt;
1470     const char *crpath;
1471     char *rpath;
1472     struct vfs_s_super *super;
1473     const vfs_path_element_t *path_element;
1474     int ret;
1475 
1476     path_element = vfs_path_get_by_index (vpath, -1);
1477 
1478     crpath = vfs_s_get_path (vpath, &super, 0);
1479     if (crpath == NULL)
1480         return -1;
1481 
1482     rpath = strutils_shell_escape (crpath);
1483 
1484     fish_get_atime (times, &atime, &atime_nsec);
1485     gmt = gmtime (&atime);
1486     g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1487                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1488                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1489     g_snprintf (utcatime_w_nsec, sizeof (utcatime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1490                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1491                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, atime_nsec);
1492 
1493     fish_get_mtime (times, &mtime, &mtime_nsec);
1494     gmt = gmtime (&mtime);
1495     g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1496                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1497                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1498     g_snprintf (utcmtime_w_nsec, sizeof (utcmtime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1499                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1500                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, mtime_nsec);
1501 
1502     ret = fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_utime,
1503                              "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld "
1504                              "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s FISH_TOUCHATIME_W_NSEC=\"%s\" "
1505                              "FISH_TOUCHMTIME_W_NSEC=\"%s\";\n", rpath, (long) atime, (long) mtime,
1506                              utcatime, utcmtime, utcatime_w_nsec, utcmtime_w_nsec);
1507 
1508     g_free (rpath);
1509 
1510     return ret;
1511 }
1512 
1513 /* --------------------------------------------------------------------------------------------- */
1514 
1515 static int
1516 fish_unlink (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1517 {
1518     const char *crpath;
1519     char *rpath;
1520     struct vfs_s_super *super;
1521     const vfs_path_element_t *path_element;
1522     int ret;
1523 
1524     path_element = vfs_path_get_by_index (vpath, -1);
1525 
1526     crpath = vfs_s_get_path (vpath, &super, 0);
1527     if (crpath == NULL)
1528         return -1;
1529 
1530     rpath = strutils_shell_escape (crpath);
1531 
1532     ret =
1533         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_unlink,
1534                            "FISH_FILENAME=%s;\n", rpath);
1535 
1536     g_free (rpath);
1537 
1538     return ret;
1539 }
1540 
1541 /* --------------------------------------------------------------------------------------------- */
1542 
1543 static int
1544 fish_exists (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1545 {
1546     const char *crpath;
1547     char *rpath;
1548     struct vfs_s_super *super;
1549     const vfs_path_element_t *path_element;
1550     int ret;
1551 
1552     path_element = vfs_path_get_by_index (vpath, -1);
1553 
1554     crpath = vfs_s_get_path (vpath, &super, 0);
1555     if (crpath == NULL)
1556         return -1;
1557 
1558     rpath = strutils_shell_escape (crpath);
1559 
1560     ret =
1561         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_exists,
1562                            "FISH_FILENAME=%s;\n", rpath);
1563 
1564     g_free (rpath);
1565 
1566     return (ret == 0 ? 1 : 0);
1567 }
1568 
1569 /* --------------------------------------------------------------------------------------------- */
1570 
1571 static int
1572 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1573 {
1574     const char *crpath;
1575     char *rpath;
1576     struct vfs_s_super *super;
1577     const vfs_path_element_t *path_element;
1578     int ret;
1579 
1580     (void) mode;
1581 
1582     path_element = vfs_path_get_by_index (vpath, -1);
1583 
1584     crpath = vfs_s_get_path (vpath, &super, 0);
1585     if (crpath == NULL)
1586         return -1;
1587 
1588     rpath = strutils_shell_escape (crpath);
1589 
1590     ret =
1591         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_mkdir,
1592                            "FISH_FILENAME=%s;\n", rpath);
1593     g_free (rpath);
1594 
1595     if (ret != 0)
1596         return ret;
1597 
1598     if (fish_exists (vpath) == 0)
1599     {
1600         path_element->class->verrno = EACCES;
1601         return -1;
1602     }
1603     return 0;
1604 }
1605 
1606 /* --------------------------------------------------------------------------------------------- */
1607 
1608 static int
1609 fish_rmdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1610 {
1611     const char *crpath;
1612     char *rpath;
1613     struct vfs_s_super *super;
1614     const vfs_path_element_t *path_element;
1615     int ret;
1616 
1617     path_element = vfs_path_get_by_index (vpath, -1);
1618 
1619     crpath = vfs_s_get_path (vpath, &super, 0);
1620     if (crpath == NULL)
1621         return -1;
1622 
1623     rpath = strutils_shell_escape (crpath);
1624 
1625     ret =
1626         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_rmdir,
1627                            "FISH_FILENAME=%s;\n", rpath);
1628 
1629     g_free (rpath);
1630 
1631     return ret;
1632 }
1633 
1634 /* --------------------------------------------------------------------------------------------- */
1635 
1636 static vfs_file_handler_t *
1637 fish_fh_new (struct vfs_s_inode *ino, gboolean changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
1638 {
1639     fish_file_handler_t *fh;
1640 
1641     fh = g_new0 (fish_file_handler_t, 1);
1642     vfs_s_init_fh (VFS_FILE_HANDLER (fh), ino, changed);
1643 
1644     return VFS_FILE_HANDLER (fh);
1645 }
1646 
1647 /* --------------------------------------------------------------------------------------------- */
1648 
1649 static int
1650 fish_fh_open (struct vfs_class *me, vfs_file_handler_t * fh, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1651 {
1652     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1653 
1654     (void) mode;
1655 
1656     /* File will be written only, so no need to retrieve it */
1657     if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1658     {
1659         /* user pressed the button [ Append ] in the "Copy" dialog */
1660         if ((flags & O_APPEND) != 0)
1661             fish->append = TRUE;
1662 
1663         if (fh->ino->localname == NULL)
1664         {
1665             vfs_path_t *vpath;
1666             int tmp_handle;
1667 
1668             tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1669             if (tmp_handle == -1)
1670             {
1671                 vfs_path_free (vpath);
1672                 goto fail;
1673             }
1674             fh->ino->localname = g_strdup (vfs_path_as_str (vpath));
1675             vfs_path_free (vpath);
1676             close (tmp_handle);
1677         }
1678         return 0;
1679     }
1680     if (fh->ino->localname == NULL && vfs_s_retrieve_file (me, fh->ino) == -1)
1681         goto fail;
1682     if (fh->ino->localname == NULL)
1683         vfs_die ("retrieve_file failed to fill in localname");
1684     return 0;
1685 
1686   fail:
1687     return -1;
1688 }
1689 
1690 /* --------------------------------------------------------------------------------------------- */
1691 
1692 static void
1693 fish_fill_names (struct vfs_class *me, fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
1694 {
1695     GList *iter;
1696 
1697     for (iter = VFS_SUBCLASS (me)->supers; iter != NULL; iter = g_list_next (iter))
1698     {
1699         const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1700 
1701         char *name;
1702         char gbuf[10];
1703         const char *flags = "";
1704 
1705         switch (super->path_element->port)
1706         {
1707         case FISH_FLAG_RSH:
1708             flags = ":r";
1709             break;
1710         case FISH_FLAG_COMPRESSED:
1711             flags = ":C";
1712             break;
1713         default:
1714             if (super->path_element->port > FISH_FLAG_RSH)
1715             {
1716                 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1717                 flags = gbuf;
1718             }
1719             break;
1720         }
1721 
1722         name =
1723             g_strconcat (vfs_fish_ops->prefix, VFS_PATH_URL_DELIMITER,
1724                          super->path_element->user, "@", super->path_element->host, flags,
1725                          PATH_SEP_STR, super->path_element->path, (char *) NULL);
1726         func (name);
1727         g_free (name);
1728     }
1729 }
1730 
1731 /* --------------------------------------------------------------------------------------------- */
1732 
1733 static void *
1734 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1735 {
1736     /*
1737        sorry, i've places hack here
1738        cause fish don't able to open files with O_EXCL flag
1739      */
1740     flags &= ~O_EXCL;
1741     return vfs_s_open (vpath, flags, mode);
1742 }
1743 
1744 /* --------------------------------------------------------------------------------------------- */
1745 /*** public functions ****************************************************************************/
1746 /* --------------------------------------------------------------------------------------------- */
1747 
1748 void
1749 vfs_init_fish (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1750 {
1751     tcp_init ();
1752 
1753     vfs_init_subclass (&fish_subclass, "fish", VFS_REMOTE | VFS_USETMP, "sh");
1754     vfs_fish_ops->fill_names = fish_fill_names;
1755     vfs_fish_ops->stat = fish_stat;
1756     vfs_fish_ops->lstat = fish_lstat;
1757     vfs_fish_ops->fstat = fish_fstat;
1758     vfs_fish_ops->chmod = fish_chmod;
1759     vfs_fish_ops->chown = fish_chown;
1760     vfs_fish_ops->utime = fish_utime;
1761     vfs_fish_ops->open = fish_open;
1762     vfs_fish_ops->symlink = fish_symlink;
1763     vfs_fish_ops->link = fish_link;
1764     vfs_fish_ops->unlink = fish_unlink;
1765     vfs_fish_ops->rename = fish_rename;
1766     vfs_fish_ops->mkdir = fish_mkdir;
1767     vfs_fish_ops->rmdir = fish_rmdir;
1768     vfs_fish_ops->ctl = fish_ctl;
1769     fish_subclass.archive_same = fish_archive_same;
1770     fish_subclass.new_archive = fish_new_archive;
1771     fish_subclass.open_archive = fish_open_archive;
1772     fish_subclass.free_archive = fish_free_archive;
1773     fish_subclass.fh_new = fish_fh_new;
1774     fish_subclass.fh_open = fish_fh_open;
1775     fish_subclass.dir_load = fish_dir_load;
1776     fish_subclass.file_store = fish_file_store;
1777     fish_subclass.linear_start = fish_linear_start;
1778     fish_subclass.linear_read = fish_linear_read;
1779     fish_subclass.linear_close = fish_linear_close;
1780     vfs_register_class (vfs_fish_ops);
1781 }
1782 
1783 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */