Code indentation.
[midnight-commander.git] / lib / vfs / vfs.c
blob2f480e37862ec91fc787a18d7bc53e7044f647c1
1 /*
2 Virtual File System switch code
4 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by: 1995 Miguel de Icaza
9 Jakub Jelinek, 1995
10 Pavel Machek, 1998
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/>.
28 /**
29 * \file
30 * \brief Source: Virtual File System switch code
31 * \author Miguel de Icaza
32 * \author Jakub Jelinek
33 * \author Pavel Machek
34 * \date 1995, 1998
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.
43 #include <config.h>
45 #include <errno.h>
47 #include "lib/global.h"
48 #include "lib/strutil.h"
49 #include "lib/util.h"
50 #include "lib/widget.h" /* message() */
51 #include "lib/event.h"
53 #ifdef HAVE_CHARSET
54 #include "lib/charsets.h"
55 #endif
57 #include "vfs.h"
58 #include "utilvfs.h"
59 #include "gc.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
74 #endif
76 #define VFS_FIRST_HANDLE 100
78 #define ISSLASH(a) (!a || (a == '/'))
80 /*** file scope type declarations ****************************************************************/
82 struct vfs_openfile
84 int handle;
85 struct vfs_class *vclass;
86 void *fsinfo;
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
105 * #enc: subtring
106 * buffer - used to store result of translation
109 static estr_t
110 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
112 estr_t state = ESTR_SUCCESS;
113 #ifdef HAVE_CHARSET
114 const char *semi;
115 const char *slash;
117 if (size == 0)
118 return 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))
126 char encoding[16];
127 GIConv coder = INVALID_CONV;
128 int ms;
130 /* first must be translated part before #enc: */
131 ms = semi - path;
133 state = _vfs_translate_path (path, ms, defcnv, buffer);
135 if (state != ESTR_SUCCESS)
136 return state;
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)
143 slash = NULL;
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);
151 encoding[ms] = '\0';
153 if (is_supported_encoding (encoding))
154 coder = str_crt_conv_to (encoding);
156 if (coder != INVALID_CONV)
158 if (slash != NULL)
159 state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
160 str_close_conv (coder);
161 return state;
164 errno = EINVAL;
165 state = ESTR_FAILURE;
167 else
169 /* path can be translated whole at once */
170 state = str_vfs_convert_to (defcnv, path, size, buffer);
172 #else
173 (void) size;
174 (void) defcnv;
176 g_string_assign (buffer, path);
177 #endif /* HAVE_CHARSET */
179 return state;
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)
190 return NULL;
192 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
193 if (h == NULL)
194 return NULL;
196 g_assert (h->handle == handle);
198 return h;
201 /* --------------------------------------------------------------------------------------------- */
202 /*** public functions ****************************************************************************/
203 /* --------------------------------------------------------------------------------------------- */
204 /** Free open file data for given file handle */
206 void
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);
216 g_free (h);
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 */
226 void *
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 */
239 struct vfs_class *
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);
261 h->fsinfo = fsinfo;
262 h->vclass = vclass;
264 /* Allocate the first free handle */
265 h->handle = vfs_free_handle_list;
266 if (h->handle == -1)
268 /* No free allocated handles, allocate one */
269 h->handle = vfs_openfiles->len;
270 g_ptr_array_add (vfs_openfiles, h);
272 else
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;
279 return h->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 /* --------------------------------------------------------------------------------------------- */
293 gboolean
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 */
298 return FALSE;
300 g_ptr_array_add (vfs__classes_list, vfs);
302 return TRUE;
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.
311 char *
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);
321 if (semi == NULL)
322 return p;
324 *semi = '\0';
325 vfs_prefix = strrchr (p, PATH_SEP);
326 if (vfs_prefix == NULL)
328 *semi = *VFS_PATH_URL_DELIMITER;
329 return p;
331 *vfs_prefix = '\0';
333 return p;
336 /* --------------------------------------------------------------------------------------------- */
338 char *
339 vfs_translate_path (const char *path)
341 estr_t state;
343 g_string_set_size (vfs_str_buffer, 0);
344 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
346 strict version
347 return (state == 0) ? vfs_str_buffer->data : NULL;
349 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
352 /* --------------------------------------------------------------------------------------------- */
354 char *
355 vfs_translate_path_n (const char *path)
357 char *result;
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
370 char *
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
383 const vfs_path_t *
384 vfs_get_raw_current_dir (void)
386 return current_path;
389 /* --------------------------------------------------------------------------------------------- */
391 * Set current directory object.
393 * @param vpath new path
395 void
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 */
405 gboolean
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 */
414 vfs_class_flags_t
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))
421 return VFSF_UNKNOWN;
423 return path_element->class->flags;
426 /* --------------------------------------------------------------------------------------------- */
428 void
429 vfs_init (void)
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 /* --------------------------------------------------------------------------------------------- */
443 void
444 vfs_setup_work_dir (void)
446 const vfs_path_element_t *path_element;
448 vfs_setup_cwd ();
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 /* --------------------------------------------------------------------------------------------- */
462 void
463 vfs_shut (void)
465 guint i;
467 vfs_gc_done ();
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)
476 vfs->done (vfs);
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
491 void
492 vfs_fill_names (fill_names_f func)
494 guint i;
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 /* --------------------------------------------------------------------------------------------- */
506 gboolean
507 vfs_file_is_local (const vfs_path_t * vpath)
509 return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
512 /* --------------------------------------------------------------------------------------------- */
514 void
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
529 * from the OS.
532 void
533 vfs_setup_cwd (void)
535 const vfs_path_element_t *path_element;
537 if (vfs_get_raw_current_dir () == NULL)
539 char *tmp;
541 tmp = g_get_current_dir ();
542 vfs_set_raw_current_dir (vfs_path_from_str (tmp));
543 g_free (tmp);
546 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
548 if ((path_element->class->flags & VFSF_LOCAL) != 0)
550 char *current_dir;
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);
567 else
568 vfs_path_free (tmp_vpath);
573 /* --------------------------------------------------------------------------------------------- */
575 * Return current directory. If it's local, reread the current directory
576 * from the OS.
579 char *
580 _vfs_get_cwd (void)
582 vfs_setup_cwd ();
583 return vfs_path_to_str (vfs_get_raw_current_dir ());
586 /* --------------------------------------------------------------------------------------------- */
588 #ifdef HAVE_CHARSET
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)
597 vfs_path_t *
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))
605 return vpath;
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);
615 return vpath;
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
636 (void) dest_vfs_fd;
637 (void) src_fsize;
638 (void) dest_fsize;
639 return 0;
641 #else /* HAVE_POSIX_FALLOCATE */
642 int *dest_fd;
643 struct vfs_class *dest_class;
645 if (!mc_global.vfs.preallocate_space)
646 return 0;
648 dest_class = vfs_class_find_by_handle (dest_vfs_fd);
649 if ((dest_class->flags & VFSF_LOCAL) == 0)
650 return 0;
652 dest_fd = (int *) vfs_class_data_find_by_handle (dest_vfs_fd);
653 if (dest_fd == NULL)
654 return 0;
656 if (src_fsize == 0)
657 return 0;
659 return posix_fallocate (*dest_fd, dest_fsize, src_fsize - dest_fsize);
661 #endif /* HAVE_POSIX_FALLOCATE */
664 /* --------------------------------------------------------------------------------------------- */