Ticket #2791: Fixed: cannot Copy/Move files with filename encoding change
[midnight-commander.git] / lib / vfs / path.c
blob740c79f1fc7ea3f3461d55d72da3dab94ad4eae9
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 local = tilde_expand (path);
156 if (*local != PATH_SEP)
158 char *curr_dir;
160 g_free (local);
161 curr_dir = vfs_get_current_dir ();
162 local = mc_build_filename (curr_dir, path, NULL);
163 g_free (curr_dir);
166 result = vfs_canon (local);
167 g_free (local);
168 return result;
172 * So we have path of following form:
173 * /p1/p2#op/.././././p3#op/p4. Good luck.
176 char *result;
178 result = g_strdup (path);
179 canonicalize_pathname (result);
180 return result;
184 /* --------------------------------------------------------------------------------------------- */
186 #ifdef HAVE_CHARSET
187 /** get encoding after last #enc: or NULL, if part does not contain #enc:
189 * @param path string
191 * @return newly allocated string.
194 static char *
195 vfs_get_encoding (const char *path)
197 char result[16];
198 char *work;
199 char *semi;
200 char *slash;
201 work = g_strdup (path);
203 /* try found #enc: */
204 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
206 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
208 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
209 slash = strchr (semi, PATH_SEP);
210 if (slash != NULL)
211 slash[0] = '\0';
213 g_strlcpy (result, semi, sizeof (result));
214 g_free (work);
215 return g_strdup (result);
217 else
219 g_free (work);
220 return NULL;
223 #endif
225 /* --------------------------------------------------------------------------------------------- */
226 /** Extract the hostname and username from the path
228 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
230 * ftp://sunsite.unc.edu/pub/linux
231 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
232 * ftp://tsx-11.mit.edu:8192/
233 * ftp://joe@foo.edu:11321/private
234 * ftp://joe:password@foo.se
236 * @param path_element is an input string to be parsed
237 * @param path is an input string to be parsed
239 * @return g_malloc()ed url info.
240 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
241 * is not set, then the current login name is supplied.
242 * Return value is a g_malloc()ed structure with the pathname relative to the
243 * host.
246 static void
247 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
249 char *pcopy;
250 const char *pend;
251 char *dir, *colon, *inner_colon, *at, *rest;
253 path_element->port = 0;
255 pcopy = g_strdup (path);
256 pend = pcopy + strlen (pcopy);
257 dir = pcopy;
259 /* search for any possible user */
260 at = strrchr (pcopy, '@');
262 /* We have a username */
263 if (at == NULL)
264 rest = pcopy;
265 else
267 *at = '\0';
268 inner_colon = strchr (pcopy, ':');
269 if (inner_colon != NULL)
271 *inner_colon = '\0';
272 inner_colon++;
273 path_element->password = g_strdup (inner_colon);
276 if (*pcopy != '\0')
277 path_element->user = g_strdup (pcopy);
279 if (pend == at + 1)
280 rest = at;
281 else
282 rest = at + 1;
285 /* Check if the host comes with a port spec, if so, chop it */
286 if (*rest != '[')
287 colon = strchr (rest, ':');
288 else
290 colon = strchr (++rest, ']');
291 if (colon != NULL)
293 colon[0] = '\0';
294 colon[1] = '\0';
295 colon++;
296 path_element->ipv6 = TRUE;
300 if (colon != NULL)
302 *colon = '\0';
303 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
305 if (path_element->port <= 0 || path_element->port >= 65536)
306 path_element->port = 0;
308 else
309 while (*(++colon) != '\0')
311 switch (*colon)
313 case 'C':
314 path_element->port = 1;
315 break;
316 case 'r':
317 path_element->port = 2;
318 break;
322 path_element->host = g_strdup (rest);
323 g_free (pcopy);
326 /* --------------------------------------------------------------------------------------------- */
328 * get VFS class for the given name
330 * @param class_name name of class
332 * @return pointer to class structure or NULL if class not found
335 static struct vfs_class *
336 vfs_get_class_by_name (const char *class_name)
338 guint i;
340 if (class_name == NULL)
341 return NULL;
343 for (i = 0; i < vfs__classes_list->len; i++)
345 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
346 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
347 return vfs;
350 return NULL;
353 /* --------------------------------------------------------------------------------------------- */
355 * Check if path string contain URL-like elements
357 * @param path_str path
359 * @return TRUE if path is deprecated or FALSE otherwise
362 static gboolean
363 vfs_path_is_str_path_deprecated (const char *path_str)
365 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
368 /* --------------------------------------------------------------------------------------------- */
369 /** Split path string to path elements by deprecated algorithm.
371 * @param path_str VFS-path
373 * @return pointer to newly created vfs_path_t object with filled path elements array.
376 static vfs_path_t *
377 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
379 vfs_path_t *vpath;
380 vfs_path_element_t *element;
381 struct vfs_class *class;
382 const char *local, *op;
384 (void) flags;
385 vpath = vfs_path_new ();
387 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
389 char *url_params;
390 element = g_new0 (vfs_path_element_t, 1);
391 element->class = class;
392 if (local == NULL)
393 local = "";
394 element->path = vfs_translate_path_n (local);
396 #ifdef HAVE_CHARSET
397 element->encoding = vfs_get_encoding (local);
398 element->dir.converter =
399 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
400 #endif
402 url_params = strchr (op, ':'); /* skip VFS prefix */
403 if (url_params != NULL)
405 *url_params = '\0';
406 url_params++;
407 vfs_path_url_split (element, url_params);
410 if (*op != '\0')
411 element->vfs_prefix = g_strdup (op);
413 g_array_prepend_val (vpath->path, element);
415 if (path[0] != '\0')
417 element = g_new0 (vfs_path_element_t, 1);
418 element->class = g_ptr_array_index (vfs__classes_list, 0);
419 element->path = vfs_translate_path_n (path);
421 #ifdef HAVE_CHARSET
422 element->encoding = vfs_get_encoding (path);
423 element->dir.converter =
424 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
425 #endif
426 g_array_prepend_val (vpath->path, element);
429 return vpath;
432 /* --------------------------------------------------------------------------------------------- */
433 /** Split path string to path elements by URL algorithm.
435 * @param path_str VFS-path
436 * @param flags flags for converter
438 * @return pointer to newly created vfs_path_t object with filled path elements array.
441 static vfs_path_t *
442 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
444 vfs_path_t *vpath;
445 vfs_path_element_t *element;
447 char *url_delimiter;
449 vpath = vfs_path_new ();
450 vpath->relative = (flags & VPF_NO_CANON) != 0;
452 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
454 char *vfs_prefix_start;
455 char *real_vfs_prefix_start = url_delimiter;
456 char *slash_pointer;
457 struct vfs_s_subclass *sub = NULL;
459 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
460 real_vfs_prefix_start--;
461 vfs_prefix_start = real_vfs_prefix_start;
463 if (*(vfs_prefix_start) == PATH_SEP)
464 vfs_prefix_start += 1;
466 *url_delimiter = '\0';
468 element = g_new0 (vfs_path_element_t, 1);
469 element->class = vfs_prefix_to_class (vfs_prefix_start);
470 element->vfs_prefix = g_strdup (vfs_prefix_start);
472 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
473 sub = VFSDATA (element);
474 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
476 slash_pointer = strchr (url_delimiter, PATH_SEP);
477 if (slash_pointer == NULL)
479 element->path = g_strdup ("");
481 else
483 element->path = vfs_translate_path_n (slash_pointer + 1);
484 #ifdef HAVE_CHARSET
485 element->encoding = vfs_get_encoding (slash_pointer);
486 #endif
487 *slash_pointer = '\0';
489 vfs_path_url_split (element, url_delimiter);
491 else
493 element->path = vfs_translate_path_n (url_delimiter);
494 #ifdef HAVE_CHARSET
495 element->encoding = vfs_get_encoding (url_delimiter);
496 #endif
498 #ifdef HAVE_CHARSET
499 element->dir.converter =
500 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
501 #endif
502 g_array_prepend_val (vpath->path, element);
504 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
505 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
506 *real_vfs_prefix_start = '\0';
507 else
508 *(real_vfs_prefix_start + 1) = '\0';
511 if (path[0] != '\0')
513 element = g_new0 (vfs_path_element_t, 1);
514 element->class = g_ptr_array_index (vfs__classes_list, 0);
515 element->path = vfs_translate_path_n (path);
516 #ifdef HAVE_CHARSET
517 element->encoding = vfs_get_encoding (path);
518 element->dir.converter =
519 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
520 #endif
521 g_array_prepend_val (vpath->path, element);
524 return vpath;
527 /* --------------------------------------------------------------------------------------------- */
529 * Add element's class info to result string (such as VFS name, host, encoding etc)
530 * This function used as helper only in vfs_path_tokens_get() function
532 * @param element current path element
533 * @param ret_tokens total tikens for return
534 * @param element_tokens accumulated element-only tokens
537 static void
538 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
539 GString * element_tokens)
541 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
542 && element_tokens->len > 0)
544 char *url_str;
546 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
547 g_string_append_c (ret_tokens, PATH_SEP);
549 g_string_append (ret_tokens, element->vfs_prefix);
550 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
552 url_str = vfs_path_build_url_params_str (element, TRUE);
553 if (*url_str != '\0')
555 g_string_append (ret_tokens, url_str);
556 g_string_append_c (ret_tokens, PATH_SEP);
559 g_free (url_str);
562 #ifdef HAVE_CHARSET
563 if (element->encoding != NULL)
565 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
566 g_string_append (ret_tokens, PATH_SEP_STR);
567 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
568 g_string_append (ret_tokens, element->encoding);
569 g_string_append (ret_tokens, PATH_SEP_STR);
571 #endif
573 g_string_append (ret_tokens, element_tokens->str);
576 /* --------------------------------------------------------------------------------------------- */
578 * Strip path to home dir.
579 * @param dir pointer to string contains full path
582 static char *
583 vfs_path_strip_home (const char *dir)
585 const char *home_dir = mc_config_get_home_dir ();
587 if (home_dir != NULL)
589 size_t len;
591 len = strlen (home_dir);
593 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
594 return g_strdup_printf ("~%s", dir + len);
597 return g_strdup (dir);
600 /* --------------------------------------------------------------------------------------------- */
602 /* --------------------------------------------------------------------------------------------- */
603 /*** public functions ****************************************************************************/
604 /* --------------------------------------------------------------------------------------------- */
606 * Convert first elements_count elements from vfs_path_t to string representation with flags.
608 * @param vpath pointer to vfs_path_t object
609 * @param elements_count count of first elements for convert
610 * @param flags flags for converter
612 * @return pointer to newly created string.
615 #define vfs_append_from_path(appendfrom, is_relative) \
617 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
619 char *stripped_home_str; \
620 stripped_home_str = vfs_path_strip_home (appendfrom); \
621 g_string_append (buffer, stripped_home_str); \
622 g_free (stripped_home_str); \
624 else \
626 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
627 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
628 g_string_append_c (buffer, PATH_SEP); \
629 g_string_append (buffer, appendfrom); \
633 char *
634 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
636 int element_index;
637 GString *buffer;
638 GString *recode_buffer;
640 if (vpath == NULL)
641 return NULL;
643 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
644 elements_count = vfs_path_elements_count (vpath);
646 if (elements_count < 0)
647 elements_count = vfs_path_elements_count (vpath) + elements_count;
649 buffer = g_string_new ("");
650 recode_buffer = g_string_new ("");
652 for (element_index = 0; element_index < elements_count; element_index++)
654 const vfs_path_element_t *element;
655 gboolean is_relative = vpath->relative && (element_index == 0);
657 element = vfs_path_get_by_index (vpath, element_index);
658 if (element->vfs_prefix != NULL)
660 char *url_str;
661 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
662 g_string_append_c (buffer, PATH_SEP);
664 g_string_append (buffer, element->vfs_prefix);
665 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
667 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
669 if (*url_str != '\0')
671 g_string_append (buffer, url_str);
672 g_string_append_c (buffer, PATH_SEP);
675 g_free (url_str);
678 #ifdef HAVE_CHARSET
679 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
681 if ((flags & VPF_HIDE_CHARSET) == 0)
683 if ((!is_relative)
684 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
685 g_string_append (buffer, PATH_SEP_STR);
686 g_string_append (buffer, VFS_ENCODING_PREFIX);
687 g_string_append (buffer, element->encoding);
689 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
690 vfs_append_from_path (recode_buffer->str, is_relative);
691 g_string_set_size (recode_buffer, 0);
693 else
694 #endif
696 vfs_append_from_path (element->path, is_relative);
699 g_string_free (recode_buffer, TRUE);
700 return g_string_free (buffer, FALSE);
703 #undef vfs_append_from_path
705 /* --------------------------------------------------------------------------------------------- */
707 * Convert first elements_count elements from vfs_path_t to string representation.
709 * @param vpath pointer to vfs_path_t object
710 * @param elements_count count of first elements for convert
712 * @return pointer to newly created string.
715 char *
716 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
718 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
721 /* --------------------------------------------------------------------------------------------- */
723 * Convert vfs_path_t to string representation.
725 * @param vpath pointer to vfs_path_t object
727 * @return pointer to newly created string.
730 char *
731 vfs_path_to_str (const vfs_path_t * vpath)
733 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
736 /* --------------------------------------------------------------------------------------------- */
738 * Split path string to path elements with flags for change parce process.
740 * @param path_str VFS-path
741 * @param flags flags for parser
743 * @return pointer to newly created vfs_path_t object with filled path elements array.
746 vfs_path_t *
747 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
749 vfs_path_t *vpath;
750 char *path;
752 if (path_str == NULL)
753 return NULL;
755 if ((flags & VPF_NO_CANON) == 0)
756 path = vfs_canon (path_str);
757 else
758 path = g_strdup (path_str);
760 if (path == NULL)
761 return NULL;
763 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
764 vpath = vfs_path_from_str_deprecated_parser (path, flags);
765 else
766 vpath = vfs_path_from_str_uri_parser (path, flags);
768 g_free (path);
770 return vpath;
773 /* --------------------------------------------------------------------------------------------- */
775 * Split path string to path elements.
777 * @param path_str VFS-path
779 * @return pointer to newly created vfs_path_t object with filled path elements array.
782 vfs_path_t *
783 vfs_path_from_str (const char *path_str)
785 return vfs_path_from_str_flags (path_str, VPF_NONE);
788 /* --------------------------------------------------------------------------------------------- */
790 * Create new vfs_path_t object.
792 * @return pointer to newly created vfs_path_t object.
795 vfs_path_t *
796 vfs_path_new (void)
798 vfs_path_t *vpath;
800 vpath = g_new0 (vfs_path_t, 1);
801 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
803 return vpath;
806 /* --------------------------------------------------------------------------------------------- */
808 * Get count of path elements.
810 * @param vpath pointer to vfs_path_t object
812 * @return count of path elements.
816 vfs_path_elements_count (const vfs_path_t * vpath)
818 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
821 /* --------------------------------------------------------------------------------------------- */
823 * Add vfs_path_element_t object to end of list in vfs_path_t object
824 * @param vpath pointer to vfs_path_t object
825 * @param path_element pointer to vfs_path_element_t object
828 void
829 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
831 g_array_append_val (vpath->path, path_element);
834 /* --------------------------------------------------------------------------------------------- */
836 * Get one path element by index.
838 * @param vpath pointer to vfs_path_t object
839 * @param element_index element index. May have negative value (in this case count was started at the end of list).
841 * @return path element.
844 const vfs_path_element_t *
845 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
847 if (vpath == NULL)
848 return NULL;
850 if (element_index < 0)
851 element_index += vfs_path_elements_count (vpath);
853 if (element_index < 0)
854 vfs_die ("vfs_path_get_by_index: incorrect index!");
856 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
859 /* --------------------------------------------------------------------------------------------- */
861 * Clone one path element
863 * @param element pointer to vfs_path_element_t object
865 * @return Newly allocated path element
868 vfs_path_element_t *
869 vfs_path_element_clone (const vfs_path_element_t * element)
871 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
873 new_element->user = g_strdup (element->user);
874 new_element->password = g_strdup (element->password);
875 new_element->host = g_strdup (element->host);
876 new_element->ipv6 = element->ipv6;
877 new_element->port = element->port;
878 new_element->path = g_strdup (element->path);
879 new_element->class = element->class;
880 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
881 #ifdef HAVE_CHARSET
882 new_element->encoding = g_strdup (element->encoding);
883 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
884 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
885 else
886 new_element->dir.converter = element->dir.converter;
887 #endif
888 new_element->dir.info = element->dir.info;
890 return new_element;
893 /* --------------------------------------------------------------------------------------------- */
895 * Free one path element.
897 * @param element pointer to vfs_path_element_t object
901 void
902 vfs_path_element_free (vfs_path_element_t * element)
904 if (element == NULL)
905 return;
907 g_free (element->user);
908 g_free (element->password);
909 g_free (element->host);
910 g_free (element->path);
911 g_free (element->vfs_prefix);
913 #ifdef HAVE_CHARSET
914 g_free (element->encoding);
916 if (vfs_path_element_need_cleanup_converter (element))
917 str_close_conv (element->dir.converter);
918 #endif
920 g_free (element);
923 /* --------------------------------------------------------------------------------------------- */
925 * Clone path
927 * @param vpath pointer to vfs_path_t object
929 * @return Newly allocated path object
932 vfs_path_t *
933 vfs_path_clone (const vfs_path_t * vpath)
935 vfs_path_t *new_vpath;
936 int vpath_element_index;
938 if (vpath == NULL)
939 return NULL;
941 new_vpath = vfs_path_new ();
942 new_vpath->relative = vpath->relative;
944 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
945 vpath_element_index++)
947 vfs_path_element_t *path_element;
949 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
950 g_array_append_val (new_vpath->path, path_element);
953 return new_vpath;
956 /* --------------------------------------------------------------------------------------------- */
958 * Free vfs_path_t object.
960 * @param vpath pointer to vfs_path_t object
964 void
965 vfs_path_free (vfs_path_t * vpath)
967 int vpath_element_index;
969 if (vpath == NULL)
970 return;
972 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
973 vpath_element_index++)
975 vfs_path_element_t *path_element;
977 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
978 vfs_path_element_free (path_element);
981 g_array_free (vpath->path, TRUE);
982 g_free (vpath);
985 /* --------------------------------------------------------------------------------------------- */
987 * Remove one path element by index
989 * @param vpath pointer to vfs_path_t object
990 * @param element_index element index. May have negative value (in this case count was started at the end of list).
994 void
995 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
997 vfs_path_element_t *element;
999 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
1000 return;
1002 if (element_index < 0)
1003 element_index = vfs_path_elements_count (vpath) + element_index;
1005 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1006 vpath->path = g_array_remove_index (vpath->path, element_index);
1007 vfs_path_element_free (element);
1010 /* --------------------------------------------------------------------------------------------- */
1011 /** Return VFS class for the given prefix */
1013 struct vfs_class *
1014 vfs_prefix_to_class (const char *prefix)
1016 guint i;
1018 /* Avoid first class (localfs) that would accept any prefix */
1019 for (i = 1; i < vfs__classes_list->len; i++)
1021 struct vfs_class *vfs;
1023 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1024 if (vfs->which != NULL)
1026 if (vfs->which (vfs, prefix) == -1)
1027 continue;
1028 return vfs;
1031 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1032 return vfs;
1035 return NULL;
1038 /* --------------------------------------------------------------------------------------------- */
1040 * Check if need cleanup charset converter for vfs_path_element_t
1042 * @param element part of path
1044 * @return TRUE if need cleanup converter or FALSE otherwise
1046 #ifdef HAVE_CHARSET
1047 gboolean
1048 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1050 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1052 #endif
1054 /* --------------------------------------------------------------------------------------------- */
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 * @return serialized vpath as newly allocated string
1064 char *
1065 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1067 mc_config_t *cpath = mc_config_init (NULL);
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;
1077 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1079 char *groupname;
1080 const vfs_path_element_t *element;
1082 groupname = g_strdup_printf ("path-element-%zd", element_index);
1083 element = vfs_path_get_by_index (vpath, element_index);
1084 /* convert one element to config group */
1086 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1087 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1088 #ifdef HAVE_CHARSET
1089 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1090 #endif
1091 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1093 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1094 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1095 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1096 if (element->port != 0)
1097 mc_config_set_int (cpath, groupname, "port", element->port);
1099 g_free (groupname);
1102 ret_value = mc_serialize_config (cpath, error);
1103 mc_config_deinit (cpath);
1104 return ret_value;
1107 /* --------------------------------------------------------------------------------------------- */
1109 * Deserialize string to vfs_path_t object
1111 * @param data data for serialization
1112 * @param error contain pointer to object for handle error code and message
1114 * @return newly allocated vfs_path_t object
1117 vfs_path_t *
1118 vfs_path_deserialize (const char *data, GError ** error)
1120 mc_config_t *cpath = mc_deserialize_config (data, error);
1121 size_t element_index = 0;
1122 vfs_path_t *vpath;
1124 if (cpath == NULL)
1125 return NULL;
1127 vpath = vfs_path_new ();
1129 while (TRUE)
1131 vfs_path_element_t *element;
1132 char *cfg_value;
1133 char *groupname;
1135 groupname = g_strdup_printf ("path-element-%zd", element_index);
1136 if (!mc_config_has_group (cpath, groupname))
1138 g_free (groupname);
1139 break;
1142 element = g_new0 (vfs_path_element_t, 1);
1144 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1145 element->class = vfs_get_class_by_name (cfg_value);
1146 if (element->class == NULL)
1148 g_free (element);
1149 vfs_path_free (vpath);
1150 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1151 g_free (cfg_value);
1152 mc_config_deinit (cpath);
1153 return NULL;
1155 g_free (cfg_value);
1157 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1159 #ifdef HAVE_CHARSET
1160 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1161 element->dir.converter =
1162 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1163 #endif
1165 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1167 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1168 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1169 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1170 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1172 vpath->path = g_array_append_val (vpath->path, element);
1174 g_free (groupname);
1175 element_index++;
1178 mc_config_deinit (cpath);
1179 if (vfs_path_elements_count (vpath) == 0)
1181 vfs_path_free (vpath);
1182 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1183 return NULL;
1186 return vpath;
1189 /* --------------------------------------------------------------------------------------------- */
1191 * Build vfs_path_t object from arguments.
1193 * @param ... path tokens, terminated by NULL
1195 * @return newly allocated vfs_path_t object
1198 vfs_path_t *
1199 vfs_path_build_filename (const char *first_element, ...)
1201 va_list args;
1202 char *str_path;
1203 vfs_path_t *vpath;
1205 if (first_element == NULL)
1206 return NULL;
1208 va_start (args, first_element);
1209 str_path = mc_build_filenamev (first_element, args);
1210 va_end (args);
1211 vpath = vfs_path_from_str (str_path);
1212 g_free (str_path);
1213 return vpath;
1216 /* --------------------------------------------------------------------------------------------- */
1218 * Append tokens to path object
1220 * @param vpath path object
1221 * @param ... NULL-terminated strings
1223 * @return newly allocated path object
1226 vfs_path_t *
1227 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1229 va_list args;
1230 char *str_path, *result_str;
1231 vfs_path_t *ret_vpath;
1233 if (vpath == NULL || first_element == NULL)
1234 return NULL;
1236 va_start (args, first_element);
1237 str_path = mc_build_filenamev (first_element, args);
1238 va_end (args);
1240 result_str = vfs_path_to_str (vpath);
1241 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1242 g_free (result_str);
1243 g_free (str_path);
1245 return ret_vpath;
1249 /* --------------------------------------------------------------------------------------------- */
1252 * Append vpath_t tokens to path object
1254 * @param ... NULL-terminated vpath objects
1256 * @return newly allocated path object
1259 vfs_path_t *
1260 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1262 va_list args;
1263 vfs_path_t *ret_vpath;
1264 const vfs_path_t *current_vpath = first_vpath;
1266 if (first_vpath == NULL)
1267 return NULL;
1269 ret_vpath = vfs_path_new ();
1271 va_start (args, first_vpath);
1274 int vindex;
1276 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1278 vfs_path_element_t *path_element;
1280 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1281 g_array_append_val (ret_vpath->path, path_element);
1283 current_vpath = va_arg (args, const vfs_path_t *);
1285 while (current_vpath != NULL);
1286 va_end (args);
1288 return ret_vpath;
1291 /* --------------------------------------------------------------------------------------------- */
1293 * get tockens count in path.
1295 * @param vpath path object
1297 * @return count of tokens
1300 size_t
1301 vfs_path_tokens_count (const vfs_path_t * vpath)
1303 size_t count_tokens = 0;
1304 int element_index;
1306 if (vpath == NULL)
1307 return 0;
1309 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1311 const vfs_path_element_t *element;
1312 char **path_tokens, **iterator;
1314 element = vfs_path_get_by_index (vpath, element_index);
1315 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1317 while (*iterator != NULL)
1319 if (**iterator != '\0')
1320 count_tokens++;
1321 iterator++;
1323 g_strfreev (path_tokens);
1325 return count_tokens;
1328 /* --------------------------------------------------------------------------------------------- */
1330 * Get subpath by tokens
1332 * @param vpath path object
1333 * @param start_position first token for got/ Started from 0.
1334 * If negative, then position will be relative to end of path
1335 * @param length count of tokens
1337 * @return newly allocated string with path tokens separated by slash
1340 char *
1341 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1343 GString *ret_tokens, *element_tokens;
1344 int element_index;
1345 size_t tokens_count = vfs_path_tokens_count (vpath);
1347 if (vpath == NULL)
1348 return NULL;
1350 if (length == 0)
1351 length = tokens_count;
1353 if (length < 0)
1354 length = tokens_count + length;
1356 if (start_position < 0)
1357 start_position = (ssize_t) tokens_count + start_position;
1359 if (start_position < 0)
1360 return NULL;
1362 if (start_position >= (ssize_t) tokens_count)
1363 return NULL;
1365 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1366 length = tokens_count - start_position;
1368 ret_tokens = g_string_sized_new (32);
1369 element_tokens = g_string_sized_new (32);
1371 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1373 const vfs_path_element_t *element;
1374 char **path_tokens, **iterator;
1376 g_string_assign (element_tokens, "");
1377 element = vfs_path_get_by_index (vpath, element_index);
1378 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1380 while (*iterator != NULL)
1382 if (**iterator != '\0')
1384 if (start_position == 0)
1386 if (length == 0)
1388 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1389 g_string_free (element_tokens, TRUE);
1390 g_strfreev (path_tokens);
1391 return g_string_free (ret_tokens, FALSE);
1393 length--;
1394 if (element_tokens->len != 0)
1395 g_string_append_c (element_tokens, PATH_SEP);
1396 g_string_append (element_tokens, *iterator);
1398 else
1399 start_position--;
1401 iterator++;
1403 g_strfreev (path_tokens);
1404 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1407 g_string_free (element_tokens, TRUE);
1408 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1411 /* --------------------------------------------------------------------------------------------- */
1413 * Get subpath by tokens
1415 * @param vpath path object
1416 * @param start_position first token for got/ Started from 0.
1417 * If negative, then position will be relative to end of path
1418 * @param length count of tokens
1420 * @return newly allocated path object with path tokens separated by slash
1423 vfs_path_t *
1424 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1426 char *str_tokens;
1427 vfs_path_t *ret_vpath = NULL;
1429 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1430 if (str_tokens != NULL)
1432 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1433 g_free (str_tokens);
1435 return ret_vpath;
1438 /* --------------------------------------------------------------------------------------------- */
1440 * Build URL parameters (such as user:pass@host:port) from one path element object
1442 * @param element path element
1444 * @return newly allocated string
1447 char *
1448 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1450 GString *buffer;
1452 if (element == NULL)
1453 return NULL;
1455 buffer = g_string_new ("");
1457 if (element->user != NULL)
1458 g_string_append (buffer, element->user);
1460 if (element->password != NULL && keep_password)
1462 g_string_append_c (buffer, ':');
1463 g_string_append (buffer, element->password);
1466 if (element->host != NULL)
1468 if ((element->user != NULL) || (element->password != NULL))
1469 g_string_append_c (buffer, '@');
1470 if (element->ipv6)
1471 g_string_append_c (buffer, '[');
1472 g_string_append (buffer, element->host);
1473 if (element->ipv6)
1474 g_string_append_c (buffer, ']');
1477 if ((element->port) != 0 && (element->host != NULL))
1479 g_string_append_c (buffer, ':');
1480 g_string_append_printf (buffer, "%d", element->port);
1483 return g_string_free (buffer, FALSE);
1486 /* --------------------------------------------------------------------------------------------- */
1488 * Build pretty string representation of one path_element_t object
1490 * @param element path element
1492 * @return newly allocated string
1495 char *
1496 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1498 char *url_params;
1499 GString *pretty_path;
1501 pretty_path = g_string_new (element->class->prefix);
1502 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1504 url_params = vfs_path_build_url_params_str (element, FALSE);
1505 g_string_append (pretty_path, url_params);
1506 g_free (url_params);
1508 if (*element->path != PATH_SEP)
1509 g_string_append_c (pretty_path, PATH_SEP);
1511 g_string_append (pretty_path, element->path);
1512 return g_string_free (pretty_path, FALSE);
1515 /* --------------------------------------------------------------------------------------------- */
1517 * Compare two path objects as strings
1519 * @param vpath1 first path object
1520 * @param vpath2 second vpath object
1522 * @return integer value like to strcmp.
1526 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1528 char *path1;
1529 char *path2;
1530 int ret_val;
1532 if (vpath1 == NULL || vpath2 == NULL)
1533 return -1;
1535 path1 = vfs_path_to_str (vpath1);
1536 path2 = vfs_path_to_str (vpath2);
1538 ret_val = strcmp (path1, path2);
1540 g_free (path1);
1541 g_free (path2);
1543 return ret_val;
1546 /* --------------------------------------------------------------------------------------------- */
1548 * Compare two path objects as strings
1550 * @param vpath1 first path object
1551 * @param vpath2 second vpath object
1552 * @param len number of first 'len' characters
1554 * @return integer value like to strcmp.
1558 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1560 char *path1;
1561 char *path2;
1562 int ret_val;
1564 if (vpath1 == NULL || vpath2 == NULL)
1565 return -1;
1567 path1 = vfs_path_to_str (vpath1);
1568 path2 = vfs_path_to_str (vpath2);
1570 ret_val = strncmp (path1, path2, len);
1572 g_free (path1);
1573 g_free (path2);
1575 return ret_val;
1578 /* --------------------------------------------------------------------------------------------- */
1580 * Calculate path length in string representation
1582 * @param vpath path object
1584 * @return length of path
1587 size_t
1588 vfs_path_len (const vfs_path_t * vpath)
1590 char *path;
1591 size_t ret_val;
1593 if (vpath == NULL)
1594 return 0;
1596 path = vfs_path_to_str (vpath);
1597 ret_val = strlen (path);
1598 g_free (path);
1599 return ret_val;
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Convert relative vpath object to absolute
1606 * @param vpath path object
1608 * @return absolute path object
1611 vfs_path_t *
1612 vfs_path_to_absolute (const vfs_path_t * vpath)
1614 vfs_path_t *absolute_vpath;
1615 char *path_str;
1617 if (!vpath->relative)
1618 return vfs_path_clone (vpath);
1620 path_str = vfs_path_to_str (vpath);
1621 absolute_vpath = vfs_path_from_str (path_str);
1622 g_free (path_str);
1623 return absolute_vpath;
1626 /* --------------------------------------------------------------------------------------------- */