1 /* Virtual File System switch code
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007 Free Software Foundation, Inc.
5 Written by: 1995 Miguel de Icaza
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /* Warning: funtions like extfs_lstat() have right to destroy any
24 * strings you pass to them. This is acutally ok as you g_strdup what
25 * you are passing to them, anyway; still, beware. */
27 /* Namespace: exports *many* functions with vfs_ prefix; exports
28 parse_ls_lga and friends which do not have that prefix. */
33 #include <stdlib.h> /* For atol() */
37 #include <sys/types.h>
39 #include <ctype.h> /* is_digit() */
41 #include "../src/global.h"
42 #include "../src/tty.h" /* enable/disable interrupt key */
43 #include "../src/wtools.h" /* message() */
44 #include "../src/main.h" /* print_vfs_message */
57 /* They keep track of the current directory */
58 static struct vfs_class
*current_vfs
;
59 static char *current_dir
;
63 struct vfs_class
*vclass
;
67 static GSList
*vfs_openfiles
;
68 #define VFS_FIRST_HANDLE 100
70 static struct vfs_class
*localfs_class
;
72 /* Create new VFS handle and put it to the list */
74 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
76 static int vfs_handle_counter
= VFS_FIRST_HANDLE
;
77 struct vfs_openfile
*h
;
79 h
= g_new (struct vfs_openfile
, 1);
80 h
->handle
= vfs_handle_counter
++;
83 vfs_openfiles
= g_slist_prepend (vfs_openfiles
, h
);
87 /* Function to match handle, passed to g_slist_find_custom() */
89 vfs_cmp_handle (gconstpointer a
, gconstpointer b
)
93 return ((struct vfs_openfile
*) a
)->handle
!= (long) b
;
96 /* Find VFS class by file handle */
97 static inline struct vfs_class
*
101 struct vfs_openfile
*h
;
103 l
= g_slist_find_custom (vfs_openfiles
, (void *) (long) handle
,
107 h
= (struct vfs_openfile
*) l
->data
;
113 /* Find private file data by file handle */
115 vfs_info (int handle
)
118 struct vfs_openfile
*h
;
120 l
= g_slist_find_custom (vfs_openfiles
, (void *) (long) handle
,
124 h
= (struct vfs_openfile
*) l
->data
;
130 /* Free open file data for given file handle */
132 vfs_free_handle (int handle
)
136 l
= g_slist_find_custom (vfs_openfiles
, (void *) (long) handle
,
138 vfs_openfiles
= g_slist_delete_link (vfs_openfiles
, l
);
141 static struct vfs_class
*vfs_list
;
144 vfs_register_class (struct vfs_class
*vfs
)
146 if (vfs
->init
) /* vfs has own initialization function */
147 if (!(*vfs
->init
)(vfs
)) /* but it failed */
150 vfs
->next
= vfs_list
;
156 /* Return VFS class for the given prefix */
157 static struct vfs_class
*
158 vfs_prefix_to_class (char *prefix
)
160 struct vfs_class
*vfs
;
162 /* Avoid last class (localfs) that would accept any prefix */
163 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
165 if ((*vfs
->which
) (vfs
, prefix
) == -1)
170 && !strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)))
176 /* Strip known vfs suffixes from a filename (possible improvement: strip
177 suffix from last path component).
178 Returns a malloced string which has to be freed. */
180 vfs_strip_suffix_from_filename (const char *filename
)
182 struct vfs_class
*vfs
;
187 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
189 p
= g_strdup (filename
);
190 if (!(semi
= strrchr (p
, '#')))
193 /* Avoid last class (localfs) that would accept any prefix */
194 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
196 if ((*vfs
->which
) (vfs
, semi
+ 1) == -1)
198 *semi
= '\0'; /* Found valid suffix */
202 && !strncmp (semi
+ 1, vfs
->prefix
, strlen (vfs
->prefix
))) {
203 *semi
= '\0'; /* Found valid suffix */
211 path_magic (const char *path
)
215 if (!stat(path
, &buf
))
222 * Splits path '/p1#op/inpath' into inpath,op; returns which vfs it is.
223 * What is left in path is p1. You still want to g_free(path), you DON'T
224 * want to free neither *inpath nor *op
227 vfs_split (char *path
, char **inpath
, char **op
)
231 struct vfs_class
*ret
;
234 vfs_die("Cannot split NULL");
236 semi
= strrchr (path
, '#');
237 if (!semi
|| !path_magic(path
))
240 slash
= strchr (semi
, PATH_SEP
);
252 if ((ret
= vfs_prefix_to_class (semi
+1))){
256 *inpath
= slash
? slash
+ 1 : NULL
;
263 ret
= vfs_split (path
, inpath
, op
);
268 static struct vfs_class
*
269 _vfs_get_class (char *path
)
273 struct vfs_class
*ret
;
275 g_return_val_if_fail(path
, NULL
);
277 semi
= strrchr (path
, '#');
278 if (!semi
|| !path_magic (path
))
281 slash
= strchr (semi
, PATH_SEP
);
286 ret
= vfs_prefix_to_class (semi
+1);
291 ret
= _vfs_get_class (path
);
298 vfs_get_class (const char *pathname
)
300 struct vfs_class
*vfs
;
301 char *path
= g_strdup (pathname
);
303 vfs
= _vfs_get_class (path
);
313 ferrno (struct vfs_class
*vfs
)
315 return vfs
->ferrno
? (*vfs
->ferrno
)(vfs
) : E_UNKNOWN
;
316 /* Hope that error message is obscure enough ;-) */
320 mc_open (const char *filename
, int flags
, ...)
326 char *file
= vfs_canon (filename
);
327 struct vfs_class
*vfs
= vfs_get_class (file
);
329 /* Get the mode flag */
330 if (flags
& O_CREAT
) {
331 va_start (ap
, flags
);
332 mode
= va_arg (ap
, int);
343 info
= (*vfs
->open
) (vfs
, file
, flags
, mode
); /* open must be supported */
346 errno
= ferrno (vfs
);
350 return vfs_new_handle (vfs
, info
);
354 #define MC_NAMEOP(name, inarg, callarg) \
355 int mc_##name inarg \
357 struct vfs_class *vfs; \
359 char *mpath = vfs_canon (path); \
360 vfs = vfs_get_class (mpath); \
361 result = vfs->name ? (*vfs->name)callarg : -1; \
364 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
368 MC_NAMEOP (chmod
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
369 MC_NAMEOP (chown
, (const char *path
, uid_t owner
, gid_t group
), (vfs
, mpath
, owner
, group
))
370 MC_NAMEOP (utime
, (const char *path
, struct utimbuf
*times
), (vfs
, mpath
, times
))
371 MC_NAMEOP (readlink
, (const char *path
, char *buf
, int bufsiz
), (vfs
, mpath
, buf
, bufsiz
))
372 MC_NAMEOP (unlink
, (const char *path
), (vfs
, mpath
))
373 MC_NAMEOP (symlink
, (const char *name1
, const char *path
), (vfs
, name1
, mpath
))
374 MC_NAMEOP (mkdir
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
375 MC_NAMEOP (rmdir
, (const char *path
), (vfs
, mpath
))
376 MC_NAMEOP (mknod
, (const char *path
, mode_t mode
, dev_t dev
), (vfs
, mpath
, mode
, dev
))
379 #define MC_HANDLEOP(name, inarg, callarg) \
380 ssize_t mc_##name inarg \
382 struct vfs_class *vfs; \
386 vfs = vfs_op (handle); \
387 result = vfs->name ? (*vfs->name)callarg : -1; \
389 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
393 MC_HANDLEOP(read
, (int handle
, void *buffer
, int count
), (vfs_info (handle
), buffer
, count
))
394 MC_HANDLEOP(write
, (int handle
, const void *buf
, int nbyte
), (vfs_info (handle
), buf
, nbyte
))
397 #define MC_RENAMEOP(name) \
398 int mc_##name (const char *fname1, const char *fname2) \
400 struct vfs_class *vfs; \
402 char *name2, *name1 = vfs_canon (fname1); \
403 vfs = vfs_get_class (name1); \
404 name2 = vfs_canon (fname2); \
405 if (vfs != vfs_get_class (name2)){ \
411 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
415 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
424 mc_ctl (int handle
, int ctlop
, void *arg
)
426 struct vfs_class
*vfs
= vfs_op (handle
);
428 return vfs
->ctl
? (*vfs
->ctl
)(vfs_info (handle
), ctlop
, arg
) : 0;
432 mc_setctl (const char *path
, int ctlop
, void *arg
)
434 struct vfs_class
*vfs
;
439 vfs_die("You don't want to pass NULL to mc_setctl.");
441 mpath
= vfs_canon (path
);
442 vfs
= vfs_get_class (mpath
);
443 result
= vfs
->setctl
? (*vfs
->setctl
)(vfs
, mpath
, ctlop
, arg
) : 0;
449 mc_close (int handle
)
451 struct vfs_class
*vfs
;
454 if (handle
== -1 || !vfs_info (handle
))
457 vfs
= vfs_op (handle
);
459 return close (handle
);
462 vfs_die ("VFS must support close.\n");
463 result
= (*vfs
->close
)(vfs_info (handle
));
464 vfs_free_handle (handle
);
466 errno
= ferrno (vfs
);
472 mc_opendir (const char *dirname
)
474 int handle
, *handlep
;
476 struct vfs_class
*vfs
;
479 dname
= vfs_canon (dirname
);
480 vfs
= vfs_get_class (dname
);
482 info
= vfs
->opendir
? (*vfs
->opendir
)(vfs
, dname
) : NULL
;
485 errno
= vfs
->opendir
? ferrno (vfs
) : E_NOTSUPP
;
488 handle
= vfs_new_handle (vfs
, info
);
490 handlep
= g_new (int, 1);
492 return (DIR *) handlep
;
496 mc_readdir (DIR *dirp
)
499 struct vfs_class
*vfs
;
500 struct dirent
*result
= NULL
;
506 handle
= *(int *) dirp
;
507 vfs
= vfs_op (handle
);
509 result
= (*vfs
->readdir
) (vfs_info (handle
));
511 errno
= vfs
->readdir
? ferrno (vfs
) : E_NOTSUPP
;
516 mc_closedir (DIR *dirp
)
518 int handle
= *(int *) dirp
;
519 struct vfs_class
*vfs
= vfs_op (handle
);
522 result
= vfs
->closedir
? (*vfs
->closedir
)(vfs_info (handle
)) : -1;
523 vfs_free_handle (handle
);
528 int mc_stat (const char *filename
, struct stat
*buf
) {
529 struct vfs_class
*vfs
;
532 path
= vfs_canon (filename
); vfs
= vfs_get_class (path
);
533 result
= vfs
->stat
? (*vfs
->stat
) (vfs
, path
, buf
) : -1;
536 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
540 int mc_lstat (const char *filename
, struct stat
*buf
) {
541 struct vfs_class
*vfs
;
544 path
= vfs_canon (filename
); vfs
= vfs_get_class (path
);
545 result
= vfs
->lstat
? (*vfs
->lstat
) (vfs
, path
, buf
) : -1;
548 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
552 int mc_fstat (int handle
, struct stat
*buf
) {
553 struct vfs_class
*vfs
;
558 vfs
= vfs_op (handle
);
559 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_info (handle
), buf
) : -1;
561 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
566 * Return current directory. If it's local, reread the current directory
567 * from the OS. You must g_strdup() whatever this function returns.
573 struct stat my_stat
, my_stat2
;
575 if (!_vfs_get_class (current_dir
)) {
576 p
= g_get_current_dir ();
577 if (!p
) /* One of the directories in the path is not readable */
580 /* Otherwise check if it is O.K. to use the current_dir */
581 if (!cd_symlinks
|| mc_stat (p
, &my_stat
)
582 || mc_stat (current_dir
, &my_stat2
)
583 || my_stat
.st_ino
!= my_stat2
.st_ino
584 || my_stat
.st_dev
!= my_stat2
.st_dev
) {
585 g_free (current_dir
);
588 } /* Otherwise we return current_dir below */
597 current_dir
= g_strdup (PATH_SEP_STR
);
600 if (strlen (current_dir
) > MC_MAXPATHLEN
- 2)
601 vfs_die ("Current dir too long.\n");
603 current_vfs
= vfs_get_class (current_dir
);
607 * Return current directory. If it's local, reread the current directory
608 * from the OS. Put directory to the provided buffer.
611 mc_get_current_wd (char *buffer
, int size
)
613 const char *cwd
= _vfs_get_cwd ();
615 g_strlcpy (buffer
, cwd
, size
);
620 * Return current directory without any OS calls.
623 vfs_get_current_dir (void)
628 off_t
mc_lseek (int fd
, off_t offset
, int whence
)
630 struct vfs_class
*vfs
;
637 result
= vfs
->lseek
? (*vfs
->lseek
)(vfs_info (fd
), offset
, whence
) : -1;
639 errno
= vfs
->lseek
? ferrno (vfs
) : E_NOTSUPP
;
644 * remove //, /./ and /../
647 #define ISSLASH(a) (!a || (a == '/'))
650 vfs_canon (const char *path
)
653 vfs_die("Cannot canonicalize NULL");
655 /* Relative to current directory */
656 if (*path
!= PATH_SEP
){
657 char *local
, *result
;
659 local
= concat_dir_and_file (current_dir
, path
);
661 result
= vfs_canon (local
);
667 * So we have path of following form:
668 * /p1/p2#op/.././././p3#op/p4. Good luck.
671 char *result
= g_strdup (path
);
672 canonicalize_pathname (result
);
679 * Return 0 on success, -1 on failure.
682 mc_chdir (const char *path
)
685 struct vfs_class
*old_vfs
, *new_vfs
;
689 new_dir
= vfs_canon (path
);
690 new_vfs
= vfs_get_class (new_dir
);
691 if (!new_vfs
->chdir
) {
696 result
= (*new_vfs
->chdir
) (new_vfs
, new_dir
);
699 errno
= ferrno (new_vfs
);
704 old_vfsid
= vfs_getid (current_vfs
, current_dir
);
705 old_vfs
= current_vfs
;
707 /* Actually change directory */
708 g_free (current_dir
);
709 current_dir
= new_dir
;
710 current_vfs
= new_vfs
;
712 /* This function uses the new current_dir implicitly */
713 vfs_stamp_create (old_vfs
, old_vfsid
);
715 /* Sometimes we assume no trailing slash on cwd */
718 p
= strchr (current_dir
, 0) - 1;
719 if (*p
== PATH_SEP
&& p
> current_dir
)
726 /* Return 1 is the current VFS class is local */
728 vfs_current_is_local (void)
730 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
733 /* Return flags of the VFS class of the given filename */
735 vfs_file_class_flags (const char *filename
)
737 struct vfs_class
*vfs
;
740 fname
= vfs_canon (filename
);
741 vfs
= vfs_get_class (fname
);
747 mc_def_getlocalcopy (const char *filename
)
754 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
758 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
762 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0) {
763 if (write (fdout
, buffer
, i
) != i
)
772 if (close (fdout
) == -1) {
777 if (mc_stat (filename
, &mystat
) != -1) {
778 chmod (tmp
, mystat
.st_mode
);
792 mc_getlocalcopy (const char *pathname
)
795 char *path
= vfs_canon (pathname
);
796 struct vfs_class
*vfs
= vfs_get_class (path
);
798 result
= vfs
->getlocalcopy
? (*vfs
->getlocalcopy
)(vfs
, path
) :
799 mc_def_getlocalcopy (path
);
802 errno
= ferrno (vfs
);
807 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
808 const char *local
, int has_changed
)
810 int fdin
= -1, fdout
= -1, i
;
817 fdin
= open (local
, O_RDONLY
);
820 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
823 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0) {
824 if (mc_write (fdout
, buffer
, i
) != i
)
830 if (close (fdin
) == -1) {
835 if (mc_close (fdout
) == -1) {
844 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
854 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
856 int return_value
= 0;
857 char *path
= vfs_canon (pathname
);
858 struct vfs_class
*vfs
= vfs_get_class (path
);
860 return_value
= vfs
->ungetlocalcopy
?
861 (*vfs
->ungetlocalcopy
)(vfs
, path
, local
, has_changed
) :
862 mc_def_ungetlocalcopy (vfs
, path
, local
, has_changed
);
871 /* localfs needs to be the first one */
873 /* fallback value for vfs_get_class() */
874 localfs_class
= vfs_list
;
883 #endif /* USE_EXT2FSLIB */
891 #endif /* WITH_SMBFS */
894 #endif /* WITH_MCFS */
895 #endif /* USE_NETCODE */
903 struct vfs_class
*vfs
;
907 g_free (current_dir
);
909 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
913 g_slist_free (vfs_openfiles
);
917 * These ones grab information from the VFS
918 * and handles them to an upper layer
921 vfs_fill_names (fill_names_f func
)
923 struct vfs_class
*vfs
;
925 for (vfs
=vfs_list
; vfs
; vfs
=vfs
->next
)
927 (*vfs
->fill_names
) (vfs
, func
);
931 * Returns vfs path corresponding to given url. If passed string is
932 * not recognized as url, g_strdup(url) is returned.
935 static const struct {
938 const char *substitute
;
939 } url_table
[] = { {"ftp://", 6, "/#ftp:"},
940 {"mc://", 5, "/#mc:"},
941 {"smb://", 6, "/#smb:"},
942 {"sh://", 5, "/#sh:"},
943 {"ssh://", 6, "/#sh:"},
948 vfs_translate_url (const char *url
)
952 for (i
= 0; i
< sizeof (url_table
)/sizeof (url_table
[0]); i
++)
953 if (strncmp (url
, url_table
[i
].name
, url_table
[i
].name_len
) == 0)
954 return g_strconcat (url_table
[i
].substitute
, url
+ url_table
[i
].name_len
, (char*) NULL
);
956 return g_strdup (url
);
959 int vfs_file_is_local (const char *filename
)
961 return vfs_file_class_flags (filename
) & VFSF_LOCAL
;