(mc_build_filenamev): refactoring.
[midnight-commander.git] / lib / vfs / path.c
blob85faa9abd20c2026b49cd45033897010809d03e0
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9 Andrew Borodin <aborodin@vmail.ru>, 2013-2022
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 <errno.h>
39 #include "lib/global.h"
40 #include "lib/strutil.h"
41 #include "lib/util.h" /* mc_build_filename() */
42 #include "lib/serialize.h"
44 #include "vfs.h"
45 #include "utilvfs.h"
46 #include "xdirentry.h"
47 #include "path.h"
49 extern GPtrArray *vfs__classes_list;
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 /*** file scope type declarations ****************************************************************/
57 /*** forward declarations (file scope functions) *************************************************/
59 /*** file scope variables ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
62 /*** file scope functions ************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
65 static gboolean
66 path_magic (const char *path)
68 struct stat buf;
70 return (stat (path, &buf) != 0);
73 /* --------------------------------------------------------------------------------------------- */
75 /**
76 * Splits path extracting vfs part.
78 * Splits path
79 * \verbatim /p1#op/inpath \endverbatim
80 * into
81 * \verbatim inpath,op; \endverbatim
82 * returns which vfs it is.
83 * What is left in path is p1. You still want to g_free(path), you DON'T
84 * want to free neither *inpath nor *op
87 static struct vfs_class *
88 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
89 size_t skip_count)
91 char *semi;
92 char *slash;
93 struct vfs_class *ret;
95 if (path == NULL)
96 vfs_die ("Cannot split NULL");
98 semi = strrstr_skip_count (path, "#", skip_count);
100 if ((semi == NULL) || (!path_magic (path)))
101 return NULL;
103 slash = strchr (semi, PATH_SEP);
104 *semi = '\0';
106 if (op != NULL)
107 *op = NULL;
109 if (inpath != NULL)
110 *inpath = NULL;
112 if (slash != NULL)
113 *slash = '\0';
115 ret = vfs_prefix_to_class (semi + 1);
116 if (ret != NULL)
118 if (op != NULL)
119 *op = semi + 1;
120 if (inpath != NULL)
121 *inpath = slash != NULL ? slash + 1 : NULL;
122 return ret;
125 if (slash != NULL)
126 *slash = PATH_SEP;
128 *semi = '#';
129 ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
130 return ret;
133 /* --------------------------------------------------------------------------------------------- */
135 * remove //, /./ and /../
137 * @return newly allocated string
140 static char *
141 vfs_canon (const char *path)
143 char *result;
145 if (path == NULL)
146 vfs_die ("Cannot canonicalize NULL");
148 if (!IS_PATH_SEP (*path))
150 /* Relative to current directory */
152 char *local;
154 if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
157 encoding prefix placed at start of string without the leading slash
158 should be autofixed by adding the leading slash
160 local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
162 else
164 const char *curr_dir;
166 curr_dir = vfs_get_current_dir ();
167 local = mc_build_filename (curr_dir, path, (char *) NULL);
169 result = vfs_canon (local);
170 g_free (local);
172 else
174 /* Absolute path */
176 result = g_strdup (path);
177 canonicalize_pathname (result);
180 return result;
183 /* --------------------------------------------------------------------------------------------- */
185 #ifdef HAVE_CHARSET
186 /** get encoding after last #enc: or NULL, if part does not contain #enc:
188 * @param path null-terminated string
189 * @param len the maximum length of path, where #enc: should be searched
191 * @return newly allocated string.
194 static char *
195 vfs_get_encoding (const char *path, ssize_t len)
197 char *semi;
199 /* try found #enc: */
200 semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
201 if (semi == NULL)
202 return NULL;
204 if (semi == path || IS_PATH_SEP (semi[-1]))
206 char *slash;
208 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
209 slash = strchr (semi, PATH_SEP);
210 if (slash != NULL)
211 return g_strndup (semi, slash - semi);
212 return g_strdup (semi);
215 return vfs_get_encoding (path, semi - path);
217 #endif
219 /* --------------------------------------------------------------------------------------------- */
220 /** Extract the hostname and username from the path
222 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
224 * ftp://sunsite.unc.edu/pub/linux
225 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
226 * ftp://tsx-11.mit.edu:8192/
227 * ftp://joe@foo.edu:11321/private
228 * ftp://joe:password@foo.se
230 * @param path_element is an input string to be parsed
231 * @param path is an input string to be parsed
233 * @return g_malloc()ed url info.
234 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
235 * is not set, then the current login name is supplied.
236 * Return value is a g_malloc()ed structure with the pathname relative to the
237 * host.
240 static void
241 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
243 char *pcopy;
244 char *colon, *at, *rest;
246 path_element->port = 0;
248 pcopy = g_strdup (path);
250 /* search for any possible user */
251 at = strrchr (pcopy, '@');
253 /* We have a username */
254 if (at == NULL)
255 rest = pcopy;
256 else
258 const char *pend;
259 char *inner_colon;
261 pend = strchr (at, '\0');
262 *at = '\0';
264 inner_colon = strchr (pcopy, ':');
265 if (inner_colon != NULL)
267 *inner_colon = '\0';
268 inner_colon++;
269 path_element->password = g_strdup (inner_colon);
272 if (*pcopy != '\0')
273 path_element->user = g_strdup (pcopy);
275 if (pend == at + 1)
276 rest = at;
277 else
278 rest = at + 1;
281 /* Check if the host comes with a port spec, if so, chop it */
282 if (*rest != '[')
283 colon = strchr (rest, ':');
284 else
286 colon = strchr (++rest, ']');
287 if (colon != NULL)
289 *colon = '\0';
290 colon++;
291 *colon = '\0';
292 path_element->ipv6 = TRUE;
296 if (colon != NULL)
298 *colon = '\0';
299 /* cppcheck-suppress invalidscanf */
300 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
302 if (path_element->port <= 0 || path_element->port >= 65536)
303 path_element->port = 0;
305 else
306 while (*(++colon) != '\0')
308 switch (*colon)
310 case 'C':
311 path_element->port = 1;
312 break;
313 case 'r':
314 path_element->port = 2;
315 break;
316 default:
317 break;
321 path_element->host = g_strdup (rest);
322 g_free (pcopy);
325 /* --------------------------------------------------------------------------------------------- */
327 * get VFS class for the given name
329 * @param class_name name of class
331 * @return pointer to class structure or NULL if class not found
334 static struct vfs_class *
335 vfs_get_class_by_name (const char *class_name)
337 guint i;
339 if (class_name == NULL)
340 return NULL;
342 for (i = 0; i < vfs__classes_list->len; i++)
344 struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
345 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
346 return vfs;
349 return NULL;
352 /* --------------------------------------------------------------------------------------------- */
354 * Check if path string contain URL-like elements
356 * @param path_str path
358 * @return TRUE if path is deprecated or FALSE otherwise
361 static gboolean
362 vfs_path_is_str_path_deprecated (const char *path_str)
364 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
367 /* --------------------------------------------------------------------------------------------- */
368 /** Split path string to path elements by deprecated algorithm.
370 * @param path_str VFS-path
372 * @return pointer to newly created vfs_path_t object with filled path elements array.
375 static vfs_path_t *
376 vfs_path_from_str_deprecated_parser (char *path)
378 vfs_path_t *vpath;
379 vfs_path_element_t *element;
380 struct vfs_class *class;
381 const char *local, *op;
383 vpath = vfs_path_new (FALSE);
385 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
387 char *url_params;
388 element = g_new0 (vfs_path_element_t, 1);
389 element->class = class;
390 if (local == NULL)
391 local = "";
392 element->path = vfs_translate_path_n (local);
394 #ifdef HAVE_CHARSET
395 element->encoding = vfs_get_encoding (local, -1);
396 element->dir.converter =
397 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
398 #endif
400 url_params = strchr (op, ':'); /* skip VFS prefix */
401 if (url_params != NULL)
403 *url_params = '\0';
404 url_params++;
405 vfs_path_url_split (element, url_params);
408 if (*op != '\0')
409 element->vfs_prefix = g_strdup (op);
411 g_array_prepend_val (vpath->path, element);
413 if (path[0] != '\0')
415 element = g_new0 (vfs_path_element_t, 1);
416 element->class = g_ptr_array_index (vfs__classes_list, 0);
417 element->path = vfs_translate_path_n (path);
419 #ifdef HAVE_CHARSET
420 element->encoding = vfs_get_encoding (path, -1);
421 element->dir.converter =
422 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
423 #endif
424 g_array_prepend_val (vpath->path, element);
427 return vpath;
430 /* --------------------------------------------------------------------------------------------- */
431 /** Split path string to path elements by URL algorithm.
433 * @param path_str VFS-path
434 * @param flags flags for converter
436 * @return pointer to newly created vfs_path_t object with filled path elements array.
439 static vfs_path_t *
440 vfs_path_from_str_uri_parser (char *path)
442 vfs_path_t *vpath;
443 vfs_path_element_t *element;
444 char *url_delimiter;
446 vpath = vfs_path_new (path != NULL && !IS_PATH_SEP (*path));
448 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
450 char *vfs_prefix_start;
451 char *real_vfs_prefix_start = url_delimiter;
453 while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
454 real_vfs_prefix_start--;
455 vfs_prefix_start = real_vfs_prefix_start;
457 if (IS_PATH_SEP (*vfs_prefix_start))
458 vfs_prefix_start += 1;
460 *url_delimiter = '\0';
462 element = g_new0 (vfs_path_element_t, 1);
463 element->class = vfs_prefix_to_class (vfs_prefix_start);
464 element->vfs_prefix = g_strdup (vfs_prefix_start);
466 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
468 if (element->class != NULL && (element->class->flags & VFSF_REMOTE) != 0)
470 char *slash_pointer;
472 slash_pointer = strchr (url_delimiter, PATH_SEP);
473 if (slash_pointer == NULL)
475 element->path = g_strdup ("");
477 else
479 element->path = vfs_translate_path_n (slash_pointer + 1);
480 #ifdef HAVE_CHARSET
481 element->encoding = vfs_get_encoding (slash_pointer, -1);
482 #endif
483 *slash_pointer = '\0';
485 vfs_path_url_split (element, url_delimiter);
487 else
489 element->path = vfs_translate_path_n (url_delimiter);
490 #ifdef HAVE_CHARSET
491 element->encoding = vfs_get_encoding (url_delimiter, -1);
492 #endif
494 #ifdef HAVE_CHARSET
495 element->dir.converter =
496 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
497 #endif
498 g_array_prepend_val (vpath->path, element);
500 if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
501 (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
502 *real_vfs_prefix_start = '\0';
503 else
504 *(real_vfs_prefix_start + 1) = '\0';
507 if (path[0] != '\0')
509 element = g_new0 (vfs_path_element_t, 1);
510 element->class = g_ptr_array_index (vfs__classes_list, 0);
511 element->path = vfs_translate_path_n (path);
512 #ifdef HAVE_CHARSET
513 element->encoding = vfs_get_encoding (path, -1);
514 element->dir.converter =
515 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
516 #endif
517 g_array_prepend_val (vpath->path, element);
520 return vpath;
523 /* --------------------------------------------------------------------------------------------- */
525 * Add element's class info to result string (such as VFS name, host, encoding etc)
526 * This function used as helper only in vfs_path_tokens_get() function
528 * @param element current path element
529 * @param ret_tokens total tikens for return
530 * @param element_tokens accumulated element-only tokens
533 static void
534 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
535 GString * element_tokens)
537 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
538 && element_tokens->len > 0)
540 GString *url_str;
542 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
543 g_string_append_c (ret_tokens, PATH_SEP);
545 g_string_append (ret_tokens, element->vfs_prefix);
546 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
548 url_str = vfs_path_build_url_params_str (element, TRUE);
549 if (url_str != NULL)
551 g_string_append_len (ret_tokens, url_str->str, url_str->len);
552 g_string_append_c (ret_tokens, PATH_SEP);
553 g_string_free (url_str, TRUE);
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 && \
602 (element->class->flags & VFSF_LOCAL) != 0) \
604 char *stripped_home_str; \
605 stripped_home_str = vfs_path_strip_home (appendfrom); \
606 g_string_append (buffer, stripped_home_str); \
607 g_free (stripped_home_str); \
609 else \
611 if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
612 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
613 g_string_append_c (buffer, PATH_SEP); \
614 g_string_append (buffer, appendfrom); \
619 * Convert first elements_count elements from vfs_path_t to string representation with flags.
621 * @param vpath pointer to vfs_path_t object
622 * @param elements_count count of first elements for convert
623 * @param flags for converter
625 * @return pointer to newly created string.
628 char *
629 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
631 int element_index;
632 GString *buffer;
633 #ifdef HAVE_CHARSET
634 GString *recode_buffer = NULL;
635 #endif
637 if (vpath == NULL)
638 return NULL;
640 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
641 elements_count = vfs_path_elements_count (vpath);
643 if (elements_count < 0)
644 elements_count = vfs_path_elements_count (vpath) + elements_count;
646 buffer = g_string_new ("");
648 for (element_index = 0; element_index < elements_count; element_index++)
650 const vfs_path_element_t *element;
651 gboolean is_relative = vpath->relative && (element_index == 0);
653 element = vfs_path_get_by_index (vpath, element_index);
654 if (element->vfs_prefix != NULL)
656 GString *url_str;
658 if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
659 g_string_append_c (buffer, PATH_SEP);
661 g_string_append (buffer, element->vfs_prefix);
662 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
664 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
665 if (url_str != NULL)
667 g_string_append_len (buffer, url_str->str, url_str->len);
668 g_string_append_c (buffer, PATH_SEP);
669 g_string_free (url_str, TRUE);
673 #ifdef HAVE_CHARSET
674 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
676 if ((flags & VPF_HIDE_CHARSET) == 0)
678 if ((!is_relative)
679 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
680 g_string_append (buffer, PATH_SEP_STR);
681 g_string_append (buffer, VFS_ENCODING_PREFIX);
682 g_string_append (buffer, element->encoding);
685 if (recode_buffer == NULL)
686 recode_buffer = g_string_sized_new (32);
687 else
688 g_string_set_size (recode_buffer, 0);
690 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
691 vfs_append_from_path (recode_buffer->str, is_relative);
693 else
694 #endif
696 vfs_append_from_path (element->path, is_relative);
700 #ifdef HAVE_CHARSET
701 if (recode_buffer != NULL)
702 g_string_free (recode_buffer, TRUE);
703 #endif
705 return g_string_free (buffer, FALSE);
708 #undef vfs_append_from_path
710 /* --------------------------------------------------------------------------------------------- */
712 * Convert first elements_count elements from vfs_path_t to string representation.
714 * @param vpath pointer to vfs_path_t object
715 * @param elements_count count of first elements for convert
717 * @return pointer to newly created string.
720 char *
721 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
723 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
726 /* --------------------------------------------------------------------------------------------- */
728 * Split path string to path elements with flags for change parce process.
730 * @param path_str VFS-path
731 * @param flags flags for parser
733 * @return pointer to newly created vfs_path_t object with filled path elements array.
736 vfs_path_t *
737 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
739 vfs_path_t *vpath;
740 char *path;
742 if (path_str == NULL)
743 return NULL;
745 if ((flags & VPF_NO_CANON) == 0)
746 path = vfs_canon (path_str);
747 else
748 path = g_strdup (path_str);
750 if (path == NULL)
751 return NULL;
753 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
754 vpath = vfs_path_from_str_deprecated_parser (path);
755 else
756 vpath = vfs_path_from_str_uri_parser (path);
758 vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
759 g_free (path);
761 return vpath;
764 /* --------------------------------------------------------------------------------------------- */
766 * Split path string to path elements.
768 * @param path_str VFS-path
770 * @return pointer to newly created vfs_path_t object with filled path elements array.
773 vfs_path_t *
774 vfs_path_from_str (const char *path_str)
776 return vfs_path_from_str_flags (path_str, VPF_NONE);
779 /* --------------------------------------------------------------------------------------------- */
781 * Create new vfs_path_t object.
783 * @return pointer to newly created vfs_path_t object.
786 vfs_path_t *
787 vfs_path_new (gboolean relative)
789 vfs_path_t *vpath;
791 vpath = g_new0 (vfs_path_t, 1);
792 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
793 vpath->relative = relative;
795 return vpath;
798 /* --------------------------------------------------------------------------------------------- */
800 * Get count of path elements.
802 * @param vpath pointer to vfs_path_t object
804 * @return count of path elements.
808 vfs_path_elements_count (const vfs_path_t * vpath)
810 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
813 /* --------------------------------------------------------------------------------------------- */
815 * Add vfs_path_element_t object to end of list in vfs_path_t object
816 * @param vpath pointer to vfs_path_t object
817 * @param path_element pointer to vfs_path_element_t object
820 void
821 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
823 g_array_append_val (vpath->path, path_element);
824 g_free (vpath->str);
825 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
828 /* --------------------------------------------------------------------------------------------- */
830 * Get one path element by index.
832 * @param vpath pointer to vfs_path_t object.
833 * May be NULL. In this case NULL is returned and errno set to 0.
834 * @param element_index element index. May have negative value (in this case count was started at
835 * the end of list). If @element_index is out of range, NULL is returned and
836 * errno set to EINVAL.
838 * @return path element
841 const vfs_path_element_t *
842 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
844 int n;
846 if (vpath == NULL)
848 errno = 0;
849 return NULL;
852 n = vfs_path_elements_count (vpath);
854 if (element_index < 0)
855 element_index += n;
857 if (element_index < 0 || element_index > n)
859 errno = EINVAL;
860 return NULL;
863 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
866 /* --------------------------------------------------------------------------------------------- */
868 * Clone one path element
870 * @param element pointer to vfs_path_element_t object
872 * @return Newly allocated path element
875 vfs_path_element_t *
876 vfs_path_element_clone (const vfs_path_element_t * element)
878 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
880 new_element->user = g_strdup (element->user);
881 new_element->password = g_strdup (element->password);
882 new_element->host = g_strdup (element->host);
883 new_element->ipv6 = element->ipv6;
884 new_element->port = element->port;
885 new_element->path = g_strdup (element->path);
886 new_element->class = element->class;
887 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
888 #ifdef HAVE_CHARSET
889 new_element->encoding = g_strdup (element->encoding);
890 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
891 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
892 else
893 new_element->dir.converter = element->dir.converter;
894 #endif
895 new_element->dir.info = element->dir.info;
897 return new_element;
900 /* --------------------------------------------------------------------------------------------- */
902 * Free one path element.
904 * @param element pointer to vfs_path_element_t object
908 void
909 vfs_path_element_free (vfs_path_element_t * element)
911 if (element == NULL)
912 return;
914 g_free (element->user);
915 g_free (element->password);
916 g_free (element->host);
917 g_free (element->path);
918 g_free (element->vfs_prefix);
920 #ifdef HAVE_CHARSET
921 g_free (element->encoding);
923 if (vfs_path_element_need_cleanup_converter (element))
924 str_close_conv (element->dir.converter);
925 #endif
927 g_free (element);
930 /* --------------------------------------------------------------------------------------------- */
932 * Clone path
934 * @param vpath pointer to vfs_path_t object
936 * @return Newly allocated path object
939 vfs_path_t *
940 vfs_path_clone (const vfs_path_t * vpath)
942 vfs_path_t *new_vpath;
943 int vpath_element_index;
945 if (vpath == NULL)
946 return NULL;
948 new_vpath = vfs_path_new (vpath->relative);
950 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
951 vpath_element_index++)
953 vfs_path_element_t *path_element;
955 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
956 g_array_append_val (new_vpath->path, path_element);
958 new_vpath->str = g_strdup (vpath->str);
960 return new_vpath;
963 /* --------------------------------------------------------------------------------------------- */
965 * Free vfs_path_t object.
967 * @param vpath pointer to vfs_path_t object
968 * @param free_str if TRUE the string representation of vpath is freed as well
970 * @return the string representation of vpath (i.e. NULL if free_str is TRUE)
973 char *
974 vfs_path_free (vfs_path_t * vpath, gboolean free_str)
976 int vpath_element_index;
977 char *ret;
979 if (vpath == NULL)
980 return NULL;
982 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
983 vpath_element_index++)
985 vfs_path_element_t *path_element;
987 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
988 vfs_path_element_free (path_element);
991 g_array_free (vpath->path, TRUE);
993 if (!free_str)
994 ret = vpath->str;
995 else
997 g_free (vpath->str);
998 ret = NULL;
1001 g_free (vpath);
1003 return ret;
1006 /* --------------------------------------------------------------------------------------------- */
1008 * Remove one path element by index
1010 * @param vpath pointer to vfs_path_t object
1011 * @param element_index element index. May have negative value (in this case count was started at the end of list).
1015 void
1016 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
1018 vfs_path_element_t *element;
1020 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
1021 return;
1023 if (element_index < 0)
1024 element_index = vfs_path_elements_count (vpath) + element_index;
1026 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1027 vpath->path = g_array_remove_index (vpath->path, element_index);
1028 vfs_path_element_free (element);
1029 g_free (vpath->str);
1030 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1033 /* --------------------------------------------------------------------------------------------- */
1034 /** Return VFS class for the given prefix */
1036 struct vfs_class *
1037 vfs_prefix_to_class (const char *prefix)
1039 guint i;
1041 /* Avoid first class (localfs) that would accept any prefix */
1042 for (i = 1; i < vfs__classes_list->len; i++)
1044 struct vfs_class *vfs;
1046 vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
1047 if (vfs->which != NULL)
1049 if (vfs->which (vfs, prefix) == -1)
1050 continue;
1051 return vfs;
1054 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1055 return vfs;
1058 return NULL;
1061 /* --------------------------------------------------------------------------------------------- */
1063 #ifdef HAVE_CHARSET
1066 * Check if need cleanup charset converter for vfs_path_element_t
1068 * @param element part of path
1070 * @return TRUE if need cleanup converter or FALSE otherwise
1073 gboolean
1074 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1076 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1079 /* --------------------------------------------------------------------------------------------- */
1081 * Change encoding for last part (vfs_path_element_t) of vpath
1083 * @param vpath pointer to path structure
1084 * encoding name of charset
1086 * @return pointer to path structure (for use function in another functions)
1088 vfs_path_t *
1089 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
1091 vfs_path_element_t *path_element;
1093 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1094 /* don't add current encoding */
1095 if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1096 return vpath;
1098 g_free (path_element->encoding);
1099 path_element->encoding = g_strdup (encoding);
1101 if (vfs_path_element_need_cleanup_converter (path_element))
1102 str_close_conv (path_element->dir.converter);
1104 path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1106 g_free (vpath->str);
1107 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1108 return vpath;
1111 #endif
1113 /* --------------------------------------------------------------------------------------------- */
1116 * Serialize vfs_path_t object to string
1118 * @param vpath data for serialization
1119 * @param error contain pointer to object for handle error code and message
1121 * @return serialized vpath as newly allocated string
1124 char *
1125 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
1127 mc_config_t *cpath;
1128 ssize_t element_index;
1129 char *ret_value;
1131 mc_return_val_if_error (mcerror, FALSE);
1133 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1135 mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1136 return NULL;
1139 cpath = mc_config_init (NULL, FALSE);
1141 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1143 char groupname[BUF_TINY];
1144 const vfs_path_element_t *element;
1146 g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1147 element = vfs_path_get_by_index (vpath, element_index);
1148 /* convert one element to config group */
1150 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1151 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1152 #ifdef HAVE_CHARSET
1153 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1154 #endif
1155 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1157 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1158 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1159 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1160 if (element->port != 0)
1161 mc_config_set_int (cpath, groupname, "port", element->port);
1164 ret_value = mc_serialize_config (cpath, mcerror);
1165 mc_config_deinit (cpath);
1166 return ret_value;
1169 /* --------------------------------------------------------------------------------------------- */
1171 * Deserialize string to vfs_path_t object
1173 * @param data data for serialization
1174 * @param error contain pointer to object for handle error code and message
1176 * @return newly allocated vfs_path_t object
1179 vfs_path_t *
1180 vfs_path_deserialize (const char *data, GError ** mcerror)
1182 mc_config_t *cpath;
1183 size_t element_index;
1184 vfs_path_t *vpath;
1186 mc_return_val_if_error (mcerror, FALSE);
1188 cpath = mc_deserialize_config (data, mcerror);
1189 if (cpath == NULL)
1190 return NULL;
1192 vpath = vfs_path_new (FALSE);
1194 for (element_index = 0;; element_index++)
1196 struct vfs_class *eclass;
1197 vfs_path_element_t *element;
1198 char *cfg_value;
1199 char groupname[BUF_TINY];
1201 g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1202 if (!mc_config_has_group (cpath, groupname))
1203 break;
1205 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1206 eclass = vfs_get_class_by_name (cfg_value);
1207 if (eclass == NULL)
1209 vfs_path_free (vpath, TRUE);
1210 g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1211 g_free (cfg_value);
1212 mc_config_deinit (cpath);
1213 return NULL;
1215 g_free (cfg_value);
1217 element = g_new0 (vfs_path_element_t, 1);
1218 element->class = eclass;
1219 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1221 #ifdef HAVE_CHARSET
1222 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1223 element->dir.converter =
1224 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1225 #endif
1227 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1229 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1230 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1231 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1232 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1234 vpath->path = g_array_append_val (vpath->path, element);
1237 mc_config_deinit (cpath);
1238 if (vfs_path_elements_count (vpath) == 0)
1240 vfs_path_free (vpath, TRUE);
1241 g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1242 return NULL;
1244 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1246 return vpath;
1249 /* --------------------------------------------------------------------------------------------- */
1251 * Build vfs_path_t object from arguments.
1253 * @param first_element of path
1254 * @param ... path tokens, terminated by NULL
1256 * @return newly allocated vfs_path_t object
1259 vfs_path_t *
1260 vfs_path_build_filename (const char *first_element, ...)
1262 va_list args;
1263 char *str_path;
1264 vfs_path_t *vpath;
1266 if (first_element == NULL)
1267 return NULL;
1269 va_start (args, first_element);
1270 str_path = mc_build_filenamev (first_element, args);
1271 va_end (args);
1272 vpath = vfs_path_from_str (str_path);
1273 g_free (str_path);
1274 return vpath;
1277 /* --------------------------------------------------------------------------------------------- */
1279 * Append tokens to path object
1281 * @param vpath path object
1282 * @param first_element of path
1283 * @param ... NULL-terminated strings
1285 * @return newly allocated path object
1288 vfs_path_t *
1289 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1291 va_list args;
1292 char *str_path;
1293 const char *result_str;
1294 vfs_path_t *ret_vpath;
1296 if (vpath == NULL || first_element == NULL)
1297 return NULL;
1299 va_start (args, first_element);
1300 str_path = mc_build_filenamev (first_element, args);
1301 va_end (args);
1303 result_str = vfs_path_as_str (vpath);
1304 ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1305 g_free (str_path);
1307 return ret_vpath;
1311 /* --------------------------------------------------------------------------------------------- */
1314 * Append vpath_t tokens to path object
1316 * @param first_vpath vpath objects
1317 * @param ... NULL-terminated vpath objects
1319 * @return newly allocated path object
1322 vfs_path_t *
1323 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1325 va_list args;
1326 vfs_path_t *ret_vpath;
1327 const vfs_path_t *current_vpath = first_vpath;
1329 if (first_vpath == NULL)
1330 return NULL;
1332 ret_vpath = vfs_path_new (FALSE);
1334 va_start (args, first_vpath);
1337 int vindex;
1339 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1341 vfs_path_element_t *path_element;
1343 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1344 g_array_append_val (ret_vpath->path, path_element);
1346 current_vpath = va_arg (args, const vfs_path_t *);
1348 while (current_vpath != NULL);
1349 va_end (args);
1351 ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1353 return ret_vpath;
1356 /* --------------------------------------------------------------------------------------------- */
1359 * get tockens count in path.
1361 * @param vpath path object
1363 * @return count of tokens
1366 size_t
1367 vfs_path_tokens_count (const vfs_path_t * vpath)
1369 size_t count_tokens = 0;
1370 int element_index;
1372 if (vpath == NULL)
1373 return 0;
1375 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1377 const vfs_path_element_t *element;
1378 const char *token, *prev_token;
1380 element = vfs_path_get_by_index (vpath, element_index);
1382 for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1383 prev_token = token + 1)
1385 /* skip empty substring */
1386 if (token != prev_token)
1387 count_tokens++;
1390 if (*prev_token != '\0')
1391 count_tokens++;
1394 return count_tokens;
1397 /* --------------------------------------------------------------------------------------------- */
1400 * Get subpath by tokens
1402 * @param vpath path object
1403 * @param start_position first token for got/ Started from 0.
1404 * If negative, then position will be relative to end of path
1405 * @param length count of tokens
1407 * @return newly allocated string with path tokens separated by slash
1410 char *
1411 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1413 GString *ret_tokens, *element_tokens;
1414 int element_index;
1415 size_t tokens_count = vfs_path_tokens_count (vpath);
1417 if (vpath == NULL)
1418 return NULL;
1420 if (length == 0)
1421 length = tokens_count;
1423 if (length < 0)
1424 length = tokens_count + length;
1426 if (start_position < 0)
1427 start_position = (ssize_t) tokens_count + start_position;
1429 if (start_position < 0)
1430 return NULL;
1432 if (start_position >= (ssize_t) tokens_count)
1433 return NULL;
1435 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1436 length = tokens_count - start_position;
1438 ret_tokens = g_string_sized_new (32);
1439 element_tokens = g_string_sized_new (32);
1441 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1443 const vfs_path_element_t *element;
1444 char **path_tokens, **iterator;
1446 g_string_assign (element_tokens, "");
1447 element = vfs_path_get_by_index (vpath, element_index);
1448 path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1450 for (iterator = path_tokens; *iterator != NULL; iterator++)
1452 if (**iterator != '\0')
1454 if (start_position == 0)
1456 if (length == 0)
1458 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1459 g_string_free (element_tokens, TRUE);
1460 g_strfreev (path_tokens);
1461 return g_string_free (ret_tokens, FALSE);
1463 length--;
1464 if (element_tokens->len != 0)
1465 g_string_append_c (element_tokens, PATH_SEP);
1466 g_string_append (element_tokens, *iterator);
1468 else
1469 start_position--;
1472 g_strfreev (path_tokens);
1473 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1476 g_string_free (element_tokens, TRUE);
1477 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1480 /* --------------------------------------------------------------------------------------------- */
1482 * Get subpath by tokens
1484 * @param vpath path object
1485 * @param start_position first token for got/ Started from 0.
1486 * If negative, then position will be relative to end of path
1487 * @param length count of tokens
1489 * @return newly allocated path object with path tokens separated by slash
1492 vfs_path_t *
1493 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1495 char *str_tokens;
1496 vfs_path_t *ret_vpath = NULL;
1498 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1499 if (str_tokens != NULL)
1501 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1502 g_free (str_tokens);
1504 return ret_vpath;
1507 /* --------------------------------------------------------------------------------------------- */
1510 * Build URL parameters (such as user:pass @ host:port) from one path element object
1512 * @param element path element
1513 * @param keep_password TRUE or FALSE
1515 * @return newly allocated non-empty string or NULL
1518 GString *
1519 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1521 GString *buffer;
1523 if (element == NULL)
1524 return NULL;
1526 buffer = g_string_sized_new (64);
1528 if (element->user != NULL)
1529 g_string_append (buffer, element->user);
1531 if (element->password != NULL && keep_password)
1533 g_string_append_c (buffer, ':');
1534 g_string_append (buffer, element->password);
1537 if (element->host != NULL)
1539 if ((element->user != NULL) || (element->password != NULL))
1540 g_string_append_c (buffer, '@');
1541 if (element->ipv6)
1542 g_string_append_c (buffer, '[');
1543 g_string_append (buffer, element->host);
1544 if (element->ipv6)
1545 g_string_append_c (buffer, ']');
1548 if ((element->port) != 0 && (element->host != NULL))
1550 g_string_append_c (buffer, ':');
1551 g_string_append_printf (buffer, "%d", element->port);
1554 if (buffer->len != 0)
1555 return buffer;
1557 g_string_free (buffer, TRUE);
1558 return NULL;
1561 /* --------------------------------------------------------------------------------------------- */
1563 * Build pretty string representation of one path_element_t object
1565 * @param element path element
1567 * @return newly allocated string
1570 GString *
1571 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1573 GString *url_params, *pretty_path;
1575 pretty_path = g_string_new (element->class->prefix);
1576 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1578 url_params = vfs_path_build_url_params_str (element, FALSE);
1579 if (url_params != NULL)
1581 g_string_append_len (pretty_path, url_params->str, url_params->len);
1582 g_string_free (url_params, TRUE);
1585 if (!IS_PATH_SEP (*element->path))
1586 g_string_append_c (pretty_path, PATH_SEP);
1588 return g_string_append (pretty_path, element->path);
1591 /* --------------------------------------------------------------------------------------------- */
1593 * Compare two path objects as strings
1595 * @param vpath1 first path object
1596 * @param vpath2 second vpath object
1598 * @return integer value like to strcmp.
1601 gboolean
1602 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1604 const char *path1, *path2;
1605 gboolean ret_val;
1607 if (vpath1 == NULL || vpath2 == NULL)
1608 return FALSE;
1610 path1 = vfs_path_as_str (vpath1);
1611 path2 = vfs_path_as_str (vpath2);
1613 ret_val = strcmp (path1, path2) == 0;
1615 return ret_val;
1618 /* --------------------------------------------------------------------------------------------- */
1620 * Compare two path objects as strings
1622 * @param vpath1 first path object
1623 * @param vpath2 second vpath object
1624 * @param len number of first 'len' characters
1626 * @return integer value like to strcmp.
1629 gboolean
1630 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1632 const char *path1, *path2;
1633 gboolean ret_val;
1635 if (vpath1 == NULL || vpath2 == NULL)
1636 return FALSE;
1638 path1 = vfs_path_as_str (vpath1);
1639 path2 = vfs_path_as_str (vpath2);
1641 ret_val = strncmp (path1, path2, len) == 0;
1643 return ret_val;
1646 /* --------------------------------------------------------------------------------------------- */
1648 * Calculate path length in string representation
1650 * @param vpath path object
1652 * @return length of path
1655 size_t
1656 vfs_path_len (const vfs_path_t * vpath)
1658 if (vpath == NULL)
1659 return 0;
1661 return strlen (vpath->str);
1664 /* --------------------------------------------------------------------------------------------- */
1666 * Convert relative vpath object to absolute
1668 * @param vpath path object
1670 * @return absolute path object
1673 vfs_path_t *
1674 vfs_path_to_absolute (const vfs_path_t * vpath)
1676 vfs_path_t *absolute_vpath;
1677 const char *path_str;
1679 if (!vpath->relative)
1680 return vfs_path_clone (vpath);
1682 path_str = vfs_path_as_str (vpath);
1683 absolute_vpath = vfs_path_from_str (path_str);
1684 return absolute_vpath;
1687 /* --------------------------------------------------------------------------------------------- */