2 Virtual File System path handlers
5 The Free Software Foundation, Inc.
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/>.
28 * \brief Source: Virtual File System: path handlers
36 #include "lib/global.h"
37 #include "lib/strutil.h"
38 #include "lib/util.h" /* mc_build_filename() */
39 #include "lib/serialize.h"
43 #include "xdirentry.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 /* --------------------------------------------------------------------------------------------- */
60 path_magic (const char *path
)
64 return (stat (path
, &buf
) != 0);
67 /* --------------------------------------------------------------------------------------------- */
70 * Splits path extracting vfs part.
73 * \verbatim /p1#op/inpath \endverbatim
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
,
87 struct vfs_class
*ret
;
90 vfs_die ("Cannot split NULL");
92 semi
= strrstr_skip_count (path
, "#", skip_count
);
94 if ((semi
== NULL
) || (!path_magic (path
)))
97 slash
= strchr (semi
, PATH_SEP
);
109 ret
= vfs_prefix_to_class (semi
+ 1);
115 *inpath
= slash
!= NULL
? slash
+ 1 : NULL
;
123 ret
= _vfs_split_with_semi_skip_count (path
, inpath
, op
, skip_count
+ 1);
127 /* --------------------------------------------------------------------------------------------- */
129 * remove //, /./ and /../
131 * @return newly allocated string
135 vfs_canon (const char *path
)
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
);
155 local
= tilde_expand (path
);
156 if (*local
!= PATH_SEP
)
161 curr_dir
= vfs_get_current_dir ();
162 local
= mc_build_filename (curr_dir
, path
, NULL
);
166 result
= vfs_canon (local
);
172 * So we have path of following form:
173 * /p1/p2#op/.././././p3#op/p4. Good luck.
178 result
= g_strdup (path
);
179 canonicalize_pathname (result
);
184 /* --------------------------------------------------------------------------------------------- */
187 /** get encoding after last #enc: or NULL, if part does not contain #enc:
191 * @return newly allocated string.
195 vfs_get_encoding (const char *path
)
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
);
213 g_strlcpy (result
, semi
, sizeof (result
));
215 return g_strdup (result
);
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
247 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
251 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
253 path_element
->port
= 0;
255 pcopy
= g_strdup (path
);
256 pend
= pcopy
+ strlen (pcopy
);
259 /* search for any possible user */
260 at
= strrchr (pcopy
, '@');
262 /* We have a username */
268 inner_colon
= strchr (pcopy
, ':');
269 if (inner_colon
!= NULL
)
273 path_element
->password
= g_strdup (inner_colon
);
277 path_element
->user
= g_strdup (pcopy
);
285 /* Check if the host comes with a port spec, if so, chop it */
287 colon
= strchr (rest
, ':');
290 colon
= strchr (++rest
, ']');
296 path_element
->ipv6
= TRUE
;
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;
309 while (*(++colon
) != '\0')
314 path_element
->port
= 1;
317 path_element
->port
= 2;
322 path_element
->host
= g_strdup (rest
);
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
)
340 if (class_name
== 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))
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
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.
377 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
380 vfs_path_element_t
*element
;
381 struct vfs_class
*class;
382 const char *local
, *op
;
385 vpath
= vfs_path_new ();
387 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
390 element
= g_new0 (vfs_path_element_t
, 1);
391 element
->class = class;
394 element
->path
= vfs_translate_path_n (local
);
397 element
->encoding
= vfs_get_encoding (local
);
398 element
->dir
.converter
=
399 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
402 url_params
= strchr (op
, ':'); /* skip VFS prefix */
403 if (url_params
!= NULL
)
407 vfs_path_url_split (element
, url_params
);
411 element
->vfs_prefix
= g_strdup (op
);
413 g_array_prepend_val (vpath
->path
, element
);
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
);
422 element
->encoding
= vfs_get_encoding (path
);
423 element
->dir
.converter
=
424 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
426 g_array_prepend_val (vpath
->path
, element
);
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.
442 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
445 vfs_path_element_t
*element
;
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
;
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 ("");
483 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
485 element
->encoding
= vfs_get_encoding (slash_pointer
);
487 *slash_pointer
= '\0';
489 vfs_path_url_split (element
, url_delimiter
);
493 element
->path
= vfs_translate_path_n (url_delimiter
);
495 element
->encoding
= vfs_get_encoding (url_delimiter
);
499 element
->dir
.converter
=
500 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
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';
508 *(real_vfs_prefix_start
+ 1) = '\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
);
517 element
->encoding
= vfs_get_encoding (path
);
518 element
->dir
.converter
=
519 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
521 g_array_prepend_val (vpath
->path
, element
);
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
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)
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
);
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
);
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
583 vfs_path_strip_home (const char *dir
)
585 const char *home_dir
= mc_config_get_home_dir ();
587 if (home_dir
!= NULL
)
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); \
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); \
634 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
638 GString
*recode_buffer
;
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
)
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
);
679 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
681 if ((flags
& VPF_HIDE_CHARSET
) == 0)
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);
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.
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.
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.
747 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
752 if (path_str
== NULL
)
755 if ((flags
& VPF_NO_CANON
) == 0)
756 path
= vfs_canon (path_str
);
758 path
= g_strdup (path_str
);
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
);
766 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
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.
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.
800 vpath
= g_new0 (vfs_path_t
, 1);
801 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
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
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
)
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
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
);
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
);
886 new_element
->dir
.converter
= element
->dir
.converter
;
888 new_element
->dir
.info
= element
->dir
.info
;
893 /* --------------------------------------------------------------------------------------------- */
895 * Free one path element.
897 * @param element pointer to vfs_path_element_t object
902 vfs_path_element_free (vfs_path_element_t
* element
)
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
);
914 g_free (element
->encoding
);
916 if (vfs_path_element_need_cleanup_converter (element
))
917 str_close_conv (element
->dir
.converter
);
923 /* --------------------------------------------------------------------------------------------- */
927 * @param vpath pointer to vfs_path_t object
929 * @return Newly allocated path object
933 vfs_path_clone (const vfs_path_t
* vpath
)
935 vfs_path_t
*new_vpath
;
936 int vpath_element_index
;
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
);
956 /* --------------------------------------------------------------------------------------------- */
958 * Free vfs_path_t object.
960 * @param vpath pointer to vfs_path_t object
965 vfs_path_free (vfs_path_t
* vpath
)
967 int vpath_element_index
;
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
);
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).
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))
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 */
1014 vfs_prefix_to_class (const char *prefix
)
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)
1031 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
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
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
);
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
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
;
1071 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
1073 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
1077 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
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
);
1089 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
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
);
1102 ret_value
= mc_serialize_config (cpath
, error
);
1103 mc_config_deinit (cpath
);
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
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;
1127 vpath
= vfs_path_new ();
1131 vfs_path_element_t
*element
;
1135 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1136 if (!mc_config_has_group (cpath
, groupname
))
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
)
1149 vfs_path_free (vpath
);
1150 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1152 mc_config_deinit (cpath
);
1157 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
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
;
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
);
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");
1189 /* --------------------------------------------------------------------------------------------- */
1191 * Build vfs_path_t object from arguments.
1193 * @param ... path tokens, terminated by NULL
1195 * @return newly allocated vfs_path_t object
1199 vfs_path_build_filename (const char *first_element
, ...)
1205 if (first_element
== NULL
)
1208 va_start (args
, first_element
);
1209 str_path
= mc_build_filenamev (first_element
, args
);
1211 vpath
= vfs_path_from_str (str_path
);
1216 /* --------------------------------------------------------------------------------------------- */
1218 * Append tokens to path object
1220 * @param vpath path object
1221 * @param ... NULL-terminated strings
1223 * @return newly allocated path object
1227 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1230 char *str_path
, *result_str
;
1231 vfs_path_t
*ret_vpath
;
1233 if (vpath
== NULL
|| first_element
== NULL
)
1236 va_start (args
, first_element
);
1237 str_path
= mc_build_filenamev (first_element
, 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
);
1249 /* --------------------------------------------------------------------------------------------- */
1252 * Append vpath_t tokens to path object
1254 * @param ... NULL-terminated vpath objects
1256 * @return newly allocated path object
1260 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1263 vfs_path_t
*ret_vpath
;
1264 const vfs_path_t
*current_vpath
= first_vpath
;
1266 if (first_vpath
== NULL
)
1269 ret_vpath
= vfs_path_new ();
1271 va_start (args
, first_vpath
);
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
);
1291 /* --------------------------------------------------------------------------------------------- */
1293 * get tockens count in path.
1295 * @param vpath path object
1297 * @return count of tokens
1301 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1303 size_t count_tokens
= 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')
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
1341 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1343 GString
*ret_tokens
, *element_tokens
;
1345 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1351 length
= tokens_count
;
1354 length
= tokens_count
+ length
;
1356 if (start_position
< 0)
1357 start_position
= (ssize_t
) tokens_count
+ start_position
;
1359 if (start_position
< 0)
1362 if (start_position
>= (ssize_t
) tokens_count
)
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)
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
);
1394 if (element_tokens
->len
!= 0)
1395 g_string_append_c (element_tokens
, PATH_SEP
);
1396 g_string_append (element_tokens
, *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
1424 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
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
);
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
1448 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1452 if (element
== 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
, '@');
1471 g_string_append_c (buffer
, '[');
1472 g_string_append (buffer
, element
->host
);
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
1496 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
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
)
1532 if (vpath1
== NULL
|| vpath2
== NULL
)
1535 path1
= vfs_path_to_str (vpath1
);
1536 path2
= vfs_path_to_str (vpath2
);
1538 ret_val
= strcmp (path1
, path2
);
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
)
1564 if (vpath1
== NULL
|| vpath2
== NULL
)
1567 path1
= vfs_path_to_str (vpath1
);
1568 path2
= vfs_path_to_str (vpath2
);
1570 ret_val
= strncmp (path1
, path2
, len
);
1578 /* --------------------------------------------------------------------------------------------- */
1580 * Calculate path length in string representation
1582 * @param vpath path object
1584 * @return length of path
1588 vfs_path_len (const vfs_path_t
* vpath
)
1596 path
= vfs_path_to_str (vpath
);
1597 ret_val
= strlen (path
);
1602 /* --------------------------------------------------------------------------------------------- */
1604 * Convert relative vpath object to absolute
1606 * @param vpath path object
1608 * @return absolute path object
1612 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1614 vfs_path_t
*absolute_vpath
;
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
);
1623 return absolute_vpath
;
1626 /* --------------------------------------------------------------------------------------------- */