(vfs_path_from_str_uri_parser): fix detection of relative path.
[midnight-commander.git] / lib / vfs / path.c
blob0c552b9347fc96487826d167997c0e1c7443176a
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011-2017
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;
439 char *url_delimiter;
441 (void) flags;
443 vpath = vfs_path_new ();
444 vpath->relative = path != NULL && !IS_PATH_SEP (*path);
446 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
448 char *vfs_prefix_start;
449 char *real_vfs_prefix_start = url_delimiter;
450 struct vfs_s_subclass *sub = NULL;
452 while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
453 real_vfs_prefix_start--;
454 vfs_prefix_start = real_vfs_prefix_start;
456 if (IS_PATH_SEP (*vfs_prefix_start))
457 vfs_prefix_start += 1;
459 *url_delimiter = '\0';
461 element = g_new0 (vfs_path_element_t, 1);
462 element->class = vfs_prefix_to_class (vfs_prefix_start);
463 element->vfs_prefix = g_strdup (vfs_prefix_start);
465 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
466 sub = VFSDATA (element);
467 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
469 char *slash_pointer;
471 slash_pointer = strchr (url_delimiter, PATH_SEP);
472 if (slash_pointer == NULL)
474 element->path = g_strdup ("");
476 else
478 element->path = vfs_translate_path_n (slash_pointer + 1);
479 #ifdef HAVE_CHARSET
480 element->encoding = vfs_get_encoding (slash_pointer, -1);
481 #endif
482 *slash_pointer = '\0';
484 vfs_path_url_split (element, url_delimiter);
486 else
488 element->path = vfs_translate_path_n (url_delimiter);
489 #ifdef HAVE_CHARSET
490 element->encoding = vfs_get_encoding (url_delimiter, -1);
491 #endif
493 #ifdef HAVE_CHARSET
494 element->dir.converter =
495 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
496 #endif
497 g_array_prepend_val (vpath->path, element);
499 if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
500 (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
501 *real_vfs_prefix_start = '\0';
502 else
503 *(real_vfs_prefix_start + 1) = '\0';
506 if (path[0] != '\0')
508 element = g_new0 (vfs_path_element_t, 1);
509 element->class = g_ptr_array_index (vfs__classes_list, 0);
510 element->path = vfs_translate_path_n (path);
511 #ifdef HAVE_CHARSET
512 element->encoding = vfs_get_encoding (path, -1);
513 element->dir.converter =
514 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
515 #endif
516 g_array_prepend_val (vpath->path, element);
519 return vpath;
522 /* --------------------------------------------------------------------------------------------- */
524 * Add element's class info to result string (such as VFS name, host, encoding etc)
525 * This function used as helper only in vfs_path_tokens_get() function
527 * @param element current path element
528 * @param ret_tokens total tikens for return
529 * @param element_tokens accumulated element-only tokens
532 static void
533 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
534 GString * element_tokens)
536 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
537 && element_tokens->len > 0)
539 char *url_str;
541 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
542 g_string_append_c (ret_tokens, PATH_SEP);
544 g_string_append (ret_tokens, element->vfs_prefix);
545 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
547 url_str = vfs_path_build_url_params_str (element, TRUE);
548 if (*url_str != '\0')
550 g_string_append (ret_tokens, url_str);
551 g_string_append_c (ret_tokens, PATH_SEP);
554 g_free (url_str);
557 #ifdef HAVE_CHARSET
558 if (element->encoding != NULL)
560 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
561 g_string_append (ret_tokens, PATH_SEP_STR);
562 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
563 g_string_append (ret_tokens, element->encoding);
564 g_string_append (ret_tokens, PATH_SEP_STR);
566 #endif
568 g_string_append (ret_tokens, element_tokens->str);
571 /* --------------------------------------------------------------------------------------------- */
573 * Strip path to home dir.
574 * @param dir pointer to string contains full path
577 static char *
578 vfs_path_strip_home (const char *dir)
580 const char *home_dir = mc_config_get_home_dir ();
582 if (home_dir != NULL)
584 size_t len;
586 len = strlen (home_dir);
588 if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
589 return g_strdup_printf ("~%s", dir + len);
592 return g_strdup (dir);
595 /* --------------------------------------------------------------------------------------------- */
596 /*** public functions ****************************************************************************/
597 /* --------------------------------------------------------------------------------------------- */
599 #define vfs_append_from_path(appendfrom, is_relative) \
601 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
603 char *stripped_home_str; \
604 stripped_home_str = vfs_path_strip_home (appendfrom); \
605 g_string_append (buffer, stripped_home_str); \
606 g_free (stripped_home_str); \
608 else \
610 if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
611 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
612 g_string_append_c (buffer, PATH_SEP); \
613 g_string_append (buffer, appendfrom); \
618 * Convert first elements_count elements from vfs_path_t to string representation with flags.
620 * @param vpath pointer to vfs_path_t object
621 * @param elements_count count of first elements for convert
622 * @param flags for converter
624 * @return pointer to newly created string.
627 char *
628 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
630 int element_index;
631 GString *buffer;
632 GString *recode_buffer;
634 if (vpath == NULL)
635 return NULL;
637 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
638 elements_count = vfs_path_elements_count (vpath);
640 if (elements_count < 0)
641 elements_count = vfs_path_elements_count (vpath) + elements_count;
643 buffer = g_string_new ("");
644 recode_buffer = g_string_new ("");
646 for (element_index = 0; element_index < elements_count; element_index++)
648 const vfs_path_element_t *element;
649 gboolean is_relative = vpath->relative && (element_index == 0);
651 element = vfs_path_get_by_index (vpath, element_index);
652 if (element->vfs_prefix != NULL)
654 char *url_str;
655 if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
656 g_string_append_c (buffer, PATH_SEP);
658 g_string_append (buffer, element->vfs_prefix);
659 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
661 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
663 if (*url_str != '\0')
665 g_string_append (buffer, url_str);
666 g_string_append_c (buffer, PATH_SEP);
669 g_free (url_str);
672 #ifdef HAVE_CHARSET
673 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
675 if ((flags & VPF_HIDE_CHARSET) == 0)
677 if ((!is_relative)
678 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
679 g_string_append (buffer, PATH_SEP_STR);
680 g_string_append (buffer, VFS_ENCODING_PREFIX);
681 g_string_append (buffer, element->encoding);
683 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
684 vfs_append_from_path (recode_buffer->str, is_relative);
685 g_string_set_size (recode_buffer, 0);
687 else
688 #endif
690 vfs_append_from_path (element->path, is_relative);
693 g_string_free (recode_buffer, TRUE);
694 return g_string_free (buffer, FALSE);
697 #undef vfs_append_from_path
699 /* --------------------------------------------------------------------------------------------- */
701 * Convert first elements_count elements from vfs_path_t to string representation.
703 * @param vpath pointer to vfs_path_t object
704 * @param elements_count count of first elements for convert
706 * @return pointer to newly created string.
709 char *
710 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
712 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
715 /* --------------------------------------------------------------------------------------------- */
717 * Split path string to path elements with flags for change parce process.
719 * @param path_str VFS-path
720 * @param flags flags for parser
722 * @return pointer to newly created vfs_path_t object with filled path elements array.
725 vfs_path_t *
726 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
728 vfs_path_t *vpath;
729 char *path;
731 if (path_str == NULL)
732 return NULL;
734 if ((flags & VPF_NO_CANON) == 0)
735 path = vfs_canon (path_str);
736 else
737 path = g_strdup (path_str);
739 if (path == NULL)
740 return NULL;
742 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
743 vpath = vfs_path_from_str_deprecated_parser (path, flags);
744 else
745 vpath = vfs_path_from_str_uri_parser (path, flags);
747 vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
748 g_free (path);
750 return vpath;
753 /* --------------------------------------------------------------------------------------------- */
755 * Split path string to path elements.
757 * @param path_str VFS-path
759 * @return pointer to newly created vfs_path_t object with filled path elements array.
762 vfs_path_t *
763 vfs_path_from_str (const char *path_str)
765 return vfs_path_from_str_flags (path_str, VPF_NONE);
768 /* --------------------------------------------------------------------------------------------- */
770 * Create new vfs_path_t object.
772 * @return pointer to newly created vfs_path_t object.
775 vfs_path_t *
776 vfs_path_new (void)
778 vfs_path_t *vpath;
780 vpath = g_new0 (vfs_path_t, 1);
781 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
783 return vpath;
786 /* --------------------------------------------------------------------------------------------- */
788 * Get count of path elements.
790 * @param vpath pointer to vfs_path_t object
792 * @return count of path elements.
796 vfs_path_elements_count (const vfs_path_t * vpath)
798 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
801 /* --------------------------------------------------------------------------------------------- */
803 * Add vfs_path_element_t object to end of list in vfs_path_t object
804 * @param vpath pointer to vfs_path_t object
805 * @param path_element pointer to vfs_path_element_t object
808 void
809 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
811 g_array_append_val (vpath->path, path_element);
812 g_free (vpath->str);
813 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
816 /* --------------------------------------------------------------------------------------------- */
818 * Get one path element by index.
820 * @param vpath pointer to vfs_path_t object
821 * @param element_index element index. May have negative value (in this case count was started at the end of list).
823 * @return path element.
826 const vfs_path_element_t *
827 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
829 if (vpath == NULL)
830 return NULL;
832 if (element_index < 0)
833 element_index += vfs_path_elements_count (vpath);
835 if (element_index < 0)
836 vfs_die ("vfs_path_get_by_index: incorrect index!");
838 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
841 /* --------------------------------------------------------------------------------------------- */
843 * Clone one path element
845 * @param element pointer to vfs_path_element_t object
847 * @return Newly allocated path element
850 vfs_path_element_t *
851 vfs_path_element_clone (const vfs_path_element_t * element)
853 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
855 new_element->user = g_strdup (element->user);
856 new_element->password = g_strdup (element->password);
857 new_element->host = g_strdup (element->host);
858 new_element->ipv6 = element->ipv6;
859 new_element->port = element->port;
860 new_element->path = g_strdup (element->path);
861 new_element->class = element->class;
862 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
863 #ifdef HAVE_CHARSET
864 new_element->encoding = g_strdup (element->encoding);
865 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
866 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
867 else
868 new_element->dir.converter = element->dir.converter;
869 #endif
870 new_element->dir.info = element->dir.info;
872 return new_element;
875 /* --------------------------------------------------------------------------------------------- */
877 * Free one path element.
879 * @param element pointer to vfs_path_element_t object
883 void
884 vfs_path_element_free (vfs_path_element_t * element)
886 if (element == NULL)
887 return;
889 g_free (element->user);
890 g_free (element->password);
891 g_free (element->host);
892 g_free (element->path);
893 g_free (element->vfs_prefix);
895 #ifdef HAVE_CHARSET
896 g_free (element->encoding);
898 if (vfs_path_element_need_cleanup_converter (element))
899 str_close_conv (element->dir.converter);
900 #endif
902 g_free (element);
905 /* --------------------------------------------------------------------------------------------- */
907 * Clone path
909 * @param vpath pointer to vfs_path_t object
911 * @return Newly allocated path object
914 vfs_path_t *
915 vfs_path_clone (const vfs_path_t * vpath)
917 vfs_path_t *new_vpath;
918 int vpath_element_index;
920 if (vpath == NULL)
921 return NULL;
923 new_vpath = vfs_path_new ();
924 new_vpath->relative = vpath->relative;
926 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
927 vpath_element_index++)
929 vfs_path_element_t *path_element;
931 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
932 g_array_append_val (new_vpath->path, path_element);
934 new_vpath->str = g_strdup (vpath->str);
936 return new_vpath;
939 /* --------------------------------------------------------------------------------------------- */
941 * Free vfs_path_t object.
943 * @param vpath pointer to vfs_path_t object
947 void
948 vfs_path_free (vfs_path_t * vpath)
950 int vpath_element_index;
952 if (vpath == NULL)
953 return;
955 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
956 vpath_element_index++)
958 vfs_path_element_t *path_element;
960 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
961 vfs_path_element_free (path_element);
964 g_array_free (vpath->path, TRUE);
965 g_free (vpath->str);
966 g_free (vpath);
969 /* --------------------------------------------------------------------------------------------- */
971 * Remove one path element by index
973 * @param vpath pointer to vfs_path_t object
974 * @param element_index element index. May have negative value (in this case count was started at the end of list).
978 void
979 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
981 vfs_path_element_t *element;
983 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
984 return;
986 if (element_index < 0)
987 element_index = vfs_path_elements_count (vpath) + element_index;
989 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
990 vpath->path = g_array_remove_index (vpath->path, element_index);
991 vfs_path_element_free (element);
992 g_free (vpath->str);
993 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
996 /* --------------------------------------------------------------------------------------------- */
997 /** Return VFS class for the given prefix */
999 struct vfs_class *
1000 vfs_prefix_to_class (const char *prefix)
1002 guint i;
1004 /* Avoid first class (localfs) that would accept any prefix */
1005 for (i = 1; i < vfs__classes_list->len; i++)
1007 struct vfs_class *vfs;
1009 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1010 if (vfs->which != NULL)
1012 if (vfs->which (vfs, prefix) == -1)
1013 continue;
1014 return vfs;
1017 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1018 return vfs;
1021 return NULL;
1024 /* --------------------------------------------------------------------------------------------- */
1026 #ifdef HAVE_CHARSET
1029 * Check if need cleanup charset converter for vfs_path_element_t
1031 * @param element part of path
1033 * @return TRUE if need cleanup converter or FALSE otherwise
1036 gboolean
1037 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1039 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1042 /* --------------------------------------------------------------------------------------------- */
1044 * Change encoding for last part (vfs_path_element_t) of vpath
1046 * @param vpath pointer to path structure
1047 * encoding name of charset
1049 * @return pointer to path structure (for use function in anoter functions)
1051 vfs_path_t *
1052 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
1054 vfs_path_element_t *path_element;
1056 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1057 /* don't add current encoding */
1058 if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1059 return vpath;
1061 g_free (path_element->encoding);
1062 path_element->encoding = g_strdup (encoding);
1064 if (vfs_path_element_need_cleanup_converter (path_element))
1065 str_close_conv (path_element->dir.converter);
1067 path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1069 g_free (vpath->str);
1070 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1071 return vpath;
1074 #endif
1076 /* --------------------------------------------------------------------------------------------- */
1079 * Serialize vfs_path_t object to string
1081 * @param vpath data for serialization
1082 * @param error contain pointer to object for handle error code and message
1084 * @return serialized vpath as newly allocated string
1087 char *
1088 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
1090 mc_config_t *cpath;
1091 ssize_t element_index;
1092 char *ret_value;
1094 mc_return_val_if_error (mcerror, FALSE);
1096 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1098 mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1099 return NULL;
1102 cpath = mc_config_init (NULL, FALSE);
1104 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1106 char groupname[BUF_TINY];
1107 const vfs_path_element_t *element;
1109 g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1110 element = vfs_path_get_by_index (vpath, element_index);
1111 /* convert one element to config group */
1113 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1114 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1115 #ifdef HAVE_CHARSET
1116 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1117 #endif
1118 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1120 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1121 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1122 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1123 if (element->port != 0)
1124 mc_config_set_int (cpath, groupname, "port", element->port);
1127 ret_value = mc_serialize_config (cpath, mcerror);
1128 mc_config_deinit (cpath);
1129 return ret_value;
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Deserialize string to vfs_path_t object
1136 * @param data data for serialization
1137 * @param error contain pointer to object for handle error code and message
1139 * @return newly allocated vfs_path_t object
1142 vfs_path_t *
1143 vfs_path_deserialize (const char *data, GError ** mcerror)
1145 mc_config_t *cpath;
1146 size_t element_index;
1147 vfs_path_t *vpath;
1149 mc_return_val_if_error (mcerror, FALSE);
1151 cpath = mc_deserialize_config (data, mcerror);
1152 if (cpath == NULL)
1153 return NULL;
1155 vpath = vfs_path_new ();
1157 for (element_index = 0;; element_index++)
1159 struct vfs_class *eclass;
1160 vfs_path_element_t *element;
1161 char *cfg_value;
1162 char groupname[BUF_TINY];
1164 g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1165 if (!mc_config_has_group (cpath, groupname))
1166 break;
1168 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1169 eclass = vfs_get_class_by_name (cfg_value);
1170 if (eclass == NULL)
1172 vfs_path_free (vpath);
1173 g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1174 g_free (cfg_value);
1175 mc_config_deinit (cpath);
1176 return NULL;
1178 g_free (cfg_value);
1180 element = g_new0 (vfs_path_element_t, 1);
1181 element->class = eclass;
1182 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1184 #ifdef HAVE_CHARSET
1185 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1186 element->dir.converter =
1187 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1188 #endif
1190 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1192 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1193 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1194 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1195 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1197 vpath->path = g_array_append_val (vpath->path, element);
1200 mc_config_deinit (cpath);
1201 if (vfs_path_elements_count (vpath) == 0)
1203 vfs_path_free (vpath);
1204 g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1205 return NULL;
1207 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1209 return vpath;
1212 /* --------------------------------------------------------------------------------------------- */
1214 * Build vfs_path_t object from arguments.
1216 * @param first_element of path
1217 * @param ... path tokens, terminated by NULL
1219 * @return newly allocated vfs_path_t object
1222 vfs_path_t *
1223 vfs_path_build_filename (const char *first_element, ...)
1225 va_list args;
1226 char *str_path;
1227 vfs_path_t *vpath;
1229 if (first_element == NULL)
1230 return NULL;
1232 va_start (args, first_element);
1233 str_path = mc_build_filenamev (first_element, args);
1234 va_end (args);
1235 vpath = vfs_path_from_str (str_path);
1236 g_free (str_path);
1237 return vpath;
1240 /* --------------------------------------------------------------------------------------------- */
1242 * Append tokens to path object
1244 * @param vpath path object
1245 * @param first_element of path
1246 * @param ... NULL-terminated strings
1248 * @return newly allocated path object
1251 vfs_path_t *
1252 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1254 va_list args;
1255 char *str_path;
1256 const char *result_str;
1257 vfs_path_t *ret_vpath;
1259 if (vpath == NULL || first_element == NULL)
1260 return NULL;
1262 va_start (args, first_element);
1263 str_path = mc_build_filenamev (first_element, args);
1264 va_end (args);
1266 result_str = vfs_path_as_str (vpath);
1267 ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1268 g_free (str_path);
1270 return ret_vpath;
1274 /* --------------------------------------------------------------------------------------------- */
1277 * Append vpath_t tokens to path object
1279 * @param first_vpath vpath objects
1280 * @param ... NULL-terminated vpath objects
1282 * @return newly allocated path object
1285 vfs_path_t *
1286 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1288 va_list args;
1289 vfs_path_t *ret_vpath;
1290 const vfs_path_t *current_vpath = first_vpath;
1292 if (first_vpath == NULL)
1293 return NULL;
1295 ret_vpath = vfs_path_new ();
1297 va_start (args, first_vpath);
1300 int vindex;
1302 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1304 vfs_path_element_t *path_element;
1306 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1307 g_array_append_val (ret_vpath->path, path_element);
1309 current_vpath = va_arg (args, const vfs_path_t *);
1311 while (current_vpath != NULL);
1312 va_end (args);
1314 ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1316 return ret_vpath;
1319 /* --------------------------------------------------------------------------------------------- */
1322 * get tockens count in path.
1324 * @param vpath path object
1326 * @return count of tokens
1329 size_t
1330 vfs_path_tokens_count (const vfs_path_t * vpath)
1332 size_t count_tokens = 0;
1333 int element_index;
1335 if (vpath == NULL)
1336 return 0;
1338 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1340 const vfs_path_element_t *element;
1341 const char *token, *prev_token;
1343 element = vfs_path_get_by_index (vpath, element_index);
1345 for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1346 prev_token = token + 1)
1348 /* skip empty substring */
1349 if (token != prev_token)
1350 count_tokens++;
1353 if (*prev_token != '\0')
1354 count_tokens++;
1357 return count_tokens;
1360 /* --------------------------------------------------------------------------------------------- */
1363 * Get subpath by tokens
1365 * @param vpath path object
1366 * @param start_position first token for got/ Started from 0.
1367 * If negative, then position will be relative to end of path
1368 * @param length count of tokens
1370 * @return newly allocated string with path tokens separated by slash
1373 char *
1374 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1376 GString *ret_tokens, *element_tokens;
1377 int element_index;
1378 size_t tokens_count = vfs_path_tokens_count (vpath);
1380 if (vpath == NULL)
1381 return NULL;
1383 if (length == 0)
1384 length = tokens_count;
1386 if (length < 0)
1387 length = tokens_count + length;
1389 if (start_position < 0)
1390 start_position = (ssize_t) tokens_count + start_position;
1392 if (start_position < 0)
1393 return NULL;
1395 if (start_position >= (ssize_t) tokens_count)
1396 return NULL;
1398 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1399 length = tokens_count - start_position;
1401 ret_tokens = g_string_sized_new (32);
1402 element_tokens = g_string_sized_new (32);
1404 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1406 const vfs_path_element_t *element;
1407 char **path_tokens, **iterator;
1409 g_string_assign (element_tokens, "");
1410 element = vfs_path_get_by_index (vpath, element_index);
1411 path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1413 for (iterator = path_tokens; *iterator != NULL; iterator++)
1415 if (**iterator != '\0')
1417 if (start_position == 0)
1419 if (length == 0)
1421 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1422 g_string_free (element_tokens, TRUE);
1423 g_strfreev (path_tokens);
1424 return g_string_free (ret_tokens, FALSE);
1426 length--;
1427 if (element_tokens->len != 0)
1428 g_string_append_c (element_tokens, PATH_SEP);
1429 g_string_append (element_tokens, *iterator);
1431 else
1432 start_position--;
1435 g_strfreev (path_tokens);
1436 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1439 g_string_free (element_tokens, TRUE);
1440 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1443 /* --------------------------------------------------------------------------------------------- */
1445 * Get subpath by tokens
1447 * @param vpath path object
1448 * @param start_position first token for got/ Started from 0.
1449 * If negative, then position will be relative to end of path
1450 * @param length count of tokens
1452 * @return newly allocated path object with path tokens separated by slash
1455 vfs_path_t *
1456 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1458 char *str_tokens;
1459 vfs_path_t *ret_vpath = NULL;
1461 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1462 if (str_tokens != NULL)
1464 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1465 g_free (str_tokens);
1467 return ret_vpath;
1470 /* --------------------------------------------------------------------------------------------- */
1473 * Build URL parameters (such as user:pass @ host:port) from one path element object
1475 * @param element path element
1476 * @param keep_password TRUE or FALSE
1478 * @return newly allocated string
1481 char *
1482 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1484 GString *buffer;
1486 if (element == NULL)
1487 return NULL;
1489 buffer = g_string_new ("");
1491 if (element->user != NULL)
1492 g_string_append (buffer, element->user);
1494 if (element->password != NULL && keep_password)
1496 g_string_append_c (buffer, ':');
1497 g_string_append (buffer, element->password);
1500 if (element->host != NULL)
1502 if ((element->user != NULL) || (element->password != NULL))
1503 g_string_append_c (buffer, '@');
1504 if (element->ipv6)
1505 g_string_append_c (buffer, '[');
1506 g_string_append (buffer, element->host);
1507 if (element->ipv6)
1508 g_string_append_c (buffer, ']');
1511 if ((element->port) != 0 && (element->host != NULL))
1513 g_string_append_c (buffer, ':');
1514 g_string_append_printf (buffer, "%d", element->port);
1517 return g_string_free (buffer, FALSE);
1520 /* --------------------------------------------------------------------------------------------- */
1522 * Build pretty string representation of one path_element_t object
1524 * @param element path element
1526 * @return newly allocated string
1529 char *
1530 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1532 char *url_params;
1533 GString *pretty_path;
1535 pretty_path = g_string_new (element->class->prefix);
1536 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1538 url_params = vfs_path_build_url_params_str (element, FALSE);
1539 g_string_append (pretty_path, url_params);
1540 g_free (url_params);
1542 if (!IS_PATH_SEP (*element->path))
1543 g_string_append_c (pretty_path, PATH_SEP);
1545 g_string_append (pretty_path, element->path);
1546 return g_string_free (pretty_path, FALSE);
1549 /* --------------------------------------------------------------------------------------------- */
1551 * Compare two path objects as strings
1553 * @param vpath1 first path object
1554 * @param vpath2 second vpath object
1556 * @return integer value like to strcmp.
1559 gboolean
1560 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1562 const char *path1, *path2;
1563 gboolean ret_val;
1565 if (vpath1 == NULL || vpath2 == NULL)
1566 return FALSE;
1568 path1 = vfs_path_as_str (vpath1);
1569 path2 = vfs_path_as_str (vpath2);
1571 ret_val = strcmp (path1, path2) == 0;
1573 return ret_val;
1576 /* --------------------------------------------------------------------------------------------- */
1578 * Compare two path objects as strings
1580 * @param vpath1 first path object
1581 * @param vpath2 second vpath object
1582 * @param len number of first 'len' characters
1584 * @return integer value like to strcmp.
1587 gboolean
1588 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1590 const char *path1, *path2;
1591 gboolean ret_val;
1593 if (vpath1 == NULL || vpath2 == NULL)
1594 return FALSE;
1596 path1 = vfs_path_as_str (vpath1);
1597 path2 = vfs_path_as_str (vpath2);
1599 ret_val = strncmp (path1, path2, len) == 0;
1601 return ret_val;
1604 /* --------------------------------------------------------------------------------------------- */
1606 * Calculate path length in string representation
1608 * @param vpath path object
1610 * @return length of path
1613 size_t
1614 vfs_path_len (const vfs_path_t * vpath)
1616 if (vpath == NULL)
1617 return 0;
1619 return strlen (vpath->str);
1622 /* --------------------------------------------------------------------------------------------- */
1624 * Convert relative vpath object to absolute
1626 * @param vpath path object
1628 * @return absolute path object
1631 vfs_path_t *
1632 vfs_path_to_absolute (const vfs_path_t * vpath)
1634 vfs_path_t *absolute_vpath;
1635 const char *path_str;
1637 if (!vpath->relative)
1638 return vfs_path_clone (vpath);
1640 path_str = vfs_path_as_str (vpath);
1641 absolute_vpath = vfs_path_from_str (path_str);
1642 return absolute_vpath;
1645 /* --------------------------------------------------------------------------------------------- */