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
);
157 curr_dir
= vfs_get_current_dir ();
158 local
= mc_build_filename (curr_dir
, path
, NULL
);
161 result
= vfs_canon (local
);
167 * So we have path of following form:
168 * /p1/p2#op/.././././p3#op/p4. Good luck.
173 result
= g_strdup (path
);
174 canonicalize_pathname (result
);
179 /* --------------------------------------------------------------------------------------------- */
182 /** get encoding after last #enc: or NULL, if part does not contain #enc:
186 * @return newly allocated string.
190 vfs_get_encoding (const char *path
)
196 work
= g_strdup (path
);
198 /* try found #enc: */
199 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
201 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
203 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
204 slash
= strchr (semi
, PATH_SEP
);
208 g_strlcpy (result
, semi
, sizeof (result
));
210 return g_strdup (result
);
220 /* --------------------------------------------------------------------------------------------- */
221 /** Extract the hostname and username from the path
223 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
225 * ftp://sunsite.unc.edu/pub/linux
226 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
227 * ftp://tsx-11.mit.edu:8192/
228 * ftp://joe@foo.edu:11321/private
229 * ftp://joe:password@foo.se
231 * @param path_element is an input string to be parsed
232 * @param path is an input string to be parsed
234 * @return g_malloc()ed url info.
235 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
236 * is not set, then the current login name is supplied.
237 * Return value is a g_malloc()ed structure with the pathname relative to the
242 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
246 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
248 path_element
->port
= 0;
250 pcopy
= g_strdup (path
);
251 pend
= pcopy
+ strlen (pcopy
);
254 /* search for any possible user */
255 at
= strrchr (pcopy
, '@');
257 /* We have a username */
263 inner_colon
= strchr (pcopy
, ':');
264 if (inner_colon
!= NULL
)
268 path_element
->password
= g_strdup (inner_colon
);
272 path_element
->user
= g_strdup (pcopy
);
280 /* Check if the host comes with a port spec, if so, chop it */
282 colon
= strchr (rest
, ':');
285 colon
= strchr (++rest
, ']');
291 path_element
->ipv6
= TRUE
;
298 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
300 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
301 path_element
->port
= 0;
304 while (*(++colon
) != '\0')
309 path_element
->port
= 1;
312 path_element
->port
= 2;
317 path_element
->host
= g_strdup (rest
);
321 /* --------------------------------------------------------------------------------------------- */
323 * get VFS class for the given name
325 * @param class_name name of class
327 * @return pointer to class structure or NULL if class not found
330 static struct vfs_class
*
331 vfs_get_class_by_name (const char *class_name
)
335 if (class_name
== NULL
)
338 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
340 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
341 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
348 /* --------------------------------------------------------------------------------------------- */
350 * Check if path string contain URL-like elements
352 * @param path_str path
354 * @return TRUE if path is deprecated or FALSE otherwise
358 vfs_path_is_str_path_deprecated (const char *path_str
)
360 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
363 /* --------------------------------------------------------------------------------------------- */
364 /** Split path string to path elements by deprecated algorithm.
366 * @param path_str VFS-path
368 * @return pointer to newly created vfs_path_t object with filled path elements array.
372 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
375 vfs_path_element_t
*element
;
376 struct vfs_class
*class;
377 const char *local
, *op
;
380 vpath
= vfs_path_new ();
382 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
385 element
= g_new0 (vfs_path_element_t
, 1);
386 element
->class = class;
389 element
->path
= vfs_translate_path_n (local
);
392 element
->encoding
= vfs_get_encoding (local
);
393 element
->dir
.converter
=
394 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
397 url_params
= strchr (op
, ':'); /* skip VFS prefix */
398 if (url_params
!= NULL
)
402 vfs_path_url_split (element
, url_params
);
406 element
->vfs_prefix
= g_strdup (op
);
408 g_array_prepend_val (vpath
->path
, element
);
412 element
= g_new0 (vfs_path_element_t
, 1);
413 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
414 element
->path
= vfs_translate_path_n (path
);
417 element
->encoding
= vfs_get_encoding (path
);
418 element
->dir
.converter
=
419 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
421 g_array_prepend_val (vpath
->path
, element
);
427 /* --------------------------------------------------------------------------------------------- */
428 /** Split path string to path elements by URL algorithm.
430 * @param path_str VFS-path
431 * @param flags flags for converter
433 * @return pointer to newly created vfs_path_t object with filled path elements array.
437 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
440 vfs_path_element_t
*element
;
444 vpath
= vfs_path_new ();
445 vpath
->relative
= (flags
& VPF_NO_CANON
) != 0;
447 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
449 char *vfs_prefix_start
;
450 char *real_vfs_prefix_start
= url_delimiter
;
452 struct vfs_s_subclass
*sub
= NULL
;
454 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
455 real_vfs_prefix_start
--;
456 vfs_prefix_start
= real_vfs_prefix_start
;
458 if (*(vfs_prefix_start
) == PATH_SEP
)
459 vfs_prefix_start
+= 1;
461 *url_delimiter
= '\0';
463 element
= g_new0 (vfs_path_element_t
, 1);
464 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
465 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
467 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
468 sub
= VFSDATA (element
);
469 if (sub
!= NULL
&& (sub
->flags
& VFS_S_REMOTE
) != 0)
471 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
472 if (slash_pointer
== NULL
)
474 element
->path
= g_strdup ("");
478 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
480 element
->encoding
= vfs_get_encoding (slash_pointer
);
482 *slash_pointer
= '\0';
484 vfs_path_url_split (element
, url_delimiter
);
488 element
->path
= vfs_translate_path_n (url_delimiter
);
490 element
->encoding
= vfs_get_encoding (url_delimiter
);
494 element
->dir
.converter
=
495 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
497 g_array_prepend_val (vpath
->path
, element
);
499 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
500 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
501 *real_vfs_prefix_start
= '\0';
503 *(real_vfs_prefix_start
+ 1) = '\0';
508 element
= g_new0 (vfs_path_element_t
, 1);
509 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
510 element
->path
= vfs_translate_path_n (path
);
512 element
->encoding
= vfs_get_encoding (path
);
513 element
->dir
.converter
=
514 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
516 g_array_prepend_val (vpath
->path
, element
);
522 /* --------------------------------------------------------------------------------------------- */
524 * Add element's class info to result string (such as VFS name, host, encoding etc)
525 * This function used as helper only in vfs_path_tokens_get() function
527 * @param element current path element
528 * @param ret_tokens total tikens for return
529 * @param element_tokens accumulated element-only tokens
533 vfs_path_tokens_add_class_info (const vfs_path_element_t
* element
, GString
* ret_tokens
,
534 GString
* element_tokens
)
536 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
537 && element_tokens
->len
> 0)
541 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
542 g_string_append_c (ret_tokens
, PATH_SEP
);
544 g_string_append (ret_tokens
, element
->vfs_prefix
);
545 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
547 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
548 if (*url_str
!= '\0')
550 g_string_append (ret_tokens
, url_str
);
551 g_string_append_c (ret_tokens
, PATH_SEP
);
558 if (element
->encoding
!= NULL
)
560 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
561 g_string_append (ret_tokens
, PATH_SEP_STR
);
562 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
563 g_string_append (ret_tokens
, element
->encoding
);
564 g_string_append (ret_tokens
, PATH_SEP_STR
);
568 g_string_append (ret_tokens
, element_tokens
->str
);
571 /* --------------------------------------------------------------------------------------------- */
573 * Strip path to home dir.
574 * @param dir pointer to string contains full path
578 vfs_path_strip_home (const char *dir
)
580 const char *home_dir
= mc_config_get_home_dir ();
582 if (home_dir
!= NULL
)
586 len
= strlen (home_dir
);
588 if (strncmp (dir
, home_dir
, len
) == 0 && (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0'))
589 return g_strdup_printf ("~%s", dir
+ len
);
592 return g_strdup (dir
);
595 /* --------------------------------------------------------------------------------------------- */
597 /* --------------------------------------------------------------------------------------------- */
598 /*** public functions ****************************************************************************/
599 /* --------------------------------------------------------------------------------------------- */
601 #define vfs_append_from_path(appendfrom, is_relative) \
603 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
605 char *stripped_home_str; \
606 stripped_home_str = vfs_path_strip_home (appendfrom); \
607 g_string_append (buffer, stripped_home_str); \
608 g_free (stripped_home_str); \
612 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
613 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
614 g_string_append_c (buffer, PATH_SEP); \
615 g_string_append (buffer, appendfrom); \
620 * Convert first elements_count elements from vfs_path_t to string representation with flags.
622 * @param vpath pointer to vfs_path_t object
623 * @param elements_count count of first elements for convert
624 * @param flags for converter
626 * @return pointer to newly created string.
630 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
634 GString
*recode_buffer
;
639 if (elements_count
== 0 || elements_count
> vfs_path_elements_count (vpath
))
640 elements_count
= vfs_path_elements_count (vpath
);
642 if (elements_count
< 0)
643 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
645 buffer
= g_string_new ("");
646 recode_buffer
= g_string_new ("");
648 for (element_index
= 0; element_index
< elements_count
; element_index
++)
650 const vfs_path_element_t
*element
;
651 gboolean is_relative
= vpath
->relative
&& (element_index
== 0);
653 element
= vfs_path_get_by_index (vpath
, element_index
);
654 if (element
->vfs_prefix
!= NULL
)
657 if ((!is_relative
) && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
658 g_string_append_c (buffer
, PATH_SEP
);
660 g_string_append (buffer
, element
->vfs_prefix
);
661 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
663 url_str
= vfs_path_build_url_params_str (element
, !(flags
& VPF_STRIP_PASSWORD
));
665 if (*url_str
!= '\0')
667 g_string_append (buffer
, url_str
);
668 g_string_append_c (buffer
, PATH_SEP
);
675 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
677 if ((flags
& VPF_HIDE_CHARSET
) == 0)
680 && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
681 g_string_append (buffer
, PATH_SEP_STR
);
682 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
683 g_string_append (buffer
, element
->encoding
);
685 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
686 vfs_append_from_path (recode_buffer
->str
, is_relative
);
687 g_string_set_size (recode_buffer
, 0);
692 vfs_append_from_path (element
->path
, is_relative
);
695 g_string_free (recode_buffer
, TRUE
);
696 return g_string_free (buffer
, FALSE
);
699 #undef vfs_append_from_path
701 /* --------------------------------------------------------------------------------------------- */
703 * Convert first elements_count elements from vfs_path_t to string representation.
705 * @param vpath pointer to vfs_path_t object
706 * @param elements_count count of first elements for convert
708 * @return pointer to newly created string.
712 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
714 return vfs_path_to_str_flags (vpath
, elements_count
, VPF_NONE
);
717 /* --------------------------------------------------------------------------------------------- */
719 * Convert vfs_path_t to string representation.
721 * @param vpath pointer to vfs_path_t object
723 * @return pointer to newly created string.
727 vfs_path_to_str (const vfs_path_t
* vpath
)
729 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
732 /* --------------------------------------------------------------------------------------------- */
734 * Split path string to path elements with flags for change parce process.
736 * @param path_str VFS-path
737 * @param flags flags for parser
739 * @return pointer to newly created vfs_path_t object with filled path elements array.
743 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
748 if (path_str
== NULL
)
751 if ((flags
& VPF_NO_CANON
) == 0)
752 path
= vfs_canon (path_str
);
754 path
= g_strdup (path_str
);
759 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
760 vpath
= vfs_path_from_str_deprecated_parser (path
, flags
);
762 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
769 /* --------------------------------------------------------------------------------------------- */
771 * Split path string to path elements.
773 * @param path_str VFS-path
775 * @return pointer to newly created vfs_path_t object with filled path elements array.
779 vfs_path_from_str (const char *path_str
)
781 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
784 /* --------------------------------------------------------------------------------------------- */
786 * Create new vfs_path_t object.
788 * @return pointer to newly created vfs_path_t object.
796 vpath
= g_new0 (vfs_path_t
, 1);
797 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
802 /* --------------------------------------------------------------------------------------------- */
804 * Get count of path elements.
806 * @param vpath pointer to vfs_path_t object
808 * @return count of path elements.
812 vfs_path_elements_count (const vfs_path_t
* vpath
)
814 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
817 /* --------------------------------------------------------------------------------------------- */
819 * Add vfs_path_element_t object to end of list in vfs_path_t object
820 * @param vpath pointer to vfs_path_t object
821 * @param path_element pointer to vfs_path_element_t object
825 vfs_path_add_element (const vfs_path_t
* vpath
, const vfs_path_element_t
* path_element
)
827 g_array_append_val (vpath
->path
, path_element
);
830 /* --------------------------------------------------------------------------------------------- */
832 * Get one path element by index.
834 * @param vpath pointer to vfs_path_t object
835 * @param element_index element index. May have negative value (in this case count was started at the end of list).
837 * @return path element.
840 const vfs_path_element_t
*
841 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
846 if (element_index
< 0)
847 element_index
+= vfs_path_elements_count (vpath
);
849 if (element_index
< 0)
850 vfs_die ("vfs_path_get_by_index: incorrect index!");
852 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
855 /* --------------------------------------------------------------------------------------------- */
857 * Clone one path element
859 * @param element pointer to vfs_path_element_t object
861 * @return Newly allocated path element
865 vfs_path_element_clone (const vfs_path_element_t
* element
)
867 vfs_path_element_t
*new_element
= g_new (vfs_path_element_t
, 1);
869 new_element
->user
= g_strdup (element
->user
);
870 new_element
->password
= g_strdup (element
->password
);
871 new_element
->host
= g_strdup (element
->host
);
872 new_element
->ipv6
= element
->ipv6
;
873 new_element
->port
= element
->port
;
874 new_element
->path
= g_strdup (element
->path
);
875 new_element
->class = element
->class;
876 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
878 new_element
->encoding
= g_strdup (element
->encoding
);
879 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
880 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
882 new_element
->dir
.converter
= element
->dir
.converter
;
884 new_element
->dir
.info
= element
->dir
.info
;
889 /* --------------------------------------------------------------------------------------------- */
891 * Free one path element.
893 * @param element pointer to vfs_path_element_t object
898 vfs_path_element_free (vfs_path_element_t
* element
)
903 g_free (element
->user
);
904 g_free (element
->password
);
905 g_free (element
->host
);
906 g_free (element
->path
);
907 g_free (element
->vfs_prefix
);
910 g_free (element
->encoding
);
912 if (vfs_path_element_need_cleanup_converter (element
))
913 str_close_conv (element
->dir
.converter
);
919 /* --------------------------------------------------------------------------------------------- */
923 * @param vpath pointer to vfs_path_t object
925 * @return Newly allocated path object
929 vfs_path_clone (const vfs_path_t
* vpath
)
931 vfs_path_t
*new_vpath
;
932 int vpath_element_index
;
937 new_vpath
= vfs_path_new ();
938 new_vpath
->relative
= vpath
->relative
;
940 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
941 vpath_element_index
++)
943 vfs_path_element_t
*path_element
;
945 path_element
= vfs_path_element_clone (vfs_path_get_by_index (vpath
, vpath_element_index
));
946 g_array_append_val (new_vpath
->path
, path_element
);
952 /* --------------------------------------------------------------------------------------------- */
954 * Free vfs_path_t object.
956 * @param vpath pointer to vfs_path_t object
961 vfs_path_free (vfs_path_t
* vpath
)
963 int vpath_element_index
;
968 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
969 vpath_element_index
++)
971 vfs_path_element_t
*path_element
;
973 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, vpath_element_index
);
974 vfs_path_element_free (path_element
);
977 g_array_free (vpath
->path
, TRUE
);
981 /* --------------------------------------------------------------------------------------------- */
983 * Remove one path element by index
985 * @param vpath pointer to vfs_path_t object
986 * @param element_index element index. May have negative value (in this case count was started at the end of list).
991 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
993 vfs_path_element_t
*element
;
995 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
998 if (element_index
< 0)
999 element_index
= vfs_path_elements_count (vpath
) + element_index
;
1001 element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, element_index
);
1002 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
1003 vfs_path_element_free (element
);
1006 /* --------------------------------------------------------------------------------------------- */
1007 /** Return VFS class for the given prefix */
1010 vfs_prefix_to_class (const char *prefix
)
1014 /* Avoid first class (localfs) that would accept any prefix */
1015 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
1017 struct vfs_class
*vfs
;
1019 vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
1020 if (vfs
->which
!= NULL
)
1022 if (vfs
->which (vfs
, prefix
) == -1)
1027 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
1034 /* --------------------------------------------------------------------------------------------- */
1039 * Check if need cleanup charset converter for vfs_path_element_t
1041 * @param element part of path
1043 * @return TRUE if need cleanup converter or FALSE otherwise
1047 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
1049 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
1053 /* --------------------------------------------------------------------------------------------- */
1056 * Serialize vfs_path_t object to string
1058 * @param vpath data for serialization
1059 * @param error contain pointer to object for handle error code and message
1061 * @return serialized vpath as newly allocated string
1065 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
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");
1078 cpath
= mc_config_init (NULL
, FALSE
);
1080 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1083 const vfs_path_element_t
*element
;
1085 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1086 element
= vfs_path_get_by_index (vpath
, element_index
);
1087 /* convert one element to config group */
1089 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1090 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1092 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1094 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1096 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1097 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1098 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1099 if (element
->port
!= 0)
1100 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1105 ret_value
= mc_serialize_config (cpath
, error
);
1106 mc_config_deinit (cpath
);
1110 /* --------------------------------------------------------------------------------------------- */
1112 * Deserialize string to vfs_path_t object
1114 * @param data data for serialization
1115 * @param error contain pointer to object for handle error code and message
1117 * @return newly allocated vfs_path_t object
1121 vfs_path_deserialize (const char *data
, GError
** error
)
1124 size_t element_index
= 0;
1127 cpath
= mc_deserialize_config (data
, error
);
1131 vpath
= vfs_path_new ();
1135 vfs_path_element_t
*element
;
1139 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1140 if (!mc_config_has_group (cpath
, groupname
))
1146 element
= g_new0 (vfs_path_element_t
, 1);
1148 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1149 element
->class = vfs_get_class_by_name (cfg_value
);
1150 if (element
->class == NULL
)
1153 vfs_path_free (vpath
);
1154 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1156 mc_config_deinit (cpath
);
1161 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1164 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1165 element
->dir
.converter
=
1166 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1169 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1171 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1172 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1173 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1174 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1176 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1182 mc_config_deinit (cpath
);
1183 if (vfs_path_elements_count (vpath
) == 0)
1185 vfs_path_free (vpath
);
1186 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1193 /* --------------------------------------------------------------------------------------------- */
1195 * Build vfs_path_t object from arguments.
1197 * @param first_element of path
1198 * @param ... path tokens, terminated by NULL
1200 * @return newly allocated vfs_path_t object
1204 vfs_path_build_filename (const char *first_element
, ...)
1210 if (first_element
== NULL
)
1213 va_start (args
, first_element
);
1214 str_path
= mc_build_filenamev (first_element
, args
);
1216 vpath
= vfs_path_from_str (str_path
);
1221 /* --------------------------------------------------------------------------------------------- */
1223 * Append tokens to path object
1225 * @param vpath path object
1226 * @param first_element of path
1227 * @param ... NULL-terminated strings
1229 * @return newly allocated path object
1233 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1236 char *str_path
, *result_str
;
1237 vfs_path_t
*ret_vpath
;
1239 if (vpath
== NULL
|| first_element
== NULL
)
1242 va_start (args
, first_element
);
1243 str_path
= mc_build_filenamev (first_element
, args
);
1246 result_str
= vfs_path_to_str (vpath
);
1247 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1248 g_free (result_str
);
1255 /* --------------------------------------------------------------------------------------------- */
1258 * Append vpath_t tokens to path object
1260 * @param first_vpath vpath objects
1261 * @param ... NULL-terminated vpath objects
1263 * @return newly allocated path object
1267 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1270 vfs_path_t
*ret_vpath
;
1271 const vfs_path_t
*current_vpath
= first_vpath
;
1273 if (first_vpath
== NULL
)
1276 ret_vpath
= vfs_path_new ();
1278 va_start (args
, first_vpath
);
1283 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1285 vfs_path_element_t
*path_element
;
1287 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1288 g_array_append_val (ret_vpath
->path
, path_element
);
1290 current_vpath
= va_arg (args
, const vfs_path_t
*);
1292 while (current_vpath
!= NULL
);
1298 /* --------------------------------------------------------------------------------------------- */
1301 * get tockens count in path.
1303 * @param vpath path object
1305 * @return count of tokens
1309 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1311 size_t count_tokens
= 0;
1317 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1319 const vfs_path_element_t
*element
;
1320 char **path_tokens
, **iterator
;
1322 element
= vfs_path_get_by_index (vpath
, element_index
);
1323 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1325 while (*iterator
!= NULL
)
1327 if (**iterator
!= '\0')
1331 g_strfreev (path_tokens
);
1333 return count_tokens
;
1336 /* --------------------------------------------------------------------------------------------- */
1339 * Get subpath by tokens
1341 * @param vpath path object
1342 * @param start_position first token for got/ Started from 0.
1343 * If negative, then position will be relative to end of path
1344 * @param length count of tokens
1346 * @return newly allocated string with path tokens separated by slash
1350 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1352 GString
*ret_tokens
, *element_tokens
;
1354 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1360 length
= tokens_count
;
1363 length
= tokens_count
+ length
;
1365 if (start_position
< 0)
1366 start_position
= (ssize_t
) tokens_count
+ start_position
;
1368 if (start_position
< 0)
1371 if (start_position
>= (ssize_t
) tokens_count
)
1374 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1375 length
= tokens_count
- start_position
;
1377 ret_tokens
= g_string_sized_new (32);
1378 element_tokens
= g_string_sized_new (32);
1380 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1382 const vfs_path_element_t
*element
;
1383 char **path_tokens
, **iterator
;
1385 g_string_assign (element_tokens
, "");
1386 element
= vfs_path_get_by_index (vpath
, element_index
);
1387 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1389 while (*iterator
!= NULL
)
1391 if (**iterator
!= '\0')
1393 if (start_position
== 0)
1397 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1398 g_string_free (element_tokens
, TRUE
);
1399 g_strfreev (path_tokens
);
1400 return g_string_free (ret_tokens
, FALSE
);
1403 if (element_tokens
->len
!= 0)
1404 g_string_append_c (element_tokens
, PATH_SEP
);
1405 g_string_append (element_tokens
, *iterator
);
1412 g_strfreev (path_tokens
);
1413 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1416 g_string_free (element_tokens
, TRUE
);
1417 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1420 /* --------------------------------------------------------------------------------------------- */
1422 * Get subpath by tokens
1424 * @param vpath path object
1425 * @param start_position first token for got/ Started from 0.
1426 * If negative, then position will be relative to end of path
1427 * @param length count of tokens
1429 * @return newly allocated path object with path tokens separated by slash
1433 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1436 vfs_path_t
*ret_vpath
= NULL
;
1438 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1439 if (str_tokens
!= NULL
)
1441 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1442 g_free (str_tokens
);
1447 /* --------------------------------------------------------------------------------------------- */
1450 * Build URL parameters (such as user:pass @ host:port) from one path element object
1452 * @param element path element
1453 * @param keep_password TRUE or FALSE
1455 * @return newly allocated string
1459 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1463 if (element
== NULL
)
1466 buffer
= g_string_new ("");
1468 if (element
->user
!= NULL
)
1469 g_string_append (buffer
, element
->user
);
1471 if (element
->password
!= NULL
&& keep_password
)
1473 g_string_append_c (buffer
, ':');
1474 g_string_append (buffer
, element
->password
);
1477 if (element
->host
!= NULL
)
1479 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1480 g_string_append_c (buffer
, '@');
1482 g_string_append_c (buffer
, '[');
1483 g_string_append (buffer
, element
->host
);
1485 g_string_append_c (buffer
, ']');
1488 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1490 g_string_append_c (buffer
, ':');
1491 g_string_append_printf (buffer
, "%d", element
->port
);
1494 return g_string_free (buffer
, FALSE
);
1497 /* --------------------------------------------------------------------------------------------- */
1499 * Build pretty string representation of one path_element_t object
1501 * @param element path element
1503 * @return newly allocated string
1507 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1510 GString
*pretty_path
;
1512 pretty_path
= g_string_new (element
->class->prefix
);
1513 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1515 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1516 g_string_append (pretty_path
, url_params
);
1517 g_free (url_params
);
1519 if (*element
->path
!= PATH_SEP
)
1520 g_string_append_c (pretty_path
, PATH_SEP
);
1522 g_string_append (pretty_path
, element
->path
);
1523 return g_string_free (pretty_path
, FALSE
);
1526 /* --------------------------------------------------------------------------------------------- */
1528 * Compare two path objects as strings
1530 * @param vpath1 first path object
1531 * @param vpath2 second vpath object
1533 * @return integer value like to strcmp.
1537 vfs_path_cmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1543 if (vpath1
== NULL
|| vpath2
== NULL
)
1546 path1
= vfs_path_to_str (vpath1
);
1547 path2
= vfs_path_to_str (vpath2
);
1549 ret_val
= strcmp (path1
, path2
);
1557 /* --------------------------------------------------------------------------------------------- */
1559 * Compare two path objects as strings
1561 * @param vpath1 first path object
1562 * @param vpath2 second vpath object
1563 * @param len number of first 'len' characters
1565 * @return integer value like to strcmp.
1569 vfs_path_ncmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1575 if (vpath1
== NULL
|| vpath2
== NULL
)
1578 path1
= vfs_path_to_str (vpath1
);
1579 path2
= vfs_path_to_str (vpath2
);
1581 ret_val
= strncmp (path1
, path2
, len
);
1589 /* --------------------------------------------------------------------------------------------- */
1591 * Calculate path length in string representation
1593 * @param vpath path object
1595 * @return length of path
1599 vfs_path_len (const vfs_path_t
* vpath
)
1607 path
= vfs_path_to_str (vpath
);
1608 ret_val
= strlen (path
);
1613 /* --------------------------------------------------------------------------------------------- */
1615 * Convert relative vpath object to absolute
1617 * @param vpath path object
1619 * @return absolute path object
1623 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1625 vfs_path_t
*absolute_vpath
;
1628 if (!vpath
->relative
)
1629 return vfs_path_clone (vpath
);
1631 path_str
= vfs_path_to_str (vpath
);
1632 absolute_vpath
= vfs_path_from_str (path_str
);
1634 return absolute_vpath
;
1637 /* --------------------------------------------------------------------------------------------- */