Use FALSE/TRUE in favour of false/true (reverts 4fcf4e9685d9b2e4b49bfa3f7d3709dbe48073b6)
[midnight-commander.git] / vfs / fish.c
blobcd607fb0f70a51dae6adc48e2829f26aeb9869ac
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>
39 #include <errno.h>
41 #include "../src/global.h"
42 #include "../src/tty.h" /* enable/disable interrupt key */
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
45 #include "utilvfs.h"
46 #include "xdirentry.h"
47 #include "vfs.h"
48 #include "vfs-impl.h"
49 #include "gc.h" /* vfs_stamp_create */
50 #include "tcputil.h"
51 #include "../src/unixcompat.h"
52 #include "fish.h"
53 #include "../mhl/memory.h"
54 #include "../mhl/string.h"
55 #include "../mhl/escape.h"
57 int fish_directory_timeout = 900;
59 #define DO_RESOLVE_SYMLINK 1
60 #define DO_OPEN 2
61 #define DO_FREE_RESOURCE 4
63 #define FISH_FLAG_COMPRESSED 1
64 #define FISH_FLAG_RSH 2
66 #define OPT_FLUSH 1
67 #define OPT_IGNORE_ERROR 2
70 * Reply codes.
72 #define PRELIM 1 /* positive preliminary */
73 #define COMPLETE 2 /* positive completion */
74 #define CONTINUE 3 /* positive intermediate */
75 #define TRANSIENT 4 /* transient negative completion */
76 #define ERROR 5 /* permanent negative completion */
78 /* command wait_flag: */
79 #define NONE 0x00
80 #define WAIT_REPLY 0x01
81 #define WANT_STRING 0x02
82 static char reply_str [80];
84 static struct vfs_class vfs_fish_ops;
86 static int
87 fish_command (struct vfs_class *me, struct vfs_s_super *super,
88 int wait_reply, const char *fmt, ...)
89 __attribute__ ((format (__printf__, 4, 5)));
91 static int fish_decode_reply (char *s, int was_garbage)
93 int code;
94 if (!sscanf(s, "%d", &code)) {
95 code = 500;
96 return 5;
98 if (code<100) return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
99 return code / 100;
102 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
103 static int fish_get_reply (struct vfs_class *me, int sock, char *string_buf, int string_len)
105 char answer[1024];
106 int was_garbage = 0;
108 for (;;) {
109 if (!vfs_s_get_line(me, sock, answer, sizeof(answer), '\n')) {
110 if (string_buf)
111 *string_buf = 0;
112 return 4;
115 if (strncmp(answer, "### ", 4)) {
116 was_garbage = 1;
117 if (string_buf)
118 g_strlcpy(string_buf, answer, string_len);
119 } else return fish_decode_reply(answer+4, was_garbage);
123 #define SUP super->u.fish
125 static int
126 fish_command (struct vfs_class *me, struct vfs_s_super *super,
127 int wait_reply, const char *fmt, ...)
129 va_list ap;
130 char *str;
131 int status;
132 FILE *logfile = MEDATA->logfile;
134 va_start (ap, fmt);
136 str = g_strdup_vprintf (fmt, ap);
137 va_end (ap);
139 if (logfile) {
140 fwrite (str, strlen (str), 1, logfile);
141 fflush (logfile);
144 enable_interrupt_key ();
146 status = write (SUP.sockw, str, strlen (str));
147 g_free (str);
149 disable_interrupt_key ();
150 if (status < 0)
151 return TRANSIENT;
153 if (wait_reply)
154 return fish_get_reply (me, SUP.sockr,
155 (wait_reply & WANT_STRING) ? reply_str :
156 NULL, sizeof (reply_str) - 1);
157 return COMPLETE;
160 static void
161 fish_free_archive (struct vfs_class *me, struct vfs_s_super *super)
163 if ((SUP.sockw != -1) || (SUP.sockr != -1)) {
164 print_vfs_message (_("fish: Disconnecting from %s"),
165 super->name ? super->name : "???");
166 fish_command (me, super, NONE, "#BYE\nexit\n");
167 close (SUP.sockw);
168 close (SUP.sockr);
169 SUP.sockw = SUP.sockr = -1;
171 g_free (SUP.host);
172 g_free (SUP.user);
173 g_free (SUP.cwdir);
174 g_free (SUP.password);
177 static void
178 fish_pipeopen(struct vfs_s_super *super, const char *path, const char *argv[])
180 int fileset1[2], fileset2[2];
181 int res;
183 if ((pipe(fileset1)<0) || (pipe(fileset2)<0))
184 vfs_die("Cannot pipe(): %m.");
186 if ((res = fork())) {
187 if (res<0) vfs_die("Cannot fork(): %m.");
188 /* We are the parent */
189 close(fileset1[0]);
190 SUP.sockw = fileset1[1];
191 close(fileset2[1]);
192 SUP.sockr = fileset2[0];
193 } else {
194 close(0);
195 dup(fileset1[0]);
196 close(fileset1[0]); close(fileset1[1]);
197 close(1); close(2);
198 dup(fileset2[1]);
199 /* stderr to /dev/null */
200 open ("/dev/null", O_WRONLY);
201 close(fileset2[0]); close(fileset2[1]);
202 execvp(path, const_cast(char **, argv));
203 _exit(3);
207 /* The returned directory should always contain a trailing slash */
208 static char *fish_getcwd(struct vfs_class *me, struct vfs_s_super *super)
210 if (fish_command (me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
211 return g_strconcat (reply_str, "/", (char *) NULL);
212 ERRNOR (EIO, NULL);
215 static int
216 fish_open_archive_int (struct vfs_class *me, struct vfs_s_super *super)
219 char gbuf[10];
220 const char *argv[10]; /* All of 10 is used now */
221 const char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
222 int i = 0;
224 argv[i++] = xsh;
225 if (SUP.flags == FISH_FLAG_COMPRESSED)
226 argv[i++] = "-C";
228 if (SUP.flags > FISH_FLAG_RSH)
230 argv[i++] = "-p";
231 snprintf (gbuf, sizeof (gbuf), "%d", SUP.flags);
232 argv[i++] = gbuf;
235 argv[i++] = "-l";
236 argv[i++] = SUP.user;
237 argv[i++] = SUP.host;
238 argv[i++] = "echo FISH:; /bin/sh";
239 argv[i++] = NULL;
241 fish_pipeopen (super, xsh, argv);
244 char answer[2048];
245 print_vfs_message (_("fish: Waiting for initial line..."));
246 if (!vfs_s_get_line (me, SUP.sockr, answer, sizeof (answer), ':'))
247 ERRNOR (E_PROTO, -1);
248 print_vfs_message ("%s", answer);
249 if (strstr (answer, "assword")) {
251 /* Currently, this does not work. ssh reads passwords from
252 /dev/tty, not from stdin :-(. */
254 message (D_ERROR, MSG_ERROR,
256 ("Sorry, we cannot do password authenticated connections for now."));
257 ERRNOR (EPERM, -1);
258 if (!SUP.password) {
259 char *p, *op;
260 p = g_strconcat (_(" fish: Password required for "),
261 SUP.user, " ", (char *) NULL);
262 op = vfs_get_password (p);
263 g_free (p);
264 if (op == NULL)
265 ERRNOR (EPERM, -1);
266 SUP.password = op;
268 print_vfs_message (_("fish: Sending password..."));
269 write (SUP.sockw, SUP.password, strlen (SUP.password));
270 write (SUP.sockw, "\n", 1);
274 print_vfs_message (_("fish: Sending initial line..."));
276 * Run `start_fish_server'. If it doesn't exist - no problem,
277 * we'll talk directly to the shell.
279 if (fish_command
280 (me, super, WAIT_REPLY,
281 "#FISH\necho; start_fish_server 2>&1; echo '### 200'\n") !=
282 COMPLETE)
283 ERRNOR (E_PROTO, -1);
285 print_vfs_message (_("fish: Handshaking version..."));
286 if (fish_command
287 (me, super, WAIT_REPLY,
288 "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
289 ERRNOR (E_PROTO, -1);
291 /* Set up remote locale to C, otherwise dates cannot be recognized */
292 if (fish_command
293 (me, super, WAIT_REPLY,
294 "LANG=C; LC_ALL=C; LC_TIME=C\n"
295 "export LANG; export LC_ALL; export LC_TIME\n" "echo '### 200'\n")
296 != COMPLETE)
297 ERRNOR (E_PROTO, -1);
299 print_vfs_message (_("fish: Setting up current directory..."));
300 SUP.cwdir = fish_getcwd (me, super);
301 print_vfs_message (_("fish: Connected, home %s."), SUP.cwdir);
302 #if 0
303 super->name =
304 g_strconcat ("/#sh:", SUP.user, "@", SUP.host, "/", (char *) NULL);
305 #endif
306 super->name = g_strdup (PATH_SEP_STR);
308 super->root =
309 vfs_s_new_inode (me, super,
310 vfs_s_default_stat (me, S_IFDIR | 0755));
311 return 0;
314 static int
315 fish_open_archive (struct vfs_class *me, struct vfs_s_super *super,
316 const char *archive_name, char *op)
318 char *host, *user, *password, *p;
319 int flags;
321 (void) archive_name;
323 p = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags,
324 &password, 0, URL_NOSLASH);
326 g_free (p);
328 SUP.host = host;
329 SUP.user = user;
330 SUP.flags = flags;
331 if (!strncmp (op, "rsh:", 4))
332 SUP.flags = FISH_FLAG_RSH;
333 SUP.cwdir = NULL;
334 if (password)
335 SUP.password = password;
336 return fish_open_archive_int (me, super);
339 static int
340 fish_archive_same (struct vfs_class *me, struct vfs_s_super *super,
341 const char *archive_name, char *op, void *cookie)
343 char *host, *user;
344 int flags;
346 (void) me;
347 (void) archive_name;
348 (void) cookie;
350 op = vfs_split_url (strchr (op, ':') + 1, &host, &user, &flags, 0, 0,
351 URL_NOSLASH);
353 g_free (op);
355 flags = ((strcmp (host, SUP.host) == 0)
356 && (strcmp (user, SUP.user) == 0) && (flags == SUP.flags));
357 g_free (host);
358 g_free (user);
360 return flags;
363 static int
364 fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
366 struct vfs_s_super *super = dir->super;
367 char buffer[8192];
368 struct vfs_s_entry *ent = NULL;
369 FILE *logfile;
370 SHELL_ESCAPED_STR quoted_path;
371 int reply_code;
373 #if 0
375 * Simple FISH debug interface :]
377 if (!(MEDATA->logfile))
379 MEDATA->logfile = fopen("/tmp/mc-FISH.sh", "w");
381 #endif // 0
383 logfile = MEDATA->logfile;
385 print_vfs_message(_("fish: Reading directory %s..."), remote_path);
387 gettimeofday(&dir->timestamp, NULL);
388 dir->timestamp.tv_sec += fish_directory_timeout;
389 quoted_path = mhl_shell_escape_dup (remote_path);
390 fish_command (me, super, NONE,
391 "#LIST /%s\n"
392 "if `perl -v > /dev/null 2>&1` ; then\n"
393 "perl -e '\n"
394 "use strict;\n"
395 "use POSIX;\n"
396 "use Fcntl;\n"
397 "use POSIX \":fcntl_h\"; #S_ISLNK was here until 5.6\n"
398 "import Fcntl \":mode\" unless defined &S_ISLNK; #and is now here\n"
399 "my $dirname = $ARGV[0];\n"
400 "if (opendir ( DIR, $dirname )) {\n"
401 "while( (my $filename = readdir(DIR))){\n"
402 "my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat(\"$dirname/$filename\");\n"
403 "my $mloctime= scalar localtime $mtime;\n"
404 "my $shell_escape_regex= s/([;<>\\*\\|`&\\$!#\\(\\)\\[\\]\\{\\}:'\\''\"\\ \\\\])/\\\\$1/g;\n"
405 "my $e_filename = $filename;\n"
406 "$e_filename =~ $shell_escape_regex;\n"
407 "if (S_ISLNK($mode) ) {\n"
408 "my $linkname = readlink (\"$dirname/$filename\");\n"
409 "$linkname =~ $shell_escape_regex;\n"
410 "\n"
411 "printf(\"R%%o %%o $uid.$gid\\n"
412 "S$size\\n"
413 "d$mloctime\\n"
414 ":\\\"$e_filename\\\" -> \\\"$linkname\\\"\\n"
415 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
416 "} else {\n"
417 "printf(\"R%%o %%o $uid.$gid\\n"
418 "S$size\\n"
419 "d$mloctime\\n"
420 ":\\\"$e_filename\\\"\\n"
421 "\\n\", S_IMODE($mode), S_IFMT($mode));\n"
422 "}}\n"
423 "printf(\"### 200\\n\");\n"
424 "closedir(DIR);\n"
425 "} else {\n"
426 "printf(\"### 500\\n\");\n"
427 "}\n"
428 "exit 0\n"
429 "' /%s ||\n" /* ARGV[0] - path to browse */
430 " echo '### 500'\n" /* do not hang if perl is to eval it */
431 "elif `ls -1 /%s >/dev/null 2>&1` ; then\n"
432 "if `ls -Q /%s >/dev/null 2>&1`; then\n"
433 "LSOPT=\"-Qlan\";\n"
434 "ADD=0;\n"
435 "else\n"
436 "LSOPT=\"-lan\";\n"
437 "ADD=1;\n"
438 "fi\n"
439 "ls $LSOPT \"/%s\" 2>/dev/null | grep '^[^cbt]' | (\n"
440 "while read p l u g s m d y n; do\n"
441 "if [ $ADD = 0 ]; then\n"
442 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
443 "elif `sed --version >/dev/null 2>&1` ; then\n"
444 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
445 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
446 "else\n"
447 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
448 "fi\n"
449 "done )\n"
450 "ls $LSOPT /%s 2>/dev/null | grep '^[cb]' | (\n"
451 "while read p l u g a i m d y n; do\n"
452 "if [ $ADD = 0 ]; then\n"
453 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
454 "elif `sed --version >/dev/null 2>&1` ; then\n"
455 "file=`echo $n | sed -e 's#^\\(.*\\) -> \\(.*\\)#\\1\" -> \"\\2#'`\n"
456 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$file\"\n\"\n"
457 "else\n"
458 "echo \"P$p $u $g\nS$s\nd$m $d $y\n:\"$n\"\n\"\n"
459 "fi\n"
460 "done)\n"
461 "echo '### 200'\n"
462 "else\n"
463 "echo '### 500'\n"
464 "fi\n",
465 quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s, quoted_path.s);
466 g_free (quoted_path.s);
467 ent = vfs_s_generate_entry(me, NULL, dir, 0);
468 while (1) {
469 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
470 if ((!res) || (res == EINTR)) {
471 vfs_s_free_entry(me, ent);
472 me->verrno = ECONNRESET;
473 goto error;
475 if (logfile) {
476 fputs (buffer, logfile);
477 fputs ("\n", logfile);
478 fflush (logfile);
480 if (!strncmp(buffer, "### ", 4))
481 break;
482 if ((!buffer[0])) {
483 if (ent->name) {
484 vfs_s_insert_entry(me, dir, ent);
485 ent = vfs_s_generate_entry(me, NULL, dir, 0);
487 continue;
490 #define ST ent->ino->st
492 switch(buffer[0]) {
493 case ':': {
494 char *data_start = buffer+1;
495 char *filename = data_start;
496 char *linkname = data_start;
497 char *filename_bound = filename + strlen(filename);
498 char *linkname_bound = filename_bound;
499 if (!strcmp(data_start, "\".\"") || !strcmp(data_start, "\"..\""))
500 break; /* We'll do "." and ".." ourselves */
502 if (S_ISLNK(ST.st_mode)) {
503 // we expect: "escaped-name" -> "escaped-name"
504 // -> cannot occur in filenames,
505 // because it will be escaped to -\>
507 if (*filename == '"')
508 ++filename;
510 linkname = strstr(filename, "\" -> \"");
511 if (!linkname)
513 /* broken client, or smth goes wrong */
514 linkname = filename_bound;
515 if (filename_bound > filename
516 && *(filename_bound - 1) == '"')
517 --filename_bound; // skip trailing "
519 else
521 filename_bound = linkname;
522 linkname += 6; // strlen ("\" -> \"")
523 if (*(linkname_bound - 1) == '"')
524 --linkname_bound; // skip trailing "
527 ent->name = mhl_str_dup_range(filename, filename_bound);
528 mhl_shell_unescape_buf(ent->name);
530 ent->ino->linkname = mhl_str_dup_range(linkname, linkname_bound);
531 mhl_shell_unescape_buf(ent->ino->linkname);
532 } else {
533 // we expect: "escaped-name"
534 if (filename_bound - filename > 2)
536 // there is at least 2 "
537 // and we skip them
538 if (*filename == '"')
539 ++filename;
540 if (*(filename_bound - 1) == '"')
541 --filename_bound;
544 ent->name = mhl_str_dup_range(filename, filename_bound);
545 mhl_shell_unescape_buf(ent->name);
547 break;
549 case 'S':
550 #ifdef HAVE_ATOLL
551 ST.st_size = (off_t) atoll (buffer+1);
552 #else
553 ST.st_size = (off_t) atof (buffer+1);
554 #endif
555 break;
556 case 'P': {
557 size_t skipped;
558 vfs_parse_filemode (buffer + 1, &skipped, &ST.st_mode);
559 break;
561 case 'R': {
562 // raw filemode:
563 // we expect: Roctal-filemode octal-filetype uid.gid
564 size_t skipped;
565 vfs_parse_raw_filemode (buffer + 1, &skipped, &ST.st_mode);
566 break;
568 case 'd': {
569 vfs_split_text(buffer+1);
570 if (!vfs_parse_filedate(0, &ST.st_ctime))
571 break;
572 ST.st_atime = ST.st_mtime = ST.st_ctime;
574 break;
575 case 'D': {
576 struct tm tim;
577 if (sscanf(buffer+1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
578 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
579 break;
580 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime(&tim);
582 break;
583 case 'E': {
584 int maj, min;
585 if (sscanf(buffer+1, "%d,%d", &maj, &min) != 2)
586 break;
587 #ifdef HAVE_STRUCT_STAT_ST_RDEV
588 ST.st_rdev = makedev (maj, min);
589 #endif
594 vfs_s_free_entry (me, ent);
595 reply_code = fish_decode_reply(buffer + 4, 0);
596 if (reply_code == COMPLETE) {
597 g_free (SUP.cwdir);
598 SUP.cwdir = g_strdup (remote_path);
599 print_vfs_message (_("%s: done."), me->name);
600 return 0;
601 } else if (reply_code == ERROR) {
602 me->verrno = EACCES;
603 } else {
604 me->verrno = E_REMOTE;
607 error:
608 print_vfs_message (_("%s: failure"), me->name);
609 return -1;
612 static int
613 fish_file_store(struct vfs_class *me, struct vfs_s_fh *fh, char *name, char *localname)
615 struct vfs_s_super *super = FH_SUPER;
616 int n, total;
617 char buffer[8192];
618 struct stat s;
619 int was_error = 0;
620 int h;
621 SHELL_ESCAPED_STR quoted_name;
623 h = open (localname, O_RDONLY);
625 if (h == -1)
626 ERRNOR (EIO, -1);
627 if (fstat(h, &s)<0) {
628 close (h);
629 ERRNOR (EIO, -1);
632 /* First, try this as stor:
634 * ( head -c number ) | ( cat > file; cat >/dev/null )
636 * If `head' is not present on the remote system, `dd' will be used.
637 * Unfortunately, we cannot trust most non-GNU `head' implementations
638 * even if `-c' options is supported. Therefore, we separate GNU head
639 * (and other modern heads?) using `-q' and `-' . This causes another
640 * implementations to fail (because of "incorrect options").
642 * Fallback is:
644 * rest=<number>
645 * while [ $rest -gt 0 ]
646 * do
647 * cnt=`expr \( $rest + 255 \) / 256`
648 * n=`dd bs=256 count=$cnt | tee -a <target_file> | wc -c`
649 * rest=`expr $rest - $n`
650 * done
652 * `dd' was not designed for full filling of input buffers,
653 * and does not report exact number of bytes (not blocks).
654 * Therefore a more complex shell script is needed.
656 * On some systems non-GNU head writes "Usage:" error report to stdout
657 * instead of stderr. It makes impossible the use of "head || dd"
658 * algorithm for file appending case, therefore just "dd" is used for it.
661 quoted_name = mhl_shell_escape_dup(name);
662 print_vfs_message(_("fish: store %s: sending command..."), quoted_name.s );
664 /* FIXME: File size is limited to ULONG_MAX */
665 if (!fh->u.fish.append)
666 n = fish_command (me, super, WAIT_REPLY,
667 "#STOR %lu /%s\n"
668 "echo '### 001'\n"
669 "file=/%s\n"
670 "res=`exec 3>&1\n"
671 "(\n"
672 "head -c %lu -q - || echo DD >&3\n"
673 ") 2>/dev/null | (\n"
674 "cat > $file\n"
675 "cat > /dev/null\n"
676 ")`; [ \"$res\" = DD ] && {\n"
677 "> \"$file\"\n"
678 "rest=%lu\n"
679 "while [ $rest -gt 0 ]\n"
680 "do\n"
681 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
682 " n=`dd bs=256 count=$cnt | tee -a \"$file\" | wc -c`\n"
683 " rest=`expr $rest - $n`\n"
684 "done\n"
685 "}; echo '### 200'\n",
686 (unsigned long) s.st_size, quoted_name.s,
687 quoted_name.s, (unsigned long) s.st_size,
688 (unsigned long) s.st_size);
689 else
690 n = fish_command (me, super, WAIT_REPLY,
691 "#STOR %lu /%s\n"
692 "echo '### 001'\n"
693 "{\n"
694 "file=/%s\n"
695 "rest=%lu\n"
696 "while [ $rest -gt 0 ]\n"
697 "do\n"
698 " cnt=`expr \\( $rest + 255 \\) / 256`\n"
699 " n=`dd bs=256 count=$cnt | tee -a $file | wc -c`\n"
700 " rest=`expr $rest - $n`\n"
701 "done\n"
702 "}; echo '### 200'\n",
703 (unsigned long) s.st_size, quoted_name.s,
704 quoted_name.s, (unsigned long) s.st_size);
706 if (n != PRELIM) {
707 close (h);
708 ERRNOR(E_REMOTE, -1);
710 total = 0;
712 while (1) {
713 int t;
714 while ((n = read(h, buffer, sizeof(buffer))) < 0) {
715 if ((errno == EINTR) && got_interrupt())
716 continue;
717 print_vfs_message(_("fish: Local read failed, sending zeros") );
718 close(h);
719 h = open( "/dev/zero", O_RDONLY );
721 if (n == 0)
722 break;
723 if ((t = write (SUP.sockw, buffer, n)) != n) {
724 if (t == -1) {
725 me->verrno = errno;
726 } else {
727 me->verrno = EIO;
729 goto error_return;
731 disable_interrupt_key();
732 total += n;
733 print_vfs_message(_("fish: storing %s %d (%lu)"),
734 was_error ? _("zeros") : _("file"), total,
735 (unsigned long) s.st_size);
737 close(h);
738 g_free(quoted_name.s);
739 if ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
740 ERRNOR (E_REMOTE, -1);
741 return 0;
742 error_return:
743 close(h);
744 fish_get_reply(me, SUP.sockr, NULL, 0);
745 g_free(quoted_name.s);
746 return -1;
749 static int
750 fish_linear_start (struct vfs_class *me, struct vfs_s_fh *fh, off_t offset)
752 char *name;
753 SHELL_ESCAPED_STR quoted_name;
754 if (offset)
755 ERRNOR (E_NOTSUPP, 0);
756 name = vfs_s_fullpath (me, fh->ino);
757 if (!name)
758 return 0;
759 quoted_name = mhl_shell_escape_dup(name);
760 fh->u.fish.append = 0;
763 * Check whether the remote file is readable by using `dd' to copy
764 * a single byte from the remote file to /dev/null. If `dd' completes
765 * with exit status of 0 use `cat' to send the file contents to the
766 * standard output (i.e. over the network).
768 offset = fish_command (me, FH_SUPER, WANT_STRING,
769 "#RETR /%s\n"
770 "if dd if=/%s of=/dev/null bs=1 count=1 2>/dev/null ;\n"
771 "then\n"
772 "ls -ln /%s 2>/dev/null | (\n"
773 "read p l u g s r\n"
774 "echo $s\n"
775 ")\n"
776 "echo '### 100'\n"
777 "cat /%s\n"
778 "echo '### 200'\n"
779 "else\n"
780 "echo '### 500'\n"
781 "fi\n",
782 quoted_name.s, quoted_name.s, quoted_name.s, quoted_name.s );
783 g_free (quoted_name.s);
784 if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
785 fh->linear = LS_LINEAR_OPEN;
786 fh->u.fish.got = 0;
787 errno = 0;
788 #if SIZEOF_OFF_T == SIZEOF_LONG
789 fh->u.fish.total = strtol (reply_str, NULL, 10);
790 #else
791 fh->u.fish.total = strtoll (reply_str, NULL, 10);
792 #endif
793 if (errno != 0)
794 ERRNOR (E_REMOTE, 0);
795 return 1;
798 static void
799 fish_linear_abort (struct vfs_class *me, struct vfs_s_fh *fh)
801 struct vfs_s_super *super = FH_SUPER;
802 char buffer[8192];
803 int n;
805 print_vfs_message( _("Aborting transfer...") );
806 do {
807 n = MIN(8192, fh->u.fish.total - fh->u.fish.got);
808 if (n) {
809 if ((n = read(SUP.sockr, buffer, n)) < 0)
810 return;
811 fh->u.fish.got += n;
813 } while (n);
815 if (fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
816 print_vfs_message( _("Error reported after abort.") );
817 else
818 print_vfs_message( _("Aborted transfer would be successful.") );
821 static int
822 fish_linear_read (struct vfs_class *me, struct vfs_s_fh *fh, void *buf, int len)
824 struct vfs_s_super *super = FH_SUPER;
825 int n = 0;
826 len = MIN( fh->u.fish.total - fh->u.fish.got, len );
827 disable_interrupt_key();
828 while (len && ((n = read (SUP.sockr, buf, len))<0)) {
829 if ((errno == EINTR) && !got_interrupt())
830 continue;
831 break;
833 enable_interrupt_key();
835 if (n>0) fh->u.fish.got += n;
836 if (n<0) fish_linear_abort(me, fh);
837 if ((!n) && ((fish_get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
838 ERRNOR (E_REMOTE, -1);
839 ERRNOR (errno, n);
842 static void
843 fish_linear_close (struct vfs_class *me, struct vfs_s_fh *fh)
845 if (fh->u.fish.total != fh->u.fish.got)
846 fish_linear_abort(me, fh);
849 static int
850 fish_ctl (void *fh, int ctlop, void *arg)
852 (void) arg;
853 return 0;
854 #if 0
855 switch (ctlop) {
856 case VFS_CTL_IS_NOTREADY:
858 int v;
860 if (!FH->linear)
861 vfs_die ("You may not do this");
862 if (FH->linear == LS_LINEAR_CLOSED || FH->linear == LS_LINEAR_PREOPEN)
863 return 0;
865 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
866 if (((v < 0) && (errno == EINTR)) || v == 0)
867 return 1;
868 return 0;
870 default:
871 return 0;
873 #endif
876 static int
877 fish_send_command(struct vfs_class *me, struct vfs_s_super *super, const char *cmd, int flags)
879 int r;
881 r = fish_command (me, super, WAIT_REPLY, "%s", cmd);
882 vfs_stamp_create (&vfs_fish_ops, super);
883 if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
884 if (flags & OPT_FLUSH)
885 vfs_s_invalidate(me, super);
886 return 0;
889 #define PREFIX \
890 char buf[BUF_LARGE]; \
891 const char *crpath; \
892 char *mpath = mhl_str_dup (path); \
893 SHELL_ESCAPED_STR rpath; \
894 struct vfs_s_super *super; \
895 if (!(crpath = vfs_s_get_path_mangle (me, mpath, &super, 0))) { \
896 g_free (mpath); \
897 return -1; \
899 rpath = mhl_shell_escape_dup(crpath); \
900 g_free (mpath);
902 #define POSTFIX(flags) \
903 g_free (rpath.s); \
904 return fish_send_command(me, super, buf, flags);
906 static int
907 fish_chmod (struct vfs_class *me, const char *path, int mode)
909 PREFIX
910 snprintf(buf, sizeof(buf), "#CHMOD %4.4o /%s\n"
911 "chmod %4.4o /%s 2>/dev/null\n"
912 "echo '### 000'\n",
913 mode & 07777, rpath.s,
914 mode & 07777, rpath.s);
915 POSTFIX(OPT_FLUSH);
918 #define FISH_OP(name, string) \
919 static int fish_##name (struct vfs_class *me, const char *path1, const char *path2) \
921 char buf[BUF_LARGE]; \
922 const char *crpath1, *crpath2; \
923 char *mpath1, *mpath2; \
924 struct vfs_s_super *super1, *super2; \
925 if (!(crpath1 = vfs_s_get_path_mangle (me, mpath1 = g_strdup(path1), &super1, 0))) { \
926 g_free (mpath1); \
927 return -1; \
929 if (!(crpath2 = vfs_s_get_path_mangle (me, mpath2 = g_strdup(path2), &super2, 0))) { \
930 g_free (mpath1); \
931 g_free (mpath2); \
933 SHELL_ESCAPED_STR rpath1 = mhl_shell_escape_dup (crpath1); \
934 g_free (mpath1); \
935 SHELL_ESCAPED_STR rpath2 = mhl_shell_escape_dup (crpath2); \
936 g_free (mpath2); \
937 snprintf(buf, sizeof(buf), string "\n", rpath1.s, rpath2.s, rpath1.s, rpath2.s); \
938 g_free (rpath1.s); \
939 g_free (rpath2.s); \
940 return fish_send_command(me, super2, buf, OPT_FLUSH); \
943 FISH_OP(rename, "#RENAME /%s /%s\n"
944 "mv /%s /%s 2>/dev/null\n"
945 "echo '### 000'" )
946 FISH_OP(link, "#LINK /%s /%s\n"
947 "ln /%s /%s 2>/dev/null\n"
948 "echo '### 000'" )
950 static int fish_symlink (struct vfs_class *me, const char *setto, const char *path)
952 SHELL_ESCAPED_STR qsetto;
953 PREFIX
954 qsetto = mhl_shell_escape_dup (setto);
955 snprintf(buf, sizeof(buf),
956 "#SYMLINK %s /%s\n"
957 "ln -s %s /%s 2>/dev/null\n"
958 "echo '### 000'\n",
959 qsetto.s, rpath.s, qsetto.s, rpath.s);
960 g_free (qsetto.s);
961 POSTFIX(OPT_FLUSH);
964 static int
965 fish_chown (struct vfs_class *me, const char *path, int owner, int group)
967 char *sowner, *sgroup;
968 struct passwd *pw;
969 struct group *gr;
971 if ((pw = getpwuid (owner)) == NULL)
972 return 0;
974 if ((gr = getgrgid (group)) == NULL)
975 return 0;
977 sowner = pw->pw_name;
978 sgroup = gr->gr_name;
980 PREFIX
981 snprintf (buf, sizeof(buf),
982 "#CHOWN %s /%s\n"
983 "chown %s /%s 2>/dev/null\n"
984 "echo '### 000'\n",
985 sowner, rpath.s,
986 sowner, rpath.s);
987 fish_send_command (me, super, buf, OPT_FLUSH);
988 /* FIXME: what should we report if chgrp succeeds but chown fails? */
989 snprintf (buf, sizeof(buf),
990 "#CHGRP /%s \"/%s\"\n"
991 "chgrp %s \"/%s\" 2>/dev/null\n"
992 "echo '### 000'\n",
993 sgroup, rpath.s,
994 sgroup, rpath.s);
995 /* fish_send_command(me, super, buf, OPT_FLUSH); */
996 POSTFIX (OPT_FLUSH)
1000 static int fish_unlink (struct vfs_class *me, const char *path)
1002 PREFIX
1003 snprintf(buf, sizeof(buf),
1004 "#DELE /%s\n"
1005 "rm -f /%s 2>/dev/null\n"
1006 "echo '### 000'\n",
1007 rpath.s, rpath.s);
1008 POSTFIX(OPT_FLUSH);
1011 static int fish_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1013 PREFIX
1015 (void) mode;
1017 snprintf(buf, sizeof(buf),
1018 "#MKD /%s\n"
1019 "mkdir /%s 2>/dev/null\n"
1020 "echo '### 000'\n",
1021 rpath.s, rpath.s);
1022 POSTFIX(OPT_FLUSH);
1025 static int fish_rmdir (struct vfs_class *me, const char *path)
1027 PREFIX
1028 snprintf(buf, sizeof(buf),
1029 "#RMD /%s\n"
1030 "rmdir /%s 2>/dev/null\n"
1031 "echo '### 000'\n",
1032 rpath.s, rpath.s);
1033 POSTFIX(OPT_FLUSH);
1036 static int
1037 fish_fh_open (struct vfs_class *me, struct vfs_s_fh *fh, int flags,
1038 int mode)
1040 (void) mode;
1042 fh->u.fish.append = 0;
1043 /* File will be written only, so no need to retrieve it */
1044 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY | O_RDWR))) {
1045 fh->u.fish.append = flags & O_APPEND;
1046 if (!fh->ino->localname) {
1047 int tmp_handle =
1048 vfs_mkstemps (&fh->ino->localname, me->name,
1049 fh->ino->ent->name);
1050 if (tmp_handle == -1)
1051 return -1;
1052 close (tmp_handle);
1054 return 0;
1056 if (!fh->ino->localname)
1057 if (vfs_s_retrieve_file (me, fh->ino) == -1)
1058 return -1;
1059 if (!fh->ino->localname)
1060 vfs_die ("retrieve_file failed to fill in localname");
1061 return 0;
1064 static void
1065 fish_fill_names (struct vfs_class *me, fill_names_f func)
1067 struct vfs_s_super *super = MEDATA->supers;
1068 char *name;
1070 char gbuf[10];
1072 while (super)
1074 const char *flags = "";
1075 switch (SUP.flags)
1077 case FISH_FLAG_RSH:
1078 flags = ":r";
1079 break;
1080 case FISH_FLAG_COMPRESSED:
1081 flags = ":C";
1082 break;
1083 default:
1084 if (SUP.flags > FISH_FLAG_RSH)
1086 break;
1087 snprintf (gbuf, sizeof (gbuf), ":%d", SUP.flags);
1088 flags = gbuf;
1090 break;
1093 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
1094 "/", SUP.cwdir, (char *) NULL);
1095 (*func)(name);
1096 g_free (name);
1097 super = super->next;
1101 void
1102 init_fish (void)
1104 static struct vfs_s_subclass fish_subclass;
1106 fish_subclass.flags = VFS_S_REMOTE;
1107 fish_subclass.archive_same = fish_archive_same;
1108 fish_subclass.open_archive = fish_open_archive;
1109 fish_subclass.free_archive = fish_free_archive;
1110 fish_subclass.fh_open = fish_fh_open;
1111 fish_subclass.dir_load = fish_dir_load;
1112 fish_subclass.file_store = fish_file_store;
1113 fish_subclass.linear_start = fish_linear_start;
1114 fish_subclass.linear_read = fish_linear_read;
1115 fish_subclass.linear_close = fish_linear_close;
1117 vfs_s_init_class (&vfs_fish_ops, &fish_subclass);
1118 vfs_fish_ops.name = "fish";
1119 vfs_fish_ops.prefix = "sh:";
1120 vfs_fish_ops.fill_names = fish_fill_names;
1121 vfs_fish_ops.chmod = fish_chmod;
1122 vfs_fish_ops.chown = fish_chown;
1123 vfs_fish_ops.symlink = fish_symlink;
1124 vfs_fish_ops.link = fish_link;
1125 vfs_fish_ops.unlink = fish_unlink;
1126 vfs_fish_ops.rename = fish_rename;
1127 vfs_fish_ops.mkdir = fish_mkdir;
1128 vfs_fish_ops.rmdir = fish_rmdir;
1129 vfs_fish_ops.ctl = fish_ctl;
1130 vfs_register_class (&vfs_fish_ops);