2 Virtual File System switch code
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
8 Written by: 1995 Miguel de Icaza
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 * \brief Source: Virtual File System switch code
31 * \author Miguel de Icaza
32 * \author Jakub Jelinek
33 * \author Pavel Machek
35 * \warning funtions like extfs_lstat() have right to destroy any
36 * strings you pass to them. This is acutally ok as you g_strdup what
37 * you are passing to them, anyway; still, beware.
39 * Namespace: exports *many* functions with vfs_ prefix; exports
40 * parse_ls_lga and friends which do not have that prefix.
47 #include "lib/global.h"
48 #include "lib/strutil.h"
50 #include "lib/widget.h" /* message() */
51 #include "lib/event.h"
54 #include "lib/charsets.h"
61 extern struct dirent
*mc_readdir_result
;
62 /*** global variables ****************************************************************************/
64 GPtrArray
*vfs__classes_list
= NULL
;
66 GString
*vfs_str_buffer
= NULL
;
67 struct vfs_class
*current_vfs
= NULL
;
70 /*** file scope macro definitions ****************************************************************/
72 #if defined(_AIX) && !defined(NAME_MAX)
73 #define NAME_MAX FILENAME_MAX
76 #define VFS_FIRST_HANDLE 100
78 #define ISSLASH(a) (!a || (a == '/'))
80 /*** file scope type declarations ****************************************************************/
85 struct vfs_class
*vclass
;
89 /*** file scope variables ************************************************************************/
91 /** They keep track of the current directory */
92 static vfs_path_t
*current_path
= NULL
;
94 static GPtrArray
*vfs_openfiles
;
95 static long vfs_free_handle_list
= -1;
97 /*** file scope functions ************************************************************************/
98 /* --------------------------------------------------------------------------------------------- */
99 /* now used only by vfs_translate_path, but could be used in other vfs
100 * plugin to automatic detect encoding
101 * path - path to translate
102 * size - how many bytes from path translate
103 * defcnv - convertor, that is used as default, when path does not contain any
105 * buffer - used to store result of translation
109 _vfs_translate_path (const char *path
, int size
, GIConv defcnv
, GString
* buffer
)
113 estr_t state
= ESTR_SUCCESS
;
118 size
= (size
> 0) ? size
: (signed int) strlen (path
);
120 /* try found /#enc: */
121 semi
= g_strrstr_len (path
, size
, VFS_ENCODING_PREFIX
);
122 if (semi
!= NULL
&& (semi
== path
|| *(semi
- 1) == PATH_SEP
))
125 GIConv coder
= INVALID_CONV
;
128 /* first must be translated part before #enc: */
131 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
133 if (state
!= ESTR_SUCCESS
)
136 /* now can be translated part after #enc: */
137 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
138 slash
= strchr (semi
, PATH_SEP
);
139 /* ignore slashes after size; */
140 if (slash
- path
>= size
)
143 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
144 ms
= min ((unsigned int) ms
, sizeof (encoding
) - 1);
145 /* limit encoding size (ms) to path size (size) */
146 if (semi
+ ms
> path
+ size
)
147 ms
= path
+ size
- semi
;
148 memcpy (encoding
, semi
, ms
);
152 if (is_supported_encoding (encoding
))
153 coder
= str_crt_conv_to (encoding
);
156 if (coder
!= INVALID_CONV
)
159 state
= str_vfs_convert_to (coder
, slash
+ 1, path
+ size
- slash
- 1, buffer
);
160 str_close_conv (coder
);
165 state
= ESTR_FAILURE
;
169 /* path can be translated whole at once */
170 state
= str_vfs_convert_to (defcnv
, path
, size
, buffer
);
176 /* --------------------------------------------------------------------------------------------- */
178 static struct vfs_openfile
*
179 vfs_get_openfile (int handle
)
181 struct vfs_openfile
*h
;
183 if (handle
< VFS_FIRST_HANDLE
|| (guint
) (handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
186 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
190 g_assert (h
->handle
== handle
);
195 /* --------------------------------------------------------------------------------------------- */
196 /*** public functions ****************************************************************************/
197 /* --------------------------------------------------------------------------------------------- */
198 /** Free open file data for given file handle */
201 vfs_free_handle (int handle
)
203 const int idx
= handle
- VFS_FIRST_HANDLE
;
205 if (handle
>= VFS_FIRST_HANDLE
&& (guint
) idx
< vfs_openfiles
->len
)
207 struct vfs_openfile
*h
;
209 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, idx
);
211 g_ptr_array_index (vfs_openfiles
, idx
) = (void *) vfs_free_handle_list
;
212 vfs_free_handle_list
= idx
;
217 /* --------------------------------------------------------------------------------------------- */
218 /** Find private file data by file handle */
221 vfs_class_data_find_by_handle (int handle
)
223 struct vfs_openfile
*h
;
225 h
= vfs_get_openfile (handle
);
227 return h
== NULL
? NULL
: h
->fsinfo
;
230 /* --------------------------------------------------------------------------------------------- */
231 /** Find VFS class by file handle */
234 vfs_class_find_by_handle (int handle
)
236 struct vfs_openfile
*h
;
238 h
= vfs_get_openfile (handle
);
240 return h
== NULL
? NULL
: h
->vclass
;
243 /* --------------------------------------------------------------------------------------------- */
246 * Create new VFS handle and put it to the list
250 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
252 struct vfs_openfile
*h
;
254 h
= g_new (struct vfs_openfile
, 1);
258 /* Allocate the first free handle */
259 h
->handle
= vfs_free_handle_list
;
262 /* No free allocated handles, allocate one */
263 h
->handle
= vfs_openfiles
->len
;
264 g_ptr_array_add (vfs_openfiles
, h
);
268 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
269 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
272 h
->handle
+= VFS_FIRST_HANDLE
;
276 /* --------------------------------------------------------------------------------------------- */
279 vfs_ferrno (struct vfs_class
*vfs
)
281 return vfs
->ferrno
? (*vfs
->ferrno
) (vfs
) : E_UNKNOWN
;
282 /* Hope that error message is obscure enough ;-) */
285 /* --------------------------------------------------------------------------------------------- */
288 vfs_register_class (struct vfs_class
* vfs
)
290 if (vfs
->init
!= NULL
) /* vfs has own initialization function */
291 if (!vfs
->init (vfs
)) /* but it failed */
294 g_ptr_array_add (vfs__classes_list
, vfs
);
299 /* --------------------------------------------------------------------------------------------- */
300 /** Strip known vfs suffixes from a filename (possible improvement: strip
301 * suffix from last path component).
302 * \return a malloced string which has to be freed.
306 vfs_strip_suffix_from_filename (const char *filename
)
308 char *semi
, *p
, *vfs_prefix
;
310 if (filename
== NULL
)
311 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
313 p
= g_strdup (filename
);
314 semi
= g_strrstr (p
, VFS_PATH_URL_DELIMITER
);
319 vfs_prefix
= strrchr (p
, PATH_SEP
);
320 if (vfs_prefix
== NULL
)
322 *semi
= *VFS_PATH_URL_DELIMITER
;
330 /* --------------------------------------------------------------------------------------------- */
333 vfs_translate_path (const char *path
)
337 g_string_set_size (vfs_str_buffer
, 0);
338 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
341 return (state == 0) ? vfs_str_buffer->data : NULL;
343 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
346 /* --------------------------------------------------------------------------------------------- */
349 vfs_translate_path_n (const char *path
)
353 result
= vfs_translate_path (path
);
354 return (result
!= NULL
) ? g_strdup (result
) : NULL
;
357 /* --------------------------------------------------------------------------------------------- */
359 * Get current directory without any OS calls.
361 * @return string contain current path
365 vfs_get_current_dir (void)
367 return vfs_path_to_str (current_path
);
370 /* --------------------------------------------------------------------------------------------- */
372 * Get raw current directory object without any OS calls.
374 * @return object contain current path
378 vfs_get_raw_current_dir (void)
383 /* --------------------------------------------------------------------------------------------- */
385 * Set current directory object.
387 * @param vpath new path
390 vfs_set_raw_current_dir (const vfs_path_t
* vpath
)
392 vfs_path_free (current_path
);
393 current_path
= (vfs_path_t
*) vpath
;
396 /* --------------------------------------------------------------------------------------------- */
397 /* Return TRUE is the current VFS class is local */
400 vfs_current_is_local (void)
402 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
405 /* --------------------------------------------------------------------------------------------- */
406 /* Return flags of the VFS class of the given filename */
409 vfs_file_class_flags (const vfs_path_t
* vpath
)
411 const vfs_path_element_t
*path_element
;
413 path_element
= vfs_path_get_by_index (vpath
, -1);
414 if (!vfs_path_element_valid (path_element
))
417 return path_element
->class->flags
;
420 /* --------------------------------------------------------------------------------------------- */
425 /* create the VFS handle arrays */
426 vfs__classes_list
= g_ptr_array_new ();
428 /* create the VFS handle array */
429 vfs_openfiles
= g_ptr_array_new ();
431 vfs_str_buffer
= g_string_new ("");
435 /* --------------------------------------------------------------------------------------------- */
438 vfs_setup_work_dir (void)
440 const vfs_path_element_t
*path_element
;
444 /* FIXME: is we really need for this check? */
446 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
447 vfs_die ("Current dir too long.\n");
450 path_element
= vfs_path_get_by_index (current_path
, -1);
451 current_vfs
= path_element
->class;
454 /* --------------------------------------------------------------------------------------------- */
463 vfs_set_raw_current_dir (NULL
);
465 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
467 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
469 if (vfs
->done
!= NULL
)
473 g_ptr_array_free (vfs_openfiles
, TRUE
);
474 g_ptr_array_free (vfs__classes_list
, TRUE
);
475 g_string_free (vfs_str_buffer
, TRUE
);
476 g_free (mc_readdir_result
);
479 /* --------------------------------------------------------------------------------------------- */
481 * These ones grab information from the VFS
482 * and handles them to an upper layer
486 vfs_fill_names (fill_names_f func
)
490 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
492 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
494 if (vfs
->fill_names
!= NULL
)
495 vfs
->fill_names (vfs
, func
);
499 /* --------------------------------------------------------------------------------------------- */
501 vfs_file_is_local (const vfs_path_t
* vpath
)
503 return (vfs_file_class_flags (vpath
) & VFSF_LOCAL
) != 0;
506 /* --------------------------------------------------------------------------------------------- */
509 vfs_print_message (const char *msg
, ...)
511 ev_vfs_print_message_t event_data
;
513 va_start (event_data
.ap
, msg
);
514 event_data
.msg
= msg
;
516 mc_event_raise (MCEVENT_GROUP_CORE
, "vfs_print_message", (gpointer
) & event_data
);
517 va_end (event_data
.ap
);
520 /* --------------------------------------------------------------------------------------------- */
522 * If it's local, reread the current directory
529 const vfs_path_element_t
*path_element
;
531 if (vfs_get_raw_current_dir () == NULL
)
535 tmp
= g_get_current_dir ();
536 vfs_set_raw_current_dir (vfs_path_from_str (tmp
));
540 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
542 if ((path_element
->class->flags
& VFSF_LOCAL
) != 0)
545 vfs_path_t
*tmp_vpath
;
547 current_dir
= g_get_current_dir ();
548 tmp_vpath
= vfs_path_from_str (current_dir
);
549 g_free (current_dir
);
551 if (tmp_vpath
!= NULL
)
552 { /* One of the directories in the path is not readable */
553 struct stat my_stat
, my_stat2
;
555 /* Check if it is O.K. to use the current_dir */
556 if (!(mc_global
.vfs
.cd_symlinks
557 && mc_stat (tmp_vpath
, &my_stat
) == 0
558 && mc_stat (vfs_get_raw_current_dir (), &my_stat2
) == 0
559 && my_stat
.st_ino
== my_stat2
.st_ino
&& my_stat
.st_dev
== my_stat2
.st_dev
))
560 vfs_set_raw_current_dir (tmp_vpath
);
562 vfs_path_free (tmp_vpath
);
567 /* --------------------------------------------------------------------------------------------- */
569 * Return current directory. If it's local, reread the current directory
577 return vfs_path_to_str (vfs_get_raw_current_dir ());
580 /* --------------------------------------------------------------------------------------------- */
583 * Change encoding for last part (vfs_path_element_t) of vpath
585 * @param vpath pointer to path structure
586 * encoding name of charset
588 * @return pointer to path structure (for use function in anoter functions)
591 vfs_change_encoding (vfs_path_t
* vpath
, const char *encoding
)
593 vfs_path_element_t
*path_element
;
595 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, -1);
596 /* don't add current encoding */
597 if ((path_element
->encoding
!= NULL
) && (strcmp (encoding
, path_element
->encoding
) == 0))
600 g_free (path_element
->encoding
);
601 path_element
->encoding
= g_strdup (encoding
);
603 if (vfs_path_element_need_cleanup_converter (path_element
))
604 str_close_conv (path_element
->dir
.converter
);
606 path_element
->dir
.converter
= str_crt_conv_from (path_element
->encoding
);
611 /* --------------------------------------------------------------------------------------------- */
614 * Preallocate space for file in new place for ensure that file
615 * will be fully copied with less fragmentation.
617 * @param dest_desc mc VFS file handler
618 * @param src_fsize source file size
619 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
621 * @return 0 if success and non-zero otherwise.
622 * Note: function doesn't touch errno global variable.
625 vfs_preallocate (int dest_vfs_fd
, off_t src_fsize
, off_t dest_fsize
)
627 #ifndef HAVE_POSIX_FALLOCATE
633 #else /* HAVE_POSIX_FALLOCATE */
635 struct vfs_class
*dest_class
;
637 if (!mc_global
.vfs
.preallocate_space
)
640 dest_class
= vfs_class_find_by_handle (dest_vfs_fd
);
641 if ((dest_class
->flags
& VFSF_LOCAL
) == 0)
644 dest_fd
= (int *) vfs_class_data_find_by_handle (dest_vfs_fd
);
651 return posix_fallocate (*dest_fd
, dest_fsize
, src_fsize
- dest_fsize
);
653 #endif /* HAVE_POSIX_FALLOCATE */
656 /* --------------------------------------------------------------------------------------------- */