Changes to handle vfs_path_t object:
[midnight-commander.git] / lib / vfs / path.c
blob252c6c1578c5e6b6b6c5e38084665948118d68bb
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
8 Slava Zanko <slavazanko@gmail.com>, 2011
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /**
27 * \file
28 * \brief Source: Virtual File System: path handlers
29 * \author Slava Zanko
30 * \date 2011
34 #include <config.h>
36 #include "lib/global.h"
37 #include "lib/strutil.h"
38 #include "lib/util.h" /* mc_build_filename() */
39 #include "lib/serialize.h"
41 #include "vfs.h"
42 #include "utilvfs.h"
43 #include "xdirentry.h"
44 #include "path.h"
46 extern GPtrArray *vfs__classes_list;
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 static gboolean
60 path_magic (const char *path)
62 struct stat buf;
64 return (stat (path, &buf) != 0);
67 /* --------------------------------------------------------------------------------------------- */
69 /**
70 * Splits path extracting vfs part.
72 * Splits path
73 * \verbatim /p1#op/inpath \endverbatim
74 * into
75 * \verbatim inpath,op; \endverbatim
76 * returns which vfs it is.
77 * What is left in path is p1. You still want to g_free(path), you DON'T
78 * want to free neither *inpath nor *op
81 static struct vfs_class *
82 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
83 size_t skip_count)
85 char *semi;
86 char *slash;
87 struct vfs_class *ret;
89 if (path == NULL)
90 vfs_die ("Cannot split NULL");
92 semi = strrstr_skip_count (path, "#", skip_count);
94 if ((semi == NULL) || (!path_magic (path)))
95 return NULL;
97 slash = strchr (semi, PATH_SEP);
98 *semi = '\0';
100 if (op != NULL)
101 *op = NULL;
103 if (inpath != NULL)
104 *inpath = NULL;
106 if (slash != NULL)
107 *slash = '\0';
109 ret = vfs_prefix_to_class (semi + 1);
110 if (ret != NULL)
112 if (op != NULL)
113 *op = semi + 1;
114 if (inpath != NULL)
115 *inpath = slash != NULL ? slash + 1 : NULL;
116 return ret;
119 if (slash != NULL)
120 *slash = PATH_SEP;
122 *semi = '#';
123 ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
124 return ret;
127 /* --------------------------------------------------------------------------------------------- */
129 * remove //, /./ and /../
131 * @return newly allocated string
134 static char *
135 vfs_canon (const char *path)
137 if (path == NULL)
138 vfs_die ("Cannot canonicalize NULL");
140 /* Relative to current directory */
141 if (*path != PATH_SEP)
143 char *result, *local;
145 local = tilde_expand (path);
146 if (*local != PATH_SEP)
148 char *curr_dir;
150 g_free (local);
151 curr_dir = vfs_get_current_dir ();
152 local = mc_build_filename (curr_dir, path, NULL);
153 g_free (curr_dir);
156 result = vfs_canon (local);
157 g_free (local);
158 return result;
162 * So we have path of following form:
163 * /p1/p2#op/.././././p3#op/p4. Good luck.
166 char *result;
168 result = g_strdup (path);
169 canonicalize_pathname (result);
170 return result;
174 /* --------------------------------------------------------------------------------------------- */
175 /** get encoding after last #enc: or NULL, if part does not contain #enc:
177 * @param path string
179 * @return newly allocated string.
182 static char *
183 vfs_get_encoding (const char *path)
185 char result[16];
186 char *work;
187 char *semi;
188 char *slash;
189 work = g_strdup (path);
191 /* try found #enc: */
192 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
194 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
196 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
197 slash = strchr (semi, PATH_SEP);
198 if (slash != NULL)
199 slash[0] = '\0';
201 g_strlcpy (result, semi, sizeof (result));
202 g_free (work);
203 return g_strdup (result);
205 else
207 g_free (work);
208 return NULL;
212 /* --------------------------------------------------------------------------------------------- */
213 /** Extract the hostname and username from the path
215 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
217 * ftp://sunsite.unc.edu/pub/linux
218 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
219 * ftp://tsx-11.mit.edu:8192/
220 * ftp://joe@foo.edu:11321/private
221 * ftp://joe:password@foo.se
223 * @param path_element is an input string to be parsed
224 * @param path is an input string to be parsed
226 * @return g_malloc()ed url info.
227 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
228 * is not set, then the current login name is supplied.
229 * Return value is a g_malloc()ed structure with the pathname relative to the
230 * host.
233 static void
234 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
236 char *pcopy;
237 const char *pend;
238 char *dir, *colon, *inner_colon, *at, *rest;
240 path_element->port = 0;
242 pcopy = g_strdup (path);
243 pend = pcopy + strlen (pcopy);
244 dir = pcopy;
246 /* search for any possible user */
247 at = strrchr (pcopy, '@');
249 /* We have a username */
250 if (at == NULL)
251 rest = pcopy;
252 else
254 *at = '\0';
255 inner_colon = strchr (pcopy, ':');
256 if (inner_colon != NULL)
258 *inner_colon = '\0';
259 inner_colon++;
260 path_element->password = g_strdup (inner_colon);
263 if (*pcopy != '\0')
264 path_element->user = g_strdup (pcopy);
266 if (pend == at + 1)
267 rest = at;
268 else
269 rest = at + 1;
272 /* Check if the host comes with a port spec, if so, chop it */
273 if (*rest != '[')
274 colon = strchr (rest, ':');
275 else
277 colon = strchr (++rest, ']');
278 if (colon != NULL)
280 colon[0] = '\0';
281 colon[1] = '\0';
282 colon++;
283 path_element->ipv6 = TRUE;
287 if (colon != NULL)
289 *colon = '\0';
290 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
292 if (path_element->port <= 0 || path_element->port >= 65536)
293 path_element->port = 0;
295 else
296 while (*(++colon) != '\0')
298 switch (*colon)
300 case 'C':
301 path_element->port = 1;
302 break;
303 case 'r':
304 path_element->port = 2;
305 break;
309 path_element->host = g_strdup (rest);
310 g_free (pcopy);
313 /* --------------------------------------------------------------------------------------------- */
315 * get VFS class for the given name
317 * @param class_name name of class
319 * @return pointer to class structure or NULL if class not found
322 static struct vfs_class *
323 vfs_get_class_by_name (const char *class_name)
325 guint i;
327 if (class_name == NULL)
328 return NULL;
330 for (i = 0; i < vfs__classes_list->len; i++)
332 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
333 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
334 return vfs;
337 return NULL;
340 /* --------------------------------------------------------------------------------------------- */
342 * Check if path string contain URL-like elements
344 * @param path_str path
346 * @return TRUE if path is deprecated or FALSE otherwise
349 static gboolean
350 vfs_path_is_str_path_deprecated (const char *path_str)
352 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
355 /* --------------------------------------------------------------------------------------------- */
356 /** Split path string to path elements by deprecated algorithm.
358 * @param path_str VFS-path
360 * @return pointer to newly created vfs_path_t object with filled path elements array.
363 static vfs_path_t *
364 vfs_path_from_str_deprecated_parser (char *path)
366 vfs_path_t *vpath;
367 vfs_path_element_t *element;
368 struct vfs_class *class;
369 const char *local, *op;
371 vpath = vfs_path_new ();
373 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
375 char *url_params;
376 element = g_new0 (vfs_path_element_t, 1);
377 element->class = class;
378 if (local == NULL)
379 local = "";
380 element->path = vfs_translate_path_n (local);
382 element->encoding = vfs_get_encoding (local);
383 element->dir.converter =
384 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
386 url_params = strchr (op, ':'); /* skip VFS prefix */
387 if (url_params != NULL)
389 *url_params = '\0';
390 url_params++;
391 vfs_path_url_split (element, url_params);
394 if (*op != '\0')
395 element->vfs_prefix = g_strdup (op);
397 g_array_prepend_val (vpath->path, element);
399 if (path[0] != '\0')
401 element = g_new0 (vfs_path_element_t, 1);
402 element->class = g_ptr_array_index (vfs__classes_list, 0);
403 element->path = vfs_translate_path_n (path);
405 element->encoding = vfs_get_encoding (path);
406 element->dir.converter =
407 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
408 g_array_prepend_val (vpath->path, element);
411 return vpath;
414 /* --------------------------------------------------------------------------------------------- */
415 /** Split path string to path elements by URL algorithm.
417 * @param path_str VFS-path
419 * @return pointer to newly created vfs_path_t object with filled path elements array.
422 static vfs_path_t *
423 vfs_path_from_str_uri_parser (char *path)
425 vfs_path_t *vpath;
426 vfs_path_element_t *element;
428 char *url_delimiter;
430 vpath = vfs_path_new ();
432 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
434 char *vfs_prefix_start;
435 char *real_vfs_prefix_start = url_delimiter;
436 char *slash_pointer;
437 struct vfs_s_subclass *sub = NULL;
439 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
440 real_vfs_prefix_start--;
441 vfs_prefix_start = real_vfs_prefix_start;
443 if (*(vfs_prefix_start) == PATH_SEP)
444 vfs_prefix_start += 1;
446 *url_delimiter = '\0';
448 element = g_new0 (vfs_path_element_t, 1);
449 element->class = vfs_prefix_to_class (vfs_prefix_start);
450 element->vfs_prefix = g_strdup (vfs_prefix_start);
452 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
453 sub = VFSDATA (element);
454 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
456 slash_pointer = strchr (url_delimiter, PATH_SEP);
457 if (slash_pointer == NULL)
459 element->path = g_strdup ("");
461 else
463 element->path = vfs_translate_path_n (slash_pointer + 1);
464 element->encoding = vfs_get_encoding (slash_pointer);
466 *slash_pointer = '\0';
468 vfs_path_url_split (element, url_delimiter);
470 else
472 element->path = vfs_translate_path_n (url_delimiter);
473 element->encoding = vfs_get_encoding (url_delimiter);
475 element->dir.converter =
476 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
477 g_array_prepend_val (vpath->path, element);
479 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
480 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
481 *real_vfs_prefix_start = '\0';
482 else
483 *(real_vfs_prefix_start + 1) = '\0';
486 if (path[0] != '\0')
488 element = g_new0 (vfs_path_element_t, 1);
489 element->class = g_ptr_array_index (vfs__classes_list, 0);
490 element->path = vfs_translate_path_n (path);
491 element->encoding = vfs_get_encoding (path);
492 element->dir.converter =
493 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
494 g_array_prepend_val (vpath->path, element);
497 return vpath;
500 /* --------------------------------------------------------------------------------------------- */
502 * Add element's class info to result string (such as VFS name, host, encoding etc)
503 * This function used as helper only in vfs_path_tokens_get() function
505 * @param element current path element
506 * @param ret_tokens total tikens for return
507 * @param element_tokens accumulated element-only tokens
510 static void
511 vfs_path_tokens_add_class_info (vfs_path_element_t * element, GString * ret_tokens,
512 GString * element_tokens)
514 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
515 && element_tokens->len > 0)
517 char *url_str;
519 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
520 g_string_append_c (ret_tokens, PATH_SEP);
522 g_string_append (ret_tokens, element->vfs_prefix);
523 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
525 url_str = vfs_path_build_url_params_str (element, TRUE);
526 if (*url_str != '\0')
528 g_string_append (ret_tokens, url_str);
529 g_string_append_c (ret_tokens, PATH_SEP);
532 g_free (url_str);
534 if (element->encoding != NULL)
536 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
537 g_string_append (ret_tokens, PATH_SEP_STR);
538 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
539 g_string_append (ret_tokens, element->encoding);
540 g_string_append (ret_tokens, PATH_SEP_STR);
543 g_string_append (ret_tokens, element_tokens->str);
546 /* --------------------------------------------------------------------------------------------- */
548 * Strip path to home dir.
549 * @param dir pointer to string contains full path
552 static char *
553 vfs_path_strip_home (const char *dir)
555 const char *home_dir = mc_config_get_home_dir ();
557 if (home_dir != NULL)
559 size_t len;
561 len = strlen (home_dir);
563 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
564 return g_strdup_printf ("~%s", dir + len);
567 return g_strdup (dir);
570 /* --------------------------------------------------------------------------------------------- */
572 /* --------------------------------------------------------------------------------------------- */
573 /*** public functions ****************************************************************************/
574 /* --------------------------------------------------------------------------------------------- */
576 * Convert first elements_count elements from vfs_path_t to string representation with flags.
578 * @param vpath pointer to vfs_path_t object
579 * @param elements_count count of first elements for convert
580 * @param flags flags for converter
582 * @return pointer to newly created string.
585 #define vfs_append_from_path(appendfrom) \
587 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
589 char *stripped_home_str; \
590 stripped_home_str = vfs_path_strip_home (appendfrom); \
591 g_string_append (buffer, stripped_home_str); \
592 g_free (stripped_home_str); \
594 else \
596 if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
597 && (buffer->str[buffer->len - 1] != PATH_SEP)) \
598 g_string_append_c (buffer, PATH_SEP); \
599 g_string_append (buffer, appendfrom); \
603 char *
604 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
606 int element_index;
607 GString *buffer;
608 GString *recode_buffer;
610 if (vpath == NULL)
611 return NULL;
613 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
614 elements_count = vfs_path_elements_count (vpath);
616 if (elements_count < 0)
617 elements_count = vfs_path_elements_count (vpath) + elements_count;
619 buffer = g_string_new ("");
620 recode_buffer = g_string_new ("");
622 for (element_index = 0; element_index < elements_count; element_index++)
624 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
626 if (element->vfs_prefix != NULL)
628 char *url_str;
630 if (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)
631 g_string_append_c (buffer, PATH_SEP);
633 g_string_append (buffer, element->vfs_prefix);
634 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
636 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
638 if (*url_str != '\0')
639 g_string_append (buffer, url_str);
641 g_free (url_str);
644 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
646 if ((flags & VPF_HIDE_CHARSET) == 0)
648 if (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)
649 g_string_append (buffer, PATH_SEP_STR);
650 g_string_append (buffer, VFS_ENCODING_PREFIX);
651 g_string_append (buffer, element->encoding);
653 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
654 vfs_append_from_path (recode_buffer->str);
655 g_string_set_size (recode_buffer, 0);
657 else
659 vfs_append_from_path (element->path);
662 g_string_free (recode_buffer, TRUE);
663 return g_string_free (buffer, FALSE);
666 #undef vfs_append_from_path
668 /* --------------------------------------------------------------------------------------------- */
670 * Convert first elements_count elements from vfs_path_t to string representation.
672 * @param vpath pointer to vfs_path_t object
673 * @param elements_count count of first elements for convert
675 * @return pointer to newly created string.
678 char *
679 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
681 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
684 /* --------------------------------------------------------------------------------------------- */
686 * Convert vfs_path_t to string representation.
688 * @param vpath pointer to vfs_path_t object
690 * @return pointer to newly created string.
693 char *
694 vfs_path_to_str (const vfs_path_t * vpath)
696 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
699 /* --------------------------------------------------------------------------------------------- */
701 * Split path string to path elements with flags for change parce process.
703 * @param path_str VFS-path
704 * @param flags flags for parser
706 * @return pointer to newly created vfs_path_t object with filled path elements array.
709 vfs_path_t *
710 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
712 vfs_path_t *vpath;
713 char *path;
715 if (path_str == NULL)
716 return NULL;
718 if ((flags & VPF_NO_CANON) == 0)
719 path = vfs_canon (path_str);
720 else
721 path = g_strdup (path_str);
723 if (path == NULL)
724 return NULL;
726 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
727 vpath = vfs_path_from_str_deprecated_parser (path);
728 else
729 vpath = vfs_path_from_str_uri_parser (path);
731 g_free (path);
733 return vpath;
736 /* --------------------------------------------------------------------------------------------- */
738 * Split path string to path elements.
740 * @param path_str VFS-path
742 * @return pointer to newly created vfs_path_t object with filled path elements array.
745 vfs_path_t *
746 vfs_path_from_str (const char *path_str)
748 return vfs_path_from_str_flags (path_str, VPF_NONE);
751 /* --------------------------------------------------------------------------------------------- */
753 * Create new vfs_path_t object.
755 * @return pointer to newly created vfs_path_t object.
758 vfs_path_t *
759 vfs_path_new (void)
761 vfs_path_t *vpath;
763 vpath = g_new0 (vfs_path_t, 1);
764 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
766 return vpath;
769 /* --------------------------------------------------------------------------------------------- */
771 * Get count of path elements.
773 * @param vpath pointer to vfs_path_t object
775 * @return count of path elements.
779 vfs_path_elements_count (const vfs_path_t * vpath)
781 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
784 /* --------------------------------------------------------------------------------------------- */
786 * Add vfs_path_element_t object to end of list in vfs_path_t object
787 * @param vpath pointer to vfs_path_t object
788 * @param path_element pointer to vfs_path_element_t object
791 void
792 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
794 g_array_append_val (vpath->path, path_element);
797 /* --------------------------------------------------------------------------------------------- */
799 * Get one path element by index.
801 * @param vpath pointer to vfs_path_t object
802 * @param element_index element index. May have negative value (in this case count was started at the end of list).
804 * @return path element.
807 vfs_path_element_t *
808 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
810 if (element_index < 0)
811 element_index += vfs_path_elements_count (vpath);
813 if (element_index < 0)
814 vfs_die ("vfs_path_get_by_index: incorrect index!");
816 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
819 /* --------------------------------------------------------------------------------------------- */
821 * Clone one path element
823 * @param element pointer to vfs_path_element_t object
825 * @return Newly allocated path element
828 vfs_path_element_t *
829 vfs_path_element_clone (const vfs_path_element_t * element)
831 vfs_path_element_t *new_element = g_new0 (vfs_path_element_t, 1);
832 memcpy (new_element, element, sizeof (vfs_path_element_t));
834 new_element->user = g_strdup (element->user);
835 new_element->password = g_strdup (element->password);
836 new_element->host = g_strdup (element->host);
837 new_element->path = g_strdup (element->path);
838 new_element->encoding = g_strdup (element->encoding);
839 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
840 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
841 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
843 return new_element;
846 /* --------------------------------------------------------------------------------------------- */
848 * Free one path element.
850 * @param element pointer to vfs_path_element_t object
854 void
855 vfs_path_element_free (vfs_path_element_t * element)
857 if (element == NULL)
858 return;
860 g_free (element->user);
861 g_free (element->password);
862 g_free (element->host);
863 g_free (element->path);
864 g_free (element->encoding);
865 g_free (element->vfs_prefix);
867 if (vfs_path_element_need_cleanup_converter (element))
869 str_close_conv (element->dir.converter);
872 g_free (element);
875 /* --------------------------------------------------------------------------------------------- */
877 * Clone path
879 * @param vpath pointer to vfs_path_t object
881 * @return Newly allocated path object
884 vfs_path_t *
885 vfs_path_clone (const vfs_path_t * vpath)
887 vfs_path_t *new_vpath;
888 int vpath_element_index;
890 if (vpath == NULL)
891 return NULL;
893 new_vpath = vfs_path_new ();
895 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
896 vpath_element_index++)
898 vfs_path_element_t *path_element;
900 path_element =
901 vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
902 g_array_append_val (new_vpath->path, path_element);
905 return new_vpath;
908 /* --------------------------------------------------------------------------------------------- */
910 * Free vfs_path_t object.
912 * @param vpath pointer to vfs_path_t object
916 void
917 vfs_path_free (vfs_path_t * vpath)
919 int vpath_element_index;
921 if (vpath == NULL)
922 return;
924 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
925 vpath_element_index++)
926 vfs_path_element_free (vfs_path_get_by_index (vpath, vpath_element_index));
928 g_array_free (vpath->path, TRUE);
929 g_free (vpath);
932 /* --------------------------------------------------------------------------------------------- */
934 * Remove one path element by index
936 * @param vpath pointer to vfs_path_t object
937 * @param element_index element index. May have negative value (in this case count was started at the end of list).
941 void
942 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
944 vfs_path_element_t *element;
946 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
947 return;
949 if (element_index < 0)
950 element_index = vfs_path_elements_count (vpath) + element_index;
952 element = vfs_path_get_by_index (vpath, element_index);
953 vpath->path = g_array_remove_index (vpath->path, element_index);
954 vfs_path_element_free (element);
957 /* --------------------------------------------------------------------------------------------- */
958 /** Return VFS class for the given prefix */
960 struct vfs_class *
961 vfs_prefix_to_class (const char *prefix)
963 guint i;
965 /* Avoid first class (localfs) that would accept any prefix */
966 for (i = 1; i < vfs__classes_list->len; i++)
968 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
969 if (vfs->which != NULL)
971 if (vfs->which (vfs, prefix) == -1)
972 continue;
973 return vfs;
976 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
977 return vfs;
980 return NULL;
983 /* --------------------------------------------------------------------------------------------- */
985 * Check if need cleanup charset converter for vfs_path_element_t
987 * @param element part of path
989 * @return TRUE if need cleanup converter or FALSE otherwise
992 gboolean
993 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
995 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
998 /* --------------------------------------------------------------------------------------------- */
1000 * Serialize vfs_path_t object to string
1002 * @param vpath data for serialization
1003 * @param error contain pointer to object for handle error code and message
1005 * @return serialized vpath as newly allocated string
1008 char *
1009 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1011 mc_config_t *cpath = mc_config_init (NULL);
1012 ssize_t element_index;
1013 char *ret_value;
1015 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1017 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1018 return NULL;
1021 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1023 char *groupname = g_strdup_printf ("path-element-%zd", element_index);
1024 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
1026 /* convert one element to config group */
1028 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1029 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1030 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1032 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1034 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1035 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1036 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1037 if (element->port != 0)
1038 mc_config_set_int (cpath, groupname, "port", element->port);
1040 g_free (groupname);
1043 ret_value = mc_serialize_config (cpath, error);
1044 mc_config_deinit (cpath);
1045 return ret_value;
1048 /* --------------------------------------------------------------------------------------------- */
1050 * Deserialize string to vfs_path_t object
1052 * @param data data for serialization
1053 * @param error contain pointer to object for handle error code and message
1055 * @return newly allocated vfs_path_t object
1058 vfs_path_t *
1059 vfs_path_deserialize (const char *data, GError ** error)
1061 mc_config_t *cpath = mc_deserialize_config (data, error);
1062 size_t element_index = 0;
1063 vfs_path_t *vpath;
1065 if (cpath == NULL)
1066 return NULL;
1068 vpath = vfs_path_new ();
1070 while (TRUE)
1072 vfs_path_element_t *element;
1073 char *cfg_value;
1074 char *groupname;
1076 groupname = g_strdup_printf ("path-element-%zd", element_index);
1077 if (!mc_config_has_group (cpath, groupname))
1079 g_free (groupname);
1080 break;
1083 element = g_new0 (vfs_path_element_t, 1);
1085 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1086 element->class = vfs_get_class_by_name (cfg_value);
1087 if (element->class == NULL)
1089 g_free (element);
1090 vfs_path_free (vpath);
1091 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1092 g_free (cfg_value);
1093 mc_config_deinit (cpath);
1094 return NULL;
1096 g_free (cfg_value);
1098 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1099 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1100 element->dir.converter =
1101 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1103 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1105 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1106 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1107 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1108 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1110 vpath->path = g_array_append_val (vpath->path, element);
1112 g_free (groupname);
1113 element_index++;
1116 mc_config_deinit (cpath);
1117 if (vfs_path_elements_count (vpath) == 0)
1119 vfs_path_free (vpath);
1120 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1121 return NULL;
1124 return vpath;
1127 /* --------------------------------------------------------------------------------------------- */
1129 * Build vfs_path_t object from arguments.
1131 * @param ... path tokens, terminated by NULL
1133 * @return newly allocated vfs_path_t object
1136 vfs_path_t *
1137 vfs_path_build_filename (const char *first_element, ...)
1139 va_list args;
1140 char *str_path;
1141 vfs_path_t *vpath;
1143 if (first_element == NULL)
1144 return NULL;
1146 va_start (args, first_element);
1147 str_path = mc_build_filenamev (first_element, args);
1148 va_end (args);
1149 vpath = vfs_path_from_str (str_path);
1150 g_free (str_path);
1151 return vpath;
1154 /* --------------------------------------------------------------------------------------------- */
1156 * Append tokens to path object
1158 * @param vpath path object
1159 * @param ... NULL-terminated strings
1161 * @return newly allocated path object
1164 vfs_path_t *
1165 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1167 va_list args;
1168 char *str_path, *result_str;
1169 vfs_path_t *ret_vpath;
1171 if (vpath == NULL || first_element == NULL)
1172 return NULL;
1174 va_start (args, first_element);
1175 str_path = mc_build_filenamev (first_element, args);
1176 va_end (args);
1178 result_str = vfs_path_to_str (vpath);
1179 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1180 g_free (result_str);
1181 g_free (str_path);
1183 return ret_vpath;
1187 /* --------------------------------------------------------------------------------------------- */
1190 * Append vpath_t tokens to path object
1192 * @param ... NULL-terminated vpath objects
1194 * @return newly allocated path object
1197 vfs_path_t *
1198 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1200 va_list args;
1201 vfs_path_t *ret_vpath;
1202 const vfs_path_t *current_vpath = first_vpath;
1204 if (first_vpath == NULL)
1205 return NULL;
1207 ret_vpath = vfs_path_new ();
1209 va_start (args, first_vpath);
1212 int vindex;
1214 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1216 vfs_path_element_t *path_element;
1218 path_element =
1219 vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1220 g_array_append_val (ret_vpath->path, path_element);
1222 current_vpath = va_arg (args, const vfs_path_t *);
1224 while (current_vpath != NULL);
1225 va_end (args);
1227 return ret_vpath;
1230 /* --------------------------------------------------------------------------------------------- */
1232 * get tockens count in path.
1234 * @param vpath path object
1236 * @return count of tokens
1239 size_t
1240 vfs_path_tokens_count (const vfs_path_t * vpath)
1242 size_t count_tokens = 0;
1243 int element_index;
1245 if (vpath == NULL)
1246 return 0;
1248 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1250 vfs_path_element_t *element;
1251 char **path_tokens, **iterator;
1253 element = vfs_path_get_by_index (vpath, element_index);
1254 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1256 while (*iterator != NULL)
1258 if (**iterator != '\0')
1259 count_tokens++;
1260 iterator++;
1262 g_strfreev (path_tokens);
1264 return count_tokens;
1267 /* --------------------------------------------------------------------------------------------- */
1269 * Get subpath by tokens
1271 * @param vpath path object
1272 * @param start_position first token for got/ Started from 0.
1273 * If negative, then position will be relative to end of path
1274 * @param length count of tokens
1276 * @return newly allocated string with path tokens separated by slash
1279 char *
1280 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1282 GString *ret_tokens, *element_tokens;
1283 int element_index;
1284 size_t tokens_count = vfs_path_tokens_count (vpath);
1286 if (vpath == NULL)
1287 return NULL;
1289 if (length == 0)
1290 length = tokens_count;
1292 if (length < 0)
1293 length = tokens_count + length;
1295 if (start_position < 0)
1296 start_position = (ssize_t) tokens_count + start_position;
1298 if (start_position < 0)
1299 return NULL;
1301 if (start_position >= (ssize_t) tokens_count)
1302 return NULL;
1304 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1305 length = tokens_count - start_position;
1307 ret_tokens = g_string_sized_new (32);
1308 element_tokens = g_string_sized_new (32);
1310 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1312 vfs_path_element_t *element;
1313 char **path_tokens, **iterator;
1315 g_string_assign (element_tokens, "");
1316 element = vfs_path_get_by_index (vpath, element_index);
1317 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1319 while (*iterator != NULL)
1321 if (**iterator != '\0')
1323 if (start_position == 0)
1325 if (length == 0)
1327 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1328 g_string_free (element_tokens, TRUE);
1329 g_strfreev (path_tokens);
1330 return g_string_free (ret_tokens, FALSE);
1332 length--;
1333 if (element_tokens->len != 0)
1334 g_string_append_c (element_tokens, PATH_SEP);
1335 g_string_append (element_tokens, *iterator);
1337 else
1338 start_position--;
1340 iterator++;
1342 g_strfreev (path_tokens);
1343 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1346 g_string_free (element_tokens, TRUE);
1347 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1350 /* --------------------------------------------------------------------------------------------- */
1352 * Get subpath by tokens
1354 * @param vpath path object
1355 * @param start_position first token for got/ Started from 0.
1356 * If negative, then position will be relative to end of path
1357 * @param length count of tokens
1359 * @return newly allocated path object with path tokens separated by slash
1362 vfs_path_t *
1363 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1365 char *str_tokens;
1366 vfs_path_t *ret_vpath = NULL;
1368 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1369 if (str_tokens != NULL)
1371 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1372 g_free (str_tokens);
1374 return ret_vpath;
1377 /* --------------------------------------------------------------------------------------------- */
1379 * Build URL parameters (such as user:pass@host:port) from one path element object
1381 * @param element path element
1383 * @return newly allocated string
1386 char *
1387 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1389 GString *buffer;
1391 if (element == NULL)
1392 return NULL;
1394 buffer = g_string_new ("");
1396 if (element->user != NULL)
1397 g_string_append (buffer, element->user);
1399 if (element->password != NULL && keep_password)
1401 g_string_append_c (buffer, ':');
1402 g_string_append (buffer, element->password);
1405 if (element->host != NULL)
1407 if ((element->user != NULL) || (element->password != NULL))
1408 g_string_append_c (buffer, '@');
1409 if (element->ipv6)
1410 g_string_append_c (buffer, '[');
1411 g_string_append (buffer, element->host);
1412 if (element->ipv6)
1413 g_string_append_c (buffer, ']');
1416 if ((element->port) != 0 && (element->host != NULL))
1418 g_string_append_c (buffer, ':');
1419 g_string_append_printf (buffer, "%d", element->port);
1422 return g_string_free (buffer, FALSE);
1425 /* --------------------------------------------------------------------------------------------- */
1427 * Compare two path objects as strings
1429 * @param vpath1 first path object
1430 * @param vpath2 second vpath object
1432 * @return integer value like to strcmp.
1436 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1438 char *path1;
1439 char *path2;
1440 int ret_val;
1442 if (vpath1 == NULL || vpath2 == NULL)
1443 return -1;
1445 path1 = vfs_path_to_str (vpath1);
1446 path2 = vfs_path_to_str (vpath2);
1448 ret_val = strcmp (path1, path2);
1450 g_free (path1);
1451 g_free (path2);
1453 return ret_val;
1456 /* --------------------------------------------------------------------------------------------- */
1458 * Compare two path objects as strings
1460 * @param vpath1 first path object
1461 * @param vpath2 second vpath object
1462 * @param len number of first 'len' characters
1464 * @return integer value like to strcmp.
1468 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1470 char *path1;
1471 char *path2;
1472 int ret_val;
1474 if (vpath1 == NULL || vpath2 == NULL)
1475 return -1;
1477 path1 = vfs_path_to_str (vpath1);
1478 path2 = vfs_path_to_str (vpath2);
1480 ret_val = strncmp (path1, path2, len);
1482 g_free (path1);
1483 g_free (path2);
1485 return ret_val;
1488 /* --------------------------------------------------------------------------------------------- */
1490 * Calculate path length in string representation
1492 * @param vpath path object
1494 * @return length of path
1497 size_t
1498 vfs_path_len (const vfs_path_t * vpath)
1500 char *path;
1501 size_t ret_val;
1503 if (vpath == NULL)
1504 return 0;
1506 path = vfs_path_to_str (vpath);
1507 ret_val = strlen (path);
1508 g_free (path);
1509 return ret_val;
1512 /* --------------------------------------------------------------------------------------------- */