Avoid compiler errors like: error: variable 'xxx' set but not used [-Werror=unused...
[midnight-commander.git] / lib / vfs / path.c
blob9e49b1698815f8fc8e3d9b233df26145b2e16db2
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 if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
148 encoding prefix placed at start of string without the leading slash
149 should be autofixed by adding the leading slash
151 local = mc_build_filename (PATH_SEP_STR, path, NULL);
153 else
155 char *curr_dir;
157 curr_dir = vfs_get_current_dir ();
158 local = mc_build_filename (curr_dir, path, NULL);
159 g_free (curr_dir);
161 result = vfs_canon (local);
162 g_free (local);
163 return result;
167 * So we have path of following form:
168 * /p1/p2#op/.././././p3#op/p4. Good luck.
171 char *result;
173 result = g_strdup (path);
174 canonicalize_pathname (result);
175 return result;
179 /* --------------------------------------------------------------------------------------------- */
181 #ifdef HAVE_CHARSET
182 /** get encoding after last #enc: or NULL, if part does not contain #enc:
184 * @param path string
186 * @return newly allocated string.
189 static char *
190 vfs_get_encoding (const char *path)
192 char result[16];
193 char *work;
194 char *semi;
195 char *slash;
196 work = g_strdup (path);
198 /* try found #enc: */
199 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
201 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
203 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
204 slash = strchr (semi, PATH_SEP);
205 if (slash != NULL)
206 slash[0] = '\0';
208 g_strlcpy (result, semi, sizeof (result));
209 g_free (work);
210 return g_strdup (result);
212 else
214 g_free (work);
215 return NULL;
218 #endif
220 /* --------------------------------------------------------------------------------------------- */
221 /** Extract the hostname and username from the path
223 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
225 * ftp://sunsite.unc.edu/pub/linux
226 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
227 * ftp://tsx-11.mit.edu:8192/
228 * ftp://joe@foo.edu:11321/private
229 * ftp://joe:password@foo.se
231 * @param path_element is an input string to be parsed
232 * @param path is an input string to be parsed
234 * @return g_malloc()ed url info.
235 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
236 * is not set, then the current login name is supplied.
237 * Return value is a g_malloc()ed structure with the pathname relative to the
238 * host.
241 static void
242 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
244 char *pcopy;
245 const char *pend;
246 char *colon, *inner_colon, *at, *rest;
248 path_element->port = 0;
250 pcopy = g_strdup (path);
251 pend = pcopy + strlen (pcopy);
253 /* search for any possible user */
254 at = strrchr (pcopy, '@');
256 /* We have a username */
257 if (at == NULL)
258 rest = pcopy;
259 else
261 *at = '\0';
262 inner_colon = strchr (pcopy, ':');
263 if (inner_colon != NULL)
265 *inner_colon = '\0';
266 inner_colon++;
267 path_element->password = g_strdup (inner_colon);
270 if (*pcopy != '\0')
271 path_element->user = g_strdup (pcopy);
273 if (pend == at + 1)
274 rest = at;
275 else
276 rest = at + 1;
279 /* Check if the host comes with a port spec, if so, chop it */
280 if (*rest != '[')
281 colon = strchr (rest, ':');
282 else
284 colon = strchr (++rest, ']');
285 if (colon != NULL)
287 colon[0] = '\0';
288 colon[1] = '\0';
289 colon++;
290 path_element->ipv6 = TRUE;
294 if (colon != NULL)
296 *colon = '\0';
297 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
299 if (path_element->port <= 0 || path_element->port >= 65536)
300 path_element->port = 0;
302 else
303 while (*(++colon) != '\0')
305 switch (*colon)
307 case 'C':
308 path_element->port = 1;
309 break;
310 case 'r':
311 path_element->port = 2;
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, vfs_path_flag_t flags)
373 vfs_path_t *vpath;
374 vfs_path_element_t *element;
375 struct vfs_class *class;
376 const char *local, *op;
378 (void) flags;
379 vpath = vfs_path_new ();
381 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
383 char *url_params;
384 element = g_new0 (vfs_path_element_t, 1);
385 element->class = class;
386 if (local == NULL)
387 local = "";
388 element->path = vfs_translate_path_n (local);
390 #ifdef HAVE_CHARSET
391 element->encoding = vfs_get_encoding (local);
392 element->dir.converter =
393 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
394 #endif
396 url_params = strchr (op, ':'); /* skip VFS prefix */
397 if (url_params != NULL)
399 *url_params = '\0';
400 url_params++;
401 vfs_path_url_split (element, url_params);
404 if (*op != '\0')
405 element->vfs_prefix = g_strdup (op);
407 g_array_prepend_val (vpath->path, element);
409 if (path[0] != '\0')
411 element = g_new0 (vfs_path_element_t, 1);
412 element->class = g_ptr_array_index (vfs__classes_list, 0);
413 element->path = vfs_translate_path_n (path);
415 #ifdef HAVE_CHARSET
416 element->encoding = vfs_get_encoding (path);
417 element->dir.converter =
418 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
419 #endif
420 g_array_prepend_val (vpath->path, element);
423 return vpath;
426 /* --------------------------------------------------------------------------------------------- */
427 /** Split path string to path elements by URL algorithm.
429 * @param path_str VFS-path
430 * @param flags flags for converter
432 * @return pointer to newly created vfs_path_t object with filled path elements array.
435 static vfs_path_t *
436 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
438 vfs_path_t *vpath;
439 vfs_path_element_t *element;
441 char *url_delimiter;
443 vpath = vfs_path_new ();
444 vpath->relative = (flags & VPF_NO_CANON) != 0;
446 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
448 char *vfs_prefix_start;
449 char *real_vfs_prefix_start = url_delimiter;
450 char *slash_pointer;
451 struct vfs_s_subclass *sub = NULL;
453 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
454 real_vfs_prefix_start--;
455 vfs_prefix_start = real_vfs_prefix_start;
457 if (*(vfs_prefix_start) == PATH_SEP)
458 vfs_prefix_start += 1;
460 *url_delimiter = '\0';
462 element = g_new0 (vfs_path_element_t, 1);
463 element->class = vfs_prefix_to_class (vfs_prefix_start);
464 element->vfs_prefix = g_strdup (vfs_prefix_start);
466 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
467 sub = VFSDATA (element);
468 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
470 slash_pointer = strchr (url_delimiter, PATH_SEP);
471 if (slash_pointer == NULL)
473 element->path = g_strdup ("");
475 else
477 element->path = vfs_translate_path_n (slash_pointer + 1);
478 #ifdef HAVE_CHARSET
479 element->encoding = vfs_get_encoding (slash_pointer);
480 #endif
481 *slash_pointer = '\0';
483 vfs_path_url_split (element, url_delimiter);
485 else
487 element->path = vfs_translate_path_n (url_delimiter);
488 #ifdef HAVE_CHARSET
489 element->encoding = vfs_get_encoding (url_delimiter);
490 #endif
492 #ifdef HAVE_CHARSET
493 element->dir.converter =
494 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
495 #endif
496 g_array_prepend_val (vpath->path, element);
498 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
499 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
500 *real_vfs_prefix_start = '\0';
501 else
502 *(real_vfs_prefix_start + 1) = '\0';
505 if (path[0] != '\0')
507 element = g_new0 (vfs_path_element_t, 1);
508 element->class = g_ptr_array_index (vfs__classes_list, 0);
509 element->path = vfs_translate_path_n (path);
510 #ifdef HAVE_CHARSET
511 element->encoding = vfs_get_encoding (path);
512 element->dir.converter =
513 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
514 #endif
515 g_array_prepend_val (vpath->path, element);
518 return vpath;
521 /* --------------------------------------------------------------------------------------------- */
523 * Add element's class info to result string (such as VFS name, host, encoding etc)
524 * This function used as helper only in vfs_path_tokens_get() function
526 * @param element current path element
527 * @param ret_tokens total tikens for return
528 * @param element_tokens accumulated element-only tokens
531 static void
532 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
533 GString * element_tokens)
535 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
536 && element_tokens->len > 0)
538 char *url_str;
540 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
541 g_string_append_c (ret_tokens, PATH_SEP);
543 g_string_append (ret_tokens, element->vfs_prefix);
544 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
546 url_str = vfs_path_build_url_params_str (element, TRUE);
547 if (*url_str != '\0')
549 g_string_append (ret_tokens, url_str);
550 g_string_append_c (ret_tokens, PATH_SEP);
553 g_free (url_str);
556 #ifdef HAVE_CHARSET
557 if (element->encoding != NULL)
559 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
560 g_string_append (ret_tokens, PATH_SEP_STR);
561 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
562 g_string_append (ret_tokens, element->encoding);
563 g_string_append (ret_tokens, PATH_SEP_STR);
565 #endif
567 g_string_append (ret_tokens, element_tokens->str);
570 /* --------------------------------------------------------------------------------------------- */
572 * Strip path to home dir.
573 * @param dir pointer to string contains full path
576 static char *
577 vfs_path_strip_home (const char *dir)
579 const char *home_dir = mc_config_get_home_dir ();
581 if (home_dir != NULL)
583 size_t len;
585 len = strlen (home_dir);
587 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
588 return g_strdup_printf ("~%s", dir + len);
591 return g_strdup (dir);
594 /* --------------------------------------------------------------------------------------------- */
596 /* --------------------------------------------------------------------------------------------- */
597 /*** public functions ****************************************************************************/
598 /* --------------------------------------------------------------------------------------------- */
600 #define vfs_append_from_path(appendfrom, is_relative) \
602 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
604 char *stripped_home_str; \
605 stripped_home_str = vfs_path_strip_home (appendfrom); \
606 g_string_append (buffer, stripped_home_str); \
607 g_free (stripped_home_str); \
609 else \
611 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
612 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
613 g_string_append_c (buffer, PATH_SEP); \
614 g_string_append (buffer, appendfrom); \
619 * Convert first elements_count elements from vfs_path_t to string representation with flags.
621 * @param vpath pointer to vfs_path_t object
622 * @param elements_count count of first elements for convert
623 * @param flags for converter
625 * @return pointer to newly created string.
628 char *
629 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
631 int element_index;
632 GString *buffer;
633 GString *recode_buffer;
635 if (vpath == NULL)
636 return NULL;
638 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
639 elements_count = vfs_path_elements_count (vpath);
641 if (elements_count < 0)
642 elements_count = vfs_path_elements_count (vpath) + elements_count;
644 buffer = g_string_new ("");
645 recode_buffer = g_string_new ("");
647 for (element_index = 0; element_index < elements_count; element_index++)
649 const vfs_path_element_t *element;
650 gboolean is_relative = vpath->relative && (element_index == 0);
652 element = vfs_path_get_by_index (vpath, element_index);
653 if (element->vfs_prefix != NULL)
655 char *url_str;
656 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
657 g_string_append_c (buffer, PATH_SEP);
659 g_string_append (buffer, element->vfs_prefix);
660 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
662 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
664 if (*url_str != '\0')
666 g_string_append (buffer, url_str);
667 g_string_append_c (buffer, PATH_SEP);
670 g_free (url_str);
673 #ifdef HAVE_CHARSET
674 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
676 if ((flags & VPF_HIDE_CHARSET) == 0)
678 if ((!is_relative)
679 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
680 g_string_append (buffer, PATH_SEP_STR);
681 g_string_append (buffer, VFS_ENCODING_PREFIX);
682 g_string_append (buffer, element->encoding);
684 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
685 vfs_append_from_path (recode_buffer->str, is_relative);
686 g_string_set_size (recode_buffer, 0);
688 else
689 #endif
691 vfs_append_from_path (element->path, is_relative);
694 g_string_free (recode_buffer, TRUE);
695 return g_string_free (buffer, FALSE);
698 #undef vfs_append_from_path
700 /* --------------------------------------------------------------------------------------------- */
702 * Convert first elements_count elements from vfs_path_t to string representation.
704 * @param vpath pointer to vfs_path_t object
705 * @param elements_count count of first elements for convert
707 * @return pointer to newly created string.
710 char *
711 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
713 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
716 /* --------------------------------------------------------------------------------------------- */
718 * Convert vfs_path_t to string representation.
720 * @param vpath pointer to vfs_path_t object
722 * @return pointer to newly created string.
725 char *
726 vfs_path_to_str (const vfs_path_t * vpath)
728 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
731 /* --------------------------------------------------------------------------------------------- */
733 * Split path string to path elements with flags for change parce process.
735 * @param path_str VFS-path
736 * @param flags flags for parser
738 * @return pointer to newly created vfs_path_t object with filled path elements array.
741 vfs_path_t *
742 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
744 vfs_path_t *vpath;
745 char *path;
747 if (path_str == NULL)
748 return NULL;
750 if ((flags & VPF_NO_CANON) == 0)
751 path = vfs_canon (path_str);
752 else
753 path = g_strdup (path_str);
755 if (path == NULL)
756 return NULL;
758 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
759 vpath = vfs_path_from_str_deprecated_parser (path, flags);
760 else
761 vpath = vfs_path_from_str_uri_parser (path, flags);
763 g_free (path);
765 return vpath;
768 /* --------------------------------------------------------------------------------------------- */
770 * Split path string to path elements.
772 * @param path_str VFS-path
774 * @return pointer to newly created vfs_path_t object with filled path elements array.
777 vfs_path_t *
778 vfs_path_from_str (const char *path_str)
780 return vfs_path_from_str_flags (path_str, VPF_NONE);
783 /* --------------------------------------------------------------------------------------------- */
785 * Create new vfs_path_t object.
787 * @return pointer to newly created vfs_path_t object.
790 vfs_path_t *
791 vfs_path_new (void)
793 vfs_path_t *vpath;
795 vpath = g_new0 (vfs_path_t, 1);
796 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
798 return vpath;
801 /* --------------------------------------------------------------------------------------------- */
803 * Get count of path elements.
805 * @param vpath pointer to vfs_path_t object
807 * @return count of path elements.
811 vfs_path_elements_count (const vfs_path_t * vpath)
813 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
816 /* --------------------------------------------------------------------------------------------- */
818 * Add vfs_path_element_t object to end of list in vfs_path_t object
819 * @param vpath pointer to vfs_path_t object
820 * @param path_element pointer to vfs_path_element_t object
823 void
824 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
826 g_array_append_val (vpath->path, path_element);
829 /* --------------------------------------------------------------------------------------------- */
831 * Get one path element by index.
833 * @param vpath pointer to vfs_path_t object
834 * @param element_index element index. May have negative value (in this case count was started at the end of list).
836 * @return path element.
839 const vfs_path_element_t *
840 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
842 if (vpath == NULL)
843 return NULL;
845 if (element_index < 0)
846 element_index += vfs_path_elements_count (vpath);
848 if (element_index < 0)
849 vfs_die ("vfs_path_get_by_index: incorrect index!");
851 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
854 /* --------------------------------------------------------------------------------------------- */
856 * Clone one path element
858 * @param element pointer to vfs_path_element_t object
860 * @return Newly allocated path element
863 vfs_path_element_t *
864 vfs_path_element_clone (const vfs_path_element_t * element)
866 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
868 new_element->user = g_strdup (element->user);
869 new_element->password = g_strdup (element->password);
870 new_element->host = g_strdup (element->host);
871 new_element->ipv6 = element->ipv6;
872 new_element->port = element->port;
873 new_element->path = g_strdup (element->path);
874 new_element->class = element->class;
875 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
876 #ifdef HAVE_CHARSET
877 new_element->encoding = g_strdup (element->encoding);
878 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
879 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
880 else
881 new_element->dir.converter = element->dir.converter;
882 #endif
883 new_element->dir.info = element->dir.info;
885 return new_element;
888 /* --------------------------------------------------------------------------------------------- */
890 * Free one path element.
892 * @param element pointer to vfs_path_element_t object
896 void
897 vfs_path_element_free (vfs_path_element_t * element)
899 if (element == NULL)
900 return;
902 g_free (element->user);
903 g_free (element->password);
904 g_free (element->host);
905 g_free (element->path);
906 g_free (element->vfs_prefix);
908 #ifdef HAVE_CHARSET
909 g_free (element->encoding);
911 if (vfs_path_element_need_cleanup_converter (element))
912 str_close_conv (element->dir.converter);
913 #endif
915 g_free (element);
918 /* --------------------------------------------------------------------------------------------- */
920 * Clone path
922 * @param vpath pointer to vfs_path_t object
924 * @return Newly allocated path object
927 vfs_path_t *
928 vfs_path_clone (const vfs_path_t * vpath)
930 vfs_path_t *new_vpath;
931 int vpath_element_index;
933 if (vpath == NULL)
934 return NULL;
936 new_vpath = vfs_path_new ();
937 new_vpath->relative = vpath->relative;
939 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
940 vpath_element_index++)
942 vfs_path_element_t *path_element;
944 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
945 g_array_append_val (new_vpath->path, path_element);
948 return new_vpath;
951 /* --------------------------------------------------------------------------------------------- */
953 * Free vfs_path_t object.
955 * @param vpath pointer to vfs_path_t object
959 void
960 vfs_path_free (vfs_path_t * vpath)
962 int vpath_element_index;
964 if (vpath == NULL)
965 return;
967 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
968 vpath_element_index++)
970 vfs_path_element_t *path_element;
972 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
973 vfs_path_element_free (path_element);
976 g_array_free (vpath->path, TRUE);
977 g_free (vpath);
980 /* --------------------------------------------------------------------------------------------- */
982 * Remove one path element by index
984 * @param vpath pointer to vfs_path_t object
985 * @param element_index element index. May have negative value (in this case count was started at the end of list).
989 void
990 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
992 vfs_path_element_t *element;
994 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
995 return;
997 if (element_index < 0)
998 element_index = vfs_path_elements_count (vpath) + element_index;
1000 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1001 vpath->path = g_array_remove_index (vpath->path, element_index);
1002 vfs_path_element_free (element);
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Return VFS class for the given prefix */
1008 struct vfs_class *
1009 vfs_prefix_to_class (const char *prefix)
1011 guint i;
1013 /* Avoid first class (localfs) that would accept any prefix */
1014 for (i = 1; i < vfs__classes_list->len; i++)
1016 struct vfs_class *vfs;
1018 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1019 if (vfs->which != NULL)
1021 if (vfs->which (vfs, prefix) == -1)
1022 continue;
1023 return vfs;
1026 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1027 return vfs;
1030 return NULL;
1033 /* --------------------------------------------------------------------------------------------- */
1035 #ifdef HAVE_CHARSET
1038 * Check if need cleanup charset converter for vfs_path_element_t
1040 * @param element part of path
1042 * @return TRUE if need cleanup converter or FALSE otherwise
1045 gboolean
1046 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1048 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1050 #endif
1052 /* --------------------------------------------------------------------------------------------- */
1055 * Serialize vfs_path_t object to string
1057 * @param vpath data for serialization
1058 * @param error contain pointer to object for handle error code and message
1060 * @return serialized vpath as newly allocated string
1063 char *
1064 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1066 mc_config_t *cpath;
1067 ssize_t element_index;
1068 char *ret_value;
1070 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1072 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1073 return NULL;
1077 cpath = mc_config_init (NULL, FALSE);
1079 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1081 char *groupname;
1082 const vfs_path_element_t *element;
1084 groupname = g_strdup_printf ("path-element-%zd", element_index);
1085 element = vfs_path_get_by_index (vpath, element_index);
1086 /* convert one element to config group */
1088 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1089 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1090 #ifdef HAVE_CHARSET
1091 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1092 #endif
1093 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1095 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1096 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1097 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1098 if (element->port != 0)
1099 mc_config_set_int (cpath, groupname, "port", element->port);
1101 g_free (groupname);
1104 ret_value = mc_serialize_config (cpath, error);
1105 mc_config_deinit (cpath);
1106 return ret_value;
1109 /* --------------------------------------------------------------------------------------------- */
1111 * Deserialize string to vfs_path_t object
1113 * @param data data for serialization
1114 * @param error contain pointer to object for handle error code and message
1116 * @return newly allocated vfs_path_t object
1119 vfs_path_t *
1120 vfs_path_deserialize (const char *data, GError ** error)
1122 mc_config_t *cpath;
1123 size_t element_index = 0;
1124 vfs_path_t *vpath;
1126 cpath = mc_deserialize_config (data, error);
1127 if (cpath == NULL)
1128 return NULL;
1130 vpath = vfs_path_new ();
1132 while (TRUE)
1134 vfs_path_element_t *element;
1135 char *cfg_value;
1136 char *groupname;
1138 groupname = g_strdup_printf ("path-element-%zd", element_index);
1139 if (!mc_config_has_group (cpath, groupname))
1141 g_free (groupname);
1142 break;
1145 element = g_new0 (vfs_path_element_t, 1);
1147 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1148 element->class = vfs_get_class_by_name (cfg_value);
1149 if (element->class == NULL)
1151 g_free (element);
1152 vfs_path_free (vpath);
1153 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1154 g_free (cfg_value);
1155 mc_config_deinit (cpath);
1156 return NULL;
1158 g_free (cfg_value);
1160 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1162 #ifdef HAVE_CHARSET
1163 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1164 element->dir.converter =
1165 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1166 #endif
1168 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1170 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1171 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1172 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1173 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1175 vpath->path = g_array_append_val (vpath->path, element);
1177 g_free (groupname);
1178 element_index++;
1181 mc_config_deinit (cpath);
1182 if (vfs_path_elements_count (vpath) == 0)
1184 vfs_path_free (vpath);
1185 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1186 return NULL;
1189 return vpath;
1192 /* --------------------------------------------------------------------------------------------- */
1194 * Build vfs_path_t object from arguments.
1196 * @param first_element of path
1197 * @param ... path tokens, terminated by NULL
1199 * @return newly allocated vfs_path_t object
1202 vfs_path_t *
1203 vfs_path_build_filename (const char *first_element, ...)
1205 va_list args;
1206 char *str_path;
1207 vfs_path_t *vpath;
1209 if (first_element == NULL)
1210 return NULL;
1212 va_start (args, first_element);
1213 str_path = mc_build_filenamev (first_element, args);
1214 va_end (args);
1215 vpath = vfs_path_from_str (str_path);
1216 g_free (str_path);
1217 return vpath;
1220 /* --------------------------------------------------------------------------------------------- */
1222 * Append tokens to path object
1224 * @param vpath path object
1225 * @param first_element of path
1226 * @param ... NULL-terminated strings
1228 * @return newly allocated path object
1231 vfs_path_t *
1232 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1234 va_list args;
1235 char *str_path, *result_str;
1236 vfs_path_t *ret_vpath;
1238 if (vpath == NULL || first_element == NULL)
1239 return NULL;
1241 va_start (args, first_element);
1242 str_path = mc_build_filenamev (first_element, args);
1243 va_end (args);
1245 result_str = vfs_path_to_str (vpath);
1246 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1247 g_free (result_str);
1248 g_free (str_path);
1250 return ret_vpath;
1254 /* --------------------------------------------------------------------------------------------- */
1257 * Append vpath_t tokens to path object
1259 * @param first_vpath vpath objects
1260 * @param ... NULL-terminated vpath objects
1262 * @return newly allocated path object
1265 vfs_path_t *
1266 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1268 va_list args;
1269 vfs_path_t *ret_vpath;
1270 const vfs_path_t *current_vpath = first_vpath;
1272 if (first_vpath == NULL)
1273 return NULL;
1275 ret_vpath = vfs_path_new ();
1277 va_start (args, first_vpath);
1280 int vindex;
1282 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1284 vfs_path_element_t *path_element;
1286 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1287 g_array_append_val (ret_vpath->path, path_element);
1289 current_vpath = va_arg (args, const vfs_path_t *);
1291 while (current_vpath != NULL);
1292 va_end (args);
1294 return ret_vpath;
1297 /* --------------------------------------------------------------------------------------------- */
1300 * get tockens count in path.
1302 * @param vpath path object
1304 * @return count of tokens
1307 size_t
1308 vfs_path_tokens_count (const vfs_path_t * vpath)
1310 size_t count_tokens = 0;
1311 int element_index;
1313 if (vpath == NULL)
1314 return 0;
1316 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1318 const vfs_path_element_t *element;
1319 char **path_tokens, **iterator;
1321 element = vfs_path_get_by_index (vpath, element_index);
1322 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1324 while (*iterator != NULL)
1326 if (**iterator != '\0')
1327 count_tokens++;
1328 iterator++;
1330 g_strfreev (path_tokens);
1332 return count_tokens;
1335 /* --------------------------------------------------------------------------------------------- */
1338 * Get subpath by tokens
1340 * @param vpath path object
1341 * @param start_position first token for got/ Started from 0.
1342 * If negative, then position will be relative to end of path
1343 * @param length count of tokens
1345 * @return newly allocated string with path tokens separated by slash
1348 char *
1349 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1351 GString *ret_tokens, *element_tokens;
1352 int element_index;
1353 size_t tokens_count = vfs_path_tokens_count (vpath);
1355 if (vpath == NULL)
1356 return NULL;
1358 if (length == 0)
1359 length = tokens_count;
1361 if (length < 0)
1362 length = tokens_count + length;
1364 if (start_position < 0)
1365 start_position = (ssize_t) tokens_count + start_position;
1367 if (start_position < 0)
1368 return NULL;
1370 if (start_position >= (ssize_t) tokens_count)
1371 return NULL;
1373 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1374 length = tokens_count - start_position;
1376 ret_tokens = g_string_sized_new (32);
1377 element_tokens = g_string_sized_new (32);
1379 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1381 const vfs_path_element_t *element;
1382 char **path_tokens, **iterator;
1384 g_string_assign (element_tokens, "");
1385 element = vfs_path_get_by_index (vpath, element_index);
1386 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1388 while (*iterator != NULL)
1390 if (**iterator != '\0')
1392 if (start_position == 0)
1394 if (length == 0)
1396 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1397 g_string_free (element_tokens, TRUE);
1398 g_strfreev (path_tokens);
1399 return g_string_free (ret_tokens, FALSE);
1401 length--;
1402 if (element_tokens->len != 0)
1403 g_string_append_c (element_tokens, PATH_SEP);
1404 g_string_append (element_tokens, *iterator);
1406 else
1407 start_position--;
1409 iterator++;
1411 g_strfreev (path_tokens);
1412 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1415 g_string_free (element_tokens, TRUE);
1416 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1419 /* --------------------------------------------------------------------------------------------- */
1421 * Get subpath by tokens
1423 * @param vpath path object
1424 * @param start_position first token for got/ Started from 0.
1425 * If negative, then position will be relative to end of path
1426 * @param length count of tokens
1428 * @return newly allocated path object with path tokens separated by slash
1431 vfs_path_t *
1432 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1434 char *str_tokens;
1435 vfs_path_t *ret_vpath = NULL;
1437 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1438 if (str_tokens != NULL)
1440 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1441 g_free (str_tokens);
1443 return ret_vpath;
1446 /* --------------------------------------------------------------------------------------------- */
1449 * Build URL parameters (such as user:pass @ host:port) from one path element object
1451 * @param element path element
1452 * @param keep_password TRUE or FALSE
1454 * @return newly allocated string
1457 char *
1458 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1460 GString *buffer;
1462 if (element == NULL)
1463 return NULL;
1465 buffer = g_string_new ("");
1467 if (element->user != NULL)
1468 g_string_append (buffer, element->user);
1470 if (element->password != NULL && keep_password)
1472 g_string_append_c (buffer, ':');
1473 g_string_append (buffer, element->password);
1476 if (element->host != NULL)
1478 if ((element->user != NULL) || (element->password != NULL))
1479 g_string_append_c (buffer, '@');
1480 if (element->ipv6)
1481 g_string_append_c (buffer, '[');
1482 g_string_append (buffer, element->host);
1483 if (element->ipv6)
1484 g_string_append_c (buffer, ']');
1487 if ((element->port) != 0 && (element->host != NULL))
1489 g_string_append_c (buffer, ':');
1490 g_string_append_printf (buffer, "%d", element->port);
1493 return g_string_free (buffer, FALSE);
1496 /* --------------------------------------------------------------------------------------------- */
1498 * Build pretty string representation of one path_element_t object
1500 * @param element path element
1502 * @return newly allocated string
1505 char *
1506 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1508 char *url_params;
1509 GString *pretty_path;
1511 pretty_path = g_string_new (element->class->prefix);
1512 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1514 url_params = vfs_path_build_url_params_str (element, FALSE);
1515 g_string_append (pretty_path, url_params);
1516 g_free (url_params);
1518 if (*element->path != PATH_SEP)
1519 g_string_append_c (pretty_path, PATH_SEP);
1521 g_string_append (pretty_path, element->path);
1522 return g_string_free (pretty_path, FALSE);
1525 /* --------------------------------------------------------------------------------------------- */
1527 * Compare two path objects as strings
1529 * @param vpath1 first path object
1530 * @param vpath2 second vpath object
1532 * @return integer value like to strcmp.
1536 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1538 char *path1;
1539 char *path2;
1540 int ret_val;
1542 if (vpath1 == NULL || vpath2 == NULL)
1543 return -1;
1545 path1 = vfs_path_to_str (vpath1);
1546 path2 = vfs_path_to_str (vpath2);
1548 ret_val = strcmp (path1, path2);
1550 g_free (path1);
1551 g_free (path2);
1553 return ret_val;
1556 /* --------------------------------------------------------------------------------------------- */
1558 * Compare two path objects as strings
1560 * @param vpath1 first path object
1561 * @param vpath2 second vpath object
1562 * @param len number of first 'len' characters
1564 * @return integer value like to strcmp.
1568 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1570 char *path1;
1571 char *path2;
1572 int ret_val;
1574 if (vpath1 == NULL || vpath2 == NULL)
1575 return -1;
1577 path1 = vfs_path_to_str (vpath1);
1578 path2 = vfs_path_to_str (vpath2);
1580 ret_val = strncmp (path1, path2, len);
1582 g_free (path1);
1583 g_free (path2);
1585 return ret_val;
1588 /* --------------------------------------------------------------------------------------------- */
1590 * Calculate path length in string representation
1592 * @param vpath path object
1594 * @return length of path
1597 size_t
1598 vfs_path_len (const vfs_path_t * vpath)
1600 char *path;
1601 size_t ret_val;
1603 if (vpath == NULL)
1604 return 0;
1606 path = vfs_path_to_str (vpath);
1607 ret_val = strlen (path);
1608 g_free (path);
1609 return ret_val;
1612 /* --------------------------------------------------------------------------------------------- */
1614 * Convert relative vpath object to absolute
1616 * @param vpath path object
1618 * @return absolute path object
1621 vfs_path_t *
1622 vfs_path_to_absolute (const vfs_path_t * vpath)
1624 vfs_path_t *absolute_vpath;
1625 char *path_str;
1627 if (!vpath->relative)
1628 return vfs_path_clone (vpath);
1630 path_str = vfs_path_to_str (vpath);
1631 absolute_vpath = vfs_path_from_str (path_str);
1632 g_free (path_str);
1633 return absolute_vpath;
1636 /* --------------------------------------------------------------------------------------------- */