* global.h: Move fcntl.h inclusion here. Define O_BINARY.
[midnight-commander.git] / vfs / mcfs.c
blobd6c15c447c1c253df7844df38c6db153f1ace6e6
1 /* Virtual File System: Midnight Commander file system.
3 Copyright (C) 1995, 1996, 1997 The Free Software Foundation
5 Written by Miguel de Icaza
6 Andrej Borsenkow
7 Norbert Warmuth
9 $Id$
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. */
25 /* Namespace: exports mcfs_vfs_ops, tcp_invalidate_socket */
27 #include <config.h>
29 #ifdef WITH_MCFS
30 #include <stdio.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdarg.h>
36 #include <fcntl.h>
37 #include <pwd.h>
38 #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
39 #include <netdb.h> /* struct hostent */
40 #include <sys/socket.h> /* AF_INET */
41 #include <netinet/in.h> /* struct in_addr */
42 #ifdef HAVE_ARPA_INET_H
43 #include <arpa/inet.h>
44 #endif
46 #include "utilvfs.h"
48 #include "vfs.h"
49 #include "mcfs.h"
50 #include "tcputil.h"
51 #include "../src/dialog.h"
53 #define MCFS_MAX_CONNECTIONS 32
55 static struct _mcfs_connection {
56 char *host;
57 char *user;
58 char *home;
59 int sock;
60 int port;
61 int version;
62 } mcfs_connections [MCFS_MAX_CONNECTIONS];
65 #define mcserver_port 9876
67 typedef struct _mcfs_connection mcfs_connection;
69 typedef struct { int handle; mcfs_connection *conn; } mcfs_handle;
71 static int my_errno;
73 static char *mcfs_gethome (mcfs_connection *mc);
75 /* Extract the hostname and username from the path */
76 /* path is in the form: hostname:user/remote-dir */
77 static char *mcfs_get_host_and_username (char *path, char **host, char **user,
78 int *port, char **pass)
80 return vfs_split_url (path, host, user, port, pass, 0, 0);
83 static void mcfs_fill_names (vfs *me, void (*func)(char *))
85 int i;
86 char *name;
88 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
89 if (mcfs_connections [i].host == 0)
90 continue;
91 name = g_strconcat ("/#mc:", mcfs_connections [i].user,
92 "@", mcfs_connections [i].host, NULL);
93 (*func) (name);
94 g_free (name);
98 /* This routine checks the server RPC version and logs the user in */
99 static int mcfs_login_server (int my_socket, char *user, int port,
100 int port_autodetected, char *netrcpass,
101 int *version)
103 int result;
104 char *pass;
106 /* Send the version number */
107 rpc_send (my_socket, RPC_INT, *version, RPC_END);
108 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
109 return 0;
111 if (result != MC_VERSION_OK){
112 message_1s (1, _(" MCFS "), _(" The server does not support this version "));
113 close (my_socket);
114 return 0;
117 /* FIXME: figure out why last_current_dir used to be passed here */
118 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
119 RPC_STRING, user, RPC_END);
121 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
122 return 0;
124 if (result == MC_NEED_PASSWORD){
125 if (port > 1024 && port_autodetected){
126 int v;
127 v = query_dialog (_(" Warning "),
128 _(" The remote server is not running on a system port \n"
129 " you need a password to log in, but the information may \n"
130 " not be safe on the remote side. Continue? \n"), 3, 2,
131 _(" Yes "), _(" No "));
133 if (v == 1){
134 close (my_socket);
135 return 0;
138 if (netrcpass != NULL)
139 pass = g_strdup (netrcpass);
140 else
141 pass = vfs_get_password (_(" MCFS Password required "));
142 if (!pass){
143 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
144 close (my_socket);
145 return 0;
147 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
149 wipe_password (pass);
151 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
152 return 0;
154 if (result != MC_LOGINOK){
155 message_1s (1, _(" MCFS "), _(" Invalid password "));
156 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
157 close (my_socket);
158 return 0;
161 return my_socket;
164 /* This used to be in utilvfs.c, but as it deals with portmapper, it
165 is probably usefull for mcfs */
167 open_tcp_link (char *host, int *port, int *version, char *caller)
169 struct sockaddr_in server_address;
170 unsigned long inaddr;
171 struct hostent *hp;
172 int my_socket;
174 if (!*host)
175 return 0;
177 memset ((char *) &server_address, 0, sizeof (server_address));
178 server_address.sin_family = AF_INET;
180 /* Try to use the dotted decimal number */
181 if ((inaddr = inet_addr (host)) != -1)
182 memcpy ((char *) &server_address.sin_addr, (char *) &inaddr,
183 sizeof (inaddr));
184 else {
185 if ((hp = gethostbyname (host)) == NULL){
186 message_2s (1, caller, _(" Cannot locate hostname: %s "), host);
187 return 0;
189 memcpy ((char *) &server_address.sin_addr, (char *) hp->h_addr,
190 hp->h_length);
193 /* Try to contact a remote portmapper to obtain the listening port */
194 if (*port == 0){
195 *port = get_remote_port (&server_address, version);
196 if (*port < 1)
197 return 0;
198 } else
199 *version = 1;
201 server_address.sin_port = htons (*port);
203 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
204 message_2s (1, caller, _(" Cannot create socket: %s "),
205 unix_error_string(errno));
206 return 0;
208 if (connect (my_socket, (struct sockaddr *) &server_address,
209 sizeof (server_address)) < 0){
210 message_2s (1, caller, _(" Cannot connect to server: %s "),
211 unix_error_string (errno));
212 close (my_socket);
213 return 0;
215 return my_socket;
218 static int mcfs_open_tcp_link (char *host, char *user,
219 int *port, char *netrcpass, int *version)
221 int my_socket;
222 int old_port = *port;
224 my_socket = open_tcp_link (host, port, version, " MCfs ");
225 if (my_socket <= 0)
226 return 0;
228 /* We got the connection to the server, verify if the server
229 implements our version of the RPC mechanism and then login
230 the user.
232 return mcfs_login_server (my_socket, user, *port, old_port == 0,
233 netrcpass, version);
236 static int mcfs_get_free_bucket_init = 1;
237 static mcfs_connection *mcfs_get_free_bucket (void)
239 int i;
241 if (mcfs_get_free_bucket_init) {
242 mcfs_get_free_bucket_init = 0;
243 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
244 mcfs_connections [i].host = 0;
246 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
247 if (!mcfs_connections [i].host)
248 return &mcfs_connections [i];
250 /* This can't happend, since we have checked for max connections before */
251 vfs_die("Internal error: mcfs_get_free_bucket");
252 return 0; /* shut up, stupid gcc */
255 /* This routine keeps track of open connections */
256 /* Returns a connected socket to host */
257 static mcfs_connection *mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
259 static int mcfs_open_connections = 0;
260 int i, sock, version;
261 mcfs_connection *bucket;
263 /* Is the link actually open? */
264 if (mcfs_get_free_bucket_init) {
265 mcfs_get_free_bucket_init = 0;
266 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
267 mcfs_connections [i].host = 0;
268 } else for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
269 if (!mcfs_connections [i].host)
270 continue;
271 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
272 (strcmp (user, mcfs_connections [i].user) == 0))
273 return &mcfs_connections [i];
275 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS){
276 message_1s (1, MSG_ERROR, _(" Too many open connections "));
277 return 0;
280 if (!(sock = mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
281 return 0;
283 bucket = mcfs_get_free_bucket ();
284 mcfs_open_connections++;
285 bucket->host = g_strdup (host);
286 bucket->user = g_strdup (user);
287 bucket->home = 0;
288 bucket->port = *port;
289 bucket->sock = sock;
290 bucket->version = version;
292 return bucket;
295 static int is_error (int result, int errno_num)
297 if (!(result == -1))
298 return my_errno = 0;
299 else
300 my_errno = errno_num;
301 return 1;
304 static int the_error (int result, int errno_num)
306 if (result == -1)
307 my_errno = errno_num;
308 else
309 my_errno = 0;
310 return result;
313 static char *mcfs_get_path (mcfs_connection **mc, char *path)
315 char *user, *host, *remote_path;
316 char *pass;
317 int port;
319 /* An absolute path name, try to determine connection socket */
320 if (strncmp (path, "/#mc:", 5))
321 return NULL;
322 path += 5;
324 /* Port = 0 means that open_tcp_link will try to contact the
325 * remote portmapper to get the port number
327 port = 0;
328 if ((remote_path = mcfs_get_host_and_username(path, &host, &user, &port, &pass)))
329 if (!(*mc = mcfs_open_link (host, user, &port, pass))){
330 g_free (remote_path);
331 remote_path = NULL;
333 g_free (host);
334 g_free (user);
335 if (pass)
336 wipe_password (pass);
338 if (!remote_path)
339 return NULL;
341 /* NOTE: tildes are deprecated. See ftpfs.c */
343 int f = !strcmp( remote_path, "/~" );
344 if (f || !strncmp( remote_path, "/~/", 3 )) {
345 char *s;
346 s = concat_dir_and_file( mcfs_gethome (*mc), remote_path +3-f );
347 g_free (remote_path);
348 remote_path = s;
351 return remote_path;
354 /* Simple function for routines returning only an integer from the server */
355 static int mcfs_handle_simple_error (int sock, int return_status)
357 int status, error;
359 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
360 return the_error (-1, EIO);
362 if (is_error (status, error))
363 return -1;
364 if (return_status)
365 return status;
366 return 0;
369 /* Nice wrappers */
370 static int mcfs_rpc_two_paths (int command, char *s1, char *s2)
372 mcfs_connection *mc;
373 char *r1, *r2;
375 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
376 return -1;
378 if ((r2 = mcfs_get_path (&mc, s2)) == 0){
379 g_free (r1);
380 return -1;
383 rpc_send (mc->sock,
384 RPC_INT, command,
385 RPC_STRING, r1,
386 RPC_STRING, r2,
387 RPC_END);
388 g_free (r1);
389 g_free (r2);
390 return mcfs_handle_simple_error (mc->sock, 0);
393 static int mcfs_rpc_path (int command, char *path)
395 mcfs_connection *mc;
396 char *remote_file;
398 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
399 return -1;
401 rpc_send (mc->sock,
402 RPC_INT, command,
403 RPC_STRING, remote_file,
404 RPC_END);
406 g_free (remote_file);
407 return mcfs_handle_simple_error (mc->sock, 0);
410 static int mcfs_rpc_path_int (int command, char *path, int data)
412 mcfs_connection *mc;
413 char *remote_file;
415 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
416 return -1;
418 rpc_send (mc->sock,
419 RPC_INT, command,
420 RPC_STRING, remote_file,
421 RPC_INT, data, RPC_END);
423 g_free (remote_file);
424 return mcfs_handle_simple_error (mc->sock, 0);
427 static int mcfs_rpc_path_int_int (int command, char *path, int n1, int n2)
429 mcfs_connection *mc;
430 char *remote_file;
432 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
433 return -1;
435 rpc_send (mc->sock,
436 RPC_INT, command,
437 RPC_STRING, remote_file,
438 RPC_INT, n1,
439 RPC_INT, n2,
440 RPC_END);
442 g_free (remote_file);
443 return mcfs_handle_simple_error (mc->sock, 0);
446 static char *mcfs_gethome (mcfs_connection *mc)
448 char *buffer;
450 if (mc->home)
451 return g_strdup (mc->home);
452 else {
453 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
454 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
455 return g_strdup (PATH_SEP_STR);
456 mc->home = buffer;
457 return g_strdup (buffer);
461 /* The callbacks */
462 static void *mcfs_open (vfs *me, char *file, int flags, int mode)
464 char *remote_file;
465 mcfs_connection *mc;
466 int result, error_num;
467 mcfs_handle *remote_handle;
469 if (!(remote_file = mcfs_get_path (&mc, file)))
470 return 0;
472 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file,
473 RPC_INT, flags, RPC_INT, mode, RPC_END);
474 g_free (remote_file);
476 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
477 return 0;
479 if (is_error (result, error_num))
480 return 0;
482 remote_handle = g_new (mcfs_handle, 2);
483 remote_handle->handle = result;
484 remote_handle->conn = mc;
486 return remote_handle;
489 static int mcfs_read (void *data, char *buffer, int count)
491 mcfs_handle *info = (mcfs_handle *) data;
492 int result, error;
493 int handle;
494 mcfs_connection *mc;
496 mc = info->conn;
497 handle = info->handle;
499 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
500 RPC_INT, count, RPC_END);
502 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
503 return the_error (-1, EIO);
505 if (is_error (result, error))
506 return 0;
508 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
509 return the_error (-1, EIO);
511 return result;
514 static int mcfs_write (void *data, char *buf, int nbyte)
516 mcfs_handle *info = (mcfs_handle *) data;
517 mcfs_connection *mc;
518 int handle;
520 mc = info->conn;
521 handle = info->handle;
523 rpc_send (mc->sock,
524 RPC_INT, MC_WRITE,
525 RPC_INT, handle,
526 RPC_INT, nbyte,
527 RPC_BLOCK, nbyte, buf,
528 RPC_END);
530 return mcfs_handle_simple_error (mc->sock, 1);
533 static int mcfs_close (void *data)
535 mcfs_handle *info = (mcfs_handle *) data;
536 mcfs_connection *mc;
537 int handle, result, error;
539 if (!data)
540 return -1;
542 handle = info->handle;
543 mc = info->conn;
545 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
547 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
548 return the_error (-1, EIO);
550 is_error (result, error);
552 g_free (data);
553 return result;
556 static int mcfs_errno (vfs *me)
558 return my_errno;
561 typedef struct dir_entry {
562 char *text;
563 struct dir_entry *next;
564 struct stat my_stat;
565 int merrno;
566 } dir_entry;
568 typedef struct {
569 mcfs_connection *conn;
570 int handle;
571 dir_entry *entries;
572 dir_entry *current;
573 } opendir_info;
575 static void *mcfs_opendir (vfs *me, char *dirname)
577 opendir_info *mcfs_info;
578 mcfs_connection *mc;
579 int handle, error_num;
580 char *remote_dir;
581 int result;
583 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
584 return 0;
586 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir, RPC_END);
587 g_free (remote_dir);
589 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
590 return 0;
592 if (is_error (result, error_num))
593 return 0;
595 handle = result;
597 mcfs_info = g_new (opendir_info, 1);
598 mcfs_info->conn = mc;
599 mcfs_info->handle = handle;
600 mcfs_info->entries = 0;
601 mcfs_info->current = 0;
603 return mcfs_info;
606 static int get_stat_info (mcfs_connection *mc, struct stat *buf);
608 static int mcfs_loaddir (opendir_info *mcfs_info)
610 int status, error;
611 mcfs_connection *mc = mcfs_info->conn;
612 int link = mc->sock;
613 int first = 1;
615 rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle, RPC_END);
617 for (;;){
618 int entry_len;
619 dir_entry *new_entry;
621 if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
622 return 0;
624 if (entry_len == 0)
625 break;
627 new_entry = g_new (dir_entry, 1);
628 new_entry->text = g_new0 (char, entry_len + 1);
630 new_entry->next = 0;
631 if (first){
632 mcfs_info->entries = new_entry;
633 mcfs_info->current = new_entry;
634 first = 0;
635 } else {
636 mcfs_info->current->next = new_entry;
637 mcfs_info->current = new_entry;
640 if (!rpc_get (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
641 return 0;
643 /* Then we get the status from the lstat */
644 if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
645 return 0;
647 if (is_error (status, error))
648 new_entry->merrno = error;
649 else {
650 new_entry->merrno = 0;
651 if (!get_stat_info (mc, &(new_entry->my_stat)))
652 return 0;
655 mcfs_info->current = mcfs_info->entries;
657 return 1;
660 static void mcfs_free_dir (dir_entry *de)
662 if (!de)
663 return;
664 mcfs_free_dir (de->next);
665 g_free (de->text);
666 g_free (de);
669 static union vfs_dirent mcfs_readdir_data;
671 /* The readdir routine loads the complete directory */
672 /* It's too slow to ask the server each time */
673 /* It now also sends the complete lstat information for each file */
674 static struct stat *cached_lstat_info;
676 static void *mcfs_readdir(void *info)
678 opendir_info *mcfs_info;
679 char *dirent_dest;
681 mcfs_info = (opendir_info *) info;
683 if (!mcfs_info->entries)
684 if (!mcfs_loaddir(mcfs_info))
685 return NULL;
687 if (mcfs_info->current == 0) {
688 cached_lstat_info = 0;
689 mcfs_free_dir(mcfs_info->entries);
690 mcfs_info->entries = 0;
691 return NULL;
693 dirent_dest = mcfs_readdir_data.dent.d_name;
694 strncpy(dirent_dest, mcfs_info->current->text, MC_MAXPATHLEN);
695 dirent_dest[MC_MAXPATHLEN] = 0;
696 cached_lstat_info = &mcfs_info->current->my_stat;
697 mcfs_info->current = mcfs_info->current->next;
699 compute_namelen(&mcfs_readdir_data.dent);
701 return &mcfs_readdir_data;
704 static int mcfs_closedir (void *info)
706 opendir_info *mcfs_info = (opendir_info *) info;
707 dir_entry *p, *q;
709 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
710 RPC_INT, mcfs_info->handle, RPC_END);
712 for (p = mcfs_info->entries; p;){
713 q = p;
714 p = p->next;
715 g_free (q->text);
716 g_free (q);
718 g_free (info);
719 return 0;
722 static time_t mcfs_get_time (mcfs_connection *mc)
724 int sock = mc->sock;
726 if (mc->version == 1) {
727 struct tm tt;
729 rpc_get (sock,
730 RPC_INT, &tt.tm_sec,
731 RPC_INT, &tt.tm_min,
732 RPC_INT, &tt.tm_hour,
733 RPC_INT, &tt.tm_mday,
734 RPC_INT, &tt.tm_year,
735 RPC_INT, &tt.tm_mon,
736 RPC_END);
737 tt.tm_year -= 1900;
738 tt.tm_isdst = 0;
740 return mktime (&tt);
741 } else {
742 char *buf;
743 long tm;
745 rpc_get (sock,
746 RPC_STRING, &buf,
747 RPC_END);
748 sscanf (buf, "%lx", &tm);
749 g_free (buf);
751 return (time_t) tm;
755 static int get_stat_info (mcfs_connection *mc, struct stat *buf)
757 long mylong;
758 int sock = mc->sock;
760 buf->st_dev = 0;
762 rpc_get (sock, RPC_INT, &mylong, RPC_END);
763 #ifdef HAVE_ST_RDEV
764 buf->st_rdev = mylong;
765 #endif
766 rpc_get (sock, RPC_INT, &mylong, RPC_END);
767 buf->st_ino = mylong;
768 rpc_get (sock, RPC_INT, &mylong, RPC_END);
769 buf->st_mode = mylong;
770 rpc_get (sock, RPC_INT, &mylong, RPC_END);
771 buf->st_nlink = mylong;
772 rpc_get (sock, RPC_INT, &mylong, RPC_END);
773 buf->st_uid = mylong;
774 rpc_get (sock, RPC_INT, &mylong, RPC_END);
775 buf->st_gid = mylong;
776 rpc_get (sock, RPC_INT, &mylong, RPC_END);
777 buf->st_size = mylong;
779 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
780 return 0;
781 #ifdef HAVE_ST_BLOCKS
782 buf->st_blocks = mylong;
783 #endif
784 buf->st_atime = mcfs_get_time (mc);
785 buf->st_mtime = mcfs_get_time (mc);
786 buf->st_ctime = mcfs_get_time (mc);
787 return 1;
790 static int mcfs_stat_cmd (int cmd, char *path, struct stat *buf)
792 char *remote_file;
793 mcfs_connection *mc;
794 int status, error;
796 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
797 return -1;
799 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
800 g_free (remote_file);
801 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
802 return the_error (-1, errno);
804 if (is_error (status, error))
805 return -1;
807 if (get_stat_info (mc, buf))
808 return 0;
809 else
810 return the_error (-1, EIO);
813 static int mcfs_stat (vfs *me, char *path, struct stat *buf)
815 return mcfs_stat_cmd (MC_STAT, path, buf);
818 static int mcfs_lstat (vfs *me, char *path, struct stat *buf)
820 int path_len = strlen (path);
821 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
823 /* Hack ... */
824 if (strcmp (path + path_len - entry_len,
825 mcfs_readdir_data.dent.d_name) == 0 &&
826 cached_lstat_info){
827 *buf = *cached_lstat_info;
828 return 0;
830 return mcfs_stat_cmd (MC_LSTAT, path, buf);
833 static int mcfs_fstat (void *data, struct stat *buf)
835 mcfs_handle *info = (mcfs_handle *) data;
836 int result, error;
837 int handle, sock;
839 sock = info->conn->sock;
840 handle = info->handle;
842 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
843 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
844 return the_error (-1, EIO);
846 if (is_error (result, error))
847 return -1;
849 if (get_stat_info (info->conn, buf))
850 return 0;
851 else
852 return the_error (-1, EIO);
855 static int mcfs_chmod (vfs *me, char *path, int mode)
857 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
860 static int mcfs_chown (vfs *me, char *path, int owner, int group)
862 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
865 static int mcfs_utime (vfs *me, char *path, struct utimbuf *times)
867 mcfs_connection *mc;
868 int status;
869 char *file;
871 if (!(file = mcfs_get_path (&mc, path)))
872 return -1;
874 status = 0;
875 if (mc->version >= 2) {
876 char abuf[BUF_SMALL];
877 char mbuf[BUF_SMALL];
878 long atime, mtime;
880 atime = (long) times->actime;
881 mtime = (long) times->modtime;
883 g_snprintf (abuf, sizeof(abuf), "%lx", atime);
884 g_snprintf (mbuf, sizeof(mbuf), "%lx", mtime);
886 rpc_send (mc->sock, RPC_INT, MC_UTIME,
887 RPC_STRING, file,
888 RPC_STRING, abuf,
889 RPC_STRING, mbuf,
890 RPC_END);
891 status = mcfs_handle_simple_error (mc->sock, 0);
894 g_free (file);
895 return (status);
898 static int mcfs_readlink (vfs *me, char *path, char *buf, int size)
900 char *remote_file, *stat_str;
901 int status, error;
902 mcfs_connection *mc;
904 if (!(remote_file = mcfs_get_path (&mc, path)))
905 return -1;
907 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file, RPC_END);
908 g_free (remote_file);
909 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
910 return the_error (-1, EIO);
912 if (is_error (status, errno))
913 return -1;
915 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
916 return the_error (-1, EIO);
918 strncpy (buf, stat_str, size);
919 g_free (stat_str);
920 return strlen (buf);
923 static int mcfs_unlink (vfs *me, char *path)
925 return mcfs_rpc_path (MC_UNLINK, path);
928 static int mcfs_symlink (vfs *me, char *n1, char *n2)
930 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
933 static int mcfs_rename (vfs *me, char *a, char *b)
935 return mcfs_rpc_two_paths (MC_RENAME, a, b);
938 static int mcfs_chdir (vfs *me, char *path)
940 char *remote_dir;
941 mcfs_connection *mc;
942 int status, error;
944 if (!(remote_dir = mcfs_get_path (&mc, path)))
945 return -1;
947 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir, RPC_END);
948 g_free (remote_dir);
949 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
950 return the_error (-1, EIO);
952 if (is_error (status, error))
953 return -1;
954 return 0;
957 static int mcfs_lseek (void *data, off_t offset, int whence)
959 mcfs_handle *info = (mcfs_handle *) data;
960 int handle, sock;
962 sock = info->conn->sock;
963 handle = info->handle;
965 rpc_send (sock,
966 RPC_INT, MC_LSEEK,
967 RPC_INT, handle,
968 RPC_INT, offset,
969 RPC_INT, whence,
970 RPC_END);
971 return mcfs_handle_simple_error (sock, 1);
974 static int mcfs_mknod (vfs *me, char *path, int mode, int dev)
976 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
979 static int mcfs_mkdir (vfs *me, char *path, mode_t mode)
981 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
984 static int mcfs_rmdir (vfs *me, char *path)
986 return mcfs_rpc_path (MC_RMDIR, path);
989 static int mcfs_link (vfs *me, char *p1, char *p2)
991 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
994 /* We do not free anything right now: we free resources when we run
995 * out of them
997 static vfsid mcfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
999 *parent = NULL;
1001 return (vfsid) -1;
1004 static int mcfs_nothingisopen (vfsid id)
1006 return 0;
1009 static void mcfs_free (vfsid id)
1011 /* FIXME: Should not be empty */
1014 /* Gives up on a socket and reopnes the connection, the child own the socket
1015 * now
1017 static void
1018 my_forget (char *path)
1020 char *host, *user, *pass, *p;
1021 int port, i, vers;
1023 if (strncmp (path, "/#mc:", 5))
1024 return;
1026 path += 5;
1027 if (path[0] == '/' && path[1] == '/')
1028 path += 2;
1030 if ((p = mcfs_get_host_and_username (path, &host, &user, &port, &pass)) == 0) {
1031 g_free (host);
1032 g_free (user);
1033 if (pass)
1034 wipe_password (pass);
1035 return;
1037 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
1038 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
1039 (strcmp (user, mcfs_connections [i].user) == 0) &&
1040 (port == mcfs_connections [i].port)){
1042 /* close socket: the child owns it now */
1043 close (mcfs_connections [i].sock);
1045 /* reopen the connection */
1046 mcfs_connections [i].sock = mcfs_open_tcp_link (host, user, &port, pass, &vers);
1049 g_free (p);
1050 g_free (host);
1051 g_free (user);
1052 if (pass)
1053 wipe_password (pass);
1056 static int
1057 mcfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1059 switch (ctlop) {
1060 case MCCTL_FORGET_ABOUT:
1061 my_forget(path);
1062 return 0;
1064 return 0;
1067 vfs vfs_mcfs_ops = {
1068 NULL, /* This is place of next pointer */
1069 "mcfs",
1070 F_NET, /* flags */
1071 "mc:", /* prefix */
1072 NULL, /* data */
1073 0, /* errno */
1074 NULL,
1075 NULL,
1076 mcfs_fill_names,
1077 NULL,
1079 mcfs_open,
1080 mcfs_close,
1081 mcfs_read,
1082 mcfs_write,
1084 mcfs_opendir,
1085 mcfs_readdir,
1086 mcfs_closedir,
1087 NULL,
1088 NULL,
1090 mcfs_stat,
1091 mcfs_lstat,
1092 mcfs_fstat,
1094 mcfs_chmod,
1095 mcfs_chown,
1096 mcfs_utime,
1098 mcfs_readlink,
1099 mcfs_symlink,
1100 mcfs_link,
1101 mcfs_unlink,
1103 mcfs_rename,
1104 mcfs_chdir,
1105 mcfs_errno,
1106 mcfs_lseek,
1107 mcfs_mknod,
1109 mcfs_getid,
1110 mcfs_nothingisopen,
1111 mcfs_free,
1113 NULL,
1114 NULL,
1116 mcfs_mkdir,
1117 mcfs_rmdir,
1118 NULL,
1119 mcfs_setctl
1121 MMAPNULL
1125 /* FIXME: This part should go to another c module, perhaps tcp.c */
1127 static void mcfs_free_bucket (int bucket)
1129 g_free (mcfs_connections [bucket].host);
1130 g_free (mcfs_connections [bucket].user);
1131 g_free (mcfs_connections [bucket].home);
1133 /* Set all the fields to zero */
1134 mcfs_connections [bucket].host =
1135 mcfs_connections [bucket].user =
1136 mcfs_connections [bucket].home = 0;
1137 mcfs_connections [bucket].sock =
1138 mcfs_connections [bucket].version = 0;
1141 static int mcfs_invalidate_socket (int sock)
1143 int i, j = -1;
1144 extern int mc_chdir (char *);
1146 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
1147 if (mcfs_connections [i].sock == sock) {
1148 mcfs_free_bucket (i);
1149 j = 0;
1152 if (j == -1)
1153 return -1; /* It was not our sock */
1154 /* Break from any possible loop */
1155 mc_chdir ("/");
1156 return 0;
1159 void tcp_invalidate_socket (int sock)
1161 mcfs_invalidate_socket (sock);
1163 /* FIXME end: 'cause it is used not only by mcfs */
1165 #endif /* WITH_MCFS */