6d1ebcec39e4e680cf46a784b8061ce71f71fe44
[midnight-commander.git] / lib / vfs / path.c
blob6d1ebcec39e4e680cf46a784b8061ce71f71fe44
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 *dir, *colon, *inner_colon, *at, *rest;
248 path_element->port = 0;
250 pcopy = g_strdup (path);
251 pend = pcopy + strlen (pcopy);
252 dir = pcopy;
254 /* search for any possible user */
255 at = strrchr (pcopy, '@');
257 /* We have a username */
258 if (at == NULL)
259 rest = pcopy;
260 else
262 *at = '\0';
263 inner_colon = strchr (pcopy, ':');
264 if (inner_colon != NULL)
266 *inner_colon = '\0';
267 inner_colon++;
268 path_element->password = g_strdup (inner_colon);
271 if (*pcopy != '\0')
272 path_element->user = g_strdup (pcopy);
274 if (pend == at + 1)
275 rest = at;
276 else
277 rest = at + 1;
280 /* Check if the host comes with a port spec, if so, chop it */
281 if (*rest != '[')
282 colon = strchr (rest, ':');
283 else
285 colon = strchr (++rest, ']');
286 if (colon != NULL)
288 colon[0] = '\0';
289 colon[1] = '\0';
290 colon++;
291 path_element->ipv6 = TRUE;
295 if (colon != NULL)
297 *colon = '\0';
298 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
300 if (path_element->port <= 0 || path_element->port >= 65536)
301 path_element->port = 0;
303 else
304 while (*(++colon) != '\0')
306 switch (*colon)
308 case 'C':
309 path_element->port = 1;
310 break;
311 case 'r':
312 path_element->port = 2;
313 break;
317 path_element->host = g_strdup (rest);
318 g_free (pcopy);
321 /* --------------------------------------------------------------------------------------------- */
323 * get VFS class for the given name
325 * @param class_name name of class
327 * @return pointer to class structure or NULL if class not found
330 static struct vfs_class *
331 vfs_get_class_by_name (const char *class_name)
333 guint i;
335 if (class_name == NULL)
336 return NULL;
338 for (i = 0; i < vfs__classes_list->len; i++)
340 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
341 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
342 return vfs;
345 return NULL;
348 /* --------------------------------------------------------------------------------------------- */
350 * Check if path string contain URL-like elements
352 * @param path_str path
354 * @return TRUE if path is deprecated or FALSE otherwise
357 static gboolean
358 vfs_path_is_str_path_deprecated (const char *path_str)
360 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
363 /* --------------------------------------------------------------------------------------------- */
364 /** Split path string to path elements by deprecated algorithm.
366 * @param path_str VFS-path
368 * @return pointer to newly created vfs_path_t object with filled path elements array.
371 static vfs_path_t *
372 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
374 vfs_path_t *vpath;
375 vfs_path_element_t *element;
376 struct vfs_class *class;
377 const char *local, *op;
379 (void) flags;
380 vpath = vfs_path_new ();
382 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
384 char *url_params;
385 element = g_new0 (vfs_path_element_t, 1);
386 element->class = class;
387 if (local == NULL)
388 local = "";
389 element->path = vfs_translate_path_n (local);
391 #ifdef HAVE_CHARSET
392 element->encoding = vfs_get_encoding (local);
393 element->dir.converter =
394 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
395 #endif
397 url_params = strchr (op, ':'); /* skip VFS prefix */
398 if (url_params != NULL)
400 *url_params = '\0';
401 url_params++;
402 vfs_path_url_split (element, url_params);
405 if (*op != '\0')
406 element->vfs_prefix = g_strdup (op);
408 g_array_prepend_val (vpath->path, element);
410 if (path[0] != '\0')
412 element = g_new0 (vfs_path_element_t, 1);
413 element->class = g_ptr_array_index (vfs__classes_list, 0);
414 element->path = vfs_translate_path_n (path);
416 #ifdef HAVE_CHARSET
417 element->encoding = vfs_get_encoding (path);
418 element->dir.converter =
419 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
420 #endif
421 g_array_prepend_val (vpath->path, element);
424 return vpath;
427 /* --------------------------------------------------------------------------------------------- */
428 /** Split path string to path elements by URL algorithm.
430 * @param path_str VFS-path
431 * @param flags flags for converter
433 * @return pointer to newly created vfs_path_t object with filled path elements array.
436 static vfs_path_t *
437 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
439 vfs_path_t *vpath;
440 vfs_path_element_t *element;
442 char *url_delimiter;
444 vpath = vfs_path_new ();
445 vpath->relative = (flags & VPF_NO_CANON) != 0;
447 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
449 char *vfs_prefix_start;
450 char *real_vfs_prefix_start = url_delimiter;
451 char *slash_pointer;
452 struct vfs_s_subclass *sub = NULL;
454 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
455 real_vfs_prefix_start--;
456 vfs_prefix_start = real_vfs_prefix_start;
458 if (*(vfs_prefix_start) == PATH_SEP)
459 vfs_prefix_start += 1;
461 *url_delimiter = '\0';
463 element = g_new0 (vfs_path_element_t, 1);
464 element->class = vfs_prefix_to_class (vfs_prefix_start);
465 element->vfs_prefix = g_strdup (vfs_prefix_start);
467 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
468 sub = VFSDATA (element);
469 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
471 slash_pointer = strchr (url_delimiter, PATH_SEP);
472 if (slash_pointer == NULL)
474 element->path = g_strdup ("");
476 else
478 element->path = vfs_translate_path_n (slash_pointer + 1);
479 #ifdef HAVE_CHARSET
480 element->encoding = vfs_get_encoding (slash_pointer);
481 #endif
482 *slash_pointer = '\0';
484 vfs_path_url_split (element, url_delimiter);
486 else
488 element->path = vfs_translate_path_n (url_delimiter);
489 #ifdef HAVE_CHARSET
490 element->encoding = vfs_get_encoding (url_delimiter);
491 #endif
493 #ifdef HAVE_CHARSET
494 element->dir.converter =
495 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
496 #endif
497 g_array_prepend_val (vpath->path, element);
499 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
500 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
501 *real_vfs_prefix_start = '\0';
502 else
503 *(real_vfs_prefix_start + 1) = '\0';
506 if (path[0] != '\0')
508 element = g_new0 (vfs_path_element_t, 1);
509 element->class = g_ptr_array_index (vfs__classes_list, 0);
510 element->path = vfs_translate_path_n (path);
511 #ifdef HAVE_CHARSET
512 element->encoding = vfs_get_encoding (path);
513 element->dir.converter =
514 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
515 #endif
516 g_array_prepend_val (vpath->path, element);
519 return vpath;
522 /* --------------------------------------------------------------------------------------------- */
524 * Add element's class info to result string (such as VFS name, host, encoding etc)
525 * This function used as helper only in vfs_path_tokens_get() function
527 * @param element current path element
528 * @param ret_tokens total tikens for return
529 * @param element_tokens accumulated element-only tokens
532 static void
533 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
534 GString * element_tokens)
536 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
537 && element_tokens->len > 0)
539 char *url_str;
541 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
542 g_string_append_c (ret_tokens, PATH_SEP);
544 g_string_append (ret_tokens, element->vfs_prefix);
545 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
547 url_str = vfs_path_build_url_params_str (element, TRUE);
548 if (*url_str != '\0')
550 g_string_append (ret_tokens, url_str);
551 g_string_append_c (ret_tokens, PATH_SEP);
554 g_free (url_str);
557 #ifdef HAVE_CHARSET
558 if (element->encoding != NULL)
560 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
561 g_string_append (ret_tokens, PATH_SEP_STR);
562 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
563 g_string_append (ret_tokens, element->encoding);
564 g_string_append (ret_tokens, PATH_SEP_STR);
566 #endif
568 g_string_append (ret_tokens, element_tokens->str);
571 /* --------------------------------------------------------------------------------------------- */
573 * Strip path to home dir.
574 * @param dir pointer to string contains full path
577 static char *
578 vfs_path_strip_home (const char *dir)
580 const char *home_dir = mc_config_get_home_dir ();
582 if (home_dir != NULL)
584 size_t len;
586 len = strlen (home_dir);
588 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
589 return g_strdup_printf ("~%s", dir + len);
592 return g_strdup (dir);
595 /* --------------------------------------------------------------------------------------------- */
597 /* --------------------------------------------------------------------------------------------- */
598 /*** public functions ****************************************************************************/
599 /* --------------------------------------------------------------------------------------------- */
601 #define vfs_append_from_path(appendfrom, is_relative) \
603 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
605 char *stripped_home_str; \
606 stripped_home_str = vfs_path_strip_home (appendfrom); \
607 g_string_append (buffer, stripped_home_str); \
608 g_free (stripped_home_str); \
610 else \
612 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
613 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
614 g_string_append_c (buffer, PATH_SEP); \
615 g_string_append (buffer, appendfrom); \
620 * Convert first elements_count elements from vfs_path_t to string representation with flags.
622 * @param vpath pointer to vfs_path_t object
623 * @param elements_count count of first elements for convert
624 * @param flags for converter
626 * @return pointer to newly created string.
629 char *
630 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
632 int element_index;
633 GString *buffer;
634 GString *recode_buffer;
636 if (vpath == NULL)
637 return NULL;
639 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
640 elements_count = vfs_path_elements_count (vpath);
642 if (elements_count < 0)
643 elements_count = vfs_path_elements_count (vpath) + elements_count;
645 buffer = g_string_new ("");
646 recode_buffer = g_string_new ("");
648 for (element_index = 0; element_index < elements_count; element_index++)
650 const vfs_path_element_t *element;
651 gboolean is_relative = vpath->relative && (element_index == 0);
653 element = vfs_path_get_by_index (vpath, element_index);
654 if (element->vfs_prefix != NULL)
656 char *url_str;
657 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
658 g_string_append_c (buffer, PATH_SEP);
660 g_string_append (buffer, element->vfs_prefix);
661 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
663 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
665 if (*url_str != '\0')
667 g_string_append (buffer, url_str);
668 g_string_append_c (buffer, PATH_SEP);
671 g_free (url_str);
674 #ifdef HAVE_CHARSET
675 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
677 if ((flags & VPF_HIDE_CHARSET) == 0)
679 if ((!is_relative)
680 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
681 g_string_append (buffer, PATH_SEP_STR);
682 g_string_append (buffer, VFS_ENCODING_PREFIX);
683 g_string_append (buffer, element->encoding);
685 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
686 vfs_append_from_path (recode_buffer->str, is_relative);
687 g_string_set_size (recode_buffer, 0);
689 else
690 #endif
692 vfs_append_from_path (element->path, is_relative);
695 g_string_free (recode_buffer, TRUE);
696 return g_string_free (buffer, FALSE);
699 #undef vfs_append_from_path
701 /* --------------------------------------------------------------------------------------------- */
703 * Convert first elements_count elements from vfs_path_t to string representation.
705 * @param vpath pointer to vfs_path_t object
706 * @param elements_count count of first elements for convert
708 * @return pointer to newly created string.
711 char *
712 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
714 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
717 /* --------------------------------------------------------------------------------------------- */
719 * Convert vfs_path_t to string representation.
721 * @param vpath pointer to vfs_path_t object
723 * @return pointer to newly created string.
726 char *
727 vfs_path_to_str (const vfs_path_t * vpath)
729 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
732 /* --------------------------------------------------------------------------------------------- */
734 * Split path string to path elements with flags for change parce process.
736 * @param path_str VFS-path
737 * @param flags flags for parser
739 * @return pointer to newly created vfs_path_t object with filled path elements array.
742 vfs_path_t *
743 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
745 vfs_path_t *vpath;
746 char *path;
748 if (path_str == NULL)
749 return NULL;
751 if ((flags & VPF_NO_CANON) == 0)
752 path = vfs_canon (path_str);
753 else
754 path = g_strdup (path_str);
756 if (path == NULL)
757 return NULL;
759 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
760 vpath = vfs_path_from_str_deprecated_parser (path, flags);
761 else
762 vpath = vfs_path_from_str_uri_parser (path, flags);
764 g_free (path);
766 return vpath;
769 /* --------------------------------------------------------------------------------------------- */
771 * Split path string to path elements.
773 * @param path_str VFS-path
775 * @return pointer to newly created vfs_path_t object with filled path elements array.
778 vfs_path_t *
779 vfs_path_from_str (const char *path_str)
781 return vfs_path_from_str_flags (path_str, VPF_NONE);
784 /* --------------------------------------------------------------------------------------------- */
786 * Create new vfs_path_t object.
788 * @return pointer to newly created vfs_path_t object.
791 vfs_path_t *
792 vfs_path_new (void)
794 vfs_path_t *vpath;
796 vpath = g_new0 (vfs_path_t, 1);
797 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
799 return vpath;
802 /* --------------------------------------------------------------------------------------------- */
804 * Get count of path elements.
806 * @param vpath pointer to vfs_path_t object
808 * @return count of path elements.
812 vfs_path_elements_count (const vfs_path_t * vpath)
814 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
817 /* --------------------------------------------------------------------------------------------- */
819 * Add vfs_path_element_t object to end of list in vfs_path_t object
820 * @param vpath pointer to vfs_path_t object
821 * @param path_element pointer to vfs_path_element_t object
824 void
825 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
827 g_array_append_val (vpath->path, path_element);
830 /* --------------------------------------------------------------------------------------------- */
832 * Get one path element by index.
834 * @param vpath pointer to vfs_path_t object
835 * @param element_index element index. May have negative value (in this case count was started at the end of list).
837 * @return path element.
840 const vfs_path_element_t *
841 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
843 if (vpath == NULL)
844 return NULL;
846 if (element_index < 0)
847 element_index += vfs_path_elements_count (vpath);
849 if (element_index < 0)
850 vfs_die ("vfs_path_get_by_index: incorrect index!");
852 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
855 /* --------------------------------------------------------------------------------------------- */
857 * Clone one path element
859 * @param element pointer to vfs_path_element_t object
861 * @return Newly allocated path element
864 vfs_path_element_t *
865 vfs_path_element_clone (const vfs_path_element_t * element)
867 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
869 new_element->user = g_strdup (element->user);
870 new_element->password = g_strdup (element->password);
871 new_element->host = g_strdup (element->host);
872 new_element->ipv6 = element->ipv6;
873 new_element->port = element->port;
874 new_element->path = g_strdup (element->path);
875 new_element->class = element->class;
876 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
877 #ifdef HAVE_CHARSET
878 new_element->encoding = g_strdup (element->encoding);
879 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
880 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
881 else
882 new_element->dir.converter = element->dir.converter;
883 #endif
884 new_element->dir.info = element->dir.info;
886 return new_element;
889 /* --------------------------------------------------------------------------------------------- */
891 * Free one path element.
893 * @param element pointer to vfs_path_element_t object
897 void
898 vfs_path_element_free (vfs_path_element_t * element)
900 if (element == NULL)
901 return;
903 g_free (element->user);
904 g_free (element->password);
905 g_free (element->host);
906 g_free (element->path);
907 g_free (element->vfs_prefix);
909 #ifdef HAVE_CHARSET
910 g_free (element->encoding);
912 if (vfs_path_element_need_cleanup_converter (element))
913 str_close_conv (element->dir.converter);
914 #endif
916 g_free (element);
919 /* --------------------------------------------------------------------------------------------- */
921 * Clone path
923 * @param vpath pointer to vfs_path_t object
925 * @return Newly allocated path object
928 vfs_path_t *
929 vfs_path_clone (const vfs_path_t * vpath)
931 vfs_path_t *new_vpath;
932 int vpath_element_index;
934 if (vpath == NULL)
935 return NULL;
937 new_vpath = vfs_path_new ();
938 new_vpath->relative = vpath->relative;
940 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
941 vpath_element_index++)
943 vfs_path_element_t *path_element;
945 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
946 g_array_append_val (new_vpath->path, path_element);
949 return new_vpath;
952 /* --------------------------------------------------------------------------------------------- */
954 * Free vfs_path_t object.
956 * @param vpath pointer to vfs_path_t object
960 void
961 vfs_path_free (vfs_path_t * vpath)
963 int vpath_element_index;
965 if (vpath == NULL)
966 return;
968 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
969 vpath_element_index++)
971 vfs_path_element_t *path_element;
973 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
974 vfs_path_element_free (path_element);
977 g_array_free (vpath->path, TRUE);
978 g_free (vpath);
981 /* --------------------------------------------------------------------------------------------- */
983 * Remove one path element by index
985 * @param vpath pointer to vfs_path_t object
986 * @param element_index element index. May have negative value (in this case count was started at the end of list).
990 void
991 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
993 vfs_path_element_t *element;
995 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
996 return;
998 if (element_index < 0)
999 element_index = vfs_path_elements_count (vpath) + element_index;
1001 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1002 vpath->path = g_array_remove_index (vpath->path, element_index);
1003 vfs_path_element_free (element);
1006 /* --------------------------------------------------------------------------------------------- */
1007 /** Return VFS class for the given prefix */
1009 struct vfs_class *
1010 vfs_prefix_to_class (const char *prefix)
1012 guint i;
1014 /* Avoid first class (localfs) that would accept any prefix */
1015 for (i = 1; i < vfs__classes_list->len; i++)
1017 struct vfs_class *vfs;
1019 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1020 if (vfs->which != NULL)
1022 if (vfs->which (vfs, prefix) == -1)
1023 continue;
1024 return vfs;
1027 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1028 return vfs;
1031 return NULL;
1034 /* --------------------------------------------------------------------------------------------- */
1036 #ifdef HAVE_CHARSET
1039 * Check if need cleanup charset converter for vfs_path_element_t
1041 * @param element part of path
1043 * @return TRUE if need cleanup converter or FALSE otherwise
1046 gboolean
1047 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1049 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1051 #endif
1053 /* --------------------------------------------------------------------------------------------- */
1056 * Serialize vfs_path_t object to string
1058 * @param vpath data for serialization
1059 * @param error contain pointer to object for handle error code and message
1061 * @returns serialized vpath as newly allocated string
1064 char *
1065 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1067 mc_config_t *cpath;
1068 ssize_t element_index;
1069 char *ret_value;
1071 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1073 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1074 return NULL;
1078 cpath = mc_config_init (NULL, FALSE);
1080 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1082 char *groupname;
1083 const vfs_path_element_t *element;
1085 groupname = g_strdup_printf ("path-element-%zd", element_index);
1086 element = vfs_path_get_by_index (vpath, element_index);
1087 /* convert one element to config group */
1089 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1090 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1091 #ifdef HAVE_CHARSET
1092 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1093 #endif
1094 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1096 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1097 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1098 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1099 if (element->port != 0)
1100 mc_config_set_int (cpath, groupname, "port", element->port);
1102 g_free (groupname);
1105 ret_value = mc_serialize_config (cpath, error);
1106 mc_config_deinit (cpath);
1107 return ret_value;
1110 /* --------------------------------------------------------------------------------------------- */
1112 * Deserialize string to vfs_path_t object
1114 * @param data data for serialization
1115 * @param error contain pointer to object for handle error code and message
1117 * @return newly allocated vfs_path_t object
1120 vfs_path_t *
1121 vfs_path_deserialize (const char *data, GError ** error)
1123 mc_config_t *cpath;
1124 size_t element_index = 0;
1125 vfs_path_t *vpath;
1127 cpath = mc_deserialize_config (data, error);
1128 if (cpath == NULL)
1129 return NULL;
1131 vpath = vfs_path_new ();
1133 while (TRUE)
1135 vfs_path_element_t *element;
1136 char *cfg_value;
1137 char *groupname;
1139 groupname = g_strdup_printf ("path-element-%zd", element_index);
1140 if (!mc_config_has_group (cpath, groupname))
1142 g_free (groupname);
1143 break;
1146 element = g_new0 (vfs_path_element_t, 1);
1148 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1149 element->class = vfs_get_class_by_name (cfg_value);
1150 if (element->class == NULL)
1152 g_free (element);
1153 vfs_path_free (vpath);
1154 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1155 g_free (cfg_value);
1156 mc_config_deinit (cpath);
1157 return NULL;
1159 g_free (cfg_value);
1161 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1163 #ifdef HAVE_CHARSET
1164 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1165 element->dir.converter =
1166 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1167 #endif
1169 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1171 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1172 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1173 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1174 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1176 vpath->path = g_array_append_val (vpath->path, element);
1178 g_free (groupname);
1179 element_index++;
1182 mc_config_deinit (cpath);
1183 if (vfs_path_elements_count (vpath) == 0)
1185 vfs_path_free (vpath);
1186 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1187 return NULL;
1190 return vpath;
1193 /* --------------------------------------------------------------------------------------------- */
1195 * Build vfs_path_t object from arguments.
1197 * @param first_element of path
1198 * @param ... path tokens, terminated by NULL
1200 * @return newly allocated vfs_path_t object
1203 vfs_path_t *
1204 vfs_path_build_filename (const char *first_element, ...)
1206 va_list args;
1207 char *str_path;
1208 vfs_path_t *vpath;
1210 if (first_element == NULL)
1211 return NULL;
1213 va_start (args, first_element);
1214 str_path = mc_build_filenamev (first_element, args);
1215 va_end (args);
1216 vpath = vfs_path_from_str (str_path);
1217 g_free (str_path);
1218 return vpath;
1221 /* --------------------------------------------------------------------------------------------- */
1223 * Append tokens to path object
1225 * @param vpath path object
1226 * @param first_element of path
1227 * @param ... NULL-terminated strings
1229 * @return newly allocated path object
1232 vfs_path_t *
1233 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1235 va_list args;
1236 char *str_path, *result_str;
1237 vfs_path_t *ret_vpath;
1239 if (vpath == NULL || first_element == NULL)
1240 return NULL;
1242 va_start (args, first_element);
1243 str_path = mc_build_filenamev (first_element, args);
1244 va_end (args);
1246 result_str = vfs_path_to_str (vpath);
1247 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1248 g_free (result_str);
1249 g_free (str_path);
1251 return ret_vpath;
1255 /* --------------------------------------------------------------------------------------------- */
1258 * Append vpath_t tokens to path object
1260 * @param first_vpath vpath objects
1261 * @param ... NULL-terminated vpath objects
1263 * @return newly allocated path object
1266 vfs_path_t *
1267 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1269 va_list args;
1270 vfs_path_t *ret_vpath;
1271 const vfs_path_t *current_vpath = first_vpath;
1273 if (first_vpath == NULL)
1274 return NULL;
1276 ret_vpath = vfs_path_new ();
1278 va_start (args, first_vpath);
1281 int vindex;
1283 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1285 vfs_path_element_t *path_element;
1287 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1288 g_array_append_val (ret_vpath->path, path_element);
1290 current_vpath = va_arg (args, const vfs_path_t *);
1292 while (current_vpath != NULL);
1293 va_end (args);
1295 return ret_vpath;
1298 /* --------------------------------------------------------------------------------------------- */
1301 * get tockens count in path.
1303 * @param vpath path object
1305 * @return count of tokens
1308 size_t
1309 vfs_path_tokens_count (const vfs_path_t * vpath)
1311 size_t count_tokens = 0;
1312 int element_index;
1314 if (vpath == NULL)
1315 return 0;
1317 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1319 const vfs_path_element_t *element;
1320 char **path_tokens, **iterator;
1322 element = vfs_path_get_by_index (vpath, element_index);
1323 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1325 while (*iterator != NULL)
1327 if (**iterator != '\0')
1328 count_tokens++;
1329 iterator++;
1331 g_strfreev (path_tokens);
1333 return count_tokens;
1336 /* --------------------------------------------------------------------------------------------- */
1339 * Get subpath by tokens
1341 * @param vpath path object
1342 * @param start_position first token for got/ Started from 0.
1343 * If negative, then position will be relative to end of path
1344 * @param length count of tokens
1346 * @return newly allocated string with path tokens separated by slash
1349 char *
1350 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1352 GString *ret_tokens, *element_tokens;
1353 int element_index;
1354 size_t tokens_count = vfs_path_tokens_count (vpath);
1356 if (vpath == NULL)
1357 return NULL;
1359 if (length == 0)
1360 length = tokens_count;
1362 if (length < 0)
1363 length = tokens_count + length;
1365 if (start_position < 0)
1366 start_position = (ssize_t) tokens_count + start_position;
1368 if (start_position < 0)
1369 return NULL;
1371 if (start_position >= (ssize_t) tokens_count)
1372 return NULL;
1374 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1375 length = tokens_count - start_position;
1377 ret_tokens = g_string_sized_new (32);
1378 element_tokens = g_string_sized_new (32);
1380 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1382 const vfs_path_element_t *element;
1383 char **path_tokens, **iterator;
1385 g_string_assign (element_tokens, "");
1386 element = vfs_path_get_by_index (vpath, element_index);
1387 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1389 while (*iterator != NULL)
1391 if (**iterator != '\0')
1393 if (start_position == 0)
1395 if (length == 0)
1397 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1398 g_string_free (element_tokens, TRUE);
1399 g_strfreev (path_tokens);
1400 return g_string_free (ret_tokens, FALSE);
1402 length--;
1403 if (element_tokens->len != 0)
1404 g_string_append_c (element_tokens, PATH_SEP);
1405 g_string_append (element_tokens, *iterator);
1407 else
1408 start_position--;
1410 iterator++;
1412 g_strfreev (path_tokens);
1413 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1416 g_string_free (element_tokens, TRUE);
1417 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1420 /* --------------------------------------------------------------------------------------------- */
1422 * Get subpath by tokens
1424 * @param vpath path object
1425 * @param start_position first token for got/ Started from 0.
1426 * If negative, then position will be relative to end of path
1427 * @param length count of tokens
1429 * @return newly allocated path object with path tokens separated by slash
1432 vfs_path_t *
1433 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1435 char *str_tokens;
1436 vfs_path_t *ret_vpath = NULL;
1438 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1439 if (str_tokens != NULL)
1441 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1442 g_free (str_tokens);
1444 return ret_vpath;
1447 /* --------------------------------------------------------------------------------------------- */
1450 * Build URL parameters (such as user:pass @ host:port) from one path element object
1452 * @param element path element
1453 * @param keep_password TRUE or FALSE
1455 * @returns newly allocated string
1458 char *
1459 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1461 GString *buffer;
1463 if (element == NULL)
1464 return NULL;
1466 buffer = g_string_new ("");
1468 if (element->user != NULL)
1469 g_string_append (buffer, element->user);
1471 if (element->password != NULL && keep_password)
1473 g_string_append_c (buffer, ':');
1474 g_string_append (buffer, element->password);
1477 if (element->host != NULL)
1479 if ((element->user != NULL) || (element->password != NULL))
1480 g_string_append_c (buffer, '@');
1481 if (element->ipv6)
1482 g_string_append_c (buffer, '[');
1483 g_string_append (buffer, element->host);
1484 if (element->ipv6)
1485 g_string_append_c (buffer, ']');
1488 if ((element->port) != 0 && (element->host != NULL))
1490 g_string_append_c (buffer, ':');
1491 g_string_append_printf (buffer, "%d", element->port);
1494 return g_string_free (buffer, FALSE);
1497 /* --------------------------------------------------------------------------------------------- */
1499 * Build pretty string representation of one path_element_t object
1501 * @param element path element
1503 * @return newly allocated string
1506 char *
1507 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1509 char *url_params;
1510 GString *pretty_path;
1512 pretty_path = g_string_new (element->class->prefix);
1513 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1515 url_params = vfs_path_build_url_params_str (element, FALSE);
1516 g_string_append (pretty_path, url_params);
1517 g_free (url_params);
1519 if (*element->path != PATH_SEP)
1520 g_string_append_c (pretty_path, PATH_SEP);
1522 g_string_append (pretty_path, element->path);
1523 return g_string_free (pretty_path, FALSE);
1526 /* --------------------------------------------------------------------------------------------- */
1528 * Compare two path objects as strings
1530 * @param vpath1 first path object
1531 * @param vpath2 second vpath object
1533 * @return integer value like to strcmp.
1537 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1539 char *path1;
1540 char *path2;
1541 int ret_val;
1543 if (vpath1 == NULL || vpath2 == NULL)
1544 return -1;
1546 path1 = vfs_path_to_str (vpath1);
1547 path2 = vfs_path_to_str (vpath2);
1549 ret_val = strcmp (path1, path2);
1551 g_free (path1);
1552 g_free (path2);
1554 return ret_val;
1557 /* --------------------------------------------------------------------------------------------- */
1559 * Compare two path objects as strings
1561 * @param vpath1 first path object
1562 * @param vpath2 second vpath object
1563 * @param len number of first 'len' characters
1565 * @return integer value like to strcmp.
1569 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1571 char *path1;
1572 char *path2;
1573 int ret_val;
1575 if (vpath1 == NULL || vpath2 == NULL)
1576 return -1;
1578 path1 = vfs_path_to_str (vpath1);
1579 path2 = vfs_path_to_str (vpath2);
1581 ret_val = strncmp (path1, path2, len);
1583 g_free (path1);
1584 g_free (path2);
1586 return ret_val;
1589 /* --------------------------------------------------------------------------------------------- */
1591 * Calculate path length in string representation
1593 * @param vpath path object
1595 * @return length of path
1598 size_t
1599 vfs_path_len (const vfs_path_t * vpath)
1601 char *path;
1602 size_t ret_val;
1604 if (vpath == NULL)
1605 return 0;
1607 path = vfs_path_to_str (vpath);
1608 ret_val = strlen (path);
1609 g_free (path);
1610 return ret_val;
1613 /* --------------------------------------------------------------------------------------------- */
1615 * Convert relative vpath object to absolute
1617 * @param vpath path object
1619 * @return absolute path object
1622 vfs_path_t *
1623 vfs_path_to_absolute (const vfs_path_t * vpath)
1625 vfs_path_t *absolute_vpath;
1626 char *path_str;
1628 if (!vpath->relative)
1629 return vfs_path_clone (vpath);
1631 path_str = vfs_path_to_str (vpath);
1632 absolute_vpath = vfs_path_from_str (path_str);
1633 g_free (path_str);
1634 return absolute_vpath;
1637 /* --------------------------------------------------------------------------------------------- */