replaced calls to g_strdup() by mhl_str_dup()
[midnight-commander.git] / vfs / fish.c
blobe94cb5ae677fdb1ec59dddf8f9bad672478d8548
1 /* Virtual File System: FISH implementation for transfering files over
2 shell connections.
4 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
5 2007 Free Software Foundation, Inc.
7 Written by: 1998 Pavel Machek
8 Spaces fix: 2000 Michal Svec
10 Derived from ftpfs.c.
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU Library General Public License
14 as published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Library General Public License for more details.
22 You should have received a copy of the GNU Library General Public
23 License along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 * Read README.fish for protocol specification.
29 * Syntax of path is: /#sh:user@host[:Cr]/path
30 * where C means you want compressed connection,
31 * and r means you want to use rsh
33 * Namespace: fish_vfs_ops exported.
36 /* Define this if your ssh can take -I option */
38 #include <config.h>
40 #include <errno.h>
42 #include <mhl/string.h>
44 #include "../src/global.h"
45 #include "../src/tty.h" /* enable/disable interrupt key */
46 #include "../src/wtools.h" /* message() */
47 #include "../src/main.h" /* print_vfs_message */
48 #include "utilvfs.h"
49 #include "xdirentry.h"
50 #include "vfs.h"
51 #include "vfs-impl.h"
52 #include "gc.h" /* vfs_stamp_create */
53 #include "tcputil.h"
54 #include "../src/unixcompat.h"
55 #include "fish.h"
56 #include "../mhl/memory.h"
57 #include "../mhl/string.h"
58 #include "../mhl/escape.h"
60 int fish_directory_timeout = 900;
62 #define DO_RESOLVE_SYMLINK 1
63 #define DO_OPEN 2
64 #define DO_FREE_RESOURCE 4
66 #define FISH_FLAG_COMPRESSED 1
67 #define FISH_FLAG_RSH 2
69 #define OPT_FLUSH 1
70 #define OPT_IGNORE_ERROR 2
73 * Reply codes.
75 #define PRELIM 1 /* positive preliminary */
76 #define COMPLETE 2 /* positive completion */
77 #define CONTINUE 3 /* positive intermediate */
78 #define TRANSIENT 4 /* transient negative completion */
79 #define ERROR 5 /* permanent negative completion */
81 /* command wait_flag: */
82 #define NONE 0x00
83 #define WAIT_REPLY 0x01
84 #define WANT_STRING 0x02
85 static char reply_str [80];
87 static struct vfs_class vfs_fish_ops;
89 static int
90 fish_command (struct vfs_class *me, struct vfs_s_super *super,
91 int wait_reply, const char *fmt, ...)
92 __attribute__ ((format (__printf__, 4, 5)));
94 static int fish_decode_reply (char *s, int was_garbage)
96 int code;
97 if (!sscanf(s, "%d", &code)) {
98 code = 500;
99 return 5;
101 if (code<100) return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
102 return code / 100;
105 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
106 static int fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
108 char answer[1024];
109 int was_garbage = 0;
111 for (;;) {
112 if (!vfs_s_get_line(me, sock, answer, sizeof(answer), '\n')) {
113 if (string_buf)
114 *string_buf = 0;
115 return 4;
118 if (strncmp(answer, "### ", 4)) {
119 was_garbage = 1;
120 if (string_buf)
121 g_strlcpy(string_buf, answer, string_len);
122 } else return fish_decode_reply(answer+4, was_garbage);
126 #define SUP super->u.fish
128 static int
129 fish_command (struct vfs_class *me, struct vfs_s_super *super,
130 int wait_reply, const char *fmt, ...)
132 va_list ap;
133 char *str;
134 int status;
135 FILE *logfile = MEDATA->logfile;
137 va_start (ap, fmt);
139 str = g_strdup_vprintf (fmt, ap);
140 va_end (ap);
142 if (logfile) {
143 fwrite (str, strlen (str), 1, logfile);
144 fflush (logfile);
147 enable_interrupt_key ();
149 status = write (SUP.sockw, str, strlen (str));
150 g_free (str);
152 disable_interrupt_key ();
153 if (status < 0)
154 return TRANSIENT;
156 if (wait_reply)
157 return fish_get_reply (me, SUP.sockr,
158 (wait_reply & WANT_STRING) ? reply_str :
159 NULL, sizeof (reply_str) - 1);
160 return COMPLETE;
163 static void
164 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
166 if ((SUP.sockw != -1) || (SUP.sockr != -1)) {
167 print_vfs_message (_("fish: Disconnecting from %s"),
168 super->name ? super->name : "???");
169 fish_command (me, super, NONE, "#BYE\nexit\n");
170 close (SUP.sockw);
171 close (SUP.sockr);
172 SUP.sockw = SUP.sockr = -1;
174 g_free (SUP.host);
175 g_free (SUP.user);
176 g_free (SUP.cwdir);
177 g_free (SUP.password);
180 static void
181 fish_pipeopen(struct vfs_s_super *super, const char *path, const char *argv[])
183 int fileset1[2], fileset2[2];
184 int res;
186 if ((pipe(fileset1)<0) || (pipe(fileset2)<0))
187 vfs_die("Cannot pipe(): %m.");
189 if ((res = fork())) {
190 if (res<0) vfs_die("Cannot fork(): %m.");
191 /* We are the parent */
192 close(fileset1[0]);
193 SUP.sockw = fileset1[1];
194 close(fileset2[1]);
195 SUP.sockr = fileset2[0];
196 } else {
197 close(0);
198 dup(fileset1[0]);
199 close(fileset1[0]); close(fileset1[1]);
200 close(1); close(2);
201 dup(fileset2[1]);
202 /* stderr to /dev/null */
203 open ("/dev/null", O_WRONLY);
204 close(fileset2[0]); close(fileset2[1]);
205 execvp(path, const_cast(char **, argv));
206 _exit(3);
210 /* The returned directory should always contain a trailing slash */
211 static char *fish_getcwd(struct vfs_class *me, struct vfs_s_super *super)
213 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
214 return g_strconcat (reply_str, "/", (char *) NULL);
215 ERRNOR (EIO, NULL);
218 static int
219 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
222 const char *argv[10];
223 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
224 int i = 0;
226 argv[i++] = xsh;
227 if (SUP.flags == FISH_FLAG_COMPRESSED)
228 argv[i++] = "-C";
229 argv[i++] = "-l";
230 argv[i++] = SUP.user;
231 argv[i++] = SUP.host;
232 argv[i++] = "echo FISH:; /bin/sh";
233 argv[i++] = NULL;
235 fish_pipeopen (super, xsh, argv);
238 char answer[2048];
239 print_vfs_message (_("fish: Waiting for initial line..."));
240 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
241 ERRNOR (E_PROTO, -1);
242 print_vfs_message ("%s", answer);
243 if (strstr (answer, "assword")) {
245 /* Currently, this does not work. ssh reads passwords from
246 /dev/tty, not from stdin :-(. */
248 message (D_ERROR, MSG_ERROR,
250 ("Sorry, we cannot do password authenticated connections for now."));
251 ERRNOR (EPERM, -1);
252 if (!SUP.password) {
253 char *p, *op;
254 p = g_strconcat (_(" fish: Password required for "),
255 SUP.user, " ", (char *) NULL);
256 op = vfs_get_password (p);
257 g_free (p);
258 if (op == NULL)
259 ERRNOR (EPERM, -1);
260 SUP.password = op;
262 print_vfs_message (_("fish: Sending password..."));
263 write (SUP.sockw, SUP.password, strlen (SUP.password));
264 write (SUP.sockw, "\n", 1);
268 print_vfs_message (_("fish: Sending initial line..."));
270 * Run `start_fish_server'. If it doesn't exist - no problem,
271 * we'll talk directly to the shell.
273 if (fish_command
274 (me, super, WAIT_REPLY,
275 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
276 COMPLETE)
277 ERRNOR (E_PROTO, -1);
279 print_vfs_message (_("fish: Handshaking version..."));
280 if (fish_command
281 (me, super, WAIT_REPLY,
282 "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
283 ERRNOR (E_PROTO, -1);
285 /* Set up remote locale to C, otherwise dates cannot be recognized */
286 if (fish_command
287 (me, super, WAIT_REPLY,
288 "LANG=C; LC_ALL=C; LC_TIME=C\n"
289 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
290 != COMPLETE)
291 ERRNOR (E_PROTO, -1);
293 print_vfs_message (_("fish: Setting up current directory..."));
294 SUP.cwdir = fish_getcwd (me, super);
295 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
296 #if 0
297 super->name =
298 g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
299 #endif
300 super->name = mhl_str_dup (PATH_SEP_STR);
302 super->root =
303 vfs_s_new_inode (me, super,
304 vfs_s_default_stat (me, S_IFDIR | 0755));
305 return 0;
308 static int
309 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
310 const char *archive_name, char *op)
312 char *host, *user, *password, *p;
313 int flags;
315 (void) archive_name;
317 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
318 &password, 0, URL_NOSLASH);
320 g_free (p);
322 SUP.host = host;
323 SUP.user = user;
324 SUP.flags = flags;
325 if (!strncmp (op, "rsh:", 4))
326 SUP.flags |= FISH_FLAG_RSH;
327 SUP.cwdir = NULL;
328 if (password)
329 SUP.password = password;
330 return fish_open_archive_int (me, super);
333 static int
334 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
335 const char *archive_name, char *op, void *cookie)
337 char *host, *user;
338 int flags;
340 (void) me;
341 (void) archive_name;
342 (void) cookie;
344 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
345 URL_NOSLASH);
347 g_free (op);
349 flags = ((strcmp (host, SUP.host) == 0)
350 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
351 g_free (host);
352 g_free (user);
354 return flags;
357 static int
358 fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
360 struct vfs_s_super *super = dir->super;
361 char buffer[8192];
362 struct vfs_s_entry *ent = NULL;
363 FILE *logfile;
364 char *quoted_path;
365 int reply_code;
367 #if 0
369 * Simple FISH debug interface :]
371 if (!(MEDATA->logfile))
373 MEDATA->logfile = fopen("/tmp/mc-FISH.sh", "w");
375 #endif // 0
377 logfile = MEDATA->logfile;
379 print_vfs_message(_("fish: Reading directory %s..."), remote_path);
381 gettimeofday(&dir->timestamp, NULL);
382 dir->timestamp.tv_sec += fish_directory_timeout;
383 quoted_path = mhl_shell_escape_dup (remote_path);
384 fish_command (me, super, NONE,
385 "#LIST /%s\n"
386 "if `perl -v > /dev/null 2>&1` ; then\n"
387 "perl -e '\n"
388 "use strict;\n"
389 "use POSIX;\n"
390 "use Fcntl;\n"
391 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
392 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
393 "my $dirname = $ARGV[0];\n"
394 "if (opendir ( DIR, $dirname )) {\n"
395 "while( (my $filename = readdir(DIR))){\n"
396 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
397 "my $mloctime= scalar localtime $mtime;\n"
398 "my $shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
399 "my $e_filename = $filename;\n"
400 "$e_filename =~ $shell_escape_regex;\n"
401 "if (S_ISLNK($mode) ) {\n"
402 "my $linkname = readlink (\"$dirname/$filename\");\n"
403 "$linkname =~ $shell_escape_regex;\n"
404 "\n"
405 "printf(\"R%%o %%o $uid.$gid\\n"
406 "S$size\\n"
407 "d$mloctime\\n"
408 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
409 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
410 "} else {\n"
411 "printf(\"R%%o %%o $uid.$gid\\n"
412 "S$size\\n"
413 "d$mloctime\\n"
414 ":\\\"$e_filename\\\"\\n"
415 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
416 "}}\n"
417 "printf(\"### 200\\n\");\n"
418 "closedir(DIR);\n"
419 "} else {\n"
420 "printf(\"### 500\\n\");\n"
421 "}\n"
422 "exit 0\n"
423 "' /%s ||\n" /* ARGV[0] - path to browse */
424 " echo '### 500'\n" /* do not hang if perl is to eval it */
425 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
426 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
427 "LSOPT=\"-Qlan\";\n"
428 "ADD=0;\n"
429 "else\n"
430 "LSOPT=\"-lan\";\n"
431 "ADD=1;\n"
432 "fi\n"
433 "ls $LSOPT \"/%s\" 2>/dev/null | grep '^[^cbt]' | (\n"
434 "while read p l u g s m d y n; do\n"
435 "if [ $ADD = 0 ]; then\n"
436 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
437 "elif `sed --version >/dev/null 2>&1` ; then\n"
438 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
439 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
440 "else\n"
441 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
442 "fi\n"
443 "done )\n"
444 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
445 "while read p l u g a i m d y n; do\n"
446 "if [ $ADD = 0 ]; then\n"
447 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
448 "elif `sed --version >/dev/null 2>&1` ; then\n"
449 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
450 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
451 "else\n"
452 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
453 "fi\n"
454 "done)\n"
455 "echo '### 200'\n"
456 "else\n"
457 "echo '### 500'\n"
458 "fi\n",
459 quoted_path, quoted_path, quoted_path, quoted_path, quoted_path, quoted_path);
460 mhl_mem_free (quoted_path);
461 ent = vfs_s_generate_entry(me, NULL, dir, 0);
462 while (1) {
463 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
464 if ((!res) || (res == EINTR)) {
465 vfs_s_free_entry(me, ent);
466 me->verrno = ECONNRESET;
467 goto error;
469 if (logfile) {
470 fputs (buffer, logfile);
471 fputs ("\n", logfile);
472 fflush (logfile);
474 if (!strncmp(buffer, "### ", 4))
475 break;
476 if ((!buffer[0])) {
477 if (ent->name) {
478 vfs_s_insert_entry(me, dir, ent);
479 ent = vfs_s_generate_entry(me, NULL, dir, 0);
481 continue;
484 #define ST ent->ino->st
486 switch(buffer[0]) {
487 case ':': {
488 char *data_start = buffer+1;
489 char *filename = data_start;
490 char *linkname = data_start;
491 char *filename_bound = filename + strlen(filename);
492 char *linkname_bound = filename_bound;
493 if (!strcmp(data_start, "\".\"") || !strcmp(data_start, "\"..\""))
494 break; /* We'll do "." and ".." ourselves */
496 if (S_ISLNK(ST.st_mode)) {
497 // we expect: "escaped-name" -> "escaped-name"
498 // -> cannot occur in filenames,
499 // because it will be escaped to -\>
501 if (*filename == '"')
502 ++filename;
504 linkname = strstr(filename, "\" -> \"");
505 if (!linkname)
507 /* broken client, or smth goes wrong */
508 linkname = filename_bound;
509 if (filename_bound > filename
510 && *(filename_bound - 1) == '"')
511 --filename_bound; // skip trailing "
513 else
515 filename_bound = linkname;
516 linkname += 6; // strlen ("\" -> \"")
517 if (*(linkname_bound - 1) == '"')
518 --linkname_bound; // skip trailing "
521 ent->name = mhl_str_dup_range(filename, filename_bound);
522 mhl_shell_unescape_buf(ent->name);
524 ent->ino->linkname = mhl_str_dup_range(linkname, linkname_bound);
525 mhl_shell_unescape_buf(ent->ino->linkname);
526 } else {
527 // we expect: "escaped-name"
528 if (filename_bound - filename > 2)
530 // there is at least 2 "
531 // and we skip them
532 if (*filename == '"')
533 ++filename;
534 if (*(filename_bound - 1) == '"')
535 --filename_bound;
538 ent->name = mhl_str_dup_range(filename, filename_bound);
539 mhl_shell_unescape_buf(ent->name);
541 break;
543 case 'S':
544 #ifdef HAVE_ATOLL
545 ST.st_size = (off_t) atoll (buffer+1);
546 #else
547 ST.st_size = (off_t) atof (buffer+1);
548 #endif
549 break;
550 case 'P': {
551 size_t skipped;
552 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
553 break;
555 case 'R': {
556 // raw filemode:
557 // we expect: Roctal-filemode octal-filetype uid.gid
558 size_t skipped;
559 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
560 break;
562 case 'd': {
563 vfs_split_text(buffer+1);
564 if (!vfs_parse_filedate(0, &ST.st_ctime))
565 break;
566 ST.st_atime = ST.st_mtime = ST.st_ctime;
568 break;
569 case 'D': {
570 struct tm tim;
571 if (sscanf(buffer+1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
572 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
573 break;
574 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime(&tim);
576 break;
577 case 'E': {
578 int maj, min;
579 if (sscanf(buffer+1, "%d,%d", &maj, &min) != 2)
580 break;
581 #ifdef HAVE_STRUCT_STAT_ST_RDEV
582 ST.st_rdev = makedev (maj, min);
583 #endif
588 vfs_s_free_entry (me, ent);
589 reply_code = fish_decode_reply(buffer + 4, 0);
590 if (reply_code == COMPLETE) {
591 g_free (SUP.cwdir);
592 SUP.cwdir = mhl_str_dup (remote_path);
593 print_vfs_message (_("%s: done."), me->name);
594 return 0;
595 } else if (reply_code == ERROR) {
596 me->verrno = EACCES;
597 } else {
598 me->verrno = E_REMOTE;
601 error:
602 print_vfs_message (_("%s: failure"), me->name);
603 return -1;
606 static int
607 fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
609 struct vfs_s_super *super = FH_SUPER;
610 int n, total;
611 char buffer[8192];
612 struct stat s;
613 int was_error = 0;
614 int h;
615 char *quoted_name;
617 h = open (localname, O_RDONLY);
619 if (h == -1)
620 ERRNOR (EIO, -1);
621 if (fstat(h, &s)<0) {
622 close (h);
623 ERRNOR (EIO, -1);
626 /* First, try this as stor:
628 * ( head -c number ) | ( cat > file; cat >/dev/null )
630 * If `head' is not present on the remote system, `dd' will be used.
631 * Unfortunately, we cannot trust most non-GNU `head' implementations
632 * even if `-c' options is supported. Therefore, we separate GNU head
633 * (and other modern heads?) using `-q' and `-' . This causes another
634 * implementations to fail (because of "incorrect options").
636 * Fallback is:
638 * rest=<number>
639 * while [ $rest -gt 0 ]
640 * do
641 * cnt=`expr \( $rest + 255 \) / 256`
642 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
643 * rest=`expr $rest - $n`
644 * done
646 * `dd' was not designed for full filling of input buffers,
647 * and does not report exact number of bytes (not blocks).
648 * Therefore a more complex shell script is needed.
650 * On some systems non-GNU head writes "Usage:" error report to stdout
651 * instead of stderr. It makes impossible the use of "head || dd"
652 * algorithm for file appending case, therefore just "dd" is used for it.
655 quoted_name = mhl_shell_escape_dup(name);
656 print_vfs_message(_("fish: store %s: sending command..."), quoted_name );
658 /* FIXME: File size is limited to ULONG_MAX */
659 if (!fh->u.fish.append)
660 n = fish_command (me, super, WAIT_REPLY,
661 "#STOR %lu /%s\n"
662 "echo '### 001'\n"
663 "file=/%s\n"
664 "res=`exec 3>&1\n"
665 "(\n"
666 "head -c %lu -q - || echo DD >&3\n"
667 ") 2>/dev/null | (\n"
668 "cat > $file\n"
669 "cat > /dev/null\n"
670 ")`; [ \"$res\" = DD ] && {\n"
671 "> \"$file\"\n"
672 "rest=%lu\n"
673 "while [ $rest -gt 0 ]\n"
674 "do\n"
675 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
676 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
677 " rest=`expr $rest - $n`\n"
678 "done\n"
679 "}; echo '### 200'\n",
680 (unsigned long) s.st_size, quoted_name,
681 quoted_name, (unsigned long) s.st_size,
682 (unsigned long) s.st_size);
683 else
684 n = fish_command (me, super, WAIT_REPLY,
685 "#STOR %lu /%s\n"
686 "echo '### 001'\n"
687 "{\n"
688 "file=/%s\n"
689 "rest=%lu\n"
690 "while [ $rest -gt 0 ]\n"
691 "do\n"
692 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
693 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
694 " rest=`expr $rest - $n`\n"
695 "done\n"
696 "}; echo '### 200'\n",
697 (unsigned long) s.st_size, quoted_name,
698 quoted_name, (unsigned long) s.st_size);
700 if (n != PRELIM) {
701 close (h);
702 ERRNOR(E_REMOTE, -1);
704 total = 0;
706 while (1) {
707 int t;
708 while ((n = read(h, buffer, sizeof(buffer))) < 0) {
709 if ((errno == EINTR) && got_interrupt())
710 continue;
711 print_vfs_message(_("fish: Local read failed, sending zeros") );
712 close(h);
713 h = open( "/dev/zero", O_RDONLY );
715 if (n == 0)
716 break;
717 if ((t = write (SUP.sockw, buffer, n)) != n) {
718 if (t == -1) {
719 me->verrno = errno;
720 } else {
721 me->verrno = EIO;
723 goto error_return;
725 disable_interrupt_key();
726 total += n;
727 print_vfs_message(_("fish: storing %s %d (%lu)"),
728 was_error ? _("zeros") : _("file"), total,
729 (unsigned long) s.st_size);
731 close(h);
732 mhl_mem_free(quoted_name);
733 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
734 ERRNOR (E_REMOTE, -1);
735 return 0;
736 error_return:
737 close(h);
738 fish_get_reply(me, SUP.sockr, NULL, 0);
739 mhl_mem_free(quoted_name);
740 return -1;
743 static int
744 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
746 char *name;
747 char *quoted_name;
748 if (offset)
749 ERRNOR (E_NOTSUPP, 0);
750 name = vfs_s_fullpath (me, fh->ino);
751 if (!name)
752 return 0;
753 quoted_name = mhl_shell_escape_dup(name);
754 fh->u.fish.append = 0;
757 * Check whether the remote file is readable by using `dd' to copy
758 * a single byte from the remote file to /dev/null. If `dd' completes
759 * with exit status of 0 use `cat' to send the file contents to the
760 * standard output (i.e. over the network).
762 offset = fish_command (me, FH_SUPER, WANT_STRING,
763 "#RETR /%s\n"
764 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
765 "then\n"
766 "ls -ln /%s 2>/dev/null | (\n"
767 "read p l u g s r\n"
768 "echo $s\n"
769 ")\n"
770 "echo '### 100'\n"
771 "cat /%s\n"
772 "echo '### 200'\n"
773 "else\n"
774 "echo '### 500'\n"
775 "fi\n",
776 quoted_name, quoted_name, quoted_name, quoted_name );
777 g_free (quoted_name);
778 if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
779 fh->linear = LS_LINEAR_OPEN;
780 fh->u.fish.got = 0;
781 errno = 0;
782 #if SIZEOF_OFF_T == SIZEOF_LONG
783 fh->u.fish.total = strtol (reply_str, NULL, 10);
784 #else
785 fh->u.fish.total = strtoll (reply_str, NULL, 10);
786 #endif
787 if (errno != 0)
788 ERRNOR (E_REMOTE, 0);
789 return 1;
792 static void
793 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
795 struct vfs_s_super *super = FH_SUPER;
796 char buffer[8192];
797 int n;
799 print_vfs_message( _("Aborting transfer...") );
800 do {
801 n = MIN(8192, fh->u.fish.total - fh->u.fish.got);
802 if (n) {
803 if ((n = read(SUP.sockr, buffer, n)) < 0)
804 return;
805 fh->u.fish.got += n;
807 } while (n);
809 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
810 print_vfs_message( _("Error reported after abort.") );
811 else
812 print_vfs_message( _("Aborted transfer would be successful.") );
815 static int
816 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
818 struct vfs_s_super *super = FH_SUPER;
819 int n = 0;
820 len = MIN( fh->u.fish.total - fh->u.fish.got, len );
821 disable_interrupt_key();
822 while (len && ((n = read (SUP.sockr, buf, len))<0)) {
823 if ((errno == EINTR) && !got_interrupt())
824 continue;
825 break;
827 enable_interrupt_key();
829 if (n>0) fh->u.fish.got += n;
830 if (n<0) fish_linear_abort(me, fh);
831 if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
832 ERRNOR (E_REMOTE, -1);
833 ERRNOR (errno, n);
836 static void
837 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
839 if (fh->u.fish.total != fh->u.fish.got)
840 fish_linear_abort(me, fh);
843 static int
844 fish_ctl (void *fh, int ctlop, void *arg)
846 (void) arg;
847 return 0;
848 #if 0
849 switch (ctlop) {
850 case VFS_CTL_IS_NOTREADY:
852 int v;
854 if (!FH->linear)
855 vfs_die ("You may not do this");
856 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
857 return 0;
859 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
860 if (((v < 0) && (errno == EINTR)) || v == 0)
861 return 1;
862 return 0;
864 default:
865 return 0;
867 #endif
870 static int
871 fish_send_command(struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
873 int r;
875 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
876 vfs_stamp_create (&vfs_fish_ops, super);
877 if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
878 if (flags & OPT_FLUSH)
879 vfs_s_invalidate(me, super);
880 return 0;
883 #define PREFIX \
884 char buf[BUF_LARGE]; \
885 const char *crpath; \
886 char *rpath, *mpath = mhl_str_dup (path); \
887 struct vfs_s_super *super; \
888 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
889 g_free (mpath); \
890 return -1; \
892 rpath = mhl_shell_escape_dup(crpath); \
893 g_free (mpath);
895 #define POSTFIX(flags) \
896 g_free (rpath); \
897 return fish_send_command(me, super, buf, flags);
899 static int
900 fish_chmod (struct vfs_class *me, const char *path, int mode)
902 PREFIX
903 g_snprintf(buf, sizeof(buf), "#CHMOD %4.4o /%s\n"
904 "chmod %4.4o /%s 2>/dev/null\n"
905 "echo '### 000'\n",
906 mode & 07777, rpath,
907 mode & 07777, rpath);
908 POSTFIX(OPT_FLUSH);
911 #define FISH_OP(name, string) \
912 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
914 char buf[BUF_LARGE]; \
915 const char *crpath1, *crpath2; \
916 char *rpath1, *rpath2, *mpath1, *mpath2; \
917 struct vfs_s_super *super1, *super2; \
918 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = mhl_str_dup(path1), &super1, 0))) { \
919 g_free (mpath1); \
920 return -1; \
922 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = mhl_str_dup(path2), &super2, 0))) { \
923 g_free (mpath1); \
924 g_free (mpath2); \
925 return -1; \
927 rpath1 = mhl_shell_escape_dup (crpath1); \
928 g_free (mpath1); \
929 rpath2 = mhl_shell_escape_dup (crpath2); \
930 g_free (mpath2); \
931 g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
932 mhl_mem_free (rpath1); \
933 mhl_mem_free (rpath2); \
934 return fish_send_command(me, super2, buf, OPT_FLUSH); \
937 FISH_OP(rename, "#RENAME /%s /%s\n"
938 "mv /%s /%s 2>/dev/null\n"
939 "echo '### 000'" )
940 FISH_OP(link, "#LINK /%s /%s\n"
941 "ln /%s /%s 2>/dev/null\n"
942 "echo '### 000'" )
944 static int fish_symlink (struct vfs_class *me, const char *setto, const char *path)
946 char *qsetto;
947 PREFIX
948 qsetto = mhl_shell_escape_dup (setto);
949 g_snprintf(buf, sizeof(buf),
950 "#SYMLINK %s /%s\n"
951 "ln -s %s /%s 2>/dev/null\n"
952 "echo '### 000'\n",
953 qsetto, rpath, qsetto, rpath);
954 mhl_mem_free (qsetto);
955 POSTFIX(OPT_FLUSH);
958 static int
959 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
961 char *sowner, *sgroup;
962 struct passwd *pw;
963 struct group *gr;
965 if ((pw = getpwuid (owner)) == NULL)
966 return 0;
968 if ((gr = getgrgid (group)) == NULL)
969 return 0;
971 sowner = pw->pw_name;
972 sgroup = gr->gr_name;
974 PREFIX
975 g_snprintf (buf, sizeof(buf),
976 "#CHOWN %s /%s\n"
977 "chown %s /%s 2>/dev/null\n"
978 "echo '### 000'\n",
979 sowner, rpath,
980 sowner, rpath);
981 fish_send_command (me, super, buf, OPT_FLUSH);
982 /* FIXME: what should we report if chgrp succeeds but chown fails? */
983 g_snprintf (buf, sizeof(buf),
984 "#CHGRP /%s \"/%s\"\n"
985 "chgrp %s \"/%s\" 2>/dev/null\n"
986 "echo '### 000'\n",
987 sgroup, rpath,
988 sgroup, rpath);
989 /* fish_send_command(me, super, buf, OPT_FLUSH); */
990 POSTFIX (OPT_FLUSH)
994 static int fish_unlink (struct vfs_class *me, const char *path)
996 PREFIX
997 g_snprintf(buf, sizeof(buf),
998 "#DELE /%s\n"
999 "rm -f /%s 2>/dev/null\n"
1000 "echo '### 000'\n",
1001 rpath, rpath);
1002 POSTFIX(OPT_FLUSH);
1005 static int fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1007 PREFIX
1009 (void) mode;
1011 g_snprintf(buf, sizeof(buf),
1012 "#MKD /%s\n"
1013 "mkdir /%s 2>/dev/null\n"
1014 "echo '### 000'\n",
1015 rpath, rpath);
1016 POSTFIX(OPT_FLUSH);
1019 static int fish_rmdir (struct vfs_class *me, const char *path)
1021 PREFIX
1022 g_snprintf(buf, sizeof(buf),
1023 "#RMD /%s\n"
1024 "rmdir /%s 2>/dev/null\n"
1025 "echo '### 000'\n",
1026 rpath, rpath);
1027 POSTFIX(OPT_FLUSH);
1030 static int
1031 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags,
1032 int mode)
1034 (void) mode;
1036 fh->u.fish.append = 0;
1037 /* File will be written only, so no need to retrieve it */
1038 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR))) {
1039 fh->u.fish.append = flags & O_APPEND;
1040 if (!fh->ino->localname) {
1041 int tmp_handle =
1042 vfs_mkstemps (&fh->ino->localname, me->name,
1043 fh->ino->ent->name);
1044 if (tmp_handle == -1)
1045 return -1;
1046 close (tmp_handle);
1048 return 0;
1050 if (!fh->ino->localname)
1051 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1052 return -1;
1053 if (!fh->ino->localname)
1054 vfs_die ("retrieve_file failed to fill in localname");
1055 return 0;
1058 static void
1059 fish_fill_names (struct vfs_class *me, fill_names_f func)
1061 struct vfs_s_super *super = MEDATA->supers;
1062 const char *flags;
1063 char *name;
1065 while (super){
1066 switch (SUP.flags & (FISH_FLAG_RSH | FISH_FLAG_COMPRESSED)) {
1067 case FISH_FLAG_RSH:
1068 flags = ":r";
1069 break;
1070 case FISH_FLAG_COMPRESSED:
1071 flags = ":C";
1072 break;
1073 case FISH_FLAG_RSH | FISH_FLAG_COMPRESSED:
1074 flags = "";
1075 break;
1076 default:
1077 flags = "";
1078 break;
1081 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
1082 "/", SUP.cwdir, (char *) NULL);
1083 (*func)(name);
1084 g_free (name);
1085 super = super->next;
1089 void
1090 init_fish (void)
1092 static struct vfs_s_subclass fish_subclass;
1094 fish_subclass.flags = VFS_S_REMOTE;
1095 fish_subclass.archive_same = fish_archive_same;
1096 fish_subclass.open_archive = fish_open_archive;
1097 fish_subclass.free_archive = fish_free_archive;
1098 fish_subclass.fh_open = fish_fh_open;
1099 fish_subclass.dir_load = fish_dir_load;
1100 fish_subclass.file_store = fish_file_store;
1101 fish_subclass.linear_start = fish_linear_start;
1102 fish_subclass.linear_read = fish_linear_read;
1103 fish_subclass.linear_close = fish_linear_close;
1105 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1106 vfs_fish_ops.name = "fish";
1107 vfs_fish_ops.prefix = "sh:";
1108 vfs_fish_ops.fill_names = fish_fill_names;
1109 vfs_fish_ops.chmod = fish_chmod;
1110 vfs_fish_ops.chown = fish_chown;
1111 vfs_fish_ops.symlink = fish_symlink;
1112 vfs_fish_ops.link = fish_link;
1113 vfs_fish_ops.unlink = fish_unlink;
1114 vfs_fish_ops.rename = fish_rename;
1115 vfs_fish_ops.mkdir = fish_mkdir;
1116 vfs_fish_ops.rmdir = fish_rmdir;
1117 vfs_fish_ops.ctl = fish_ctl;
1118 vfs_register_class (&vfs_fish_ops);