Ticket #2827: tweak and cleanup of code in case of --disable-charset option usage.
[midnight-commander.git] / lib / vfs / path.c
blob26aefd8e05c3fcfa84161cbf560234a5f5f61cb8
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 /* --------------------------------------------------------------------------------------------- */
176 #ifdef HAVE_CHARSET
177 /** get encoding after last #enc: or NULL, if part does not contain #enc:
179 * @param path string
181 * @return newly allocated string.
184 static char *
185 vfs_get_encoding (const char *path)
187 char result[16];
188 char *work;
189 char *semi;
190 char *slash;
191 work = g_strdup (path);
193 /* try found #enc: */
194 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
196 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
198 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
199 slash = strchr (semi, PATH_SEP);
200 if (slash != NULL)
201 slash[0] = '\0';
203 g_strlcpy (result, semi, sizeof (result));
204 g_free (work);
205 return g_strdup (result);
207 else
209 g_free (work);
210 return NULL;
213 #endif
215 /* --------------------------------------------------------------------------------------------- */
216 /** Extract the hostname and username from the path
218 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
220 * ftp://sunsite.unc.edu/pub/linux
221 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
222 * ftp://tsx-11.mit.edu:8192/
223 * ftp://joe@foo.edu:11321/private
224 * ftp://joe:password@foo.se
226 * @param path_element is an input string to be parsed
227 * @param path is an input string to be parsed
229 * @return g_malloc()ed url info.
230 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
231 * is not set, then the current login name is supplied.
232 * Return value is a g_malloc()ed structure with the pathname relative to the
233 * host.
236 static void
237 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
239 char *pcopy;
240 const char *pend;
241 char *dir, *colon, *inner_colon, *at, *rest;
243 path_element->port = 0;
245 pcopy = g_strdup (path);
246 pend = pcopy + strlen (pcopy);
247 dir = pcopy;
249 /* search for any possible user */
250 at = strrchr (pcopy, '@');
252 /* We have a username */
253 if (at == NULL)
254 rest = pcopy;
255 else
257 *at = '\0';
258 inner_colon = strchr (pcopy, ':');
259 if (inner_colon != NULL)
261 *inner_colon = '\0';
262 inner_colon++;
263 path_element->password = g_strdup (inner_colon);
266 if (*pcopy != '\0')
267 path_element->user = g_strdup (pcopy);
269 if (pend == at + 1)
270 rest = at;
271 else
272 rest = at + 1;
275 /* Check if the host comes with a port spec, if so, chop it */
276 if (*rest != '[')
277 colon = strchr (rest, ':');
278 else
280 colon = strchr (++rest, ']');
281 if (colon != NULL)
283 colon[0] = '\0';
284 colon[1] = '\0';
285 colon++;
286 path_element->ipv6 = TRUE;
290 if (colon != NULL)
292 *colon = '\0';
293 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
295 if (path_element->port <= 0 || path_element->port >= 65536)
296 path_element->port = 0;
298 else
299 while (*(++colon) != '\0')
301 switch (*colon)
303 case 'C':
304 path_element->port = 1;
305 break;
306 case 'r':
307 path_element->port = 2;
308 break;
312 path_element->host = g_strdup (rest);
313 g_free (pcopy);
316 /* --------------------------------------------------------------------------------------------- */
318 * get VFS class for the given name
320 * @param class_name name of class
322 * @return pointer to class structure or NULL if class not found
325 static struct vfs_class *
326 vfs_get_class_by_name (const char *class_name)
328 guint i;
330 if (class_name == NULL)
331 return NULL;
333 for (i = 0; i < vfs__classes_list->len; i++)
335 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
336 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
337 return vfs;
340 return NULL;
343 /* --------------------------------------------------------------------------------------------- */
345 * Check if path string contain URL-like elements
347 * @param path_str path
349 * @return TRUE if path is deprecated or FALSE otherwise
352 static gboolean
353 vfs_path_is_str_path_deprecated (const char *path_str)
355 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
358 /* --------------------------------------------------------------------------------------------- */
359 /** Split path string to path elements by deprecated algorithm.
361 * @param path_str VFS-path
363 * @return pointer to newly created vfs_path_t object with filled path elements array.
366 static vfs_path_t *
367 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
369 vfs_path_t *vpath;
370 vfs_path_element_t *element;
371 struct vfs_class *class;
372 const char *local, *op;
374 (void) flags;
375 vpath = vfs_path_new ();
377 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
379 char *url_params;
380 element = g_new0 (vfs_path_element_t, 1);
381 element->class = class;
382 if (local == NULL)
383 local = "";
384 element->path = vfs_translate_path_n (local);
386 #ifdef HAVE_CHARSET
387 element->encoding = vfs_get_encoding (local);
388 element->dir.converter =
389 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
390 #endif
392 url_params = strchr (op, ':'); /* skip VFS prefix */
393 if (url_params != NULL)
395 *url_params = '\0';
396 url_params++;
397 vfs_path_url_split (element, url_params);
400 if (*op != '\0')
401 element->vfs_prefix = g_strdup (op);
403 g_array_prepend_val (vpath->path, element);
405 if (path[0] != '\0')
407 element = g_new0 (vfs_path_element_t, 1);
408 element->class = g_ptr_array_index (vfs__classes_list, 0);
409 element->path = vfs_translate_path_n (path);
411 #ifdef HAVE_CHARSET
412 element->encoding = vfs_get_encoding (path);
413 element->dir.converter =
414 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
415 #endif
416 g_array_prepend_val (vpath->path, element);
419 return vpath;
422 /* --------------------------------------------------------------------------------------------- */
423 /** Split path string to path elements by URL algorithm.
425 * @param path_str VFS-path
427 * @return pointer to newly created vfs_path_t object with filled path elements array.
430 static vfs_path_t *
431 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
433 vfs_path_t *vpath;
434 vfs_path_element_t *element;
436 char *url_delimiter;
438 vpath = vfs_path_new ();
439 vpath->relative = (flags & VPF_NO_CANON) != 0;
441 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
443 char *vfs_prefix_start;
444 char *real_vfs_prefix_start = url_delimiter;
445 char *slash_pointer;
446 struct vfs_s_subclass *sub = NULL;
448 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
449 real_vfs_prefix_start--;
450 vfs_prefix_start = real_vfs_prefix_start;
452 if (*(vfs_prefix_start) == PATH_SEP)
453 vfs_prefix_start += 1;
455 *url_delimiter = '\0';
457 element = g_new0 (vfs_path_element_t, 1);
458 element->class = vfs_prefix_to_class (vfs_prefix_start);
459 element->vfs_prefix = g_strdup (vfs_prefix_start);
461 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
462 sub = VFSDATA (element);
463 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
465 slash_pointer = strchr (url_delimiter, PATH_SEP);
466 if (slash_pointer == NULL)
468 element->path = g_strdup ("");
470 else
472 element->path = vfs_translate_path_n (slash_pointer + 1);
473 #ifdef HAVE_CHARSET
474 element->encoding = vfs_get_encoding (slash_pointer);
475 #endif
476 *slash_pointer = '\0';
478 vfs_path_url_split (element, url_delimiter);
480 else
482 element->path = vfs_translate_path_n (url_delimiter);
483 #ifdef HAVE_CHARSET
484 element->encoding = vfs_get_encoding (url_delimiter);
485 #endif
487 #ifdef HAVE_CHARSET
488 element->dir.converter =
489 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
490 #endif
491 g_array_prepend_val (vpath->path, element);
493 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
494 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
495 *real_vfs_prefix_start = '\0';
496 else
497 *(real_vfs_prefix_start + 1) = '\0';
500 if (path[0] != '\0')
502 element = g_new0 (vfs_path_element_t, 1);
503 element->class = g_ptr_array_index (vfs__classes_list, 0);
504 element->path = vfs_translate_path_n (path);
505 #ifdef HAVE_CHARSET
506 element->encoding = vfs_get_encoding (path);
507 element->dir.converter =
508 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
509 #endif
510 g_array_prepend_val (vpath->path, element);
513 return vpath;
516 /* --------------------------------------------------------------------------------------------- */
518 * Add element's class info to result string (such as VFS name, host, encoding etc)
519 * This function used as helper only in vfs_path_tokens_get() function
521 * @param element current path element
522 * @param ret_tokens total tikens for return
523 * @param element_tokens accumulated element-only tokens
526 static void
527 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
528 GString * element_tokens)
530 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
531 && element_tokens->len > 0)
533 char *url_str;
535 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
536 g_string_append_c (ret_tokens, PATH_SEP);
538 g_string_append (ret_tokens, element->vfs_prefix);
539 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
541 url_str = vfs_path_build_url_params_str (element, TRUE);
542 if (*url_str != '\0')
544 g_string_append (ret_tokens, url_str);
545 g_string_append_c (ret_tokens, PATH_SEP);
548 g_free (url_str);
551 #ifdef HAVE_CHARSET
552 if (element->encoding != NULL)
554 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
555 g_string_append (ret_tokens, PATH_SEP_STR);
556 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
557 g_string_append (ret_tokens, element->encoding);
558 g_string_append (ret_tokens, PATH_SEP_STR);
560 #endif
562 g_string_append (ret_tokens, element_tokens->str);
565 /* --------------------------------------------------------------------------------------------- */
567 * Strip path to home dir.
568 * @param dir pointer to string contains full path
571 static char *
572 vfs_path_strip_home (const char *dir)
574 const char *home_dir = mc_config_get_home_dir ();
576 if (home_dir != NULL)
578 size_t len;
580 len = strlen (home_dir);
582 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
583 return g_strdup_printf ("~%s", dir + len);
586 return g_strdup (dir);
589 /* --------------------------------------------------------------------------------------------- */
591 /* --------------------------------------------------------------------------------------------- */
592 /*** public functions ****************************************************************************/
593 /* --------------------------------------------------------------------------------------------- */
595 * Convert first elements_count elements from vfs_path_t to string representation with flags.
597 * @param vpath pointer to vfs_path_t object
598 * @param elements_count count of first elements for convert
599 * @param flags flags for converter
601 * @return pointer to newly created string.
604 #define vfs_append_from_path(appendfrom, is_relative) \
606 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
608 char *stripped_home_str; \
609 stripped_home_str = vfs_path_strip_home (appendfrom); \
610 g_string_append (buffer, stripped_home_str); \
611 g_free (stripped_home_str); \
613 else \
615 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
616 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
617 g_string_append_c (buffer, PATH_SEP); \
618 g_string_append (buffer, appendfrom); \
622 char *
623 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
625 int element_index;
626 GString *buffer;
627 GString *recode_buffer;
629 if (vpath == NULL)
630 return NULL;
632 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
633 elements_count = vfs_path_elements_count (vpath);
635 if (elements_count < 0)
636 elements_count = vfs_path_elements_count (vpath) + elements_count;
638 buffer = g_string_new ("");
639 recode_buffer = g_string_new ("");
641 for (element_index = 0; element_index < elements_count; element_index++)
643 const vfs_path_element_t *element;
644 gboolean is_relative = vpath->relative && (element_index == 0);
646 element = vfs_path_get_by_index (vpath, element_index);
647 if (element->vfs_prefix != NULL)
649 char *url_str;
650 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
651 g_string_append_c (buffer, PATH_SEP);
653 g_string_append (buffer, element->vfs_prefix);
654 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
656 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
658 if (*url_str != '\0')
660 g_string_append (buffer, url_str);
661 g_string_append_c (buffer, PATH_SEP);
664 g_free (url_str);
667 #ifdef HAVE_CHARSET
668 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
670 if ((flags & VPF_HIDE_CHARSET) == 0)
672 if ((!is_relative)
673 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
674 g_string_append (buffer, PATH_SEP_STR);
675 g_string_append (buffer, VFS_ENCODING_PREFIX);
676 g_string_append (buffer, element->encoding);
678 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
679 vfs_append_from_path (recode_buffer->str, is_relative);
680 g_string_set_size (recode_buffer, 0);
682 else
683 #endif
685 vfs_append_from_path (element->path, is_relative);
688 g_string_free (recode_buffer, TRUE);
689 return g_string_free (buffer, FALSE);
692 #undef vfs_append_from_path
694 /* --------------------------------------------------------------------------------------------- */
696 * Convert first elements_count elements from vfs_path_t to string representation.
698 * @param vpath pointer to vfs_path_t object
699 * @param elements_count count of first elements for convert
701 * @return pointer to newly created string.
704 char *
705 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
707 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
710 /* --------------------------------------------------------------------------------------------- */
712 * Convert vfs_path_t to string representation.
714 * @param vpath pointer to vfs_path_t object
716 * @return pointer to newly created string.
719 char *
720 vfs_path_to_str (const vfs_path_t * vpath)
722 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
725 /* --------------------------------------------------------------------------------------------- */
727 * Split path string to path elements with flags for change parce process.
729 * @param path_str VFS-path
730 * @param flags flags for parser
732 * @return pointer to newly created vfs_path_t object with filled path elements array.
735 vfs_path_t *
736 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
738 vfs_path_t *vpath;
739 char *path;
741 if (path_str == NULL)
742 return NULL;
744 if ((flags & VPF_NO_CANON) == 0)
745 path = vfs_canon (path_str);
746 else
747 path = g_strdup (path_str);
749 if (path == NULL)
750 return NULL;
752 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
753 vpath = vfs_path_from_str_deprecated_parser (path, flags);
754 else
755 vpath = vfs_path_from_str_uri_parser (path, flags);
757 g_free (path);
759 return vpath;
762 /* --------------------------------------------------------------------------------------------- */
764 * Split path string to path elements.
766 * @param path_str VFS-path
768 * @return pointer to newly created vfs_path_t object with filled path elements array.
771 vfs_path_t *
772 vfs_path_from_str (const char *path_str)
774 return vfs_path_from_str_flags (path_str, VPF_NONE);
777 /* --------------------------------------------------------------------------------------------- */
779 * Create new vfs_path_t object.
781 * @return pointer to newly created vfs_path_t object.
784 vfs_path_t *
785 vfs_path_new (void)
787 vfs_path_t *vpath;
789 vpath = g_new0 (vfs_path_t, 1);
790 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
792 return vpath;
795 /* --------------------------------------------------------------------------------------------- */
797 * Get count of path elements.
799 * @param vpath pointer to vfs_path_t object
801 * @return count of path elements.
805 vfs_path_elements_count (const vfs_path_t * vpath)
807 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
810 /* --------------------------------------------------------------------------------------------- */
812 * Add vfs_path_element_t object to end of list in vfs_path_t object
813 * @param vpath pointer to vfs_path_t object
814 * @param path_element pointer to vfs_path_element_t object
817 void
818 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
820 g_array_append_val (vpath->path, path_element);
823 /* --------------------------------------------------------------------------------------------- */
825 * Get one path element by index.
827 * @param vpath pointer to vfs_path_t object
828 * @param element_index element index. May have negative value (in this case count was started at the end of list).
830 * @return path element.
833 const vfs_path_element_t *
834 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
836 if (vpath == NULL)
837 return NULL;
839 if (element_index < 0)
840 element_index += vfs_path_elements_count (vpath);
842 if (element_index < 0)
843 vfs_die ("vfs_path_get_by_index: incorrect index!");
845 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
848 /* --------------------------------------------------------------------------------------------- */
850 * Clone one path element
852 * @param element pointer to vfs_path_element_t object
854 * @return Newly allocated path element
857 vfs_path_element_t *
858 vfs_path_element_clone (const vfs_path_element_t * element)
860 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
862 new_element->user = g_strdup (element->user);
863 new_element->password = g_strdup (element->password);
864 new_element->host = g_strdup (element->host);
865 new_element->ipv6 = element->ipv6;
866 new_element->port = element->port;
867 new_element->path = g_strdup (element->path);
868 new_element->class = element->class;
869 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
870 #ifdef HAVE_CHARSET
871 new_element->encoding = g_strdup (element->encoding);
872 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
873 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
874 else
875 new_element->dir.converter = element->dir.converter;
876 #endif
877 new_element->dir.info = element->dir.info;
879 return new_element;
882 /* --------------------------------------------------------------------------------------------- */
884 * Free one path element.
886 * @param element pointer to vfs_path_element_t object
890 void
891 vfs_path_element_free (vfs_path_element_t * element)
893 if (element == NULL)
894 return;
896 g_free (element->user);
897 g_free (element->password);
898 g_free (element->host);
899 g_free (element->path);
900 g_free (element->vfs_prefix);
902 #ifdef HAVE_CHARSET
903 g_free (element->encoding);
905 if (vfs_path_element_need_cleanup_converter (element))
906 str_close_conv (element->dir.converter);
907 #endif
909 g_free (element);
912 /* --------------------------------------------------------------------------------------------- */
914 * Clone path
916 * @param vpath pointer to vfs_path_t object
918 * @return Newly allocated path object
921 vfs_path_t *
922 vfs_path_clone (const vfs_path_t * vpath)
924 vfs_path_t *new_vpath;
925 int vpath_element_index;
927 if (vpath == NULL)
928 return NULL;
930 new_vpath = vfs_path_new ();
931 new_vpath->relative = vpath->relative;
933 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
934 vpath_element_index++)
936 vfs_path_element_t *path_element;
938 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
939 g_array_append_val (new_vpath->path, path_element);
942 return new_vpath;
945 /* --------------------------------------------------------------------------------------------- */
947 * Free vfs_path_t object.
949 * @param vpath pointer to vfs_path_t object
953 void
954 vfs_path_free (vfs_path_t * vpath)
956 int vpath_element_index;
958 if (vpath == NULL)
959 return;
961 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
962 vpath_element_index++)
964 vfs_path_element_t *path_element;
966 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
967 vfs_path_element_free (path_element);
970 g_array_free (vpath->path, TRUE);
971 g_free (vpath);
974 /* --------------------------------------------------------------------------------------------- */
976 * Remove one path element by index
978 * @param vpath pointer to vfs_path_t object
979 * @param element_index element index. May have negative value (in this case count was started at the end of list).
983 void
984 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
986 vfs_path_element_t *element;
988 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
989 return;
991 if (element_index < 0)
992 element_index = vfs_path_elements_count (vpath) + element_index;
994 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
995 vpath->path = g_array_remove_index (vpath->path, element_index);
996 vfs_path_element_free (element);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Return VFS class for the given prefix */
1002 struct vfs_class *
1003 vfs_prefix_to_class (const char *prefix)
1005 guint i;
1007 /* Avoid first class (localfs) that would accept any prefix */
1008 for (i = 1; i < vfs__classes_list->len; i++)
1010 struct vfs_class *vfs;
1012 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1013 if (vfs->which != NULL)
1015 if (vfs->which (vfs, prefix) == -1)
1016 continue;
1017 return vfs;
1020 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1021 return vfs;
1024 return NULL;
1027 /* --------------------------------------------------------------------------------------------- */
1029 * Check if need cleanup charset converter for vfs_path_element_t
1031 * @param element part of path
1033 * @return TRUE if need cleanup converter or FALSE otherwise
1035 #ifdef HAVE_CHARSET
1036 gboolean
1037 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1039 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1041 #endif
1043 /* --------------------------------------------------------------------------------------------- */
1045 * Serialize vfs_path_t object to string
1047 * @param vpath data for serialization
1048 * @param error contain pointer to object for handle error code and message
1050 * @return serialized vpath as newly allocated string
1053 char *
1054 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1056 mc_config_t *cpath = mc_config_init (NULL);
1057 ssize_t element_index;
1058 char *ret_value;
1060 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1062 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1063 return NULL;
1066 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1068 char *groupname;
1069 const vfs_path_element_t *element;
1071 groupname = g_strdup_printf ("path-element-%zd", element_index);
1072 element = vfs_path_get_by_index (vpath, element_index);
1073 /* convert one element to config group */
1075 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1076 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1077 #ifdef HAVE_CHARSET
1078 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1079 #endif
1080 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1082 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1083 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1084 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1085 if (element->port != 0)
1086 mc_config_set_int (cpath, groupname, "port", element->port);
1088 g_free (groupname);
1091 ret_value = mc_serialize_config (cpath, error);
1092 mc_config_deinit (cpath);
1093 return ret_value;
1096 /* --------------------------------------------------------------------------------------------- */
1098 * Deserialize string to vfs_path_t object
1100 * @param data data for serialization
1101 * @param error contain pointer to object for handle error code and message
1103 * @return newly allocated vfs_path_t object
1106 vfs_path_t *
1107 vfs_path_deserialize (const char *data, GError ** error)
1109 mc_config_t *cpath = mc_deserialize_config (data, error);
1110 size_t element_index = 0;
1111 vfs_path_t *vpath;
1113 if (cpath == NULL)
1114 return NULL;
1116 vpath = vfs_path_new ();
1118 while (TRUE)
1120 vfs_path_element_t *element;
1121 char *cfg_value;
1122 char *groupname;
1124 groupname = g_strdup_printf ("path-element-%zd", element_index);
1125 if (!mc_config_has_group (cpath, groupname))
1127 g_free (groupname);
1128 break;
1131 element = g_new0 (vfs_path_element_t, 1);
1133 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1134 element->class = vfs_get_class_by_name (cfg_value);
1135 if (element->class == NULL)
1137 g_free (element);
1138 vfs_path_free (vpath);
1139 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1140 g_free (cfg_value);
1141 mc_config_deinit (cpath);
1142 return NULL;
1144 g_free (cfg_value);
1146 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1148 #ifdef HAVE_CHARSET
1149 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1150 element->dir.converter =
1151 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1152 #endif
1154 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1156 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1157 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1158 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1159 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1161 vpath->path = g_array_append_val (vpath->path, element);
1163 g_free (groupname);
1164 element_index++;
1167 mc_config_deinit (cpath);
1168 if (vfs_path_elements_count (vpath) == 0)
1170 vfs_path_free (vpath);
1171 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1172 return NULL;
1175 return vpath;
1178 /* --------------------------------------------------------------------------------------------- */
1180 * Build vfs_path_t object from arguments.
1182 * @param ... path tokens, terminated by NULL
1184 * @return newly allocated vfs_path_t object
1187 vfs_path_t *
1188 vfs_path_build_filename (const char *first_element, ...)
1190 va_list args;
1191 char *str_path;
1192 vfs_path_t *vpath;
1194 if (first_element == NULL)
1195 return NULL;
1197 va_start (args, first_element);
1198 str_path = mc_build_filenamev (first_element, args);
1199 va_end (args);
1200 vpath = vfs_path_from_str (str_path);
1201 g_free (str_path);
1202 return vpath;
1205 /* --------------------------------------------------------------------------------------------- */
1207 * Append tokens to path object
1209 * @param vpath path object
1210 * @param ... NULL-terminated strings
1212 * @return newly allocated path object
1215 vfs_path_t *
1216 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1218 va_list args;
1219 char *str_path, *result_str;
1220 vfs_path_t *ret_vpath;
1222 if (vpath == NULL || first_element == NULL)
1223 return NULL;
1225 va_start (args, first_element);
1226 str_path = mc_build_filenamev (first_element, args);
1227 va_end (args);
1229 result_str = vfs_path_to_str (vpath);
1230 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1231 g_free (result_str);
1232 g_free (str_path);
1234 return ret_vpath;
1238 /* --------------------------------------------------------------------------------------------- */
1241 * Append vpath_t tokens to path object
1243 * @param ... NULL-terminated vpath objects
1245 * @return newly allocated path object
1248 vfs_path_t *
1249 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1251 va_list args;
1252 vfs_path_t *ret_vpath;
1253 const vfs_path_t *current_vpath = first_vpath;
1255 if (first_vpath == NULL)
1256 return NULL;
1258 ret_vpath = vfs_path_new ();
1260 va_start (args, first_vpath);
1263 int vindex;
1265 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1267 vfs_path_element_t *path_element;
1269 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1270 g_array_append_val (ret_vpath->path, path_element);
1272 current_vpath = va_arg (args, const vfs_path_t *);
1274 while (current_vpath != NULL);
1275 va_end (args);
1277 return ret_vpath;
1280 /* --------------------------------------------------------------------------------------------- */
1282 * get tockens count in path.
1284 * @param vpath path object
1286 * @return count of tokens
1289 size_t
1290 vfs_path_tokens_count (const vfs_path_t * vpath)
1292 size_t count_tokens = 0;
1293 int element_index;
1295 if (vpath == NULL)
1296 return 0;
1298 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1300 const vfs_path_element_t *element;
1301 char **path_tokens, **iterator;
1303 element = vfs_path_get_by_index (vpath, element_index);
1304 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1306 while (*iterator != NULL)
1308 if (**iterator != '\0')
1309 count_tokens++;
1310 iterator++;
1312 g_strfreev (path_tokens);
1314 return count_tokens;
1317 /* --------------------------------------------------------------------------------------------- */
1319 * Get subpath by tokens
1321 * @param vpath path object
1322 * @param start_position first token for got/ Started from 0.
1323 * If negative, then position will be relative to end of path
1324 * @param length count of tokens
1326 * @return newly allocated string with path tokens separated by slash
1329 char *
1330 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1332 GString *ret_tokens, *element_tokens;
1333 int element_index;
1334 size_t tokens_count = vfs_path_tokens_count (vpath);
1336 if (vpath == NULL)
1337 return NULL;
1339 if (length == 0)
1340 length = tokens_count;
1342 if (length < 0)
1343 length = tokens_count + length;
1345 if (start_position < 0)
1346 start_position = (ssize_t) tokens_count + start_position;
1348 if (start_position < 0)
1349 return NULL;
1351 if (start_position >= (ssize_t) tokens_count)
1352 return NULL;
1354 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1355 length = tokens_count - start_position;
1357 ret_tokens = g_string_sized_new (32);
1358 element_tokens = g_string_sized_new (32);
1360 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1362 const vfs_path_element_t *element;
1363 char **path_tokens, **iterator;
1365 g_string_assign (element_tokens, "");
1366 element = vfs_path_get_by_index (vpath, element_index);
1367 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1369 while (*iterator != NULL)
1371 if (**iterator != '\0')
1373 if (start_position == 0)
1375 if (length == 0)
1377 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1378 g_string_free (element_tokens, TRUE);
1379 g_strfreev (path_tokens);
1380 return g_string_free (ret_tokens, FALSE);
1382 length--;
1383 if (element_tokens->len != 0)
1384 g_string_append_c (element_tokens, PATH_SEP);
1385 g_string_append (element_tokens, *iterator);
1387 else
1388 start_position--;
1390 iterator++;
1392 g_strfreev (path_tokens);
1393 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1396 g_string_free (element_tokens, TRUE);
1397 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1400 /* --------------------------------------------------------------------------------------------- */
1402 * Get subpath by tokens
1404 * @param vpath path object
1405 * @param start_position first token for got/ Started from 0.
1406 * If negative, then position will be relative to end of path
1407 * @param length count of tokens
1409 * @return newly allocated path object with path tokens separated by slash
1412 vfs_path_t *
1413 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1415 char *str_tokens;
1416 vfs_path_t *ret_vpath = NULL;
1418 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1419 if (str_tokens != NULL)
1421 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1422 g_free (str_tokens);
1424 return ret_vpath;
1427 /* --------------------------------------------------------------------------------------------- */
1429 * Build URL parameters (such as user:pass@host:port) from one path element object
1431 * @param element path element
1433 * @return newly allocated string
1436 char *
1437 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1439 GString *buffer;
1441 if (element == NULL)
1442 return NULL;
1444 buffer = g_string_new ("");
1446 if (element->user != NULL)
1447 g_string_append (buffer, element->user);
1449 if (element->password != NULL && keep_password)
1451 g_string_append_c (buffer, ':');
1452 g_string_append (buffer, element->password);
1455 if (element->host != NULL)
1457 if ((element->user != NULL) || (element->password != NULL))
1458 g_string_append_c (buffer, '@');
1459 if (element->ipv6)
1460 g_string_append_c (buffer, '[');
1461 g_string_append (buffer, element->host);
1462 if (element->ipv6)
1463 g_string_append_c (buffer, ']');
1466 if ((element->port) != 0 && (element->host != NULL))
1468 g_string_append_c (buffer, ':');
1469 g_string_append_printf (buffer, "%d", element->port);
1472 return g_string_free (buffer, FALSE);
1475 /* --------------------------------------------------------------------------------------------- */
1477 * Build pretty string representation of one path_element_t object
1479 * @param element path element
1481 * @return newly allocated string
1484 char *
1485 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1487 char *url_params;
1488 GString *pretty_path;
1490 pretty_path = g_string_new (element->class->prefix);
1491 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1493 url_params = vfs_path_build_url_params_str (element, FALSE);
1494 g_string_append (pretty_path, url_params);
1495 g_free (url_params);
1497 if (*element->path != PATH_SEP)
1498 g_string_append_c (pretty_path, PATH_SEP);
1500 g_string_append (pretty_path, element->path);
1501 return g_string_free (pretty_path, FALSE);
1504 /* --------------------------------------------------------------------------------------------- */
1506 * Compare two path objects as strings
1508 * @param vpath1 first path object
1509 * @param vpath2 second vpath object
1511 * @return integer value like to strcmp.
1515 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1517 char *path1;
1518 char *path2;
1519 int ret_val;
1521 if (vpath1 == NULL || vpath2 == NULL)
1522 return -1;
1524 path1 = vfs_path_to_str (vpath1);
1525 path2 = vfs_path_to_str (vpath2);
1527 ret_val = strcmp (path1, path2);
1529 g_free (path1);
1530 g_free (path2);
1532 return ret_val;
1535 /* --------------------------------------------------------------------------------------------- */
1537 * Compare two path objects as strings
1539 * @param vpath1 first path object
1540 * @param vpath2 second vpath object
1541 * @param len number of first 'len' characters
1543 * @return integer value like to strcmp.
1547 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1549 char *path1;
1550 char *path2;
1551 int ret_val;
1553 if (vpath1 == NULL || vpath2 == NULL)
1554 return -1;
1556 path1 = vfs_path_to_str (vpath1);
1557 path2 = vfs_path_to_str (vpath2);
1559 ret_val = strncmp (path1, path2, len);
1561 g_free (path1);
1562 g_free (path2);
1564 return ret_val;
1567 /* --------------------------------------------------------------------------------------------- */
1569 * Calculate path length in string representation
1571 * @param vpath path object
1573 * @return length of path
1576 size_t
1577 vfs_path_len (const vfs_path_t * vpath)
1579 char *path;
1580 size_t ret_val;
1582 if (vpath == NULL)
1583 return 0;
1585 path = vfs_path_to_str (vpath);
1586 ret_val = strlen (path);
1587 g_free (path);
1588 return ret_val;
1591 /* --------------------------------------------------------------------------------------------- */
1593 * Convert relative vpath object to absolute
1595 * @param vpath path object
1597 * @return absolute path object
1600 vfs_path_t *
1601 vfs_path_to_absolute (const vfs_path_t * vpath)
1603 vfs_path_t *absolute_vpath;
1604 char *path_str;
1606 if (!vpath->relative)
1607 return vfs_path_clone (vpath);
1609 path_str = vfs_path_to_str (vpath);
1610 absolute_vpath = vfs_path_from_str (path_str);
1611 g_free (path_str);
1612 return absolute_vpath;
1615 /* --------------------------------------------------------------------------------------------- */