Traslated some chmod mgs related (for win32 support? not shure)
[midnight-commander.git] / vfs / mcfs.c
blob664b5bbc2545201f48be0c61e2080962d6b1c7e0
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>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <fcntl.h>
35 #include <pwd.h>
36 #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
37 #include <netdb.h> /* struct hostent */
38 #include <sys/socket.h> /* AF_INET */
39 #include <netinet/in.h> /* struct in_addr */
40 #include <arpa/inet.h>
42 #ifdef USE_TERMNET
43 #include <termnet.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
54 #define mcserver_port 9876
56 static struct _mcfs_connection {
57 char *host;
58 char *user;
59 char *home;
60 int sock;
61 int port;
62 int version;
63 } mcfs_connections [MCFS_MAX_CONNECTIONS];
65 typedef struct _mcfs_connection mcfs_connection;
67 typedef struct { int handle; mcfs_connection *conn; } mcfs_handle;
69 static int my_errno;
71 static char *mcfs_gethome (mcfs_connection *mc);
73 /* Extract the hostname and username from the path */
74 /* path is in the form: hostname:user/remote-dir */
75 static char *mcfs_get_host_and_username (char *path, char **host, char **user,
76 int *port, char **pass)
78 return vfs_split_url (path, host, user, port, pass, 0, 0);
81 static void mcfs_fill_names (vfs *me, void (*func)(char *))
83 int i;
84 char *name;
86 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
87 if (mcfs_connections [i].host == 0)
88 continue;
89 name = g_strconcat ("/#mc:", mcfs_connections [i].user,
90 "@", mcfs_connections [i].host, NULL);
91 (*func) (name);
92 g_free (name);
96 static void mcfs_free_bucket (int bucket)
98 g_free (mcfs_connections [bucket].host);
99 g_free (mcfs_connections [bucket].user);
100 g_free (mcfs_connections [bucket].home);
102 /* Set all the fields to zero */
103 mcfs_connections [bucket].host =
104 mcfs_connections [bucket].user =
105 mcfs_connections [bucket].home = 0;
106 mcfs_connections [bucket].sock =
107 mcfs_connections [bucket].version = 0;
110 /* FIXME: This part should go to another c module, perhaps tcp.c */
111 static int mcfs_invalidate_socket (int);
113 void tcp_invalidate_socket (int sock)
115 mcfs_invalidate_socket (sock);
117 /* FIXME end: 'cause it is used not only by mcfs */
119 static int mcfs_invalidate_socket (int sock)
121 int i, j = -1;
122 extern int mc_chdir (char *);
124 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
125 if (mcfs_connections [i].sock == sock) {
126 mcfs_free_bucket (i);
127 j = 0;
130 if (j == -1)
131 return -1; /* It was not our sock */
132 /* Break from any possible loop */
133 mc_chdir ("/");
134 return 0;
137 /* This routine checks the server RPC version and logs the user in */
138 static int mcfs_login_server (int my_socket, char *user, int port,
139 int port_autodetected, char *netrcpass,
140 int *version)
142 int result;
143 char *pass;
145 /* Send the version number */
146 rpc_send (my_socket, RPC_INT, *version, RPC_END);
147 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
148 return 0;
150 if (result != MC_VERSION_OK){
151 message_1s (1, _(" MCFS "), _(" The server does not support this version "));
152 close (my_socket);
153 return 0;
156 /* FIXME: figure out why last_current_dir used to be passed here */
157 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
158 RPC_STRING, user, RPC_END);
160 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
161 return 0;
163 if (result == MC_NEED_PASSWORD){
164 if (port > 1024 && port_autodetected){
165 int v;
166 #ifndef VFS_STANDALONE
167 v = query_dialog (_(" Warning "),
168 _(" The remote server is not running on a system port \n"
169 " you need a password to log in, but the information may \n"
170 " not be safe on the remote side. Continue? \n"), 3, 2,
171 _(" Yes "), _(" No "));
172 #else
173 message_1s( 1, _(" MCFS "), _(" The remote server is running on strange port. Giving up.\n"));
174 v = 1;
175 #endif
177 if (v == 1){
178 close (my_socket);
179 return 0;
182 if (netrcpass != NULL)
183 pass = g_strdup (netrcpass);
184 else
185 pass = vfs_get_password (_(" MCFS Password required "));
186 if (!pass){
187 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
188 close (my_socket);
189 return 0;
191 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
193 wipe_password (pass);
195 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
196 return 0;
198 if (result != MC_LOGINOK){
199 message_1s (1, _(" MCFS "), _(" Invalid password "));
200 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
201 close (my_socket);
202 return 0;
205 return my_socket;
208 /* This used to be in utilvfs.c, but as it deals with portmapper, it
209 is probably usefull for mcfs */
211 open_tcp_link (char *host, int *port, int *version, char *caller)
213 struct sockaddr_in server_address;
214 unsigned long inaddr;
215 struct hostent *hp;
216 int my_socket;
218 if (!*host)
219 return 0;
221 bzero ((char *) &server_address, sizeof (server_address));
222 server_address.sin_family = AF_INET;
224 /* Try to use the dotted decimal number */
225 if ((inaddr = inet_addr (host)) != -1)
226 bcopy ((char *) &inaddr, (char *) &server_address.sin_addr,
227 sizeof (inaddr));
228 else {
229 if ((hp = gethostbyname (host)) == NULL){
230 message_2s (1, caller, _(" Cannot locate hostname: %s "), host);
231 return 0;
233 bcopy ((char *) hp->h_addr, (char *) &server_address.sin_addr,
234 hp->h_length);
237 /* Try to contact a remote portmapper to obtain the listening port */
238 if (*port == 0){
239 *port = get_remote_port (&server_address, version);
240 if (*port < 1)
241 return 0;
242 } else
243 *version = 1;
245 server_address.sin_port = htons (*port);
247 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
248 message_2s (1, caller, _(" Cannot create socket: %s "),
249 unix_error_string(errno));
250 return 0;
252 if (connect (my_socket, (struct sockaddr *) &server_address,
253 sizeof (server_address)) < 0){
254 message_2s (1, caller, _(" Cannot connect to server: %s "),
255 unix_error_string (errno));
256 close (my_socket);
257 return 0;
259 return my_socket;
262 static int mcfs_open_tcp_link (char *host, char *user,
263 int *port, char *netrcpass, int *version)
265 int my_socket;
266 int old_port = *port;
268 my_socket = open_tcp_link (host, port, version, " MCfs ");
269 if (my_socket <= 0)
270 return 0;
272 /* We got the connection to the server, verify if the server
273 implements our version of the RPC mechanism and then login
274 the user.
276 return mcfs_login_server (my_socket, user, *port, old_port == 0,
277 netrcpass, version);
280 static int mcfs_get_free_bucket_init = 1;
281 static mcfs_connection *mcfs_get_free_bucket (void)
283 int i;
285 if (mcfs_get_free_bucket_init) {
286 mcfs_get_free_bucket_init = 0;
287 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
288 mcfs_connections [i].host = 0;
290 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
291 if (!mcfs_connections [i].host)
292 return &mcfs_connections [i];
294 /* This can't happend, since we have checked for max connections before */
295 vfs_die("Internal error: mcfs_get_free_bucket");
296 return 0; /* shut up, stupid gcc */
299 /* This routine keeps track of open connections */
300 /* Returns a connected socket to host */
301 static mcfs_connection *mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
303 static int mcfs_open_connections = 0;
304 int i, sock, version;
305 mcfs_connection *bucket;
307 /* Is the link actually open? */
308 if (mcfs_get_free_bucket_init) {
309 mcfs_get_free_bucket_init = 0;
310 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
311 mcfs_connections [i].host = 0;
312 } else for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
313 if (!mcfs_connections [i].host)
314 continue;
315 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
316 (strcmp (user, mcfs_connections [i].user) == 0))
317 return &mcfs_connections [i];
319 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS){
320 message_1s (1, MSG_ERROR, _(" Too many open connections "));
321 return 0;
324 if (!(sock = mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
325 return 0;
327 bucket = mcfs_get_free_bucket ();
328 mcfs_open_connections++;
329 bucket->host = g_strdup (host);
330 bucket->user = g_strdup (user);
331 bucket->home = 0;
332 bucket->port = *port;
333 bucket->sock = sock;
334 bucket->version = version;
336 return bucket;
339 static int is_error (int result, int errno_num)
341 if (!(result == -1))
342 return my_errno = 0;
343 else
344 my_errno = errno_num;
345 return 1;
348 static int the_error (int result, int errno_num)
350 if (result == -1)
351 my_errno = errno_num;
352 else
353 my_errno = 0;
354 return result;
357 static char *mcfs_get_path (mcfs_connection **mc, char *path)
359 char *user, *host, *remote_path;
360 char *pass;
361 int port;
363 /* An absolute path name, try to determine connection socket */
364 if (strncmp (path, "/#mc:", 5))
365 return NULL;
366 path += 5;
368 /* Port = 0 means that open_tcp_link will try to contact the
369 * remote portmapper to get the port number
371 port = 0;
372 if ((remote_path = mcfs_get_host_and_username(path, &host, &user, &port, &pass)))
373 if (!(*mc = mcfs_open_link (host, user, &port, pass))){
374 g_free (remote_path);
375 remote_path = NULL;
377 g_free (host);
378 g_free (user);
379 if (pass)
380 wipe_password (pass);
382 if (!remote_path)
383 return NULL;
385 /* NOTE: tildes are deprecated. See ftpfs.c */
387 int f = !strcmp( remote_path, "/~" );
388 if (f || !strncmp( remote_path, "/~/", 3 )) {
389 char *s;
390 s = concat_dir_and_file( mcfs_gethome (*mc), remote_path +3-f );
391 g_free (remote_path);
392 remote_path = s;
395 return remote_path;
398 /* Simple function for routines returning only an integer from the server */
399 static int mcfs_handle_simple_error (int sock, int return_status)
401 int status, error;
403 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
404 return the_error (-1, EIO);
406 if (is_error (status, error))
407 return -1;
408 if (return_status)
409 return status;
410 return 0;
413 /* Nice wrappers */
414 static int mcfs_rpc_two_paths (int command, char *s1, char *s2)
416 mcfs_connection *mc;
417 char *r1, *r2;
419 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
420 return -1;
422 if ((r2 = mcfs_get_path (&mc, s2)) == 0){
423 g_free (r1);
424 return -1;
427 rpc_send (mc->sock,
428 RPC_INT, command,
429 RPC_STRING, r1,
430 RPC_STRING, r2,
431 RPC_END);
432 g_free (r1);
433 g_free (r2);
434 return mcfs_handle_simple_error (mc->sock, 0);
437 static int mcfs_rpc_path (int command, char *path)
439 mcfs_connection *mc;
440 char *remote_file;
442 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
443 return -1;
445 rpc_send (mc->sock,
446 RPC_INT, command,
447 RPC_STRING, remote_file,
448 RPC_END);
450 g_free (remote_file);
451 return mcfs_handle_simple_error (mc->sock, 0);
454 static int mcfs_rpc_path_int (int command, char *path, int data)
456 mcfs_connection *mc;
457 char *remote_file;
459 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
460 return -1;
462 rpc_send (mc->sock,
463 RPC_INT, command,
464 RPC_STRING, remote_file,
465 RPC_INT, data, RPC_END);
467 g_free (remote_file);
468 return mcfs_handle_simple_error (mc->sock, 0);
471 static int mcfs_rpc_path_int_int (int command, char *path, int n1, int n2)
473 mcfs_connection *mc;
474 char *remote_file;
476 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
477 return -1;
479 rpc_send (mc->sock,
480 RPC_INT, command,
481 RPC_STRING, remote_file,
482 RPC_INT, n1,
483 RPC_INT, n2,
484 RPC_END);
486 g_free (remote_file);
487 return mcfs_handle_simple_error (mc->sock, 0);
490 static char *mcfs_gethome (mcfs_connection *mc)
492 char *buffer;
494 if (mc->home)
495 return g_strdup (mc->home);
496 else {
497 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
498 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
499 return g_strdup (PATH_SEP_STR);
500 mc->home = buffer;
501 return g_strdup (buffer);
505 /* The callbacks */
506 static void *mcfs_open (vfs *me, char *file, int flags, int mode)
508 char *remote_file;
509 mcfs_connection *mc;
510 int result, error_num;
511 mcfs_handle *remote_handle;
513 if (!(remote_file = mcfs_get_path (&mc, file)))
514 return 0;
516 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file,
517 RPC_INT, flags, RPC_INT, mode, RPC_END);
518 g_free (remote_file);
520 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
521 return 0;
523 if (is_error (result, error_num))
524 return 0;
526 remote_handle = g_new (mcfs_handle, 2);
527 remote_handle->handle = result;
528 remote_handle->conn = mc;
530 return remote_handle;
533 static int mcfs_read (void *data, char *buffer, int count)
535 mcfs_handle *info = (mcfs_handle *) data;
536 int result, error;
537 int handle;
538 mcfs_connection *mc;
540 mc = info->conn;
541 handle = info->handle;
543 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
544 RPC_INT, count, RPC_END);
546 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
547 return the_error (-1, EIO);
549 if (is_error (result, error))
550 return 0;
552 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
553 return the_error (-1, EIO);
555 return result;
558 static int mcfs_write (void *data, char *buf, int nbyte)
560 mcfs_handle *info = (mcfs_handle *) data;
561 mcfs_connection *mc;
562 int handle;
564 mc = info->conn;
565 handle = info->handle;
567 rpc_send (mc->sock,
568 RPC_INT, MC_WRITE,
569 RPC_INT, handle,
570 RPC_INT, nbyte,
571 RPC_BLOCK, nbyte, buf,
572 RPC_END);
574 return mcfs_handle_simple_error (mc->sock, 1);
577 static int mcfs_close (void *data)
579 mcfs_handle *info = (mcfs_handle *) data;
580 mcfs_connection *mc;
581 int handle, result, error;
583 if (!data)
584 return -1;
586 handle = info->handle;
587 mc = info->conn;
589 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
591 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
592 return the_error (-1, EIO);
594 is_error (result, error);
596 g_free (data);
597 return result;
600 static int mcfs_errno (vfs *me)
602 return my_errno;
605 typedef struct dir_entry {
606 char *text;
607 struct dir_entry *next;
608 struct stat my_stat;
609 int merrno;
610 } dir_entry;
612 typedef struct {
613 mcfs_connection *conn;
614 int handle;
615 dir_entry *entries;
616 dir_entry *current;
617 } opendir_info;
619 static void *mcfs_opendir (vfs *me, char *dirname)
621 opendir_info *mcfs_info;
622 mcfs_connection *mc;
623 int handle, error_num;
624 char *remote_dir;
625 int result;
627 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
628 return 0;
630 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir, RPC_END);
631 g_free (remote_dir);
633 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
634 return 0;
636 if (is_error (result, error_num))
637 return 0;
639 handle = result;
641 mcfs_info = g_new (opendir_info, 1);
642 mcfs_info->conn = mc;
643 mcfs_info->handle = handle;
644 mcfs_info->entries = 0;
645 mcfs_info->current = 0;
647 return mcfs_info;
650 static int get_stat_info (mcfs_connection *mc, struct stat *buf);
652 static int mcfs_loaddir (opendir_info *mcfs_info)
654 int status, error;
655 mcfs_connection *mc = mcfs_info->conn;
656 int link = mc->sock;
657 int first = 1;
659 rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle, RPC_END);
661 for (;;){
662 int entry_len;
663 dir_entry *new_entry;
665 if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
666 return 0;
668 if (entry_len == 0)
669 break;
671 new_entry = g_new (dir_entry, 1);
672 new_entry->text = g_new0 (char, entry_len + 1);
674 new_entry->next = 0;
675 if (first){
676 mcfs_info->entries = new_entry;
677 mcfs_info->current = new_entry;
678 first = 0;
679 } else {
680 mcfs_info->current->next = new_entry;
681 mcfs_info->current = new_entry;
684 if (!rpc_get (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
685 return 0;
687 /* Then we get the status from the lstat */
688 if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
689 return 0;
691 if (is_error (status, error))
692 new_entry->merrno = error;
693 else {
694 new_entry->merrno = 0;
695 if (!get_stat_info (mc, &(new_entry->my_stat)))
696 return 0;
699 mcfs_info->current = mcfs_info->entries;
701 return 1;
704 static void mcfs_free_dir (dir_entry *de)
706 if (!de)
707 return;
708 mcfs_free_dir (de->next);
709 g_free (de->text);
710 g_free (de);
713 /* Explanation:
714 * On some operating systems (Slowaris 2 for example)
715 * the d_name member is just a char long (Nice trick that break everything,
716 * so we need to set up some space for the filename.
718 static struct {
719 struct dirent dent;
720 #ifdef NEED_EXTRA_DIRENT_BUFFER
721 char extra_buffer [MC_MAXPATHLEN];
722 #endif
723 } mcfs_readdir_data;
725 /* The readdir routine loads the complete directory */
726 /* It's too slow to ask the server each time */
727 /* It now also sends the complete lstat information for each file */
728 static struct stat *cached_lstat_info;
729 static void *mcfs_readdir (void *info)
731 opendir_info *mcfs_info;
732 char *dirent_dest;
734 mcfs_info = (opendir_info *) info;
736 if (!mcfs_info->entries)
737 if (!mcfs_loaddir (mcfs_info))
738 return NULL;
740 if (mcfs_info->current == 0){
741 cached_lstat_info = 0;
742 mcfs_free_dir (mcfs_info->entries);
743 mcfs_info->entries = 0;
744 return NULL;
746 dirent_dest = &(mcfs_readdir_data.dent.d_name [0]);
747 strcpy (dirent_dest, mcfs_info->current->text);
748 cached_lstat_info = &mcfs_info->current->my_stat;
749 mcfs_info->current = mcfs_info->current->next;
751 #ifndef DIRENT_LENGTH_COMPUTED
752 mcfs_readdir_data.dent.d_namlen = strlen (mcfs_readdir_data.dent.d_name);
753 #endif
755 return &mcfs_readdir_data;
758 static int mcfs_closedir (void *info)
760 opendir_info *mcfs_info = (opendir_info *) info;
761 dir_entry *p, *q;
763 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
764 RPC_INT, mcfs_info->handle, RPC_END);
766 for (p = mcfs_info->entries; p;){
767 q = p;
768 p = p->next;
769 g_free (q->text);
770 g_free (q);
772 g_free (info);
773 return 0;
776 static time_t mcfs_get_time (mcfs_connection *mc)
778 int sock = mc->sock;
780 if (mc->version == 1) {
781 struct tm tt;
783 rpc_get (sock,
784 RPC_INT, &tt.tm_sec,
785 RPC_INT, &tt.tm_min,
786 RPC_INT, &tt.tm_hour,
787 RPC_INT, &tt.tm_mday,
788 RPC_INT, &tt.tm_year,
789 RPC_INT, &tt.tm_mon,
790 RPC_END);
791 tt.tm_year -= 1900;
792 tt.tm_isdst = 0;
794 return mktime (&tt);
795 } else {
796 char *buf;
797 long tm;
799 rpc_get (sock,
800 RPC_STRING, &buf,
801 RPC_END);
802 sscanf (buf, "%lx", &tm);
803 g_free (buf);
805 return (time_t) tm;
809 static int get_stat_info (mcfs_connection *mc, struct stat *buf)
811 long mylong;
812 int sock = mc->sock;
814 buf->st_dev = 0;
816 rpc_get (sock, RPC_INT, &mylong, RPC_END);
817 #ifdef HAVE_ST_RDEV
818 buf->st_rdev = mylong;
819 #endif
820 rpc_get (sock, RPC_INT, &mylong, RPC_END);
821 buf->st_ino = mylong;
822 rpc_get (sock, RPC_INT, &mylong, RPC_END);
823 buf->st_mode = mylong;
824 rpc_get (sock, RPC_INT, &mylong, RPC_END);
825 buf->st_nlink = mylong;
826 rpc_get (sock, RPC_INT, &mylong, RPC_END);
827 buf->st_uid = mylong;
828 rpc_get (sock, RPC_INT, &mylong, RPC_END);
829 buf->st_gid = mylong;
830 rpc_get (sock, RPC_INT, &mylong, RPC_END);
831 buf->st_size = mylong;
833 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
834 return 0;
835 #ifdef HAVE_ST_BLOCKS
836 buf->st_blocks = mylong;
837 #endif
838 buf->st_atime = mcfs_get_time (mc);
839 buf->st_mtime = mcfs_get_time (mc);
840 buf->st_ctime = mcfs_get_time (mc);
841 return 1;
844 static int mcfs_stat_cmd (int cmd, char *path, struct stat *buf)
846 char *remote_file;
847 mcfs_connection *mc;
848 int status, error;
850 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
851 return -1;
853 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
854 g_free (remote_file);
855 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
856 return the_error (-1, errno);
858 if (is_error (status, error))
859 return -1;
861 if (get_stat_info (mc, buf))
862 return 0;
863 else
864 return the_error (-1, EIO);
867 static int mcfs_stat (vfs *me, char *path, struct stat *buf)
869 return mcfs_stat_cmd (MC_STAT, path, buf);
872 static int mcfs_lstat (vfs *me, char *path, struct stat *buf)
874 int path_len = strlen (path);
875 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
877 /* Hack ... */
878 if (strcmp (path + path_len - entry_len,
879 mcfs_readdir_data.dent.d_name) == 0 &&
880 cached_lstat_info){
881 *buf = *cached_lstat_info;
882 return 0;
884 return mcfs_stat_cmd (MC_LSTAT, path, buf);
887 static int mcfs_fstat (void *data, struct stat *buf)
889 mcfs_handle *info = (mcfs_handle *) data;
890 int result, error;
891 int handle, sock;
893 sock = info->conn->sock;
894 handle = info->handle;
896 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
897 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
898 return the_error (-1, EIO);
900 if (is_error (result, error))
901 return -1;
903 if (get_stat_info (info->conn, buf))
904 return 0;
905 else
906 return the_error (-1, EIO);
909 static int mcfs_chmod (vfs *me, char *path, int mode)
911 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
914 static int mcfs_chown (vfs *me, char *path, int owner, int group)
916 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
919 static int mcfs_utime (vfs *me, char *path, struct utimbuf *times)
921 mcfs_connection *mc;
922 int status;
923 char *file;
925 if (!(file = mcfs_get_path (&mc, path)))
926 return -1;
928 status = 0;
929 if (mc->version >= 2) {
930 char abuf[BUF_SMALL];
931 char mbuf[BUF_SMALL];
932 long atime, mtime;
934 atime = (long) times->actime;
935 mtime = (long) times->modtime;
937 g_snprintf (abuf, sizeof(abuf), "%lx", atime);
938 g_snprintf (mbuf, sizeof(mbuf), "%lx", mtime);
940 rpc_send (mc->sock, RPC_INT, MC_UTIME,
941 RPC_STRING, file,
942 RPC_STRING, abuf,
943 RPC_STRING, mbuf,
944 RPC_END);
945 status = mcfs_handle_simple_error (mc->sock, 0);
948 g_free (file);
949 return (status);
952 static int mcfs_readlink (vfs *me, char *path, char *buf, int size)
954 char *remote_file, *stat_str;
955 int status, error;
956 mcfs_connection *mc;
958 if (!(remote_file = mcfs_get_path (&mc, path)))
959 return -1;
961 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file, RPC_END);
962 g_free (remote_file);
963 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
964 return the_error (-1, EIO);
966 if (is_error (status, errno))
967 return -1;
969 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
970 return the_error (-1, EIO);
972 strncpy (buf, stat_str, size);
973 g_free (stat_str);
974 return strlen (buf);
977 static int mcfs_unlink (vfs *me, char *path)
979 return mcfs_rpc_path (MC_UNLINK, path);
982 static int mcfs_symlink (vfs *me, char *n1, char *n2)
984 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
987 static int mcfs_rename (vfs *me, char *a, char *b)
989 return mcfs_rpc_two_paths (MC_RENAME, a, b);
992 static int mcfs_chdir (vfs *me, char *path)
994 char *remote_dir;
995 mcfs_connection *mc;
996 int status, error;
998 if (!(remote_dir = mcfs_get_path (&mc, path)))
999 return -1;
1001 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir, RPC_END);
1002 g_free (remote_dir);
1003 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1004 return the_error (-1, EIO);
1006 if (is_error (status, error))
1007 return -1;
1008 return 0;
1011 static int mcfs_lseek (void *data, off_t offset, int whence)
1013 mcfs_handle *info = (mcfs_handle *) data;
1014 int handle, sock;
1016 sock = info->conn->sock;
1017 handle = info->handle;
1019 rpc_send (sock,
1020 RPC_INT, MC_LSEEK,
1021 RPC_INT, handle,
1022 RPC_INT, offset,
1023 RPC_INT, whence,
1024 RPC_END);
1025 return mcfs_handle_simple_error (sock, 1);
1028 static int mcfs_mknod (vfs *me, char *path, int mode, int dev)
1030 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
1033 static int mcfs_mkdir (vfs *me, char *path, mode_t mode)
1035 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
1038 static int mcfs_rmdir (vfs *me, char *path)
1040 return mcfs_rpc_path (MC_RMDIR, path);
1043 static int mcfs_link (vfs *me, char *p1, char *p2)
1045 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
1048 /* We do not free anything right now: we free resources when we run
1049 * out of them
1051 static vfsid mcfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1053 *parent = NULL;
1055 return (vfsid) -1;
1058 static int mcfs_nothingisopen (vfsid id)
1060 return 0;
1063 static void mcfs_free (vfsid id)
1065 /* FIXME: Should not be empty */
1068 /* Gives up on a socket and reopnes the connection, the child own the socket
1069 * now
1071 static void
1072 my_forget (char *path)
1074 char *host, *user, *pass, *p;
1075 int port, i, vers;
1077 if (strncmp (path, "/#mc:", 5))
1078 return;
1080 path += 5;
1081 if (path[0] == '/' && path[1] == '/')
1082 path += 2;
1084 if ((p = mcfs_get_host_and_username (path, &host, &user, &port, &pass)) == 0) {
1085 g_free (host);
1086 g_free (user);
1087 if (pass)
1088 wipe_password (pass);
1089 return;
1091 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
1092 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
1093 (strcmp (user, mcfs_connections [i].user) == 0) &&
1094 (port == mcfs_connections [i].port)){
1096 /* close socket: the child owns it now */
1097 close (mcfs_connections [i].sock);
1099 /* reopen the connection */
1100 mcfs_connections [i].sock = mcfs_open_tcp_link (host, user, &port, pass, &vers);
1103 g_free (p);
1104 g_free (host);
1105 g_free (user);
1106 if (pass)
1107 wipe_password (pass);
1110 static int
1111 mcfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1113 switch (ctlop) {
1114 case MCCTL_FORGET_ABOUT:
1115 my_forget(path);
1116 return 0;
1118 return 0;
1121 vfs vfs_mcfs_ops = {
1122 NULL, /* This is place of next pointer */
1123 "mcfs",
1124 F_NET, /* flags */
1125 "mc:", /* prefix */
1126 NULL, /* data */
1127 0, /* errno */
1128 NULL,
1129 NULL,
1130 mcfs_fill_names,
1131 NULL,
1133 mcfs_open,
1134 mcfs_close,
1135 mcfs_read,
1136 mcfs_write,
1138 mcfs_opendir,
1139 mcfs_readdir,
1140 mcfs_closedir,
1141 NULL,
1142 NULL,
1144 mcfs_stat,
1145 mcfs_lstat,
1146 mcfs_fstat,
1148 mcfs_chmod,
1149 mcfs_chown,
1150 mcfs_utime,
1152 mcfs_readlink,
1153 mcfs_symlink,
1154 mcfs_link,
1155 mcfs_unlink,
1157 mcfs_rename,
1158 mcfs_chdir,
1159 mcfs_errno,
1160 mcfs_lseek,
1161 mcfs_mknod,
1163 mcfs_getid,
1164 mcfs_nothingisopen,
1165 mcfs_free,
1167 NULL,
1168 NULL,
1170 mcfs_mkdir,
1171 mcfs_rmdir,
1172 NULL,
1173 mcfs_setctl
1175 MMAPNULL