mc.sh & mc.csh creation fixed...
[midnight-commander.git] / vfs / fish.c
blobff1672ab94eb9e85c6196f4432d3f4ea7bc12379
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("Could not pipe(): %m.");
165 if ((res = fork())) {
166 if (res<0) vfs_die("Could not 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 can not 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;
327 fish_which (vfs *me, char *path)
329 if (!strncmp (path, "/#sh:", 5))
330 return 1;
331 if (!strncmp (path, "/#ssh:", 6))
332 return 1;
333 if (!strncmp (path, "/#rsh:", 6))
334 return 1;
335 return 0;
338 static int
339 dir_uptodate(vfs *me, vfs_s_inode *ino)
341 struct timeval tim;
343 gettimeofday(&tim, NULL);
344 if (force_expiration) {
345 force_expiration = 0;
346 return 0;
348 if (tim.tv_sec < ino->u.fish.timestamp.tv_sec)
349 return 1;
350 return 0;
353 static int
354 dir_load(vfs *me, vfs_s_inode *dir, char *remote_path)
356 vfs_s_super *super = dir->super;
357 char buffer[8192];
358 vfs_s_entry *ent = NULL;
359 FILE *logfile;
360 char *quoted_path;
362 logfile = MEDATA->logfile;
364 print_vfs_message(_("fish: Reading directory %s..."), remote_path);
366 gettimeofday(&dir->u.fish.timestamp, NULL);
367 dir->u.fish.timestamp.tv_sec += 10; /* was 360: 10 is good for
368 stressing direntry layer a bit */
369 quoted_path = name_quote (remote_path, 0);
370 command(me, super, NONE,
371 "#LIST /%s\n"
372 "ls -lLa /%s 2>/dev/null | grep '^[^cbt]' | (\n"
373 "while read p x u g s m d y n; do\n"
374 "echo \"P$p $u.$g\nS$s\nd$m $d $y\n:$n\n\"\n"
375 "done\n"
376 ")\n"
377 "ls -lLa /%s 2>/dev/null | grep '^[cb]' | (\n"
378 "while read p x u g a i m d y n; do\n"
379 "echo \"P$p $u.$g\nE$a$i\nd$m $d $y\n:$n\n\"\n"
380 "done\n"
381 ")\n"
382 "echo '### 200'\n",
383 remote_path, quoted_path, quoted_path);
384 g_free (quoted_path);
385 #define SIMPLE_ENTRY vfs_s_generate_entry(me, NULL, dir, 0)
386 ent = SIMPLE_ENTRY;
387 while (1) {
388 int res = vfs_s_get_line_interruptible (me, buffer, sizeof (buffer), SUP.sockr);
389 if ((!res) || (res == EINTR)) {
390 vfs_s_free_entry(me, ent);
391 me->verrno = ECONNRESET;
392 goto error;
394 if (logfile) {
395 fputs (buffer, logfile);
396 fputs ("\n", logfile);
397 fflush (logfile);
399 if (!strncmp(buffer, "### ", 4))
400 break;
401 if ((!buffer[0])) {
402 if (ent->name) {
403 vfs_s_insert_entry(me, dir, ent);
404 ent = SIMPLE_ENTRY;
406 continue;
409 #define ST ent->ino->st
411 switch(buffer[0]) {
412 case ':': {
413 /* char *c; */
414 if (!strcmp(buffer+1, ".") || !strcmp(buffer+1, ".."))
415 break; /* We'll do . and .. ourself */
416 ent->name = g_strdup(buffer+1);
417 /* if ((c=strchr(ent->name, ' ')))
418 *c = 0; / * this is ugly, but we can not handle " " in name */
419 break;
421 case 'S': ST.st_size = atoi(buffer+1); break;
422 case 'P': {
423 int i;
424 if ((i = vfs_parse_filetype(buffer[1])) ==-1)
425 break;
426 ST.st_mode = i;
427 if ((i = vfs_parse_filemode(buffer+2)) ==-1)
428 break;
429 ST.st_mode |= i;
430 if (S_ISLNK(ST.st_mode))
431 ST.st_mode = 0;
433 break;
434 case 'd': {
435 vfs_split_text(buffer+1);
436 if (!vfs_parse_filedate(0, &ST.st_ctime))
437 break;
438 ST.st_atime = ST.st_mtime = ST.st_ctime;
440 break;
441 case 'D': {
442 struct tm tim;
443 if (sscanf(buffer+1, "%d %d %d %d %d %d", &tim.tm_year, &tim.tm_mon,
444 &tim.tm_mday, &tim.tm_hour, &tim.tm_min, &tim.tm_sec) != 6)
445 break;
446 ST.st_atime = ST.st_mtime = ST.st_ctime = mktime(&tim);
448 break;
449 case 'E': {
450 int maj, min;
451 if (sscanf(buffer+1, "%d,%d", &maj, &min) != 2)
452 break;
453 #ifdef HAVE_ST_RDEV
454 ST.st_rdev = (maj << 8) | min;
455 #endif
457 case 'L': ent->ino->linkname = g_strdup(buffer+1);
458 break;
462 vfs_s_free_entry (me, ent);
463 me->verrno = E_REMOTE;
464 if (decode_reply(buffer+4, 0) == COMPLETE) {
465 g_free (SUP.cwdir);
466 SUP.cwdir = g_strdup (remote_path);
467 print_vfs_message (_("%s: done."), me->name);
468 return 0;
471 error:
472 print_vfs_message (_("%s: failure"), me->name);
473 return 1;
476 static int
477 file_store(vfs *me, vfs_s_fh *fh, char *name, char *localname)
479 vfs_s_super *super = FH_SUPER;
480 int n, total;
481 char buffer[8192];
482 struct stat s;
483 int was_error = 0;
484 int h;
485 char *quoted_name;
487 h = open (localname, O_RDONLY);
489 if (h == -1)
490 ERRNOR (EIO, -1);
491 if (fstat(h, &s)<0) {
492 close (h);
493 ERRNOR (EIO, -1);
495 /* Use this as stor: ( dd block ; dd smallblock ) | ( cat > file; cat > /dev/null ) */
497 print_vfs_message(_("fish: store %s: sending command..."), name );
498 quoted_name = name_quote (name, 0);
500 * FIXME: Limit size to unsigned long for now.
501 * Files longer than 256 * ULONG_MAX are not supported.
503 if (!fh->u.fish.append)
504 n = command (me, super, WAIT_REPLY,
505 "#STOR %lu /%s\n"
506 "> /%s\n"
507 "echo '### 001'\n"
508 "(\n"
509 "dd ibs=256 obs=4096 count=%lu\n"
510 "dd bs=%lu count=1\n"
511 ") 2>/dev/null | (\n"
512 "cat > /%s\n"
513 "cat > /dev/null\n"
514 "); echo '### 200'\n",
515 (unsigned long) s.st_size, name, quoted_name,
516 (unsigned long) (s.st_size >> 8),
517 ((unsigned long) s.st_size) & (256 - 1), quoted_name);
518 else
519 n = command (me, super, WAIT_REPLY,
520 "#STOR %lu /%s\n"
521 "echo '### 001'\n"
522 "(\n"
523 "dd ibs=256 obs=4096 count=%lu\n"
524 "dd bs=%lu count=1\n"
525 ") 2>/dev/null | (\n"
526 "cat >> /%s\n"
527 "cat > /dev/null\n"
528 "); echo '### 200'\n",
529 (unsigned long) s.st_size, name,
530 (unsigned long) (s.st_size >> 8),
531 ((unsigned long) s.st_size) & (256 - 1), quoted_name);
533 g_free (quoted_name);
534 if (n != PRELIM) {
535 close (h);
536 ERRNOR(E_REMOTE, -1);
538 total = 0;
540 while (1) {
541 while ((n = read(h, buffer, sizeof(buffer))) < 0) {
542 if ((errno == EINTR) && got_interrupt)
543 continue;
544 print_vfs_message(_("fish: Local read failed, sending zeros") );
545 close(h);
546 h = open( "/dev/zero", O_RDONLY );
548 if (n == 0)
549 break;
550 while (write(SUP.sockw, buffer, n) < 0) {
551 me->verrno = errno;
552 goto error_return;
554 disable_interrupt_key();
555 total += n;
556 print_vfs_message(_("fish: storing %s %d (%lu)"),
557 was_error ? _("zeros") : _("file"), total,
558 (unsigned long) s.st_size);
560 close(h);
561 if ((get_reply (me, SUP.sockr, NULL, 0) != COMPLETE) || was_error)
562 ERRNOR (E_REMOTE, -1);
563 return 0;
564 error_return:
565 close(h);
566 get_reply(me, SUP.sockr, NULL, 0);
567 return -1;
570 static int linear_start(vfs *me, vfs_s_fh *fh, int offset)
572 char *name;
573 char *quoted_name;
574 if (offset)
575 ERRNOR (E_NOTSUPP, 0);
576 /* fe->local_stat.st_mtime = 0; FIXME: what is this good for? */
577 name = vfs_s_fullpath (me, fh->ino);
578 if (!name)
579 return 0;
580 quoted_name = name_quote (name, 0);
581 g_free (name);
582 name = quoted_name;
583 fh->u.fish.append = 0;
584 offset = command(me, FH_SUPER, WANT_STRING,
585 "#RETR /%s\n"
586 "ls -l /%s 2>/dev/null | (\n"
587 "read var1 var2 var3 var4 var5 var6\n"
588 "echo \"$var5\"\n"
589 ")\n"
590 "echo '### 100'\n"
591 "cat /%s\n"
592 "echo '### 200'\n",
593 name, name, name );
594 g_free (name);
595 if (offset != PRELIM) ERRNOR (E_REMOTE, 0);
596 fh->linear = LS_LINEAR_OPEN;
597 fh->u.fish.got = 0;
598 if (sscanf( reply_str, "%d", &fh->u.fish.total )!=1)
599 ERRNOR (E_REMOTE, 0);
600 return 1;
603 static void
604 linear_abort (vfs *me, vfs_s_fh *fh)
606 vfs_s_super *super = FH_SUPER;
607 char buffer[8192];
608 int n;
610 print_vfs_message( _("Aborting transfer...") );
611 do {
612 n = MIN(8192, fh->u.fish.total - fh->u.fish.got);
613 if (n)
614 if ((n = read(SUP.sockr, buffer, n)) < 0)
615 return;
616 } while (n);
618 if (get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)
619 print_vfs_message( _("Error reported after abort.") );
620 else
621 print_vfs_message( _("Aborted transfer would be successful.") );
624 static int
625 linear_read (vfs *me, vfs_s_fh *fh, void *buf, int len)
627 vfs_s_super *super = FH_SUPER;
628 int n = 0;
629 len = MIN( fh->u.fish.total - fh->u.fish.got, len );
630 disable_interrupt_key();
631 while (len && ((n = read (SUP.sockr, buf, len))<0)) {
632 if ((errno == EINTR) && !got_interrupt())
633 continue;
634 break;
636 enable_interrupt_key();
638 if (n>0) fh->u.fish.got += n;
639 if (n<0) linear_abort(me, fh);
640 if ((!n) && ((get_reply (me, SUP.sockr, NULL, 0) != COMPLETE)))
641 ERRNOR (E_REMOTE, -1);
642 ERRNOR (errno, n);
645 static void
646 linear_close (vfs *me, vfs_s_fh *fh)
648 if (fh->u.fish.total != fh->u.fish.got)
649 linear_abort(me, fh);
650 else if (stat (fh->ino->localname, &fh->ino->u.fish.local_stat) < 0)
651 fh->ino->u.fish.local_stat.st_mtime = 0;
654 static int
655 fish_ctl (void *fh, int ctlop, int arg)
657 return 0;
658 switch (ctlop) {
659 case MCCTL_IS_NOTREADY:
661 int v;
663 if (!FH->linear)
664 vfs_die ("You may not do this");
665 if (FH->linear == LS_LINEAR_CLOSED)
666 return 0;
668 v = vfs_s_select_on_two (FH_SUPER->u.fish.sockr, 0);
669 if (((v < 0) && (errno == EINTR)) || v == 0)
670 return 1;
671 return 0;
673 default:
674 return 0;
678 static int
679 send_fish_command(vfs *me, vfs_s_super *super, char *cmd, int flags)
681 int r;
683 r = command (me, super, WAIT_REPLY, cmd);
684 vfs_add_noncurrent_stamps (&vfs_fish_ops, (vfsid) super, NULL);
685 if (r != COMPLETE) ERRNOR (E_REMOTE, -1);
686 if (flags & OPT_FLUSH)
687 vfs_s_invalidate(me, super);
688 return 0;
691 #define PREFIX \
692 char buf[BUF_LARGE]; \
693 char *rpath; \
694 vfs_s_super *super; \
695 if (!(rpath = vfs_s_get_path_mangle(me, path, &super, 0))) \
696 return -1; \
697 rpath = name_quote (rpath, 0);
699 #define POSTFIX(flags) \
700 g_free (rpath); \
701 return send_fish_command(me, super, buf, flags);
703 static int
704 fish_chmod (vfs *me, char *path, int mode)
706 PREFIX
707 g_snprintf(buf, sizeof(buf), "#CHMOD %4.4o /%s\n"
708 "chmod %4.4o \"/%s\" 2>/dev/null\n"
709 "echo '### 000'\n",
710 mode & 07777, rpath,
711 mode & 07777, rpath);
712 POSTFIX(OPT_FLUSH);
715 #define FISH_OP(name, chk, string) \
716 static int fish_##name (vfs *me, char *path1, char *path2) \
718 char buf[BUF_LARGE]; \
719 char *rpath1, *rpath2; \
720 vfs_s_super *super1, *super2; \
721 if (!(rpath1 = vfs_s_get_path_mangle(me, path1, &super1, 0))) \
722 return -1; \
723 if (!(rpath2 = vfs_s_get_path_mangle(me, path2, &super2, 0))) \
724 return -1; \
725 rpath1 = name_quote (rpath1, 0); \
726 rpath2 = name_quote (rpath2, 0); \
727 g_snprintf(buf, sizeof(buf), string "\n", rpath1, rpath2, rpath1, rpath2); \
728 g_free (rpath1); \
729 g_free (rpath2); \
730 return send_fish_command(me, super2, buf, OPT_FLUSH); \
733 #define XTEST if (bucket1 != bucket2) { ERRNOR (EXDEV, -1); }
734 FISH_OP(rename, XTEST, "#RENAME /%s /%s\n"
735 "mv /%s /%s 2>/dev/null\n"
736 "echo '### 000'" )
737 FISH_OP(link, XTEST, "#LINK /%s /%s\n"
738 "ln /%s /%s 2>/dev/null\n"
739 "echo '### 000'" )
741 static int fish_symlink (vfs *me, char *setto, char *path)
743 PREFIX
744 setto = name_quote (setto, 0);
745 g_snprintf(buf, sizeof(buf),
746 "#SYMLINK %s /%s\n"
747 "ln -s %s /%s 2>/dev/null\n"
748 "echo '### 000'\n",
749 setto, rpath, setto, rpath);
750 g_free (setto);
751 POSTFIX(OPT_FLUSH);
754 static int
755 fish_chown (vfs *me, char *path, int owner, int group)
757 char *sowner, *sgroup;
758 struct passwd *pw;
759 struct group *gr;
760 PREFIX
762 if ((pw = getpwuid (owner)) == NULL)
763 return 0;
765 if ((gr = getgrgid (group)) == NULL)
766 return 0;
768 sowner = pw->pw_name;
769 sgroup = gr->gr_name;
770 g_snprintf(buf, sizeof(buf),
771 "#CHOWN /%s /%s\n"
772 "chown %s /%s 2>/dev/null\n"
773 "echo '### 000'\n",
774 sowner, rpath,
775 sowner, rpath);
776 send_fish_command(me, super, buf, OPT_FLUSH);
777 /* FIXME: what should we report if chgrp succeeds but chown fails? */
778 g_snprintf(buf, sizeof(buf),
779 "#CHGRP /%s /%s\n"
780 "chgrp %s /%s 2>/dev/null\n"
781 "echo '### 000'\n",
782 sgroup, rpath,
783 sgroup, rpath);
784 /* send_fish_command(me, super, buf, OPT_FLUSH); */
785 POSTFIX(OPT_FLUSH)
788 static int fish_unlink (vfs *me, char *path)
790 PREFIX
791 g_snprintf(buf, sizeof(buf),
792 "#DELE /%s\n"
793 "rm -f /%s 2>/dev/null\n"
794 "echo '### 000'\n",
795 rpath, rpath);
796 POSTFIX(OPT_FLUSH);
799 static int fish_mkdir (vfs *me, char *path, mode_t mode)
801 PREFIX
802 g_snprintf(buf, sizeof(buf),
803 "#MKD /%s\n"
804 "mkdir /%s 2>/dev/null\n"
805 "echo '### 000'\n",
806 rpath, rpath);
807 POSTFIX(OPT_FLUSH);
810 static int fish_rmdir (vfs *me, char *path)
812 PREFIX
813 g_snprintf(buf, sizeof(buf),
814 "#RMD /%s\n"
815 "rmdir /%s 2>/dev/null\n"
816 "echo '### 000'\n",
817 rpath, rpath);
818 POSTFIX(OPT_FLUSH);
821 static int fish_fh_open (vfs *me, vfs_s_fh *fh, int flags, int mode)
823 fh->u.fish.append = 0;
824 /* File will be written only, so no need to retrieve it */
825 if (((flags & O_WRONLY) == O_WRONLY) && !(flags & (O_RDONLY|O_RDWR))){
826 fh->u.fish.append = flags & O_APPEND;
827 if (!fh->ino->localname){
828 int tmp_handle = mc_mkstemps (&fh->ino->localname, me->name, NULL);
829 if (tmp_handle == -1)
830 return -1;
831 close (tmp_handle);
833 return 0;
835 if (!fh->ino->localname)
836 if (vfs_s_retrieve_file (me, fh->ino)==-1)
837 return -1;
838 if (!fh->ino->localname)
839 vfs_die( "retrieve_file failed to fill in localname" );
840 return 0;
843 static struct vfs_s_data fish_data = {
844 NULL,
847 NULL,
849 NULL, /* init_inode */
850 NULL, /* free_inode */
851 NULL, /* init_entry */
853 NULL, /* archive_check */
854 archive_same,
855 open_archive,
856 free_archive,
858 fish_fh_open, /* fh_open */
859 NULL, /* fh_close */
861 vfs_s_find_entry_linear,
862 dir_load,
863 dir_uptodate,
864 file_store,
866 linear_start,
867 linear_read,
868 linear_close
871 static void
872 fish_fill_names (vfs *me, void (*func)(char *))
874 struct vfs_s_super * super = fish_data.supers;
875 char *flags;
876 char *name;
878 while (super){
879 switch (SUP.flags & (FISH_FLAG_RSH | FISH_FLAG_COMPRESSED)) {
880 case FISH_FLAG_RSH:
881 flags = ":r";
882 break;
883 case FISH_FLAG_COMPRESSED:
884 flags = ":C";
885 break;
886 case FISH_FLAG_RSH | FISH_FLAG_COMPRESSED:
887 flags = "";
888 break;
889 default:
890 flags = "";
891 break;
894 name = g_strconcat ("/#sh:", SUP.user, "@", SUP.host, flags,
895 "/", SUP.cwdir, NULL);
896 (*func)(name);
897 g_free (name);
898 super = super->next;
902 vfs vfs_fish_ops = {
903 NULL, /* This is place of next pointer */
904 "fish",
905 F_EXEC, /* flags */
906 "sh:", /* prefix */
907 &fish_data, /* data */
908 0, /* errno */
909 NULL,
910 NULL,
911 fish_fill_names,
912 NULL,
914 vfs_s_open,
915 vfs_s_close,
916 vfs_s_read,
917 vfs_s_write,
919 vfs_s_opendir,
920 vfs_s_readdir,
921 vfs_s_closedir,
922 vfs_s_telldir,
923 vfs_s_seekdir,
925 vfs_s_stat,
926 vfs_s_lstat,
927 vfs_s_fstat,
929 fish_chmod,
930 fish_chown,
931 NULL, /* utime */
933 vfs_s_readlink,
934 fish_symlink, /* symlink */
935 fish_link, /* link */
936 fish_unlink,
938 fish_rename, /* rename */
939 vfs_s_chdir,
940 vfs_s_ferrno,
941 vfs_s_lseek,
942 NULL, /* mknod */
944 vfs_s_getid,
945 vfs_s_nothingisopen,
946 vfs_s_free,
948 NULL, /* vfs_s_getlocalcopy, */
949 NULL, /* vfs_s_ungetlocalcopy, */
951 fish_mkdir,
952 fish_rmdir,
953 fish_ctl,
954 vfs_s_setctl
956 MMAPNULL