Remove min() and max() macros. Use MIN() and MAX() macros from GLib.
[midnight-commander.git] / lib / vfs / vfs.c
blob1d6ed149944c72e586b42d444c86f1ec5b3b3cb3
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 #define ISSLASH(a) (a == '\0' || IS_PATH_SEP (a))
84 /*** file scope type declarations ****************************************************************/
86 struct vfs_openfile
88 int handle;
89 vfs_class *vclass;
90 void *fsinfo;
93 /*** file scope variables ************************************************************************/
95 /** They keep track of the current directory */
96 static vfs_path_t *current_path = NULL;
98 static GPtrArray *vfs_openfiles;
99 static long vfs_free_handle_list = -1;
101 /* --------------------------------------------------------------------------------------------- */
102 /*** file scope functions ************************************************************************/
103 /* --------------------------------------------------------------------------------------------- */
104 /* now used only by vfs_translate_path, but could be used in other vfs
105 * plugin to automatic detect encoding
106 * path - path to translate
107 * size - how many bytes from path translate
108 * defcnv - convertor, that is used as default, when path does not contain any
109 * #enc: subtring
110 * buffer - used to store result of translation
113 static estr_t
114 _vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
116 estr_t state = ESTR_SUCCESS;
117 #ifdef HAVE_CHARSET
118 const char *semi;
120 if (size == 0)
121 return ESTR_SUCCESS;
123 size = (size > 0) ? size : (signed int) strlen (path);
125 /* try found /#enc: */
126 semi = g_strrstr_len (path, size, VFS_ENCODING_PREFIX);
127 if (semi != NULL && (semi == path || IS_PATH_SEP (semi[-1])))
129 char encoding[16];
130 const char *slash;
131 GIConv coder = INVALID_CONV;
132 int ms;
134 /* first must be translated part before #enc: */
135 ms = semi - path;
137 state = _vfs_translate_path (path, ms, defcnv, buffer);
139 if (state != ESTR_SUCCESS)
140 return state;
142 /* now can be translated part after #enc: */
143 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
144 slash = strchr (semi, PATH_SEP);
145 /* ignore slashes after size; */
146 if (slash - path >= size)
147 slash = NULL;
149 ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
150 ms = MIN ((unsigned int) ms, sizeof (encoding) - 1);
151 /* limit encoding size (ms) to path size (size) */
152 if (semi + ms > path + size)
153 ms = path + size - semi;
154 memcpy (encoding, semi, ms);
155 encoding[ms] = '\0';
157 if (is_supported_encoding (encoding))
158 coder = str_crt_conv_to (encoding);
160 if (coder != INVALID_CONV)
162 if (slash != NULL)
163 state = str_vfs_convert_to (coder, slash + 1, path + size - slash - 1, buffer);
164 str_close_conv (coder);
165 return state;
168 errno = EINVAL;
169 state = ESTR_FAILURE;
171 else
173 /* path can be translated whole at once */
174 state = str_vfs_convert_to (defcnv, path, size, buffer);
176 #else
177 (void) size;
178 (void) defcnv;
180 g_string_assign (buffer, path);
181 #endif /* HAVE_CHARSET */
183 return state;
186 /* --------------------------------------------------------------------------------------------- */
188 static struct vfs_openfile *
189 vfs_get_openfile (int handle)
191 struct vfs_openfile *h;
193 if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
194 return NULL;
196 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
197 if (h == NULL)
198 return NULL;
200 g_assert (h->handle == handle);
202 return h;
205 /* --------------------------------------------------------------------------------------------- */
206 /*** public functions ****************************************************************************/
207 /* --------------------------------------------------------------------------------------------- */
208 /** Free open file data for given file handle */
210 void
211 vfs_free_handle (int handle)
213 const int idx = handle - VFS_FIRST_HANDLE;
215 if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
217 struct vfs_openfile *h;
219 h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
220 g_free (h);
221 g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
222 vfs_free_handle_list = idx;
227 /* --------------------------------------------------------------------------------------------- */
228 /** Find VFS class by file handle */
230 struct vfs_class *
231 vfs_class_find_by_handle (int handle, void **fsinfo)
233 struct vfs_openfile *h;
235 h = vfs_get_openfile (handle);
237 if (h == NULL)
238 return NULL;
240 if (fsinfo != NULL)
241 *fsinfo = h->fsinfo;
243 return h->vclass;
246 /* --------------------------------------------------------------------------------------------- */
249 * Create new VFS handle and put it to the list
253 vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
255 struct vfs_openfile *h;
257 h = g_new (struct vfs_openfile, 1);
258 h->fsinfo = fsinfo;
259 h->vclass = vclass;
261 /* Allocate the first free handle */
262 h->handle = vfs_free_handle_list;
263 if (h->handle == -1)
265 /* No free allocated handles, allocate one */
266 h->handle = vfs_openfiles->len;
267 g_ptr_array_add (vfs_openfiles, h);
269 else
271 vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
272 g_ptr_array_index (vfs_openfiles, h->handle) = h;
275 h->handle += VFS_FIRST_HANDLE;
276 return h->handle;
279 /* --------------------------------------------------------------------------------------------- */
282 vfs_ferrno (struct vfs_class *vfs)
284 return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
285 /* Hope that error message is obscure enough ;-) */
288 /* --------------------------------------------------------------------------------------------- */
290 gboolean
291 vfs_register_class (struct vfs_class * vfs)
293 if (vfs->init != NULL) /* vfs has own initialization function */
294 if (!vfs->init (vfs)) /* but it failed */
295 return FALSE;
297 g_ptr_array_add (vfs__classes_list, vfs);
299 return TRUE;
302 /* --------------------------------------------------------------------------------------------- */
303 /** Strip known vfs suffixes from a filename (possible improvement: strip
304 * suffix from last path component).
305 * \return a malloced string which has to be freed.
308 char *
309 vfs_strip_suffix_from_filename (const char *filename)
311 char *semi, *p, *vfs_prefix;
313 if (filename == NULL)
314 vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
316 p = g_strdup (filename);
317 semi = g_strrstr (p, VFS_PATH_URL_DELIMITER);
318 if (semi == NULL)
319 return p;
321 *semi = '\0';
322 vfs_prefix = strrchr (p, PATH_SEP);
323 if (vfs_prefix == NULL)
325 *semi = *VFS_PATH_URL_DELIMITER;
326 return p;
328 *vfs_prefix = '\0';
330 return p;
333 /* --------------------------------------------------------------------------------------------- */
335 const char *
336 vfs_translate_path (const char *path)
338 estr_t state;
340 g_string_set_size (vfs_str_buffer, 0);
341 state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
342 return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
345 /* --------------------------------------------------------------------------------------------- */
347 char *
348 vfs_translate_path_n (const char *path)
350 const char *result;
352 result = vfs_translate_path (path);
353 return g_strdup (result);
356 /* --------------------------------------------------------------------------------------------- */
358 * Get current directory without any OS calls.
360 * @return string contains current path
363 const char *
364 vfs_get_current_dir (void)
366 return current_path->str;
369 /* --------------------------------------------------------------------------------------------- */
371 * Get current directory without any OS calls.
373 * @return newly allocated string contains current path
376 char *
377 vfs_get_current_dir_n (void)
379 return g_strdup (current_path->str);
382 /* --------------------------------------------------------------------------------------------- */
384 * Get raw current directory object without any OS calls.
386 * @return object contain current path
389 const vfs_path_t *
390 vfs_get_raw_current_dir (void)
392 return current_path;
395 /* --------------------------------------------------------------------------------------------- */
397 * Set current directory object.
399 * @param vpath new path
401 void
402 vfs_set_raw_current_dir (const vfs_path_t * vpath)
404 vfs_path_free (current_path);
405 current_path = (vfs_path_t *) vpath;
408 /* --------------------------------------------------------------------------------------------- */
409 /* Return TRUE is the current VFS class is local */
411 gboolean
412 vfs_current_is_local (void)
414 return (current_vfs->flags & VFSF_LOCAL) != 0;
417 /* --------------------------------------------------------------------------------------------- */
418 /* Return flags of the VFS class of the given filename */
420 vfs_class_flags_t
421 vfs_file_class_flags (const vfs_path_t * vpath)
423 const vfs_path_element_t *path_element;
425 path_element = vfs_path_get_by_index (vpath, -1);
426 if (!vfs_path_element_valid (path_element))
427 return VFSF_UNKNOWN;
429 return path_element->class->flags;
432 /* --------------------------------------------------------------------------------------------- */
434 void
435 vfs_init (void)
437 /* create the VFS handle arrays */
438 vfs__classes_list = g_ptr_array_new ();
440 /* create the VFS handle array */
441 vfs_openfiles = g_ptr_array_new ();
443 vfs_str_buffer = g_string_new ("");
447 /* --------------------------------------------------------------------------------------------- */
449 void
450 vfs_setup_work_dir (void)
452 const vfs_path_element_t *path_element;
454 vfs_setup_cwd ();
456 /* FIXME: is we really need for this check? */
458 if (strlen (current_dir) > MC_MAXPATHLEN - 2)
459 vfs_die ("Current dir too long.\n");
462 path_element = vfs_path_get_by_index (current_path, -1);
463 current_vfs = path_element->class;
466 /* --------------------------------------------------------------------------------------------- */
468 void
469 vfs_shut (void)
471 guint i;
473 vfs_gc_done ();
475 vfs_set_raw_current_dir (NULL);
477 for (i = 0; i < vfs__classes_list->len; i++)
479 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
481 if (vfs->done != NULL)
482 vfs->done (vfs);
485 g_ptr_array_free (vfs_openfiles, TRUE);
486 g_ptr_array_free (vfs__classes_list, TRUE);
487 g_string_free (vfs_str_buffer, TRUE);
488 g_free (mc_readdir_result);
491 /* --------------------------------------------------------------------------------------------- */
493 * These ones grab information from the VFS
494 * and handles them to an upper layer
497 void
498 vfs_fill_names (fill_names_f func)
500 guint i;
502 for (i = 0; i < vfs__classes_list->len; i++)
504 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
506 if (vfs->fill_names != NULL)
507 vfs->fill_names (vfs, func);
511 /* --------------------------------------------------------------------------------------------- */
512 gboolean
513 vfs_file_is_local (const vfs_path_t * vpath)
515 return (vfs_file_class_flags (vpath) & VFSF_LOCAL) != 0;
518 /* --------------------------------------------------------------------------------------------- */
520 void
521 vfs_print_message (const char *msg, ...)
523 ev_vfs_print_message_t event_data;
524 va_list ap;
526 va_start (ap, msg);
527 event_data.msg = g_strdup_vprintf (msg, ap);
528 va_end (ap);
530 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
533 /* --------------------------------------------------------------------------------------------- */
535 * If it's local, reread the current directory
536 * from the OS.
539 void
540 vfs_setup_cwd (void)
542 char *current_dir;
543 vfs_path_t *tmp_vpath;
544 const vfs_path_element_t *path_element;
546 if (vfs_get_raw_current_dir () == NULL)
548 current_dir = g_get_current_dir ();
549 vfs_set_raw_current_dir (vfs_path_from_str (current_dir));
550 g_free (current_dir);
552 current_dir = getenv ("PWD");
553 tmp_vpath = vfs_path_from_str (current_dir);
555 if (tmp_vpath != NULL)
557 struct stat my_stat, my_stat2;
559 if (mc_global.vfs.cd_symlinks
560 && mc_stat (tmp_vpath, &my_stat) == 0
561 && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
562 && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev)
563 vfs_set_raw_current_dir (tmp_vpath);
564 else
565 vfs_path_free (tmp_vpath);
569 path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
571 if ((path_element->class->flags & VFSF_LOCAL) != 0)
573 current_dir = g_get_current_dir ();
574 tmp_vpath = vfs_path_from_str (current_dir);
575 g_free (current_dir);
577 if (tmp_vpath != NULL)
578 { /* One of the directories in the path is not readable */
579 struct stat my_stat, my_stat2;
581 /* Check if it is O.K. to use the current_dir */
582 if (!(mc_global.vfs.cd_symlinks
583 && mc_stat (tmp_vpath, &my_stat) == 0
584 && mc_stat (vfs_get_raw_current_dir (), &my_stat2) == 0
585 && my_stat.st_ino == my_stat2.st_ino && my_stat.st_dev == my_stat2.st_dev))
586 vfs_set_raw_current_dir (tmp_vpath);
587 else
588 vfs_path_free (tmp_vpath);
593 /* --------------------------------------------------------------------------------------------- */
595 * Return current directory. If it's local, reread the current directory
596 * from the OS.
599 char *
600 _vfs_get_cwd (void)
602 const vfs_path_t *current_dir_vpath;
604 vfs_setup_cwd ();
605 current_dir_vpath = vfs_get_raw_current_dir ();
606 return g_strdup (vfs_path_as_str (current_dir_vpath));
609 /* --------------------------------------------------------------------------------------------- */
611 * Preallocate space for file in new place for ensure that file
612 * will be fully copied with less fragmentation.
614 * @param dest_vfs_fd mc VFS file handler
615 * @param src_fsize source file size
616 * @param dest_fsize destination file size (if destination exists, otherwise should be 0)
618 * @return 0 if success and non-zero otherwise.
619 * Note: function doesn't touch errno global variable.
623 vfs_preallocate (int dest_vfs_fd, off_t src_fsize, off_t dest_fsize)
625 #ifndef HAVE_POSIX_FALLOCATE
626 (void) dest_vfs_fd;
627 (void) src_fsize;
628 (void) dest_fsize;
629 return 0;
631 #else /* HAVE_POSIX_FALLOCATE */
632 void *dest_fd = NULL;
633 struct vfs_class *dest_class;
635 if (!mc_global.vfs.preallocate_space)
636 return 0;
638 if (src_fsize == 0)
639 return 0;
641 dest_class = vfs_class_find_by_handle (dest_vfs_fd, &dest_fd);
642 if ((dest_class->flags & VFSF_LOCAL) == 0 || dest_fd == NULL)
643 return 0;
645 return posix_fallocate (*(int *) dest_fd, dest_fsize, src_fsize - dest_fsize);
647 #endif /* HAVE_POSIX_FALLOCATE */
650 /* --------------------------------------------------------------------------------------------- */