2 Virtual File System switch code
4 Copyright (C) 1995-2019
5 Free Software Foundation, Inc.
7 Written by: 1995 Miguel de Icaza
10 Slava Zanko <slavazanko@gmail.com>, 2013
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 functions 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.
49 #ifdef HAVE_LINUX_FS_H
51 #endif /* HAVE_LINUX_FS_H */
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
54 #endif /* HAVE_SYS_IOCTL_H */
55 #endif /* __linux__ */
57 #include "lib/global.h"
58 #include "lib/strutil.h"
60 #include "lib/widget.h" /* message() */
61 #include "lib/event.h"
64 #include "lib/charsets.h"
71 /* TODO: move it to the separate .h */
72 extern struct dirent
*mc_readdir_result
;
73 extern GPtrArray
*vfs__classes_list
;
74 extern GString
*vfs_str_buffer
;
75 extern vfs_class
*current_vfs
;
77 /*** global variables ****************************************************************************/
79 GPtrArray
*vfs__classes_list
= NULL
;
80 GString
*vfs_str_buffer
= NULL
;
81 vfs_class
*current_vfs
= NULL
;
83 /*** file scope macro definitions ****************************************************************/
85 #define VFS_FIRST_HANDLE 100
87 /*** file scope type declarations ****************************************************************/
96 /*** file scope variables ************************************************************************/
98 /** They keep track of the current directory */
99 static vfs_path_t
*current_path
= NULL
;
101 static GPtrArray
*vfs_openfiles
= NULL
;
102 static long vfs_free_handle_list
= -1;
104 /* --------------------------------------------------------------------------------------------- */
105 /*** file scope functions ************************************************************************/
106 /* --------------------------------------------------------------------------------------------- */
107 /* now used only by vfs_translate_path, but could be used in other vfs
108 * plugin to automatic detect encoding
109 * path - path to translate
110 * size - how many bytes from path translate
111 * defcnv - convertor, that is used as default, when path does not contain any
113 * buffer - used to store result of translation
117 _vfs_translate_path (const char *path
, int size
, GIConv defcnv
, GString
* buffer
)
119 estr_t state
= ESTR_SUCCESS
;
126 size
= (size
> 0) ? size
: (signed int) strlen (path
);
128 /* try found /#enc: */
129 semi
= g_strrstr_len (path
, size
, VFS_ENCODING_PREFIX
);
130 if (semi
!= NULL
&& (semi
== path
|| IS_PATH_SEP (semi
[-1])))
134 GIConv coder
= INVALID_CONV
;
137 /* first must be translated part before #enc: */
140 state
= _vfs_translate_path (path
, ms
, defcnv
, buffer
);
142 if (state
!= ESTR_SUCCESS
)
145 /* now can be translated part after #enc: */
146 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
147 slash
= strchr (semi
, PATH_SEP
);
148 /* ignore slashes after size; */
149 if (slash
- path
>= size
)
152 ms
= (slash
!= NULL
) ? slash
- semi
: (int) strlen (semi
);
153 ms
= MIN ((unsigned int) ms
, sizeof (encoding
) - 1);
154 /* limit encoding size (ms) to path size (size) */
155 if (semi
+ ms
> path
+ size
)
156 ms
= path
+ size
- semi
;
157 memcpy (encoding
, semi
, ms
);
160 if (is_supported_encoding (encoding
))
161 coder
= str_crt_conv_to (encoding
);
163 if (coder
!= INVALID_CONV
)
166 state
= str_vfs_convert_to (coder
, slash
+ 1, path
+ size
- slash
- 1, buffer
);
167 str_close_conv (coder
);
172 state
= ESTR_FAILURE
;
176 /* path can be translated whole at once */
177 state
= str_vfs_convert_to (defcnv
, path
, size
, buffer
);
183 g_string_assign (buffer
, path
);
184 #endif /* HAVE_CHARSET */
189 /* --------------------------------------------------------------------------------------------- */
191 static struct vfs_openfile
*
192 vfs_get_openfile (int handle
)
194 struct vfs_openfile
*h
;
196 if (handle
< VFS_FIRST_HANDLE
|| (guint
) (handle
- VFS_FIRST_HANDLE
) >= vfs_openfiles
->len
)
199 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, handle
- VFS_FIRST_HANDLE
);
203 g_assert (h
->handle
== handle
);
208 /* --------------------------------------------------------------------------------------------- */
211 vfs_test_current_dir (const vfs_path_t
* vpath
)
213 struct stat my_stat
, my_stat2
;
215 return (mc_global
.vfs
.cd_symlinks
&& mc_stat (vpath
, &my_stat
) == 0
216 && mc_stat (vfs_get_raw_current_dir (), &my_stat2
) == 0
217 && my_stat
.st_ino
== my_stat2
.st_ino
&& my_stat
.st_dev
== my_stat2
.st_dev
);
221 /* --------------------------------------------------------------------------------------------- */
222 /*** public functions ****************************************************************************/
223 /* --------------------------------------------------------------------------------------------- */
224 /** Free open file data for given file handle */
227 vfs_free_handle (int handle
)
229 const int idx
= handle
- VFS_FIRST_HANDLE
;
231 if (handle
>= VFS_FIRST_HANDLE
&& (guint
) idx
< vfs_openfiles
->len
)
233 struct vfs_openfile
*h
;
235 h
= (struct vfs_openfile
*) g_ptr_array_index (vfs_openfiles
, idx
);
237 g_ptr_array_index (vfs_openfiles
, idx
) = (void *) vfs_free_handle_list
;
238 vfs_free_handle_list
= idx
;
243 /* --------------------------------------------------------------------------------------------- */
244 /** Find VFS class by file handle */
247 vfs_class_find_by_handle (int handle
, void **fsinfo
)
249 struct vfs_openfile
*h
;
251 h
= vfs_get_openfile (handle
);
262 /* --------------------------------------------------------------------------------------------- */
265 * Create new VFS handle and put it to the list
269 vfs_new_handle (struct vfs_class
*vclass
, void *fsinfo
)
271 struct vfs_openfile
*h
;
273 h
= g_new (struct vfs_openfile
, 1);
277 /* Allocate the first free handle */
278 h
->handle
= vfs_free_handle_list
;
281 /* No free allocated handles, allocate one */
282 h
->handle
= vfs_openfiles
->len
;
283 g_ptr_array_add (vfs_openfiles
, h
);
287 vfs_free_handle_list
= (long) g_ptr_array_index (vfs_openfiles
, vfs_free_handle_list
);
288 g_ptr_array_index (vfs_openfiles
, h
->handle
) = h
;
291 h
->handle
+= VFS_FIRST_HANDLE
;
295 /* --------------------------------------------------------------------------------------------- */
298 vfs_ferrno (struct vfs_class
*vfs
)
300 return vfs
->ferrno
? (*vfs
->ferrno
) (vfs
) : E_UNKNOWN
;
301 /* Hope that error message is obscure enough ;-) */
304 /* --------------------------------------------------------------------------------------------- */
307 vfs_register_class (struct vfs_class
* vfs
)
309 if (vfs
->init
!= NULL
) /* vfs has own initialization function */
310 if (!vfs
->init (vfs
)) /* but it failed */
313 g_ptr_array_add (vfs__classes_list
, vfs
);
318 /* --------------------------------------------------------------------------------------------- */
319 /** Strip known vfs suffixes from a filename (possible improvement: strip
320 * suffix from last path component).
321 * \return a malloced string which has to be freed.
325 vfs_strip_suffix_from_filename (const char *filename
)
329 if (filename
== NULL
)
330 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
332 p
= g_strdup (filename
);
333 semi
= g_strrstr (p
, VFS_PATH_URL_DELIMITER
);
339 vfs_prefix
= strrchr (p
, PATH_SEP
);
340 if (vfs_prefix
== NULL
)
341 *semi
= *VFS_PATH_URL_DELIMITER
;
349 /* --------------------------------------------------------------------------------------------- */
352 vfs_translate_path (const char *path
)
356 g_string_set_size (vfs_str_buffer
, 0);
357 state
= _vfs_translate_path (path
, -1, str_cnv_from_term
, vfs_str_buffer
);
358 return (state
!= ESTR_FAILURE
) ? vfs_str_buffer
->str
: NULL
;
361 /* --------------------------------------------------------------------------------------------- */
364 vfs_translate_path_n (const char *path
)
368 result
= vfs_translate_path (path
);
369 return g_strdup (result
);
372 /* --------------------------------------------------------------------------------------------- */
374 * Get current directory without any OS calls.
376 * @return string contains current path
380 vfs_get_current_dir (void)
382 return current_path
->str
;
385 /* --------------------------------------------------------------------------------------------- */
387 * Get current directory without any OS calls.
389 * @return newly allocated string contains current path
393 vfs_get_current_dir_n (void)
395 return g_strdup (current_path
->str
);
398 /* --------------------------------------------------------------------------------------------- */
400 * Get raw current directory object without any OS calls.
402 * @return object contain current path
406 vfs_get_raw_current_dir (void)
411 /* --------------------------------------------------------------------------------------------- */
413 * Set current directory object.
415 * @param vpath new path
418 vfs_set_raw_current_dir (const vfs_path_t
* vpath
)
420 vfs_path_free (current_path
);
421 current_path
= (vfs_path_t
*) vpath
;
424 /* --------------------------------------------------------------------------------------------- */
425 /* Return TRUE is the current VFS class is local */
428 vfs_current_is_local (void)
430 return (current_vfs
->flags
& VFSF_LOCAL
) != 0;
433 /* --------------------------------------------------------------------------------------------- */
434 /* Return flags of the VFS class of the given filename */
437 vfs_file_class_flags (const vfs_path_t
* vpath
)
439 const vfs_path_element_t
*path_element
;
441 path_element
= vfs_path_get_by_index (vpath
, -1);
442 if (!vfs_path_element_valid (path_element
))
445 return path_element
->class->flags
;
448 /* --------------------------------------------------------------------------------------------- */
453 /* create the VFS handle arrays */
454 vfs__classes_list
= g_ptr_array_new ();
456 /* create the VFS handle array */
457 vfs_openfiles
= g_ptr_array_new ();
459 vfs_str_buffer
= g_string_new ("");
463 /* --------------------------------------------------------------------------------------------- */
466 vfs_setup_work_dir (void)
468 const vfs_path_element_t
*path_element
;
472 /* FIXME: is we really need for this check? */
474 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
475 vfs_die ("Current dir too long.\n");
478 path_element
= vfs_path_get_by_index (current_path
, -1);
479 current_vfs
= path_element
->class;
482 /* --------------------------------------------------------------------------------------------- */
491 vfs_set_raw_current_dir (NULL
);
493 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
495 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
497 if (vfs
->done
!= NULL
)
501 /* NULL-ize pointers to make unit tests happy */
502 g_ptr_array_free (vfs_openfiles
, TRUE
);
503 vfs_openfiles
= NULL
;
504 g_ptr_array_free (vfs__classes_list
, TRUE
);
505 vfs__classes_list
= NULL
;
506 g_string_free (vfs_str_buffer
, TRUE
);
507 vfs_str_buffer
= NULL
;
509 vfs_free_handle_list
= -1;
510 MC_PTR_FREE (mc_readdir_result
);
513 /* --------------------------------------------------------------------------------------------- */
515 * These ones grab information from the VFS
516 * and handles them to an upper layer
520 vfs_fill_names (fill_names_f func
)
524 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
526 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
528 if (vfs
->fill_names
!= NULL
)
529 vfs
->fill_names (vfs
, func
);
533 /* --------------------------------------------------------------------------------------------- */
535 vfs_file_is_local (const vfs_path_t
* vpath
)
537 return (vfs_file_class_flags (vpath
) & VFSF_LOCAL
) != 0;
540 /* --------------------------------------------------------------------------------------------- */
543 vfs_print_message (const char *msg
, ...)
545 ev_vfs_print_message_t event_data
;
549 event_data
.msg
= g_strdup_vprintf (msg
, ap
);
552 mc_event_raise (MCEVENT_GROUP_CORE
, "vfs_print_message", (gpointer
) & event_data
);
555 /* --------------------------------------------------------------------------------------------- */
557 * If it's local, reread the current directory
565 vfs_path_t
*tmp_vpath
;
566 const vfs_path_element_t
*path_element
;
568 if (vfs_get_raw_current_dir () == NULL
)
570 current_dir
= g_get_current_dir ();
571 vfs_set_raw_current_dir (vfs_path_from_str (current_dir
));
572 g_free (current_dir
);
574 current_dir
= getenv ("PWD");
575 tmp_vpath
= vfs_path_from_str (current_dir
);
577 if (tmp_vpath
!= NULL
)
579 if (vfs_test_current_dir (tmp_vpath
))
580 vfs_set_raw_current_dir (tmp_vpath
);
582 vfs_path_free (tmp_vpath
);
586 path_element
= vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
588 if ((path_element
->class->flags
& VFSF_LOCAL
) != 0)
590 current_dir
= g_get_current_dir ();
591 tmp_vpath
= vfs_path_from_str (current_dir
);
592 g_free (current_dir
);
594 if (tmp_vpath
!= NULL
)
596 /* One of directories in the path is not readable */
598 /* Check if it is O.K. to use the current_dir */
599 if (!vfs_test_current_dir (tmp_vpath
))
600 vfs_set_raw_current_dir (tmp_vpath
);
602 vfs_path_free (tmp_vpath
);
607 /* --------------------------------------------------------------------------------------------- */
609 * Return current directory. If it's local, reread the current directory
616 const vfs_path_t
*current_dir_vpath
;
619 current_dir_vpath
= vfs_get_raw_current_dir ();
620 return g_strdup (vfs_path_as_str (current_dir_vpath
));
623 /* --------------------------------------------------------------------------------------------- */
625 * Preallocate space for file in new place for ensure that file
626 * will be fully copied with less fragmentation.
628 * @param dest_vfs_fd mc VFS file handler
629 * @param src_fsize source file size
630 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
632 * @return 0 if success and non-zero otherwise.
633 * Note: function doesn't touch errno global variable.
637 vfs_preallocate (int dest_vfs_fd
, off_t src_fsize
, off_t dest_fsize
)
639 #ifndef HAVE_POSIX_FALLOCATE
645 #else /* HAVE_POSIX_FALLOCATE */
646 void *dest_fd
= NULL
;
647 struct vfs_class
*dest_class
;
652 dest_class
= vfs_class_find_by_handle (dest_vfs_fd
, &dest_fd
);
653 if ((dest_class
->flags
& VFSF_LOCAL
) == 0 || dest_fd
== NULL
)
656 return posix_fallocate (*(int *) dest_fd
, dest_fsize
, src_fsize
- dest_fsize
);
658 #endif /* HAVE_POSIX_FALLOCATE */
661 /* --------------------------------------------------------------------------------------------- */
664 vfs_clone_file (int dest_vfs_fd
, int src_vfs_fd
)
667 void *dest_fd
= NULL
;
669 struct vfs_class
*dest_class
;
670 struct vfs_class
*src_class
;
672 dest_class
= vfs_class_find_by_handle (dest_vfs_fd
, &dest_fd
);
673 if ((dest_class
->flags
& VFSF_LOCAL
) == 0)
684 src_class
= vfs_class_find_by_handle (src_vfs_fd
, &src_fd
);
685 if ((src_class
->flags
& VFSF_LOCAL
) == 0)
696 return ioctl (*(int *) dest_fd
, FICLONE
, *(int *) src_fd
);
705 /* --------------------------------------------------------------------------------------------- */