it.po converted in UTF-8, changed some upper/lower convention in a more 'italian...
[midnight-commander.git] / vfs / fish.c
blob1d3d1acf6d88e3e6a965c11cd2883acff4765aa9
1 /* Virtual File System: FISH implementation for transfering files over
2 shell connections.
4 Copyright (C) 1998 The Free Software Foundation
6 Written by: 1998 Pavel Machek
7 Spaces fix: 2000 Michal Svec
9 Derived from ftpfs.c.
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Library General Public License
13 as published by the Free Software Foundation; either version 2 of
14 the License, or (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU Library General Public License for more details.
21 You should have received a copy of the GNU Library General Public
22 License along with this program; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 * Read README.fish for protocol specification.
28 * Syntax of path is: /#sh:user@host[:Cr]/path
29 * where C means you want compressed connection,
30 * and r means you want to use rsh
32 * Namespace: fish_vfs_ops exported.
35 /* Define this if your ssh can take -I option */
37 #include <config.h>
39 #undef HAVE_HACKED_SSH
41 #include "utilvfs.h"
42 #include "../src/dialog.h" /* For MSG_ERROR */
44 #include "xdirentry.h"
45 #include "vfs.h"
46 #include "tcputil.h"
47 #include "fish.h"
50 * Reply codes.
52 #define PRELIM 1 /* positive preliminary */
53 #define COMPLETE 2 /* positive completion */
54 #define CONTINUE 3 /* positive intermediate */
55 #define TRANSIENT 4 /* transient negative completion */
56 #define ERROR 5 /* permanent negative completion */
58 /* If true, the directory cache is forced to reload */
59 static int force_expiration = 0;
61 /* FIXME: prev two variables should be killed */
63 /* command wait_flag: */
64 #define NONE 0x00
65 #define WAIT_REPLY 0x01
66 #define WANT_STRING 0x02
67 static char reply_str [80];
69 static int
70 command (vfs *me, vfs_s_super *super, int wait_reply, const char *fmt, ...)
71 __attribute__ ((format (printf, 4, 5)));
73 static int decode_reply (char *s, int was_garbage)
75 int code;
76 if (!sscanf(s, "%d", &code)) {
77 code = 500;
78 return 5;
80 if (code<100) return was_garbage ? ERROR : (!code ? COMPLETE : PRELIM);
81 return code / 100;
84 /* Returns a reply code, check /usr/include/arpa/ftp.h for possible values */
85 static int get_reply (vfs *me, int sock, char *string_buf, int string_len)
87 char answer[1024];
88 int was_garbage = 0;
90 for (;;) {
91 if (!vfs_s_get_line(me, sock, answer, sizeof(answer), '\n')) {
92 if (string_buf)
93 *string_buf = 0;
94 return 4;
96 if (strncmp(answer, "### ", 4)) {
97 was_garbage = 1;
98 if (string_buf) {
99 strncpy(string_buf, answer, string_len - 1);
100 *(string_buf + string_len - 1) = 0;
102 } else return decode_reply(answer+4, was_garbage);
106 #define SUP super->u.fish
108 static int
109 command (vfs *me, vfs_s_super *super, int wait_reply, const char *fmt, ...)
111 va_list ap;
112 char *str;
113 int status;
114 FILE *logfile = MEDATA->logfile;
116 va_start (ap, fmt);
118 str = g_strdup_vprintf (fmt, ap);
119 va_end (ap);
121 if (logfile){
122 fwrite (str, strlen (str), 1, logfile);
123 fflush (logfile);
126 enable_interrupt_key();
128 status = write(SUP.sockw, str, strlen(str));
129 g_free (str);
131 disable_interrupt_key();
132 if (status < 0)
133 return TRANSIENT;
135 if (wait_reply)
136 return get_reply (me, SUP.sockr, (wait_reply & WANT_STRING) ? reply_str : NULL, sizeof (reply_str)-1);
137 return COMPLETE;
140 static void
141 free_archive (vfs *me, vfs_s_super *super)
143 if ((SUP.sockw != -1) || (SUP.sockr != -1)){
144 print_vfs_message (_("fish: Disconnecting from %s"), super->name?super->name:"???");
145 command(me, super, NONE, "#BYE\nexit\n");
146 close(SUP.sockw);
147 close(SUP.sockr);
148 SUP.sockw = SUP.sockr = -1;
150 g_free (SUP.host);
151 g_free (SUP.user);
152 g_free (SUP.cwdir);
153 g_free (SUP.password);
156 static void
157 pipeopen(vfs_s_super *super, char *path, char *argv[])
159 int fileset1[2], fileset2[2];
160 int res;
162 if ((pipe(fileset1)<0) || (pipe(fileset2)<0))
163 vfs_die("Cannot pipe(): %m.");
165 if ((res = fork())) {
166 if (res<0) vfs_die("Cannot fork(): %m.");
167 /* We are the parent */
168 close(fileset1[0]);
169 SUP.sockw = fileset1[1];
170 close(fileset2[1]);
171 SUP.sockr = fileset2[0];
172 } else {
173 close(0);
174 dup(fileset1[0]);
175 close(fileset1[0]); close(fileset1[1]);
176 close(1); close(2);
177 dup(fileset2[1]);
178 /* stderr to /dev/null */
179 open ("/dev/null", O_WRONLY);
180 close(fileset2[0]); close(fileset2[1]);
181 execvp(path, argv);
182 _exit(3);
186 /* The returned directory should always contain a trailing slash */
187 static char *fish_getcwd(vfs *me, vfs_s_super *super)
189 if (command(me, super, WANT_STRING, "#PWD\npwd; echo '### 200'\n") == COMPLETE)
190 return g_strconcat (reply_str, "/", NULL);
191 ERRNOR (EIO, NULL);
193 static int
194 open_archive_int (vfs *me, vfs_s_super *super)
196 char *argv[100];
197 char *xsh = (SUP.flags == FISH_FLAG_RSH ? "rsh" : "ssh");
198 int i = 0;
200 argv[i++] = xsh;
201 #ifdef HAVE_HACKED_SSH
202 argv[i++] = "-I";
203 #endif
204 argv[i++] = "-l";
205 argv[i++] = SUP.user;
206 argv[i++] = SUP.host;
207 if (SUP.flags == FISH_FLAG_COMPRESSED)
208 argv[i++] = "-C";
209 argv[i++] = "echo FISH:; /bin/sh";
210 argv[i++] = NULL;
212 #if 0
213 /* Debugging hack */
214 if (!MEDATA->logfile)
215 MEDATA->logfile = fopen( "/home/pavel/talk.fish", "w+" ); /* FIXME */
216 #endif
218 pipeopen(super, xsh, argv );
221 char answer[2048];
222 print_vfs_message( _("fish: Waiting for initial line...") );
223 if (!vfs_s_get_line(me, SUP.sockr, answer, sizeof(answer), ':'))
224 ERRNOR (E_PROTO, -1);
225 print_vfs_message( answer );
226 if (strstr(answer, "assword")) {
228 /* Currently, this does not work. ssh reads passwords from
229 /dev/tty, not from stdin :-(. */
231 #ifndef HAVE_HACKED_SSH
232 message_1s (1, MSG_ERROR, _("Sorry, we cannot do password authenticated connections for now."));
233 ERRNOR (EPERM, -1);
234 #endif
235 if (!SUP.password){
236 char *p, *op;
237 p = g_strconcat (_(" fish: Password required for "), SUP.user,
238 " ", NULL);
239 op = vfs_get_password (p);
240 g_free (p);
241 if (op == NULL)
242 ERRNOR (EPERM, -1);
243 SUP.password = g_strdup (op);
244 wipe_password(op);
246 print_vfs_message( _("fish: Sending password...") );
247 write(SUP.sockw, SUP.password, strlen(SUP.password));
248 write(SUP.sockw, "\n", 1);
252 print_vfs_message( _("fish: Sending initial line...") );
254 * Run `start_fish_server'. If it doesn't exist - no problem,
255 * we'll talk directly to the shell.
257 if (command (me, super, WAIT_REPLY,
258 "#FISH\necho; start_fish_server 2>&1;"
259 " echo '### 200'\n") != COMPLETE)
260 ERRNOR (E_PROTO, -1);
262 print_vfs_message( _("fish: Handshaking version...") );
263 if (command (me, super, WAIT_REPLY, "#VER 0.0.0\necho '### 000'\n") != COMPLETE)
264 ERRNOR (E_PROTO, -1);
266 /* Set up remote locale to C, otherwise dates cannot be recognized */
267 if (command (me, super, WAIT_REPLY, "LANG=C; LC_ALL=C; LC_TIME=C\n"
268 "export LANG; export LC_ALL; export LC_TIME\n"
269 "echo '### 200'\n") != COMPLETE)
270 ERRNOR (E_PROTO, -1);
272 print_vfs_message( _("fish: Setting up current directory...") );
273 SUP.cwdir = fish_getcwd (me, super);
274 print_vfs_message( _("fish: Connected, home %s."), SUP.cwdir );
275 #if 0
276 super->name = g_strconcat ( "/#sh:", SUP.user, "@", SUP.host, "/", NULL );
277 #endif
278 super->name = g_strdup(PATH_SEP_STR);
280 super->root = vfs_s_new_inode (me, super, vfs_s_default_stat(me, S_IFDIR | 0755));
281 return 0;
284 static int
285 open_archive (vfs *me, vfs_s_super *super, char *archive_name, char *op)
287 char *host, *user, *password, *p;
288 int flags;
290 p = vfs_split_url (strchr(op, ':')+1, &host, &user, &flags, &password, 0, URL_NOSLASH);
292 if (p)
293 g_free (p);
295 SUP.host = host;
296 SUP.user = user;
297 SUP.flags = flags;
298 if (!strncmp( op, "rsh:", 4 ))
299 SUP.flags |= FISH_FLAG_RSH;
300 SUP.cwdir = NULL;
301 if (password)
302 SUP.password = password;
303 return open_archive_int (me, super);
306 static int
307 archive_same(vfs *me, vfs_s_super *super, char *archive_name, char *op, void *cookie)
309 char *host, *user;
310 int flags;
312 op = vfs_split_url (strchr(op, ':')+1, &host, &user, &flags, 0, 0, URL_NOSLASH);
314 if (op)
315 g_free (op);
317 flags = ((strcmp (host, SUP.host) == 0) &&
318 (strcmp (user, SUP.user) == 0) &&
319 (flags == SUP.flags));
320 g_free (host);
321 g_free (user);
323 return flags;
326 static int
327 dir_uptodate(vfs *me, vfs_s_inode *ino)
329 struct timeval tim;
331 gettimeofday(&tim, NULL);
332 if (force_expiration) {
333 force_expiration = 0;
334 return 0;
336 if (tim.tv_sec < ino->u.fish.timestamp.tv_sec)
337 return 1;
338 return 0;
341 static int
342 dir_load(vfs *me, vfs_s_inode *dir, char *remote_path)
344 vfs_s_super *super = dir->super;
345 char buffer[8192];
346 vfs_s_entry *ent = NULL;
347 FILE *logfile;
348 char *quoted_path;
350 logfile = MEDATA->logfile;
352 print_vfs_message(_("fish: Reading directory %s..."), remote_path);
354 gettimeofday(&dir->u.fish.timestamp, NULL);
355 dir->u.fish.timestamp.tv_sec += 10; /* was 360: 10 is good for
356 stressing direntry layer a bit */
357 quoted_path = name_quote (remote_path, 0);
358 command(me, super, NONE,
359 "#LIST /%s\n"
360 "ls -lLa /%s 2>/dev/null | grep '^[^cbt]' | (\n"
361 "while read p x u g s m d y n; do\n"
362 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
363 "done\n"
364 ")\n"
365 "ls -lLa /%s 2>/dev/null | grep '^[cb]' | (\n"
366 "while read p x u g a i m d y n; do\n"
367 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
368 "done\n"
369 ")\n"
370 "echo '### 200'\n",
371 remote_path, quoted_path, quoted_path);
372 g_free (quoted_path);
373 #define SIMPLE_ENTRY vfs_s_generate_entry(me, NULL, dir, 0)
374 ent = SIMPLE_ENTRY;
375 while (1) {
376 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
377 if ((!res) || (res == EINTR)) {
378 vfs_s_free_entry(me, ent);
379 me->verrno = ECONNRESET;
380 goto error;
382 if (logfile) {
383 fputs (buffer, logfile);
384 fputs ("\n", logfile);
385 fflush (logfile);
387 if (!strncmp(buffer, "### ", 4))
388 break;
389 if ((!buffer[0])) {
390 if (ent->name) {
391 vfs_s_insert_entry(me, dir, ent);
392 ent = SIMPLE_ENTRY;
394 continue;
397 #define ST ent->ino->st
399 switch(buffer[0]) {
400 case ':': {
401 /* char *c; */
402 if (!strcmp(buffer+1, ".") || !strcmp(buffer+1, ".."))
403 break; /* We'll do . and .. ourself */
404 ent->name = g_strdup(buffer+1);
405 /* if ((c=strchr(ent->name, ' ')))
406 *c = 0; / * this is ugly, but we cannot handle " " in name */
407 break;
409 case 'S': ST.st_size = atoi(buffer+1); break;
410 case 'P': {
411 int i;
412 if ((i = vfs_parse_filetype(buffer[1])) ==-1)
413 break;
414 ST.st_mode = i;
415 if ((i = vfs_parse_filemode(buffer+2)) ==-1)
416 break;
417 ST.st_mode |= i;
418 if (S_ISLNK(ST.st_mode))
419 ST.st_mode = 0;
421 break;
422 case 'd': {
423 vfs_split_text(buffer+1);
424 if (!vfs_parse_filedate(0, &ST.st_ctime))
425 break;
426 ST.st_atime = ST.st_mtime = ST.st_ctime;
428 break;
429 case 'D': {
430 struct tm tim;
431 if (sscanf(buffer+1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
432 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
433 break;
434 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime(&tim);
436 break;
437 case 'E': {
438 int maj, min;
439 if (sscanf(buffer+1, "%d,%d", &maj, &min) != 2)
440 break;
441 #ifdef HAVE_ST_RDEV
442 ST.st_rdev = (maj << 8) | min;
443 #endif
445 case 'L': ent->ino->linkname = g_strdup(buffer+1);
446 break;
450 vfs_s_free_entry (me, ent);
451 me->verrno = E_REMOTE;
452 if (decode_reply(buffer+4, 0) == COMPLETE) {
453 g_free (SUP.cwdir);
454 SUP.cwdir = g_strdup (remote_path);
455 print_vfs_message (_("%s: done."), me->name);
456 return 0;
459 error:
460 print_vfs_message (_("%s: failure"), me->name);
461 return 1;
464 static int
465 file_store(vfs *me, vfs_s_fh *fh, char *name, char *localname)
467 vfs_s_super *super = FH_SUPER;
468 int n, total;
469 char buffer[8192];
470 struct stat s;
471 int was_error = 0;
472 int h;
473 char *quoted_name;
475 h = open (localname, O_RDONLY);
477 if (h == -1)
478 ERRNOR (EIO, -1);
479 if (fstat(h, &s)<0) {
480 close (h);
481 ERRNOR (EIO, -1);
483 /* Use this as stor: ( dd block ; dd smallblock ) | ( cat > file; cat > /dev/null ) */
485 print_vfs_message(_("fish: store %s: sending command..."), name );
486 quoted_name = name_quote (name, 0);
488 * FIXME: Limit size to unsigned long for now.
489 * Files longer than 256 * ULONG_MAX are not supported.
491 if (!fh->u.fish.append)
492 n = command (me, super, WAIT_REPLY,
493 "#STOR %lu /%s\n"
494 "> /%s\n"
495 "echo '### 001'\n"
496 "(\n"
497 "dd ibs=256 obs=4096 count=%lu\n"
498 "dd bs=%lu count=1\n"
499 ") 2>/dev/null | (\n"
500 "cat > /%s\n"
501 "cat > /dev/null\n"
502 "); echo '### 200'\n",
503 (unsigned long) s.st_size, name, quoted_name,
504 (unsigned long) (s.st_size >> 8),
505 ((unsigned long) s.st_size) & (256 - 1), quoted_name);
506 else
507 n = command (me, super, WAIT_REPLY,
508 "#STOR %lu /%s\n"
509 "echo '### 001'\n"
510 "(\n"
511 "dd ibs=256 obs=4096 count=%lu\n"
512 "dd bs=%lu count=1\n"
513 ") 2>/dev/null | (\n"
514 "cat >> /%s\n"
515 "cat > /dev/null\n"
516 "); echo '### 200'\n",
517 (unsigned long) s.st_size, name,
518 (unsigned long) (s.st_size >> 8),
519 ((unsigned long) s.st_size) & (256 - 1), quoted_name);
521 g_free (quoted_name);
522 if (n != PRELIM) {
523 close (h);
524 ERRNOR(E_REMOTE, -1);
526 total = 0;
528 while (1) {
529 while ((n = read(h, buffer, sizeof(buffer))) < 0) {
530 if ((errno == EINTR) && got_interrupt)
531 continue;
532 print_vfs_message(_("fish: Local read failed, sending zeros") );
533 close(h);
534 h = open( "/dev/zero", O_RDONLY );
536 if (n == 0)
537 break;
538 while (write(SUP.sockw, buffer, n) < 0) {
539 me->verrno = errno;
540 goto error_return;
542 disable_interrupt_key();
543 total += n;
544 print_vfs_message(_("fish: storing %s %d (%lu)"),
545 was_error ? _("zeros") : _("file"), total,
546 (unsigned long) s.st_size);
548 close(h);
549 if ((get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
550 ERRNOR (E_REMOTE, -1);
551 return 0;
552 error_return:
553 close(h);
554 get_reply(me, SUP.sockr, NULL, 0);
555 return -1;
558 static int linear_start(vfs *me, vfs_s_fh *fh, int offset)
560 char *name;
561 char *quoted_name;
562 if (offset)
563 ERRNOR (E_NOTSUPP, 0);
564 /* fe->local_stat.st_mtime = 0; FIXME: what is this good for? */
565 name = vfs_s_fullpath (me, fh->ino);
566 if (!name)
567 return 0;
568 quoted_name = name_quote (name, 0);
569 g_free (name);
570 name = quoted_name;
571 fh->u.fish.append = 0;
572 offset = command(me, FH_SUPER, WANT_STRING,
573 "#RETR /%s\n"
574 "ls -l /%s 2>/dev/null | (\n"
575 "read var1 var2 var3 var4 var5 var6\n"
576 "echo \"$var5\"\n"
577 ")\n"
578 "echo '### 100'\n"
579 "cat /%s\n"
580 "echo '### 200'\n",
581 name, name, name );
582 g_free (name);
583 if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
584 fh->linear = LS_LINEAR_OPEN;
585 fh->u.fish.got = 0;
586 if (sscanf( reply_str, "%d", &fh->u.fish.total )!=1)
587 ERRNOR (E_REMOTE, 0);
588 return 1;
591 static void
592 linear_abort (vfs *me, vfs_s_fh *fh)
594 vfs_s_super *super = FH_SUPER;
595 char buffer[8192];
596 int n;
598 print_vfs_message( _("Aborting transfer...") );
599 do {
600 n = MIN(8192, fh->u.fish.total - fh->u.fish.got);
601 if (n)
602 if ((n = read(SUP.sockr, buffer, n)) < 0)
603 return;
604 } while (n);
606 if (get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
607 print_vfs_message( _("Error reported after abort.") );
608 else
609 print_vfs_message( _("Aborted transfer would be successful.") );
612 static int
613 linear_read (vfs *me, vfs_s_fh *fh, void *buf, int len)
615 vfs_s_super *super = FH_SUPER;
616 int n = 0;
617 len = MIN( fh->u.fish.total - fh->u.fish.got, len );
618 disable_interrupt_key();
619 while (len && ((n = read (SUP.sockr, buf, len))<0)) {
620 if ((errno == EINTR) && !got_interrupt())
621 continue;
622 break;
624 enable_interrupt_key();
626 if (n>0) fh->u.fish.got += n;
627 if (n<0) linear_abort(me, fh);
628 if ((!n) && ((get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
629 ERRNOR (E_REMOTE, -1);
630 ERRNOR (errno, n);
633 static void
634 linear_close (vfs *me, vfs_s_fh *fh)
636 if (fh->u.fish.total != fh->u.fish.got)
637 linear_abort(me, fh);
638 else if (stat (fh->ino->localname, &fh->ino->u.fish.local_stat) < 0)
639 fh->ino->u.fish.local_stat.st_mtime = 0;
642 static int
643 fish_ctl (void *fh, int ctlop, int arg)
645 return 0;
646 switch (ctlop) {
647 case MCCTL_IS_NOTREADY:
649 int v;
651 if (!FH->linear)
652 vfs_die ("You may not do this");
653 if (FH->linear == LS_LINEAR_CLOSED)
654 return 0;
656 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
657 if (((v < 0) && (errno == EINTR)) || v == 0)
658 return 1;
659 return 0;
661 default:
662 return 0;
666 static int
667 send_fish_command(vfs *me, vfs_s_super *super, char *cmd, int flags)
669 int r;
671 r = command (me, super, WAIT_REPLY, cmd);
672 vfs_add_noncurrent_stamps (&vfs_fish_ops, (vfsid) super, NULL);
673 if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
674 if (flags & OPT_FLUSH)
675 vfs_s_invalidate(me, super);
676 return 0;
679 #define PREFIX \
680 char buf[BUF_LARGE]; \
681 char *rpath; \
682 vfs_s_super *super; \
683 if (!(rpath = vfs_s_get_path_mangle(me, path, &super, 0))) \
684 return -1; \
685 rpath = name_quote (rpath, 0);
687 #define POSTFIX(flags) \
688 g_free (rpath); \
689 return send_fish_command(me, super, buf, flags);
691 static int
692 fish_chmod (vfs *me, char *path, int mode)
694 PREFIX
695 g_snprintf(buf, sizeof(buf), "#CHMOD %4.4o /%s\n"
696 "chmod %4.4o \"/%s\" 2>/dev/null\n"
697 "echo '### 000'\n",
698 mode & 07777, rpath,
699 mode & 07777, rpath);
700 POSTFIX(OPT_FLUSH);
703 #define FISH_OP(name, chk, string) \
704 static int fish_##name (vfs *me, char *path1, char *path2) \
706 char buf[BUF_LARGE]; \
707 char *rpath1, *rpath2; \
708 vfs_s_super *super1, *super2; \
709 if (!(rpath1 = vfs_s_get_path_mangle(me, path1, &super1, 0))) \
710 return -1; \
711 if (!(rpath2 = vfs_s_get_path_mangle(me, path2, &super2, 0))) \
712 return -1; \
713 rpath1 = name_quote (rpath1, 0); \
714 rpath2 = name_quote (rpath2, 0); \
715 g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
716 g_free (rpath1); \
717 g_free (rpath2); \
718 return send_fish_command(me, super2, buf, OPT_FLUSH); \
721 #define XTEST if (bucket1 != bucket2) { ERRNOR (EXDEV, -1); }
722 FISH_OP(rename, XTEST, "#RENAME /%s /%s\n"
723 "mv /%s /%s 2>/dev/null\n"
724 "echo '### 000'" )
725 FISH_OP(link, XTEST, "#LINK /%s /%s\n"
726 "ln /%s /%s 2>/dev/null\n"
727 "echo '### 000'" )
729 static int fish_symlink (vfs *me, char *setto, char *path)
731 PREFIX
732 setto = name_quote (setto, 0);
733 g_snprintf(buf, sizeof(buf),
734 "#SYMLINK %s /%s\n"
735 "ln -s %s /%s 2>/dev/null\n"
736 "echo '### 000'\n",
737 setto, rpath, setto, rpath);
738 g_free (setto);
739 POSTFIX(OPT_FLUSH);
742 static int
743 fish_chown (vfs *me, char *path, int owner, int group)
745 char *sowner, *sgroup;
746 struct passwd *pw;
747 struct group *gr;
748 PREFIX
750 if ((pw = getpwuid (owner)) == NULL)
751 return 0;
753 if ((gr = getgrgid (group)) == NULL)
754 return 0;
756 sowner = pw->pw_name;
757 sgroup = gr->gr_name;
758 g_snprintf(buf, sizeof(buf),
759 "#CHOWN /%s /%s\n"
760 "chown %s /%s 2>/dev/null\n"
761 "echo '### 000'\n",
762 sowner, rpath,
763 sowner, rpath);
764 send_fish_command(me, super, buf, OPT_FLUSH);
765 /* FIXME: what should we report if chgrp succeeds but chown fails? */
766 g_snprintf(buf, sizeof(buf),
767 "#CHGRP /%s /%s\n"
768 "chgrp %s /%s 2>/dev/null\n"
769 "echo '### 000'\n",
770 sgroup, rpath,
771 sgroup, rpath);
772 /* send_fish_command(me, super, buf, OPT_FLUSH); */
773 POSTFIX(OPT_FLUSH)
776 static int fish_unlink (vfs *me, char *path)
778 PREFIX
779 g_snprintf(buf, sizeof(buf),
780 "#DELE /%s\n"
781 "rm -f /%s 2>/dev/null\n"
782 "echo '### 000'\n",
783 rpath, rpath);
784 POSTFIX(OPT_FLUSH);
787 static int fish_mkdir (vfs *me, char *path, mode_t mode)
789 PREFIX
790 g_snprintf(buf, sizeof(buf),
791 "#MKD /%s\n"
792 "mkdir /%s 2>/dev/null\n"
793 "echo '### 000'\n",
794 rpath, rpath);
795 POSTFIX(OPT_FLUSH);
798 static int fish_rmdir (vfs *me, char *path)
800 PREFIX
801 g_snprintf(buf, sizeof(buf),
802 "#RMD /%s\n"
803 "rmdir /%s 2>/dev/null\n"
804 "echo '### 000'\n",
805 rpath, rpath);
806 POSTFIX(OPT_FLUSH);
809 static int fish_fh_open (vfs *me, vfs_s_fh *fh, int flags, int mode)
811 fh->u.fish.append = 0;
812 /* File will be written only, so no need to retrieve it */
813 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY|O_RDWR))){
814 fh->u.fish.append = flags & O_APPEND;
815 if (!fh->ino->localname){
816 int tmp_handle = mc_mkstemps (&fh->ino->localname, me->name, NULL);
817 if (tmp_handle == -1)
818 return -1;
819 close (tmp_handle);
821 return 0;
823 if (!fh->ino->localname)
824 if (vfs_s_retrieve_file (me, fh->ino)==-1)
825 return -1;
826 if (!fh->ino->localname)
827 vfs_die( "retrieve_file failed to fill in localname" );
828 return 0;
831 static struct vfs_s_data fish_data = {
832 NULL,
835 NULL,
837 NULL, /* init_inode */
838 NULL, /* free_inode */
839 NULL, /* init_entry */
841 NULL, /* archive_check */
842 archive_same,
843 open_archive,
844 free_archive,
846 fish_fh_open, /* fh_open */
847 NULL, /* fh_close */
849 vfs_s_find_entry_linear,
850 dir_load,
851 dir_uptodate,
852 file_store,
854 linear_start,
855 linear_read,
856 linear_close
859 static void
860 fish_fill_names (vfs *me, void (*func)(char *))
862 struct vfs_s_super * super = fish_data.supers;
863 char *flags;
864 char *name;
866 while (super){
867 switch (SUP.flags & (FISH_FLAG_RSH | FISH_FLAG_COMPRESSED)) {
868 case FISH_FLAG_RSH:
869 flags = ":r";
870 break;
871 case FISH_FLAG_COMPRESSED:
872 flags = ":C";
873 break;
874 case FISH_FLAG_RSH | FISH_FLAG_COMPRESSED:
875 flags = "";
876 break;
877 default:
878 flags = "";
879 break;
882 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
883 "/", SUP.cwdir, NULL);
884 (*func)(name);
885 g_free (name);
886 super = super->next;
890 vfs vfs_fish_ops = {
891 NULL, /* This is place of next pointer */
892 "fish",
893 F_EXEC, /* flags */
894 "sh:", /* prefix */
895 &fish_data, /* data */
896 0, /* errno */
897 NULL,
898 NULL,
899 fish_fill_names,
900 NULL,
902 vfs_s_open,
903 vfs_s_close,
904 vfs_s_read,
905 vfs_s_write,
907 vfs_s_opendir,
908 vfs_s_readdir,
909 vfs_s_closedir,
910 vfs_s_telldir,
911 vfs_s_seekdir,
913 vfs_s_stat,
914 vfs_s_lstat,
915 vfs_s_fstat,
917 fish_chmod,
918 fish_chown,
919 NULL, /* utime */
921 vfs_s_readlink,
922 fish_symlink, /* symlink */
923 fish_link, /* link */
924 fish_unlink,
926 fish_rename, /* rename */
927 vfs_s_chdir,
928 vfs_s_ferrno,
929 vfs_s_lseek,
930 NULL, /* mknod */
932 vfs_s_getid,
933 vfs_s_nothingisopen,
934 vfs_s_free,
936 NULL, /* vfs_s_getlocalcopy, */
937 NULL, /* vfs_s_ungetlocalcopy, */
939 fish_mkdir,
940 fish_rmdir,
941 fish_ctl,
942 vfs_s_setctl
944 MMAPNULL