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 /* --------------------------------------------------------------------------------------------- */
98 /*** file scope functions ************************************************************************/
99 /* --------------------------------------------------------------------------------------------- */
100 /* now used only by vfs_translate_path, but could be used in other vfs
101 * plugin to automatic detect encoding
102 * path - path to translate
103 * size - how many bytes from path translate
104 * defcnv - convertor, that is used as default, when path does not contain any
106 * buffer - used to store result of translation
110 _vfs_translate_path (const char *path
, int size
, GIConv defcnv
, GString
* buffer
)
112 estr_t state
= ESTR_SUCCESS
;
120 size
= (size
> 0) ? size
: (signed int) strlen (path
);
122 /* try found /#enc: */
123 semi
= g_strrstr_len (path
, size
, VFS_ENCODING_PREFIX
);
124 if (semi
!= NULL
&& (semi
== path
|| *(semi
- 1) == PATH_SEP
))
127 GIConv coder
= INVALID_CONV
;
130 /* first must be translated part before #enc: */
133 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
135 if (state
!= ESTR_SUCCESS
)
138 /* now can be translated part after #enc: */
139 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
140 slash
= strchr (semi
, PATH_SEP
);
141 /* ignore slashes after size; */
142 if (slash
- path
>= size
)
145 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
146 ms
= min ((unsigned int) ms
, sizeof (encoding
) - 1);
147 /* limit encoding size (ms) to path size (size) */
148 if (semi
+ ms
> path
+ size
)
149 ms
= path
+ size
- semi
;
150 memcpy (encoding
, semi
, ms
);
153 if (is_supported_encoding (encoding
))
154 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 g_string_assign (buffer
, path
);
177 #endif /* HAVE_CHARSET */
182 /* --------------------------------------------------------------------------------------------- */
184 static struct vfs_openfile
*
185 vfs_get_openfile (int handle
)
187 struct vfs_openfile
*h
;
189 if (handle
< VFS_FIRST_HANDLE
|| (guint
) (handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
192 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
196 g_assert (h
->handle
== handle
);
201 /* --------------------------------------------------------------------------------------------- */
202 /*** public functions ****************************************************************************/
203 /* --------------------------------------------------------------------------------------------- */
204 /** Free open file data for given file handle */
207 vfs_free_handle (int handle
)
209 const int idx
= handle
- VFS_FIRST_HANDLE
;
211 if (handle
>= VFS_FIRST_HANDLE
&& (guint
) idx
< vfs_openfiles
->len
)
213 struct vfs_openfile
*h
;
215 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, idx
);
217 g_ptr_array_index (vfs_openfiles
, idx
) = (void *) vfs_free_handle_list
;
218 vfs_free_handle_list
= idx
;
223 /* --------------------------------------------------------------------------------------------- */
224 /** Find private file data by file handle */
227 vfs_class_data_find_by_handle (int handle
)
229 struct vfs_openfile
*h
;
231 h
= vfs_get_openfile (handle
);
233 return h
== NULL
? NULL
: h
->fsinfo
;
236 /* --------------------------------------------------------------------------------------------- */
237 /** Find VFS class by file handle */
240 vfs_class_find_by_handle (int handle
)
242 struct vfs_openfile
*h
;
244 h
= vfs_get_openfile (handle
);
246 return h
== NULL
? NULL
: h
->vclass
;
249 /* --------------------------------------------------------------------------------------------- */
252 * Create new VFS handle and put it to the list
256 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
258 struct vfs_openfile
*h
;
260 h
= g_new (struct vfs_openfile
, 1);
264 /* Allocate the first free handle */
265 h
->handle
= vfs_free_handle_list
;
268 /* No free allocated handles, allocate one */
269 h
->handle
= vfs_openfiles
->len
;
270 g_ptr_array_add (vfs_openfiles
, h
);
274 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
275 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
278 h
->handle
+= VFS_FIRST_HANDLE
;
282 /* --------------------------------------------------------------------------------------------- */
285 vfs_ferrno (struct vfs_class
*vfs
)
287 return vfs
->ferrno
? (*vfs
->ferrno
) (vfs
) : E_UNKNOWN
;
288 /* Hope that error message is obscure enough ;-) */
291 /* --------------------------------------------------------------------------------------------- */
294 vfs_register_class (struct vfs_class
* vfs
)
296 if (vfs
->init
!= NULL
) /* vfs has own initialization function */
297 if (!vfs
->init (vfs
)) /* but it failed */
300 g_ptr_array_add (vfs__classes_list
, vfs
);
305 /* --------------------------------------------------------------------------------------------- */
306 /** Strip known vfs suffixes from a filename (possible improvement: strip
307 * suffix from last path component).
308 * \return a malloced string which has to be freed.
312 vfs_strip_suffix_from_filename (const char *filename
)
314 char *semi
, *p
, *vfs_prefix
;
316 if (filename
== NULL
)
317 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
319 p
= g_strdup (filename
);
320 semi
= g_strrstr (p
, VFS_PATH_URL_DELIMITER
);
325 vfs_prefix
= strrchr (p
, PATH_SEP
);
326 if (vfs_prefix
== NULL
)
328 *semi
= *VFS_PATH_URL_DELIMITER
;
336 /* --------------------------------------------------------------------------------------------- */
339 vfs_translate_path (const char *path
)
343 g_string_set_size (vfs_str_buffer
, 0);
344 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
347 return (state == 0) ? vfs_str_buffer->data : NULL;
349 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
352 /* --------------------------------------------------------------------------------------------- */
355 vfs_translate_path_n (const char *path
)
359 result
= vfs_translate_path (path
);
360 return (result
!= NULL
) ? g_strdup (result
) : NULL
;
363 /* --------------------------------------------------------------------------------------------- */
365 * Get current directory without any OS calls.
367 * @return string contain current path
371 vfs_get_current_dir (void)
373 return vfs_path_to_str (current_path
);
376 /* --------------------------------------------------------------------------------------------- */
378 * Get raw current directory object without any OS calls.
380 * @return object contain current path
384 vfs_get_raw_current_dir (void)
389 /* --------------------------------------------------------------------------------------------- */
391 * Set current directory object.
393 * @param vpath new path
396 vfs_set_raw_current_dir (const vfs_path_t
* vpath
)
398 vfs_path_free (current_path
);
399 current_path
= (vfs_path_t
*) vpath
;
402 /* --------------------------------------------------------------------------------------------- */
403 /* Return TRUE is the current VFS class is local */
406 vfs_current_is_local (void)
408 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
411 /* --------------------------------------------------------------------------------------------- */
412 /* Return flags of the VFS class of the given filename */
415 vfs_file_class_flags (const vfs_path_t
* vpath
)
417 const vfs_path_element_t
*path_element
;
419 path_element
= vfs_path_get_by_index (vpath
, -1);
420 if (!vfs_path_element_valid (path_element
))
423 return path_element
->class->flags
;
426 /* --------------------------------------------------------------------------------------------- */
431 /* create the VFS handle arrays */
432 vfs__classes_list
= g_ptr_array_new ();
434 /* create the VFS handle array */
435 vfs_openfiles
= g_ptr_array_new ();
437 vfs_str_buffer
= g_string_new ("");
441 /* --------------------------------------------------------------------------------------------- */
444 vfs_setup_work_dir (void)
446 const vfs_path_element_t
*path_element
;
450 /* FIXME: is we really need for this check? */
452 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
453 vfs_die ("Current dir too long.\n");
456 path_element
= vfs_path_get_by_index (current_path
, -1);
457 current_vfs
= path_element
->class;
460 /* --------------------------------------------------------------------------------------------- */
469 vfs_set_raw_current_dir (NULL
);
471 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
473 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
475 if (vfs
->done
!= NULL
)
479 g_ptr_array_free (vfs_openfiles
, TRUE
);
480 g_ptr_array_free (vfs__classes_list
, TRUE
);
481 g_string_free (vfs_str_buffer
, TRUE
);
482 g_free (mc_readdir_result
);
485 /* --------------------------------------------------------------------------------------------- */
487 * These ones grab information from the VFS
488 * and handles them to an upper layer
492 vfs_fill_names (fill_names_f func
)
496 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
498 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
500 if (vfs
->fill_names
!= NULL
)
501 vfs
->fill_names (vfs
, func
);
505 /* --------------------------------------------------------------------------------------------- */
507 vfs_file_is_local (const vfs_path_t
* vpath
)
509 return (vfs_file_class_flags (vpath
) & VFSF_LOCAL
) != 0;
512 /* --------------------------------------------------------------------------------------------- */
515 vfs_print_message (const char *msg
, ...)
517 ev_vfs_print_message_t event_data
;
519 va_start (event_data
.ap
, msg
);
520 event_data
.msg
= msg
;
522 mc_event_raise (MCEVENT_GROUP_CORE
, "vfs_print_message", (gpointer
) & event_data
);
523 va_end (event_data
.ap
);
526 /* --------------------------------------------------------------------------------------------- */
528 * If it's local, reread the current directory
535 const vfs_path_element_t
*path_element
;
537 if (vfs_get_raw_current_dir () == NULL
)
541 tmp
= g_get_current_dir ();
542 vfs_set_raw_current_dir (vfs_path_from_str (tmp
));
546 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
548 if ((path_element
->class->flags
& VFSF_LOCAL
) != 0)
551 vfs_path_t
*tmp_vpath
;
553 current_dir
= g_get_current_dir ();
554 tmp_vpath
= vfs_path_from_str (current_dir
);
555 g_free (current_dir
);
557 if (tmp_vpath
!= NULL
)
558 { /* One of the directories in the path is not readable */
559 struct stat my_stat
, my_stat2
;
561 /* Check if it is O.K. to use the current_dir */
562 if (!(mc_global
.vfs
.cd_symlinks
563 && mc_stat (tmp_vpath
, &my_stat
) == 0
564 && mc_stat (vfs_get_raw_current_dir (), &my_stat2
) == 0
565 && my_stat
.st_ino
== my_stat2
.st_ino
&& my_stat
.st_dev
== my_stat2
.st_dev
))
566 vfs_set_raw_current_dir (tmp_vpath
);
568 vfs_path_free (tmp_vpath
);
573 /* --------------------------------------------------------------------------------------------- */
575 * Return current directory. If it's local, reread the current directory
583 return vfs_path_to_str (vfs_get_raw_current_dir ());
586 /* --------------------------------------------------------------------------------------------- */
590 * Change encoding for last part (vfs_path_element_t) of vpath
592 * @param vpath pointer to path structure
593 * encoding name of charset
595 * @return pointer to path structure (for use function in anoter functions)
598 vfs_change_encoding (vfs_path_t
* vpath
, const char *encoding
)
600 vfs_path_element_t
*path_element
;
602 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, -1);
603 /* don't add current encoding */
604 if ((path_element
->encoding
!= NULL
) && (strcmp (encoding
, path_element
->encoding
) == 0))
607 g_free (path_element
->encoding
);
608 path_element
->encoding
= g_strdup (encoding
);
610 if (vfs_path_element_need_cleanup_converter (path_element
))
611 str_close_conv (path_element
->dir
.converter
);
613 path_element
->dir
.converter
= str_crt_conv_from (path_element
->encoding
);
617 #endif /* HAVE_CHARSET */
619 /* --------------------------------------------------------------------------------------------- */
622 * Preallocate space for file in new place for ensure that file
623 * will be fully copied with less fragmentation.
625 * @param dest_desc mc VFS file handler
626 * @param src_fsize source file size
627 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
629 * @return 0 if success and non-zero otherwise.
630 * Note: function doesn't touch errno global variable.
633 vfs_preallocate (int dest_vfs_fd
, off_t src_fsize
, off_t dest_fsize
)
635 #ifndef HAVE_POSIX_FALLOCATE
641 #else /* HAVE_POSIX_FALLOCATE */
643 struct vfs_class
*dest_class
;
645 if (!mc_global
.vfs
.preallocate_space
)
648 dest_class
= vfs_class_find_by_handle (dest_vfs_fd
);
649 if ((dest_class
->flags
& VFSF_LOCAL
) == 0)
652 dest_fd
= (int *) vfs_class_data_find_by_handle (dest_vfs_fd
);
659 return posix_fallocate (*dest_fd
, dest_fsize
, src_fsize
- dest_fsize
);
661 #endif /* HAVE_POSIX_FALLOCATE */
664 /* --------------------------------------------------------------------------------------------- */