* POTFILES.in: add gnome/gcustom-layout.c file
[midnight-commander.git] / vfs / mcfs.c
blobfd5c422f5cba6f747f3e375c0529e9efa6e42c57
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 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
23 /* Namespace: exports mcfs_vfs_ops, tcp_invalidate_socket */
25 #include <config.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdarg.h>
32 #include <fcntl.h>
33 #include <pwd.h>
34 #include <sys/types.h> /* POSIX-required by sys/socket.h and netdb.h */
35 #include <netdb.h> /* struct hostent */
36 #include <sys/socket.h> /* AF_INET */
37 #include <netinet/in.h> /* struct in_addr */
38 #include <arpa/inet.h>
39 #ifdef SCO_FLAVOR
40 #include <sys/timeb.h> /* alex: for struct timeb definition */
41 #endif /* SCO_FLAVOR */
42 #include <time.h>
44 #ifdef USE_TERMNET
45 #include <termnet.h>
46 #endif
48 #include "utilvfs.h"
50 #include "vfs.h"
51 #include "mcfs.h"
52 #include "tcputil.h"
53 #include "../src/dialog.h"
55 #define MCFS_MAX_CONNECTIONS 32
56 #define mcserver_port 9876
58 static mcfs_open_connections = 0;
59 static struct _mcfs_connection {
60 char *host;
61 char *user;
62 char *home;
63 int sock;
64 int port;
65 int version;
66 } mcfs_connections [MCFS_MAX_CONNECTIONS];
68 typedef struct _mcfs_connection mcfs_connection;
70 typedef struct { int handle; mcfs_connection *conn; } mcfs_handle;
72 static int my_errno;
74 static char *mcfs_gethome (mcfs_connection *mc);
76 /* Extract the hostname and username from the path */
77 /* path is in the form: hostname:user/remote-dir */
78 static char *mcfs_get_host_and_username (char *path, char **host, char **user,
79 int *port, char **pass)
81 return vfs_split_url (path, host, user, port, pass, 0, 0);
84 static void mcfs_fill_names (vfs *me, void (*func)(char *))
86 int i;
87 char *name;
89 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
90 if (mcfs_connections [i].host == 0)
91 continue;
92 name = g_strconcat ("/#mc:", mcfs_connections [i].user,
93 "@", mcfs_connections [i].host, NULL);
94 (*func) (name);
95 g_free (name);
99 static void mcfs_free_bucket (int bucket)
101 g_free (mcfs_connections [bucket].host);
102 g_free (mcfs_connections [bucket].user);
103 g_free (mcfs_connections [bucket].home);
105 /* Set all the fields to zero */
106 mcfs_connections [bucket].host =
107 mcfs_connections [bucket].user =
108 mcfs_connections [bucket].home = 0;
109 mcfs_connections [bucket].sock =
110 mcfs_connections [bucket].version = 0;
113 /* FIXME: This part should go to another c module, perhaps tcp.c */
114 static int mcfs_invalidate_socket (int);
116 void tcp_invalidate_socket (int sock)
118 mcfs_invalidate_socket (sock);
120 /* FIXME end: 'cause it is used not only by mcfs */
122 static int mcfs_invalidate_socket (int sock)
124 int i, j = -1;
125 extern int mc_chdir (char *);
127 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
128 if (mcfs_connections [i].sock == sock) {
129 mcfs_free_bucket (i);
130 j = 0;
133 if (j == -1)
134 return -1; /* It was not our sock */
135 /* Break from any possible loop */
136 mc_chdir ("/");
137 return 0;
140 /* This routine checks the server RPC version and logs the user in */
141 static int mcfs_login_server (int my_socket, char *user, int port,
142 int port_autodetected, char *netrcpass,
143 int *version)
145 int result;
146 char *pass;
148 /* Send the version number */
149 rpc_send (my_socket, RPC_INT, *version, RPC_END);
150 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
151 return 0;
153 if (result != MC_VERSION_OK){
154 message_1s (1, _(" MCFS "), _(" The server does not support this version "));
155 close (my_socket);
156 return 0;
159 /* FIXME: figure out why last_current_dir used to be passed here */
160 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
161 RPC_STRING, user, RPC_END);
163 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
164 return 0;
166 if (result == MC_NEED_PASSWORD){
167 if (port > 1024 && port_autodetected){
168 int v;
169 #ifndef VFS_STANDALONE
170 v = query_dialog (_(" Warning "),
171 _(" The remote server is not running on a system port \n"
172 " you need a password to log in, but the information may \n"
173 " not be safe on the remote side. Continue? \n"), 3, 2,
174 _(" Yes "), _(" No "));
175 #else
176 message_1s( 1, " MCFS ", _(" The remote server is running on strange port. Giving up.\n"));
177 v = 1;
178 #endif
180 if (v == 1){
181 close (my_socket);
182 return 0;
185 if (netrcpass != NULL)
186 pass = g_strdup (netrcpass);
187 else
188 pass = vfs_get_password (_(" MCFS Password required "));
189 if (!pass){
190 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
191 close (my_socket);
192 return 0;
194 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
196 wipe_password (pass);
198 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
199 return 0;
201 if (result != MC_LOGINOK){
202 message_1s (1, " MCFS ", _(" Invalid password "));
203 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
204 close (my_socket);
205 return 0;
208 return my_socket;
211 /* This used to be in utilvfs.c, but as it deals with portmapper, it
212 is probably usefull for mcfs */
214 open_tcp_link (char *host, int *port, int *version, char *caller)
216 struct sockaddr_in server_address;
217 unsigned long inaddr;
218 struct hostent *hp;
219 int my_socket;
221 if (!*host)
222 return 0;
224 bzero ((char *) &server_address, sizeof (server_address));
225 server_address.sin_family = AF_INET;
227 /* Try to use the dotted decimal number */
228 if ((inaddr = inet_addr (host)) != -1)
229 bcopy ((char *) &inaddr, (char *) &server_address.sin_addr,
230 sizeof (inaddr));
231 else {
232 if ((hp = gethostbyname (host)) == NULL){
233 message_2s (1, caller, " Can't locate hostname: %s ", host);
234 return 0;
236 bcopy ((char *) hp->h_addr, (char *) &server_address.sin_addr,
237 hp->h_length);
240 /* Try to contact a remote portmapper to obtain the listening port */
241 if (*port == 0){
242 *port = get_remote_port (&server_address, version);
243 if (*port < 1)
244 return 0;
245 } else
246 *version = 1;
248 server_address.sin_port = htons (*port);
250 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
251 message_2s (1, caller, " Can't create socket: %s ",
252 unix_error_string(errno));
253 return 0;
255 if (connect (my_socket, (struct sockaddr *) &server_address,
256 sizeof (server_address)) < 0){
257 message_2s (1, caller, " Can't connect to server: %s ",
258 unix_error_string (errno));
259 close (my_socket);
260 return 0;
262 return my_socket;
265 static int mcfs_open_tcp_link (char *host, char *user,
266 int *port, char *netrcpass, int *version)
268 int my_socket;
269 int old_port = *port;
271 my_socket = open_tcp_link (host, port, version, " MCfs ");
272 if (my_socket <= 0)
273 return 0;
275 /* We got the connection to the server, verify if the server
276 implements our version of the RPC mechanism and then login
277 the user.
279 return mcfs_login_server (my_socket, user, *port, old_port == 0,
280 netrcpass, version);
283 static int mcfs_get_free_bucket_init = 1;
284 static mcfs_connection *mcfs_get_free_bucket ()
286 int i;
288 if (mcfs_get_free_bucket_init) {
289 mcfs_get_free_bucket_init = 0;
290 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
291 mcfs_connections [i].host = 0;
293 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
294 if (!mcfs_connections [i].host)
295 return &mcfs_connections [i];
297 /* This can't happend, since we have checked for max connections before */
298 vfs_die("Internal error: mcfs_get_free_bucket");
299 return 0; /* shut up, stupid gcc */
302 /* This routine keeps track of open connections */
303 /* Returns a connected socket to host */
304 static mcfs_connection *mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
306 int i, sock, version;
307 mcfs_connection *bucket;
309 /* Is the link actually open? */
310 if (mcfs_get_free_bucket_init) {
311 mcfs_get_free_bucket_init = 0;
312 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
313 mcfs_connections [i].host = 0;
314 } else for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
315 if (!mcfs_connections [i].host)
316 continue;
317 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
318 (strcmp (user, mcfs_connections [i].user) == 0))
319 return &mcfs_connections [i];
321 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS){
322 message_1s (1, MSG_ERROR, _(" Too many open connections "));
323 return 0;
326 if (!(sock = mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
327 return 0;
329 bucket = mcfs_get_free_bucket ();
330 mcfs_open_connections++;
331 bucket->host = g_strdup (host);
332 bucket->user = g_strdup (user);
333 bucket->home = 0;
334 bucket->port = *port;
335 bucket->sock = sock;
336 bucket->version = version;
338 return bucket;
341 static int is_error (int result, int errno_num)
343 if (!(result == -1))
344 return my_errno = 0;
345 else
346 my_errno = errno_num;
347 return 1;
350 static int the_error (int result, int errno_num)
352 if (result == -1)
353 my_errno = errno_num;
354 else
355 my_errno = 0;
356 return result;
359 static char *mcfs_get_path (mcfs_connection **mc, char *path)
361 char *user, *host, *remote_path;
362 char *pass;
363 int port;
365 /* An absolute path name, try to determine connection socket */
366 if (strncmp (path, "/#mc:", 5))
367 return NULL;
368 path += 5;
370 /* Port = 0 means that open_tcp_link will try to contact the
371 * remote portmapper to get the port number
373 port = 0;
374 if ((remote_path = mcfs_get_host_and_username(path, &host, &user, &port, &pass)))
375 if (!(*mc = mcfs_open_link (host, user, &port, pass))){
376 g_free (remote_path);
377 remote_path = NULL;
379 g_free (host);
380 g_free (user);
381 if (pass)
382 wipe_password (pass);
384 if (!remote_path)
385 return NULL;
387 /* NOTE: tildes are deprecated. See ftpfs.c */
389 int f = !strcmp( remote_path, "/~" );
390 if (f || !strncmp( remote_path, "/~/", 3 )) {
391 char *s;
392 s = concat_dir_and_file( mcfs_gethome (*mc), remote_path +3-f );
393 g_free (remote_path);
394 remote_path = s;
397 return remote_path;
400 /* Simple function for routines returning only an integer from the server */
401 static int mcfs_handle_simple_error (int sock, int return_status)
403 int status, error;
405 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
406 return the_error (-1, EIO);
408 if (is_error (status, error))
409 return -1;
410 if (return_status)
411 return status;
412 return 0;
415 /* Nice wrappers */
416 static int mcfs_rpc_two_paths (int command, char *s1, char *s2)
418 mcfs_connection *mc;
419 char *r1, *r2;
421 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
422 return -1;
424 if ((r2 = mcfs_get_path (&mc, s2)) == 0){
425 g_free (r1);
426 return -1;
429 rpc_send (mc->sock,
430 RPC_INT, command,
431 RPC_STRING, r1,
432 RPC_STRING, r2,
433 RPC_END);
434 g_free (r1);
435 g_free (r2);
436 return mcfs_handle_simple_error (mc->sock, 0);
439 static int mcfs_rpc_path (int command, char *path)
441 mcfs_connection *mc;
442 char *remote_file;
444 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
445 return -1;
447 rpc_send (mc->sock,
448 RPC_INT, command,
449 RPC_STRING, remote_file,
450 RPC_END);
452 g_free (remote_file);
453 return mcfs_handle_simple_error (mc->sock, 0);
456 static int mcfs_rpc_path_int (int command, char *path, int data)
458 mcfs_connection *mc;
459 char *remote_file;
461 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
462 return -1;
464 rpc_send (mc->sock,
465 RPC_INT, command,
466 RPC_STRING, remote_file,
467 RPC_INT, data, RPC_END);
469 g_free (remote_file);
470 return mcfs_handle_simple_error (mc->sock, 0);
473 static int mcfs_rpc_path_int_int (int command, char *path, int n1, int n2)
475 mcfs_connection *mc;
476 char *remote_file;
478 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
479 return -1;
481 rpc_send (mc->sock,
482 RPC_INT, command,
483 RPC_STRING, remote_file,
484 RPC_INT, n1,
485 RPC_INT, n2,
486 RPC_END);
488 g_free (remote_file);
489 return mcfs_handle_simple_error (mc->sock, 0);
492 static char *mcfs_gethome (mcfs_connection *mc)
494 char *buffer;
496 if (mc->home)
497 return g_strdup (mc->home);
498 else {
499 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
500 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
501 return g_strdup (PATH_SEP_STR);
502 mc->home = buffer;
503 return g_strdup (buffer);
507 /* The callbacks */
508 static void *mcfs_open (vfs *me, char *file, int flags, int mode)
510 char *remote_file;
511 mcfs_connection *mc;
512 int result, error_num;
513 mcfs_handle *remote_handle;
515 if (!(remote_file = mcfs_get_path (&mc, file)))
516 return 0;
518 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file,
519 RPC_INT, flags, RPC_INT, mode, RPC_END);
520 g_free (remote_file);
522 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
523 return 0;
525 if (is_error (result, error_num))
526 return 0;
528 remote_handle = g_new (mcfs_handle, 2);
529 remote_handle->handle = result;
530 remote_handle->conn = mc;
532 return remote_handle;
535 static int mcfs_read (void *data, char *buffer, int count)
537 mcfs_handle *info = (mcfs_handle *) data;
538 int result, error;
539 int handle;
540 mcfs_connection *mc;
542 mc = info->conn;
543 handle = info->handle;
545 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
546 RPC_INT, count, RPC_END);
548 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
549 return the_error (-1, EIO);
551 if (is_error (result, error))
552 return 0;
554 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
555 return the_error (-1, EIO);
557 return result;
560 static int mcfs_write (void *data, char *buf, int nbyte)
562 mcfs_handle *info = (mcfs_handle *) data;
563 mcfs_connection *mc;
564 int handle;
566 mc = info->conn;
567 handle = info->handle;
569 rpc_send (mc->sock,
570 RPC_INT, MC_WRITE,
571 RPC_INT, handle,
572 RPC_INT, nbyte,
573 RPC_BLOCK, nbyte, buf,
574 RPC_END);
576 return mcfs_handle_simple_error (mc->sock, 1);
579 static int mcfs_close (void *data)
581 mcfs_handle *info = (mcfs_handle *) data;
582 mcfs_connection *mc;
583 int handle, result, error;
585 if (!data)
586 return -1;
588 handle = info->handle;
589 mc = info->conn;
591 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
593 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
594 return the_error (-1, EIO);
596 is_error (result, error);
598 g_free (data);
599 return result;
602 static int mcfs_errno (vfs *me)
604 return my_errno;
607 typedef struct dir_entry {
608 char *text;
609 struct dir_entry *next;
610 struct stat my_stat;
611 int merrno;
612 } dir_entry;
614 typedef struct {
615 mcfs_connection *conn;
616 int handle;
617 dir_entry *entries;
618 dir_entry *current;
619 } opendir_info;
621 static void *mcfs_opendir (vfs *me, char *dirname)
623 opendir_info *mcfs_info;
624 mcfs_connection *mc;
625 int handle, error_num;
626 char *remote_dir;
627 int result;
629 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
630 return 0;
632 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir, RPC_END);
633 g_free (remote_dir);
635 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
636 return 0;
638 if (is_error (result, error_num))
639 return 0;
641 handle = result;
643 mcfs_info = g_new (opendir_info, 1);
644 mcfs_info->conn = mc;
645 mcfs_info->handle = handle;
646 mcfs_info->entries = 0;
647 mcfs_info->current = 0;
649 return mcfs_info;
652 static int get_stat_info (mcfs_connection *mc, struct stat *buf);
654 static int mcfs_loaddir (opendir_info *mcfs_info)
656 int status, error;
657 mcfs_connection *mc = mcfs_info->conn;
658 int link = mc->sock;
659 int first = 1;
661 rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle, RPC_END);
663 for (;;){
664 int entry_len;
665 dir_entry *new_entry;
667 if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
668 return 0;
670 if (entry_len == 0)
671 break;
673 new_entry = g_new (dir_entry, 1);
674 new_entry->text = g_new0 (char, entry_len + 1);
676 new_entry->next = 0;
677 if (first){
678 mcfs_info->entries = new_entry;
679 mcfs_info->current = new_entry;
680 first = 0;
681 } else {
682 mcfs_info->current->next = new_entry;
683 mcfs_info->current = new_entry;
686 if (!rpc_get (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
687 return 0;
689 /* Then we get the status from the lstat */
690 if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
691 return 0;
693 if (is_error (status, error))
694 new_entry->merrno = error;
695 else {
696 new_entry->merrno = 0;
697 if (!get_stat_info (mc, &(new_entry->my_stat)))
698 return 0;
701 mcfs_info->current = mcfs_info->entries;
703 return 1;
706 static void mcfs_free_dir (dir_entry *de)
708 if (!de)
709 return;
710 mcfs_free_dir (de->next);
711 g_free (de->text);
712 g_free (de);
715 /* Explanation:
716 * On some operating systems (Slowaris 2 for example)
717 * the d_name member is just a char long (Nice trick that break everything,
718 * so we need to set up some space for the filename.
720 static struct {
721 struct dirent dent;
722 #ifdef NEED_EXTRA_DIRENT_BUFFER
723 char extra_buffer [MC_MAXPATHLEN];
724 #endif
725 } mcfs_readdir_data;
727 /* The readdir routine loads the complete directory */
728 /* It's too slow to ask the server each time */
729 /* It now also sends the complete lstat information for each file */
730 static struct stat *cached_lstat_info;
731 static void *mcfs_readdir (void *info)
733 opendir_info *mcfs_info;
734 char *dirent_dest;
736 mcfs_info = (opendir_info *) info;
738 if (!mcfs_info->entries)
739 if (!mcfs_loaddir (mcfs_info))
740 return NULL;
742 if (mcfs_info->current == 0){
743 cached_lstat_info = 0;
744 mcfs_free_dir (mcfs_info->entries);
745 mcfs_info->entries = 0;
746 return NULL;
748 dirent_dest = &(mcfs_readdir_data.dent.d_name [0]);
749 strcpy (dirent_dest, mcfs_info->current->text);
750 cached_lstat_info = &mcfs_info->current->my_stat;
751 mcfs_info->current = mcfs_info->current->next;
753 #ifndef DIRENT_LENGTH_COMPUTED
754 mcfs_readdir_data.dent.d_namlen = strlen (mcfs_readdir_data.dent.d_name);
755 #endif
757 return &mcfs_readdir_data;
760 static int mcfs_closedir (void *info)
762 opendir_info *mcfs_info = (opendir_info *) info;
763 dir_entry *p, *q;
765 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
766 RPC_INT, mcfs_info->handle, RPC_END);
768 for (p = mcfs_info->entries; p;){
769 q = p;
770 p = p->next;
771 g_free (q->text);
772 g_free (q);
774 g_free (info);
775 return 0;
778 static time_t mcfs_get_time (mcfs_connection *mc)
780 int sock = mc->sock;
782 if (mc->version == 1) {
783 struct tm tt;
785 rpc_get (sock,
786 RPC_INT, &tt.tm_sec,
787 RPC_INT, &tt.tm_min,
788 RPC_INT, &tt.tm_hour,
789 RPC_INT, &tt.tm_mday,
790 RPC_INT, &tt.tm_year,
791 RPC_INT, &tt.tm_mon,
792 RPC_END);
793 tt.tm_year -= 1900;
794 tt.tm_isdst = 0;
796 return mktime (&tt);
797 } else {
798 char *buf;
799 long tm;
801 rpc_get (sock,
802 RPC_STRING, &buf,
803 RPC_END);
804 sscanf (buf, "%lx", &tm);
805 g_free (buf);
807 return (time_t) tm;
811 static int get_stat_info (mcfs_connection *mc, struct stat *buf)
813 long mylong;
814 int sock = mc->sock;
816 buf->st_dev = 0;
818 rpc_get (sock, RPC_INT, &mylong, RPC_END);
819 #ifdef HAVE_ST_RDEV
820 buf->st_rdev = mylong;
821 #endif
822 rpc_get (sock, RPC_INT, &mylong, RPC_END);
823 buf->st_ino = mylong;
824 rpc_get (sock, RPC_INT, &mylong, RPC_END);
825 buf->st_mode = mylong;
826 rpc_get (sock, RPC_INT, &mylong, RPC_END);
827 buf->st_nlink = mylong;
828 rpc_get (sock, RPC_INT, &mylong, RPC_END);
829 buf->st_uid = mylong;
830 rpc_get (sock, RPC_INT, &mylong, RPC_END);
831 buf->st_gid = mylong;
832 rpc_get (sock, RPC_INT, &mylong, RPC_END);
833 buf->st_size = mylong;
835 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
836 return 0;
837 #ifdef HAVE_ST_BLOCKS
838 buf->st_blocks = mylong;
839 #endif
840 buf->st_atime = mcfs_get_time (mc);
841 buf->st_mtime = mcfs_get_time (mc);
842 buf->st_ctime = mcfs_get_time (mc);
843 return 1;
846 static int mcfs_stat_cmd (int cmd, char *path, struct stat *buf)
848 char *remote_file;
849 mcfs_connection *mc;
850 int status, error;
852 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
853 return -1;
855 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
856 g_free (remote_file);
857 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
858 return the_error (-1, errno);
860 if (is_error (status, error))
861 return -1;
863 if (get_stat_info (mc, buf))
864 return 0;
865 else
866 return the_error (-1, EIO);
869 static int mcfs_stat (vfs *me, char *path, struct stat *buf)
871 return mcfs_stat_cmd (MC_STAT, path, buf);
874 static int mcfs_lstat (vfs *me, char *path, struct stat *buf)
876 int path_len = strlen (path);
877 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
879 /* Hack ... */
880 if (strcmp (path + path_len - entry_len,
881 mcfs_readdir_data.dent.d_name) == 0 &&
882 cached_lstat_info){
883 *buf = *cached_lstat_info;
884 return 0;
886 return mcfs_stat_cmd (MC_LSTAT, path, buf);
889 static int mcfs_fstat (void *data, struct stat *buf)
891 mcfs_handle *info = (mcfs_handle *) data;
892 int result, error;
893 int handle, sock;
895 sock = info->conn->sock;
896 handle = info->handle;
898 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
899 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
900 return the_error (-1, EIO);
902 if (is_error (result, error))
903 return -1;
905 if (get_stat_info (info->conn, buf))
906 return 0;
907 else
908 return the_error (-1, EIO);
911 static int mcfs_chmod (vfs *me, char *path, int mode)
913 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
916 static int mcfs_chown (vfs *me, char *path, int owner, int group)
918 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
921 static int mcfs_utime (vfs *me, char *path, struct utimbuf *times)
923 mcfs_connection *mc;
924 int status;
925 char *file;
927 if (!(file = mcfs_get_path (&mc, path)))
928 return -1;
930 status = 0;
931 if (mc->version >= 2) {
932 char abuf[BUF_SMALL];
933 char mbuf[BUF_SMALL];
934 long atime, mtime;
936 atime = (long) times->actime;
937 mtime = (long) times->modtime;
939 g_snprintf (abuf, sizeof(abuf), "%lx", atime);
940 g_snprintf (mbuf, sizeof(mbuf), "%lx", mtime);
942 rpc_send (mc->sock, RPC_INT, MC_UTIME,
943 RPC_STRING, file,
944 RPC_STRING, abuf,
945 RPC_STRING, mbuf,
946 RPC_END);
947 status = mcfs_handle_simple_error (mc->sock, 0);
950 g_free (file);
951 return (status);
954 static int mcfs_readlink (vfs *me, char *path, char *buf, int size)
956 char *remote_file, *stat_str;
957 int status, error;
958 mcfs_connection *mc;
960 if (!(remote_file = mcfs_get_path (&mc, path)))
961 return -1;
963 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file, RPC_END);
964 g_free (remote_file);
965 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
966 return the_error (-1, EIO);
968 if (is_error (status, errno))
969 return -1;
971 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
972 return the_error (-1, EIO);
974 strncpy (buf, stat_str, size);
975 g_free (stat_str);
976 return strlen (buf);
979 static int mcfs_unlink (vfs *me, char *path)
981 return mcfs_rpc_path (MC_UNLINK, path);
984 static int mcfs_symlink (vfs *me, char *n1, char *n2)
986 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
989 static int mcfs_rename (vfs *me, char *a, char *b)
991 return mcfs_rpc_two_paths (MC_RENAME, a, b);
994 static int mcfs_chdir (vfs *me, char *path)
996 char *remote_dir;
997 mcfs_connection *mc;
998 int status, error;
1000 if (!(remote_dir = mcfs_get_path (&mc, path)))
1001 return -1;
1003 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir, RPC_END);
1004 g_free (remote_dir);
1005 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1006 return the_error (-1, EIO);
1008 if (is_error (status, error))
1009 return -1;
1010 return 0;
1013 static int mcfs_lseek (void *data, off_t offset, int whence)
1015 mcfs_handle *info = (mcfs_handle *) data;
1016 int handle, sock;
1018 sock = info->conn->sock;
1019 handle = info->handle;
1021 rpc_send (sock,
1022 RPC_INT, MC_LSEEK,
1023 RPC_INT, handle,
1024 RPC_INT, offset,
1025 RPC_INT, whence,
1026 RPC_END);
1027 return mcfs_handle_simple_error (sock, 1);
1030 static int mcfs_mknod (vfs *me, char *path, int mode, int dev)
1032 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
1035 static int mcfs_mkdir (vfs *me, char *path, mode_t mode)
1037 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
1040 static int mcfs_rmdir (vfs *me, char *path)
1042 return mcfs_rpc_path (MC_RMDIR, path);
1045 static int mcfs_link (vfs *me, char *p1, char *p2)
1047 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
1050 /* We do not free anything right now: we free resources when we run
1051 * out of them
1053 static vfsid mcfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1055 *parent = NULL;
1057 return (vfsid) -1;
1060 static int mcfs_nothingisopen (vfsid id)
1062 return 0;
1065 static void mcfs_free (vfsid id)
1067 /* FIXME: Should not be empty */
1070 /* Gives up on a socket and reopnes the connection, the child own the socket
1071 * now
1073 static void
1074 my_forget (char *path)
1076 char *host, *user, *pass, *p;
1077 int port, i, vers;
1079 if (strncmp (path, "/#mc:", 5))
1080 return;
1082 path += 5;
1083 if (path[0] == '/' && path[1] == '/')
1084 path += 2;
1086 if ((p = mcfs_get_host_and_username (path, &host, &user, &port, &pass)) == 0) {
1087 g_free (host);
1088 g_free (user);
1089 if (pass)
1090 wipe_password (pass);
1091 return;
1093 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
1094 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
1095 (strcmp (user, mcfs_connections [i].user) == 0) &&
1096 (port == mcfs_connections [i].port)){
1098 /* close socket: the child owns it now */
1099 close (mcfs_connections [i].sock);
1101 /* reopen the connection */
1102 mcfs_connections [i].sock = mcfs_open_tcp_link (host, user, &port, pass, &vers);
1105 g_free (p);
1106 g_free (host);
1107 g_free (user);
1108 if (pass)
1109 wipe_password (pass);
1112 static int
1113 mcfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1115 switch (ctlop) {
1116 case MCCTL_FORGET_ABOUT:
1117 my_forget(path);
1118 return 0;
1120 return 0;
1123 vfs vfs_mcfs_ops = {
1124 NULL, /* This is place of next pointer */
1125 "Midnight Commander's private remote filesystem",
1126 F_NET, /* flags */
1127 "mc:", /* prefix */
1128 NULL, /* data */
1129 0, /* errno */
1130 NULL,
1131 NULL,
1132 mcfs_fill_names,
1133 NULL,
1135 mcfs_open,
1136 mcfs_close,
1137 mcfs_read,
1138 mcfs_write,
1140 mcfs_opendir,
1141 mcfs_readdir,
1142 mcfs_closedir,
1143 NULL,
1144 NULL,
1146 mcfs_stat,
1147 mcfs_lstat,
1148 mcfs_fstat,
1150 mcfs_chmod,
1151 mcfs_chown,
1152 mcfs_utime,
1154 mcfs_readlink,
1155 mcfs_symlink,
1156 mcfs_link,
1157 mcfs_unlink,
1159 mcfs_rename,
1160 mcfs_chdir,
1161 mcfs_errno,
1162 mcfs_lseek,
1163 mcfs_mknod,
1165 mcfs_getid,
1166 mcfs_nothingisopen,
1167 mcfs_free,
1169 NULL,
1170 NULL,
1172 mcfs_mkdir,
1173 mcfs_rmdir,
1174 NULL,
1175 mcfs_setctl
1177 MMAPNULL