(vfs_path_deserialize): refactoring.
[midnight-commander.git] / lib / vfs / path.c
blob04d00b629d5528eb0e54164f1d315e2617d84f9e
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9 Andrew Borodin <aborodin@vmail.ru>, 2013
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /**
28 * \file
29 * \brief Source: Virtual File System: path handlers
30 * \author Slava Zanko
31 * \date 2011
35 #include <config.h>
37 #include "lib/global.h"
38 #include "lib/strutil.h"
39 #include "lib/util.h" /* mc_build_filename() */
40 #include "lib/serialize.h"
42 #include "vfs.h"
43 #include "utilvfs.h"
44 #include "xdirentry.h"
45 #include "path.h"
47 extern GPtrArray *vfs__classes_list;
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 static gboolean
61 path_magic (const char *path)
63 struct stat buf;
65 return (stat (path, &buf) != 0);
68 /* --------------------------------------------------------------------------------------------- */
70 /**
71 * Splits path extracting vfs part.
73 * Splits path
74 * \verbatim /p1#op/inpath \endverbatim
75 * into
76 * \verbatim inpath,op; \endverbatim
77 * returns which vfs it is.
78 * What is left in path is p1. You still want to g_free(path), you DON'T
79 * want to free neither *inpath nor *op
82 static struct vfs_class *
83 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
84 size_t skip_count)
86 char *semi;
87 char *slash;
88 struct vfs_class *ret;
90 if (path == NULL)
91 vfs_die ("Cannot split NULL");
93 semi = strrstr_skip_count (path, "#", skip_count);
95 if ((semi == NULL) || (!path_magic (path)))
96 return NULL;
98 slash = strchr (semi, PATH_SEP);
99 *semi = '\0';
101 if (op != NULL)
102 *op = NULL;
104 if (inpath != NULL)
105 *inpath = NULL;
107 if (slash != NULL)
108 *slash = '\0';
110 ret = vfs_prefix_to_class (semi + 1);
111 if (ret != NULL)
113 if (op != NULL)
114 *op = semi + 1;
115 if (inpath != NULL)
116 *inpath = slash != NULL ? slash + 1 : NULL;
117 return ret;
120 if (slash != NULL)
121 *slash = PATH_SEP;
123 *semi = '#';
124 ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
125 return ret;
128 /* --------------------------------------------------------------------------------------------- */
130 * remove //, /./ and /../
132 * @return newly allocated string
135 static char *
136 vfs_canon (const char *path)
138 char *result;
140 if (path == NULL)
141 vfs_die ("Cannot canonicalize NULL");
143 if (!IS_PATH_SEP (*path))
145 /* Relative to current directory */
147 char *local;
149 if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
152 encoding prefix placed at start of string without the leading slash
153 should be autofixed by adding the leading slash
155 local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
157 else
159 const char *curr_dir;
161 curr_dir = vfs_get_current_dir ();
162 local = mc_build_filename (curr_dir, path, (char *) NULL);
164 result = vfs_canon (local);
165 g_free (local);
167 else
169 /* Absolute path */
171 result = g_strdup (path);
172 canonicalize_pathname (result);
175 return result;
178 /* --------------------------------------------------------------------------------------------- */
180 #ifdef HAVE_CHARSET
181 /** get encoding after last #enc: or NULL, if part does not contain #enc:
183 * @param path null-terminated string
184 * @param len the maximum length of path, where #enc: should be searched
186 * @return newly allocated string.
189 static char *
190 vfs_get_encoding (const char *path, ssize_t len)
192 char *semi;
194 /* try found #enc: */
195 semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
196 if (semi == NULL)
197 return NULL;
199 if (semi == path || IS_PATH_SEP (semi[-1]))
201 char *slash;
203 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
204 slash = strchr (semi, PATH_SEP);
205 if (slash != NULL)
206 return g_strndup (semi, slash - semi);
207 return g_strdup (semi);
210 return vfs_get_encoding (path, semi - path);
212 #endif
214 /* --------------------------------------------------------------------------------------------- */
215 /** Extract the hostname and username from the path
217 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
219 * ftp://sunsite.unc.edu/pub/linux
220 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
221 * ftp://tsx-11.mit.edu:8192/
222 * ftp://joe@foo.edu:11321/private
223 * ftp://joe:password@foo.se
225 * @param path_element is an input string to be parsed
226 * @param path is an input string to be parsed
228 * @return g_malloc()ed url info.
229 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
230 * is not set, then the current login name is supplied.
231 * Return value is a g_malloc()ed structure with the pathname relative to the
232 * host.
235 static void
236 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
238 char *pcopy;
239 const char *pend;
240 char *colon, *at, *rest;
242 path_element->port = 0;
244 pcopy = g_strdup (path);
245 pend = pcopy + strlen (pcopy);
247 /* search for any possible user */
248 at = strrchr (pcopy, '@');
250 /* We have a username */
251 if (at == NULL)
252 rest = pcopy;
253 else
255 char *inner_colon;
257 *at = '\0';
258 inner_colon = strchr (pcopy, ':');
259 if (inner_colon != NULL)
261 *inner_colon = '\0';
262 inner_colon++;
263 path_element->password = g_strdup (inner_colon);
266 if (*pcopy != '\0')
267 path_element->user = g_strdup (pcopy);
269 if (pend == at + 1)
270 rest = at;
271 else
272 rest = at + 1;
275 /* Check if the host comes with a port spec, if so, chop it */
276 if (*rest != '[')
277 colon = strchr (rest, ':');
278 else
280 colon = strchr (++rest, ']');
281 if (colon != NULL)
283 colon[0] = '\0';
284 colon[1] = '\0';
285 colon++;
286 path_element->ipv6 = TRUE;
290 if (colon != NULL)
292 *colon = '\0';
293 /* cppcheck-suppress invalidscanf */
294 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
296 if (path_element->port <= 0 || path_element->port >= 65536)
297 path_element->port = 0;
299 else
300 while (*(++colon) != '\0')
302 switch (*colon)
304 case 'C':
305 path_element->port = 1;
306 break;
307 case 'r':
308 path_element->port = 2;
309 break;
310 default:
311 break;
315 path_element->host = g_strdup (rest);
316 g_free (pcopy);
319 /* --------------------------------------------------------------------------------------------- */
321 * get VFS class for the given name
323 * @param class_name name of class
325 * @return pointer to class structure or NULL if class not found
328 static struct vfs_class *
329 vfs_get_class_by_name (const char *class_name)
331 guint i;
333 if (class_name == NULL)
334 return NULL;
336 for (i = 0; i < vfs__classes_list->len; i++)
338 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
339 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
340 return vfs;
343 return NULL;
346 /* --------------------------------------------------------------------------------------------- */
348 * Check if path string contain URL-like elements
350 * @param path_str path
352 * @return TRUE if path is deprecated or FALSE otherwise
355 static gboolean
356 vfs_path_is_str_path_deprecated (const char *path_str)
358 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
361 /* --------------------------------------------------------------------------------------------- */
362 /** Split path string to path elements by deprecated algorithm.
364 * @param path_str VFS-path
366 * @return pointer to newly created vfs_path_t object with filled path elements array.
369 static vfs_path_t *
370 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
372 vfs_path_t *vpath;
373 vfs_path_element_t *element;
374 struct vfs_class *class;
375 const char *local, *op;
377 (void) flags;
378 vpath = vfs_path_new ();
380 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
382 char *url_params;
383 element = g_new0 (vfs_path_element_t, 1);
384 element->class = class;
385 if (local == NULL)
386 local = "";
387 element->path = vfs_translate_path_n (local);
389 #ifdef HAVE_CHARSET
390 element->encoding = vfs_get_encoding (local, -1);
391 element->dir.converter =
392 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
393 #endif
395 url_params = strchr (op, ':'); /* skip VFS prefix */
396 if (url_params != NULL)
398 *url_params = '\0';
399 url_params++;
400 vfs_path_url_split (element, url_params);
403 if (*op != '\0')
404 element->vfs_prefix = g_strdup (op);
406 g_array_prepend_val (vpath->path, element);
408 if (path[0] != '\0')
410 element = g_new0 (vfs_path_element_t, 1);
411 element->class = g_ptr_array_index (vfs__classes_list, 0);
412 element->path = vfs_translate_path_n (path);
414 #ifdef HAVE_CHARSET
415 element->encoding = vfs_get_encoding (path, -1);
416 element->dir.converter =
417 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
418 #endif
419 g_array_prepend_val (vpath->path, element);
422 return vpath;
425 /* --------------------------------------------------------------------------------------------- */
426 /** Split path string to path elements by URL algorithm.
428 * @param path_str VFS-path
429 * @param flags flags for converter
431 * @return pointer to newly created vfs_path_t object with filled path elements array.
434 static vfs_path_t *
435 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
437 vfs_path_t *vpath;
438 vfs_path_element_t *element;
440 char *url_delimiter;
442 vpath = vfs_path_new ();
443 vpath->relative = (flags & VPF_NO_CANON) != 0;
445 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
447 char *vfs_prefix_start;
448 char *real_vfs_prefix_start = url_delimiter;
449 struct vfs_s_subclass *sub = NULL;
451 while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
452 real_vfs_prefix_start--;
453 vfs_prefix_start = real_vfs_prefix_start;
455 if (IS_PATH_SEP (*vfs_prefix_start))
456 vfs_prefix_start += 1;
458 *url_delimiter = '\0';
460 element = g_new0 (vfs_path_element_t, 1);
461 element->class = vfs_prefix_to_class (vfs_prefix_start);
462 element->vfs_prefix = g_strdup (vfs_prefix_start);
464 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
465 sub = VFSDATA (element);
466 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
468 char *slash_pointer;
470 slash_pointer = strchr (url_delimiter, PATH_SEP);
471 if (slash_pointer == NULL)
473 element->path = g_strdup ("");
475 else
477 element->path = vfs_translate_path_n (slash_pointer + 1);
478 #ifdef HAVE_CHARSET
479 element->encoding = vfs_get_encoding (slash_pointer, -1);
480 #endif
481 *slash_pointer = '\0';
483 vfs_path_url_split (element, url_delimiter);
485 else
487 element->path = vfs_translate_path_n (url_delimiter);
488 #ifdef HAVE_CHARSET
489 element->encoding = vfs_get_encoding (url_delimiter, -1);
490 #endif
492 #ifdef HAVE_CHARSET
493 element->dir.converter =
494 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
495 #endif
496 g_array_prepend_val (vpath->path, element);
498 if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
499 (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
500 *real_vfs_prefix_start = '\0';
501 else
502 *(real_vfs_prefix_start + 1) = '\0';
505 if (path[0] != '\0')
507 element = g_new0 (vfs_path_element_t, 1);
508 element->class = g_ptr_array_index (vfs__classes_list, 0);
509 element->path = vfs_translate_path_n (path);
510 #ifdef HAVE_CHARSET
511 element->encoding = vfs_get_encoding (path, -1);
512 element->dir.converter =
513 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
514 #endif
515 g_array_prepend_val (vpath->path, element);
518 return vpath;
521 /* --------------------------------------------------------------------------------------------- */
523 * Add element's class info to result string (such as VFS name, host, encoding etc)
524 * This function used as helper only in vfs_path_tokens_get() function
526 * @param element current path element
527 * @param ret_tokens total tikens for return
528 * @param element_tokens accumulated element-only tokens
531 static void
532 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
533 GString * element_tokens)
535 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
536 && element_tokens->len > 0)
538 char *url_str;
540 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
541 g_string_append_c (ret_tokens, PATH_SEP);
543 g_string_append (ret_tokens, element->vfs_prefix);
544 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
546 url_str = vfs_path_build_url_params_str (element, TRUE);
547 if (*url_str != '\0')
549 g_string_append (ret_tokens, url_str);
550 g_string_append_c (ret_tokens, PATH_SEP);
553 g_free (url_str);
556 #ifdef HAVE_CHARSET
557 if (element->encoding != NULL)
559 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
560 g_string_append (ret_tokens, PATH_SEP_STR);
561 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
562 g_string_append (ret_tokens, element->encoding);
563 g_string_append (ret_tokens, PATH_SEP_STR);
565 #endif
567 g_string_append (ret_tokens, element_tokens->str);
570 /* --------------------------------------------------------------------------------------------- */
572 * Strip path to home dir.
573 * @param dir pointer to string contains full path
576 static char *
577 vfs_path_strip_home (const char *dir)
579 const char *home_dir = mc_config_get_home_dir ();
581 if (home_dir != NULL)
583 size_t len;
585 len = strlen (home_dir);
587 if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
588 return g_strdup_printf ("~%s", dir + len);
591 return g_strdup (dir);
594 /* --------------------------------------------------------------------------------------------- */
595 /*** public functions ****************************************************************************/
596 /* --------------------------------------------------------------------------------------------- */
598 #define vfs_append_from_path(appendfrom, is_relative) \
600 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
602 char *stripped_home_str; \
603 stripped_home_str = vfs_path_strip_home (appendfrom); \
604 g_string_append (buffer, stripped_home_str); \
605 g_free (stripped_home_str); \
607 else \
609 if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
610 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
611 g_string_append_c (buffer, PATH_SEP); \
612 g_string_append (buffer, appendfrom); \
617 * Convert first elements_count elements from vfs_path_t to string representation with flags.
619 * @param vpath pointer to vfs_path_t object
620 * @param elements_count count of first elements for convert
621 * @param flags for converter
623 * @return pointer to newly created string.
626 char *
627 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
629 int element_index;
630 GString *buffer;
631 GString *recode_buffer;
633 if (vpath == NULL)
634 return NULL;
636 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
637 elements_count = vfs_path_elements_count (vpath);
639 if (elements_count < 0)
640 elements_count = vfs_path_elements_count (vpath) + elements_count;
642 buffer = g_string_new ("");
643 recode_buffer = g_string_new ("");
645 for (element_index = 0; element_index < elements_count; element_index++)
647 const vfs_path_element_t *element;
648 gboolean is_relative = vpath->relative && (element_index == 0);
650 element = vfs_path_get_by_index (vpath, element_index);
651 if (element->vfs_prefix != NULL)
653 char *url_str;
654 if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
655 g_string_append_c (buffer, PATH_SEP);
657 g_string_append (buffer, element->vfs_prefix);
658 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
660 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
662 if (*url_str != '\0')
664 g_string_append (buffer, url_str);
665 g_string_append_c (buffer, PATH_SEP);
668 g_free (url_str);
671 #ifdef HAVE_CHARSET
672 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
674 if ((flags & VPF_HIDE_CHARSET) == 0)
676 if ((!is_relative)
677 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
678 g_string_append (buffer, PATH_SEP_STR);
679 g_string_append (buffer, VFS_ENCODING_PREFIX);
680 g_string_append (buffer, element->encoding);
682 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
683 vfs_append_from_path (recode_buffer->str, is_relative);
684 g_string_set_size (recode_buffer, 0);
686 else
687 #endif
689 vfs_append_from_path (element->path, is_relative);
692 g_string_free (recode_buffer, TRUE);
693 return g_string_free (buffer, FALSE);
696 #undef vfs_append_from_path
698 /* --------------------------------------------------------------------------------------------- */
700 * Convert first elements_count elements from vfs_path_t to string representation.
702 * @param vpath pointer to vfs_path_t object
703 * @param elements_count count of first elements for convert
705 * @return pointer to newly created string.
708 char *
709 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
711 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
714 /* --------------------------------------------------------------------------------------------- */
716 * Split path string to path elements with flags for change parce process.
718 * @param path_str VFS-path
719 * @param flags flags for parser
721 * @return pointer to newly created vfs_path_t object with filled path elements array.
724 vfs_path_t *
725 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
727 vfs_path_t *vpath;
728 char *path;
730 if (path_str == NULL)
731 return NULL;
733 if ((flags & VPF_NO_CANON) == 0)
734 path = vfs_canon (path_str);
735 else
736 path = g_strdup (path_str);
738 if (path == NULL)
739 return NULL;
741 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
742 vpath = vfs_path_from_str_deprecated_parser (path, flags);
743 else
744 vpath = vfs_path_from_str_uri_parser (path, flags);
746 vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
747 g_free (path);
749 return vpath;
752 /* --------------------------------------------------------------------------------------------- */
754 * Split path string to path elements.
756 * @param path_str VFS-path
758 * @return pointer to newly created vfs_path_t object with filled path elements array.
761 vfs_path_t *
762 vfs_path_from_str (const char *path_str)
764 return vfs_path_from_str_flags (path_str, VPF_NONE);
767 /* --------------------------------------------------------------------------------------------- */
769 * Create new vfs_path_t object.
771 * @return pointer to newly created vfs_path_t object.
774 vfs_path_t *
775 vfs_path_new (void)
777 vfs_path_t *vpath;
779 vpath = g_new0 (vfs_path_t, 1);
780 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
782 return vpath;
785 /* --------------------------------------------------------------------------------------------- */
787 * Get count of path elements.
789 * @param vpath pointer to vfs_path_t object
791 * @return count of path elements.
795 vfs_path_elements_count (const vfs_path_t * vpath)
797 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
800 /* --------------------------------------------------------------------------------------------- */
802 * Add vfs_path_element_t object to end of list in vfs_path_t object
803 * @param vpath pointer to vfs_path_t object
804 * @param path_element pointer to vfs_path_element_t object
807 void
808 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
810 g_array_append_val (vpath->path, path_element);
811 g_free (vpath->str);
812 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
815 /* --------------------------------------------------------------------------------------------- */
817 * Get one path element by index.
819 * @param vpath pointer to vfs_path_t object
820 * @param element_index element index. May have negative value (in this case count was started at the end of list).
822 * @return path element.
825 const vfs_path_element_t *
826 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
828 if (vpath == NULL)
829 return NULL;
831 if (element_index < 0)
832 element_index += vfs_path_elements_count (vpath);
834 if (element_index < 0)
835 vfs_die ("vfs_path_get_by_index: incorrect index!");
837 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
840 /* --------------------------------------------------------------------------------------------- */
842 * Clone one path element
844 * @param element pointer to vfs_path_element_t object
846 * @return Newly allocated path element
849 vfs_path_element_t *
850 vfs_path_element_clone (const vfs_path_element_t * element)
852 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
854 new_element->user = g_strdup (element->user);
855 new_element->password = g_strdup (element->password);
856 new_element->host = g_strdup (element->host);
857 new_element->ipv6 = element->ipv6;
858 new_element->port = element->port;
859 new_element->path = g_strdup (element->path);
860 new_element->class = element->class;
861 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
862 #ifdef HAVE_CHARSET
863 new_element->encoding = g_strdup (element->encoding);
864 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
865 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
866 else
867 new_element->dir.converter = element->dir.converter;
868 #endif
869 new_element->dir.info = element->dir.info;
871 return new_element;
874 /* --------------------------------------------------------------------------------------------- */
876 * Free one path element.
878 * @param element pointer to vfs_path_element_t object
882 void
883 vfs_path_element_free (vfs_path_element_t * element)
885 if (element == NULL)
886 return;
888 g_free (element->user);
889 g_free (element->password);
890 g_free (element->host);
891 g_free (element->path);
892 g_free (element->vfs_prefix);
894 #ifdef HAVE_CHARSET
895 g_free (element->encoding);
897 if (vfs_path_element_need_cleanup_converter (element))
898 str_close_conv (element->dir.converter);
899 #endif
901 g_free (element);
904 /* --------------------------------------------------------------------------------------------- */
906 * Clone path
908 * @param vpath pointer to vfs_path_t object
910 * @return Newly allocated path object
913 vfs_path_t *
914 vfs_path_clone (const vfs_path_t * vpath)
916 vfs_path_t *new_vpath;
917 int vpath_element_index;
919 if (vpath == NULL)
920 return NULL;
922 new_vpath = vfs_path_new ();
923 new_vpath->relative = vpath->relative;
925 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
926 vpath_element_index++)
928 vfs_path_element_t *path_element;
930 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
931 g_array_append_val (new_vpath->path, path_element);
933 new_vpath->str = g_strdup (vpath->str);
935 return new_vpath;
938 /* --------------------------------------------------------------------------------------------- */
940 * Free vfs_path_t object.
942 * @param vpath pointer to vfs_path_t object
946 void
947 vfs_path_free (vfs_path_t * vpath)
949 int vpath_element_index;
951 if (vpath == NULL)
952 return;
954 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
955 vpath_element_index++)
957 vfs_path_element_t *path_element;
959 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
960 vfs_path_element_free (path_element);
963 g_array_free (vpath->path, TRUE);
964 g_free (vpath->str);
965 g_free (vpath);
968 /* --------------------------------------------------------------------------------------------- */
970 * Remove one path element by index
972 * @param vpath pointer to vfs_path_t object
973 * @param element_index element index. May have negative value (in this case count was started at the end of list).
977 void
978 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
980 vfs_path_element_t *element;
982 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
983 return;
985 if (element_index < 0)
986 element_index = vfs_path_elements_count (vpath) + element_index;
988 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
989 vpath->path = g_array_remove_index (vpath->path, element_index);
990 vfs_path_element_free (element);
991 g_free (vpath->str);
992 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
995 /* --------------------------------------------------------------------------------------------- */
996 /** Return VFS class for the given prefix */
998 struct vfs_class *
999 vfs_prefix_to_class (const char *prefix)
1001 guint i;
1003 /* Avoid first class (localfs) that would accept any prefix */
1004 for (i = 1; i < vfs__classes_list->len; i++)
1006 struct vfs_class *vfs;
1008 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1009 if (vfs->which != NULL)
1011 if (vfs->which (vfs, prefix) == -1)
1012 continue;
1013 return vfs;
1016 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1017 return vfs;
1020 return NULL;
1023 /* --------------------------------------------------------------------------------------------- */
1025 #ifdef HAVE_CHARSET
1028 * Check if need cleanup charset converter for vfs_path_element_t
1030 * @param element part of path
1032 * @return TRUE if need cleanup converter or FALSE otherwise
1035 gboolean
1036 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1038 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1041 /* --------------------------------------------------------------------------------------------- */
1043 * Change encoding for last part (vfs_path_element_t) of vpath
1045 * @param vpath pointer to path structure
1046 * encoding name of charset
1048 * @return pointer to path structure (for use function in anoter functions)
1050 vfs_path_t *
1051 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
1053 vfs_path_element_t *path_element;
1055 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1056 /* don't add current encoding */
1057 if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1058 return vpath;
1060 g_free (path_element->encoding);
1061 path_element->encoding = g_strdup (encoding);
1063 if (vfs_path_element_need_cleanup_converter (path_element))
1064 str_close_conv (path_element->dir.converter);
1066 path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1068 g_free (vpath->str);
1069 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1070 return vpath;
1073 #endif
1075 /* --------------------------------------------------------------------------------------------- */
1078 * Serialize vfs_path_t object to string
1080 * @param vpath data for serialization
1081 * @param error contain pointer to object for handle error code and message
1083 * @return serialized vpath as newly allocated string
1086 char *
1087 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
1089 mc_config_t *cpath;
1090 ssize_t element_index;
1091 char *ret_value;
1093 mc_return_val_if_error (mcerror, FALSE);
1095 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1097 mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1098 return NULL;
1101 cpath = mc_config_init (NULL, FALSE);
1103 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1105 char groupname[BUF_TINY];
1106 const vfs_path_element_t *element;
1108 g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1109 element = vfs_path_get_by_index (vpath, element_index);
1110 /* convert one element to config group */
1112 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1113 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1114 #ifdef HAVE_CHARSET
1115 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1116 #endif
1117 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1119 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1120 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1121 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1122 if (element->port != 0)
1123 mc_config_set_int (cpath, groupname, "port", element->port);
1126 ret_value = mc_serialize_config (cpath, mcerror);
1127 mc_config_deinit (cpath);
1128 return ret_value;
1131 /* --------------------------------------------------------------------------------------------- */
1133 * Deserialize string to vfs_path_t object
1135 * @param data data for serialization
1136 * @param error contain pointer to object for handle error code and message
1138 * @return newly allocated vfs_path_t object
1141 vfs_path_t *
1142 vfs_path_deserialize (const char *data, GError ** mcerror)
1144 mc_config_t *cpath;
1145 size_t element_index;
1146 vfs_path_t *vpath;
1148 mc_return_val_if_error (mcerror, FALSE);
1150 cpath = mc_deserialize_config (data, mcerror);
1151 if (cpath == NULL)
1152 return NULL;
1154 vpath = vfs_path_new ();
1156 for (element_index = 0;; element_index++)
1158 struct vfs_class *eclass;
1159 vfs_path_element_t *element;
1160 char *cfg_value;
1161 char groupname[BUF_TINY];
1163 g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1164 if (!mc_config_has_group (cpath, groupname))
1165 break;
1167 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1168 eclass = vfs_get_class_by_name (cfg_value);
1169 if (eclass == NULL)
1171 vfs_path_free (vpath);
1172 g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1173 g_free (cfg_value);
1174 mc_config_deinit (cpath);
1175 return NULL;
1177 g_free (cfg_value);
1179 element = g_new0 (vfs_path_element_t, 1);
1180 element->class = eclass;
1181 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1183 #ifdef HAVE_CHARSET
1184 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1185 element->dir.converter =
1186 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1187 #endif
1189 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1191 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1192 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1193 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1194 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1196 vpath->path = g_array_append_val (vpath->path, element);
1199 mc_config_deinit (cpath);
1200 if (vfs_path_elements_count (vpath) == 0)
1202 vfs_path_free (vpath);
1203 g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1204 return NULL;
1206 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1208 return vpath;
1211 /* --------------------------------------------------------------------------------------------- */
1213 * Build vfs_path_t object from arguments.
1215 * @param first_element of path
1216 * @param ... path tokens, terminated by NULL
1218 * @return newly allocated vfs_path_t object
1221 vfs_path_t *
1222 vfs_path_build_filename (const char *first_element, ...)
1224 va_list args;
1225 char *str_path;
1226 vfs_path_t *vpath;
1228 if (first_element == NULL)
1229 return NULL;
1231 va_start (args, first_element);
1232 str_path = mc_build_filenamev (first_element, args);
1233 va_end (args);
1234 vpath = vfs_path_from_str (str_path);
1235 g_free (str_path);
1236 return vpath;
1239 /* --------------------------------------------------------------------------------------------- */
1241 * Append tokens to path object
1243 * @param vpath path object
1244 * @param first_element of path
1245 * @param ... NULL-terminated strings
1247 * @return newly allocated path object
1250 vfs_path_t *
1251 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1253 va_list args;
1254 char *str_path;
1255 const char *result_str;
1256 vfs_path_t *ret_vpath;
1258 if (vpath == NULL || first_element == NULL)
1259 return NULL;
1261 va_start (args, first_element);
1262 str_path = mc_build_filenamev (first_element, args);
1263 va_end (args);
1265 result_str = vfs_path_as_str (vpath);
1266 ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1267 g_free (str_path);
1269 return ret_vpath;
1273 /* --------------------------------------------------------------------------------------------- */
1276 * Append vpath_t tokens to path object
1278 * @param first_vpath vpath objects
1279 * @param ... NULL-terminated vpath objects
1281 * @return newly allocated path object
1284 vfs_path_t *
1285 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1287 va_list args;
1288 vfs_path_t *ret_vpath;
1289 const vfs_path_t *current_vpath = first_vpath;
1291 if (first_vpath == NULL)
1292 return NULL;
1294 ret_vpath = vfs_path_new ();
1296 va_start (args, first_vpath);
1299 int vindex;
1301 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1303 vfs_path_element_t *path_element;
1305 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1306 g_array_append_val (ret_vpath->path, path_element);
1308 current_vpath = va_arg (args, const vfs_path_t *);
1310 while (current_vpath != NULL);
1311 va_end (args);
1313 ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1315 return ret_vpath;
1318 /* --------------------------------------------------------------------------------------------- */
1321 * get tockens count in path.
1323 * @param vpath path object
1325 * @return count of tokens
1328 size_t
1329 vfs_path_tokens_count (const vfs_path_t * vpath)
1331 size_t count_tokens = 0;
1332 int element_index;
1334 if (vpath == NULL)
1335 return 0;
1337 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1339 const vfs_path_element_t *element;
1340 const char *token, *prev_token;
1342 element = vfs_path_get_by_index (vpath, element_index);
1344 for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1345 prev_token = token + 1)
1347 /* skip empty substring */
1348 if (token != prev_token)
1349 count_tokens++;
1352 if (*prev_token != '\0')
1353 count_tokens++;
1356 return count_tokens;
1359 /* --------------------------------------------------------------------------------------------- */
1362 * Get subpath by tokens
1364 * @param vpath path object
1365 * @param start_position first token for got/ Started from 0.
1366 * If negative, then position will be relative to end of path
1367 * @param length count of tokens
1369 * @return newly allocated string with path tokens separated by slash
1372 char *
1373 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1375 GString *ret_tokens, *element_tokens;
1376 int element_index;
1377 size_t tokens_count = vfs_path_tokens_count (vpath);
1379 if (vpath == NULL)
1380 return NULL;
1382 if (length == 0)
1383 length = tokens_count;
1385 if (length < 0)
1386 length = tokens_count + length;
1388 if (start_position < 0)
1389 start_position = (ssize_t) tokens_count + start_position;
1391 if (start_position < 0)
1392 return NULL;
1394 if (start_position >= (ssize_t) tokens_count)
1395 return NULL;
1397 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1398 length = tokens_count - start_position;
1400 ret_tokens = g_string_sized_new (32);
1401 element_tokens = g_string_sized_new (32);
1403 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1405 const vfs_path_element_t *element;
1406 char **path_tokens, **iterator;
1408 g_string_assign (element_tokens, "");
1409 element = vfs_path_get_by_index (vpath, element_index);
1410 path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1412 for (iterator = path_tokens; *iterator != NULL; iterator++)
1414 if (**iterator != '\0')
1416 if (start_position == 0)
1418 if (length == 0)
1420 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1421 g_string_free (element_tokens, TRUE);
1422 g_strfreev (path_tokens);
1423 return g_string_free (ret_tokens, FALSE);
1425 length--;
1426 if (element_tokens->len != 0)
1427 g_string_append_c (element_tokens, PATH_SEP);
1428 g_string_append (element_tokens, *iterator);
1430 else
1431 start_position--;
1434 g_strfreev (path_tokens);
1435 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1438 g_string_free (element_tokens, TRUE);
1439 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1442 /* --------------------------------------------------------------------------------------------- */
1444 * Get subpath by tokens
1446 * @param vpath path object
1447 * @param start_position first token for got/ Started from 0.
1448 * If negative, then position will be relative to end of path
1449 * @param length count of tokens
1451 * @return newly allocated path object with path tokens separated by slash
1454 vfs_path_t *
1455 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1457 char *str_tokens;
1458 vfs_path_t *ret_vpath = NULL;
1460 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1461 if (str_tokens != NULL)
1463 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1464 g_free (str_tokens);
1466 return ret_vpath;
1469 /* --------------------------------------------------------------------------------------------- */
1472 * Build URL parameters (such as user:pass @ host:port) from one path element object
1474 * @param element path element
1475 * @param keep_password TRUE or FALSE
1477 * @return newly allocated string
1480 char *
1481 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1483 GString *buffer;
1485 if (element == NULL)
1486 return NULL;
1488 buffer = g_string_new ("");
1490 if (element->user != NULL)
1491 g_string_append (buffer, element->user);
1493 if (element->password != NULL && keep_password)
1495 g_string_append_c (buffer, ':');
1496 g_string_append (buffer, element->password);
1499 if (element->host != NULL)
1501 if ((element->user != NULL) || (element->password != NULL))
1502 g_string_append_c (buffer, '@');
1503 if (element->ipv6)
1504 g_string_append_c (buffer, '[');
1505 g_string_append (buffer, element->host);
1506 if (element->ipv6)
1507 g_string_append_c (buffer, ']');
1510 if ((element->port) != 0 && (element->host != NULL))
1512 g_string_append_c (buffer, ':');
1513 g_string_append_printf (buffer, "%d", element->port);
1516 return g_string_free (buffer, FALSE);
1519 /* --------------------------------------------------------------------------------------------- */
1521 * Build pretty string representation of one path_element_t object
1523 * @param element path element
1525 * @return newly allocated string
1528 char *
1529 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1531 char *url_params;
1532 GString *pretty_path;
1534 pretty_path = g_string_new (element->class->prefix);
1535 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1537 url_params = vfs_path_build_url_params_str (element, FALSE);
1538 g_string_append (pretty_path, url_params);
1539 g_free (url_params);
1541 if (!IS_PATH_SEP (*element->path))
1542 g_string_append_c (pretty_path, PATH_SEP);
1544 g_string_append (pretty_path, element->path);
1545 return g_string_free (pretty_path, FALSE);
1548 /* --------------------------------------------------------------------------------------------- */
1550 * Compare two path objects as strings
1552 * @param vpath1 first path object
1553 * @param vpath2 second vpath object
1555 * @return integer value like to strcmp.
1558 gboolean
1559 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1561 const char *path1, *path2;
1562 gboolean ret_val;
1564 if (vpath1 == NULL || vpath2 == NULL)
1565 return FALSE;
1567 path1 = vfs_path_as_str (vpath1);
1568 path2 = vfs_path_as_str (vpath2);
1570 ret_val = strcmp (path1, path2) == 0;
1572 return ret_val;
1575 /* --------------------------------------------------------------------------------------------- */
1577 * Compare two path objects as strings
1579 * @param vpath1 first path object
1580 * @param vpath2 second vpath object
1581 * @param len number of first 'len' characters
1583 * @return integer value like to strcmp.
1586 gboolean
1587 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1589 const char *path1, *path2;
1590 gboolean ret_val;
1592 if (vpath1 == NULL || vpath2 == NULL)
1593 return FALSE;
1595 path1 = vfs_path_as_str (vpath1);
1596 path2 = vfs_path_as_str (vpath2);
1598 ret_val = strncmp (path1, path2, len) == 0;
1600 return ret_val;
1603 /* --------------------------------------------------------------------------------------------- */
1605 * Calculate path length in string representation
1607 * @param vpath path object
1609 * @return length of path
1612 size_t
1613 vfs_path_len (const vfs_path_t * vpath)
1615 if (vpath == NULL)
1616 return 0;
1618 return strlen (vpath->str);
1621 /* --------------------------------------------------------------------------------------------- */
1623 * Convert relative vpath object to absolute
1625 * @param vpath path object
1627 * @return absolute path object
1630 vfs_path_t *
1631 vfs_path_to_absolute (const vfs_path_t * vpath)
1633 vfs_path_t *absolute_vpath;
1634 const char *path_str;
1636 if (!vpath->relative)
1637 return vfs_path_clone (vpath);
1639 path_str = vfs_path_as_str (vpath);
1640 absolute_vpath = vfs_path_from_str (path_str);
1641 return absolute_vpath;
1644 /* --------------------------------------------------------------------------------------------- */