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. */
25 * \brief Source: Virtual File System switch code
26 * \author Miguel de Icaza
27 * \author Jakub Jelinek
28 * \author Pavel Machek
30 * \warning funtions like extfs_lstat() have right to destroy any
31 * strings you pass to them. This is acutally ok as you g_strdup what
32 * you are passing to them, anyway; still, beware.
34 * Namespace: exports *many* functions with vfs_ prefix; exports
35 * parse_ls_lga and friends which do not have that prefix.
41 #include <stdlib.h> /* For atol() */
45 #include <sys/types.h>
47 #include <ctype.h> /* is_digit() */
53 #include "../src/global.h"
55 #include "../src/wtools.h" /* message() */
56 #include "../src/main.h" /* print_vfs_message */
57 #include "../src/strutil.h"
70 /** They keep track of the current directory */
71 static struct vfs_class
*current_vfs
;
72 static char *current_dir
;
76 struct vfs_class
*vclass
;
86 static GPtrArray
*vfs_openfiles
;
87 static long vfs_free_handle_list
= -1;
88 #define VFS_FIRST_HANDLE 100
90 static struct vfs_class
*localfs_class
;
91 static GString
*vfs_str_buffer
;
93 static const char *supported_encodings
[] = {
108 /** Create new VFS handle and put it to the list */
110 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
112 struct vfs_openfile
*h
;
114 h
= g_new (struct vfs_openfile
, 1);
118 /* Allocate the first free handle */
119 h
->handle
= vfs_free_handle_list
;
120 if (h
->handle
== -1) {
121 /* No free allocated handles, allocate one */
122 h
->handle
= vfs_openfiles
->len
;
123 g_ptr_array_add (vfs_openfiles
, h
);
125 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
126 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
129 h
->handle
+= VFS_FIRST_HANDLE
;
133 /** Find VFS class by file handle */
134 static struct vfs_class
*
137 struct vfs_openfile
*h
;
139 if (handle
< VFS_FIRST_HANDLE
||
140 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
143 h
= (struct vfs_openfile
*) g_ptr_array_index (
144 vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
148 g_assert (h
->handle
== handle
);
153 /** Find private file data by file handle */
155 vfs_info (int handle
)
157 struct vfs_openfile
*h
;
159 if (handle
< VFS_FIRST_HANDLE
||
160 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
163 h
= (struct vfs_openfile
*) g_ptr_array_index (
164 vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
168 g_assert (h
->handle
== handle
);
173 /** Free open file data for given file handle */
175 vfs_free_handle (int handle
)
177 if (handle
< VFS_FIRST_HANDLE
||
178 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
181 g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
) =
182 (void *) vfs_free_handle_list
;
183 vfs_free_handle_list
= handle
- VFS_FIRST_HANDLE
;
186 static struct vfs_class
*vfs_list
;
189 vfs_register_class (struct vfs_class
*vfs
)
191 if (vfs
->init
) /* vfs has own initialization function */
192 if (!(*vfs
->init
)(vfs
)) /* but it failed */
195 vfs
->next
= vfs_list
;
201 /** Return VFS class for the given prefix */
202 static struct vfs_class
*
203 vfs_prefix_to_class (char *prefix
)
205 struct vfs_class
*vfs
;
207 /* Avoid last class (localfs) that would accept any prefix */
208 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
210 if ((*vfs
->which
) (vfs
, prefix
) == -1)
215 && !strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)))
221 /** Strip known vfs suffixes from a filename (possible improvement: strip
222 * suffix from last path component).
223 * \return a malloced string which has to be freed.
226 vfs_strip_suffix_from_filename (const char *filename
)
228 struct vfs_class
*vfs
;
233 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
235 p
= g_strdup (filename
);
236 if (!(semi
= strrchr (p
, '#')))
239 /* Avoid last class (localfs) that would accept any prefix */
240 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
242 if ((*vfs
->which
) (vfs
, semi
+ 1) == -1)
244 *semi
= '\0'; /* Found valid suffix */
248 && !strncmp (semi
+ 1, vfs
->prefix
, strlen (vfs
->prefix
))) {
249 *semi
= '\0'; /* Found valid suffix */
257 path_magic (const char *path
)
261 if (!stat(path
, &buf
))
268 * Splits path '/p1#op/inpath' into inpath,op; returns which vfs it is.
269 * What is left in path is p1. You still want to g_free(path), you DON'T
270 * want to free neither *inpath nor *op
273 vfs_split (char *path
, char **inpath
, char **op
)
277 struct vfs_class
*ret
;
280 vfs_die("Cannot split NULL");
282 semi
= strrchr (path
, '#');
283 if (!semi
|| !path_magic(path
))
286 slash
= strchr (semi
, PATH_SEP
);
298 if ((ret
= vfs_prefix_to_class (semi
+1))){
302 *inpath
= slash
? slash
+ 1 : NULL
;
309 ret
= vfs_split (path
, inpath
, op
);
314 static struct vfs_class
*
315 _vfs_get_class (char *path
)
319 struct vfs_class
*ret
;
321 g_return_val_if_fail(path
, NULL
);
323 semi
= strrchr (path
, '#');
324 if (!semi
|| !path_magic (path
))
327 slash
= strchr (semi
, PATH_SEP
);
332 ret
= vfs_prefix_to_class (semi
+1);
337 ret
= _vfs_get_class (path
);
344 vfs_get_class (const char *pathname
)
346 struct vfs_class
*vfs
;
347 char *path
= g_strdup (pathname
);
349 vfs
= _vfs_get_class (path
);
359 vfs_get_encoding (const char *path
)
361 static char result
[16];
366 work
= g_strdup (path
);
367 semi
= g_strrstr (work
, "#enc:");
370 semi
+= 5 * sizeof (char);
371 slash
= strchr (semi
, PATH_SEP
);
375 g_strlcpy (result
, semi
, sizeof(result
));
384 /* return if encoding can by used in vfs (is ascci full compactible) */
385 /* contains only a few encoding now */
387 vfs_supported_enconding (const char *encoding
) {
391 for (t
= 0; supported_encodings
[t
] != NULL
; t
++) {
392 result
+= (g_ascii_strncasecmp (encoding
, supported_encodings
[t
],
393 strlen (supported_encodings
[t
])) == 0);
399 /* now used only by vfs_translate_path, but could be used in other vfs
400 * plugin to automatic detect encoding
401 * path - path to translate
402 * size - how many bytes from path translate
403 * defcnv - convertor, that is used as default, when path does not contain any
405 * buffer - used to store result of translation
408 _vfs_translate_path (const char *path
, int size
,
409 GIConv defcnv
, GString
*buffer
)
414 estr_t state
= ESTR_SUCCESS
;
415 static char encoding
[16];
419 if (size
== 0) return 0;
420 size
= ( size
> 0) ? size
: (signed int)strlen (path
);
422 /* try found #end: */
423 semi
= g_strrstr_len (path
, size
, "#enc:");
425 /* first must be translated part before #enc: */
428 /* remove '/' before #enc */
429 ps
= str_cget_prev_char (semi
);
430 if (ps
[0] == PATH_SEP
) ms
= ps
- path
;
432 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
434 if (state
!= ESTR_SUCCESS
)
436 /* now can be translated part after #enc: */
439 slash
= strchr (semi
, PATH_SEP
);
440 /* ignore slashes after size; */
441 if (slash
- path
>= size
) slash
= NULL
;
443 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
444 ms
= min ((unsigned int) ms
, sizeof (encoding
) - 1);
445 /* limit encoding size (ms) to path size (size) */
446 if (semi
+ ms
> path
+ size
) ms
= path
+ size
- semi
;
447 memcpy (encoding
, semi
, ms
);
450 switch (vfs_supported_enconding (encoding
)) {
452 coder
= str_crt_conv_to (encoding
);
453 if (coder
!= INVALID_CONV
) {
455 state
= str_vfs_convert_to (coder
, slash
,
456 path
+ size
- slash
, buffer
);
457 } else if (buffer
->str
[0] == '\0') {
458 /* exmaple "/#enc:utf-8" */
459 g_string_append_c(buffer
, PATH_SEP
);
461 str_close_conv (coder
);
473 /* path can be translated whole at once */
474 state
= str_vfs_convert_to (defcnv
, path
, size
, buffer
);
482 vfs_translate_path (const char *path
)
486 g_string_set_size(vfs_str_buffer
,0);
487 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
490 return (state == 0) ? vfs_str_buffer->data : NULL;
492 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
496 vfs_translate_path_n (const char *path
)
500 result
= vfs_translate_path (path
);
501 return (result
!= NULL
) ? g_strdup (result
) : NULL
;
505 vfs_canon_and_translate (const char *path
)
510 canon
= g_strdup ("");
512 canon
= vfs_canon (path
);
513 result
= vfs_translate_path_n (canon
);
519 ferrno (struct vfs_class
*vfs
)
521 return vfs
->ferrno
? (*vfs
->ferrno
)(vfs
) : E_UNKNOWN
;
522 /* Hope that error message is obscure enough ;-) */
526 mc_open (const char *filename
, int flags
, ...)
532 char *file
= vfs_canon_and_translate (filename
);
534 struct vfs_class
*vfs
= vfs_get_class (file
);
536 /* Get the mode flag */
537 if (flags
& O_CREAT
) {
538 va_start (ap
, flags
);
539 mode
= va_arg (ap
, int);
550 info
= (*vfs
->open
) (vfs
, file
, flags
, mode
); /* open must be supported */
553 errno
= ferrno (vfs
);
557 return vfs_new_handle (vfs
, info
);
562 #define MC_NAMEOP(name, inarg, callarg) \
563 int mc_##name inarg \
565 struct vfs_class *vfs; \
567 char *mpath = vfs_canon_and_translate (path); \
568 if (mpath != NULL) { \
569 vfs = vfs_get_class (mpath); \
574 result = vfs->name ? (*vfs->name)callarg : -1; \
577 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
582 MC_NAMEOP (chmod
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
583 MC_NAMEOP (chown
, (const char *path
, uid_t owner
, gid_t group
), (vfs
, mpath
, owner
, group
))
584 MC_NAMEOP (utime
, (const char *path
, struct utimbuf
*times
), (vfs
, mpath
, times
))
585 MC_NAMEOP (readlink
, (const char *path
, char *buf
, int bufsiz
), (vfs
, mpath
, buf
, bufsiz
))
586 MC_NAMEOP (unlink
, (const char *path
), (vfs
, mpath
))
587 MC_NAMEOP (mkdir
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
588 MC_NAMEOP (rmdir
, (const char *path
), (vfs
, mpath
))
589 MC_NAMEOP (mknod
, (const char *path
, mode_t mode
, dev_t dev
), (vfs
, mpath
, mode
, dev
))
592 mc_symlink (const char *name1
, const char *path
)
594 struct vfs_class
*vfs
;
600 mpath
= vfs_canon_and_translate (path
);
602 tmp
= g_strdup (name1
);
603 lpath
= vfs_translate_path_n (tmp
);
607 vfs
= vfs_get_class (mpath
);
608 result
= vfs
->symlink
? (*vfs
->symlink
) (vfs
, lpath
, mpath
) : -1;
612 errno
= vfs
->symlink
? ferrno (vfs
) : E_NOTSUPP
;
620 #define MC_HANDLEOP(name, inarg, callarg) \
621 ssize_t mc_##name inarg \
623 struct vfs_class *vfs; \
627 vfs = vfs_op (handle); \
630 result = vfs->name ? (*vfs->name)callarg : -1; \
632 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
636 MC_HANDLEOP(read
, (int handle
, void *buffer
, int count
), (vfs_info (handle
), buffer
, count
))
637 MC_HANDLEOP(write
, (int handle
, const void *buf
, int nbyte
), (vfs_info (handle
), buf
, nbyte
))
640 #define MC_RENAMEOP(name) \
641 int mc_##name (const char *fname1, const char *fname2) \
643 struct vfs_class *vfs; \
645 char *name2, *name1; \
646 name1 = vfs_canon_and_translate (fname1); \
647 if (name1 != NULL) { \
648 name2 = vfs_canon_and_translate (fname2); \
649 if (name2 != NULL) { \
650 vfs = vfs_get_class (name1); \
651 if (vfs != vfs_get_class (name2)){ \
657 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
661 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
675 mc_ctl (int handle
, int ctlop
, void *arg
)
677 struct vfs_class
*vfs
= vfs_op (handle
);
682 return vfs
->ctl
? (*vfs
->ctl
)(vfs_info (handle
), ctlop
, arg
) : 0;
686 mc_setctl (const char *path
, int ctlop
, void *arg
)
688 struct vfs_class
*vfs
;
693 vfs_die("You don't want to pass NULL to mc_setctl.");
695 mpath
= vfs_canon_and_translate (path
);
697 vfs
= vfs_get_class (mpath
);
698 result
= vfs
->setctl
? (*vfs
->setctl
)(vfs
, mpath
, ctlop
, arg
) : 0;
705 mc_close (int handle
)
707 struct vfs_class
*vfs
;
710 if (handle
== -1 || !vfs_info (handle
))
713 vfs
= vfs_op (handle
);
718 return close (handle
);
721 vfs_die ("VFS must support close.\n");
722 result
= (*vfs
->close
)(vfs_info (handle
));
723 vfs_free_handle (handle
);
725 errno
= ferrno (vfs
);
731 mc_opendir (const char *dirname
)
733 int handle
, *handlep
;
735 struct vfs_class
*vfs
;
738 struct vfs_dirinfo
*dirinfo
;
739 const char *encoding
;
741 canon
= vfs_canon (dirname
);
742 dname
= vfs_translate_path_n (canon
);
745 vfs
= vfs_get_class (dname
);
746 info
= vfs
->opendir
? (*vfs
->opendir
)(vfs
, dname
) : NULL
;
750 errno
= vfs
->opendir
? ferrno (vfs
) : E_NOTSUPP
;
755 dirinfo
= g_new (struct vfs_dirinfo
, 1);
756 dirinfo
->info
= info
;
758 encoding
= vfs_get_encoding (canon
);
760 dirinfo
->converter
= (encoding
!= NULL
) ? str_crt_conv_from (encoding
) :
762 if (dirinfo
->converter
== INVALID_CONV
) dirinfo
->converter
=str_cnv_from_term
;
764 handle
= vfs_new_handle (vfs
, dirinfo
);
766 handlep
= g_new (int, 1);
768 return (DIR *) handlep
;
775 static struct dirent
* mc_readdir_result
= NULL
;
778 mc_readdir (DIR *dirp
)
781 struct vfs_class
*vfs
;
782 struct dirent
*entry
= NULL
;
783 struct vfs_dirinfo
*dirinfo
;
786 if (!mc_readdir_result
)
788 /* We can't just allocate struct dirent as (see man dirent.h)
789 * struct dirent has VERY nonnaive semantics of allocating
790 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
791 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
792 * heap corrupter. So, allocate longliving dirent with at least
793 * (NAME_MAX + 1) for d_name in it.
794 * Strictly saying resulting dirent is unusable as we don't adjust internal
795 * structures, holding dirent size. But we don't use it in libc infrastructure.
796 * TODO: to make simpler homemade dirent-alike structure.
798 mc_readdir_result
= (struct dirent
*)malloc(sizeof(struct dirent
) + NAME_MAX
+ 1);
805 handle
= *(int *) dirp
;
807 vfs
= vfs_op (handle
);
811 dirinfo
= vfs_info (handle
);
813 entry
= (*vfs
->readdir
) (dirinfo
->info
);
814 if (entry
== NULL
) return NULL
;
815 g_string_set_size(vfs_str_buffer
,0);
816 state
= str_vfs_convert_from (dirinfo
->converter
,
817 entry
->d_name
, vfs_str_buffer
);
818 mc_readdir_result
->d_ino
= entry
->d_ino
;
819 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, NAME_MAX
+ 1);
821 if (entry
== NULL
) errno
= vfs
->readdir
? ferrno (vfs
) : E_NOTSUPP
;
822 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
826 mc_closedir (DIR *dirp
)
828 int handle
= *(int *) dirp
;
829 struct vfs_class
*vfs
= vfs_op (handle
);
831 struct vfs_dirinfo
*dirinfo
;
836 dirinfo
= vfs_info (handle
);
837 if (dirinfo
->converter
!= str_cnv_from_term
) str_close_conv (dirinfo
->converter
);
839 result
= vfs
->closedir
? (*vfs
->closedir
)(dirinfo
->info
) : -1;
840 vfs_free_handle (handle
);
846 int mc_stat (const char *filename
, struct stat
*buf
) {
847 struct vfs_class
*vfs
;
851 path
= vfs_canon_and_translate (filename
);
856 vfs
= vfs_get_class (path
);
863 result
= vfs
->stat
? (*vfs
->stat
) (vfs
, path
, buf
) : -1;
868 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
872 int mc_lstat (const char *filename
, struct stat
*buf
) {
873 struct vfs_class
*vfs
;
877 path
= vfs_canon_and_translate (filename
);
882 vfs
= vfs_get_class (path
);
888 result
= vfs
->lstat
? (*vfs
->lstat
) (vfs
, path
, buf
) : -1;
891 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
895 int mc_fstat (int handle
, struct stat
*buf
) {
896 struct vfs_class
*vfs
;
902 vfs
= vfs_op (handle
);
906 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_info (handle
), buf
) : -1;
908 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
913 * Return current directory. If it's local, reread the current directory
914 * from the OS. You must g_strdup() whatever this function returns.
921 const char *encoding
;
924 struct stat my_stat
, my_stat2
;
926 trans
= vfs_translate_path_n (current_dir
); /* add check if NULL */
928 if (!_vfs_get_class (trans
)) {
929 encoding
= vfs_get_encoding (current_dir
);
930 if (encoding
== NULL
) {
931 tmp
= g_get_current_dir ();
932 if (tmp
!= NULL
) { /* One of the directories in the path is not readable */
933 g_string_set_size(vfs_str_buffer
,0);
934 state
= str_vfs_convert_from (str_cnv_from_term
, tmp
, vfs_str_buffer
);
936 sys_cwd
= (state
== ESTR_SUCCESS
) ? g_strdup (vfs_str_buffer
->str
) : NULL
;
940 /* Otherwise check if it is O.K. to use the current_dir */
941 if (!cd_symlinks
|| mc_stat (sys_cwd
, &my_stat
)
942 || mc_stat (current_dir
, &my_stat2
)
943 || my_stat
.st_ino
!= my_stat2
.st_ino
944 || my_stat
.st_dev
!= my_stat2
.st_dev
) {
945 g_free (current_dir
);
946 current_dir
= sys_cwd
;
948 }/* Otherwise we return current_dir below */
959 current_dir
= g_strdup (PATH_SEP_STR
);
962 if (strlen (current_dir
) > MC_MAXPATHLEN
- 2)
963 vfs_die ("Current dir too long.\n");
965 current_vfs
= vfs_get_class (current_dir
);
969 * Return current directory. If it's local, reread the current directory
970 * from the OS. Put directory to the provided buffer.
973 mc_get_current_wd (char *buffer
, int size
)
975 const char *cwd
= _vfs_get_cwd ();
977 g_strlcpy (buffer
, cwd
, size
);
982 * Return current directory without any OS calls.
985 vfs_get_current_dir (void)
990 off_t
mc_lseek (int fd
, off_t offset
, int whence
)
992 struct vfs_class
*vfs
;
1002 result
= vfs
->lseek
? (*vfs
->lseek
)(vfs_info (fd
), offset
, whence
) : -1;
1004 errno
= vfs
->lseek
? ferrno (vfs
) : E_NOTSUPP
;
1009 * remove //, /./ and /../
1012 #define ISSLASH(a) (!a || (a == '/'))
1015 vfs_canon (const char *path
)
1018 vfs_die("Cannot canonicalize NULL");
1020 /* Relative to current directory */
1021 if (*path
!= PATH_SEP
){
1022 char *local
, *result
;
1024 local
= concat_dir_and_file (current_dir
, path
);
1026 result
= vfs_canon (local
);
1032 * So we have path of following form:
1033 * /p1/p2#op/.././././p3#op/p4. Good luck.
1036 char *result
= g_strdup (path
);
1037 canonicalize_pathname (result
);
1044 * Return 0 on success, -1 on failure.
1047 mc_chdir (const char *path
)
1051 struct vfs_class
*old_vfs
, *new_vfs
;
1055 new_dir
= vfs_canon (path
);
1056 trans_dir
= vfs_translate_path_n (new_dir
);
1057 if (trans_dir
!= NULL
) {
1058 new_vfs
= vfs_get_class (trans_dir
);
1059 if (!new_vfs
->chdir
) {
1065 result
= (*new_vfs
->chdir
) (new_vfs
, trans_dir
);
1068 errno
= ferrno (new_vfs
);
1074 old_vfsid
= vfs_getid (current_vfs
, current_dir
);
1075 old_vfs
= current_vfs
;
1077 /* Actually change directory */
1078 g_free (current_dir
);
1079 current_dir
= new_dir
;
1080 current_vfs
= new_vfs
;
1082 /* This function uses the new current_dir implicitly */
1083 vfs_stamp_create (old_vfs
, old_vfsid
);
1085 /* Sometimes we assume no trailing slash on cwd */
1088 p
= strchr (current_dir
, 0) - 1;
1089 if (*p
== PATH_SEP
&& p
> current_dir
)
1101 /* Return 1 is the current VFS class is local */
1103 vfs_current_is_local (void)
1105 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
1108 /* Return flags of the VFS class of the given filename */
1110 vfs_file_class_flags (const char *filename
)
1112 struct vfs_class
*vfs
;
1115 fname
= vfs_canon_and_translate (filename
);
1116 if (fname
!= NULL
) {
1117 vfs
= vfs_get_class (fname
);
1124 mc_def_getlocalcopy (const char *filename
)
1131 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
1135 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
1139 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1140 if (write (fdout
, buffer
, i
) != i
)
1145 i
= mc_close (fdin
);
1149 if (close (fdout
) == -1) {
1154 if (mc_stat (filename
, &mystat
) != -1) {
1155 chmod (tmp
, mystat
.st_mode
);
1169 mc_getlocalcopy (const char *pathname
)
1174 path
= vfs_canon_and_translate (pathname
);
1176 struct vfs_class
*vfs
= vfs_get_class (path
);
1178 result
= vfs
->getlocalcopy
? (*vfs
->getlocalcopy
)(vfs
, path
) :
1179 mc_def_getlocalcopy (path
);
1182 errno
= ferrno (vfs
);
1188 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
1189 const char *local
, int has_changed
)
1191 int fdin
= -1, fdout
= -1, i
;
1198 fdin
= open (local
, O_RDONLY
);
1201 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
1204 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1205 if (mc_write (fdout
, buffer
, i
) != i
)
1211 if (close (fdin
) == -1) {
1216 if (mc_close (fdout
) == -1) {
1225 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
1235 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
1237 int return_value
= 0;
1240 path
= vfs_canon_and_translate (pathname
);
1242 struct vfs_class
*vfs
= vfs_get_class (path
);
1244 return_value
= vfs
->ungetlocalcopy
?
1245 (*vfs
->ungetlocalcopy
)(vfs
, path
, local
, has_changed
) :
1246 mc_def_ungetlocalcopy (vfs
, path
, local
, has_changed
);
1248 return return_value
;
1256 /* create the VFS handle array */
1257 vfs_openfiles
= g_ptr_array_new ();
1259 vfs_str_buffer
= g_string_new("");
1260 /* localfs needs to be the first one */
1262 /* fallback value for vfs_get_class() */
1263 localfs_class
= vfs_list
;
1270 #ifdef USE_EXT2FSLIB
1272 #endif /* USE_EXT2FSLIB */
1280 #endif /* WITH_SMBFS */
1281 #ifdef ENABLE_VFS_MCFS
1283 #endif /* ENABLE_VFS_MCFS */
1284 #endif /* USE_NETCODE */
1292 struct vfs_class
*vfs
;
1296 g_free (current_dir
);
1298 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
1302 g_ptr_array_free (vfs_openfiles
, TRUE
);
1303 g_string_free (vfs_str_buffer
, TRUE
);
1304 g_free (mc_readdir_result
);
1308 * These ones grab information from the VFS
1309 * and handles them to an upper layer
1312 vfs_fill_names (fill_names_f func
)
1314 struct vfs_class
*vfs
;
1316 for (vfs
=vfs_list
; vfs
; vfs
=vfs
->next
)
1317 if (vfs
->fill_names
)
1318 (*vfs
->fill_names
) (vfs
, func
);
1322 * Returns vfs path corresponding to given url. If passed string is
1323 * not recognized as url, g_strdup(url) is returned.
1326 static const struct {
1329 const char *substitute
;
1330 } url_table
[] = { {"ftp://", 6, "/#ftp:"},
1331 {"mc://", 5, "/#mc:"},
1332 {"smb://", 6, "/#smb:"},
1333 {"sh://", 5, "/#sh:"},
1334 {"ssh://", 6, "/#sh:"},
1339 vfs_translate_url (const char *url
)
1343 for (i
= 0; i
< sizeof (url_table
)/sizeof (url_table
[0]); i
++)
1344 if (strncmp (url
, url_table
[i
].name
, url_table
[i
].name_len
) == 0)
1345 return g_strconcat (url_table
[i
].substitute
, url
+ url_table
[i
].name_len
, (char*) NULL
);
1347 return g_strdup (url
);
1350 int vfs_file_is_local (const char *filename
)
1352 return vfs_file_class_flags (filename
) & VFSF_LOCAL
;