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-2020
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Pavel Machek, 1998
  10    Michal Svec, 2000
  11    Andrew Borodin <aborodin@vmail.ru>, 2010-2019
  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 <stdlib.h>
  58 #include <string.h>
  59 #include <inttypes.h>           /* uintmax_t */
  60 
  61 #include "lib/global.h"
  62 #include "lib/tty/tty.h"        /* enable/disable interrupt key */
  63 #include "lib/strescape.h"
  64 #include "lib/unixcompat.h"
  65 #include "lib/fileloc.h"
  66 #include "lib/util.h"           /* my_exit() */
  67 #include "lib/mcconfig.h"
  68 #include "lib/timer.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     dir->timestamp = mc_timer_elapsed (mc_global.timer) + fish_directory_timeout * G_USEC_PER_SEC;
 770 
 771     quoted_path = strutils_shell_escape (remote_path);
 772     (void) fish_command_v (me, super, NONE, FISH_SUPER (super)->scr_ls, "FISH_FILENAME=%s;\n",
 773                            quoted_path);
 774     g_free (quoted_path);
 775 
 776     ent = vfs_s_generate_entry (me, NULL, dir, 0);
 777 
 778     while (TRUE)
 779     {
 780         int res;
 781 
 782         res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), FISH_SUPER (super)->sockr);
 783 
 784         if ((res == 0) || (res == EINTR))
 785         {
 786             vfs_s_free_entry (me, ent);
 787             me->verrno = ECONNRESET;
 788             goto error;
 789         }
 790         if (me->logfile != NULL)
 791         {
 792             fputs (buffer, me->logfile);
 793             fputs ("\n", me->logfile);
 794             fflush (me->logfile);
 795         }
 796         if (strncmp (buffer, "### ", 4) == 0)
 797             break;
 798         if (buffer[0] == '\0')
 799         {
 800             if (ent->name != NULL)
 801             {
 802                 vfs_s_insert_entry (me, dir, ent);
 803                 ent = vfs_s_generate_entry (me, NULL, dir, 0);
 804             }
 805             continue;
 806         }
 807 
 808 #define ST ent->ino->st
 809 
 810         switch (buffer[0])
 811         {
 812         case ':':
 813             {
 814                 char *temp;
 815                 char *data_start = buffer + 1;
 816                 char *filename = data_start;
 817                 char *filename_bound;
 818 
 819                 filename_bound = filename + strlen (filename);
 820 
 821                 if (strcmp (data_start, "\".\"") == 0 || strcmp (data_start, "\"..\"") == 0)
 822                     break;      /* We'll do "." and ".." ourselves */
 823 
 824                 if (S_ISLNK (ST.st_mode))
 825                 {
 826                     char *linkname;
 827                     char *linkname_bound;
 828                     /* we expect: "escaped-name" -> "escaped-name"
 829                        //     -> cannot occur in filenames,
 830                        //     because it will be escaped to -\> */
 831 
 832 
 833                     linkname_bound = filename_bound;
 834 
 835                     if (*filename == '"')
 836                         ++filename;
 837 
 838                     linkname = strstr (filename, "\" -> \"");
 839                     if (linkname == NULL)
 840                     {
 841                         /* broken client, or smth goes wrong */
 842                         linkname = filename_bound;
 843                         if (filename_bound > filename && *(filename_bound - 1) == '"')
 844                             --filename_bound;   /* skip trailing " */
 845                     }
 846                     else
 847                     {
 848                         filename_bound = linkname;
 849                         linkname += 6;  /* strlen ("\" -> \"") */
 850                         if (*(linkname_bound - 1) == '"')
 851                             --linkname_bound;   /* skip trailing " */
 852                     }
 853 
 854                     ent->name = g_strndup (filename, filename_bound - filename);
 855                     temp = ent->name;
 856                     ent->name = strutils_shell_unescape (ent->name);
 857                     g_free (temp);
 858 
 859                     ent->ino->linkname = g_strndup (linkname, linkname_bound - linkname);
 860                     temp = ent->ino->linkname;
 861                     ent->ino->linkname = strutils_shell_unescape (ent->ino->linkname);
 862                     g_free (temp);
 863                 }
 864                 else
 865                 {
 866                     /* we expect: "escaped-name" */
 867                     if (filename_bound - filename > 2)
 868                     {
 869                         /*
 870                            there is at least 2 "
 871                            and we skip them
 872                          */
 873                         if (*filename == '"')
 874                             ++filename;
 875                         if (*(filename_bound - 1) == '"')
 876                             --filename_bound;
 877                     }
 878                     ent->name = g_strndup (filename, filename_bound - filename);
 879                     temp = ent->name;
 880                     ent->name = strutils_shell_unescape (ent->name);
 881                     g_free (temp);
 882                 }
 883                 break;
 884             }
 885         case 'S':
 886             ST.st_size = (off_t) g_ascii_strtoll (buffer + 1, NULL, 10);
 887             break;
 888         case 'P':
 889             {
 890                 size_t skipped;
 891 
 892                 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
 893                 break;
 894             }
 895         case 'R':
 896             {
 897                 /*
 898                    raw filemode:
 899                    we expect: Roctal-filemode octal-filetype uid.gid
 900                  */
 901                 size_t skipped;
 902 
 903                 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
 904                 break;
 905             }
 906         case 'd':
 907             {
 908                 vfs_split_text (buffer + 1);
 909                 if (vfs_parse_filedate (0, &ST.st_ctime) == 0)
 910                     break;
 911                 ST.st_atime = ST.st_mtime = ST.st_ctime;
 912 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 913                 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
 914 #endif
 915             }
 916             break;
 917         case 'D':
 918             {
 919                 struct tm tim;
 920 
 921                 /* cppcheck-suppress invalidscanf */
 922                 if (sscanf (buffer + 1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
 923                             &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
 924                     break;
 925                 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime (&tim);
 926 #ifdef HAVE_STRUCT_STAT_ST_MTIM
 927                 ST.st_atim.tv_nsec = ST.st_mtim.tv_nsec = ST.st_ctim.tv_nsec = 0;
 928 #endif
 929             }
 930             break;
 931         case 'E':
 932             {
 933                 int maj, min;
 934 
 935                 /* cppcheck-suppress invalidscanf */
 936                 if (sscanf (buffer + 1, "%d,%d", &maj, &min) != 2)
 937                     break;
 938 #ifdef HAVE_STRUCT_STAT_ST_RDEV
 939                 ST.st_rdev = makedev (maj, min);
 940 #endif
 941             }
 942         default:
 943             break;
 944         }
 945     }
 946 
 947     vfs_s_free_entry (me, ent);
 948     reply_code = fish_decode_reply (buffer + 4, 0);
 949     if (reply_code == COMPLETE)
 950     {
 951         vfs_print_message (_("%s: done."), me->name);
 952         return 0;
 953     }
 954 
 955     me->verrno = reply_code == ERROR ? EACCES : E_REMOTE;
 956 
 957   error:
 958     vfs_print_message (_("%s: failure"), me->name);
 959     return -1;
 960 }
 961 
 962 /* --------------------------------------------------------------------------------------------- */
 963 
 964 static int
 965 fish_file_store (struct vfs_class *me, vfs_file_handler_t * fh, char *name, char *localname)
     /* [previous][next][first][last][top][bottom][index][help]  */
 966 {
 967     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
 968     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
 969     fish_super_t *fish_super = FISH_SUPER (super);
 970     int code;
 971     off_t total = 0;
 972     char buffer[BUF_8K];
 973     struct stat s;
 974     int h;
 975     char *quoted_name;
 976 
 977     h = open (localname, O_RDONLY);
 978     if (h == -1)
 979         ERRNOR (EIO, -1);
 980     if (fstat (h, &s) < 0)
 981     {
 982         close (h);
 983         ERRNOR (EIO, -1);
 984     }
 985 
 986     /* First, try this as stor:
 987      *
 988      *     ( head -c number ) | ( cat > file; cat >/dev/null )
 989      *
 990      *  If 'head' is not present on the remote system, 'dd' will be used.
 991      * Unfortunately, we cannot trust most non-GNU 'head' implementations
 992      * even if '-c' options is supported. Therefore, we separate GNU head
 993      * (and other modern heads?) using '-q' and '-' . This causes another
 994      * implementations to fail (because of "incorrect options").
 995      *
 996      *  Fallback is:
 997      *
 998      *     rest=<number>
 999      *     while [ $rest -gt 0 ]
1000      *     do
1001      *        cnt=`expr \( $rest + 255 \) / 256`
1002      *        n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
1003      *        rest=`expr $rest - $n`
1004      *     done
1005      *
1006      *  'dd' was not designed for full filling of input buffers,
1007      *  and does not report exact number of bytes (not blocks).
1008      *  Therefore a more complex shell script is needed.
1009      *
1010      *   On some systems non-GNU head writes "Usage:" error report to stdout
1011      *  instead of stderr. It makes impossible the use of "head || dd"
1012      *  algorithm for file appending case, therefore just "dd" is used for it.
1013      */
1014 
1015     quoted_name = strutils_shell_escape (name);
1016     vfs_print_message (_("fish: store %s: sending command..."), quoted_name);
1017 
1018     /* FIXME: File size is limited to ULONG_MAX */
1019     code =
1020         fish_command_v (me, super, WAIT_REPLY,
1021                         fish->append ? fish_super->scr_append : fish_super->scr_send,
1022                         "FISH_FILENAME=%s FISH_FILESIZE=%" PRIuMAX ";\n", quoted_name,
1023                         (uintmax_t) s.st_size);
1024     g_free (quoted_name);
1025 
1026     if (code != PRELIM)
1027     {
1028         close (h);
1029         ERRNOR (E_REMOTE, -1);
1030     }
1031 
1032     while (TRUE)
1033     {
1034         ssize_t n, t;
1035 
1036         while ((n = read (h, buffer, sizeof (buffer))) < 0)
1037         {
1038             if ((errno == EINTR) && tty_got_interrupt ())
1039                 continue;
1040             vfs_print_message ("%s", _("fish: Local read failed, sending zeros"));
1041             close (h);
1042             h = open ("/dev/zero", O_RDONLY);
1043         }
1044 
1045         if (n == 0)
1046             break;
1047 
1048         t = write (fish_super->sockw, buffer, n);
1049         if (t != n)
1050         {
1051             if (t == -1)
1052                 me->verrno = errno;
1053             else
1054                 me->verrno = EIO;
1055             goto error_return;
1056         }
1057         tty_disable_interrupt_key ();
1058         total += n;
1059         vfs_print_message ("%s: %" PRIuMAX "/%" PRIuMAX, _("fish: storing file"),
1060                            (uintmax_t) total, (uintmax_t) s.st_size);
1061     }
1062     close (h);
1063 
1064     if (fish_get_reply (me, fish_super->sockr, NULL, 0) != COMPLETE)
1065         ERRNOR (E_REMOTE, -1);
1066     return 0;
1067 
1068   error_return:
1069     close (h);
1070     fish_get_reply (me, fish_super->sockr, NULL, 0);
1071     return -1;
1072 }
1073 
1074 /* --------------------------------------------------------------------------------------------- */
1075 
1076 static int
1077 fish_linear_start (struct vfs_class *me, vfs_file_handler_t * fh, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
1078 {
1079     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1080     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1081     char *name;
1082     char *quoted_name;
1083 
1084     name = vfs_s_fullpath (me, fh->ino);
1085     if (name == NULL)
1086         return 0;
1087     quoted_name = strutils_shell_escape (name);
1088     g_free (name);
1089     fish->append = FALSE;
1090 
1091     /*
1092      * Check whether the remote file is readable by using 'dd' to copy 
1093      * a single byte from the remote file to /dev/null. If 'dd' completes
1094      * with exit status of 0 use 'cat' to send the file contents to the
1095      * standard output (i.e. over the network).
1096      */
1097 
1098     offset =
1099         fish_command_v (me, super, WANT_STRING, FISH_SUPER (super)->scr_get,
1100                         "FISH_FILENAME=%s FISH_START_OFFSET=%" PRIuMAX ";\n", quoted_name,
1101                         (uintmax_t) offset);
1102     g_free (quoted_name);
1103 
1104     if (offset != PRELIM)
1105         ERRNOR (E_REMOTE, 0);
1106     fh->linear = LS_LINEAR_OPEN;
1107     fish->got = 0;
1108     errno = 0;
1109 #if SIZEOF_OFF_T == SIZEOF_LONG
1110     fish->total = (off_t) strtol (reply_str, NULL, 10);
1111 #else
1112     fish->total = (off_t) g_ascii_strtoll (reply_str, NULL, 10);
1113 #endif
1114     if (errno != 0)
1115         ERRNOR (E_REMOTE, 0);
1116     return 1;
1117 }
1118 
1119 /* --------------------------------------------------------------------------------------------- */
1120 
1121 static void
1122 fish_linear_abort (struct vfs_class *me, vfs_file_handler_t * fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1123 {
1124     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1125     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1126     char buffer[BUF_8K];
1127     ssize_t n;
1128 
1129     vfs_print_message ("%s", _("Aborting transfer..."));
1130 
1131     do
1132     {
1133         n = MIN ((off_t) sizeof (buffer), (fish->total - fish->got));
1134         if (n != 0)
1135         {
1136             n = read (FISH_SUPER (super)->sockr, buffer, n);
1137             if (n < 0)
1138                 return;
1139             fish->got += n;
1140         }
1141     }
1142     while (n != 0);
1143 
1144     if (fish_get_reply (me, FISH_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1145         vfs_print_message ("%s", _("Error reported after abort."));
1146     else
1147         vfs_print_message ("%s", _("Aborted transfer would be successful."));
1148 }
1149 
1150 /* --------------------------------------------------------------------------------------------- */
1151 
1152 static ssize_t
1153 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]  */
1154 {
1155     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1156     struct vfs_s_super *super = VFS_FILE_HANDLER_SUPER (fh);
1157     ssize_t n = 0;
1158 
1159     len = MIN ((size_t) (fish->total - fish->got), len);
1160     tty_disable_interrupt_key ();
1161     while (len != 0 && ((n = read (FISH_SUPER (super)->sockr, buf, len)) < 0))
1162     {
1163         if ((errno == EINTR) && !tty_got_interrupt ())
1164             continue;
1165         break;
1166     }
1167     tty_enable_interrupt_key ();
1168 
1169     if (n > 0)
1170         fish->got += n;
1171     else if (n < 0)
1172         fish_linear_abort (me, fh);
1173     else if (fish_get_reply (me, FISH_SUPER (super)->sockr, NULL, 0) != COMPLETE)
1174         ERRNOR (E_REMOTE, -1);
1175     ERRNOR (errno, n);
1176 }
1177 
1178 /* --------------------------------------------------------------------------------------------- */
1179 
1180 static void
1181 fish_linear_close (struct vfs_class *me, vfs_file_handler_t * fh)
     /* [previous][next][first][last][top][bottom][index][help]  */
1182 {
1183     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1184 
1185     if (fish->total != fish->got)
1186         fish_linear_abort (me, fh);
1187 }
1188 
1189 /* --------------------------------------------------------------------------------------------- */
1190 
1191 static int
1192 fish_ctl (void *fh, int ctlop, void *arg)
     /* [previous][next][first][last][top][bottom][index][help]  */
1193 {
1194     (void) arg;
1195     (void) fh;
1196     (void) ctlop;
1197 
1198     return 0;
1199 
1200 #if 0
1201     switch (ctlop)
1202     {
1203     case VFS_CTL_IS_NOTREADY:
1204         {
1205             vfs_file_handler_t *file = VFS_FILE_HANDLER (fh);
1206             int v;
1207 
1208             if (file->linear == LS_NOT_LINEAR)
1209                 vfs_die ("You may not do this");
1210             if (file->linear == LS_LINEAR_CLOSED || file->linear == LS_LINEAR_PREOPEN)
1211                 return 0;
1212 
1213             v = vfs_s_select_on_two (VFS_FILE_HANDLER_SUPER (fh)->u.fish.sockr, 0);
1214 
1215             return (((v < 0) && (errno == EINTR)) || v == 0) ? 1 : 0;
1216         }
1217     default:
1218         return 0;
1219     }
1220 #endif
1221 }
1222 
1223 /* --------------------------------------------------------------------------------------------- */
1224 
1225 static int
1226 fish_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1227 {
1228     const char *crpath1, *crpath2;
1229     char *rpath1, *rpath2;
1230     struct vfs_s_super *super, *super2;
1231     const vfs_path_element_t *path_element;
1232     int ret;
1233 
1234     path_element = vfs_path_get_by_index (vpath1, -1);
1235 
1236     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1237     if (crpath1 == NULL)
1238         return -1;
1239 
1240     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1241     if (crpath2 == NULL)
1242         return -1;
1243 
1244     rpath1 = strutils_shell_escape (crpath1);
1245     rpath2 = strutils_shell_escape (crpath2);
1246 
1247     ret =
1248         fish_send_command (path_element->class, super2, OPT_FLUSH, FISH_SUPER (super)->scr_mv,
1249                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1250 
1251     g_free (rpath1);
1252     g_free (rpath2);
1253 
1254     return ret;
1255 }
1256 
1257 /* --------------------------------------------------------------------------------------------- */
1258 
1259 static int
1260 fish_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1261 {
1262     const char *crpath1, *crpath2;
1263     char *rpath1, *rpath2;
1264     struct vfs_s_super *super, *super2;
1265     const vfs_path_element_t *path_element;
1266     int ret;
1267 
1268     path_element = vfs_path_get_by_index (vpath1, -1);
1269 
1270     crpath1 = vfs_s_get_path (vpath1, &super, 0);
1271     if (crpath1 == NULL)
1272         return -1;
1273 
1274     crpath2 = vfs_s_get_path (vpath2, &super2, 0);
1275     if (crpath2 == NULL)
1276         return -1;
1277 
1278     rpath1 = strutils_shell_escape (crpath1);
1279     rpath2 = strutils_shell_escape (crpath2);
1280 
1281     ret =
1282         fish_send_command (path_element->class, super2, OPT_FLUSH, FISH_SUPER (super)->scr_hardlink,
1283                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", rpath1, rpath2);
1284 
1285     g_free (rpath1);
1286     g_free (rpath2);
1287 
1288     return ret;
1289 }
1290 
1291 /* --------------------------------------------------------------------------------------------- */
1292 
1293 static int
1294 fish_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1295 {
1296     char *qsetto;
1297     const char *crpath;
1298     char *rpath;
1299     struct vfs_s_super *super;
1300     const vfs_path_element_t *path_element;
1301     int ret;
1302 
1303     path_element = vfs_path_get_by_index (vpath2, -1);
1304 
1305     crpath = vfs_s_get_path (vpath2, &super, 0);
1306     if (crpath == NULL)
1307         return -1;
1308 
1309     rpath = strutils_shell_escape (crpath);
1310     qsetto = strutils_shell_escape (vfs_path_get_by_index (vpath1, -1)->path);
1311 
1312     ret =
1313         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_ln,
1314                            "FISH_FILEFROM=%s FISH_FILETO=%s;\n", qsetto, rpath);
1315 
1316     g_free (qsetto);
1317     g_free (rpath);
1318 
1319     return ret;
1320 }
1321 
1322 /* --------------------------------------------------------------------------------------------- */
1323 
1324 static int
1325 fish_stat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1326 {
1327     int ret;
1328 
1329     ret = vfs_s_stat (vpath, buf);
1330     fish_set_blksize (buf);
1331     return ret;
1332 }
1333 
1334 /* --------------------------------------------------------------------------------------------- */
1335 
1336 static int
1337 fish_lstat (const vfs_path_t * vpath, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1338 {
1339     int ret;
1340 
1341     ret = vfs_s_lstat (vpath, buf);
1342     fish_set_blksize (buf);
1343     return ret;
1344 }
1345 
1346 /* --------------------------------------------------------------------------------------------- */
1347 
1348 static int
1349 fish_fstat (void *vfs_info, struct stat *buf)
     /* [previous][next][first][last][top][bottom][index][help]  */
1350 {
1351     int ret;
1352 
1353     ret = vfs_s_fstat (vfs_info, buf);
1354     fish_set_blksize (buf);
1355     return ret;
1356 }
1357 
1358 /* --------------------------------------------------------------------------------------------- */
1359 
1360 static int
1361 fish_chmod (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1362 {
1363     const char *crpath;
1364     char *rpath;
1365     struct vfs_s_super *super;
1366     const vfs_path_element_t *path_element;
1367     int ret;
1368 
1369     path_element = vfs_path_get_by_index (vpath, -1);
1370 
1371     crpath = vfs_s_get_path (vpath, &super, 0);
1372     if (crpath == NULL)
1373         return -1;
1374 
1375     rpath = strutils_shell_escape (crpath);
1376 
1377     ret =
1378         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_chmod,
1379                            "FISH_FILENAME=%s FISH_FILEMODE=%4.4o;\n", rpath,
1380                            (unsigned int) (mode & 07777));
1381 
1382     g_free (rpath);
1383 
1384     return ret;;
1385 }
1386 
1387 /* --------------------------------------------------------------------------------------------- */
1388 
1389 static int
1390 fish_chown (const vfs_path_t * vpath, uid_t owner, gid_t group)
     /* [previous][next][first][last][top][bottom][index][help]  */
1391 {
1392     char *sowner, *sgroup;
1393     struct passwd *pw;
1394     struct group *gr;
1395     const char *crpath;
1396     char *rpath;
1397     struct vfs_s_super *super;
1398     const vfs_path_element_t *path_element;
1399     int ret;
1400 
1401     pw = getpwuid (owner);
1402     if (pw == NULL)
1403         return 0;
1404 
1405     gr = getgrgid (group);
1406     if (gr == NULL)
1407         return 0;
1408 
1409     sowner = pw->pw_name;
1410     sgroup = gr->gr_name;
1411 
1412     path_element = vfs_path_get_by_index (vpath, -1);
1413 
1414     crpath = vfs_s_get_path (vpath, &super, 0);
1415     if (crpath == NULL)
1416         return -1;
1417 
1418     rpath = strutils_shell_escape (crpath);
1419 
1420     /* FIXME: what should we report if chgrp succeeds but chown fails? */
1421     ret =
1422         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_chown,
1423                            "FISH_FILENAME=%s FISH_FILEOWNER=%s FISH_FILEGROUP=%s;\n", rpath, sowner,
1424                            sgroup);
1425 
1426     g_free (rpath);
1427 
1428     return ret;
1429 }
1430 
1431 /* --------------------------------------------------------------------------------------------- */
1432 
1433 static void
1434 fish_get_atime (mc_timesbuf_t * times, time_t * sec, long *nsec)
     /* [previous][next][first][last][top][bottom][index][help]  */
1435 {
1436 #ifdef HAVE_UTIMENSAT
1437     *sec = (*times)[0].tv_sec;
1438     *nsec = (*times)[0].tv_nsec;
1439 #else
1440     *sec = times->actime;
1441     *nsec = 0;
1442 #endif
1443 }
1444 
1445 /* --------------------------------------------------------------------------------------------- */
1446 
1447 static void
1448 fish_get_mtime (mc_timesbuf_t * times, time_t * sec, long *nsec)
     /* [previous][next][first][last][top][bottom][index][help]  */
1449 {
1450 #ifdef HAVE_UTIMENSAT
1451     *sec = (*times)[1].tv_sec;
1452     *nsec = (*times)[1].tv_nsec;
1453 #else
1454     *sec = times->modtime;
1455     *nsec = 0;
1456 #endif
1457 }
1458 
1459 /* --------------------------------------------------------------------------------------------- */
1460 
1461 static int
1462 fish_utime (const vfs_path_t * vpath, mc_timesbuf_t * times)
     /* [previous][next][first][last][top][bottom][index][help]  */
1463 {
1464     char utcatime[16], utcmtime[16];
1465     char utcatime_w_nsec[30], utcmtime_w_nsec[30];
1466     time_t atime, mtime;
1467     long atime_nsec, mtime_nsec;
1468     struct tm *gmt;
1469     const char *crpath;
1470     char *rpath;
1471     struct vfs_s_super *super;
1472     const vfs_path_element_t *path_element;
1473     int ret;
1474 
1475     path_element = vfs_path_get_by_index (vpath, -1);
1476 
1477     crpath = vfs_s_get_path (vpath, &super, 0);
1478     if (crpath == NULL)
1479         return -1;
1480 
1481     rpath = strutils_shell_escape (crpath);
1482 
1483     fish_get_atime (times, &atime, &atime_nsec);
1484     gmt = gmtime (&atime);
1485     g_snprintf (utcatime, sizeof (utcatime), "%04d%02d%02d%02d%02d.%02d",
1486                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1487                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1488     g_snprintf (utcatime_w_nsec, sizeof (utcatime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1489                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1490                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, atime_nsec);
1491 
1492     fish_get_mtime (times, &mtime, &mtime_nsec);
1493     gmt = gmtime (&mtime);
1494     g_snprintf (utcmtime, sizeof (utcmtime), "%04d%02d%02d%02d%02d.%02d",
1495                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1496                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec);
1497     g_snprintf (utcmtime_w_nsec, sizeof (utcmtime_w_nsec), "%04d-%02d-%02d %02d:%02d:%02d.%09ld",
1498                 gmt->tm_year + 1900, gmt->tm_mon + 1, gmt->tm_mday,
1499                 gmt->tm_hour, gmt->tm_min, gmt->tm_sec, mtime_nsec);
1500 
1501     ret = fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_utime,
1502                              "FISH_FILENAME=%s FISH_FILEATIME=%ld FISH_FILEMTIME=%ld "
1503                              "FISH_TOUCHATIME=%s FISH_TOUCHMTIME=%s FISH_TOUCHATIME_W_NSEC=\"%s\" "
1504                              "FISH_TOUCHMTIME_W_NSEC=\"%s\";\n", rpath, (long) atime, (long) mtime,
1505                              utcatime, utcmtime, utcatime_w_nsec, utcmtime_w_nsec);
1506 
1507     g_free (rpath);
1508 
1509     return ret;
1510 }
1511 
1512 /* --------------------------------------------------------------------------------------------- */
1513 
1514 static int
1515 fish_unlink (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1516 {
1517     const char *crpath;
1518     char *rpath;
1519     struct vfs_s_super *super;
1520     const vfs_path_element_t *path_element;
1521     int ret;
1522 
1523     path_element = vfs_path_get_by_index (vpath, -1);
1524 
1525     crpath = vfs_s_get_path (vpath, &super, 0);
1526     if (crpath == NULL)
1527         return -1;
1528 
1529     rpath = strutils_shell_escape (crpath);
1530 
1531     ret =
1532         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_unlink,
1533                            "FISH_FILENAME=%s;\n", rpath);
1534 
1535     g_free (rpath);
1536 
1537     return ret;
1538 }
1539 
1540 /* --------------------------------------------------------------------------------------------- */
1541 
1542 static int
1543 fish_exists (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1544 {
1545     const char *crpath;
1546     char *rpath;
1547     struct vfs_s_super *super;
1548     const vfs_path_element_t *path_element;
1549     int ret;
1550 
1551     path_element = vfs_path_get_by_index (vpath, -1);
1552 
1553     crpath = vfs_s_get_path (vpath, &super, 0);
1554     if (crpath == NULL)
1555         return -1;
1556 
1557     rpath = strutils_shell_escape (crpath);
1558 
1559     ret =
1560         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_exists,
1561                            "FISH_FILENAME=%s;\n", rpath);
1562 
1563     g_free (rpath);
1564 
1565     return (ret == 0 ? 1 : 0);
1566 }
1567 
1568 /* --------------------------------------------------------------------------------------------- */
1569 
1570 static int
1571 fish_mkdir (const vfs_path_t * vpath, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1572 {
1573     const char *crpath;
1574     char *rpath;
1575     struct vfs_s_super *super;
1576     const vfs_path_element_t *path_element;
1577     int ret;
1578 
1579     (void) mode;
1580 
1581     path_element = vfs_path_get_by_index (vpath, -1);
1582 
1583     crpath = vfs_s_get_path (vpath, &super, 0);
1584     if (crpath == NULL)
1585         return -1;
1586 
1587     rpath = strutils_shell_escape (crpath);
1588 
1589     ret =
1590         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_mkdir,
1591                            "FISH_FILENAME=%s;\n", rpath);
1592     g_free (rpath);
1593 
1594     if (ret != 0)
1595         return ret;
1596 
1597     if (fish_exists (vpath) == 0)
1598     {
1599         path_element->class->verrno = EACCES;
1600         return -1;
1601     }
1602     return 0;
1603 }
1604 
1605 /* --------------------------------------------------------------------------------------------- */
1606 
1607 static int
1608 fish_rmdir (const vfs_path_t * vpath)
     /* [previous][next][first][last][top][bottom][index][help]  */
1609 {
1610     const char *crpath;
1611     char *rpath;
1612     struct vfs_s_super *super;
1613     const vfs_path_element_t *path_element;
1614     int ret;
1615 
1616     path_element = vfs_path_get_by_index (vpath, -1);
1617 
1618     crpath = vfs_s_get_path (vpath, &super, 0);
1619     if (crpath == NULL)
1620         return -1;
1621 
1622     rpath = strutils_shell_escape (crpath);
1623 
1624     ret =
1625         fish_send_command (path_element->class, super, OPT_FLUSH, FISH_SUPER (super)->scr_rmdir,
1626                            "FISH_FILENAME=%s;\n", rpath);
1627 
1628     g_free (rpath);
1629 
1630     return ret;
1631 }
1632 
1633 /* --------------------------------------------------------------------------------------------- */
1634 
1635 static vfs_file_handler_t *
1636 fish_fh_new (struct vfs_s_inode *ino, gboolean changed)
     /* [previous][next][first][last][top][bottom][index][help]  */
1637 {
1638     fish_file_handler_t *fh;
1639 
1640     fh = g_new0 (fish_file_handler_t, 1);
1641     vfs_s_init_fh (VFS_FILE_HANDLER (fh), ino, changed);
1642 
1643     return VFS_FILE_HANDLER (fh);
1644 }
1645 
1646 /* --------------------------------------------------------------------------------------------- */
1647 
1648 static int
1649 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]  */
1650 {
1651     fish_file_handler_t *fish = FISH_FILE_HANDLER (fh);
1652 
1653     (void) mode;
1654 
1655     /* File will be written only, so no need to retrieve it */
1656     if (((flags & O_WRONLY) == O_WRONLY) && ((flags & (O_RDONLY | O_RDWR)) == 0))
1657     {
1658         /* user pressed the button [ Append ] in the "Copy" dialog */
1659         if ((flags & O_APPEND) != 0)
1660             fish->append = TRUE;
1661 
1662         if (fh->ino->localname == NULL)
1663         {
1664             vfs_path_t *vpath;
1665             int tmp_handle;
1666 
1667             tmp_handle = vfs_mkstemps (&vpath, me->name, fh->ino->ent->name);
1668             if (tmp_handle == -1)
1669             {
1670                 vfs_path_free (vpath);
1671                 goto fail;
1672             }
1673             fh->ino->localname = g_strdup (vfs_path_as_str (vpath));
1674             vfs_path_free (vpath);
1675             close (tmp_handle);
1676         }
1677         return 0;
1678     }
1679     if (fh->ino->localname == NULL && vfs_s_retrieve_file (me, fh->ino) == -1)
1680         goto fail;
1681     if (fh->ino->localname == NULL)
1682         vfs_die ("retrieve_file failed to fill in localname");
1683     return 0;
1684 
1685   fail:
1686     return -1;
1687 }
1688 
1689 /* --------------------------------------------------------------------------------------------- */
1690 
1691 static void
1692 fish_fill_names (struct vfs_class *me, fill_names_f func)
     /* [previous][next][first][last][top][bottom][index][help]  */
1693 {
1694     GList *iter;
1695 
1696     for (iter = VFS_SUBCLASS (me)->supers; iter != NULL; iter = g_list_next (iter))
1697     {
1698         const struct vfs_s_super *super = (const struct vfs_s_super *) iter->data;
1699 
1700         char *name;
1701         char gbuf[10];
1702         const char *flags = "";
1703 
1704         switch (super->path_element->port)
1705         {
1706         case FISH_FLAG_RSH:
1707             flags = ":r";
1708             break;
1709         case FISH_FLAG_COMPRESSED:
1710             flags = ":C";
1711             break;
1712         default:
1713             if (super->path_element->port > FISH_FLAG_RSH)
1714             {
1715                 g_snprintf (gbuf, sizeof (gbuf), ":%d", super->path_element->port);
1716                 flags = gbuf;
1717             }
1718             break;
1719         }
1720 
1721         name =
1722             g_strconcat (vfs_fish_ops->prefix, VFS_PATH_URL_DELIMITER,
1723                          super->path_element->user, "@", super->path_element->host, flags,
1724                          PATH_SEP_STR, super->path_element->path, (char *) NULL);
1725         func (name);
1726         g_free (name);
1727     }
1728 }
1729 
1730 /* --------------------------------------------------------------------------------------------- */
1731 
1732 static void *
1733 fish_open (const vfs_path_t * vpath, int flags, mode_t mode)
     /* [previous][next][first][last][top][bottom][index][help]  */
1734 {
1735     /*
1736        sorry, i've places hack here
1737        cause fish don't able to open files with O_EXCL flag
1738      */
1739     flags &= ~O_EXCL;
1740     return vfs_s_open (vpath, flags, mode);
1741 }
1742 
1743 /* --------------------------------------------------------------------------------------------- */
1744 /*** public functions ****************************************************************************/
1745 /* --------------------------------------------------------------------------------------------- */
1746 
1747 void
1748 vfs_init_fish (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1749 {
1750     tcp_init ();
1751 
1752     vfs_init_subclass (&fish_subclass, "fish", VFSF_REMOTE | VFSF_USETMP, "sh");
1753     vfs_fish_ops->fill_names = fish_fill_names;
1754     vfs_fish_ops->stat = fish_stat;
1755     vfs_fish_ops->lstat = fish_lstat;
1756     vfs_fish_ops->fstat = fish_fstat;
1757     vfs_fish_ops->chmod = fish_chmod;
1758     vfs_fish_ops->chown = fish_chown;
1759     vfs_fish_ops->utime = fish_utime;
1760     vfs_fish_ops->open = fish_open;
1761     vfs_fish_ops->symlink = fish_symlink;
1762     vfs_fish_ops->link = fish_link;
1763     vfs_fish_ops->unlink = fish_unlink;
1764     vfs_fish_ops->rename = fish_rename;
1765     vfs_fish_ops->mkdir = fish_mkdir;
1766     vfs_fish_ops->rmdir = fish_rmdir;
1767     vfs_fish_ops->ctl = fish_ctl;
1768     fish_subclass.archive_same = fish_archive_same;
1769     fish_subclass.new_archive = fish_new_archive;
1770     fish_subclass.open_archive = fish_open_archive;
1771     fish_subclass.free_archive = fish_free_archive;
1772     fish_subclass.fh_new = fish_fh_new;
1773     fish_subclass.fh_open = fish_fh_open;
1774     fish_subclass.dir_load = fish_dir_load;
1775     fish_subclass.file_store = fish_file_store;
1776     fish_subclass.linear_start = fish_linear_start;
1777     fish_subclass.linear_read = fish_linear_read;
1778     fish_subclass.linear_close = fish_linear_close;
1779     vfs_register_class (vfs_fish_ops);
1780 }
1781 
1782 /* --------------------------------------------------------------------------------------------- */

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