Added cs to the list of languages
[midnight-commander.git] / vfs / mcfs.c
blob6b3ce7ef16826626dee40fff8888ef1fe3e2641e
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 <netdb.h> /* struct hostent */
35 #include <sys/socket.h> /* AF_INET */
36 #include <netinet/in.h> /* struct in_addr */
37 #include <arpa/inet.h>
38 #ifdef SCO_FLAVOR
39 #include <sys/timeb.h> /* alex: for struct timeb definition */
40 #endif /* SCO_FLAVOR */
41 #include <time.h>
43 #ifdef USE_TERMNET
44 #include <termnet.h>
45 #endif
47 #include "utilvfs.h"
49 #include "vfs.h"
50 #include "mcfs.h"
51 #include "tcputil.h"
52 #include "../src/dialog.h"
54 #define MCFS_MAX_CONNECTIONS 32
55 #define mcserver_port 9876
57 static mcfs_open_connections = 0;
58 static struct _mcfs_connection {
59 char *host;
60 char *user;
61 char *home;
62 int sock;
63 int port;
64 int version;
65 } mcfs_connections [MCFS_MAX_CONNECTIONS];
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 static void mcfs_free_bucket (int bucket)
100 g_free (mcfs_connections [bucket].host);
101 g_free (mcfs_connections [bucket].user);
102 g_free (mcfs_connections [bucket].home);
104 /* Set all the fields to zero */
105 mcfs_connections [bucket].host =
106 mcfs_connections [bucket].user =
107 mcfs_connections [bucket].home = 0;
108 mcfs_connections [bucket].sock =
109 mcfs_connections [bucket].version = 0;
112 /* FIXME: This part should go to another c module, perhaps tcp.c */
113 static int mcfs_invalidate_socket (int);
115 void tcp_invalidate_socket (int sock)
117 mcfs_invalidate_socket (sock);
119 /* FIXME end: 'cause it is used not only by mcfs */
121 static int mcfs_invalidate_socket (int sock)
123 int i, j = -1;
124 extern int mc_chdir (char *);
126 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
127 if (mcfs_connections [i].sock == sock) {
128 mcfs_free_bucket (i);
129 j = 0;
132 if (j == -1)
133 return -1; /* It was not our sock */
134 /* Break from any possible loop */
135 mc_chdir ("/");
136 return 0;
139 /* This routine checks the server RPC version and logs the user in */
140 static int mcfs_login_server (int my_socket, char *user, int port,
141 int port_autodetected, char *netrcpass,
142 int *version)
144 int result;
145 char *pass;
147 /* Send the version number */
148 rpc_send (my_socket, RPC_INT, *version, RPC_END);
149 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
150 return 0;
152 if (result != MC_VERSION_OK){
153 message_1s (1, _(" MCFS "), _(" The server does not support this version "));
154 close (my_socket);
155 return 0;
158 /* FIXME: figure out why last_current_dir used to be passed here */
159 rpc_send (my_socket, RPC_INT, MC_LOGIN, RPC_STRING, "/",
160 RPC_STRING, user, RPC_END);
162 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
163 return 0;
165 if (result == MC_NEED_PASSWORD){
166 if (port > 1024 && port_autodetected){
167 int v;
168 #ifndef VFS_STANDALONE
169 v = query_dialog (_(" Warning "),
170 _(" The remote server is not running on a system port \n"
171 " you need a password to log in, but the information may \n"
172 " not be safe on the remote side. Continue? \n"), 3, 2,
173 _(" Yes "), _(" No "));
174 #else
175 message_1s( 1, " MCFS ", _(" The remote server is running on strange port. Giving up.\n"));
176 v = 1;
177 #endif
179 if (v == 1){
180 close (my_socket);
181 return 0;
184 if (netrcpass != NULL)
185 pass = g_strdup (netrcpass);
186 else
187 pass = vfs_get_password (_(" MCFS Password required "));
188 if (!pass){
189 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
190 close (my_socket);
191 return 0;
193 rpc_send (my_socket, RPC_INT, MC_PASS, RPC_STRING, pass, RPC_END);
195 wipe_password (pass);
197 if (0 == rpc_get (my_socket, RPC_INT, &result, RPC_END))
198 return 0;
200 if (result != MC_LOGINOK){
201 message_1s (1, " MCFS ", _(" Invalid password "));
202 rpc_send (my_socket, RPC_INT, MC_QUIT, RPC_END);
203 close (my_socket);
204 return 0;
207 return my_socket;
210 /* This used to be in utilvfs.c, but as it deals with portmapper, it
211 is probably usefull for mcfs */
213 open_tcp_link (char *host, int *port, int *version, char *caller)
215 struct sockaddr_in server_address;
216 unsigned long inaddr;
217 struct hostent *hp;
218 int my_socket;
220 if (!*host)
221 return 0;
223 bzero ((char *) &server_address, sizeof (server_address));
224 server_address.sin_family = AF_INET;
226 /* Try to use the dotted decimal number */
227 if ((inaddr = inet_addr (host)) != -1)
228 bcopy ((char *) &inaddr, (char *) &server_address.sin_addr,
229 sizeof (inaddr));
230 else {
231 if ((hp = gethostbyname (host)) == NULL){
232 message_2s (1, caller, " Can't locate hostname: %s ", host);
233 return 0;
235 bcopy ((char *) hp->h_addr, (char *) &server_address.sin_addr,
236 hp->h_length);
239 /* Try to contact a remote portmapper to obtain the listening port */
240 if (*port == 0){
241 *port = get_remote_port (&server_address, version);
242 if (*port < 1)
243 return 0;
244 } else
245 *version = 1;
247 server_address.sin_port = htons (*port);
249 if ((my_socket = socket (AF_INET, SOCK_STREAM, 0)) < 0){
250 message_2s (1, caller, " Can't create socket: %s ",
251 unix_error_string(errno));
252 return 0;
254 if (connect (my_socket, (struct sockaddr *) &server_address,
255 sizeof (server_address)) < 0){
256 message_2s (1, caller, " Can't connect to server: %s ",
257 unix_error_string (errno));
258 close (my_socket);
259 return 0;
261 return my_socket;
264 static int mcfs_open_tcp_link (char *host, char *user,
265 int *port, char *netrcpass, int *version)
267 int my_socket;
268 int old_port = *port;
270 my_socket = open_tcp_link (host, port, version, " MCfs ");
271 if (my_socket <= 0)
272 return 0;
274 /* We got the connection to the server, verify if the server
275 implements our version of the RPC mechanism and then login
276 the user.
278 return mcfs_login_server (my_socket, user, *port, old_port == 0,
279 netrcpass, version);
282 static int mcfs_get_free_bucket_init = 1;
283 static mcfs_connection *mcfs_get_free_bucket ()
285 int i;
287 if (mcfs_get_free_bucket_init) {
288 mcfs_get_free_bucket_init = 0;
289 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
290 mcfs_connections [i].host = 0;
292 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
293 if (!mcfs_connections [i].host)
294 return &mcfs_connections [i];
296 /* This can't happend, since we have checked for max connections before */
297 vfs_die("Internal error: mcfs_get_free_bucket");
298 return 0; /* shut up, stupid gcc */
301 /* This routine keeps track of open connections */
302 /* Returns a connected socket to host */
303 static mcfs_connection *mcfs_open_link (char *host, char *user, int *port, char *netrcpass)
305 int i, sock, version;
306 mcfs_connection *bucket;
308 /* Is the link actually open? */
309 if (mcfs_get_free_bucket_init) {
310 mcfs_get_free_bucket_init = 0;
311 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++)
312 mcfs_connections [i].host = 0;
313 } else for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
314 if (!mcfs_connections [i].host)
315 continue;
316 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
317 (strcmp (user, mcfs_connections [i].user) == 0))
318 return &mcfs_connections [i];
320 if (mcfs_open_connections == MCFS_MAX_CONNECTIONS){
321 message_1s (1, MSG_ERROR, _(" Too many open connections "));
322 return 0;
325 if (!(sock = mcfs_open_tcp_link (host, user, port, netrcpass, &version)))
326 return 0;
328 bucket = mcfs_get_free_bucket ();
329 mcfs_open_connections++;
330 bucket->host = g_strdup (host);
331 bucket->user = g_strdup (user);
332 bucket->home = 0;
333 bucket->port = *port;
334 bucket->sock = sock;
335 bucket->version = version;
337 return bucket;
340 static int is_error (int result, int errno_num)
342 if (!(result == -1))
343 return my_errno = 0;
344 else
345 my_errno = errno_num;
346 return 1;
349 static int the_error (int result, int errno_num)
351 if (result == -1)
352 my_errno = errno_num;
353 else
354 my_errno = 0;
355 return result;
358 static char *mcfs_get_path (mcfs_connection **mc, char *path)
360 char *user, *host, *remote_path;
361 char *pass;
362 int port;
364 /* An absolute path name, try to determine connection socket */
365 if (strncmp (path, "/#mc:", 5))
366 return NULL;
367 path += 5;
369 /* Port = 0 means that open_tcp_link will try to contact the
370 * remote portmapper to get the port number
372 port = 0;
373 if ((remote_path = mcfs_get_host_and_username(path, &host, &user, &port, &pass)))
374 if (!(*mc = mcfs_open_link (host, user, &port, pass))){
375 g_free (remote_path);
376 remote_path = NULL;
378 g_free (host);
379 g_free (user);
380 if (pass)
381 wipe_password (pass);
383 if (!remote_path)
384 return NULL;
386 /* NOTE: tildes are deprecated. See ftpfs.c */
388 int f = !strcmp( remote_path, "/~" );
389 if (f || !strncmp( remote_path, "/~/", 3 )) {
390 char *s;
391 s = concat_dir_and_file( mcfs_gethome (*mc), remote_path +3-f );
392 g_free (remote_path);
393 remote_path = s;
396 return remote_path;
399 /* Simple function for routines returning only an integer from the server */
400 static int mcfs_handle_simple_error (int sock, int return_status)
402 int status, error;
404 if (0 == rpc_get (sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
405 return the_error (-1, EIO);
407 if (is_error (status, error))
408 return -1;
409 if (return_status)
410 return status;
411 return 0;
414 /* Nice wrappers */
415 static int mcfs_rpc_two_paths (int command, char *s1, char *s2)
417 mcfs_connection *mc;
418 char *r1, *r2;
420 if ((r1 = mcfs_get_path (&mc, s1)) == 0)
421 return -1;
423 if ((r2 = mcfs_get_path (&mc, s2)) == 0){
424 g_free (r1);
425 return -1;
428 rpc_send (mc->sock,
429 RPC_INT, command,
430 RPC_STRING, r1,
431 RPC_STRING, r2,
432 RPC_END);
433 g_free (r1);
434 g_free (r2);
435 return mcfs_handle_simple_error (mc->sock, 0);
438 static int mcfs_rpc_path (int command, char *path)
440 mcfs_connection *mc;
441 char *remote_file;
443 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
444 return -1;
446 rpc_send (mc->sock,
447 RPC_INT, command,
448 RPC_STRING, remote_file,
449 RPC_END);
451 g_free (remote_file);
452 return mcfs_handle_simple_error (mc->sock, 0);
455 static int mcfs_rpc_path_int (int command, char *path, int data)
457 mcfs_connection *mc;
458 char *remote_file;
460 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
461 return -1;
463 rpc_send (mc->sock,
464 RPC_INT, command,
465 RPC_STRING, remote_file,
466 RPC_INT, data, RPC_END);
468 g_free (remote_file);
469 return mcfs_handle_simple_error (mc->sock, 0);
472 static int mcfs_rpc_path_int_int (int command, char *path, int n1, int n2)
474 mcfs_connection *mc;
475 char *remote_file;
477 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
478 return -1;
480 rpc_send (mc->sock,
481 RPC_INT, command,
482 RPC_STRING, remote_file,
483 RPC_INT, n1,
484 RPC_INT, n2,
485 RPC_END);
487 g_free (remote_file);
488 return mcfs_handle_simple_error (mc->sock, 0);
491 static char *mcfs_gethome (mcfs_connection *mc)
493 char *buffer;
495 if (mc->home)
496 return g_strdup (mc->home);
497 else {
498 rpc_send (mc->sock, RPC_INT, MC_GETHOME, RPC_END);
499 if (0 == rpc_get (mc->sock, RPC_STRING, &buffer, RPC_END))
500 return g_strdup (PATH_SEP_STR);
501 mc->home = buffer;
502 return g_strdup (buffer);
506 /* The callbacks */
507 static void *mcfs_open (vfs *me, char *file, int flags, int mode)
509 char *remote_file;
510 mcfs_connection *mc;
511 int result, error_num;
512 mcfs_handle *remote_handle;
514 if (!(remote_file = mcfs_get_path (&mc, file)))
515 return 0;
517 rpc_send (mc->sock, RPC_INT, MC_OPEN, RPC_STRING, remote_file,
518 RPC_INT, flags, RPC_INT, mode, RPC_END);
519 g_free (remote_file);
521 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
522 return 0;
524 if (is_error (result, error_num))
525 return 0;
527 remote_handle = g_new (mcfs_handle, 2);
528 remote_handle->handle = result;
529 remote_handle->conn = mc;
531 return remote_handle;
534 static int mcfs_read (void *data, char *buffer, int count)
536 mcfs_handle *info = (mcfs_handle *) data;
537 int result, error;
538 int handle;
539 mcfs_connection *mc;
541 mc = info->conn;
542 handle = info->handle;
544 rpc_send (mc->sock, RPC_INT, MC_READ, RPC_INT, handle,
545 RPC_INT, count, RPC_END);
547 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
548 return the_error (-1, EIO);
550 if (is_error (result, error))
551 return 0;
553 if (0 == rpc_get (mc->sock, RPC_BLOCK, result, buffer, RPC_END))
554 return the_error (-1, EIO);
556 return result;
559 static int mcfs_write (void *data, char *buf, int nbyte)
561 mcfs_handle *info = (mcfs_handle *) data;
562 mcfs_connection *mc;
563 int handle;
565 mc = info->conn;
566 handle = info->handle;
568 rpc_send (mc->sock,
569 RPC_INT, MC_WRITE,
570 RPC_INT, handle,
571 RPC_INT, nbyte,
572 RPC_BLOCK, nbyte, buf,
573 RPC_END);
575 return mcfs_handle_simple_error (mc->sock, 1);
578 static int mcfs_close (void *data)
580 mcfs_handle *info = (mcfs_handle *) data;
581 mcfs_connection *mc;
582 int handle, result, error;
584 if (!data)
585 return -1;
587 handle = info->handle;
588 mc = info->conn;
590 rpc_send (mc->sock, RPC_INT, MC_CLOSE, RPC_INT, handle, RPC_END);
592 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
593 return the_error (-1, EIO);
595 is_error (result, error);
597 g_free (data);
598 return result;
601 static int mcfs_errno (vfs *me)
603 return my_errno;
606 typedef struct dir_entry {
607 char *text;
608 struct dir_entry *next;
609 struct stat my_stat;
610 int merrno;
611 } dir_entry;
613 typedef struct {
614 mcfs_connection *conn;
615 int handle;
616 dir_entry *entries;
617 dir_entry *current;
618 } opendir_info;
620 static void *mcfs_opendir (vfs *me, char *dirname)
622 opendir_info *mcfs_info;
623 mcfs_connection *mc;
624 int handle, error_num;
625 char *remote_dir;
626 int result;
628 if (!(remote_dir = mcfs_get_path (&mc, dirname)))
629 return 0;
631 rpc_send (mc->sock, RPC_INT, MC_OPENDIR, RPC_STRING, remote_dir, RPC_END);
632 g_free (remote_dir);
634 if (0 == rpc_get (mc->sock, RPC_INT, &result, RPC_INT, &error_num, RPC_END))
635 return 0;
637 if (is_error (result, error_num))
638 return 0;
640 handle = result;
642 mcfs_info = g_new (opendir_info, 1);
643 mcfs_info->conn = mc;
644 mcfs_info->handle = handle;
645 mcfs_info->entries = 0;
646 mcfs_info->current = 0;
648 return mcfs_info;
651 static int get_stat_info (mcfs_connection *mc, struct stat *buf);
653 static int mcfs_loaddir (opendir_info *mcfs_info)
655 int status, error;
656 mcfs_connection *mc = mcfs_info->conn;
657 int link = mc->sock;
658 int first = 1;
660 rpc_send (link, RPC_INT, MC_READDIR, RPC_INT, mcfs_info->handle, RPC_END);
662 for (;;){
663 int entry_len;
664 dir_entry *new_entry;
666 if (!rpc_get (link, RPC_INT, &entry_len, RPC_END))
667 return 0;
669 if (entry_len == 0)
670 break;
672 new_entry = g_new (dir_entry, 1);
673 new_entry->text = g_new0 (char, entry_len + 1);
675 new_entry->next = 0;
676 if (first){
677 mcfs_info->entries = new_entry;
678 mcfs_info->current = new_entry;
679 first = 0;
680 } else {
681 mcfs_info->current->next = new_entry;
682 mcfs_info->current = new_entry;
685 if (!rpc_get (link, RPC_BLOCK, entry_len, new_entry->text, RPC_END))
686 return 0;
688 /* Then we get the status from the lstat */
689 if (!rpc_get (link, RPC_INT, &status, RPC_INT, &error, RPC_END))
690 return 0;
692 if (is_error (status, error))
693 new_entry->merrno = error;
694 else {
695 new_entry->merrno = 0;
696 if (!get_stat_info (mc, &(new_entry->my_stat)))
697 return 0;
700 mcfs_info->current = mcfs_info->entries;
702 return 1;
705 static void mcfs_free_dir (dir_entry *de)
707 if (!de)
708 return;
709 mcfs_free_dir (de->next);
710 g_free (de->text);
711 g_free (de);
714 /* Explanation:
715 * On some operating systems (Slowaris 2 for example)
716 * the d_name member is just a char long (Nice trick that break everything,
717 * so we need to set up some space for the filename.
719 static struct {
720 struct dirent dent;
721 #ifdef NEED_EXTRA_DIRENT_BUFFER
722 char extra_buffer [MC_MAXPATHLEN];
723 #endif
724 } mcfs_readdir_data;
726 /* The readdir routine loads the complete directory */
727 /* It's too slow to ask the server each time */
728 /* It now also sends the complete lstat information for each file */
729 static struct stat *cached_lstat_info;
730 static void *mcfs_readdir (void *info)
732 opendir_info *mcfs_info;
733 char *dirent_dest;
735 mcfs_info = (opendir_info *) info;
737 if (!mcfs_info->entries)
738 if (!mcfs_loaddir (mcfs_info))
739 return NULL;
741 if (mcfs_info->current == 0){
742 cached_lstat_info = 0;
743 mcfs_free_dir (mcfs_info->entries);
744 mcfs_info->entries = 0;
745 return NULL;
747 dirent_dest = &(mcfs_readdir_data.dent.d_name [0]);
748 strcpy (dirent_dest, mcfs_info->current->text);
749 cached_lstat_info = &mcfs_info->current->my_stat;
750 mcfs_info->current = mcfs_info->current->next;
752 #ifndef DIRENT_LENGTH_COMPUTED
753 mcfs_readdir_data.dent.d_namlen = strlen (mcfs_readdir_data.dent.d_name);
754 #endif
756 return &mcfs_readdir_data;
759 static int mcfs_closedir (void *info)
761 opendir_info *mcfs_info = (opendir_info *) info;
762 dir_entry *p, *q;
764 rpc_send (mcfs_info->conn->sock, RPC_INT, MC_CLOSEDIR,
765 RPC_INT, mcfs_info->handle, RPC_END);
767 for (p = mcfs_info->entries; p;){
768 q = p;
769 p = p->next;
770 g_free (q->text);
771 g_free (q);
773 g_free (info);
774 return 0;
777 static time_t mcfs_get_time (mcfs_connection *mc)
779 int sock = mc->sock;
781 if (mc->version == 1) {
782 struct tm tt;
784 rpc_get (sock,
785 RPC_INT, &tt.tm_sec,
786 RPC_INT, &tt.tm_min,
787 RPC_INT, &tt.tm_hour,
788 RPC_INT, &tt.tm_mday,
789 RPC_INT, &tt.tm_year,
790 RPC_INT, &tt.tm_mon,
791 RPC_END);
792 tt.tm_year -= 1900;
793 tt.tm_isdst = 0;
795 return mktime (&tt);
796 } else {
797 char *buf;
798 long tm;
800 rpc_get (sock,
801 RPC_STRING, &buf,
802 RPC_END);
803 sscanf (buf, "%lx", &tm);
804 g_free (buf);
806 return (time_t) tm;
810 static int get_stat_info (mcfs_connection *mc, struct stat *buf)
812 long mylong;
813 int sock = mc->sock;
815 #ifdef HAVE_ST_RDEV
816 buf->st_rdev = 0;
817 #endif
819 rpc_get (sock, RPC_INT, &mylong, RPC_END);
820 buf->st_dev = mylong;
821 rpc_get (sock, RPC_INT, &mylong, RPC_END);
822 buf->st_ino = mylong;
823 rpc_get (sock, RPC_INT, &mylong, RPC_END);
824 buf->st_mode = mylong;
825 rpc_get (sock, RPC_INT, &mylong, RPC_END);
826 buf->st_nlink = mylong;
827 rpc_get (sock, RPC_INT, &mylong, RPC_END);
828 buf->st_uid = mylong;
829 rpc_get (sock, RPC_INT, &mylong, RPC_END);
830 buf->st_gid = mylong;
831 rpc_get (sock, RPC_INT, &mylong, RPC_END);
832 buf->st_size = mylong;
834 if (!rpc_get (sock, RPC_INT, &mylong, RPC_END))
835 return 0;
836 #ifdef HAVE_ST_BLOCKS
837 buf->st_blocks = mylong;
838 #endif
839 buf->st_atime = mcfs_get_time (mc);
840 buf->st_mtime = mcfs_get_time (mc);
841 buf->st_ctime = mcfs_get_time (mc);
842 return 1;
845 static int mcfs_stat_cmd (int cmd, char *path, struct stat *buf)
847 char *remote_file;
848 mcfs_connection *mc;
849 int status, error;
851 if ((remote_file = mcfs_get_path (&mc, path)) == 0)
852 return -1;
854 rpc_send (mc->sock, RPC_INT, cmd, RPC_STRING, remote_file, RPC_END);
855 g_free (remote_file);
856 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
857 return the_error (-1, errno);
859 if (is_error (status, error))
860 return -1;
862 if (get_stat_info (mc, buf))
863 return 0;
864 else
865 return the_error (-1, EIO);
868 static int mcfs_stat (vfs *me, char *path, struct stat *buf)
870 return mcfs_stat_cmd (MC_STAT, path, buf);
873 static int mcfs_lstat (vfs *me, char *path, struct stat *buf)
875 int path_len = strlen (path);
876 int entry_len = strlen (mcfs_readdir_data.dent.d_name);
878 /* Hack ... */
879 if (strcmp (path + path_len - entry_len,
880 mcfs_readdir_data.dent.d_name) == 0 &&
881 cached_lstat_info){
882 *buf = *cached_lstat_info;
883 return 0;
885 return mcfs_stat_cmd (MC_LSTAT, path, buf);
888 static int mcfs_fstat (void *data, struct stat *buf)
890 mcfs_handle *info = (mcfs_handle *) data;
891 int result, error;
892 int handle, sock;
894 sock = info->conn->sock;
895 handle = info->handle;
897 rpc_send (sock, RPC_INT, MC_FSTAT, RPC_INT, handle, RPC_END);
898 if (!rpc_get (sock, RPC_INT, &result, RPC_INT, &error, RPC_END))
899 return the_error (-1, EIO);
901 if (is_error (result, error))
902 return -1;
904 if (get_stat_info (info->conn, buf))
905 return 0;
906 else
907 return the_error (-1, EIO);
910 static int mcfs_chmod (vfs *me, char *path, int mode)
912 return mcfs_rpc_path_int (MC_CHMOD, path, mode);
915 static int mcfs_chown (vfs *me, char *path, int owner, int group)
917 return mcfs_rpc_path_int_int (MC_CHOWN, path, owner, group);
920 static int mcfs_utime (vfs *me, char *path, struct utimbuf *times)
922 mcfs_connection *mc;
923 int status;
924 char *file;
926 if (!(file = mcfs_get_path (&mc, path)))
927 return -1;
929 status = 0;
930 if (mc->version >= 2) {
931 char abuf[BUF_SMALL];
932 char mbuf[BUF_SMALL];
933 long atime, mtime;
935 atime = (long) times->actime;
936 mtime = (long) times->modtime;
938 g_snprintf (abuf, sizeof(abuf), "%lx", atime);
939 g_snprintf (mbuf, sizeof(mbuf), "%lx", mtime);
941 rpc_send (mc->sock, RPC_INT, MC_UTIME,
942 RPC_STRING, file,
943 RPC_STRING, abuf,
944 RPC_STRING, mbuf,
945 RPC_END);
946 status = mcfs_handle_simple_error (mc->sock, 0);
949 g_free (file);
950 return (status);
953 static int mcfs_readlink (vfs *me, char *path, char *buf, int size)
955 char *remote_file, *stat_str;
956 int status, error;
957 mcfs_connection *mc;
959 if (!(remote_file = mcfs_get_path (&mc, path)))
960 return -1;
962 rpc_send (mc->sock, RPC_INT, MC_READLINK, RPC_STRING, remote_file, RPC_END);
963 g_free (remote_file);
964 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
965 return the_error (-1, EIO);
967 if (is_error (status, errno))
968 return -1;
970 if (!rpc_get (mc->sock, RPC_STRING, &stat_str, RPC_END))
971 return the_error (-1, EIO);
973 strncpy (buf, stat_str, size);
974 g_free (stat_str);
975 return strlen (buf);
978 static int mcfs_unlink (vfs *me, char *path)
980 return mcfs_rpc_path (MC_UNLINK, path);
983 static int mcfs_symlink (vfs *me, char *n1, char *n2)
985 return mcfs_rpc_two_paths (MC_SYMLINK, n1, n2);
988 static int mcfs_rename (vfs *me, char *a, char *b)
990 return mcfs_rpc_two_paths (MC_RENAME, a, b);
993 static int mcfs_chdir (vfs *me, char *path)
995 char *remote_dir;
996 mcfs_connection *mc;
997 int status, error;
999 if (!(remote_dir = mcfs_get_path (&mc, path)))
1000 return -1;
1002 rpc_send (mc->sock, RPC_INT, MC_CHDIR, RPC_STRING, remote_dir, RPC_END);
1003 g_free (remote_dir);
1004 if (!rpc_get (mc->sock, RPC_INT, &status, RPC_INT, &error, RPC_END))
1005 return the_error (-1, EIO);
1007 if (is_error (status, error))
1008 return -1;
1009 return 0;
1012 static int mcfs_lseek (void *data, off_t offset, int whence)
1014 mcfs_handle *info = (mcfs_handle *) data;
1015 int handle, sock;
1017 sock = info->conn->sock;
1018 handle = info->handle;
1020 rpc_send (sock,
1021 RPC_INT, MC_LSEEK,
1022 RPC_INT, handle,
1023 RPC_INT, offset,
1024 RPC_INT, whence,
1025 RPC_END);
1026 return mcfs_handle_simple_error (sock, 1);
1029 static int mcfs_mknod (vfs *me, char *path, int mode, int dev)
1031 return mcfs_rpc_path_int_int (MC_MKNOD, path, mode, dev);
1034 static int mcfs_mkdir (vfs *me, char *path, mode_t mode)
1036 return mcfs_rpc_path_int (MC_MKDIR, path, mode);
1039 static int mcfs_rmdir (vfs *me, char *path)
1041 return mcfs_rpc_path (MC_RMDIR, path);
1044 static int mcfs_link (vfs *me, char *p1, char *p2)
1046 return mcfs_rpc_two_paths (MC_LINK, p1, p2);
1049 /* We do not free anything right now: we free resources when we run
1050 * out of them
1052 static vfsid mcfs_getid (vfs *me, char *p, struct vfs_stamping **parent)
1054 *parent = NULL;
1056 return (vfsid) -1;
1059 static int mcfs_nothingisopen (vfsid id)
1061 return 0;
1064 static void mcfs_free (vfsid id)
1066 /* FIXME: Should not be empty */
1069 /* Gives up on a socket and reopnes the connection, the child own the socket
1070 * now
1072 static void
1073 my_forget (char *path)
1075 char *host, *user, *pass, *p;
1076 int port, i, vers;
1078 if (strncmp (path, "/#mc:", 5))
1079 return;
1081 path += 5;
1082 if (path[0] == '/' && path[1] == '/')
1083 path += 2;
1085 if ((p = mcfs_get_host_and_username (path, &host, &user, &port, &pass)) == 0) {
1086 g_free (host);
1087 g_free (user);
1088 if (pass)
1089 wipe_password (pass);
1090 return;
1092 for (i = 0; i < MCFS_MAX_CONNECTIONS; i++){
1093 if ((strcmp (host, mcfs_connections [i].host) == 0) &&
1094 (strcmp (user, mcfs_connections [i].user) == 0) &&
1095 (port == mcfs_connections [i].port)){
1097 /* close socket: the child owns it now */
1098 close (mcfs_connections [i].sock);
1100 /* reopen the connection */
1101 mcfs_connections [i].sock = mcfs_open_tcp_link (host, user, &port, pass, &vers);
1104 g_free (p);
1105 g_free (host);
1106 g_free (user);
1107 if (pass)
1108 wipe_password (pass);
1111 static int
1112 mcfs_setctl (vfs *me, char *path, int ctlop, char *arg)
1114 switch (ctlop) {
1115 case MCCTL_FORGET_ABOUT:
1116 my_forget(path);
1117 return 0;
1119 return 0;
1122 vfs vfs_mcfs_ops = {
1123 NULL, /* This is place of next pointer */
1124 "Midnight Commander's private remote filesystem",
1125 F_NET, /* flags */
1126 "mc:", /* prefix */
1127 NULL, /* data */
1128 0, /* errno */
1129 NULL,
1130 NULL,
1131 mcfs_fill_names,
1132 NULL,
1134 mcfs_open,
1135 mcfs_close,
1136 mcfs_read,
1137 mcfs_write,
1139 mcfs_opendir,
1140 mcfs_readdir,
1141 mcfs_closedir,
1142 NULL,
1143 NULL,
1145 mcfs_stat,
1146 mcfs_lstat,
1147 mcfs_fstat,
1149 mcfs_chmod,
1150 mcfs_chown,
1151 mcfs_utime,
1153 mcfs_readlink,
1154 mcfs_symlink,
1155 mcfs_link,
1156 mcfs_unlink,
1158 mcfs_rename,
1159 mcfs_chdir,
1160 mcfs_errno,
1161 mcfs_lseek,
1162 mcfs_mknod,
1164 mcfs_getid,
1165 mcfs_nothingisopen,
1166 mcfs_free,
1168 NULL,
1169 NULL,
1171 mcfs_mkdir,
1172 mcfs_rmdir,
1173 NULL,
1174 mcfs_setctl
1176 MMAPNULL