Merge branch 'master' of git://midnight-commander.org/git/mc
[midnight-commander.git] / vfs / mcfs.c
blobe10e2333aa73d44ad271ba903203314b53162e95
1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2007 Free Software Foundation, Inc.
6 Written by Miguel de Icaza
7 Andrej Borsenkow
8 Norbert Warmuth
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public License
12 as published by the Free Software Foundation; either version 2 of
13 the License, or (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public
21 License along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
24 /**
25 * \file
26 * \brief Source: Midnight Commander file system
27 * \author Miguel de Icaza
28 * \author Andrej Borsenkow
29 * \author Norbert Warmuth
31 * Namespace: exports mcfs_vfs_ops, tcp_invalidate_socket
34 #include <config.h>
36 #ifdef ENABLE_VFS_MCFS
37 #include <stdio.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <pwd.h>
44 #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
45 #include <netdb.h> /* struct hostent */
46 #include <sys/socket.h> /* AF_INET */
47 #include <netinet/in.h> /* struct in_addr */
48 #ifdef HAVE_ARPA_INET_H
49 #include <arpa/inet.h>
50 #endif
52 #ifdef HAVE_PMAP_SET
53 #include <rpc/rpc.h>
54 #include <rpc/pmap_prot.h>
55 #ifdef HAVE_RPC_PMAP_CLNT_H
56 #include <rpc/pmap_clnt.h>
57 #endif
58 #endif
60 #include "../src/global.h"
61 #include "../src/tty.h" /* enable/disable interrupt key */
62 #include "../src/wtools.h" /* message() */
63 #include "../src/main.h" /* print_vfs_message */
64 #include "utilvfs.h"
65 #include "vfs.h"
66 #include "vfs-impl.h"
67 #include "mcfs.h"
68 #include "mcfsutil.h"
69 #include "tcputil.h"
71 #ifndef INADDR_NONE
72 # define INADDR_NONE (0xffffffffU)
73 #endif
75 #define MCFS_MAX_CONNECTIONS 32
77 static struct _mcfs_connection {
78 char *host;
79 char *user;
80 char *home;
81 int sock;
82 int port;
83 int version;
84 } mcfs_connections[MCFS_MAX_CONNECTIONS];
87 #define mcserver_port 9876
89 typedef struct _mcfs_connection mcfs_connection;
91 typedef struct {
92 int handle;
93 mcfs_connection *conn;
94 } mcfs_handle;
96 static char *mcfs_gethome (mcfs_connection * mc);
97 static int my_errno;
98 static struct vfs_class vfs_mcfs_ops;
100 /* Extract the hostname and username from the path */
101 /* path is in the form: hostname:user/remote-dir */
102 static char *
103 mcfs_get_host_and_username (const char *path, char **host, char **user,
104 int *port, char **pass)
106 return vfs_split_url (path, host, user, port, pass, 0, 0);
109 static void
110 mcfs_fill_names (struct vfs_class *me, fill_names_f func)
112 int i;
113 char *name;
115 (void) me;
117 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
118 if (mcfs_connections[i].host == 0)
119 continue;
120 name = g_strconcat ("/#mc:", mcfs_connections[i].user,
121 "@", mcfs_connections[i].host, (char *) NULL);
122 (*func) (name);
123 g_free (name);
127 /* This routine checks the server RPC version and logs the user in */
128 static int
129 mcfs_login_server (int my_socket, char *user, int port,
130 int port_autodetected, char *netrcpass, int *version)
132 int result;
133 char *pass;
135 /* Send the version number */
136 rpc_send (my_socket, RPC_INT, *version, RPC_END);
137 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
138 return 0;
140 if (result != MC_VERSION_OK) {
141 message (D_ERROR, _(" MCFS "),
142 _(" The server does not support this version "));
143 close (my_socket);
144 return 0;
147 /* FIXME: figure out why last_current_dir used to be passed here */
148 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
149 RPC_STRING, user, RPC_END);
151 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
152 return 0;
154 if (result == MC_NEED_PASSWORD) {
155 if (port > 1024 && port_autodetected) {
156 int v;
157 v = query_dialog (_("Warning"),
159 (" The remote server is not running on a system port \n"
160 " you need a password to log in, but the information may \n"
161 " not be safe on the remote side. Continue? \n"),
162 D_ERROR, 2, _("&Yes"), _("&No"));
164 if (v == 1) {
165 close (my_socket);
166 return 0;
169 if (netrcpass != NULL)
170 pass = g_strdup (netrcpass);
171 else
172 pass = vfs_get_password (_(" MCFS Password required "));
173 if (!pass) {
174 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
175 close (my_socket);
176 return 0;
178 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
180 wipe_password (pass);
182 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
183 return 0;
185 if (result != MC_LOGINOK) {
186 message (D_ERROR, _(" MCFS "), _(" Invalid password "));
187 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
188 close (my_socket);
189 return 0;
192 return my_socket;
195 static int
196 mcfs_get_remote_port (struct sockaddr_in *sin, int *version)
198 #ifdef HAVE_PMAP_GETMAPS
199 int port;
200 struct pmaplist *pl;
202 *version = 1;
203 port = mcserver_port;
204 for (pl = pmap_getmaps (sin); pl; pl = pl->pml_next)
205 if (pl->pml_map.pm_prog == RPC_PROGNUM
206 && pl->pml_map.pm_prot == IPPROTO_TCP
207 && pl->pml_map.pm_vers >= (unsigned long) *version) {
208 *version = (int) pl->pml_map.pm_vers;
209 port = pl->pml_map.pm_port;
211 return port;
212 #else
213 #ifdef HAVE_PMAP_GETPORT
214 int port;
215 for (*version = RPC_PROGVER; *version >= 1; (*version)--)
216 if (port = pmap_getport (sin, RPC_PROGNUM, *version, IPPROTO_TCP))
217 return port;
218 #endif /* HAVE_PMAP_GETPORT */
219 *version = 1;
220 return mcserver_port;
221 #endif /* HAVE_PMAP_GETMAPS */
224 /* This used to be in utilvfs.c, but as it deals with portmapper, it
225 is probably useful for mcfs */
226 static int
227 mcfs_create_tcp_link (const char *host, int *port, int *version, const char *caller)
229 struct sockaddr_in server_address;
230 unsigned long inaddr;
231 struct hostent *hp;
232 int my_socket;
234 if (!*host)
235 return 0;
237 memset ((char *) &server_address, 0, sizeof (server_address));
238 server_address.sin_family = AF_INET;
240 /* Try to use the dotted decimal number */
241 if ((inaddr = inet_addr (host)) != INADDR_NONE)
242 memcpy ((char *) &server_address.sin_addr, (char *) &inaddr,
243 sizeof (inaddr));
244 else {
245 if ((hp = gethostbyname (host)) == NULL) {
246 message (D_ERROR, caller, _(" Cannot locate hostname: %s "),
247 host);
248 return 0;
250 memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr,
251 hp->h_length);
254 /* Try to contact a remote portmapper to obtain the listening port */
255 if (*port == 0) {
256 *port = mcfs_get_remote_port (&server_address, version);
257 if (*port < 1)
258 return 0;
259 } else
260 *version = 1;
262 server_address.sin_port = htons (*port);
264 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
265 message (D_ERROR, caller, _(" Cannot create socket: %s "),
266 unix_error_string (errno));
267 return 0;
269 if (connect (my_socket, (struct sockaddr *) &server_address,
270 sizeof (server_address)) < 0) {
271 message (D_ERROR, caller, _(" Cannot connect to server: %s "),
272 unix_error_string (errno));
273 close (my_socket);
274 return 0;
276 return my_socket;
279 static int
280 mcfs_open_tcp_link (char *host, char *user,
281 int *port, char *netrcpass, int *version)
283 int my_socket;
284 int old_port = *port;
286 my_socket = mcfs_create_tcp_link (host, port, version, " MCfs ");
287 if (my_socket <= 0)
288 return 0;
290 /* We got the connection to the server, verify if the server
291 implements our version of the RPC mechanism and then login
292 the user.
294 return mcfs_login_server (my_socket, user, *port, old_port == 0,
295 netrcpass, version);
298 static int mcfs_get_free_bucket_init = 1;
299 static mcfs_connection *
300 mcfs_get_free_bucket (void)
302 int i;
304 if (mcfs_get_free_bucket_init) {
305 mcfs_get_free_bucket_init = 0;
306 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
307 mcfs_connections[i].host = 0;
309 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
310 if (!mcfs_connections[i].host)
311 return &mcfs_connections[i];
313 /* This can't happend, since we have checked for max connections before */
314 vfs_die ("Internal error: mcfs_get_free_bucket");
315 return 0; /* shut up, stupid gcc */
318 /* This routine keeps track of open connections */
319 /* Returns a connected socket to host */
320 static mcfs_connection *
321 mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
323 static int mcfs_open_connections = 0;
324 int i, sock, version;
325 mcfs_connection *bucket;
327 /* Is the link actually open? */
328 if (mcfs_get_free_bucket_init) {
329 mcfs_get_free_bucket_init = 0;
330 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
331 mcfs_connections[i].host = 0;
332 } else
333 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
334 if (!mcfs_connections[i].host)
335 continue;
336 if ((strcmp (host, mcfs_connections[i].host) == 0) &&
337 (strcmp (user, mcfs_connections[i].user) == 0))
338 return &mcfs_connections[i];
340 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS) {
341 message (D_ERROR, MSG_ERROR, _(" Too many open connections "));
342 return 0;
345 if (!
346 (sock =
347 mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
348 return 0;
350 bucket = mcfs_get_free_bucket ();
351 mcfs_open_connections++;
352 bucket->host = g_strdup (host);
353 bucket->user = g_strdup (user);
354 bucket->home = 0;
355 bucket->port = *port;
356 bucket->sock = sock;
357 bucket->version = version;
359 return bucket;
362 static int
363 mcfs_is_error (int result, int errno_num)
365 if (!(result == -1))
366 return my_errno = 0;
367 else
368 my_errno = errno_num;
369 return 1;
372 static int
373 mcfs_set_error (int result, int errno_num)
375 if (result == -1)
376 my_errno = errno_num;
377 else
378 my_errno = 0;
379 return result;
382 static char *
383 mcfs_get_path (mcfs_connection **mc, const char *path)
385 char *user, *host, *remote_path;
386 char *pass;
387 int port;
389 /* An absolute path name, try to determine connection socket */
390 if (strncmp (path, "/#mc:", 5))
391 return NULL;
392 path += 5;
394 /* Port = 0 means that mcfs_create_tcp_link will try to contact the
395 * remote portmapper to get the port number
397 port = 0;
398 if ((remote_path =
399 mcfs_get_host_and_username (path, &host, &user, &port, &pass)))
400 if (!(*mc = mcfs_open_link (host, user, &port, pass))) {
401 g_free (remote_path);
402 remote_path = NULL;
404 g_free (host);
405 g_free (user);
406 if (pass)
407 wipe_password (pass);
409 if (!remote_path)
410 return NULL;
412 /* NOTE: tildes are deprecated. See ftpfs.c */
414 int f = !strcmp (remote_path, "/~");
415 if (f || !strncmp (remote_path, "/~/", 3)) {
416 char *s;
417 s = concat_dir_and_file (mcfs_gethome (*mc),
418 remote_path + 3 - f);
419 g_free (remote_path);
420 remote_path = s;
423 return remote_path;
426 /* Simple function for routines returning only an integer from the server */
427 static int
428 mcfs_handle_simple_error (int sock, int return_status)
430 int status, error;
432 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
433 return mcfs_set_error (-1, EIO);
435 if (mcfs_is_error (status, error))
436 return -1;
437 if (return_status)
438 return status;
439 return 0;
442 /* Nice wrappers */
443 static int
444 mcfs_rpc_two_paths (int command, const char *s1, const char *s2)
446 mcfs_connection *mc;
447 char *r1, *r2;
449 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
450 return -1;
452 if ((r2 = mcfs_get_path (&mc, s2)) == 0) {
453 g_free (r1);
454 return -1;
457 rpc_send (mc->sock,
458 RPC_INT, command, RPC_STRING, r1, RPC_STRING, r2, RPC_END);
459 g_free (r1);
460 g_free (r2);
461 return mcfs_handle_simple_error (mc->sock, 0);
464 static int
465 mcfs_rpc_path (int command, const char *path)
467 mcfs_connection *mc;
468 char *remote_file;
470 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
471 return -1;
473 rpc_send (mc->sock,
474 RPC_INT, command, RPC_STRING, remote_file, RPC_END);
476 g_free (remote_file);
477 return mcfs_handle_simple_error (mc->sock, 0);
480 static int
481 mcfs_rpc_path_int (int command, const char *path, int data)
483 mcfs_connection *mc;
484 char *remote_file;
486 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
487 return -1;
489 rpc_send (mc->sock,
490 RPC_INT, command,
491 RPC_STRING, remote_file, RPC_INT, data, RPC_END);
493 g_free (remote_file);
494 return mcfs_handle_simple_error (mc->sock, 0);
497 static int
498 mcfs_rpc_path_int_int (int command, const char *path, int n1, int n2)
500 mcfs_connection *mc;
501 char *remote_file;
503 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
504 return -1;
506 rpc_send (mc->sock,
507 RPC_INT, command,
508 RPC_STRING, remote_file, RPC_INT, n1, RPC_INT, n2, RPC_END);
510 g_free (remote_file);
511 return mcfs_handle_simple_error (mc->sock, 0);
514 static char *
515 mcfs_gethome (mcfs_connection *mc)
517 char *buffer;
519 if (mc->home)
520 return g_strdup (mc->home);
521 else {
522 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
523 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
524 return g_strdup (PATH_SEP_STR);
525 mc->home = buffer;
526 return g_strdup (buffer);
530 /* The callbacks */
531 static void *
532 mcfs_open (struct vfs_class *me, const char *file, int flags, int mode)
534 char *remote_file;
535 mcfs_connection *mc;
536 int result, error_num;
537 mcfs_handle *remote_handle;
539 (void) me;
541 if (!(remote_file = mcfs_get_path (&mc, file)))
542 return 0;
544 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file, RPC_INT,
545 flags, RPC_INT, mode, RPC_END);
546 g_free (remote_file);
548 if (0 ==
549 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
550 return 0;
552 if (mcfs_is_error (result, error_num))
553 return 0;
555 remote_handle = g_new (mcfs_handle, 2);
556 remote_handle->handle = result;
557 remote_handle->conn = mc;
559 return remote_handle;
562 static ssize_t
563 mcfs_read (void *data, char *buffer, int count)
565 mcfs_handle *info = (mcfs_handle *) data;
566 int result, error;
567 int handle;
568 mcfs_connection *mc;
570 mc = info->conn;
571 handle = info->handle;
573 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
574 RPC_INT, count, RPC_END);
576 if (0 ==
577 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
578 return mcfs_set_error (-1, EIO);
580 if (mcfs_is_error (result, error))
581 return 0;
583 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
584 return mcfs_set_error (-1, EIO);
586 return result;
589 static ssize_t
590 mcfs_write (void *data, const char *buf, int nbyte)
592 mcfs_handle *info = (mcfs_handle *) data;
593 mcfs_connection *mc;
594 int handle;
596 mc = info->conn;
597 handle = info->handle;
599 rpc_send (mc->sock,
600 RPC_INT, MC_WRITE,
601 RPC_INT, handle,
602 RPC_INT, nbyte, RPC_BLOCK, nbyte, buf, RPC_END);
604 return mcfs_handle_simple_error (mc->sock, 1);
607 static int
608 mcfs_close (void *data)
610 mcfs_handle *info = (mcfs_handle *) data;
611 mcfs_connection *mc;
612 int handle, result, error;
614 if (!data)
615 return -1;
617 handle = info->handle;
618 mc = info->conn;
620 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
622 if (0 ==
623 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
624 return mcfs_set_error (-1, EIO);
626 mcfs_is_error (result, error);
628 g_free (data);
629 return result;
632 static int
633 mcfs_errno (struct vfs_class *me)
635 (void) me;
637 return my_errno;
640 typedef struct dir_entry {
641 char *text;
642 struct dir_entry *next;
643 struct stat my_stat;
644 int merrno;
645 } dir_entry;
647 typedef struct {
648 mcfs_connection *conn;
649 int handle;
650 dir_entry *entries;
651 dir_entry *current;
652 } opendir_info;
654 static void *
655 mcfs_opendir (struct vfs_class *me, const char *dirname)
657 opendir_info *mcfs_info;
658 mcfs_connection *mc;
659 int handle, error_num;
660 char *remote_dir;
661 int result;
663 (void) me;
665 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
666 return 0;
668 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir,
669 RPC_END);
670 g_free (remote_dir);
672 if (0 ==
673 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
674 return 0;
676 if (mcfs_is_error (result, error_num))
677 return 0;
679 handle = result;
681 mcfs_info = g_new (opendir_info, 1);
682 mcfs_info->conn = mc;
683 mcfs_info->handle = handle;
684 mcfs_info->entries = 0;
685 mcfs_info->current = 0;
687 return mcfs_info;
690 static int mcfs_get_stat_info (mcfs_connection * mc, struct stat *buf);
692 static int
693 mcfs_loaddir (opendir_info *mcfs_info)
695 int status, error;
696 mcfs_connection *mc = mcfs_info->conn;
697 int link = mc->sock;
698 int first = 1;
700 rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle,
701 RPC_END);
703 for (;;) {
704 int entry_len;
705 dir_entry *new_entry;
707 if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
708 return 0;
710 if (entry_len == 0)
711 break;
713 new_entry = g_new (dir_entry, 1);
714 new_entry->text = g_new0 (char, entry_len + 1);
716 new_entry->next = 0;
717 if (first) {
718 mcfs_info->entries = new_entry;
719 mcfs_info->current = new_entry;
720 first = 0;
721 } else {
722 mcfs_info->current->next = new_entry;
723 mcfs_info->current = new_entry;
726 if (!rpc_get
727 (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
728 return 0;
730 /* Then we get the status from the lstat */
731 if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
732 return 0;
734 if (mcfs_is_error (status, error))
735 new_entry->merrno = error;
736 else {
737 new_entry->merrno = 0;
738 if (!mcfs_get_stat_info (mc, &(new_entry->my_stat)))
739 return 0;
742 mcfs_info->current = mcfs_info->entries;
744 return 1;
747 static void
748 mcfs_free_dir (dir_entry *de)
750 if (!de)
751 return;
752 mcfs_free_dir (de->next);
753 g_free (de->text);
754 g_free (de);
757 static union vfs_dirent mcfs_readdir_data;
759 /* The readdir routine loads the complete directory */
760 /* It's too slow to ask the server each time */
761 /* It now also sends the complete lstat information for each file */
762 static struct stat *cached_lstat_info;
764 static void *
765 mcfs_readdir (void *info)
767 opendir_info *mcfs_info;
768 char *dirent_dest;
770 mcfs_info = (opendir_info *) info;
772 if (!mcfs_info->entries)
773 if (!mcfs_loaddir (mcfs_info))
774 return NULL;
776 if (mcfs_info->current == 0) {
777 cached_lstat_info = 0;
778 mcfs_free_dir (mcfs_info->entries);
779 mcfs_info->entries = 0;
780 return NULL;
782 dirent_dest = mcfs_readdir_data.dent.d_name;
783 g_strlcpy (dirent_dest, mcfs_info->current->text, MC_MAXPATHLEN);
784 cached_lstat_info = &mcfs_info->current->my_stat;
785 mcfs_info->current = mcfs_info->current->next;
787 compute_namelen (&mcfs_readdir_data.dent);
789 return &mcfs_readdir_data;
792 static int
793 mcfs_closedir (void *info)
795 opendir_info *mcfs_info = (opendir_info *) info;
796 dir_entry *p, *q;
798 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
799 RPC_INT, mcfs_info->handle, RPC_END);
801 for (p = mcfs_info->entries; p;) {
802 q = p;
803 p = p->next;
804 g_free (q->text);
805 g_free (q);
807 g_free (info);
808 return 0;
811 static time_t
812 mcfs_get_time (mcfs_connection *mc)
814 int sock = mc->sock;
816 if (mc->version == 1) {
817 struct tm tt;
819 rpc_get (sock,
820 RPC_INT, &tt.tm_sec,
821 RPC_INT, &tt.tm_min,
822 RPC_INT, &tt.tm_hour,
823 RPC_INT, &tt.tm_mday,
824 RPC_INT, &tt.tm_year, RPC_INT, &tt.tm_mon, RPC_END);
825 tt.tm_year -= 1900;
826 tt.tm_isdst = 0;
828 return mktime (&tt);
829 } else {
830 char *buf;
831 long tm;
833 rpc_get (sock, RPC_STRING, &buf, RPC_END);
834 sscanf (buf, "%lx", &tm);
835 g_free (buf);
837 return (time_t) tm;
841 static int
842 mcfs_get_stat_info (mcfs_connection *mc, struct stat *buf)
844 long mylong;
845 int sock = mc->sock;
847 buf->st_dev = 0;
849 rpc_get (sock, RPC_INT, &mylong, RPC_END);
850 #ifdef HAVE_STRUCT_STAT_ST_RDEV
851 buf->st_rdev = mylong;
852 #endif
853 rpc_get (sock, RPC_INT, &mylong, RPC_END);
854 buf->st_ino = mylong;
855 rpc_get (sock, RPC_INT, &mylong, RPC_END);
856 buf->st_mode = mylong;
857 rpc_get (sock, RPC_INT, &mylong, RPC_END);
858 buf->st_nlink = mylong;
859 rpc_get (sock, RPC_INT, &mylong, RPC_END);
860 buf->st_uid = mylong;
861 rpc_get (sock, RPC_INT, &mylong, RPC_END);
862 buf->st_gid = mylong;
863 rpc_get (sock, RPC_INT, &mylong, RPC_END);
864 buf->st_size = mylong;
866 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
867 return 0;
868 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
869 buf->st_blocks = mylong;
870 #endif
871 buf->st_atime = mcfs_get_time (mc);
872 buf->st_mtime = mcfs_get_time (mc);
873 buf->st_ctime = mcfs_get_time (mc);
874 return 1;
877 static int
878 mcfs_stat_cmd (int cmd, const char *path, struct stat *buf)
880 char *remote_file;
881 mcfs_connection *mc;
882 int status, error;
884 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
885 return -1;
887 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
888 g_free (remote_file);
889 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
890 return mcfs_set_error (-1, errno);
892 if (mcfs_is_error (status, error))
893 return -1;
895 if (mcfs_get_stat_info (mc, buf))
896 return 0;
897 else
898 return mcfs_set_error (-1, EIO);
901 static int
902 mcfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
904 (void) me;
906 return mcfs_stat_cmd (MC_STAT, path, buf);
909 static int
910 mcfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
912 int path_len = strlen (path);
913 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
915 (void) me;
917 /* Hack ... */
918 if (strcmp (path + path_len - entry_len,
919 mcfs_readdir_data.dent.d_name) == 0 && cached_lstat_info) {
920 *buf = *cached_lstat_info;
921 return 0;
923 return mcfs_stat_cmd (MC_LSTAT, path, buf);
926 static int
927 mcfs_fstat (void *data, struct stat *buf)
929 mcfs_handle *info = (mcfs_handle *) data;
930 int result, error;
931 int handle, sock;
933 sock = info->conn->sock;
934 handle = info->handle;
936 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
937 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
938 return mcfs_set_error (-1, EIO);
940 if (mcfs_is_error (result, error))
941 return -1;
943 if (mcfs_get_stat_info (info->conn, buf))
944 return 0;
945 else
946 return mcfs_set_error (-1, EIO);
949 static int
950 mcfs_chmod (struct vfs_class *me, const char *path, int mode)
952 (void) me;
954 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
957 static int
958 mcfs_chown (struct vfs_class *me, const char *path, int owner, int group)
960 (void) me;
962 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
965 static int
966 mcfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
968 mcfs_connection *mc;
969 int status;
970 char *file;
972 (void) me;
974 if (!(file = mcfs_get_path (&mc, path)))
975 return -1;
977 status = 0;
978 if (mc->version >= 2) {
979 char abuf[BUF_SMALL];
980 char mbuf[BUF_SMALL];
981 long atime, mtime;
983 atime = (long) times->actime;
984 mtime = (long) times->modtime;
986 g_snprintf (abuf, sizeof (abuf), "%lx", atime);
987 g_snprintf (mbuf, sizeof (mbuf), "%lx", mtime);
989 rpc_send (mc->sock, RPC_INT, MC_UTIME,
990 RPC_STRING, file,
991 RPC_STRING, abuf, RPC_STRING, mbuf, RPC_END);
992 status = mcfs_handle_simple_error (mc->sock, 0);
995 g_free (file);
996 return (status);
999 static int
1000 mcfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
1002 char *remote_file, *stat_str;
1003 int status, error;
1004 mcfs_connection *mc;
1005 size_t len;
1007 (void) me;
1009 if (!(remote_file = mcfs_get_path (&mc, path)))
1010 return -1;
1012 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file,
1013 RPC_END);
1014 g_free (remote_file);
1015 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1016 return mcfs_set_error (-1, EIO);
1018 if (mcfs_is_error (status, errno))
1019 return -1;
1021 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
1022 return mcfs_set_error (-1, EIO);
1024 len = strlen (stat_str);
1025 if (len < size)
1026 size = len;
1027 /* readlink() does not append a NUL character to buf */
1028 memcpy (buf, stat_str, size);
1029 g_free (stat_str);
1030 return size;
1033 static int
1034 mcfs_unlink (struct vfs_class *me, const char *path)
1036 (void) me;
1038 return mcfs_rpc_path (MC_UNLINK, path);
1041 static int
1042 mcfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
1044 (void) me;
1046 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
1049 static int
1050 mcfs_rename (struct vfs_class *me, const char *a, const char *b)
1052 (void) me;
1054 return mcfs_rpc_two_paths (MC_RENAME, a, b);
1057 static int
1058 mcfs_chdir (struct vfs_class *me, const char *path)
1060 char *remote_dir;
1061 mcfs_connection *mc;
1062 int status, error;
1064 (void) me;
1066 if (!(remote_dir = mcfs_get_path (&mc, path)))
1067 return -1;
1069 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir,
1070 RPC_END);
1071 g_free (remote_dir);
1072 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1073 return mcfs_set_error (-1, EIO);
1075 if (mcfs_is_error (status, error))
1076 return -1;
1077 return 0;
1080 static off_t
1081 mcfs_lseek (void *data, off_t offset, int whence)
1083 mcfs_handle *info = (mcfs_handle *) data;
1084 int handle, sock;
1086 sock = info->conn->sock;
1087 handle = info->handle;
1089 /* FIXME: off_t may be too long to fit */
1090 rpc_send (sock, RPC_INT, MC_LSEEK, RPC_INT, handle, RPC_INT,
1091 (int) offset, RPC_INT, whence, RPC_END);
1093 return mcfs_handle_simple_error (sock, 1);
1096 static int
1097 mcfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1099 (void) me;
1101 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
1104 static int
1105 mcfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1107 (void) me;
1109 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
1112 static int
1113 mcfs_rmdir (struct vfs_class *me, const char *path)
1115 (void) me;
1117 return mcfs_rpc_path (MC_RMDIR, path);
1120 static int
1121 mcfs_link (struct vfs_class *me, const char *p1, const char *p2)
1123 (void) me;
1125 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
1128 /* Gives up on a socket and reopens the connection, the child own the socket
1129 * now
1131 static void
1132 mcfs_forget (const char *path)
1134 char *host, *user, *pass, *p;
1135 int port, i, vers;
1137 if (strncmp (path, "/#mc:", 5))
1138 return;
1140 path += 5;
1141 if (path[0] == '/' && path[1] == '/')
1142 path += 2;
1144 if ((p =
1145 mcfs_get_host_and_username (path, &host, &user, &port,
1146 &pass)) == 0) {
1147 g_free (host);
1148 g_free (user);
1149 if (pass)
1150 wipe_password (pass);
1151 return;
1153 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
1154 if ((strcmp (host, mcfs_connections[i].host) == 0) &&
1155 (strcmp (user, mcfs_connections[i].user) == 0) &&
1156 (port == mcfs_connections[i].port)) {
1158 /* close socket: the child owns it now */
1159 close (mcfs_connections[i].sock);
1161 /* reopen the connection */
1162 mcfs_connections[i].sock =
1163 mcfs_open_tcp_link (host, user, &port, pass, &vers);
1166 g_free (p);
1167 g_free (host);
1168 g_free (user);
1169 if (pass)
1170 wipe_password (pass);
1173 static int
1174 mcfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1176 (void) me;
1177 (void) arg;
1179 switch (ctlop) {
1180 case VFS_SETCTL_FORGET:
1181 mcfs_forget (path);
1182 return 0;
1184 return 0;
1187 void
1188 init_mcfs (void)
1190 vfs_mcfs_ops.name = "mcfs";
1191 vfs_mcfs_ops.prefix = "mc:";
1192 vfs_mcfs_ops.fill_names = mcfs_fill_names;
1193 vfs_mcfs_ops.open = mcfs_open;
1194 vfs_mcfs_ops.close = mcfs_close;
1195 vfs_mcfs_ops.read = mcfs_read;
1196 vfs_mcfs_ops.write = mcfs_write;
1197 vfs_mcfs_ops.opendir = mcfs_opendir;
1198 vfs_mcfs_ops.readdir = mcfs_readdir;
1199 vfs_mcfs_ops.closedir = mcfs_closedir;
1200 vfs_mcfs_ops.stat = mcfs_stat;
1201 vfs_mcfs_ops.lstat = mcfs_lstat;
1202 vfs_mcfs_ops.fstat = mcfs_fstat;
1203 vfs_mcfs_ops.chmod = mcfs_chmod;
1204 vfs_mcfs_ops.chown = mcfs_chown;
1205 vfs_mcfs_ops.utime = mcfs_utime;
1206 vfs_mcfs_ops.readlink = mcfs_readlink;
1207 vfs_mcfs_ops.symlink = mcfs_symlink;
1208 vfs_mcfs_ops.link = mcfs_link;
1209 vfs_mcfs_ops.unlink = mcfs_unlink;
1210 vfs_mcfs_ops.rename = mcfs_rename;
1211 vfs_mcfs_ops.chdir = mcfs_chdir;
1212 vfs_mcfs_ops.ferrno = mcfs_errno;
1213 vfs_mcfs_ops.lseek = mcfs_lseek;
1214 vfs_mcfs_ops.mknod = mcfs_mknod;
1215 vfs_mcfs_ops.mkdir = mcfs_mkdir;
1216 vfs_mcfs_ops.rmdir = mcfs_rmdir;
1217 vfs_mcfs_ops.setctl = mcfs_setctl;
1218 vfs_register_class (&vfs_mcfs_ops);
1221 static void
1222 mcfs_free_bucket (int bucket)
1224 g_free (mcfs_connections[bucket].host);
1225 g_free (mcfs_connections[bucket].user);
1226 g_free (mcfs_connections[bucket].home);
1228 /* Set all the fields to zero */
1229 mcfs_connections[bucket].host =
1230 mcfs_connections[bucket].user = mcfs_connections[bucket].home = 0;
1231 mcfs_connections[bucket].sock = mcfs_connections[bucket].version = 0;
1234 static int
1235 mcfs_invalidate_socket (int sock)
1237 int i, j = -1;
1239 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
1240 if (mcfs_connections[i].sock == sock) {
1241 mcfs_free_bucket (i);
1242 j = 0;
1245 if (j == -1)
1246 return -1; /* It was not our sock */
1247 /* Break from any possible loop */
1248 mc_chdir ("/");
1249 return 0;
1252 void
1253 tcp_invalidate_socket (int sock)
1255 mcfs_invalidate_socket (sock);
1257 #else
1258 void mcfs__unused(void)
1261 CFLAGS="-ansi -pedantic -Wall -Wextra -Werror"
1264 #endif /* ENABLE_VFS_MCFS */