(vfs_get_raw_current_dir): return pointer to constant.
[midnight-commander.git] / lib / vfs / vfs.c
blobdfff016f2cbe3268956acde0bc5b22402161367b
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 /*** 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
104 * #enc: subtring
105 * buffer - used to store result of translation
108 static estr_t
109 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
111 const char *semi;
112 const char *slash;
113 estr_t state = ESTR_SUCCESS;
115 if (size == 0)
116 return 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))
124 char encoding[16];
125 GIConv coder = INVALID_CONV;
126 int ms;
128 /* first must be translated part before #enc: */
129 ms = semi - path;
131 state = _vfs_translate_path (path, ms, defcnv, buffer);
133 if (state != ESTR_SUCCESS)
134 return state;
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)
141 slash = NULL;
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);
149 encoding[ms] = '\0';
151 #ifdef HAVE_CHARSET
152 if (is_supported_encoding (encoding))
153 coder = str_crt_conv_to (encoding);
154 #endif
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);
173 return state;
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)
184 return NULL;
186 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
187 if (h == NULL)
188 return NULL;
190 g_assert (h->handle == handle);
192 return h;
195 /* --------------------------------------------------------------------------------------------- */
196 /*** public functions ****************************************************************************/
197 /* --------------------------------------------------------------------------------------------- */
198 /** Free open file data for given file handle */
200 void
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);
210 g_free (h);
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 */
220 void *
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 */
233 struct vfs_class *
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);
255 h->fsinfo = fsinfo;
256 h->vclass = vclass;
258 /* Allocate the first free handle */
259 h->handle = vfs_free_handle_list;
260 if (h->handle == -1)
262 /* No free allocated handles, allocate one */
263 h->handle = vfs_openfiles->len;
264 g_ptr_array_add (vfs_openfiles, h);
266 else
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;
273 return h->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 /* --------------------------------------------------------------------------------------------- */
287 gboolean
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 */
292 return FALSE;
294 g_ptr_array_add (vfs__classes_list, vfs);
296 return TRUE;
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.
305 char *
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);
315 if (semi == NULL)
316 return p;
318 *semi = '\0';
319 vfs_prefix = strrchr (p, PATH_SEP);
320 if (vfs_prefix == NULL)
322 *semi = *VFS_PATH_URL_DELIMITER;
323 return p;
325 *vfs_prefix = '\0';
327 return p;
330 /* --------------------------------------------------------------------------------------------- */
332 char *
333 vfs_translate_path (const char *path)
335 estr_t state;
337 g_string_set_size (vfs_str_buffer, 0);
338 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
340 strict version
341 return (state == 0) ? vfs_str_buffer->data : NULL;
343 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
346 /* --------------------------------------------------------------------------------------------- */
348 char *
349 vfs_translate_path_n (const char *path)
351 char *result;
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
364 char *
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
377 const vfs_path_t *
378 vfs_get_raw_current_dir (void)
380 return current_path;
383 /* --------------------------------------------------------------------------------------------- */
385 * Set current directory object.
387 * @param vpath new path
389 void
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 */
399 gboolean
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 */
408 vfs_class_flags_t
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))
415 return VFSF_UNKNOWN;
417 return path_element->class->flags;
420 /* --------------------------------------------------------------------------------------------- */
422 void
423 vfs_init (void)
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 /* --------------------------------------------------------------------------------------------- */
437 void
438 vfs_setup_work_dir (void)
440 const vfs_path_element_t *path_element;
442 vfs_setup_cwd ();
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 /* --------------------------------------------------------------------------------------------- */
456 void
457 vfs_shut (void)
459 guint i;
461 vfs_gc_done ();
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)
470 vfs->done (vfs);
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
485 void
486 vfs_fill_names (fill_names_f func)
488 guint i;
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 /* --------------------------------------------------------------------------------------------- */
500 gboolean
501 vfs_file_is_local (const vfs_path_t * vpath)
503 return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
506 /* --------------------------------------------------------------------------------------------- */
508 void
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
523 * from the OS.
526 void
527 vfs_setup_cwd (void)
529 const vfs_path_element_t *path_element;
531 if (vfs_get_raw_current_dir () == NULL)
533 char *tmp;
535 tmp = g_get_current_dir ();
536 vfs_set_raw_current_dir (vfs_path_from_str (tmp));
537 g_free (tmp);
540 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
542 if ((path_element->class->flags & VFSF_LOCAL) != 0)
544 char *current_dir;
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);
561 else
562 vfs_path_free (tmp_vpath);
567 /* --------------------------------------------------------------------------------------------- */
569 * Return current directory. If it's local, reread the current directory
570 * from the OS.
573 char *
574 _vfs_get_cwd (void)
576 vfs_setup_cwd ();
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)
590 vfs_path_t *
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))
598 return vpath;
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);
608 return vpath;
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
628 (void) dest_vfs_fd;
629 (void) src_fsize;
630 (void) dest_fsize;
631 return 0;
633 #else /* HAVE_POSIX_FALLOCATE */
634 int *dest_fd;
635 struct vfs_class *dest_class;
637 if (!mc_global.vfs.preallocate_space)
638 return 0;
640 dest_class = vfs_class_find_by_handle (dest_vfs_fd);
641 if ((dest_class->flags & VFSF_LOCAL) == 0)
642 return 0;
644 dest_fd = (int *) vfs_class_data_find_by_handle (dest_vfs_fd);
645 if (dest_fd == NULL)
646 return 0;
648 if (src_fsize == 0)
649 return 0;
651 return posix_fallocate (*dest_fd, dest_fsize, src_fsize - dest_fsize);
653 #endif /* HAVE_POSIX_FALLOCATE */
656 /* --------------------------------------------------------------------------------------------- */