NULL-ize some variables to make unit tests happy.
[midnight-commander.git] / lib / vfs / vfs.c
blob8d2594c401ac2d82e90350c70992da7885d95a61
1 /*
2 Virtual File System switch code
4 Copyright (C) 1995-2016
5 Free Software Foundation, Inc.
7 Written by: 1995 Miguel de Icaza
8 Jakub Jelinek, 1995
9 Pavel Machek, 1998
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/>.
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 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.
43 #include <config.h>
45 #include <errno.h>
46 #include <stdlib.h>
48 #include "lib/global.h"
49 #include "lib/strutil.h"
50 #include "lib/util.h"
51 #include "lib/widget.h" /* message() */
52 #include "lib/event.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h"
56 #endif
58 #include "vfs.h"
59 #include "utilvfs.h"
60 #include "gc.h"
62 /* TODO: move it to the separate .h */
63 extern struct dirent *mc_readdir_result;
64 extern GPtrArray *vfs__classes_list;
65 extern GString *vfs_str_buffer;
66 extern vfs_class *current_vfs;
68 /*** global variables ****************************************************************************/
70 GPtrArray *vfs__classes_list = NULL;
71 GString *vfs_str_buffer = NULL;
72 vfs_class *current_vfs = NULL;
74 /*** file scope macro definitions ****************************************************************/
76 #if defined(_AIX) && !defined(NAME_MAX)
77 #define NAME_MAX FILENAME_MAX
78 #endif
80 #define VFS_FIRST_HANDLE 100
82 /*** file scope type declarations ****************************************************************/
84 struct vfs_openfile
86 int handle;
87 vfs_class *vclass;
88 void *fsinfo;
91 /*** file scope variables ************************************************************************/
93 /** They keep track of the current directory */
94 static vfs_path_t *current_path = NULL;
96 static GPtrArray *vfs_openfiles = NULL;
97 static long vfs_free_handle_list = -1;
99 /* --------------------------------------------------------------------------------------------- */
100 /*** file scope functions ************************************************************************/
101 /* --------------------------------------------------------------------------------------------- */
102 /* now used only by vfs_translate_path, but could be used in other vfs
103 * plugin to automatic detect encoding
104 * path - path to translate
105 * size - how many bytes from path translate
106 * defcnv - convertor, that is used as default, when path does not contain any
107 * #enc: subtring
108 * buffer - used to store result of translation
111 static estr_t
112 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
114 estr_t state = ESTR_SUCCESS;
115 #ifdef HAVE_CHARSET
116 const char *semi;
118 if (size == 0)
119 return ESTR_SUCCESS;
121 size = (size > 0) ? size : (signed int) strlen (path);
123 /* try found /#enc: */
124 semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
125 if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
127 char encoding[16];
128 const char *slash;
129 GIConv coder = INVALID_CONV;
130 int ms;
132 /* first must be translated part before #enc: */
133 ms = semi - path;
135 state = _vfs_translate_path (path, ms, defcnv, buffer);
137 if (state != ESTR_SUCCESS)
138 return state;
140 /* now can be translated part after #enc: */
141 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
142 slash = strchr (semi, PATH_SEP);
143 /* ignore slashes after size; */
144 if (slash - path >= size)
145 slash = NULL;
147 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
148 ms = MIN ((unsigned int) ms, sizeof (encoding) - 1);
149 /* limit encoding size (ms) to path size (size) */
150 if (semi + ms > path + size)
151 ms = path + size - semi;
152 memcpy (encoding, semi, ms);
153 encoding[ms] = '\0';
155 if (is_supported_encoding (encoding))
156 coder = str_crt_conv_to (encoding);
158 if (coder != INVALID_CONV)
160 if (slash != NULL)
161 state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
162 str_close_conv (coder);
163 return state;
166 errno = EINVAL;
167 state = ESTR_FAILURE;
169 else
171 /* path can be translated whole at once */
172 state = str_vfs_convert_to (defcnv, path, size, buffer);
174 #else
175 (void) size;
176 (void) defcnv;
178 g_string_assign (buffer, path);
179 #endif /* HAVE_CHARSET */
181 return state;
184 /* --------------------------------------------------------------------------------------------- */
186 static struct vfs_openfile *
187 vfs_get_openfile (int handle)
189 struct vfs_openfile *h;
191 if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
192 return NULL;
194 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
195 if (h == NULL)
196 return NULL;
198 g_assert (h->handle == handle);
200 return h;
203 /* --------------------------------------------------------------------------------------------- */
205 static gboolean
206 vfs_test_current_dir (const vfs_path_t * vpath)
208 struct stat my_stat, my_stat2;
210 return (mc_global.vfs.cd_symlinks && mc_stat (vpath, &my_stat) == 0
211 && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
212 && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev);
216 /* --------------------------------------------------------------------------------------------- */
217 /*** public functions ****************************************************************************/
218 /* --------------------------------------------------------------------------------------------- */
219 /** Free open file data for given file handle */
221 void
222 vfs_free_handle (int handle)
224 const int idx = handle - VFS_FIRST_HANDLE;
226 if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
228 struct vfs_openfile *h;
230 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
231 g_free (h);
232 g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
233 vfs_free_handle_list = idx;
238 /* --------------------------------------------------------------------------------------------- */
239 /** Find VFS class by file handle */
241 struct vfs_class *
242 vfs_class_find_by_handle (int handle, void **fsinfo)
244 struct vfs_openfile *h;
246 h = vfs_get_openfile (handle);
248 if (h == NULL)
249 return NULL;
251 if (fsinfo != NULL)
252 *fsinfo = h->fsinfo;
254 return h->vclass;
257 /* --------------------------------------------------------------------------------------------- */
260 * Create new VFS handle and put it to the list
264 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
266 struct vfs_openfile *h;
268 h = g_new (struct vfs_openfile, 1);
269 h->fsinfo = fsinfo;
270 h->vclass = vclass;
272 /* Allocate the first free handle */
273 h->handle = vfs_free_handle_list;
274 if (h->handle == -1)
276 /* No free allocated handles, allocate one */
277 h->handle = vfs_openfiles->len;
278 g_ptr_array_add (vfs_openfiles, h);
280 else
282 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
283 g_ptr_array_index (vfs_openfiles, h->handle) = h;
286 h->handle += VFS_FIRST_HANDLE;
287 return h->handle;
290 /* --------------------------------------------------------------------------------------------- */
293 vfs_ferrno (struct vfs_class *vfs)
295 return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
296 /* Hope that error message is obscure enough ;-) */
299 /* --------------------------------------------------------------------------------------------- */
301 gboolean
302 vfs_register_class (struct vfs_class * vfs)
304 if (vfs->init != NULL) /* vfs has own initialization function */
305 if (!vfs->init (vfs)) /* but it failed */
306 return FALSE;
308 g_ptr_array_add (vfs__classes_list, vfs);
310 return TRUE;
313 /* --------------------------------------------------------------------------------------------- */
314 /** Strip known vfs suffixes from a filename (possible improvement: strip
315 * suffix from last path component).
316 * \return a malloced string which has to be freed.
319 char *
320 vfs_strip_suffix_from_filename (const char *filename)
322 char *semi, *p;
324 if (filename == NULL)
325 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
327 p = g_strdup (filename);
328 semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
329 if (semi != NULL)
331 char *vfs_prefix;
333 *semi = '\0';
334 vfs_prefix = strrchr (p, PATH_SEP);
335 if (vfs_prefix == NULL)
336 *semi = *VFS_PATH_URL_DELIMITER;
337 else
338 *vfs_prefix = '\0';
341 return p;
344 /* --------------------------------------------------------------------------------------------- */
346 const char *
347 vfs_translate_path (const char *path)
349 estr_t state;
351 g_string_set_size (vfs_str_buffer, 0);
352 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
353 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
356 /* --------------------------------------------------------------------------------------------- */
358 char *
359 vfs_translate_path_n (const char *path)
361 const char *result;
363 result = vfs_translate_path (path);
364 return g_strdup (result);
367 /* --------------------------------------------------------------------------------------------- */
369 * Get current directory without any OS calls.
371 * @return string contains current path
374 const char *
375 vfs_get_current_dir (void)
377 return current_path->str;
380 /* --------------------------------------------------------------------------------------------- */
382 * Get current directory without any OS calls.
384 * @return newly allocated string contains current path
387 char *
388 vfs_get_current_dir_n (void)
390 return g_strdup (current_path->str);
393 /* --------------------------------------------------------------------------------------------- */
395 * Get raw current directory object without any OS calls.
397 * @return object contain current path
400 const vfs_path_t *
401 vfs_get_raw_current_dir (void)
403 return current_path;
406 /* --------------------------------------------------------------------------------------------- */
408 * Set current directory object.
410 * @param vpath new path
412 void
413 vfs_set_raw_current_dir (const vfs_path_t * vpath)
415 vfs_path_free (current_path);
416 current_path = (vfs_path_t *) vpath;
419 /* --------------------------------------------------------------------------------------------- */
420 /* Return TRUE is the current VFS class is local */
422 gboolean
423 vfs_current_is_local (void)
425 return (current_vfs->flags & VFSF_LOCAL) != 0;
428 /* --------------------------------------------------------------------------------------------- */
429 /* Return flags of the VFS class of the given filename */
431 vfs_class_flags_t
432 vfs_file_class_flags (const vfs_path_t * vpath)
434 const vfs_path_element_t *path_element;
436 path_element = vfs_path_get_by_index (vpath, -1);
437 if (!vfs_path_element_valid (path_element))
438 return VFSF_UNKNOWN;
440 return path_element->class->flags;
443 /* --------------------------------------------------------------------------------------------- */
445 void
446 vfs_init (void)
448 /* create the VFS handle arrays */
449 vfs__classes_list = g_ptr_array_new ();
451 /* create the VFS handle array */
452 vfs_openfiles = g_ptr_array_new ();
454 vfs_str_buffer = g_string_new ("");
458 /* --------------------------------------------------------------------------------------------- */
460 void
461 vfs_setup_work_dir (void)
463 const vfs_path_element_t *path_element;
465 vfs_setup_cwd ();
467 /* FIXME: is we really need for this check? */
469 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
470 vfs_die ("Current dir too long.\n");
473 path_element = vfs_path_get_by_index (current_path, -1);
474 current_vfs = path_element->class;
477 /* --------------------------------------------------------------------------------------------- */
479 void
480 vfs_shut (void)
482 guint i;
484 vfs_gc_done ();
486 vfs_set_raw_current_dir (NULL);
488 for (i = 0; i < vfs__classes_list->len; i++)
490 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
492 if (vfs->done != NULL)
493 vfs->done (vfs);
496 /* NULL-ize pointers to make unit tests happy */
497 g_ptr_array_free (vfs_openfiles, TRUE);
498 vfs_openfiles = NULL;
499 g_ptr_array_free (vfs__classes_list, TRUE);
500 vfs__classes_list = NULL;
501 g_string_free (vfs_str_buffer, TRUE);
502 vfs_str_buffer = NULL;
503 current_vfs = NULL;
504 vfs_free_handle_list = -1;
505 MC_PTR_FREE (mc_readdir_result);
508 /* --------------------------------------------------------------------------------------------- */
510 * These ones grab information from the VFS
511 * and handles them to an upper layer
514 void
515 vfs_fill_names (fill_names_f func)
517 guint i;
519 for (i = 0; i < vfs__classes_list->len; i++)
521 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
523 if (vfs->fill_names != NULL)
524 vfs->fill_names (vfs, func);
528 /* --------------------------------------------------------------------------------------------- */
529 gboolean
530 vfs_file_is_local (const vfs_path_t * vpath)
532 return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
535 /* --------------------------------------------------------------------------------------------- */
537 void
538 vfs_print_message (const char *msg, ...)
540 ev_vfs_print_message_t event_data;
541 va_list ap;
543 va_start (ap, msg);
544 event_data.msg = g_strdup_vprintf (msg, ap);
545 va_end (ap);
547 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
550 /* --------------------------------------------------------------------------------------------- */
552 * If it's local, reread the current directory
553 * from the OS.
556 void
557 vfs_setup_cwd (void)
559 char *current_dir;
560 vfs_path_t *tmp_vpath;
561 const vfs_path_element_t *path_element;
563 if (vfs_get_raw_current_dir () == NULL)
565 current_dir = g_get_current_dir ();
566 vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
567 g_free (current_dir);
569 current_dir = getenv ("PWD");
570 tmp_vpath = vfs_path_from_str (current_dir);
572 if (tmp_vpath != NULL)
574 if (vfs_test_current_dir (tmp_vpath))
575 vfs_set_raw_current_dir (tmp_vpath);
576 else
577 vfs_path_free (tmp_vpath);
581 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
583 if ((path_element->class->flags & VFSF_LOCAL) != 0)
585 current_dir = g_get_current_dir ();
586 tmp_vpath = vfs_path_from_str (current_dir);
587 g_free (current_dir);
589 if (tmp_vpath != NULL)
591 /* One of directories in the path is not readable */
593 /* Check if it is O.K. to use the current_dir */
594 if (!vfs_test_current_dir (tmp_vpath))
595 vfs_set_raw_current_dir (tmp_vpath);
596 else
597 vfs_path_free (tmp_vpath);
602 /* --------------------------------------------------------------------------------------------- */
604 * Return current directory. If it's local, reread the current directory
605 * from the OS.
608 char *
609 _vfs_get_cwd (void)
611 const vfs_path_t *current_dir_vpath;
613 vfs_setup_cwd ();
614 current_dir_vpath = vfs_get_raw_current_dir ();
615 return g_strdup (vfs_path_as_str (current_dir_vpath));
618 /* --------------------------------------------------------------------------------------------- */
620 * Preallocate space for file in new place for ensure that file
621 * will be fully copied with less fragmentation.
623 * @param dest_vfs_fd mc VFS file handler
624 * @param src_fsize source file size
625 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
627 * @return 0 if success and non-zero otherwise.
628 * Note: function doesn't touch errno global variable.
632 vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
634 #ifndef HAVE_POSIX_FALLOCATE
635 (void) dest_vfs_fd;
636 (void) src_fsize;
637 (void) dest_fsize;
638 return 0;
640 #else /* HAVE_POSIX_FALLOCATE */
641 void *dest_fd = NULL;
642 struct vfs_class *dest_class;
644 if (!mc_global.vfs.preallocate_space)
645 return 0;
647 if (src_fsize == 0)
648 return 0;
650 dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
651 if ((dest_class->flags & VFSF_LOCAL) == 0 || dest_fd == NULL)
652 return 0;
654 return posix_fallocate (*(int *) dest_fd, dest_fsize, src_fsize - dest_fsize);
656 #endif /* HAVE_POSIX_FALLOCATE */
659 /* --------------------------------------------------------------------------------------------- */