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
;
81 struct vfs_class
*vclass
;
92 static GPtrArray
*vfs_openfiles
;
93 static long vfs_free_handle_list
= -1;
94 #define VFS_FIRST_HANDLE 100
96 static struct vfs_class
*localfs_class
;
97 static GString
*vfs_str_buffer
;
99 static const char *supported_encodings
[] = {
114 /** Create new VFS handle and put it to the list */
116 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
118 struct vfs_openfile
*h
;
120 h
= g_new (struct vfs_openfile
, 1);
124 /* Allocate the first free handle */
125 h
->handle
= vfs_free_handle_list
;
128 /* No free allocated handles, allocate one */
129 h
->handle
= vfs_openfiles
->len
;
130 g_ptr_array_add (vfs_openfiles
, h
);
134 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
135 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
138 h
->handle
+= VFS_FIRST_HANDLE
;
142 /** Find VFS class by file handle */
143 static struct vfs_class
*
146 struct vfs_openfile
*h
;
148 if (handle
< VFS_FIRST_HANDLE
|| (guint
) (handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
151 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
155 g_assert (h
->handle
== handle
);
160 /** Find private file data by file handle */
162 vfs_info (int handle
)
164 struct vfs_openfile
*h
;
166 if (handle
< VFS_FIRST_HANDLE
|| (guint
) (handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
169 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
173 g_assert (h
->handle
== handle
);
178 /** Free open file data for given file handle */
180 vfs_free_handle (int handle
)
182 const int idx
= handle
- VFS_FIRST_HANDLE
;
184 if (handle
>= VFS_FIRST_HANDLE
&& (guint
) idx
< vfs_openfiles
->len
)
186 struct vfs_openfile
*h
;
188 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, idx
);
190 g_ptr_array_index (vfs_openfiles
, idx
) = (void *) vfs_free_handle_list
;
191 vfs_free_handle_list
= idx
;
195 static struct vfs_class
*vfs_list
;
198 vfs_register_class (struct vfs_class
*vfs
)
200 if (vfs
->init
!= NULL
) /* vfs has own initialization function */
201 if (!(*vfs
->init
) (vfs
)) /* but it failed */
204 vfs
->next
= vfs_list
;
210 /** Return VFS class for the given prefix */
211 static struct vfs_class
*
212 vfs_prefix_to_class (char *prefix
)
214 struct vfs_class
*vfs
;
216 /* Avoid last class (localfs) that would accept any prefix */
217 for (vfs
= vfs_list
; vfs
->next
!= NULL
; vfs
= vfs
->next
)
219 if (vfs
->which
!= NULL
)
221 if ((*vfs
->which
) (vfs
, prefix
) == -1)
225 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
231 /** Strip known vfs suffixes from a filename (possible improvement: strip
232 * suffix from last path component).
233 * \return a malloced string which has to be freed.
236 vfs_strip_suffix_from_filename (const char *filename
)
238 struct vfs_class
*vfs
;
242 if (filename
== NULL
)
243 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
245 p
= g_strdup (filename
);
246 semi
= strrchr (p
, '#');
250 /* Avoid last class (localfs) that would accept any prefix */
251 for (vfs
= vfs_list
; vfs
->next
!= NULL
; vfs
= vfs
->next
)
253 if (vfs
->which
!= NULL
)
255 if ((*vfs
->which
) (vfs
, semi
+ 1) == -1)
257 *semi
= '\0'; /* Found valid suffix */
260 if (vfs
->prefix
!= NULL
&& strncmp (semi
+ 1, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
262 *semi
= '\0'; /* Found valid suffix */
270 path_magic (const char *path
)
274 return (stat (path
, &buf
) != 0);
278 * Splits path extracting vfs part.
281 * \verbatim /p1#op/inpath \endverbatim
283 * \verbatim inpath,op; \endverbatim
284 * returns which vfs it is.
285 * What is left in path is p1. You still want to g_free(path), you DON'T
286 * want to free neither *inpath nor *op
289 vfs_split (char *path
, char **inpath
, char **op
)
293 struct vfs_class
*ret
;
296 vfs_die ("Cannot split NULL");
298 semi
= strrchr (path
, '#');
299 if (semi
== NULL
|| !path_magic (path
))
302 slash
= strchr (semi
, PATH_SEP
);
314 ret
= vfs_prefix_to_class (semi
+ 1);
320 *inpath
= slash
!= NULL
? slash
+ 1 : NULL
;
327 ret
= vfs_split (path
, inpath
, op
);
332 static struct vfs_class
*
333 _vfs_get_class (char *path
)
337 struct vfs_class
*ret
;
339 g_return_val_if_fail (path
, NULL
);
341 semi
= strrchr (path
, '#');
342 if (semi
== NULL
|| !path_magic (path
))
345 slash
= strchr (semi
, PATH_SEP
);
350 ret
= vfs_prefix_to_class (semi
+ 1);
355 ret
= _vfs_get_class (path
);
362 vfs_get_class (const char *pathname
)
364 struct vfs_class
*vfs
;
365 char *path
= g_strdup (pathname
);
367 vfs
= _vfs_get_class (path
);
377 vfs_get_encoding (const char *path
)
379 static char result
[16];
384 work
= g_strdup (path
);
385 semi
= g_strrstr (work
, "#enc:");
389 semi
+= 5 * sizeof (char);
390 slash
= strchr (semi
, PATH_SEP
);
394 g_strlcpy (result
, semi
, sizeof (result
));
405 /* return if encoding can by used in vfs (is ascci full compactible) */
406 /* contains only a few encoding now */
408 vfs_supported_enconding (const char *encoding
)
413 for (t
= 0; supported_encodings
[t
] != NULL
; t
++)
415 result
+= (g_ascii_strncasecmp (encoding
, supported_encodings
[t
],
416 strlen (supported_encodings
[t
])) == 0);
422 /* now used only by vfs_translate_path, but could be used in other vfs
423 * plugin to automatic detect encoding
424 * path - path to translate
425 * size - how many bytes from path translate
426 * defcnv - convertor, that is used as default, when path does not contain any
428 * buffer - used to store result of translation
431 _vfs_translate_path (const char *path
, int size
, GIConv defcnv
, GString
* buffer
)
436 estr_t state
= ESTR_SUCCESS
;
437 static char encoding
[16];
443 size
= (size
> 0) ? size
: (signed int) strlen (path
);
445 /* try found #end: */
446 semi
= g_strrstr_len (path
, size
, "#enc:");
449 /* first must be translated part before #enc: */
452 /* remove '/' before #enc */
453 ps
= str_cget_prev_char (semi
);
454 if (ps
[0] == PATH_SEP
)
457 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
459 if (state
!= ESTR_SUCCESS
)
461 /* now can be translated part after #enc: */
464 slash
= strchr (semi
, PATH_SEP
);
465 /* ignore slashes after size; */
466 if (slash
- path
>= size
)
469 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
470 ms
= min ((unsigned int) ms
, sizeof (encoding
) - 1);
471 /* limit encoding size (ms) to path size (size) */
472 if (semi
+ ms
> path
+ size
)
473 ms
= path
+ size
- semi
;
474 memcpy (encoding
, semi
, ms
);
477 switch (vfs_supported_enconding (encoding
))
480 coder
= str_crt_conv_to (encoding
);
481 if (coder
!= INVALID_CONV
)
485 state
= str_vfs_convert_to (coder
, slash
, path
+ size
- slash
, buffer
);
487 else if (buffer
->str
[0] == '\0')
489 /* exmaple "/#enc:utf-8" */
490 g_string_append_c (buffer
, PATH_SEP
);
492 str_close_conv (coder
);
507 /* path can be translated whole at once */
508 state
= str_vfs_convert_to (defcnv
, path
, size
, buffer
);
516 vfs_translate_path (const char *path
)
520 g_string_set_size (vfs_str_buffer
, 0);
521 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
524 return (state == 0) ? vfs_str_buffer->data : NULL;
526 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
530 vfs_translate_path_n (const char *path
)
534 result
= vfs_translate_path (path
);
535 return (result
!= NULL
) ? g_strdup (result
) : NULL
;
539 vfs_canon_and_translate (const char *path
)
544 canon
= g_strdup ("");
546 canon
= vfs_canon (path
);
547 result
= vfs_translate_path_n (canon
);
553 ferrno (struct vfs_class
*vfs
)
555 return vfs
->ferrno
? (*vfs
->ferrno
) (vfs
) : E_UNKNOWN
;
556 /* Hope that error message is obscure enough ;-) */
560 mc_open (const char *filename
, int flags
, ...)
566 struct vfs_class
*vfs
;
568 file
= vfs_canon_and_translate (filename
);
572 vfs
= vfs_get_class (file
);
574 /* Get the mode flag */
577 va_start (ap
, flags
);
578 mode
= va_arg (ap
, int);
582 if (vfs
->open
== NULL
)
589 info
= vfs
->open (vfs
, file
, flags
, mode
); /* open must be supported */
593 errno
= ferrno (vfs
);
597 return vfs_new_handle (vfs
, info
);
601 #define MC_NAMEOP(name, inarg, callarg) \
602 int mc_##name inarg \
604 struct vfs_class *vfs; \
606 char *mpath = vfs_canon_and_translate (path); \
609 vfs = vfs_get_class (mpath); \
614 result = vfs->name != NULL ? vfs->name callarg : -1; \
617 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
621 MC_NAMEOP (chmod
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
622 MC_NAMEOP (chown
, (const char *path
, uid_t owner
, gid_t group
), (vfs
, mpath
, owner
, group
))
623 MC_NAMEOP (utime
, (const char *path
, struct utimbuf
* times
), (vfs
, mpath
, times
))
624 MC_NAMEOP (readlink
, (const char *path
, char *buf
, int bufsiz
), (vfs
, mpath
, buf
, bufsiz
))
625 MC_NAMEOP (unlink
, (const char *path
), (vfs
, mpath
))
626 MC_NAMEOP (mkdir
, (const char *path
, mode_t mode
), (vfs
, mpath
, mode
))
627 MC_NAMEOP (rmdir
, (const char *path
), (vfs
, mpath
))
628 MC_NAMEOP (mknod
, (const char *path
, mode_t mode
, dev_t dev
), (vfs
, mpath
, mode
, dev
))
631 mc_symlink (const char *name1
, const char *path
)
633 struct vfs_class
*vfs
;
639 mpath
= vfs_canon_and_translate (path
);
643 tmp
= g_strdup (name1
);
644 lpath
= vfs_translate_path_n (tmp
);
649 vfs
= vfs_get_class (mpath
);
650 result
= vfs
->symlink
!= NULL
? vfs
->symlink (vfs
, lpath
, mpath
) : -1;
655 errno
= vfs
->symlink
!= NULL
? ferrno (vfs
) : E_NOTSUPP
;
664 #define MC_HANDLEOP(name, inarg, callarg) \
665 ssize_t mc_##name inarg \
667 struct vfs_class *vfs; \
671 vfs = vfs_op (handle); \
674 result = vfs->name != NULL ? vfs->name callarg : -1; \
676 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
680 MC_HANDLEOP (read
, (int handle
, void *buffer
, int count
), (vfs_info (handle
), buffer
, count
))
681 MC_HANDLEOP (write
, (int handle
, const void *buf
, int nbyte
), (vfs_info (handle
), buf
, nbyte
))
683 #define MC_RENAMEOP(name) \
684 int mc_##name (const char *fname1, const char *fname2) \
686 struct vfs_class *vfs; \
688 char *name2, *name1; \
689 name1 = vfs_canon_and_translate (fname1); \
692 name2 = vfs_canon_and_translate (fname2); \
693 if (name2 == NULL) { \
697 vfs = vfs_get_class (name1); \
698 if (vfs != vfs_get_class (name2)) \
705 result = vfs->name != NULL ? vfs->name (vfs, name1, name2) : -1; \
709 errno = vfs->name != NULL ? ferrno (vfs) : E_NOTSUPP; \
713 MC_RENAMEOP (link
) MC_RENAMEOP (rename
)
716 mc_ctl (int handle
, int ctlop
, void *arg
)
718 struct vfs_class
*vfs
= vfs_op (handle
);
723 return vfs
->ctl
? (*vfs
->ctl
) (vfs_info (handle
), ctlop
, arg
) : 0;
727 mc_setctl (const char *path
, int ctlop
, void *arg
)
729 struct vfs_class
*vfs
;
734 vfs_die ("You don't want to pass NULL to mc_setctl.");
736 mpath
= vfs_canon_and_translate (path
);
739 vfs
= vfs_get_class (mpath
);
740 result
= vfs
->setctl
!= NULL
? vfs
->setctl (vfs
, mpath
, ctlop
, arg
) : 0;
748 mc_close (int handle
)
750 struct vfs_class
*vfs
;
753 if (handle
== -1 || !vfs_info (handle
))
756 vfs
= vfs_op (handle
);
761 return close (handle
);
764 vfs_die ("VFS must support close.\n");
765 result
= (*vfs
->close
) (vfs_info (handle
));
766 vfs_free_handle (handle
);
768 errno
= ferrno (vfs
);
774 mc_opendir (const char *dirname
)
776 int handle
, *handlep
;
778 struct vfs_class
*vfs
;
781 struct vfs_dirinfo
*dirinfo
;
782 const char *encoding
;
784 canon
= vfs_canon (dirname
);
785 dname
= vfs_translate_path_n (canon
);
789 vfs
= vfs_get_class (dname
);
790 info
= vfs
->opendir
? (*vfs
->opendir
) (vfs
, dname
) : NULL
;
795 errno
= vfs
->opendir
? ferrno (vfs
) : E_NOTSUPP
;
800 dirinfo
= g_new (struct vfs_dirinfo
, 1);
801 dirinfo
->info
= info
;
803 encoding
= vfs_get_encoding (canon
);
805 dirinfo
->converter
= (encoding
!= NULL
) ? str_crt_conv_from (encoding
) : str_cnv_from_term
;
806 if (dirinfo
->converter
== INVALID_CONV
)
807 dirinfo
->converter
= str_cnv_from_term
;
809 handle
= vfs_new_handle (vfs
, dirinfo
);
811 handlep
= g_new (int, 1);
813 return (DIR *) handlep
;
822 static struct dirent
*mc_readdir_result
= NULL
;
825 mc_readdir (DIR * dirp
)
828 struct vfs_class
*vfs
;
829 struct dirent
*entry
= NULL
;
830 struct vfs_dirinfo
*dirinfo
;
833 if (!mc_readdir_result
)
835 /* We can't just allocate struct dirent as (see man dirent.h)
836 * struct dirent has VERY nonnaive semantics of allocating
837 * d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
838 * than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
839 * heap corrupter. So, allocate longliving dirent with at least
840 * (MAXNAMLEN + 1) for d_name in it.
841 * Strictly saying resulting dirent is unusable as we don't adjust internal
842 * structures, holding dirent size. But we don't use it in libc infrastructure.
843 * TODO: to make simpler homemade dirent-alike structure.
845 mc_readdir_result
= (struct dirent
*) g_malloc (sizeof (struct dirent
) + MAXNAMLEN
+ 1);
853 handle
= *(int *) dirp
;
855 vfs
= vfs_op (handle
);
859 dirinfo
= vfs_info (handle
);
862 entry
= (*vfs
->readdir
) (dirinfo
->info
);
865 g_string_set_size (vfs_str_buffer
, 0);
866 state
= str_vfs_convert_from (dirinfo
->converter
, entry
->d_name
, vfs_str_buffer
);
867 mc_readdir_result
->d_ino
= entry
->d_ino
;
868 g_strlcpy (mc_readdir_result
->d_name
, vfs_str_buffer
->str
, MAXNAMLEN
+ 1);
871 errno
= vfs
->readdir
? ferrno (vfs
) : E_NOTSUPP
;
872 return (entry
!= NULL
) ? mc_readdir_result
: NULL
;
876 mc_closedir (DIR * dirp
)
878 int handle
= *(int *) dirp
;
879 struct vfs_class
*vfs
;
882 vfs
= vfs_op (handle
);
885 struct vfs_dirinfo
*dirinfo
;
887 dirinfo
= vfs_info (handle
);
888 if (dirinfo
->converter
!= str_cnv_from_term
)
889 str_close_conv (dirinfo
->converter
);
891 result
= vfs
->closedir
? (*vfs
->closedir
) (dirinfo
->info
) : -1;
892 vfs_free_handle (handle
);
900 mc_stat (const char *filename
, struct stat
*buf
)
902 struct vfs_class
*vfs
;
906 path
= vfs_canon_and_translate (filename
);
911 vfs
= vfs_get_class (path
);
919 result
= vfs
->stat
? (*vfs
->stat
) (vfs
, path
, buf
) : -1;
924 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
929 mc_lstat (const char *filename
, struct stat
*buf
)
931 struct vfs_class
*vfs
;
935 path
= vfs_canon_and_translate (filename
);
940 vfs
= vfs_get_class (path
);
947 result
= vfs
->lstat
? (*vfs
->lstat
) (vfs
, path
, buf
) : -1;
950 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
955 mc_fstat (int handle
, struct stat
*buf
)
957 struct vfs_class
*vfs
;
963 vfs
= vfs_op (handle
);
967 result
= vfs
->fstat
? (*vfs
->fstat
) (vfs_info (handle
), buf
) : -1;
969 errno
= vfs
->name
? ferrno (vfs
) : E_NOTSUPP
;
974 * Return current directory. If it's local, reread the current directory
975 * from the OS. You must g_strdup() whatever this function returns.
982 trans
= vfs_translate_path_n (current_dir
);
984 if (_vfs_get_class (trans
) == NULL
)
986 const char *encoding
= vfs_get_encoding (current_dir
);
988 if (encoding
== NULL
)
992 tmp
= g_get_current_dir ();
994 { /* One of the directories in the path is not readable */
998 g_string_set_size (vfs_str_buffer
, 0);
999 state
= str_vfs_convert_from (str_cnv_from_term
, tmp
, vfs_str_buffer
);
1002 sys_cwd
= (state
== ESTR_SUCCESS
) ? g_strdup (vfs_str_buffer
->str
) : NULL
;
1003 if (sys_cwd
!= NULL
)
1005 struct stat my_stat
, my_stat2
;
1006 /* Check if it is O.K. to use the current_dir */
1008 && mc_stat (sys_cwd
, &my_stat
) == 0
1009 && mc_stat (current_dir
, &my_stat2
) == 0
1010 && my_stat
.st_ino
== my_stat2
.st_ino
&& my_stat
.st_dev
== my_stat2
.st_dev
)
1014 g_free (current_dir
);
1015 current_dir
= sys_cwd
;
1029 current_dir
= g_strdup (PATH_SEP_STR
);
1032 if (strlen (current_dir
) > MC_MAXPATHLEN
- 2)
1033 vfs_die ("Current dir too long.\n");
1035 current_vfs
= vfs_get_class (current_dir
);
1039 * Return current directory. If it's local, reread the current directory
1040 * from the OS. Put directory to the provided buffer.
1043 mc_get_current_wd (char *buffer
, int size
)
1045 const char *cwd
= _vfs_get_cwd ();
1047 g_strlcpy (buffer
, cwd
, size
);
1052 * Return current directory without any OS calls.
1055 vfs_get_current_dir (void)
1061 mc_lseek (int fd
, off_t offset
, int whence
)
1063 struct vfs_class
*vfs
;
1073 result
= vfs
->lseek
? (*vfs
->lseek
) (vfs_info (fd
), offset
, whence
) : -1;
1075 errno
= vfs
->lseek
? ferrno (vfs
) : E_NOTSUPP
;
1080 * remove //, /./ and /../
1083 #define ISSLASH(a) (!a || (a == '/'))
1086 vfs_canon (const char *path
)
1089 vfs_die ("Cannot canonicalize NULL");
1091 /* Relative to current directory */
1092 if (*path
!= PATH_SEP
)
1094 char *local
, *result
;
1096 local
= concat_dir_and_file (current_dir
, path
);
1098 result
= vfs_canon (local
);
1104 * So we have path of following form:
1105 * /p1/p2#op/.././././p3#op/p4. Good luck.
1108 char *result
= g_strdup (path
);
1109 canonicalize_pathname (result
);
1116 * Return 0 on success, -1 on failure.
1119 mc_chdir (const char *path
)
1123 struct vfs_class
*old_vfs
, *new_vfs
;
1127 new_dir
= vfs_canon (path
);
1128 trans_dir
= vfs_translate_path_n (new_dir
);
1129 if (trans_dir
!= NULL
)
1131 new_vfs
= vfs_get_class (trans_dir
);
1132 if (!new_vfs
->chdir
)
1139 result
= (*new_vfs
->chdir
) (new_vfs
, trans_dir
);
1143 errno
= ferrno (new_vfs
);
1149 old_vfsid
= vfs_getid (current_vfs
, current_dir
);
1150 old_vfs
= current_vfs
;
1152 /* Actually change directory */
1153 g_free (current_dir
);
1154 current_dir
= new_dir
;
1155 current_vfs
= new_vfs
;
1157 /* This function uses the new current_dir implicitly */
1158 vfs_stamp_create (old_vfs
, old_vfsid
);
1160 /* Sometimes we assume no trailing slash on cwd */
1164 p
= strchr (current_dir
, 0) - 1;
1165 if (*p
== PATH_SEP
&& p
> current_dir
)
1179 /* Return 1 is the current VFS class is local */
1181 vfs_current_is_local (void)
1183 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
1186 /* Return flags of the VFS class of the given filename */
1188 vfs_file_class_flags (const char *filename
)
1190 struct vfs_class
*vfs
;
1193 fname
= vfs_canon_and_translate (filename
);
1197 vfs
= vfs_get_class (fname
);
1203 mc_def_getlocalcopy (const char *filename
)
1211 fdin
= mc_open (filename
, O_RDONLY
| O_LINEAR
);
1215 fdout
= vfs_mkstemps (&tmp
, "vfs", filename
);
1219 while ((i
= mc_read (fdin
, buffer
, sizeof (buffer
))) > 0)
1221 if (write (fdout
, buffer
, i
) != i
)
1226 i
= mc_close (fdin
);
1230 if (close (fdout
) == -1)
1236 if (mc_stat (filename
, &mystat
) != -1)
1237 chmod (tmp
, mystat
.st_mode
);
1251 mc_getlocalcopy (const char *pathname
)
1253 char *result
= NULL
;
1256 path
= vfs_canon_and_translate (pathname
);
1259 struct vfs_class
*vfs
= vfs_get_class (path
);
1261 result
= vfs
->getlocalcopy
!= NULL
?
1262 vfs
->getlocalcopy (vfs
, path
) :
1263 mc_def_getlocalcopy (path
);
1266 errno
= ferrno (vfs
);
1273 mc_def_ungetlocalcopy (struct vfs_class
*vfs
, const char *filename
,
1274 const char *local
, int has_changed
)
1276 int fdin
= -1, fdout
= -1, i
;
1284 fdin
= open (local
, O_RDONLY
);
1287 fdout
= mc_open (filename
, O_WRONLY
| O_TRUNC
);
1290 while ((i
= read (fdin
, buffer
, sizeof (buffer
))) > 0)
1292 if (mc_write (fdout
, buffer
, i
) != i
)
1298 if (close (fdin
) == -1)
1304 if (mc_close (fdout
) == -1)
1314 message (D_ERROR
, _("Changes to file lost"), "%s", filename
);
1324 mc_ungetlocalcopy (const char *pathname
, const char *local
, int has_changed
)
1326 int return_value
= -1;
1329 path
= vfs_canon_and_translate (pathname
);
1332 struct vfs_class
*vfs
= vfs_get_class (path
);
1334 return_value
= vfs
->ungetlocalcopy
!= NULL
?
1335 vfs
->ungetlocalcopy (vfs
, path
, local
, has_changed
) :
1336 mc_def_ungetlocalcopy (vfs
, path
, local
, has_changed
);
1340 return return_value
;
1347 /* create the VFS handle array */
1348 vfs_openfiles
= g_ptr_array_new ();
1350 vfs_str_buffer
= g_string_new ("");
1351 /* localfs needs to be the first one */
1353 /* fallback value for vfs_get_class() */
1354 localfs_class
= vfs_list
;
1361 #ifdef USE_EXT2FSLIB
1363 #endif /* USE_EXT2FSLIB */
1368 #ifdef ENABLE_VFS_SMB
1370 #endif /* ENABLE_VFS_SMB */
1371 #ifdef ENABLE_VFS_MCFS
1373 #endif /* ENABLE_VFS_MCFS */
1374 #endif /* USE_NETCODE */
1382 struct vfs_class
*vfs
;
1386 g_free (current_dir
);
1388 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
1392 g_ptr_array_free (vfs_openfiles
, TRUE
);
1393 g_string_free (vfs_str_buffer
, TRUE
);
1394 g_free (mc_readdir_result
);
1398 * These ones grab information from the VFS
1399 * and handles them to an upper layer
1402 vfs_fill_names (fill_names_f func
)
1404 struct vfs_class
*vfs
;
1406 for (vfs
= vfs_list
; vfs
; vfs
= vfs
->next
)
1407 if (vfs
->fill_names
)
1408 (*vfs
->fill_names
) (vfs
, func
);
1412 * Returns vfs path corresponding to given url. If passed string is
1413 * not recognized as url, g_strdup(url) is returned.
1420 const char *substitute
;
1424 { "ftp://", 6, "/#ftp:" },
1425 { "mc://", 5, "/#mc:" },
1426 { "smb://", 6, "/#smb:" },
1427 { "sh://", 5, "/#sh:" },
1428 { "ssh://", 6, "/#sh:" },
1434 vfs_translate_url (const char *url
)
1438 for (i
= 0; i
< sizeof (url_table
) / sizeof (url_table
[0]); i
++)
1439 if (strncmp (url
, url_table
[i
].name
, url_table
[i
].name_len
) == 0)
1440 return g_strconcat (url_table
[i
].substitute
, url
+ url_table
[i
].name_len
,
1443 return g_strdup (url
);
1447 vfs_file_is_local (const char *filename
)
1449 return vfs_file_class_flags (filename
) & VFSF_LOCAL
;