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 "lib/global.h"
54 #include "lib/strutil.h"
56 #include "src/wtools.h" /* message() */
57 #include "src/main.h" /* print_vfs_message */
70 #if defined(_AIX) && !defined(NAME_MAX)
71 # define NAME_MAX FILENAME_MAX
74 /** They keep track of the current directory */
75 static struct vfs_class
*current_vfs
;
76 static char *current_dir
;
80 struct vfs_class
*vclass
;
90 static GPtrArray
*vfs_openfiles
;
91 static long vfs_free_handle_list
= -1;
92 #define VFS_FIRST_HANDLE 100
94 static struct vfs_class
*localfs_class
;
95 static GString
*vfs_str_buffer
;
97 static const char *supported_encodings
[] = {
112 /** Create new VFS handle and put it to the list */
114 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
116 struct vfs_openfile
*h
;
118 h
= g_new (struct vfs_openfile
, 1);
122 /* Allocate the first free handle */
123 h
->handle
= vfs_free_handle_list
;
124 if (h
->handle
== -1) {
125 /* No free allocated handles, allocate one */
126 h
->handle
= vfs_openfiles
->len
;
127 g_ptr_array_add (vfs_openfiles
, h
);
129 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
130 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
133 h
->handle
+= VFS_FIRST_HANDLE
;
137 /** Find VFS class by file handle */
138 static struct vfs_class
*
141 struct vfs_openfile
*h
;
143 if (handle
< VFS_FIRST_HANDLE
||
144 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
147 h
= (struct vfs_openfile
*) g_ptr_array_index (
148 vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
152 g_assert (h
->handle
== handle
);
157 /** Find private file data by file handle */
159 vfs_info (int handle
)
161 struct vfs_openfile
*h
;
163 if (handle
< VFS_FIRST_HANDLE
||
164 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
167 h
= (struct vfs_openfile
*) g_ptr_array_index (
168 vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
172 g_assert (h
->handle
== handle
);
177 /** Free open file data for given file handle */
179 vfs_free_handle (int handle
)
181 if (handle
< VFS_FIRST_HANDLE
||
182 (guint
)(handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
185 g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
) =
186 (void *) vfs_free_handle_list
;
187 vfs_free_handle_list
= handle
- VFS_FIRST_HANDLE
;
190 static struct vfs_class
*vfs_list
;
193 vfs_register_class (struct vfs_class
*vfs
)
195 if (vfs
->init
) /* vfs has own initialization function */
196 if (!(*vfs
->init
)(vfs
)) /* but it failed */
199 vfs
->next
= vfs_list
;
205 /** Return VFS class for the given prefix */
206 static struct vfs_class
*
207 vfs_prefix_to_class (char *prefix
)
209 struct vfs_class
*vfs
;
211 /* Avoid last class (localfs) that would accept any prefix */
212 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
214 if ((*vfs
->which
) (vfs
, prefix
) == -1)
219 && !strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)))
225 /** Strip known vfs suffixes from a filename (possible improvement: strip
226 * suffix from last path component).
227 * \return a malloced string which has to be freed.
230 vfs_strip_suffix_from_filename (const char *filename
)
232 struct vfs_class
*vfs
;
237 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
239 p
= g_strdup (filename
);
240 if (!(semi
= strrchr (p
, '#')))
243 /* Avoid last class (localfs) that would accept any prefix */
244 for (vfs
= vfs_list
; vfs
->next
; vfs
= vfs
->next
) {
246 if ((*vfs
->which
) (vfs
, semi
+ 1) == -1)
248 *semi
= '\0'; /* Found valid suffix */
252 && !strncmp (semi
+ 1, vfs
->prefix
, strlen (vfs
->prefix
))) {
253 *semi
= '\0'; /* Found valid suffix */
261 path_magic (const char *path
)
265 if (!stat(path
, &buf
))
272 * Splits path extracting vfs part.
275 * \verbatim /p1#op/inpath \endverbatim
277 * \verbatim inpath,op; \endverbatim
278 * returns which vfs it is.
279 * What is left in path is p1. You still want to g_free(path), you DON'T
280 * want to free neither *inpath nor *op
283 vfs_split (char *path
, char **inpath
, char **op
)
287 struct vfs_class
*ret
;
290 vfs_die("Cannot split NULL");
292 semi
= strrchr (path
, '#');
293 if (!semi
|| !path_magic(path
))
296 slash
= strchr (semi
, PATH_SEP
);
308 if ((ret
= vfs_prefix_to_class (semi
+1))){
312 *inpath
= slash
? slash
+ 1 : NULL
;
319 ret
= vfs_split (path
, inpath
, op
);
324 static struct vfs_class
*
325 _vfs_get_class (char *path
)
329 struct vfs_class
*ret
;
331 g_return_val_if_fail(path
, NULL
);
333 semi
= strrchr (path
, '#');
334 if (!semi
|| !path_magic (path
))
337 slash
= strchr (semi
, PATH_SEP
);
342 ret
= vfs_prefix_to_class (semi
+1);
347 ret
= _vfs_get_class (path
);
354 vfs_get_class (const char *pathname
)
356 struct vfs_class
*vfs
;
357 char *path
= g_strdup (pathname
);
359 vfs
= _vfs_get_class (path
);
369 vfs_get_encoding (const char *path
)
371 static char result
[16];
376 work
= g_strdup (path
);
377 semi
= g_strrstr (work
, "#enc:");
380 semi
+= 5 * sizeof (char);
381 slash
= strchr (semi
, PATH_SEP
);
385 g_strlcpy (result
, semi
, sizeof(result
));
394 /* return if encoding can by used in vfs (is ascci full compactible) */
395 /* contains only a few encoding now */
397 vfs_supported_enconding (const char *encoding
) {
401 for (t
= 0; supported_encodings
[t
] != NULL
; t
++) {
402 result
+= (g_ascii_strncasecmp (encoding
, supported_encodings
[t
],
403 strlen (supported_encodings
[t
])) == 0);
409 /* now used only by vfs_translate_path, but could be used in other vfs
410 * plugin to automatic detect encoding
411 * path - path to translate
412 * size - how many bytes from path translate
413 * defcnv - convertor, that is used as default, when path does not contain any
415 * buffer - used to store result of translation
418 _vfs_translate_path (const char *path
, int size
,
419 GIConv defcnv
, GString
*buffer
)
424 estr_t state
= ESTR_SUCCESS
;
425 static char encoding
[16];
429 if (size
== 0) return 0;
430 size
= ( size
> 0) ? size
: (signed int)strlen (path
);
432 /* try found #end: */
433 semi
= g_strrstr_len (path
, size
, "#enc:");
435 /* first must be translated part before #enc: */
438 /* remove '/' before #enc */
439 ps
= str_cget_prev_char (semi
);
440 if (ps
[0] == PATH_SEP
) ms
= ps
- path
;
442 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
444 if (state
!= ESTR_SUCCESS
)
446 /* now can be translated part after #enc: */
449 slash
= strchr (semi
, PATH_SEP
);
450 /* ignore slashes after size; */
451 if (slash
- path
>= size
) slash
= NULL
;
453 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
454 ms
= min ((unsigned int) ms
, sizeof (encoding
) - 1);
455 /* limit encoding size (ms) to path size (size) */
456 if (semi
+ ms
> path
+ size
) ms
= path
+ size
- semi
;
457 memcpy (encoding
, semi
, ms
);
460 switch (vfs_supported_enconding (encoding
)) {
462 coder
= str_crt_conv_to (encoding
);
463 if (coder
!= INVALID_CONV
) {
465 state
= str_vfs_convert_to (coder
, slash
,
466 path
+ size
- slash
, buffer
);
467 } else if (buffer
->str
[0] == '\0') {
468 /* exmaple "/#enc:utf-8" */
469 g_string_append_c(buffer
, PATH_SEP
);
471 str_close_conv (coder
);
483 /* path can be translated whole at once */
484 state
= str_vfs_convert_to (defcnv
, path
, size
, buffer
);
492 vfs_translate_path (const char *path
)
496 g_string_set_size(vfs_str_buffer
,0);
497 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
500 return (state == 0) ? vfs_str_buffer->data : NULL;
502 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
506 vfs_translate_path_n (const char *path
)
510 result
= vfs_translate_path (path
);
511 return (result
!= NULL
) ? g_strdup (result
) : NULL
;
515 vfs_canon_and_translate (const char *path
)
520 canon
= g_strdup ("");
522 canon
= vfs_canon (path
);
523 result
= vfs_translate_path_n (canon
);
529 ferrno (struct vfs_class
*vfs
)
531 return vfs
->ferrno
? (*vfs
->ferrno
)(vfs
) : E_UNKNOWN
;
532 /* Hope that error message is obscure enough ;-) */
536 mc_open (const char *filename
, int flags
, ...)
542 char *file
= vfs_canon_and_translate (filename
);
544 struct vfs_class
*vfs
= vfs_get_class (file
);
546 /* Get the mode flag */
547 if (flags
& O_CREAT
) {
548 va_start (ap
, flags
);
549 mode
= va_arg (ap
, int);
560 info
= (*vfs
->open
) (vfs
, file
, flags
, mode
); /* open must be supported */
563 errno
= ferrno (vfs
);
567 return vfs_new_handle (vfs
, info
);
572 #define MC_NAMEOP(name, inarg, callarg) \
573 int mc_##name inarg \
575 struct vfs_class *vfs; \
577 char *mpath = vfs_canon_and_translate (path); \
578 if (mpath != NULL) { \
579 vfs = vfs_get_class (mpath); \
584 result = vfs->name ? (*vfs->name)callarg : -1; \
587 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
592 MC_NAMEOP (chmod
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
593 MC_NAMEOP (chown
, (const char *path
, uid_t owner
, gid_t group
), (vfs
, mpath
, owner
, group
))
594 MC_NAMEOP (utime
, (const char *path
, struct utimbuf
*times
), (vfs
, mpath
, times
))
595 MC_NAMEOP (readlink
, (const char *path
, char *buf
, int bufsiz
), (vfs
, mpath
, buf
, bufsiz
))
596 MC_NAMEOP (unlink
, (const char *path
), (vfs
, mpath
))
597 MC_NAMEOP (mkdir
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
598 MC_NAMEOP (rmdir
, (const char *path
), (vfs
, mpath
))
599 MC_NAMEOP (mknod
, (const char *path
, mode_t mode
, dev_t dev
), (vfs
, mpath
, mode
, dev
))
602 mc_symlink (const char *name1
, const char *path
)
604 struct vfs_class
*vfs
;
610 mpath
= vfs_canon_and_translate (path
);
612 tmp
= g_strdup (name1
);
613 lpath
= vfs_translate_path_n (tmp
);
617 vfs
= vfs_get_class (mpath
);
618 result
= vfs
->symlink
? (*vfs
->symlink
) (vfs
, lpath
, mpath
) : -1;
623 errno
= vfs
->symlink
? ferrno (vfs
) : E_NOTSUPP
;
631 #define MC_HANDLEOP(name, inarg, callarg) \
632 ssize_t mc_##name inarg \
634 struct vfs_class *vfs; \
638 vfs = vfs_op (handle); \
641 result = vfs->name ? (*vfs->name)callarg : -1; \
643 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
647 MC_HANDLEOP(read
, (int handle
, void *buffer
, int count
), (vfs_info (handle
), buffer
, count
))
648 MC_HANDLEOP(write
, (int handle
, const void *buf
, int nbyte
), (vfs_info (handle
), buf
, nbyte
))
651 #define MC_RENAMEOP(name) \
652 int mc_##name (const char *fname1, const char *fname2) \
654 struct vfs_class *vfs; \
656 char *name2, *name1; \
657 name1 = vfs_canon_and_translate (fname1); \
658 if (name1 != NULL) { \
659 name2 = vfs_canon_and_translate (fname2); \
660 if (name2 != NULL) { \
661 vfs = vfs_get_class (name1); \
662 if (vfs != vfs_get_class (name2)){ \
668 result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
672 errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
686 mc_ctl (int handle
, int ctlop
, void *arg
)
688 struct vfs_class
*vfs
= vfs_op (handle
);
693 return vfs
->ctl
? (*vfs
->ctl
)(vfs_info (handle
), ctlop
, arg
) : 0;
697 mc_setctl (const char *path
, int ctlop
, void *arg
)
699 struct vfs_class
*vfs
;
704 vfs_die("You don't want to pass NULL to mc_setctl.");
706 mpath
= vfs_canon_and_translate (path
);
708 vfs
= vfs_get_class (mpath
);
709 result
= vfs
->setctl
? (*vfs
->setctl
)(vfs
, mpath
, ctlop
, arg
) : 0;
716 mc_close (int handle
)
718 struct vfs_class
*vfs
;
721 if (handle
== -1 || !vfs_info (handle
))
724 vfs
= vfs_op (handle
);
729 return close (handle
);
732 vfs_die ("VFS must support close.\n");
733 result
= (*vfs
->close
)(vfs_info (handle
));
734 vfs_free_handle (handle
);
736 errno
= ferrno (vfs
);
742 mc_opendir (const char *dirname
)
744 int handle
, *handlep
;
746 struct vfs_class
*vfs
;
749 struct vfs_dirinfo
*dirinfo
;
750 const char *encoding
;
752 canon
= vfs_canon (dirname
);
753 dname
= vfs_translate_path_n (canon
);
756 vfs
= vfs_get_class (dname
);
757 info
= vfs
->opendir
? (*vfs
->opendir
)(vfs
, dname
) : NULL
;
761 errno
= vfs
->opendir
? ferrno (vfs
) : E_NOTSUPP
;
766 dirinfo
= g_new (struct vfs_dirinfo
, 1);
767 dirinfo
->info
= info
;
769 encoding
= vfs_get_encoding (canon
);
771 dirinfo
->converter
= (encoding
!= NULL
) ? str_crt_conv_from (encoding
) :
773 if (dirinfo
->converter
== INVALID_CONV
) dirinfo
->converter
=str_cnv_from_term
;
775 handle
= vfs_new_handle (vfs
, dirinfo
);
777 handlep
= g_new (int, 1);
779 return (DIR *) handlep
;
786 static struct dirent
* mc_readdir_result
= NULL
;
789 mc_readdir (DIR *dirp
)
792 struct vfs_class
*vfs
;
793 struct dirent
*entry
= NULL
;
794 struct vfs_dirinfo
*dirinfo
;
797 if (!mc_readdir_result
)
799 /* We can't just allocate struct dirent as (see man dirent.h)
800 * struct dirent has VERY nonnaive semantics of allocating
801 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
802 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
803 * heap corrupter. So, allocate longliving dirent with at least
804 * (MAXNAMLEN + 1) for d_name in it.
805 * Strictly saying resulting dirent is unusable as we don't adjust internal
806 * structures, holding dirent size. But we don't use it in libc infrastructure.
807 * TODO: to make simpler homemade dirent-alike structure.
809 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof(struct dirent
) + MAXNAMLEN
+ 1);
816 handle
= *(int *) dirp
;
818 vfs
= vfs_op (handle
);
822 dirinfo
= vfs_info (handle
);
824 entry
= (*vfs
->readdir
) (dirinfo
->info
);
825 if (entry
== NULL
) return NULL
;
826 g_string_set_size(vfs_str_buffer
,0);
827 state
= str_vfs_convert_from (dirinfo
->converter
,
828 entry
->d_name
, vfs_str_buffer
);
829 mc_readdir_result
->d_ino
= entry
->d_ino
;
830 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
832 if (entry
== NULL
) errno
= vfs
->readdir
? ferrno (vfs
) : E_NOTSUPP
;
833 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
837 mc_closedir (DIR *dirp
)
839 int handle
= *(int *) dirp
;
840 struct vfs_class
*vfs
= vfs_op (handle
);
842 struct vfs_dirinfo
*dirinfo
;
847 dirinfo
= vfs_info (handle
);
848 if (dirinfo
->converter
!= str_cnv_from_term
) str_close_conv (dirinfo
->converter
);
850 result
= vfs
->closedir
? (*vfs
->closedir
)(dirinfo
->info
) : -1;
851 vfs_free_handle (handle
);
857 int mc_stat (const char *filename
, struct stat
*buf
) {
858 struct vfs_class
*vfs
;
862 path
= vfs_canon_and_translate (filename
);
867 vfs
= vfs_get_class (path
);
874 result
= vfs
->stat
? (*vfs
->stat
) (vfs
, path
, buf
) : -1;
879 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
883 int mc_lstat (const char *filename
, struct stat
*buf
) {
884 struct vfs_class
*vfs
;
888 path
= vfs_canon_and_translate (filename
);
893 vfs
= vfs_get_class (path
);
899 result
= vfs
->lstat
? (*vfs
->lstat
) (vfs
, path
, buf
) : -1;
902 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
906 int mc_fstat (int handle
, struct stat
*buf
) {
907 struct vfs_class
*vfs
;
913 vfs
= vfs_op (handle
);
917 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_info (handle
), buf
) : -1;
919 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
924 * Return current directory. If it's local, reread the current directory
925 * from the OS. You must g_strdup() whatever this function returns.
932 trans
= vfs_translate_path_n (current_dir
);
934 if (_vfs_get_class (trans
) == NULL
) {
935 const char *encoding
= vfs_get_encoding (current_dir
);
937 if (encoding
== NULL
) {
940 tmp
= g_get_current_dir ();
941 if (tmp
!= NULL
) { /* One of the directories in the path is not readable */
945 g_string_set_size (vfs_str_buffer
, 0);
946 state
= str_vfs_convert_from (str_cnv_from_term
, tmp
, vfs_str_buffer
);
949 sys_cwd
= (state
== ESTR_SUCCESS
) ? g_strdup (vfs_str_buffer
->str
) : NULL
;
950 if (sys_cwd
!= NULL
) {
951 struct stat my_stat
, my_stat2
;
952 /* Check if it is O.K. to use the current_dir */
954 && mc_stat (sys_cwd
, &my_stat
) == 0
955 && mc_stat (current_dir
, &my_stat2
) == 0
956 && my_stat
.st_ino
== my_stat2
.st_ino
957 && my_stat
.st_dev
== my_stat2
.st_dev
)
960 g_free (current_dir
);
961 current_dir
= sys_cwd
;
975 current_dir
= g_strdup (PATH_SEP_STR
);
978 if (strlen (current_dir
) > MC_MAXPATHLEN
- 2)
979 vfs_die ("Current dir too long.\n");
981 current_vfs
= vfs_get_class (current_dir
);
985 * Return current directory. If it's local, reread the current directory
986 * from the OS. Put directory to the provided buffer.
989 mc_get_current_wd (char *buffer
, int size
)
991 const char *cwd
= _vfs_get_cwd ();
993 g_strlcpy (buffer
, cwd
, size
);
998 * Return current directory without any OS calls.
1001 vfs_get_current_dir (void)
1006 off_t
mc_lseek (int fd
, off_t offset
, int whence
)
1008 struct vfs_class
*vfs
;
1018 result
= vfs
->lseek
? (*vfs
->lseek
)(vfs_info (fd
), offset
, whence
) : -1;
1020 errno
= vfs
->lseek
? ferrno (vfs
) : E_NOTSUPP
;
1025 * remove //, /./ and /../
1028 #define ISSLASH(a) (!a || (a == '/'))
1031 vfs_canon (const char *path
)
1034 vfs_die("Cannot canonicalize NULL");
1036 /* Relative to current directory */
1037 if (*path
!= PATH_SEP
){
1038 char *local
, *result
;
1040 local
= concat_dir_and_file (current_dir
, path
);
1042 result
= vfs_canon (local
);
1048 * So we have path of following form:
1049 * /p1/p2#op/.././././p3#op/p4. Good luck.
1052 char *result
= g_strdup (path
);
1053 canonicalize_pathname (result
);
1060 * Return 0 on success, -1 on failure.
1063 mc_chdir (const char *path
)
1067 struct vfs_class
*old_vfs
, *new_vfs
;
1071 new_dir
= vfs_canon (path
);
1072 trans_dir
= vfs_translate_path_n (new_dir
);
1073 if (trans_dir
!= NULL
) {
1074 new_vfs
= vfs_get_class (trans_dir
);
1075 if (!new_vfs
->chdir
) {
1081 result
= (*new_vfs
->chdir
) (new_vfs
, trans_dir
);
1084 errno
= ferrno (new_vfs
);
1090 old_vfsid
= vfs_getid (current_vfs
, current_dir
);
1091 old_vfs
= current_vfs
;
1093 /* Actually change directory */
1094 g_free (current_dir
);
1095 current_dir
= new_dir
;
1096 current_vfs
= new_vfs
;
1098 /* This function uses the new current_dir implicitly */
1099 vfs_stamp_create (old_vfs
, old_vfsid
);
1101 /* Sometimes we assume no trailing slash on cwd */
1104 p
= strchr (current_dir
, 0) - 1;
1105 if (*p
== PATH_SEP
&& p
> current_dir
)
1117 /* Return 1 is the current VFS class is local */
1119 vfs_current_is_local (void)
1121 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
1124 /* Return flags of the VFS class of the given filename */
1126 vfs_file_class_flags (const char *filename
)
1128 struct vfs_class
*vfs
;
1131 fname
= vfs_canon_and_translate (filename
);
1132 if (fname
!= NULL
) {
1133 vfs
= vfs_get_class (fname
);
1140 mc_def_getlocalcopy (const char *filename
)
1148 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
1152 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
1156 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1157 if (write (fdout
, buffer
, i
) != i
)
1162 i
= mc_close (fdin
);
1166 if (close (fdout
) == -1) {
1171 if (mc_stat (filename
, &mystat
) != -1) {
1172 chmod (tmp
, mystat
.st_mode
);
1186 mc_getlocalcopy (const char *pathname
)
1191 path
= vfs_canon_and_translate (pathname
);
1193 struct vfs_class
*vfs
= vfs_get_class (path
);
1195 result
= vfs
->getlocalcopy
? (*vfs
->getlocalcopy
)(vfs
, path
) :
1196 mc_def_getlocalcopy (path
);
1199 errno
= ferrno (vfs
);
1205 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
1206 const char *local
, int has_changed
)
1208 int fdin
= -1, fdout
= -1, i
;
1215 fdin
= open (local
, O_RDONLY
);
1218 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
1221 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0) {
1222 if (mc_write (fdout
, buffer
, i
) != i
)
1228 if (close (fdin
) == -1) {
1233 if (mc_close (fdout
) == -1) {
1242 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
1252 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
1254 int return_value
= 0;
1257 path
= vfs_canon_and_translate (pathname
);
1259 struct vfs_class
*vfs
= vfs_get_class (path
);
1261 return_value
= vfs
->ungetlocalcopy
?
1262 (*vfs
->ungetlocalcopy
)(vfs
, path
, local
, has_changed
) :
1263 mc_def_ungetlocalcopy (vfs
, path
, local
, has_changed
);
1265 return return_value
;
1273 /* create the VFS handle array */
1274 vfs_openfiles
= g_ptr_array_new ();
1276 vfs_str_buffer
= g_string_new("");
1277 /* localfs needs to be the first one */
1279 /* fallback value for vfs_get_class() */
1280 localfs_class
= vfs_list
;
1287 #ifdef USE_EXT2FSLIB
1289 #endif /* USE_EXT2FSLIB */
1294 #ifdef ENABLE_VFS_SMB
1296 #endif /* ENABLE_VFS_SMB */
1297 #ifdef ENABLE_VFS_MCFS
1299 #endif /* ENABLE_VFS_MCFS */
1300 #endif /* USE_NETCODE */
1308 struct vfs_class
*vfs
;
1312 g_free (current_dir
);
1314 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
1318 g_ptr_array_free (vfs_openfiles
, TRUE
);
1319 g_string_free (vfs_str_buffer
, TRUE
);
1320 g_free (mc_readdir_result
);
1324 * These ones grab information from the VFS
1325 * and handles them to an upper layer
1328 vfs_fill_names (fill_names_f func
)
1330 struct vfs_class
*vfs
;
1332 for (vfs
=vfs_list
; vfs
; vfs
=vfs
->next
)
1333 if (vfs
->fill_names
)
1334 (*vfs
->fill_names
) (vfs
, func
);
1338 * Returns vfs path corresponding to given url. If passed string is
1339 * not recognized as url, g_strdup(url) is returned.
1342 static const struct {
1345 const char *substitute
;
1346 } url_table
[] = { {"ftp://", 6, "/#ftp:"},
1347 {"mc://", 5, "/#mc:"},
1348 {"smb://", 6, "/#smb:"},
1349 {"sh://", 5, "/#sh:"},
1350 {"ssh://", 6, "/#sh:"},
1355 vfs_translate_url (const char *url
)
1359 for (i
= 0; i
< sizeof (url_table
)/sizeof (url_table
[0]); i
++)
1360 if (strncmp (url
, url_table
[i
].name
, url_table
[i
].name_len
) == 0)
1361 return g_strconcat (url_table
[i
].substitute
, url
+ url_table
[i
].name_len
, (char*) NULL
);
1363 return g_strdup (url
);
1366 int vfs_file_is_local (const char *filename
)
1368 return vfs_file_class_flags (filename
) & VFSF_LOCAL
;