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 inline 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
);
679 return vfs
->ctl
? (*vfs
->ctl
)(vfs_info (handle
), ctlop
, arg
) : 0;
683 mc_setctl (const char *path
, int ctlop
, void *arg
)
685 struct vfs_class
*vfs
;
690 vfs_die("You don't want to pass NULL to mc_setctl.");
692 mpath
= vfs_canon_and_translate (path
);
694 vfs
= vfs_get_class (mpath
);
695 result
= vfs
->setctl
? (*vfs
->setctl
)(vfs
, mpath
, ctlop
, arg
) : 0;
702 mc_close (int handle
)
704 struct vfs_class
*vfs
;
707 if (handle
== -1 || !vfs_info (handle
))
710 vfs
= vfs_op (handle
);
712 return close (handle
);
715 vfs_die ("VFS must support close.\n");
716 result
= (*vfs
->close
)(vfs_info (handle
));
717 vfs_free_handle (handle
);
719 errno
= ferrno (vfs
);
725 mc_opendir (const char *dirname
)
727 int handle
, *handlep
;
729 struct vfs_class
*vfs
;
732 struct vfs_dirinfo
*dirinfo
;
733 const char *encoding
;
735 canon
= vfs_canon (dirname
);
736 dname
= vfs_translate_path_n (canon
);
739 vfs
= vfs_get_class (dname
);
740 info
= vfs
->opendir
? (*vfs
->opendir
)(vfs
, dname
) : NULL
;
744 errno
= vfs
->opendir
? ferrno (vfs
) : E_NOTSUPP
;
749 dirinfo
= g_new (struct vfs_dirinfo
, 1);
750 dirinfo
->info
= info
;
752 encoding
= vfs_get_encoding (canon
);
754 dirinfo
->converter
= (encoding
!= NULL
) ? str_crt_conv_from (encoding
) :
756 if (dirinfo
->converter
== INVALID_CONV
) dirinfo
->converter
=str_cnv_from_term
;
758 handle
= vfs_new_handle (vfs
, dirinfo
);
760 handlep
= g_new (int, 1);
762 return (DIR *) handlep
;
769 static struct dirent
* mc_readdir_result
= NULL
;
772 mc_readdir (DIR *dirp
)
775 struct vfs_class
*vfs
;
776 struct dirent
*entry
= NULL
;
777 struct vfs_dirinfo
*dirinfo
;
780 if (!mc_readdir_result
)
782 /* We can't just allocate struct dirent as (see man dirent.h)
783 * struct dirent has VERY nonnaive semantics of allocating
784 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
785 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
786 * heap corrupter. So, allocate longliving dirent with at least
787 * (NAME_MAX + 1) for d_name in it.
788 * Strictly saying resulting dirent is unusable as we don't adjust internal
789 * structures, holding dirent size. But we don't use it in libc infrastructure.
790 * TODO: to make simpler homemade dirent-alike structure.
792 mc_readdir_result
= (struct dirent
*)malloc(sizeof(struct dirent
) + NAME_MAX
+ 1);
799 handle
= *(int *) dirp
;
800 vfs
= vfs_op (handle
);
801 dirinfo
= vfs_info (handle
);
803 entry
= (*vfs
->readdir
) (dirinfo
->info
);
804 if (entry
== NULL
) return NULL
;
805 g_string_set_size(vfs_str_buffer
,0);
806 state
= str_vfs_convert_from (dirinfo
->converter
,
807 entry
->d_name
, vfs_str_buffer
);
808 mc_readdir_result
->d_ino
= entry
->d_ino
;
809 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, NAME_MAX
+ 1);
811 if (entry
== NULL
) errno
= vfs
->readdir
? ferrno (vfs
) : E_NOTSUPP
;
812 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
816 mc_closedir (DIR *dirp
)
818 int handle
= *(int *) dirp
;
819 struct vfs_class
*vfs
= vfs_op (handle
);
821 struct vfs_dirinfo
*dirinfo
;
823 dirinfo
= vfs_info (handle
);
824 if (dirinfo
->converter
!= str_cnv_from_term
) str_close_conv (dirinfo
->converter
);
826 result
= vfs
->closedir
? (*vfs
->closedir
)(dirinfo
->info
) : -1;
827 vfs_free_handle (handle
);
833 int mc_stat (const char *filename
, struct stat
*buf
) {
834 struct vfs_class
*vfs
;
838 path
= vfs_canon_and_translate (filename
);
841 vfs
= vfs_get_class (path
);
843 result
= vfs
->stat
? (*vfs
->stat
) (vfs
, path
, buf
) : -1;
848 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
853 int mc_lstat (const char *filename
, struct stat
*buf
) {
854 struct vfs_class
*vfs
;
858 path
= vfs_canon_and_translate (filename
);
861 vfs
= vfs_get_class (path
);
862 result
= vfs
->lstat
? (*vfs
->lstat
) (vfs
, path
, buf
) : -1;
865 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
870 int mc_fstat (int handle
, struct stat
*buf
) {
871 struct vfs_class
*vfs
;
876 vfs
= vfs_op (handle
);
877 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_info (handle
), buf
) : -1;
879 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
884 * Return current directory. If it's local, reread the current directory
885 * from the OS. You must g_strdup() whatever this function returns.
892 const char *encoding
;
895 struct stat my_stat
, my_stat2
;
897 trans
= vfs_translate_path_n (current_dir
); /* add check if NULL */
899 if (!_vfs_get_class (trans
)) {
900 encoding
= vfs_get_encoding (current_dir
);
901 if (encoding
== NULL
) {
902 tmp
= g_get_current_dir ();
903 if (tmp
!= NULL
) { /* One of the directories in the path is not readable */
904 g_string_set_size(vfs_str_buffer
,0);
905 state
= str_vfs_convert_from (str_cnv_from_term
, tmp
, vfs_str_buffer
);
907 sys_cwd
= (state
== ESTR_SUCCESS
) ? g_strdup (vfs_str_buffer
->str
) : NULL
;
911 /* Otherwise check if it is O.K. to use the current_dir */
912 if (!cd_symlinks
|| mc_stat (sys_cwd
, &my_stat
)
913 || mc_stat (current_dir
, &my_stat2
)
914 || my_stat
.st_ino
!= my_stat2
.st_ino
915 || my_stat
.st_dev
!= my_stat2
.st_dev
) {
916 g_free (current_dir
);
917 current_dir
= sys_cwd
;
919 }/* Otherwise we return current_dir below */
930 current_dir
= g_strdup (PATH_SEP_STR
);
933 if (strlen (current_dir
) > MC_MAXPATHLEN
- 2)
934 vfs_die ("Current dir too long.\n");
936 current_vfs
= vfs_get_class (current_dir
);
940 * Return current directory. If it's local, reread the current directory
941 * from the OS. Put directory to the provided buffer.
944 mc_get_current_wd (char *buffer
, int size
)
946 const char *cwd
= _vfs_get_cwd ();
948 g_strlcpy (buffer
, cwd
, size
);
953 * Return current directory without any OS calls.
956 vfs_get_current_dir (void)
961 off_t
mc_lseek (int fd
, off_t offset
, int whence
)
963 struct vfs_class
*vfs
;
970 result
= vfs
->lseek
? (*vfs
->lseek
)(vfs_info (fd
), offset
, whence
) : -1;
972 errno
= vfs
->lseek
? ferrno (vfs
) : E_NOTSUPP
;
977 * remove //, /./ and /../
980 #define ISSLASH(a) (!a || (a == '/'))
983 vfs_canon (const char *path
)
986 vfs_die("Cannot canonicalize NULL");
988 /* Relative to current directory */
989 if (*path
!= PATH_SEP
){
990 char *local
, *result
;
992 local
= concat_dir_and_file (current_dir
, path
);
994 result
= vfs_canon (local
);
1000 * So we have path of following form:
1001 * /p1/p2#op/.././././p3#op/p4. Good luck.
1004 char *result
= g_strdup (path
);
1005 canonicalize_pathname (result
);
1012 * Return 0 on success, -1 on failure.
1015 mc_chdir (const char *path
)
1019 struct vfs_class
*old_vfs
, *new_vfs
;
1023 new_dir
= vfs_canon (path
);
1024 trans_dir
= vfs_translate_path_n (new_dir
);
1025 if (trans_dir
!= NULL
) {
1026 new_vfs
= vfs_get_class (trans_dir
);
1027 if (!new_vfs
->chdir
) {
1033 result
= (*new_vfs
->chdir
) (new_vfs
, trans_dir
);
1036 errno
= ferrno (new_vfs
);
1042 old_vfsid
= vfs_getid (current_vfs
, current_dir
);
1043 old_vfs
= current_vfs
;
1045 /* Actually change directory */
1046 g_free (current_dir
);
1047 current_dir
= new_dir
;
1048 current_vfs
= new_vfs
;
1050 /* This function uses the new current_dir implicitly */
1051 vfs_stamp_create (old_vfs
, old_vfsid
);
1053 /* Sometimes we assume no trailing slash on cwd */
1056 p
= strchr (current_dir
, 0) - 1;
1057 if (*p
== PATH_SEP
&& p
> current_dir
)
1069 /* Return 1 is the current VFS class is local */
1071 vfs_current_is_local (void)
1073 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
1076 /* Return flags of the VFS class of the given filename */
1078 vfs_file_class_flags (const char *filename
)
1080 struct vfs_class
*vfs
;
1083 fname
= vfs_canon_and_translate (filename
);
1084 if (fname
!= NULL
) {
1085 vfs
= vfs_get_class (fname
);
1092 mc_def_getlocalcopy (const char *filename
)
1099 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
1103 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
1107 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1108 if (write (fdout
, buffer
, i
) != i
)
1113 i
= mc_close (fdin
);
1117 if (close (fdout
) == -1) {
1122 if (mc_stat (filename
, &mystat
) != -1) {
1123 chmod (tmp
, mystat
.st_mode
);
1137 mc_getlocalcopy (const char *pathname
)
1142 path
= vfs_canon_and_translate (pathname
);
1144 struct vfs_class
*vfs
= vfs_get_class (path
);
1146 result
= vfs
->getlocalcopy
? (*vfs
->getlocalcopy
)(vfs
, path
) :
1147 mc_def_getlocalcopy (path
);
1150 errno
= ferrno (vfs
);
1156 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
1157 const char *local
, int has_changed
)
1159 int fdin
= -1, fdout
= -1, i
;
1166 fdin
= open (local
, O_RDONLY
);
1169 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
1172 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1173 if (mc_write (fdout
, buffer
, i
) != i
)
1179 if (close (fdin
) == -1) {
1184 if (mc_close (fdout
) == -1) {
1193 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
1203 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
1205 int return_value
= 0;
1208 path
= vfs_canon_and_translate (pathname
);
1210 struct vfs_class
*vfs
= vfs_get_class (path
);
1212 return_value
= vfs
->ungetlocalcopy
?
1213 (*vfs
->ungetlocalcopy
)(vfs
, path
, local
, has_changed
) :
1214 mc_def_ungetlocalcopy (vfs
, path
, local
, has_changed
);
1216 return return_value
;
1224 /* create the VFS handle array */
1225 vfs_openfiles
= g_ptr_array_new ();
1227 vfs_str_buffer
= g_string_new("");
1228 /* localfs needs to be the first one */
1230 /* fallback value for vfs_get_class() */
1231 localfs_class
= vfs_list
;
1238 #ifdef USE_EXT2FSLIB
1240 #endif /* USE_EXT2FSLIB */
1248 #endif /* WITH_SMBFS */
1249 #ifdef ENABLE_VFS_MCFS
1251 #endif /* ENABLE_VFS_MCFS */
1252 #endif /* USE_NETCODE */
1260 struct vfs_class
*vfs
;
1264 g_free (current_dir
);
1266 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
1270 g_ptr_array_free (vfs_openfiles
, TRUE
);
1272 g_string_free (vfs_str_buffer
, TRUE
);
1276 * These ones grab information from the VFS
1277 * and handles them to an upper layer
1280 vfs_fill_names (fill_names_f func
)
1282 struct vfs_class
*vfs
;
1284 for (vfs
=vfs_list
; vfs
; vfs
=vfs
->next
)
1285 if (vfs
->fill_names
)
1286 (*vfs
->fill_names
) (vfs
, func
);
1290 * Returns vfs path corresponding to given url. If passed string is
1291 * not recognized as url, g_strdup(url) is returned.
1294 static const struct {
1297 const char *substitute
;
1298 } url_table
[] = { {"ftp://", 6, "/#ftp:"},
1299 {"mc://", 5, "/#mc:"},
1300 {"smb://", 6, "/#smb:"},
1301 {"sh://", 5, "/#sh:"},
1302 {"ssh://", 6, "/#sh:"},
1307 vfs_translate_url (const char *url
)
1311 for (i
= 0; i
< sizeof (url_table
)/sizeof (url_table
[0]); i
++)
1312 if (strncmp (url
, url_table
[i
].name
, url_table
[i
].name_len
) == 0)
1313 return g_strconcat (url_table
[i
].substitute
, url
+ url_table
[i
].name_len
, (char*) NULL
);
1315 return g_strdup (url
);
1318 int vfs_file_is_local (const char *filename
)
1320 return vfs_file_class_flags (filename
) & VFSF_LOCAL
;