Partially revert "VFS: (vfs_s_subclass): make the derived class from vfs_class."
[midnight-commander.git] / lib / vfs / path.c
bloba9b285b57c0823cbf329eaf26f6d073203dc9b64
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011-2018
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 char *colon, *at, *rest;
241 path_element->port = 0;
243 pcopy = g_strdup (path);
245 /* search for any possible user */
246 at = strrchr (pcopy, '@');
248 /* We have a username */
249 if (at == NULL)
250 rest = pcopy;
251 else
253 const char *pend;
254 char *inner_colon;
256 pend = strchr (at, '\0');
257 *at = '\0';
259 inner_colon = strchr (pcopy, ':');
260 if (inner_colon != NULL)
262 *inner_colon = '\0';
263 inner_colon++;
264 path_element->password = g_strdup (inner_colon);
267 if (*pcopy != '\0')
268 path_element->user = g_strdup (pcopy);
270 if (pend == at + 1)
271 rest = at;
272 else
273 rest = at + 1;
276 /* Check if the host comes with a port spec, if so, chop it */
277 if (*rest != '[')
278 colon = strchr (rest, ':');
279 else
281 colon = strchr (++rest, ']');
282 if (colon != NULL)
284 *colon = '\0';
285 colon++;
286 *colon = '\0';
287 path_element->ipv6 = TRUE;
291 if (colon != NULL)
293 *colon = '\0';
294 /* cppcheck-suppress invalidscanf */
295 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
297 if (path_element->port <= 0 || path_element->port >= 65536)
298 path_element->port = 0;
300 else
301 while (*(++colon) != '\0')
303 switch (*colon)
305 case 'C':
306 path_element->port = 1;
307 break;
308 case 'r':
309 path_element->port = 2;
310 break;
311 default:
312 break;
316 path_element->host = g_strdup (rest);
317 g_free (pcopy);
320 /* --------------------------------------------------------------------------------------------- */
322 * get VFS class for the given name
324 * @param class_name name of class
326 * @return pointer to class structure or NULL if class not found
329 static struct vfs_class *
330 vfs_get_class_by_name (const char *class_name)
332 guint i;
334 if (class_name == NULL)
335 return NULL;
337 for (i = 0; i < vfs__classes_list->len; i++)
339 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
340 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
341 return vfs;
344 return NULL;
347 /* --------------------------------------------------------------------------------------------- */
349 * Check if path string contain URL-like elements
351 * @param path_str path
353 * @return TRUE if path is deprecated or FALSE otherwise
356 static gboolean
357 vfs_path_is_str_path_deprecated (const char *path_str)
359 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
362 /* --------------------------------------------------------------------------------------------- */
363 /** Split path string to path elements by deprecated algorithm.
365 * @param path_str VFS-path
367 * @return pointer to newly created vfs_path_t object with filled path elements array.
370 static vfs_path_t *
371 vfs_path_from_str_deprecated_parser (char *path)
373 vfs_path_t *vpath;
374 vfs_path_element_t *element;
375 struct vfs_class *class;
376 const char *local, *op;
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)
437 vfs_path_t *vpath;
438 vfs_path_element_t *element;
439 char *url_delimiter;
441 vpath = vfs_path_new ();
442 vpath->relative = path != NULL && !IS_PATH_SEP (*path);
444 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
446 char *vfs_prefix_start;
447 char *real_vfs_prefix_start = url_delimiter;
448 struct vfs_s_subclass *sub = NULL;
450 while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
451 real_vfs_prefix_start--;
452 vfs_prefix_start = real_vfs_prefix_start;
454 if (IS_PATH_SEP (*vfs_prefix_start))
455 vfs_prefix_start += 1;
457 *url_delimiter = '\0';
459 element = g_new0 (vfs_path_element_t, 1);
460 element->class = vfs_prefix_to_class (vfs_prefix_start);
461 element->vfs_prefix = g_strdup (vfs_prefix_start);
463 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
464 sub = VFSDATA (element);
465 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
467 char *slash_pointer;
469 slash_pointer = strchr (url_delimiter, PATH_SEP);
470 if (slash_pointer == NULL)
472 element->path = g_strdup ("");
474 else
476 element->path = vfs_translate_path_n (slash_pointer + 1);
477 #ifdef HAVE_CHARSET
478 element->encoding = vfs_get_encoding (slash_pointer, -1);
479 #endif
480 *slash_pointer = '\0';
482 vfs_path_url_split (element, url_delimiter);
484 else
486 element->path = vfs_translate_path_n (url_delimiter);
487 #ifdef HAVE_CHARSET
488 element->encoding = vfs_get_encoding (url_delimiter, -1);
489 #endif
491 #ifdef HAVE_CHARSET
492 element->dir.converter =
493 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
494 #endif
495 g_array_prepend_val (vpath->path, element);
497 if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
498 (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
499 *real_vfs_prefix_start = '\0';
500 else
501 *(real_vfs_prefix_start + 1) = '\0';
504 if (path[0] != '\0')
506 element = g_new0 (vfs_path_element_t, 1);
507 element->class = g_ptr_array_index (vfs__classes_list, 0);
508 element->path = vfs_translate_path_n (path);
509 #ifdef HAVE_CHARSET
510 element->encoding = vfs_get_encoding (path, -1);
511 element->dir.converter =
512 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
513 #endif
514 g_array_prepend_val (vpath->path, element);
517 return vpath;
520 /* --------------------------------------------------------------------------------------------- */
522 * Add element's class info to result string (such as VFS name, host, encoding etc)
523 * This function used as helper only in vfs_path_tokens_get() function
525 * @param element current path element
526 * @param ret_tokens total tikens for return
527 * @param element_tokens accumulated element-only tokens
530 static void
531 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
532 GString * element_tokens)
534 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
535 && element_tokens->len > 0)
537 char *url_str;
539 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
540 g_string_append_c (ret_tokens, PATH_SEP);
542 g_string_append (ret_tokens, element->vfs_prefix);
543 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
545 url_str = vfs_path_build_url_params_str (element, TRUE);
546 if (*url_str != '\0')
548 g_string_append (ret_tokens, url_str);
549 g_string_append_c (ret_tokens, PATH_SEP);
552 g_free (url_str);
555 #ifdef HAVE_CHARSET
556 if (element->encoding != NULL)
558 if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
559 g_string_append (ret_tokens, PATH_SEP_STR);
560 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
561 g_string_append (ret_tokens, element->encoding);
562 g_string_append (ret_tokens, PATH_SEP_STR);
564 #endif
566 g_string_append (ret_tokens, element_tokens->str);
569 /* --------------------------------------------------------------------------------------------- */
571 * Strip path to home dir.
572 * @param dir pointer to string contains full path
575 static char *
576 vfs_path_strip_home (const char *dir)
578 const char *home_dir = mc_config_get_home_dir ();
580 if (home_dir != NULL)
582 size_t len;
584 len = strlen (home_dir);
586 if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
587 return g_strdup_printf ("~%s", dir + len);
590 return g_strdup (dir);
593 /* --------------------------------------------------------------------------------------------- */
594 /*** public functions ****************************************************************************/
595 /* --------------------------------------------------------------------------------------------- */
597 #define vfs_append_from_path(appendfrom, is_relative) \
599 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
601 char *stripped_home_str; \
602 stripped_home_str = vfs_path_strip_home (appendfrom); \
603 g_string_append (buffer, stripped_home_str); \
604 g_free (stripped_home_str); \
606 else \
608 if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
609 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
610 g_string_append_c (buffer, PATH_SEP); \
611 g_string_append (buffer, appendfrom); \
616 * Convert first elements_count elements from vfs_path_t to string representation with flags.
618 * @param vpath pointer to vfs_path_t object
619 * @param elements_count count of first elements for convert
620 * @param flags for converter
622 * @return pointer to newly created string.
625 char *
626 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
628 int element_index;
629 GString *buffer;
630 GString *recode_buffer;
632 if (vpath == NULL)
633 return NULL;
635 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
636 elements_count = vfs_path_elements_count (vpath);
638 if (elements_count < 0)
639 elements_count = vfs_path_elements_count (vpath) + elements_count;
641 buffer = g_string_new ("");
642 recode_buffer = g_string_new ("");
644 for (element_index = 0; element_index < elements_count; element_index++)
646 const vfs_path_element_t *element;
647 gboolean is_relative = vpath->relative && (element_index == 0);
649 element = vfs_path_get_by_index (vpath, element_index);
650 if (element->vfs_prefix != NULL)
652 char *url_str;
653 if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
654 g_string_append_c (buffer, PATH_SEP);
656 g_string_append (buffer, element->vfs_prefix);
657 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
659 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
661 if (*url_str != '\0')
663 g_string_append (buffer, url_str);
664 g_string_append_c (buffer, PATH_SEP);
667 g_free (url_str);
670 #ifdef HAVE_CHARSET
671 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
673 if ((flags & VPF_HIDE_CHARSET) == 0)
675 if ((!is_relative)
676 && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
677 g_string_append (buffer, PATH_SEP_STR);
678 g_string_append (buffer, VFS_ENCODING_PREFIX);
679 g_string_append (buffer, element->encoding);
681 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
682 vfs_append_from_path (recode_buffer->str, is_relative);
683 g_string_set_size (recode_buffer, 0);
685 else
686 #endif
688 vfs_append_from_path (element->path, is_relative);
691 g_string_free (recode_buffer, TRUE);
692 return g_string_free (buffer, FALSE);
695 #undef vfs_append_from_path
697 /* --------------------------------------------------------------------------------------------- */
699 * Convert first elements_count elements from vfs_path_t to string representation.
701 * @param vpath pointer to vfs_path_t object
702 * @param elements_count count of first elements for convert
704 * @return pointer to newly created string.
707 char *
708 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
710 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
713 /* --------------------------------------------------------------------------------------------- */
715 * Split path string to path elements with flags for change parce process.
717 * @param path_str VFS-path
718 * @param flags flags for parser
720 * @return pointer to newly created vfs_path_t object with filled path elements array.
723 vfs_path_t *
724 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
726 vfs_path_t *vpath;
727 char *path;
729 if (path_str == NULL)
730 return NULL;
732 if ((flags & VPF_NO_CANON) == 0)
733 path = vfs_canon (path_str);
734 else
735 path = g_strdup (path_str);
737 if (path == NULL)
738 return NULL;
740 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
741 vpath = vfs_path_from_str_deprecated_parser (path);
742 else
743 vpath = vfs_path_from_str_uri_parser (path);
745 vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
746 g_free (path);
748 return vpath;
751 /* --------------------------------------------------------------------------------------------- */
753 * Split path string to path elements.
755 * @param path_str VFS-path
757 * @return pointer to newly created vfs_path_t object with filled path elements array.
760 vfs_path_t *
761 vfs_path_from_str (const char *path_str)
763 return vfs_path_from_str_flags (path_str, VPF_NONE);
766 /* --------------------------------------------------------------------------------------------- */
768 * Create new vfs_path_t object.
770 * @return pointer to newly created vfs_path_t object.
773 vfs_path_t *
774 vfs_path_new (void)
776 vfs_path_t *vpath;
778 vpath = g_new0 (vfs_path_t, 1);
779 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
781 return vpath;
784 /* --------------------------------------------------------------------------------------------- */
786 * Get count of path elements.
788 * @param vpath pointer to vfs_path_t object
790 * @return count of path elements.
794 vfs_path_elements_count (const vfs_path_t * vpath)
796 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
799 /* --------------------------------------------------------------------------------------------- */
801 * Add vfs_path_element_t object to end of list in vfs_path_t object
802 * @param vpath pointer to vfs_path_t object
803 * @param path_element pointer to vfs_path_element_t object
806 void
807 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
809 g_array_append_val (vpath->path, path_element);
810 g_free (vpath->str);
811 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
814 /* --------------------------------------------------------------------------------------------- */
816 * Get one path element by index.
818 * @param vpath pointer to vfs_path_t object
819 * @param element_index element index. May have negative value (in this case count was started at the end of list).
821 * @return path element.
824 const vfs_path_element_t *
825 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
827 if (vpath == NULL)
828 return NULL;
830 if (element_index < 0)
831 element_index += vfs_path_elements_count (vpath);
833 if (element_index < 0)
834 vfs_die ("vfs_path_get_by_index: incorrect index!");
836 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
839 /* --------------------------------------------------------------------------------------------- */
841 * Clone one path element
843 * @param element pointer to vfs_path_element_t object
845 * @return Newly allocated path element
848 vfs_path_element_t *
849 vfs_path_element_clone (const vfs_path_element_t * element)
851 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
853 new_element->user = g_strdup (element->user);
854 new_element->password = g_strdup (element->password);
855 new_element->host = g_strdup (element->host);
856 new_element->ipv6 = element->ipv6;
857 new_element->port = element->port;
858 new_element->path = g_strdup (element->path);
859 new_element->class = element->class;
860 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
861 #ifdef HAVE_CHARSET
862 new_element->encoding = g_strdup (element->encoding);
863 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
864 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
865 else
866 new_element->dir.converter = element->dir.converter;
867 #endif
868 new_element->dir.info = element->dir.info;
870 return new_element;
873 /* --------------------------------------------------------------------------------------------- */
875 * Free one path element.
877 * @param element pointer to vfs_path_element_t object
881 void
882 vfs_path_element_free (vfs_path_element_t * element)
884 if (element == NULL)
885 return;
887 g_free (element->user);
888 g_free (element->password);
889 g_free (element->host);
890 g_free (element->path);
891 g_free (element->vfs_prefix);
893 #ifdef HAVE_CHARSET
894 g_free (element->encoding);
896 if (vfs_path_element_need_cleanup_converter (element))
897 str_close_conv (element->dir.converter);
898 #endif
900 g_free (element);
903 /* --------------------------------------------------------------------------------------------- */
905 * Clone path
907 * @param vpath pointer to vfs_path_t object
909 * @return Newly allocated path object
912 vfs_path_t *
913 vfs_path_clone (const vfs_path_t * vpath)
915 vfs_path_t *new_vpath;
916 int vpath_element_index;
918 if (vpath == NULL)
919 return NULL;
921 new_vpath = vfs_path_new ();
922 new_vpath->relative = vpath->relative;
924 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
925 vpath_element_index++)
927 vfs_path_element_t *path_element;
929 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
930 g_array_append_val (new_vpath->path, path_element);
932 new_vpath->str = g_strdup (vpath->str);
934 return new_vpath;
937 /* --------------------------------------------------------------------------------------------- */
939 * Free vfs_path_t object.
941 * @param vpath pointer to vfs_path_t object
945 void
946 vfs_path_free (vfs_path_t * vpath)
948 int vpath_element_index;
950 if (vpath == NULL)
951 return;
953 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
954 vpath_element_index++)
956 vfs_path_element_t *path_element;
958 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
959 vfs_path_element_free (path_element);
962 g_array_free (vpath->path, TRUE);
963 g_free (vpath->str);
964 g_free (vpath);
967 /* --------------------------------------------------------------------------------------------- */
969 * Remove one path element by index
971 * @param vpath pointer to vfs_path_t object
972 * @param element_index element index. May have negative value (in this case count was started at the end of list).
976 void
977 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
979 vfs_path_element_t *element;
981 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
982 return;
984 if (element_index < 0)
985 element_index = vfs_path_elements_count (vpath) + element_index;
987 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
988 vpath->path = g_array_remove_index (vpath->path, element_index);
989 vfs_path_element_free (element);
990 g_free (vpath->str);
991 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
994 /* --------------------------------------------------------------------------------------------- */
995 /** Return VFS class for the given prefix */
997 struct vfs_class *
998 vfs_prefix_to_class (const char *prefix)
1000 guint i;
1002 /* Avoid first class (localfs) that would accept any prefix */
1003 for (i = 1; i < vfs__classes_list->len; i++)
1005 struct vfs_class *vfs;
1007 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1008 if (vfs->which != NULL)
1010 if (vfs->which (vfs, prefix) == -1)
1011 continue;
1012 return vfs;
1015 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1016 return vfs;
1019 return NULL;
1022 /* --------------------------------------------------------------------------------------------- */
1024 #ifdef HAVE_CHARSET
1027 * Check if need cleanup charset converter for vfs_path_element_t
1029 * @param element part of path
1031 * @return TRUE if need cleanup converter or FALSE otherwise
1034 gboolean
1035 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1037 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1040 /* --------------------------------------------------------------------------------------------- */
1042 * Change encoding for last part (vfs_path_element_t) of vpath
1044 * @param vpath pointer to path structure
1045 * encoding name of charset
1047 * @return pointer to path structure (for use function in anoter functions)
1049 vfs_path_t *
1050 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
1052 vfs_path_element_t *path_element;
1054 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1055 /* don't add current encoding */
1056 if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1057 return vpath;
1059 g_free (path_element->encoding);
1060 path_element->encoding = g_strdup (encoding);
1062 if (vfs_path_element_need_cleanup_converter (path_element))
1063 str_close_conv (path_element->dir.converter);
1065 path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1067 g_free (vpath->str);
1068 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1069 return vpath;
1072 #endif
1074 /* --------------------------------------------------------------------------------------------- */
1077 * Serialize vfs_path_t object to string
1079 * @param vpath data for serialization
1080 * @param error contain pointer to object for handle error code and message
1082 * @return serialized vpath as newly allocated string
1085 char *
1086 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
1088 mc_config_t *cpath;
1089 ssize_t element_index;
1090 char *ret_value;
1092 mc_return_val_if_error (mcerror, FALSE);
1094 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1096 mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1097 return NULL;
1100 cpath = mc_config_init (NULL, FALSE);
1102 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1104 char groupname[BUF_TINY];
1105 const vfs_path_element_t *element;
1107 g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1108 element = vfs_path_get_by_index (vpath, element_index);
1109 /* convert one element to config group */
1111 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1112 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1113 #ifdef HAVE_CHARSET
1114 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1115 #endif
1116 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1118 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1119 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1120 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1121 if (element->port != 0)
1122 mc_config_set_int (cpath, groupname, "port", element->port);
1125 ret_value = mc_serialize_config (cpath, mcerror);
1126 mc_config_deinit (cpath);
1127 return ret_value;
1130 /* --------------------------------------------------------------------------------------------- */
1132 * Deserialize string to vfs_path_t object
1134 * @param data data for serialization
1135 * @param error contain pointer to object for handle error code and message
1137 * @return newly allocated vfs_path_t object
1140 vfs_path_t *
1141 vfs_path_deserialize (const char *data, GError ** mcerror)
1143 mc_config_t *cpath;
1144 size_t element_index;
1145 vfs_path_t *vpath;
1147 mc_return_val_if_error (mcerror, FALSE);
1149 cpath = mc_deserialize_config (data, mcerror);
1150 if (cpath == NULL)
1151 return NULL;
1153 vpath = vfs_path_new ();
1155 for (element_index = 0;; element_index++)
1157 struct vfs_class *eclass;
1158 vfs_path_element_t *element;
1159 char *cfg_value;
1160 char groupname[BUF_TINY];
1162 g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1163 if (!mc_config_has_group (cpath, groupname))
1164 break;
1166 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1167 eclass = vfs_get_class_by_name (cfg_value);
1168 if (eclass == NULL)
1170 vfs_path_free (vpath);
1171 g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1172 g_free (cfg_value);
1173 mc_config_deinit (cpath);
1174 return NULL;
1176 g_free (cfg_value);
1178 element = g_new0 (vfs_path_element_t, 1);
1179 element->class = eclass;
1180 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1182 #ifdef HAVE_CHARSET
1183 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1184 element->dir.converter =
1185 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1186 #endif
1188 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1190 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1191 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1192 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1193 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1195 vpath->path = g_array_append_val (vpath->path, element);
1198 mc_config_deinit (cpath);
1199 if (vfs_path_elements_count (vpath) == 0)
1201 vfs_path_free (vpath);
1202 g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1203 return NULL;
1205 vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1207 return vpath;
1210 /* --------------------------------------------------------------------------------------------- */
1212 * Build vfs_path_t object from arguments.
1214 * @param first_element of path
1215 * @param ... path tokens, terminated by NULL
1217 * @return newly allocated vfs_path_t object
1220 vfs_path_t *
1221 vfs_path_build_filename (const char *first_element, ...)
1223 va_list args;
1224 char *str_path;
1225 vfs_path_t *vpath;
1227 if (first_element == NULL)
1228 return NULL;
1230 va_start (args, first_element);
1231 str_path = mc_build_filenamev (first_element, args);
1232 va_end (args);
1233 vpath = vfs_path_from_str (str_path);
1234 g_free (str_path);
1235 return vpath;
1238 /* --------------------------------------------------------------------------------------------- */
1240 * Append tokens to path object
1242 * @param vpath path object
1243 * @param first_element of path
1244 * @param ... NULL-terminated strings
1246 * @return newly allocated path object
1249 vfs_path_t *
1250 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1252 va_list args;
1253 char *str_path;
1254 const char *result_str;
1255 vfs_path_t *ret_vpath;
1257 if (vpath == NULL || first_element == NULL)
1258 return NULL;
1260 va_start (args, first_element);
1261 str_path = mc_build_filenamev (first_element, args);
1262 va_end (args);
1264 result_str = vfs_path_as_str (vpath);
1265 ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1266 g_free (str_path);
1268 return ret_vpath;
1272 /* --------------------------------------------------------------------------------------------- */
1275 * Append vpath_t tokens to path object
1277 * @param first_vpath vpath objects
1278 * @param ... NULL-terminated vpath objects
1280 * @return newly allocated path object
1283 vfs_path_t *
1284 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1286 va_list args;
1287 vfs_path_t *ret_vpath;
1288 const vfs_path_t *current_vpath = first_vpath;
1290 if (first_vpath == NULL)
1291 return NULL;
1293 ret_vpath = vfs_path_new ();
1295 va_start (args, first_vpath);
1298 int vindex;
1300 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1302 vfs_path_element_t *path_element;
1304 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1305 g_array_append_val (ret_vpath->path, path_element);
1307 current_vpath = va_arg (args, const vfs_path_t *);
1309 while (current_vpath != NULL);
1310 va_end (args);
1312 ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1314 return ret_vpath;
1317 /* --------------------------------------------------------------------------------------------- */
1320 * get tockens count in path.
1322 * @param vpath path object
1324 * @return count of tokens
1327 size_t
1328 vfs_path_tokens_count (const vfs_path_t * vpath)
1330 size_t count_tokens = 0;
1331 int element_index;
1333 if (vpath == NULL)
1334 return 0;
1336 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1338 const vfs_path_element_t *element;
1339 const char *token, *prev_token;
1341 element = vfs_path_get_by_index (vpath, element_index);
1343 for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1344 prev_token = token + 1)
1346 /* skip empty substring */
1347 if (token != prev_token)
1348 count_tokens++;
1351 if (*prev_token != '\0')
1352 count_tokens++;
1355 return count_tokens;
1358 /* --------------------------------------------------------------------------------------------- */
1361 * Get subpath by tokens
1363 * @param vpath path object
1364 * @param start_position first token for got/ Started from 0.
1365 * If negative, then position will be relative to end of path
1366 * @param length count of tokens
1368 * @return newly allocated string with path tokens separated by slash
1371 char *
1372 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1374 GString *ret_tokens, *element_tokens;
1375 int element_index;
1376 size_t tokens_count = vfs_path_tokens_count (vpath);
1378 if (vpath == NULL)
1379 return NULL;
1381 if (length == 0)
1382 length = tokens_count;
1384 if (length < 0)
1385 length = tokens_count + length;
1387 if (start_position < 0)
1388 start_position = (ssize_t) tokens_count + start_position;
1390 if (start_position < 0)
1391 return NULL;
1393 if (start_position >= (ssize_t) tokens_count)
1394 return NULL;
1396 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1397 length = tokens_count - start_position;
1399 ret_tokens = g_string_sized_new (32);
1400 element_tokens = g_string_sized_new (32);
1402 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1404 const vfs_path_element_t *element;
1405 char **path_tokens, **iterator;
1407 g_string_assign (element_tokens, "");
1408 element = vfs_path_get_by_index (vpath, element_index);
1409 path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1411 for (iterator = path_tokens; *iterator != NULL; iterator++)
1413 if (**iterator != '\0')
1415 if (start_position == 0)
1417 if (length == 0)
1419 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1420 g_string_free (element_tokens, TRUE);
1421 g_strfreev (path_tokens);
1422 return g_string_free (ret_tokens, FALSE);
1424 length--;
1425 if (element_tokens->len != 0)
1426 g_string_append_c (element_tokens, PATH_SEP);
1427 g_string_append (element_tokens, *iterator);
1429 else
1430 start_position--;
1433 g_strfreev (path_tokens);
1434 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1437 g_string_free (element_tokens, TRUE);
1438 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1441 /* --------------------------------------------------------------------------------------------- */
1443 * Get subpath by tokens
1445 * @param vpath path object
1446 * @param start_position first token for got/ Started from 0.
1447 * If negative, then position will be relative to end of path
1448 * @param length count of tokens
1450 * @return newly allocated path object with path tokens separated by slash
1453 vfs_path_t *
1454 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1456 char *str_tokens;
1457 vfs_path_t *ret_vpath = NULL;
1459 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1460 if (str_tokens != NULL)
1462 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1463 g_free (str_tokens);
1465 return ret_vpath;
1468 /* --------------------------------------------------------------------------------------------- */
1471 * Build URL parameters (such as user:pass @ host:port) from one path element object
1473 * @param element path element
1474 * @param keep_password TRUE or FALSE
1476 * @return newly allocated string
1479 char *
1480 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1482 GString *buffer;
1484 if (element == NULL)
1485 return NULL;
1487 buffer = g_string_new ("");
1489 if (element->user != NULL)
1490 g_string_append (buffer, element->user);
1492 if (element->password != NULL && keep_password)
1494 g_string_append_c (buffer, ':');
1495 g_string_append (buffer, element->password);
1498 if (element->host != NULL)
1500 if ((element->user != NULL) || (element->password != NULL))
1501 g_string_append_c (buffer, '@');
1502 if (element->ipv6)
1503 g_string_append_c (buffer, '[');
1504 g_string_append (buffer, element->host);
1505 if (element->ipv6)
1506 g_string_append_c (buffer, ']');
1509 if ((element->port) != 0 && (element->host != NULL))
1511 g_string_append_c (buffer, ':');
1512 g_string_append_printf (buffer, "%d", element->port);
1515 return g_string_free (buffer, FALSE);
1518 /* --------------------------------------------------------------------------------------------- */
1520 * Build pretty string representation of one path_element_t object
1522 * @param element path element
1524 * @return newly allocated string
1527 char *
1528 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1530 char *url_params;
1531 GString *pretty_path;
1533 pretty_path = g_string_new (element->class->prefix);
1534 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1536 url_params = vfs_path_build_url_params_str (element, FALSE);
1537 g_string_append (pretty_path, url_params);
1538 g_free (url_params);
1540 if (!IS_PATH_SEP (*element->path))
1541 g_string_append_c (pretty_path, PATH_SEP);
1543 g_string_append (pretty_path, element->path);
1544 return g_string_free (pretty_path, FALSE);
1547 /* --------------------------------------------------------------------------------------------- */
1549 * Compare two path objects as strings
1551 * @param vpath1 first path object
1552 * @param vpath2 second vpath object
1554 * @return integer value like to strcmp.
1557 gboolean
1558 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1560 const char *path1, *path2;
1561 gboolean ret_val;
1563 if (vpath1 == NULL || vpath2 == NULL)
1564 return FALSE;
1566 path1 = vfs_path_as_str (vpath1);
1567 path2 = vfs_path_as_str (vpath2);
1569 ret_val = strcmp (path1, path2) == 0;
1571 return ret_val;
1574 /* --------------------------------------------------------------------------------------------- */
1576 * Compare two path objects as strings
1578 * @param vpath1 first path object
1579 * @param vpath2 second vpath object
1580 * @param len number of first 'len' characters
1582 * @return integer value like to strcmp.
1585 gboolean
1586 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1588 const char *path1, *path2;
1589 gboolean ret_val;
1591 if (vpath1 == NULL || vpath2 == NULL)
1592 return FALSE;
1594 path1 = vfs_path_as_str (vpath1);
1595 path2 = vfs_path_as_str (vpath2);
1597 ret_val = strncmp (path1, path2, len) == 0;
1599 return ret_val;
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Calculate path length in string representation
1606 * @param vpath path object
1608 * @return length of path
1611 size_t
1612 vfs_path_len (const vfs_path_t * vpath)
1614 if (vpath == NULL)
1615 return 0;
1617 return strlen (vpath->str);
1620 /* --------------------------------------------------------------------------------------------- */
1622 * Convert relative vpath object to absolute
1624 * @param vpath path object
1626 * @return absolute path object
1629 vfs_path_t *
1630 vfs_path_to_absolute (const vfs_path_t * vpath)
1632 vfs_path_t *absolute_vpath;
1633 const char *path_str;
1635 if (!vpath->relative)
1636 return vfs_path_clone (vpath);
1638 path_str = vfs_path_as_str (vpath);
1639 absolute_vpath = vfs_path_from_str (path_str);
1640 return absolute_vpath;
1643 /* --------------------------------------------------------------------------------------------- */