Makefile.am: Fixed incorrect variable name if maintainer mode is active.
[midnight-commander.git] / vfs / mcfs.c
blob9903e57ca5737f467aa685e768759db32728f67e
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/wtools.h" /* message() */
62 #include "../src/main.h" /* print_vfs_message */
63 #include "utilvfs.h"
64 #include "vfs.h"
65 #include "vfs-impl.h"
66 #include "mcfs.h"
67 #include "mcfsutil.h"
68 #include "tcputil.h"
70 #ifndef INADDR_NONE
71 # define INADDR_NONE (0xffffffffU)
72 #endif
74 #define MCFS_MAX_CONNECTIONS 32
76 static struct _mcfs_connection {
77 char *host;
78 char *user;
79 char *home;
80 int sock;
81 int port;
82 int version;
83 } mcfs_connections[MCFS_MAX_CONNECTIONS];
86 #define mcserver_port 9876
88 typedef struct _mcfs_connection mcfs_connection;
90 typedef struct {
91 int handle;
92 mcfs_connection *conn;
93 } mcfs_handle;
95 static char *mcfs_gethome (mcfs_connection * mc);
96 static int my_errno;
97 static struct vfs_class vfs_mcfs_ops;
99 /* Extract the hostname and username from the path */
100 /* path is in the form: hostname:user/remote-dir */
101 static char *
102 mcfs_get_host_and_username (const char *path, char **host, char **user,
103 int *port, char **pass)
105 return vfs_split_url (path, host, user, port, pass, 0, 0);
108 static void
109 mcfs_fill_names (struct vfs_class *me, fill_names_f func)
111 int i;
112 char *name;
114 (void) me;
116 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
117 if (mcfs_connections[i].host == 0)
118 continue;
119 name = g_strconcat ("/#mc:", mcfs_connections[i].user,
120 "@", mcfs_connections[i].host, (char *) NULL);
121 (*func) (name);
122 g_free (name);
126 /* This routine checks the server RPC version and logs the user in */
127 static int
128 mcfs_login_server (int my_socket, char *user, int port,
129 int port_autodetected, char *netrcpass, int *version)
131 int result;
132 char *pass;
134 /* Send the version number */
135 rpc_send (my_socket, RPC_INT, *version, RPC_END);
136 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
137 return 0;
139 if (result != MC_VERSION_OK) {
140 message (D_ERROR, _(" MCFS "),
141 _(" The server does not support this version "));
142 close (my_socket);
143 return 0;
146 /* FIXME: figure out why last_current_dir used to be passed here */
147 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
148 RPC_STRING, user, RPC_END);
150 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
151 return 0;
153 if (result == MC_NEED_PASSWORD) {
154 if (port > 1024 && port_autodetected) {
155 int v;
156 v = query_dialog (_("Warning"),
158 (" The remote server is not running on a system port \n"
159 " you need a password to log in, but the information may \n"
160 " not be safe on the remote side. Continue? \n"),
161 D_ERROR, 2, _("&Yes"), _("&No"));
163 if (v == 1) {
164 close (my_socket);
165 return 0;
168 if (netrcpass != NULL)
169 pass = g_strdup (netrcpass);
170 else
171 pass = vfs_get_password (_(" MCFS Password required "));
172 if (!pass) {
173 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
174 close (my_socket);
175 return 0;
177 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
179 wipe_password (pass);
181 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
182 return 0;
184 if (result != MC_LOGINOK) {
185 message (D_ERROR, _(" MCFS "), _(" Invalid password "));
186 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
187 close (my_socket);
188 return 0;
191 return my_socket;
194 static int
195 mcfs_get_remote_port (struct sockaddr_in *sin, int *version)
197 #ifdef HAVE_PMAP_GETMAPS
198 int port;
199 struct pmaplist *pl;
201 *version = 1;
202 port = mcserver_port;
203 for (pl = pmap_getmaps (sin); pl; pl = pl->pml_next)
204 if (pl->pml_map.pm_prog == RPC_PROGNUM
205 && pl->pml_map.pm_prot == IPPROTO_TCP
206 && pl->pml_map.pm_vers >= (unsigned long) *version) {
207 *version = (int) pl->pml_map.pm_vers;
208 port = pl->pml_map.pm_port;
210 return port;
211 #else
212 #ifdef HAVE_PMAP_GETPORT
213 int port;
214 for (*version = RPC_PROGVER; *version >= 1; (*version)--)
215 if (port = pmap_getport (sin, RPC_PROGNUM, *version, IPPROTO_TCP))
216 return port;
217 #endif /* HAVE_PMAP_GETPORT */
218 *version = 1;
219 return mcserver_port;
220 #endif /* HAVE_PMAP_GETMAPS */
223 /* This used to be in utilvfs.c, but as it deals with portmapper, it
224 is probably useful for mcfs */
225 static int
226 mcfs_create_tcp_link (const char *host, int *port, int *version, const char *caller)
228 struct sockaddr_in server_address;
229 unsigned long inaddr;
230 struct hostent *hp;
231 int my_socket;
233 if (!*host)
234 return 0;
236 memset ((char *) &server_address, 0, sizeof (server_address));
237 server_address.sin_family = AF_INET;
239 /* Try to use the dotted decimal number */
240 if ((inaddr = inet_addr (host)) != INADDR_NONE)
241 memcpy ((char *) &server_address.sin_addr, (char *) &inaddr,
242 sizeof (inaddr));
243 else {
244 if ((hp = gethostbyname (host)) == NULL) {
245 message (D_ERROR, caller, _(" Cannot locate hostname: %s "),
246 host);
247 return 0;
249 memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr,
250 hp->h_length);
253 /* Try to contact a remote portmapper to obtain the listening port */
254 if (*port == 0) {
255 *port = mcfs_get_remote_port (&server_address, version);
256 if (*port < 1)
257 return 0;
258 } else
259 *version = 1;
261 server_address.sin_port = htons (*port);
263 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
264 message (D_ERROR, caller, _(" Cannot create socket: %s "),
265 unix_error_string (errno));
266 return 0;
268 if (connect (my_socket, (struct sockaddr *) &server_address,
269 sizeof (server_address)) < 0) {
270 message (D_ERROR, caller, _(" Cannot connect to server: %s "),
271 unix_error_string (errno));
272 close (my_socket);
273 return 0;
275 return my_socket;
278 static int
279 mcfs_open_tcp_link (char *host, char *user,
280 int *port, char *netrcpass, int *version)
282 int my_socket;
283 int old_port = *port;
285 my_socket = mcfs_create_tcp_link (host, port, version, " MCfs ");
286 if (my_socket <= 0)
287 return 0;
289 /* We got the connection to the server, verify if the server
290 implements our version of the RPC mechanism and then login
291 the user.
293 return mcfs_login_server (my_socket, user, *port, old_port == 0,
294 netrcpass, version);
297 static int mcfs_get_free_bucket_init = 1;
298 static mcfs_connection *
299 mcfs_get_free_bucket (void)
301 int i;
303 if (mcfs_get_free_bucket_init) {
304 mcfs_get_free_bucket_init = 0;
305 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
306 mcfs_connections[i].host = 0;
308 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
309 if (!mcfs_connections[i].host)
310 return &mcfs_connections[i];
312 /* This can't happend, since we have checked for max connections before */
313 vfs_die ("Internal error: mcfs_get_free_bucket");
314 return 0; /* shut up, stupid gcc */
317 /* This routine keeps track of open connections */
318 /* Returns a connected socket to host */
319 static mcfs_connection *
320 mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
322 static int mcfs_open_connections = 0;
323 int i, sock, version;
324 mcfs_connection *bucket;
326 /* Is the link actually open? */
327 if (mcfs_get_free_bucket_init) {
328 mcfs_get_free_bucket_init = 0;
329 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
330 mcfs_connections[i].host = 0;
331 } else
332 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
333 if (!mcfs_connections[i].host)
334 continue;
335 if ((strcmp (host, mcfs_connections[i].host) == 0) &&
336 (strcmp (user, mcfs_connections[i].user) == 0))
337 return &mcfs_connections[i];
339 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS) {
340 message (D_ERROR, MSG_ERROR, _(" Too many open connections "));
341 return 0;
344 if (!
345 (sock =
346 mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
347 return 0;
349 bucket = mcfs_get_free_bucket ();
350 mcfs_open_connections++;
351 bucket->host = g_strdup (host);
352 bucket->user = g_strdup (user);
353 bucket->home = 0;
354 bucket->port = *port;
355 bucket->sock = sock;
356 bucket->version = version;
358 return bucket;
361 static int
362 mcfs_is_error (int result, int errno_num)
364 if (!(result == -1))
365 return my_errno = 0;
366 else
367 my_errno = errno_num;
368 return 1;
371 static int
372 mcfs_set_error (int result, int errno_num)
374 if (result == -1)
375 my_errno = errno_num;
376 else
377 my_errno = 0;
378 return result;
381 static char *
382 mcfs_get_path (mcfs_connection **mc, const char *path)
384 char *user, *host, *remote_path;
385 char *pass;
386 int port;
388 /* An absolute path name, try to determine connection socket */
389 if (strncmp (path, "/#mc:", 5))
390 return NULL;
391 path += 5;
393 /* Port = 0 means that mcfs_create_tcp_link will try to contact the
394 * remote portmapper to get the port number
396 port = 0;
397 if ((remote_path =
398 mcfs_get_host_and_username (path, &host, &user, &port, &pass)))
399 if (!(*mc = mcfs_open_link (host, user, &port, pass))) {
400 g_free (remote_path);
401 remote_path = NULL;
403 g_free (host);
404 g_free (user);
405 if (pass)
406 wipe_password (pass);
408 if (!remote_path)
409 return NULL;
411 /* NOTE: tildes are deprecated. See ftpfs.c */
413 int f = !strcmp (remote_path, "/~");
414 if (f || !strncmp (remote_path, "/~/", 3)) {
415 char *s;
416 s = concat_dir_and_file (mcfs_gethome (*mc),
417 remote_path + 3 - f);
418 g_free (remote_path);
419 remote_path = s;
422 return remote_path;
425 /* Simple function for routines returning only an integer from the server */
426 static int
427 mcfs_handle_simple_error (int sock, int return_status)
429 int status, error;
431 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
432 return mcfs_set_error (-1, EIO);
434 if (mcfs_is_error (status, error))
435 return -1;
436 if (return_status)
437 return status;
438 return 0;
441 /* Nice wrappers */
442 static int
443 mcfs_rpc_two_paths (int command, const char *s1, const char *s2)
445 mcfs_connection *mc;
446 char *r1, *r2;
448 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
449 return -1;
451 if ((r2 = mcfs_get_path (&mc, s2)) == 0) {
452 g_free (r1);
453 return -1;
456 rpc_send (mc->sock,
457 RPC_INT, command, RPC_STRING, r1, RPC_STRING, r2, RPC_END);
458 g_free (r1);
459 g_free (r2);
460 return mcfs_handle_simple_error (mc->sock, 0);
463 static int
464 mcfs_rpc_path (int command, const char *path)
466 mcfs_connection *mc;
467 char *remote_file;
469 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
470 return -1;
472 rpc_send (mc->sock,
473 RPC_INT, command, RPC_STRING, remote_file, RPC_END);
475 g_free (remote_file);
476 return mcfs_handle_simple_error (mc->sock, 0);
479 static int
480 mcfs_rpc_path_int (int command, const char *path, int data)
482 mcfs_connection *mc;
483 char *remote_file;
485 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
486 return -1;
488 rpc_send (mc->sock,
489 RPC_INT, command,
490 RPC_STRING, remote_file, RPC_INT, data, RPC_END);
492 g_free (remote_file);
493 return mcfs_handle_simple_error (mc->sock, 0);
496 static int
497 mcfs_rpc_path_int_int (int command, const char *path, int n1, int n2)
499 mcfs_connection *mc;
500 char *remote_file;
502 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
503 return -1;
505 rpc_send (mc->sock,
506 RPC_INT, command,
507 RPC_STRING, remote_file, RPC_INT, n1, RPC_INT, n2, RPC_END);
509 g_free (remote_file);
510 return mcfs_handle_simple_error (mc->sock, 0);
513 static char *
514 mcfs_gethome (mcfs_connection *mc)
516 char *buffer;
518 if (mc->home)
519 return g_strdup (mc->home);
520 else {
521 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
522 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
523 return g_strdup (PATH_SEP_STR);
524 mc->home = buffer;
525 return g_strdup (buffer);
529 /* The callbacks */
530 static void *
531 mcfs_open (struct vfs_class *me, const char *file, int flags, int mode)
533 char *remote_file;
534 mcfs_connection *mc;
535 int result, error_num;
536 mcfs_handle *remote_handle;
538 (void) me;
540 if (!(remote_file = mcfs_get_path (&mc, file)))
541 return 0;
543 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file, RPC_INT,
544 flags, RPC_INT, mode, RPC_END);
545 g_free (remote_file);
547 if (0 ==
548 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
549 return 0;
551 if (mcfs_is_error (result, error_num))
552 return 0;
554 remote_handle = g_new (mcfs_handle, 2);
555 remote_handle->handle = result;
556 remote_handle->conn = mc;
558 return remote_handle;
561 static ssize_t
562 mcfs_read (void *data, char *buffer, int count)
564 mcfs_handle *info = (mcfs_handle *) data;
565 int result, error;
566 int handle;
567 mcfs_connection *mc;
569 mc = info->conn;
570 handle = info->handle;
572 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
573 RPC_INT, count, RPC_END);
575 if (0 ==
576 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
577 return mcfs_set_error (-1, EIO);
579 if (mcfs_is_error (result, error))
580 return 0;
582 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
583 return mcfs_set_error (-1, EIO);
585 return result;
588 static ssize_t
589 mcfs_write (void *data, const char *buf, int nbyte)
591 mcfs_handle *info = (mcfs_handle *) data;
592 mcfs_connection *mc;
593 int handle;
595 mc = info->conn;
596 handle = info->handle;
598 rpc_send (mc->sock,
599 RPC_INT, MC_WRITE,
600 RPC_INT, handle,
601 RPC_INT, nbyte, RPC_BLOCK, nbyte, buf, RPC_END);
603 return mcfs_handle_simple_error (mc->sock, 1);
606 static int
607 mcfs_close (void *data)
609 mcfs_handle *info = (mcfs_handle *) data;
610 mcfs_connection *mc;
611 int handle, result, error;
613 if (!data)
614 return -1;
616 handle = info->handle;
617 mc = info->conn;
619 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
621 if (0 ==
622 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
623 return mcfs_set_error (-1, EIO);
625 mcfs_is_error (result, error);
627 g_free (data);
628 return result;
631 static int
632 mcfs_errno (struct vfs_class *me)
634 (void) me;
636 return my_errno;
639 typedef struct dir_entry {
640 char *text;
641 struct dir_entry *next;
642 struct stat my_stat;
643 int merrno;
644 } dir_entry;
646 typedef struct {
647 mcfs_connection *conn;
648 int handle;
649 dir_entry *entries;
650 dir_entry *current;
651 } opendir_info;
653 static void *
654 mcfs_opendir (struct vfs_class *me, const char *dirname)
656 opendir_info *mcfs_info;
657 mcfs_connection *mc;
658 int handle, error_num;
659 char *remote_dir;
660 int result;
662 (void) me;
664 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
665 return 0;
667 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir,
668 RPC_END);
669 g_free (remote_dir);
671 if (0 ==
672 rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
673 return 0;
675 if (mcfs_is_error (result, error_num))
676 return 0;
678 handle = result;
680 mcfs_info = g_new (opendir_info, 1);
681 mcfs_info->conn = mc;
682 mcfs_info->handle = handle;
683 mcfs_info->entries = 0;
684 mcfs_info->current = 0;
686 return mcfs_info;
689 static int mcfs_get_stat_info (mcfs_connection * mc, struct stat *buf);
691 static int
692 mcfs_loaddir (opendir_info *mcfs_info)
694 int status, error;
695 mcfs_connection *mc = mcfs_info->conn;
696 int lc_link = mc->sock;
697 int first = 1;
699 rpc_send (lc_link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle,
700 RPC_END);
702 for (;;) {
703 int entry_len;
704 dir_entry *new_entry;
706 if (!rpc_get (lc_link, RPC_INT, &entry_len, RPC_END))
707 return 0;
709 if (entry_len == 0)
710 break;
712 new_entry = g_new (dir_entry, 1);
713 new_entry->text = g_new0 (char, entry_len + 1);
715 new_entry->next = 0;
716 if (first) {
717 mcfs_info->entries = new_entry;
718 mcfs_info->current = new_entry;
719 first = 0;
720 } else {
721 mcfs_info->current->next = new_entry;
722 mcfs_info->current = new_entry;
725 if (!rpc_get
726 (lc_link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
727 return 0;
729 /* Then we get the status from the lstat */
730 if (!rpc_get (lc_link, RPC_INT, &status, RPC_INT, &error, RPC_END))
731 return 0;
733 if (mcfs_is_error (status, error))
734 new_entry->merrno = error;
735 else {
736 new_entry->merrno = 0;
737 if (!mcfs_get_stat_info (mc, &(new_entry->my_stat)))
738 return 0;
741 mcfs_info->current = mcfs_info->entries;
743 return 1;
746 static void
747 mcfs_free_dir (dir_entry *de)
749 if (!de)
750 return;
751 mcfs_free_dir (de->next);
752 g_free (de->text);
753 g_free (de);
756 static union vfs_dirent mcfs_readdir_data;
758 /* The readdir routine loads the complete directory */
759 /* It's too slow to ask the server each time */
760 /* It now also sends the complete lstat information for each file */
761 static struct stat *cached_lstat_info;
763 static void *
764 mcfs_readdir (void *info)
766 opendir_info *mcfs_info;
767 char *dirent_dest;
769 mcfs_info = (opendir_info *) info;
771 if (!mcfs_info->entries)
772 if (!mcfs_loaddir (mcfs_info))
773 return NULL;
775 if (mcfs_info->current == 0) {
776 cached_lstat_info = 0;
777 mcfs_free_dir (mcfs_info->entries);
778 mcfs_info->entries = 0;
779 return NULL;
781 dirent_dest = mcfs_readdir_data.dent.d_name;
782 g_strlcpy (dirent_dest, mcfs_info->current->text, MC_MAXPATHLEN);
783 cached_lstat_info = &mcfs_info->current->my_stat;
784 mcfs_info->current = mcfs_info->current->next;
786 compute_namelen (&mcfs_readdir_data.dent);
788 return &mcfs_readdir_data;
791 static int
792 mcfs_closedir (void *info)
794 opendir_info *mcfs_info = (opendir_info *) info;
795 dir_entry *p, *q;
797 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
798 RPC_INT, mcfs_info->handle, RPC_END);
800 for (p = mcfs_info->entries; p;) {
801 q = p;
802 p = p->next;
803 g_free (q->text);
804 g_free (q);
806 g_free (info);
807 return 0;
810 static time_t
811 mcfs_get_time (mcfs_connection *mc)
813 int sock = mc->sock;
815 if (mc->version == 1) {
816 struct tm tt;
818 rpc_get (sock,
819 RPC_INT, &tt.tm_sec,
820 RPC_INT, &tt.tm_min,
821 RPC_INT, &tt.tm_hour,
822 RPC_INT, &tt.tm_mday,
823 RPC_INT, &tt.tm_year, RPC_INT, &tt.tm_mon, RPC_END);
824 tt.tm_year -= 1900;
825 tt.tm_isdst = 0;
827 return mktime (&tt);
828 } else {
829 char *buf;
830 long tm;
832 rpc_get (sock, RPC_STRING, &buf, RPC_END);
833 sscanf (buf, "%lx", &tm);
834 g_free (buf);
836 return (time_t) tm;
840 static int
841 mcfs_get_stat_info (mcfs_connection *mc, struct stat *buf)
843 long mylong;
844 int sock = mc->sock;
846 buf->st_dev = 0;
848 rpc_get (sock, RPC_INT, &mylong, RPC_END);
849 #ifdef HAVE_STRUCT_STAT_ST_RDEV
850 buf->st_rdev = mylong;
851 #endif
852 rpc_get (sock, RPC_INT, &mylong, RPC_END);
853 buf->st_ino = mylong;
854 rpc_get (sock, RPC_INT, &mylong, RPC_END);
855 buf->st_mode = mylong;
856 rpc_get (sock, RPC_INT, &mylong, RPC_END);
857 buf->st_nlink = mylong;
858 rpc_get (sock, RPC_INT, &mylong, RPC_END);
859 buf->st_uid = mylong;
860 rpc_get (sock, RPC_INT, &mylong, RPC_END);
861 buf->st_gid = mylong;
862 rpc_get (sock, RPC_INT, &mylong, RPC_END);
863 buf->st_size = mylong;
865 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
866 return 0;
867 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
868 buf->st_blocks = mylong;
869 #endif
870 buf->st_atime = mcfs_get_time (mc);
871 buf->st_mtime = mcfs_get_time (mc);
872 buf->st_ctime = mcfs_get_time (mc);
873 return 1;
876 static int
877 mcfs_stat_cmd (int cmd, const char *path, struct stat *buf)
879 char *remote_file;
880 mcfs_connection *mc;
881 int status, error;
883 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
884 return -1;
886 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
887 g_free (remote_file);
888 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
889 return mcfs_set_error (-1, errno);
891 if (mcfs_is_error (status, error))
892 return -1;
894 if (mcfs_get_stat_info (mc, buf))
895 return 0;
896 else
897 return mcfs_set_error (-1, EIO);
900 static int
901 mcfs_stat (struct vfs_class *me, const char *path, struct stat *buf)
903 (void) me;
905 return mcfs_stat_cmd (MC_STAT, path, buf);
908 static int
909 mcfs_lstat (struct vfs_class *me, const char *path, struct stat *buf)
911 int path_len = strlen (path);
912 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
914 (void) me;
916 /* Hack ... */
917 if (strcmp (path + path_len - entry_len,
918 mcfs_readdir_data.dent.d_name) == 0 && cached_lstat_info) {
919 *buf = *cached_lstat_info;
920 return 0;
922 return mcfs_stat_cmd (MC_LSTAT, path, buf);
925 static int
926 mcfs_fstat (void *data, struct stat *buf)
928 mcfs_handle *info = (mcfs_handle *) data;
929 int result, error;
930 int handle, sock;
932 sock = info->conn->sock;
933 handle = info->handle;
935 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
936 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
937 return mcfs_set_error (-1, EIO);
939 if (mcfs_is_error (result, error))
940 return -1;
942 if (mcfs_get_stat_info (info->conn, buf))
943 return 0;
944 else
945 return mcfs_set_error (-1, EIO);
948 static int
949 mcfs_chmod (struct vfs_class *me, const char *path, int mode)
951 (void) me;
953 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
956 static int
957 mcfs_chown (struct vfs_class *me, const char *path, int owner, int group)
959 (void) me;
961 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
964 static int
965 mcfs_utime (struct vfs_class *me, const char *path, struct utimbuf *times)
967 mcfs_connection *mc;
968 int status;
969 char *file;
971 (void) me;
973 if (!(file = mcfs_get_path (&mc, path)))
974 return -1;
976 status = 0;
977 if (mc->version >= 2) {
978 char abuf[BUF_SMALL];
979 char mbuf[BUF_SMALL];
980 long atime, mtime;
982 atime = (long) times->actime;
983 mtime = (long) times->modtime;
985 g_snprintf (abuf, sizeof (abuf), "%lx", atime);
986 g_snprintf (mbuf, sizeof (mbuf), "%lx", mtime);
988 rpc_send (mc->sock, RPC_INT, MC_UTIME,
989 RPC_STRING, file,
990 RPC_STRING, abuf, RPC_STRING, mbuf, RPC_END);
991 status = mcfs_handle_simple_error (mc->sock, 0);
994 g_free (file);
995 return (status);
998 static int
999 mcfs_readlink (struct vfs_class *me, const char *path, char *buf, size_t size)
1001 char *remote_file, *stat_str;
1002 int status, error;
1003 mcfs_connection *mc;
1004 size_t len;
1006 (void) me;
1008 if (!(remote_file = mcfs_get_path (&mc, path)))
1009 return -1;
1011 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file,
1012 RPC_END);
1013 g_free (remote_file);
1014 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1015 return mcfs_set_error (-1, EIO);
1017 if (mcfs_is_error (status, errno))
1018 return -1;
1020 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
1021 return mcfs_set_error (-1, EIO);
1023 len = strlen (stat_str);
1024 if (len < size)
1025 size = len;
1026 /* readlink() does not append a NUL character to buf */
1027 memcpy (buf, stat_str, size);
1028 g_free (stat_str);
1029 return size;
1032 static int
1033 mcfs_unlink (struct vfs_class *me, const char *path)
1035 (void) me;
1037 return mcfs_rpc_path (MC_UNLINK, path);
1040 static int
1041 mcfs_symlink (struct vfs_class *me, const char *n1, const char *n2)
1043 (void) me;
1045 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
1048 static int
1049 mcfs_rename (struct vfs_class *me, const char *a, const char *b)
1051 (void) me;
1053 return mcfs_rpc_two_paths (MC_RENAME, a, b);
1056 static int
1057 mcfs_chdir (struct vfs_class *me, const char *path)
1059 char *remote_dir;
1060 mcfs_connection *mc;
1061 int status, error;
1063 (void) me;
1065 if (!(remote_dir = mcfs_get_path (&mc, path)))
1066 return -1;
1068 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir,
1069 RPC_END);
1070 g_free (remote_dir);
1071 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1072 return mcfs_set_error (-1, EIO);
1074 if (mcfs_is_error (status, error))
1075 return -1;
1076 return 0;
1079 static off_t
1080 mcfs_lseek (void *data, off_t offset, int whence)
1082 mcfs_handle *info = (mcfs_handle *) data;
1083 int handle, sock;
1085 sock = info->conn->sock;
1086 handle = info->handle;
1088 /* FIXME: off_t may be too long to fit */
1089 rpc_send (sock, RPC_INT, MC_LSEEK, RPC_INT, handle, RPC_INT,
1090 (int) offset, RPC_INT, whence, RPC_END);
1092 return mcfs_handle_simple_error (sock, 1);
1095 static int
1096 mcfs_mknod (struct vfs_class *me, const char *path, int mode, int dev)
1098 (void) me;
1100 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
1103 static int
1104 mcfs_mkdir (struct vfs_class *me, const char *path, mode_t mode)
1106 (void) me;
1108 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
1111 static int
1112 mcfs_rmdir (struct vfs_class *me, const char *path)
1114 (void) me;
1116 return mcfs_rpc_path (MC_RMDIR, path);
1119 static int
1120 mcfs_link (struct vfs_class *me, const char *p1, const char *p2)
1122 (void) me;
1124 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
1127 /* Gives up on a socket and reopens the connection, the child own the socket
1128 * now
1130 static void
1131 mcfs_forget (const char *path)
1133 char *host, *user, *pass, *p;
1134 int port, i, vers;
1136 if (strncmp (path, "/#mc:", 5))
1137 return;
1139 path += 5;
1140 if (path[0] == '/' && path[1] == '/')
1141 path += 2;
1143 if ((p =
1144 mcfs_get_host_and_username (path, &host, &user, &port,
1145 &pass)) == 0) {
1146 g_free (host);
1147 g_free (user);
1148 if (pass)
1149 wipe_password (pass);
1150 return;
1152 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++) {
1153 if ((strcmp (host, mcfs_connections[i].host) == 0) &&
1154 (strcmp (user, mcfs_connections[i].user) == 0) &&
1155 (port == mcfs_connections[i].port)) {
1157 /* close socket: the child owns it now */
1158 close (mcfs_connections[i].sock);
1160 /* reopen the connection */
1161 mcfs_connections[i].sock =
1162 mcfs_open_tcp_link (host, user, &port, pass, &vers);
1165 g_free (p);
1166 g_free (host);
1167 g_free (user);
1168 if (pass)
1169 wipe_password (pass);
1172 static int
1173 mcfs_setctl (struct vfs_class *me, const char *path, int ctlop, void *arg)
1175 (void) me;
1176 (void) arg;
1178 switch (ctlop) {
1179 case VFS_SETCTL_FORGET:
1180 mcfs_forget (path);
1181 return 0;
1183 return 0;
1186 void
1187 init_mcfs (void)
1189 vfs_mcfs_ops.name = "mcfs";
1190 vfs_mcfs_ops.prefix = "mc:";
1191 vfs_mcfs_ops.fill_names = mcfs_fill_names;
1192 vfs_mcfs_ops.open = mcfs_open;
1193 vfs_mcfs_ops.close = mcfs_close;
1194 vfs_mcfs_ops.read = mcfs_read;
1195 vfs_mcfs_ops.write = mcfs_write;
1196 vfs_mcfs_ops.opendir = mcfs_opendir;
1197 vfs_mcfs_ops.readdir = mcfs_readdir;
1198 vfs_mcfs_ops.closedir = mcfs_closedir;
1199 vfs_mcfs_ops.stat = mcfs_stat;
1200 vfs_mcfs_ops.lstat = mcfs_lstat;
1201 vfs_mcfs_ops.fstat = mcfs_fstat;
1202 vfs_mcfs_ops.chmod = mcfs_chmod;
1203 vfs_mcfs_ops.chown = mcfs_chown;
1204 vfs_mcfs_ops.utime = mcfs_utime;
1205 vfs_mcfs_ops.readlink = mcfs_readlink;
1206 vfs_mcfs_ops.symlink = mcfs_symlink;
1207 vfs_mcfs_ops.link = mcfs_link;
1208 vfs_mcfs_ops.unlink = mcfs_unlink;
1209 vfs_mcfs_ops.rename = mcfs_rename;
1210 vfs_mcfs_ops.chdir = mcfs_chdir;
1211 vfs_mcfs_ops.ferrno = mcfs_errno;
1212 vfs_mcfs_ops.lseek = mcfs_lseek;
1213 vfs_mcfs_ops.mknod = mcfs_mknod;
1214 vfs_mcfs_ops.mkdir = mcfs_mkdir;
1215 vfs_mcfs_ops.rmdir = mcfs_rmdir;
1216 vfs_mcfs_ops.setctl = mcfs_setctl;
1217 vfs_register_class (&vfs_mcfs_ops);
1220 static void
1221 mcfs_free_bucket (int bucket)
1223 g_free (mcfs_connections[bucket].host);
1224 g_free (mcfs_connections[bucket].user);
1225 g_free (mcfs_connections[bucket].home);
1227 /* Set all the fields to zero */
1228 mcfs_connections[bucket].host =
1229 mcfs_connections[bucket].user = mcfs_connections[bucket].home = 0;
1230 mcfs_connections[bucket].sock = mcfs_connections[bucket].version = 0;
1233 static int
1234 mcfs_invalidate_socket (int sock)
1236 int i, j = -1;
1238 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
1239 if (mcfs_connections[i].sock == sock) {
1240 mcfs_free_bucket (i);
1241 j = 0;
1244 if (j == -1)
1245 return -1; /* It was not our sock */
1246 /* Break from any possible loop */
1247 mc_chdir ("/");
1248 return 0;
1251 void
1252 tcp_invalidate_socket (int sock)
1254 mcfs_invalidate_socket (sock);
1256 #endif /* ENABLE_VFS_MCFS */