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
)
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");
1067 cpath
= mc_config_init (NULL
, FALSE
);
1069 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1072 const vfs_path_element_t
*element
;
1074 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1075 element
= vfs_path_get_by_index (vpath
, element_index
);
1076 /* convert one element to config group */
1078 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1079 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1081 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1083 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1085 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1086 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1087 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1088 if (element
->port
!= 0)
1089 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1094 ret_value
= mc_serialize_config (cpath
, error
);
1095 mc_config_deinit (cpath
);
1099 /* --------------------------------------------------------------------------------------------- */
1101 * Deserialize string to vfs_path_t object
1103 * @param data data for serialization
1104 * @param error contain pointer to object for handle error code and message
1106 * @return newly allocated vfs_path_t object
1110 vfs_path_deserialize (const char *data
, GError
** error
)
1113 size_t element_index
= 0;
1116 cpath
= mc_deserialize_config (data
, error
);
1120 vpath
= vfs_path_new ();
1124 vfs_path_element_t
*element
;
1128 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1129 if (!mc_config_has_group (cpath
, groupname
))
1135 element
= g_new0 (vfs_path_element_t
, 1);
1137 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1138 element
->class = vfs_get_class_by_name (cfg_value
);
1139 if (element
->class == NULL
)
1142 vfs_path_free (vpath
);
1143 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1145 mc_config_deinit (cpath
);
1150 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1153 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1154 element
->dir
.converter
=
1155 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1158 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1160 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1161 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1162 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1163 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1165 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1171 mc_config_deinit (cpath
);
1172 if (vfs_path_elements_count (vpath
) == 0)
1174 vfs_path_free (vpath
);
1175 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1182 /* --------------------------------------------------------------------------------------------- */
1184 * Build vfs_path_t object from arguments.
1186 * @param ... path tokens, terminated by NULL
1188 * @return newly allocated vfs_path_t object
1192 vfs_path_build_filename (const char *first_element
, ...)
1198 if (first_element
== NULL
)
1201 va_start (args
, first_element
);
1202 str_path
= mc_build_filenamev (first_element
, args
);
1204 vpath
= vfs_path_from_str (str_path
);
1209 /* --------------------------------------------------------------------------------------------- */
1211 * Append tokens to path object
1213 * @param vpath path object
1214 * @param ... NULL-terminated strings
1216 * @return newly allocated path object
1220 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1223 char *str_path
, *result_str
;
1224 vfs_path_t
*ret_vpath
;
1226 if (vpath
== NULL
|| first_element
== NULL
)
1229 va_start (args
, first_element
);
1230 str_path
= mc_build_filenamev (first_element
, args
);
1233 result_str
= vfs_path_to_str (vpath
);
1234 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1235 g_free (result_str
);
1242 /* --------------------------------------------------------------------------------------------- */
1245 * Append vpath_t tokens to path object
1247 * @param ... NULL-terminated vpath objects
1249 * @return newly allocated path object
1253 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1256 vfs_path_t
*ret_vpath
;
1257 const vfs_path_t
*current_vpath
= first_vpath
;
1259 if (first_vpath
== NULL
)
1262 ret_vpath
= vfs_path_new ();
1264 va_start (args
, first_vpath
);
1269 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1271 vfs_path_element_t
*path_element
;
1273 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1274 g_array_append_val (ret_vpath
->path
, path_element
);
1276 current_vpath
= va_arg (args
, const vfs_path_t
*);
1278 while (current_vpath
!= NULL
);
1284 /* --------------------------------------------------------------------------------------------- */
1286 * get tockens count in path.
1288 * @param vpath path object
1290 * @return count of tokens
1294 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1296 size_t count_tokens
= 0;
1302 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1304 const vfs_path_element_t
*element
;
1305 char **path_tokens
, **iterator
;
1307 element
= vfs_path_get_by_index (vpath
, element_index
);
1308 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1310 while (*iterator
!= NULL
)
1312 if (**iterator
!= '\0')
1316 g_strfreev (path_tokens
);
1318 return count_tokens
;
1321 /* --------------------------------------------------------------------------------------------- */
1323 * Get subpath by tokens
1325 * @param vpath path object
1326 * @param start_position first token for got/ Started from 0.
1327 * If negative, then position will be relative to end of path
1328 * @param length count of tokens
1330 * @return newly allocated string with path tokens separated by slash
1334 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1336 GString
*ret_tokens
, *element_tokens
;
1338 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1344 length
= tokens_count
;
1347 length
= tokens_count
+ length
;
1349 if (start_position
< 0)
1350 start_position
= (ssize_t
) tokens_count
+ start_position
;
1352 if (start_position
< 0)
1355 if (start_position
>= (ssize_t
) tokens_count
)
1358 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1359 length
= tokens_count
- start_position
;
1361 ret_tokens
= g_string_sized_new (32);
1362 element_tokens
= g_string_sized_new (32);
1364 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1366 const vfs_path_element_t
*element
;
1367 char **path_tokens
, **iterator
;
1369 g_string_assign (element_tokens
, "");
1370 element
= vfs_path_get_by_index (vpath
, element_index
);
1371 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1373 while (*iterator
!= NULL
)
1375 if (**iterator
!= '\0')
1377 if (start_position
== 0)
1381 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1382 g_string_free (element_tokens
, TRUE
);
1383 g_strfreev (path_tokens
);
1384 return g_string_free (ret_tokens
, FALSE
);
1387 if (element_tokens
->len
!= 0)
1388 g_string_append_c (element_tokens
, PATH_SEP
);
1389 g_string_append (element_tokens
, *iterator
);
1396 g_strfreev (path_tokens
);
1397 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1400 g_string_free (element_tokens
, TRUE
);
1401 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1404 /* --------------------------------------------------------------------------------------------- */
1406 * Get subpath by tokens
1408 * @param vpath path object
1409 * @param start_position first token for got/ Started from 0.
1410 * If negative, then position will be relative to end of path
1411 * @param length count of tokens
1413 * @return newly allocated path object with path tokens separated by slash
1417 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1420 vfs_path_t
*ret_vpath
= NULL
;
1422 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1423 if (str_tokens
!= NULL
)
1425 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1426 g_free (str_tokens
);
1431 /* --------------------------------------------------------------------------------------------- */
1433 * Build URL parameters (such as user:pass@host:port) from one path element object
1435 * @param element path element
1437 * @return newly allocated string
1441 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1445 if (element
== NULL
)
1448 buffer
= g_string_new ("");
1450 if (element
->user
!= NULL
)
1451 g_string_append (buffer
, element
->user
);
1453 if (element
->password
!= NULL
&& keep_password
)
1455 g_string_append_c (buffer
, ':');
1456 g_string_append (buffer
, element
->password
);
1459 if (element
->host
!= NULL
)
1461 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1462 g_string_append_c (buffer
, '@');
1464 g_string_append_c (buffer
, '[');
1465 g_string_append (buffer
, element
->host
);
1467 g_string_append_c (buffer
, ']');
1470 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1472 g_string_append_c (buffer
, ':');
1473 g_string_append_printf (buffer
, "%d", element
->port
);
1476 return g_string_free (buffer
, FALSE
);
1479 /* --------------------------------------------------------------------------------------------- */
1481 * Build pretty string representation of one path_element_t object
1483 * @param element path element
1485 * @return newly allocated string
1489 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1492 GString
*pretty_path
;
1494 pretty_path
= g_string_new (element
->class->prefix
);
1495 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1497 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1498 g_string_append (pretty_path
, url_params
);
1499 g_free (url_params
);
1501 if (*element
->path
!= PATH_SEP
)
1502 g_string_append_c (pretty_path
, PATH_SEP
);
1504 g_string_append (pretty_path
, element
->path
);
1505 return g_string_free (pretty_path
, FALSE
);
1508 /* --------------------------------------------------------------------------------------------- */
1510 * Compare two path objects as strings
1512 * @param vpath1 first path object
1513 * @param vpath2 second vpath object
1515 * @return integer value like to strcmp.
1519 vfs_path_cmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1525 if (vpath1
== NULL
|| vpath2
== NULL
)
1528 path1
= vfs_path_to_str (vpath1
);
1529 path2
= vfs_path_to_str (vpath2
);
1531 ret_val
= strcmp (path1
, path2
);
1539 /* --------------------------------------------------------------------------------------------- */
1541 * Compare two path objects as strings
1543 * @param vpath1 first path object
1544 * @param vpath2 second vpath object
1545 * @param len number of first 'len' characters
1547 * @return integer value like to strcmp.
1551 vfs_path_ncmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1557 if (vpath1
== NULL
|| vpath2
== NULL
)
1560 path1
= vfs_path_to_str (vpath1
);
1561 path2
= vfs_path_to_str (vpath2
);
1563 ret_val
= strncmp (path1
, path2
, len
);
1571 /* --------------------------------------------------------------------------------------------- */
1573 * Calculate path length in string representation
1575 * @param vpath path object
1577 * @return length of path
1581 vfs_path_len (const vfs_path_t
* vpath
)
1589 path
= vfs_path_to_str (vpath
);
1590 ret_val
= strlen (path
);
1595 /* --------------------------------------------------------------------------------------------- */
1597 * Convert relative vpath object to absolute
1599 * @param vpath path object
1601 * @return absolute path object
1605 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1607 vfs_path_t
*absolute_vpath
;
1610 if (!vpath
->relative
)
1611 return vfs_path_clone (vpath
);
1613 path_str
= vfs_path_to_str (vpath
);
1614 absolute_vpath
= vfs_path_from_str (path_str
);
1616 return absolute_vpath
;
1619 /* --------------------------------------------------------------------------------------------- */