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 local
= tilde_expand (path
);
146 if (*local
!= PATH_SEP
)
151 curr_dir
= vfs_get_current_dir ();
152 local
= mc_build_filename (curr_dir
, path
, NULL
);
156 result
= vfs_canon (local
);
162 * So we have path of following form:
163 * /p1/p2#op/.././././p3#op/p4. Good luck.
168 result
= g_strdup (path
);
169 canonicalize_pathname (result
);
174 /* --------------------------------------------------------------------------------------------- */
177 /** get encoding after last #enc: or NULL, if part does not contain #enc:
181 * @return newly allocated string.
185 vfs_get_encoding (const char *path
)
191 work
= g_strdup (path
);
193 /* try found #enc: */
194 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
196 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
198 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
199 slash
= strchr (semi
, PATH_SEP
);
203 g_strlcpy (result
, semi
, sizeof (result
));
205 return g_strdup (result
);
215 /* --------------------------------------------------------------------------------------------- */
216 /** Extract the hostname and username from the path
218 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
220 * ftp://sunsite.unc.edu/pub/linux
221 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
222 * ftp://tsx-11.mit.edu:8192/
223 * ftp://joe@foo.edu:11321/private
224 * ftp://joe:password@foo.se
226 * @param path_element is an input string to be parsed
227 * @param path is an input string to be parsed
229 * @return g_malloc()ed url info.
230 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
231 * is not set, then the current login name is supplied.
232 * Return value is a g_malloc()ed structure with the pathname relative to the
237 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
241 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
243 path_element
->port
= 0;
245 pcopy
= g_strdup (path
);
246 pend
= pcopy
+ strlen (pcopy
);
249 /* search for any possible user */
250 at
= strrchr (pcopy
, '@');
252 /* We have a username */
258 inner_colon
= strchr (pcopy
, ':');
259 if (inner_colon
!= NULL
)
263 path_element
->password
= g_strdup (inner_colon
);
267 path_element
->user
= g_strdup (pcopy
);
275 /* Check if the host comes with a port spec, if so, chop it */
277 colon
= strchr (rest
, ':');
280 colon
= strchr (++rest
, ']');
286 path_element
->ipv6
= TRUE
;
293 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
295 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
296 path_element
->port
= 0;
299 while (*(++colon
) != '\0')
304 path_element
->port
= 1;
307 path_element
->port
= 2;
312 path_element
->host
= g_strdup (rest
);
316 /* --------------------------------------------------------------------------------------------- */
318 * get VFS class for the given name
320 * @param class_name name of class
322 * @return pointer to class structure or NULL if class not found
325 static struct vfs_class
*
326 vfs_get_class_by_name (const char *class_name
)
330 if (class_name
== NULL
)
333 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
335 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
336 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
343 /* --------------------------------------------------------------------------------------------- */
345 * Check if path string contain URL-like elements
347 * @param path_str path
349 * @return TRUE if path is deprecated or FALSE otherwise
353 vfs_path_is_str_path_deprecated (const char *path_str
)
355 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
358 /* --------------------------------------------------------------------------------------------- */
359 /** Split path string to path elements by deprecated algorithm.
361 * @param path_str VFS-path
363 * @return pointer to newly created vfs_path_t object with filled path elements array.
367 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
370 vfs_path_element_t
*element
;
371 struct vfs_class
*class;
372 const char *local
, *op
;
375 vpath
= vfs_path_new ();
377 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
380 element
= g_new0 (vfs_path_element_t
, 1);
381 element
->class = class;
384 element
->path
= vfs_translate_path_n (local
);
387 element
->encoding
= vfs_get_encoding (local
);
388 element
->dir
.converter
=
389 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
392 url_params
= strchr (op
, ':'); /* skip VFS prefix */
393 if (url_params
!= NULL
)
397 vfs_path_url_split (element
, url_params
);
401 element
->vfs_prefix
= g_strdup (op
);
403 g_array_prepend_val (vpath
->path
, element
);
407 element
= g_new0 (vfs_path_element_t
, 1);
408 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
409 element
->path
= vfs_translate_path_n (path
);
412 element
->encoding
= vfs_get_encoding (path
);
413 element
->dir
.converter
=
414 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
416 g_array_prepend_val (vpath
->path
, element
);
422 /* --------------------------------------------------------------------------------------------- */
423 /** Split path string to path elements by URL algorithm.
425 * @param path_str VFS-path
427 * @return pointer to newly created vfs_path_t object with filled path elements array.
431 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
434 vfs_path_element_t
*element
;
438 vpath
= vfs_path_new ();
439 vpath
->relative
= (flags
& VPF_NO_CANON
) != 0;
441 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
443 char *vfs_prefix_start
;
444 char *real_vfs_prefix_start
= url_delimiter
;
446 struct vfs_s_subclass
*sub
= NULL
;
448 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
449 real_vfs_prefix_start
--;
450 vfs_prefix_start
= real_vfs_prefix_start
;
452 if (*(vfs_prefix_start
) == PATH_SEP
)
453 vfs_prefix_start
+= 1;
455 *url_delimiter
= '\0';
457 element
= g_new0 (vfs_path_element_t
, 1);
458 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
459 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
461 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
462 sub
= VFSDATA (element
);
463 if (sub
!= NULL
&& (sub
->flags
& VFS_S_REMOTE
) != 0)
465 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
466 if (slash_pointer
== NULL
)
468 element
->path
= g_strdup ("");
472 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
474 element
->encoding
= vfs_get_encoding (slash_pointer
);
476 *slash_pointer
= '\0';
478 vfs_path_url_split (element
, url_delimiter
);
482 element
->path
= vfs_translate_path_n (url_delimiter
);
484 element
->encoding
= vfs_get_encoding (url_delimiter
);
488 element
->dir
.converter
=
489 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
491 g_array_prepend_val (vpath
->path
, element
);
493 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
494 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
495 *real_vfs_prefix_start
= '\0';
497 *(real_vfs_prefix_start
+ 1) = '\0';
502 element
= g_new0 (vfs_path_element_t
, 1);
503 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
504 element
->path
= vfs_translate_path_n (path
);
506 element
->encoding
= vfs_get_encoding (path
);
507 element
->dir
.converter
=
508 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
510 g_array_prepend_val (vpath
->path
, element
);
516 /* --------------------------------------------------------------------------------------------- */
518 * Add element's class info to result string (such as VFS name, host, encoding etc)
519 * This function used as helper only in vfs_path_tokens_get() function
521 * @param element current path element
522 * @param ret_tokens total tikens for return
523 * @param element_tokens accumulated element-only tokens
527 vfs_path_tokens_add_class_info (const vfs_path_element_t
* element
, GString
* ret_tokens
,
528 GString
* element_tokens
)
530 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
531 && element_tokens
->len
> 0)
535 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
536 g_string_append_c (ret_tokens
, PATH_SEP
);
538 g_string_append (ret_tokens
, element
->vfs_prefix
);
539 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
541 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
542 if (*url_str
!= '\0')
544 g_string_append (ret_tokens
, url_str
);
545 g_string_append_c (ret_tokens
, PATH_SEP
);
552 if (element
->encoding
!= NULL
)
554 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
555 g_string_append (ret_tokens
, PATH_SEP_STR
);
556 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
557 g_string_append (ret_tokens
, element
->encoding
);
558 g_string_append (ret_tokens
, PATH_SEP_STR
);
562 g_string_append (ret_tokens
, element_tokens
->str
);
565 /* --------------------------------------------------------------------------------------------- */
567 * Strip path to home dir.
568 * @param dir pointer to string contains full path
572 vfs_path_strip_home (const char *dir
)
574 const char *home_dir
= mc_config_get_home_dir ();
576 if (home_dir
!= NULL
)
580 len
= strlen (home_dir
);
582 if (strncmp (dir
, home_dir
, len
) == 0 && (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0'))
583 return g_strdup_printf ("~%s", dir
+ len
);
586 return g_strdup (dir
);
589 /* --------------------------------------------------------------------------------------------- */
591 /* --------------------------------------------------------------------------------------------- */
592 /*** public functions ****************************************************************************/
593 /* --------------------------------------------------------------------------------------------- */
595 * Convert first elements_count elements from vfs_path_t to string representation with flags.
597 * @param vpath pointer to vfs_path_t object
598 * @param elements_count count of first elements for convert
599 * @param flags flags for converter
601 * @return pointer to newly created string.
604 #define vfs_append_from_path(appendfrom, is_relative) \
606 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
608 char *stripped_home_str; \
609 stripped_home_str = vfs_path_strip_home (appendfrom); \
610 g_string_append (buffer, stripped_home_str); \
611 g_free (stripped_home_str); \
615 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
616 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
617 g_string_append_c (buffer, PATH_SEP); \
618 g_string_append (buffer, appendfrom); \
623 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
627 GString
*recode_buffer
;
632 if (elements_count
== 0 || elements_count
> vfs_path_elements_count (vpath
))
633 elements_count
= vfs_path_elements_count (vpath
);
635 if (elements_count
< 0)
636 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
638 buffer
= g_string_new ("");
639 recode_buffer
= g_string_new ("");
641 for (element_index
= 0; element_index
< elements_count
; element_index
++)
643 const vfs_path_element_t
*element
;
644 gboolean is_relative
= vpath
->relative
&& (element_index
== 0);
646 element
= vfs_path_get_by_index (vpath
, element_index
);
647 if (element
->vfs_prefix
!= NULL
)
650 if ((!is_relative
) && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
651 g_string_append_c (buffer
, PATH_SEP
);
653 g_string_append (buffer
, element
->vfs_prefix
);
654 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
656 url_str
= vfs_path_build_url_params_str (element
, !(flags
& VPF_STRIP_PASSWORD
));
658 if (*url_str
!= '\0')
660 g_string_append (buffer
, url_str
);
661 g_string_append_c (buffer
, PATH_SEP
);
668 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
670 if ((flags
& VPF_HIDE_CHARSET
) == 0)
673 && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
674 g_string_append (buffer
, PATH_SEP_STR
);
675 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
676 g_string_append (buffer
, element
->encoding
);
678 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
679 vfs_append_from_path (recode_buffer
->str
, is_relative
);
680 g_string_set_size (recode_buffer
, 0);
685 vfs_append_from_path (element
->path
, is_relative
);
688 g_string_free (recode_buffer
, TRUE
);
689 return g_string_free (buffer
, FALSE
);
692 #undef vfs_append_from_path
694 /* --------------------------------------------------------------------------------------------- */
696 * Convert first elements_count elements from vfs_path_t to string representation.
698 * @param vpath pointer to vfs_path_t object
699 * @param elements_count count of first elements for convert
701 * @return pointer to newly created string.
705 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
707 return vfs_path_to_str_flags (vpath
, elements_count
, VPF_NONE
);
710 /* --------------------------------------------------------------------------------------------- */
712 * Convert vfs_path_t to string representation.
714 * @param vpath pointer to vfs_path_t object
716 * @return pointer to newly created string.
720 vfs_path_to_str (const vfs_path_t
* vpath
)
722 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
725 /* --------------------------------------------------------------------------------------------- */
727 * Split path string to path elements with flags for change parce process.
729 * @param path_str VFS-path
730 * @param flags flags for parser
732 * @return pointer to newly created vfs_path_t object with filled path elements array.
736 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
741 if (path_str
== NULL
)
744 if ((flags
& VPF_NO_CANON
) == 0)
745 path
= vfs_canon (path_str
);
747 path
= g_strdup (path_str
);
752 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
753 vpath
= vfs_path_from_str_deprecated_parser (path
, flags
);
755 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
762 /* --------------------------------------------------------------------------------------------- */
764 * Split path string to path elements.
766 * @param path_str VFS-path
768 * @return pointer to newly created vfs_path_t object with filled path elements array.
772 vfs_path_from_str (const char *path_str
)
774 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
777 /* --------------------------------------------------------------------------------------------- */
779 * Create new vfs_path_t object.
781 * @return pointer to newly created vfs_path_t object.
789 vpath
= g_new0 (vfs_path_t
, 1);
790 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
795 /* --------------------------------------------------------------------------------------------- */
797 * Get count of path elements.
799 * @param vpath pointer to vfs_path_t object
801 * @return count of path elements.
805 vfs_path_elements_count (const vfs_path_t
* vpath
)
807 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
810 /* --------------------------------------------------------------------------------------------- */
812 * Add vfs_path_element_t object to end of list in vfs_path_t object
813 * @param vpath pointer to vfs_path_t object
814 * @param path_element pointer to vfs_path_element_t object
818 vfs_path_add_element (const vfs_path_t
* vpath
, const vfs_path_element_t
* path_element
)
820 g_array_append_val (vpath
->path
, path_element
);
823 /* --------------------------------------------------------------------------------------------- */
825 * Get one path element by index.
827 * @param vpath pointer to vfs_path_t object
828 * @param element_index element index. May have negative value (in this case count was started at the end of list).
830 * @return path element.
833 const vfs_path_element_t
*
834 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
839 if (element_index
< 0)
840 element_index
+= vfs_path_elements_count (vpath
);
842 if (element_index
< 0)
843 vfs_die ("vfs_path_get_by_index: incorrect index!");
845 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
848 /* --------------------------------------------------------------------------------------------- */
850 * Clone one path element
852 * @param element pointer to vfs_path_element_t object
854 * @return Newly allocated path element
858 vfs_path_element_clone (const vfs_path_element_t
* element
)
860 vfs_path_element_t
*new_element
= g_new (vfs_path_element_t
, 1);
862 new_element
->user
= g_strdup (element
->user
);
863 new_element
->password
= g_strdup (element
->password
);
864 new_element
->host
= g_strdup (element
->host
);
865 new_element
->ipv6
= element
->ipv6
;
866 new_element
->port
= element
->port
;
867 new_element
->path
= g_strdup (element
->path
);
868 new_element
->class = element
->class;
869 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
871 new_element
->encoding
= g_strdup (element
->encoding
);
872 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
873 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
875 new_element
->dir
.converter
= element
->dir
.converter
;
877 new_element
->dir
.info
= element
->dir
.info
;
882 /* --------------------------------------------------------------------------------------------- */
884 * Free one path element.
886 * @param element pointer to vfs_path_element_t object
891 vfs_path_element_free (vfs_path_element_t
* element
)
896 g_free (element
->user
);
897 g_free (element
->password
);
898 g_free (element
->host
);
899 g_free (element
->path
);
900 g_free (element
->vfs_prefix
);
903 g_free (element
->encoding
);
905 if (vfs_path_element_need_cleanup_converter (element
))
906 str_close_conv (element
->dir
.converter
);
912 /* --------------------------------------------------------------------------------------------- */
916 * @param vpath pointer to vfs_path_t object
918 * @return Newly allocated path object
922 vfs_path_clone (const vfs_path_t
* vpath
)
924 vfs_path_t
*new_vpath
;
925 int vpath_element_index
;
930 new_vpath
= vfs_path_new ();
931 new_vpath
->relative
= vpath
->relative
;
933 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
934 vpath_element_index
++)
936 vfs_path_element_t
*path_element
;
938 path_element
= vfs_path_element_clone (vfs_path_get_by_index (vpath
, vpath_element_index
));
939 g_array_append_val (new_vpath
->path
, path_element
);
945 /* --------------------------------------------------------------------------------------------- */
947 * Free vfs_path_t object.
949 * @param vpath pointer to vfs_path_t object
954 vfs_path_free (vfs_path_t
* vpath
)
956 int vpath_element_index
;
961 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
962 vpath_element_index
++)
964 vfs_path_element_t
*path_element
;
966 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, vpath_element_index
);
967 vfs_path_element_free (path_element
);
970 g_array_free (vpath
->path
, TRUE
);
974 /* --------------------------------------------------------------------------------------------- */
976 * Remove one path element by index
978 * @param vpath pointer to vfs_path_t object
979 * @param element_index element index. May have negative value (in this case count was started at the end of list).
984 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
986 vfs_path_element_t
*element
;
988 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
991 if (element_index
< 0)
992 element_index
= vfs_path_elements_count (vpath
) + element_index
;
994 element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, element_index
);
995 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
996 vfs_path_element_free (element
);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Return VFS class for the given prefix */
1003 vfs_prefix_to_class (const char *prefix
)
1007 /* Avoid first class (localfs) that would accept any prefix */
1008 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
1010 struct vfs_class
*vfs
;
1012 vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
1013 if (vfs
->which
!= NULL
)
1015 if (vfs
->which (vfs
, prefix
) == -1)
1020 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
1027 /* --------------------------------------------------------------------------------------------- */
1029 * Check if need cleanup charset converter for vfs_path_element_t
1031 * @param element part of path
1033 * @return TRUE if need cleanup converter or FALSE otherwise
1037 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
1039 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
1043 /* --------------------------------------------------------------------------------------------- */
1045 * Serialize vfs_path_t object to string
1047 * @param vpath data for serialization
1048 * @param error contain pointer to object for handle error code and message
1050 * @return serialized vpath as newly allocated string
1054 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
1056 mc_config_t
*cpath
= mc_config_init (NULL
);
1057 ssize_t element_index
;
1060 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
1062 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
1066 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1069 const vfs_path_element_t
*element
;
1071 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1072 element
= vfs_path_get_by_index (vpath
, element_index
);
1073 /* convert one element to config group */
1075 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1076 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1078 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1080 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1082 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1083 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1084 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1085 if (element
->port
!= 0)
1086 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1091 ret_value
= mc_serialize_config (cpath
, error
);
1092 mc_config_deinit (cpath
);
1096 /* --------------------------------------------------------------------------------------------- */
1098 * Deserialize string to vfs_path_t object
1100 * @param data data for serialization
1101 * @param error contain pointer to object for handle error code and message
1103 * @return newly allocated vfs_path_t object
1107 vfs_path_deserialize (const char *data
, GError
** error
)
1109 mc_config_t
*cpath
= mc_deserialize_config (data
, error
);
1110 size_t element_index
= 0;
1116 vpath
= vfs_path_new ();
1120 vfs_path_element_t
*element
;
1124 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1125 if (!mc_config_has_group (cpath
, groupname
))
1131 element
= g_new0 (vfs_path_element_t
, 1);
1133 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1134 element
->class = vfs_get_class_by_name (cfg_value
);
1135 if (element
->class == NULL
)
1138 vfs_path_free (vpath
);
1139 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1141 mc_config_deinit (cpath
);
1146 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1149 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1150 element
->dir
.converter
=
1151 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1154 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1156 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1157 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1158 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1159 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1161 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1167 mc_config_deinit (cpath
);
1168 if (vfs_path_elements_count (vpath
) == 0)
1170 vfs_path_free (vpath
);
1171 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1178 /* --------------------------------------------------------------------------------------------- */
1180 * Build vfs_path_t object from arguments.
1182 * @param ... path tokens, terminated by NULL
1184 * @return newly allocated vfs_path_t object
1188 vfs_path_build_filename (const char *first_element
, ...)
1194 if (first_element
== NULL
)
1197 va_start (args
, first_element
);
1198 str_path
= mc_build_filenamev (first_element
, args
);
1200 vpath
= vfs_path_from_str (str_path
);
1205 /* --------------------------------------------------------------------------------------------- */
1207 * Append tokens to path object
1209 * @param vpath path object
1210 * @param ... NULL-terminated strings
1212 * @return newly allocated path object
1216 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1219 char *str_path
, *result_str
;
1220 vfs_path_t
*ret_vpath
;
1222 if (vpath
== NULL
|| first_element
== NULL
)
1225 va_start (args
, first_element
);
1226 str_path
= mc_build_filenamev (first_element
, args
);
1229 result_str
= vfs_path_to_str (vpath
);
1230 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1231 g_free (result_str
);
1238 /* --------------------------------------------------------------------------------------------- */
1241 * Append vpath_t tokens to path object
1243 * @param ... NULL-terminated vpath objects
1245 * @return newly allocated path object
1249 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1252 vfs_path_t
*ret_vpath
;
1253 const vfs_path_t
*current_vpath
= first_vpath
;
1255 if (first_vpath
== NULL
)
1258 ret_vpath
= vfs_path_new ();
1260 va_start (args
, first_vpath
);
1265 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1267 vfs_path_element_t
*path_element
;
1269 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1270 g_array_append_val (ret_vpath
->path
, path_element
);
1272 current_vpath
= va_arg (args
, const vfs_path_t
*);
1274 while (current_vpath
!= NULL
);
1280 /* --------------------------------------------------------------------------------------------- */
1282 * get tockens count in path.
1284 * @param vpath path object
1286 * @return count of tokens
1290 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1292 size_t count_tokens
= 0;
1298 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1300 const vfs_path_element_t
*element
;
1301 char **path_tokens
, **iterator
;
1303 element
= vfs_path_get_by_index (vpath
, element_index
);
1304 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1306 while (*iterator
!= NULL
)
1308 if (**iterator
!= '\0')
1312 g_strfreev (path_tokens
);
1314 return count_tokens
;
1317 /* --------------------------------------------------------------------------------------------- */
1319 * Get subpath by tokens
1321 * @param vpath path object
1322 * @param start_position first token for got/ Started from 0.
1323 * If negative, then position will be relative to end of path
1324 * @param length count of tokens
1326 * @return newly allocated string with path tokens separated by slash
1330 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1332 GString
*ret_tokens
, *element_tokens
;
1334 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1340 length
= tokens_count
;
1343 length
= tokens_count
+ length
;
1345 if (start_position
< 0)
1346 start_position
= (ssize_t
) tokens_count
+ start_position
;
1348 if (start_position
< 0)
1351 if (start_position
>= (ssize_t
) tokens_count
)
1354 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1355 length
= tokens_count
- start_position
;
1357 ret_tokens
= g_string_sized_new (32);
1358 element_tokens
= g_string_sized_new (32);
1360 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1362 const vfs_path_element_t
*element
;
1363 char **path_tokens
, **iterator
;
1365 g_string_assign (element_tokens
, "");
1366 element
= vfs_path_get_by_index (vpath
, element_index
);
1367 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1369 while (*iterator
!= NULL
)
1371 if (**iterator
!= '\0')
1373 if (start_position
== 0)
1377 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1378 g_string_free (element_tokens
, TRUE
);
1379 g_strfreev (path_tokens
);
1380 return g_string_free (ret_tokens
, FALSE
);
1383 if (element_tokens
->len
!= 0)
1384 g_string_append_c (element_tokens
, PATH_SEP
);
1385 g_string_append (element_tokens
, *iterator
);
1392 g_strfreev (path_tokens
);
1393 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1396 g_string_free (element_tokens
, TRUE
);
1397 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1400 /* --------------------------------------------------------------------------------------------- */
1402 * Get subpath by tokens
1404 * @param vpath path object
1405 * @param start_position first token for got/ Started from 0.
1406 * If negative, then position will be relative to end of path
1407 * @param length count of tokens
1409 * @return newly allocated path object with path tokens separated by slash
1413 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1416 vfs_path_t
*ret_vpath
= NULL
;
1418 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1419 if (str_tokens
!= NULL
)
1421 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1422 g_free (str_tokens
);
1427 /* --------------------------------------------------------------------------------------------- */
1429 * Build URL parameters (such as user:pass@host:port) from one path element object
1431 * @param element path element
1433 * @return newly allocated string
1437 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1441 if (element
== NULL
)
1444 buffer
= g_string_new ("");
1446 if (element
->user
!= NULL
)
1447 g_string_append (buffer
, element
->user
);
1449 if (element
->password
!= NULL
&& keep_password
)
1451 g_string_append_c (buffer
, ':');
1452 g_string_append (buffer
, element
->password
);
1455 if (element
->host
!= NULL
)
1457 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1458 g_string_append_c (buffer
, '@');
1460 g_string_append_c (buffer
, '[');
1461 g_string_append (buffer
, element
->host
);
1463 g_string_append_c (buffer
, ']');
1466 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1468 g_string_append_c (buffer
, ':');
1469 g_string_append_printf (buffer
, "%d", element
->port
);
1472 return g_string_free (buffer
, FALSE
);
1475 /* --------------------------------------------------------------------------------------------- */
1477 * Build pretty string representation of one path_element_t object
1479 * @param element path element
1481 * @return newly allocated string
1485 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1488 GString
*pretty_path
;
1490 pretty_path
= g_string_new (element
->class->prefix
);
1491 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1493 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1494 g_string_append (pretty_path
, url_params
);
1495 g_free (url_params
);
1497 if (*element
->path
!= PATH_SEP
)
1498 g_string_append_c (pretty_path
, PATH_SEP
);
1500 g_string_append (pretty_path
, element
->path
);
1501 return g_string_free (pretty_path
, FALSE
);
1504 /* --------------------------------------------------------------------------------------------- */
1506 * Compare two path objects as strings
1508 * @param vpath1 first path object
1509 * @param vpath2 second vpath object
1511 * @return integer value like to strcmp.
1515 vfs_path_cmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1521 if (vpath1
== NULL
|| vpath2
== NULL
)
1524 path1
= vfs_path_to_str (vpath1
);
1525 path2
= vfs_path_to_str (vpath2
);
1527 ret_val
= strcmp (path1
, path2
);
1535 /* --------------------------------------------------------------------------------------------- */
1537 * Compare two path objects as strings
1539 * @param vpath1 first path object
1540 * @param vpath2 second vpath object
1541 * @param len number of first 'len' characters
1543 * @return integer value like to strcmp.
1547 vfs_path_ncmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1553 if (vpath1
== NULL
|| vpath2
== NULL
)
1556 path1
= vfs_path_to_str (vpath1
);
1557 path2
= vfs_path_to_str (vpath2
);
1559 ret_val
= strncmp (path1
, path2
, len
);
1567 /* --------------------------------------------------------------------------------------------- */
1569 * Calculate path length in string representation
1571 * @param vpath path object
1573 * @return length of path
1577 vfs_path_len (const vfs_path_t
* vpath
)
1585 path
= vfs_path_to_str (vpath
);
1586 ret_val
= strlen (path
);
1591 /* --------------------------------------------------------------------------------------------- */
1593 * Convert relative vpath object to absolute
1595 * @param vpath path object
1597 * @return absolute path object
1601 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1603 vfs_path_t
*absolute_vpath
;
1606 if (!vpath
->relative
)
1607 return vfs_path_clone (vpath
);
1609 path_str
= vfs_path_to_str (vpath
);
1610 absolute_vpath
= vfs_path_from_str (path_str
);
1612 return absolute_vpath
;
1615 /* --------------------------------------------------------------------------------------------- */