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 /* --------------------------------------------------------------------------------------------- */
175 /** get encoding after last #enc: or NULL, if part does not contain #enc:
179 * @return newly allocated string.
183 vfs_get_encoding (const char *path
)
189 work
= g_strdup (path
);
191 /* try found #enc: */
192 semi
= g_strrstr (work
, VFS_ENCODING_PREFIX
);
194 if (semi
!= NULL
&& (semi
== work
|| *(semi
- 1) == PATH_SEP
))
196 semi
+= strlen (VFS_ENCODING_PREFIX
); /* skip "#enc:" */
197 slash
= strchr (semi
, PATH_SEP
);
201 g_strlcpy (result
, semi
, sizeof (result
));
203 return g_strdup (result
);
212 /* --------------------------------------------------------------------------------------------- */
213 /** Extract the hostname and username from the path
215 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
217 * ftp://sunsite.unc.edu/pub/linux
218 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
219 * ftp://tsx-11.mit.edu:8192/
220 * ftp://joe@foo.edu:11321/private
221 * ftp://joe:password@foo.se
223 * @param path_element is an input string to be parsed
224 * @param path is an input string to be parsed
226 * @return g_malloc()ed url info.
227 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
228 * is not set, then the current login name is supplied.
229 * Return value is a g_malloc()ed structure with the pathname relative to the
234 vfs_path_url_split (vfs_path_element_t
* path_element
, const char *path
)
238 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
240 path_element
->port
= 0;
242 pcopy
= g_strdup (path
);
243 pend
= pcopy
+ strlen (pcopy
);
246 /* search for any possible user */
247 at
= strrchr (pcopy
, '@');
249 /* We have a username */
255 inner_colon
= strchr (pcopy
, ':');
256 if (inner_colon
!= NULL
)
260 path_element
->password
= g_strdup (inner_colon
);
264 path_element
->user
= g_strdup (pcopy
);
272 /* Check if the host comes with a port spec, if so, chop it */
274 colon
= strchr (rest
, ':');
277 colon
= strchr (++rest
, ']');
283 path_element
->ipv6
= TRUE
;
290 if (sscanf (colon
+ 1, "%d", &path_element
->port
) == 1)
292 if (path_element
->port
<= 0 || path_element
->port
>= 65536)
293 path_element
->port
= 0;
296 while (*(++colon
) != '\0')
301 path_element
->port
= 1;
304 path_element
->port
= 2;
309 path_element
->host
= g_strdup (rest
);
313 /* --------------------------------------------------------------------------------------------- */
315 * get VFS class for the given name
317 * @param class_name name of class
319 * @return pointer to class structure or NULL if class not found
322 static struct vfs_class
*
323 vfs_get_class_by_name (const char *class_name
)
327 if (class_name
== NULL
)
330 for (i
= 0; i
< vfs__classes_list
->len
; i
++)
332 struct vfs_class
*vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
333 if ((vfs
->name
!= NULL
) && (strcmp (vfs
->name
, class_name
) == 0))
340 /* --------------------------------------------------------------------------------------------- */
342 * Check if path string contain URL-like elements
344 * @param path_str path
346 * @return TRUE if path is deprecated or FALSE otherwise
350 vfs_path_is_str_path_deprecated (const char *path_str
)
352 return strstr (path_str
, VFS_PATH_URL_DELIMITER
) == NULL
;
355 /* --------------------------------------------------------------------------------------------- */
356 /** Split path string to path elements by deprecated algorithm.
358 * @param path_str VFS-path
360 * @return pointer to newly created vfs_path_t object with filled path elements array.
364 vfs_path_from_str_deprecated_parser (char *path
, vfs_path_flag_t flags
)
367 vfs_path_element_t
*element
;
368 struct vfs_class
*class;
369 const char *local
, *op
;
372 vpath
= vfs_path_new ();
374 while ((class = _vfs_split_with_semi_skip_count (path
, &local
, &op
, 0)) != NULL
)
377 element
= g_new0 (vfs_path_element_t
, 1);
378 element
->class = class;
381 element
->path
= vfs_translate_path_n (local
);
383 element
->encoding
= vfs_get_encoding (local
);
384 element
->dir
.converter
=
385 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
387 url_params
= strchr (op
, ':'); /* skip VFS prefix */
388 if (url_params
!= NULL
)
392 vfs_path_url_split (element
, url_params
);
396 element
->vfs_prefix
= g_strdup (op
);
398 g_array_prepend_val (vpath
->path
, element
);
402 element
= g_new0 (vfs_path_element_t
, 1);
403 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
404 element
->path
= vfs_translate_path_n (path
);
406 element
->encoding
= vfs_get_encoding (path
);
407 element
->dir
.converter
=
408 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
409 g_array_prepend_val (vpath
->path
, element
);
415 /* --------------------------------------------------------------------------------------------- */
416 /** Split path string to path elements by URL algorithm.
418 * @param path_str VFS-path
420 * @return pointer to newly created vfs_path_t object with filled path elements array.
424 vfs_path_from_str_uri_parser (char *path
, vfs_path_flag_t flags
)
427 vfs_path_element_t
*element
;
431 vpath
= vfs_path_new ();
432 vpath
->relative
= (flags
& VPF_NO_CANON
) != 0;
434 while ((url_delimiter
= g_strrstr (path
, VFS_PATH_URL_DELIMITER
)) != NULL
)
436 char *vfs_prefix_start
;
437 char *real_vfs_prefix_start
= url_delimiter
;
439 struct vfs_s_subclass
*sub
= NULL
;
441 while (real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) != PATH_SEP
)
442 real_vfs_prefix_start
--;
443 vfs_prefix_start
= real_vfs_prefix_start
;
445 if (*(vfs_prefix_start
) == PATH_SEP
)
446 vfs_prefix_start
+= 1;
448 *url_delimiter
= '\0';
450 element
= g_new0 (vfs_path_element_t
, 1);
451 element
->class = vfs_prefix_to_class (vfs_prefix_start
);
452 element
->vfs_prefix
= g_strdup (vfs_prefix_start
);
454 url_delimiter
+= strlen (VFS_PATH_URL_DELIMITER
);
455 sub
= VFSDATA (element
);
456 if (sub
!= NULL
&& (sub
->flags
& VFS_S_REMOTE
) != 0)
458 slash_pointer
= strchr (url_delimiter
, PATH_SEP
);
459 if (slash_pointer
== NULL
)
461 element
->path
= g_strdup ("");
465 element
->path
= vfs_translate_path_n (slash_pointer
+ 1);
466 element
->encoding
= vfs_get_encoding (slash_pointer
);
468 *slash_pointer
= '\0';
470 vfs_path_url_split (element
, url_delimiter
);
474 element
->path
= vfs_translate_path_n (url_delimiter
);
475 element
->encoding
= vfs_get_encoding (url_delimiter
);
477 element
->dir
.converter
=
478 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
479 g_array_prepend_val (vpath
->path
, element
);
481 if ((real_vfs_prefix_start
> path
&& *(real_vfs_prefix_start
) == PATH_SEP
) ||
482 (real_vfs_prefix_start
== path
&& *(real_vfs_prefix_start
) != PATH_SEP
))
483 *real_vfs_prefix_start
= '\0';
485 *(real_vfs_prefix_start
+ 1) = '\0';
490 element
= g_new0 (vfs_path_element_t
, 1);
491 element
->class = g_ptr_array_index (vfs__classes_list
, 0);
492 element
->path
= vfs_translate_path_n (path
);
493 element
->encoding
= vfs_get_encoding (path
);
494 element
->dir
.converter
=
495 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
496 g_array_prepend_val (vpath
->path
, element
);
502 /* --------------------------------------------------------------------------------------------- */
504 * Add element's class info to result string (such as VFS name, host, encoding etc)
505 * This function used as helper only in vfs_path_tokens_get() function
507 * @param element current path element
508 * @param ret_tokens total tikens for return
509 * @param element_tokens accumulated element-only tokens
513 vfs_path_tokens_add_class_info (const vfs_path_element_t
* element
, GString
* ret_tokens
,
514 GString
* element_tokens
)
516 if (((element
->class->flags
& VFSF_LOCAL
) == 0 || ret_tokens
->len
> 0)
517 && element_tokens
->len
> 0)
521 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
522 g_string_append_c (ret_tokens
, PATH_SEP
);
524 g_string_append (ret_tokens
, element
->vfs_prefix
);
525 g_string_append (ret_tokens
, VFS_PATH_URL_DELIMITER
);
527 url_str
= vfs_path_build_url_params_str (element
, TRUE
);
528 if (*url_str
!= '\0')
530 g_string_append (ret_tokens
, url_str
);
531 g_string_append_c (ret_tokens
, PATH_SEP
);
536 if (element
->encoding
!= NULL
)
538 if (ret_tokens
->len
> 0 && ret_tokens
->str
[ret_tokens
->len
- 1] != PATH_SEP
)
539 g_string_append (ret_tokens
, PATH_SEP_STR
);
540 g_string_append (ret_tokens
, VFS_ENCODING_PREFIX
);
541 g_string_append (ret_tokens
, element
->encoding
);
542 g_string_append (ret_tokens
, PATH_SEP_STR
);
545 g_string_append (ret_tokens
, element_tokens
->str
);
548 /* --------------------------------------------------------------------------------------------- */
550 * Strip path to home dir.
551 * @param dir pointer to string contains full path
555 vfs_path_strip_home (const char *dir
)
557 const char *home_dir
= mc_config_get_home_dir ();
559 if (home_dir
!= NULL
)
563 len
= strlen (home_dir
);
565 if (strncmp (dir
, home_dir
, len
) == 0 && (dir
[len
] == PATH_SEP
|| dir
[len
] == '\0'))
566 return g_strdup_printf ("~%s", dir
+ len
);
569 return g_strdup (dir
);
572 /* --------------------------------------------------------------------------------------------- */
574 /* --------------------------------------------------------------------------------------------- */
575 /*** public functions ****************************************************************************/
576 /* --------------------------------------------------------------------------------------------- */
578 * Convert first elements_count elements from vfs_path_t to string representation with flags.
580 * @param vpath pointer to vfs_path_t object
581 * @param elements_count count of first elements for convert
582 * @param flags flags for converter
584 * @return pointer to newly created string.
587 #define vfs_append_from_path(appendfrom, is_relative) \
589 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
591 char *stripped_home_str; \
592 stripped_home_str = vfs_path_strip_home (appendfrom); \
593 g_string_append (buffer, stripped_home_str); \
594 g_free (stripped_home_str); \
598 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
599 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
600 g_string_append_c (buffer, PATH_SEP); \
601 g_string_append (buffer, appendfrom); \
606 vfs_path_to_str_flags (const vfs_path_t
* vpath
, int elements_count
, vfs_path_flag_t flags
)
610 GString
*recode_buffer
;
615 if (elements_count
== 0 || elements_count
> vfs_path_elements_count (vpath
))
616 elements_count
= vfs_path_elements_count (vpath
);
618 if (elements_count
< 0)
619 elements_count
= vfs_path_elements_count (vpath
) + elements_count
;
621 buffer
= g_string_new ("");
622 recode_buffer
= g_string_new ("");
624 for (element_index
= 0; element_index
< elements_count
; element_index
++)
626 const vfs_path_element_t
*element
;
627 gboolean is_relative
= vpath
->relative
&& (element_index
== 0);
629 element
= vfs_path_get_by_index (vpath
, element_index
);
630 if (element
->vfs_prefix
!= NULL
)
633 if ((!is_relative
) && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
634 g_string_append_c (buffer
, PATH_SEP
);
636 g_string_append (buffer
, element
->vfs_prefix
);
637 g_string_append (buffer
, VFS_PATH_URL_DELIMITER
);
639 url_str
= vfs_path_build_url_params_str (element
, !(flags
& VPF_STRIP_PASSWORD
));
641 if (*url_str
!= '\0')
643 g_string_append (buffer
, url_str
);
644 g_string_append_c (buffer
, PATH_SEP
);
650 if ((flags
& VPF_RECODE
) == 0 && vfs_path_element_need_cleanup_converter (element
))
652 if ((flags
& VPF_HIDE_CHARSET
) == 0)
655 && (buffer
->len
== 0 || buffer
->str
[buffer
->len
- 1] != PATH_SEP
))
656 g_string_append (buffer
, PATH_SEP_STR
);
657 g_string_append (buffer
, VFS_ENCODING_PREFIX
);
658 g_string_append (buffer
, element
->encoding
);
660 str_vfs_convert_from (element
->dir
.converter
, element
->path
, recode_buffer
);
661 vfs_append_from_path (recode_buffer
->str
, is_relative
);
662 g_string_set_size (recode_buffer
, 0);
666 vfs_append_from_path (element
->path
, is_relative
);
669 g_string_free (recode_buffer
, TRUE
);
670 return g_string_free (buffer
, FALSE
);
673 #undef vfs_append_from_path
675 /* --------------------------------------------------------------------------------------------- */
677 * Convert first elements_count elements from vfs_path_t to string representation.
679 * @param vpath pointer to vfs_path_t object
680 * @param elements_count count of first elements for convert
682 * @return pointer to newly created string.
686 vfs_path_to_str_elements_count (const vfs_path_t
* vpath
, int elements_count
)
688 return vfs_path_to_str_flags (vpath
, elements_count
, VPF_NONE
);
691 /* --------------------------------------------------------------------------------------------- */
693 * Convert vfs_path_t to string representation.
695 * @param vpath pointer to vfs_path_t object
697 * @return pointer to newly created string.
701 vfs_path_to_str (const vfs_path_t
* vpath
)
703 return vfs_path_to_str_elements_count (vpath
, vfs_path_elements_count (vpath
));
706 /* --------------------------------------------------------------------------------------------- */
708 * Split path string to path elements with flags for change parce process.
710 * @param path_str VFS-path
711 * @param flags flags for parser
713 * @return pointer to newly created vfs_path_t object with filled path elements array.
717 vfs_path_from_str_flags (const char *path_str
, vfs_path_flag_t flags
)
722 if (path_str
== NULL
)
725 if ((flags
& VPF_NO_CANON
) == 0)
726 path
= vfs_canon (path_str
);
728 path
= g_strdup (path_str
);
733 if ((flags
& VPF_USE_DEPRECATED_PARSER
) != 0 && vfs_path_is_str_path_deprecated (path
))
734 vpath
= vfs_path_from_str_deprecated_parser (path
, flags
);
736 vpath
= vfs_path_from_str_uri_parser (path
, flags
);
743 /* --------------------------------------------------------------------------------------------- */
745 * Split path string to path elements.
747 * @param path_str VFS-path
749 * @return pointer to newly created vfs_path_t object with filled path elements array.
753 vfs_path_from_str (const char *path_str
)
755 return vfs_path_from_str_flags (path_str
, VPF_NONE
);
758 /* --------------------------------------------------------------------------------------------- */
760 * Create new vfs_path_t object.
762 * @return pointer to newly created vfs_path_t object.
770 vpath
= g_new0 (vfs_path_t
, 1);
771 vpath
->path
= g_array_new (FALSE
, TRUE
, sizeof (vfs_path_element_t
*));
776 /* --------------------------------------------------------------------------------------------- */
778 * Get count of path elements.
780 * @param vpath pointer to vfs_path_t object
782 * @return count of path elements.
786 vfs_path_elements_count (const vfs_path_t
* vpath
)
788 return (vpath
!= NULL
&& vpath
->path
!= NULL
) ? vpath
->path
->len
: 0;
791 /* --------------------------------------------------------------------------------------------- */
793 * Add vfs_path_element_t object to end of list in vfs_path_t object
794 * @param vpath pointer to vfs_path_t object
795 * @param path_element pointer to vfs_path_element_t object
799 vfs_path_add_element (const vfs_path_t
* vpath
, const vfs_path_element_t
* path_element
)
801 g_array_append_val (vpath
->path
, path_element
);
804 /* --------------------------------------------------------------------------------------------- */
806 * Get one path element by index.
808 * @param vpath pointer to vfs_path_t object
809 * @param element_index element index. May have negative value (in this case count was started at the end of list).
811 * @return path element.
814 const vfs_path_element_t
*
815 vfs_path_get_by_index (const vfs_path_t
* vpath
, int element_index
)
820 if (element_index
< 0)
821 element_index
+= vfs_path_elements_count (vpath
);
823 if (element_index
< 0)
824 vfs_die ("vfs_path_get_by_index: incorrect index!");
826 return g_array_index (vpath
->path
, vfs_path_element_t
*, element_index
);
829 /* --------------------------------------------------------------------------------------------- */
831 * Clone one path element
833 * @param element pointer to vfs_path_element_t object
835 * @return Newly allocated path element
839 vfs_path_element_clone (const vfs_path_element_t
* element
)
841 vfs_path_element_t
*new_element
= g_new (vfs_path_element_t
, 1);
843 new_element
->user
= g_strdup (element
->user
);
844 new_element
->password
= g_strdup (element
->password
);
845 new_element
->host
= g_strdup (element
->host
);
846 new_element
->ipv6
= element
->ipv6
;
847 new_element
->port
= element
->port
;
848 new_element
->path
= g_strdup (element
->path
);
849 new_element
->class = element
->class;
850 new_element
->encoding
= g_strdup (element
->encoding
);
851 new_element
->vfs_prefix
= g_strdup (element
->vfs_prefix
);
852 if (vfs_path_element_need_cleanup_converter (element
) && new_element
->encoding
!= NULL
)
853 new_element
->dir
.converter
= str_crt_conv_from (new_element
->encoding
);
855 new_element
->dir
.converter
= element
->dir
.converter
;
856 new_element
->dir
.info
= element
->dir
.info
;
861 /* --------------------------------------------------------------------------------------------- */
863 * Free one path element.
865 * @param element pointer to vfs_path_element_t object
870 vfs_path_element_free (vfs_path_element_t
* element
)
875 g_free (element
->user
);
876 g_free (element
->password
);
877 g_free (element
->host
);
878 g_free (element
->path
);
879 g_free (element
->encoding
);
880 g_free (element
->vfs_prefix
);
882 if (vfs_path_element_need_cleanup_converter (element
))
884 str_close_conv (element
->dir
.converter
);
890 /* --------------------------------------------------------------------------------------------- */
894 * @param vpath pointer to vfs_path_t object
896 * @return Newly allocated path object
900 vfs_path_clone (const vfs_path_t
* vpath
)
902 vfs_path_t
*new_vpath
;
903 int vpath_element_index
;
908 new_vpath
= vfs_path_new ();
909 new_vpath
->relative
= vpath
->relative
;
911 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
912 vpath_element_index
++)
914 vfs_path_element_t
*path_element
;
916 path_element
= vfs_path_element_clone (vfs_path_get_by_index (vpath
, vpath_element_index
));
917 g_array_append_val (new_vpath
->path
, path_element
);
923 /* --------------------------------------------------------------------------------------------- */
925 * Free vfs_path_t object.
927 * @param vpath pointer to vfs_path_t object
932 vfs_path_free (vfs_path_t
* vpath
)
934 int vpath_element_index
;
939 for (vpath_element_index
= 0; vpath_element_index
< vfs_path_elements_count (vpath
);
940 vpath_element_index
++)
942 vfs_path_element_t
*path_element
;
944 path_element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, vpath_element_index
);
945 vfs_path_element_free (path_element
);
948 g_array_free (vpath
->path
, TRUE
);
952 /* --------------------------------------------------------------------------------------------- */
954 * Remove one path element by index
956 * @param vpath pointer to vfs_path_t object
957 * @param element_index element index. May have negative value (in this case count was started at the end of list).
962 vfs_path_remove_element_by_index (vfs_path_t
* vpath
, int element_index
)
964 vfs_path_element_t
*element
;
966 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 1))
969 if (element_index
< 0)
970 element_index
= vfs_path_elements_count (vpath
) + element_index
;
972 element
= (vfs_path_element_t
*) vfs_path_get_by_index (vpath
, element_index
);
973 vpath
->path
= g_array_remove_index (vpath
->path
, element_index
);
974 vfs_path_element_free (element
);
977 /* --------------------------------------------------------------------------------------------- */
978 /** Return VFS class for the given prefix */
981 vfs_prefix_to_class (const char *prefix
)
985 /* Avoid first class (localfs) that would accept any prefix */
986 for (i
= 1; i
< vfs__classes_list
->len
; i
++)
988 struct vfs_class
*vfs
;
990 vfs
= (struct vfs_class
*) g_ptr_array_index (vfs__classes_list
, i
);
991 if (vfs
->which
!= NULL
)
993 if (vfs
->which (vfs
, prefix
) == -1)
998 if (vfs
->prefix
!= NULL
&& strncmp (prefix
, vfs
->prefix
, strlen (vfs
->prefix
)) == 0)
1005 /* --------------------------------------------------------------------------------------------- */
1007 * Check if need cleanup charset converter for vfs_path_element_t
1009 * @param element part of path
1011 * @return TRUE if need cleanup converter or FALSE otherwise
1015 vfs_path_element_need_cleanup_converter (const vfs_path_element_t
* element
)
1017 return (element
->dir
.converter
!= str_cnv_from_term
&& element
->dir
.converter
!= INVALID_CONV
);
1020 /* --------------------------------------------------------------------------------------------- */
1022 * Serialize vfs_path_t object to string
1024 * @param vpath data for serialization
1025 * @param error contain pointer to object for handle error code and message
1027 * @return serialized vpath as newly allocated string
1031 vfs_path_serialize (const vfs_path_t
* vpath
, GError
** error
)
1033 mc_config_t
*cpath
= mc_config_init (NULL
);
1034 ssize_t element_index
;
1037 if ((vpath
== NULL
) || (vfs_path_elements_count (vpath
) == 0))
1039 g_set_error (error
, MC_ERROR
, -1, "vpath object is empty");
1043 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1046 const vfs_path_element_t
*element
;
1048 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1049 element
= vfs_path_get_by_index (vpath
, element_index
);
1050 /* convert one element to config group */
1052 mc_config_set_string_raw (cpath
, groupname
, "path", element
->path
);
1053 mc_config_set_string_raw (cpath
, groupname
, "class-name", element
->class->name
);
1054 mc_config_set_string_raw (cpath
, groupname
, "encoding", element
->encoding
);
1056 mc_config_set_string_raw (cpath
, groupname
, "vfs_prefix", element
->vfs_prefix
);
1058 mc_config_set_string_raw (cpath
, groupname
, "user", element
->user
);
1059 mc_config_set_string_raw (cpath
, groupname
, "password", element
->password
);
1060 mc_config_set_string_raw (cpath
, groupname
, "host", element
->host
);
1061 if (element
->port
!= 0)
1062 mc_config_set_int (cpath
, groupname
, "port", element
->port
);
1067 ret_value
= mc_serialize_config (cpath
, error
);
1068 mc_config_deinit (cpath
);
1072 /* --------------------------------------------------------------------------------------------- */
1074 * Deserialize string to vfs_path_t object
1076 * @param data data for serialization
1077 * @param error contain pointer to object for handle error code and message
1079 * @return newly allocated vfs_path_t object
1083 vfs_path_deserialize (const char *data
, GError
** error
)
1085 mc_config_t
*cpath
= mc_deserialize_config (data
, error
);
1086 size_t element_index
= 0;
1092 vpath
= vfs_path_new ();
1096 vfs_path_element_t
*element
;
1100 groupname
= g_strdup_printf ("path-element-%zd", element_index
);
1101 if (!mc_config_has_group (cpath
, groupname
))
1107 element
= g_new0 (vfs_path_element_t
, 1);
1109 cfg_value
= mc_config_get_string_raw (cpath
, groupname
, "class-name", NULL
);
1110 element
->class = vfs_get_class_by_name (cfg_value
);
1111 if (element
->class == NULL
)
1114 vfs_path_free (vpath
);
1115 g_set_error (error
, MC_ERROR
, -1, "Unable to find VFS class by name '%s'", cfg_value
);
1117 mc_config_deinit (cpath
);
1122 element
->path
= mc_config_get_string_raw (cpath
, groupname
, "path", NULL
);
1123 element
->encoding
= mc_config_get_string_raw (cpath
, groupname
, "encoding", NULL
);
1124 element
->dir
.converter
=
1125 (element
->encoding
!= NULL
) ? str_crt_conv_from (element
->encoding
) : INVALID_CONV
;
1127 element
->vfs_prefix
= mc_config_get_string_raw (cpath
, groupname
, "vfs_prefix", NULL
);
1129 element
->user
= mc_config_get_string_raw (cpath
, groupname
, "user", NULL
);
1130 element
->password
= mc_config_get_string_raw (cpath
, groupname
, "password", NULL
);
1131 element
->host
= mc_config_get_string_raw (cpath
, groupname
, "host", NULL
);
1132 element
->port
= mc_config_get_int (cpath
, groupname
, "port", 0);
1134 vpath
->path
= g_array_append_val (vpath
->path
, element
);
1140 mc_config_deinit (cpath
);
1141 if (vfs_path_elements_count (vpath
) == 0)
1143 vfs_path_free (vpath
);
1144 g_set_error (error
, MC_ERROR
, -1, "No any path elements found");
1151 /* --------------------------------------------------------------------------------------------- */
1153 * Build vfs_path_t object from arguments.
1155 * @param ... path tokens, terminated by NULL
1157 * @return newly allocated vfs_path_t object
1161 vfs_path_build_filename (const char *first_element
, ...)
1167 if (first_element
== NULL
)
1170 va_start (args
, first_element
);
1171 str_path
= mc_build_filenamev (first_element
, args
);
1173 vpath
= vfs_path_from_str (str_path
);
1178 /* --------------------------------------------------------------------------------------------- */
1180 * Append tokens to path object
1182 * @param vpath path object
1183 * @param ... NULL-terminated strings
1185 * @return newly allocated path object
1189 vfs_path_append_new (const vfs_path_t
* vpath
, const char *first_element
, ...)
1192 char *str_path
, *result_str
;
1193 vfs_path_t
*ret_vpath
;
1195 if (vpath
== NULL
|| first_element
== NULL
)
1198 va_start (args
, first_element
);
1199 str_path
= mc_build_filenamev (first_element
, args
);
1202 result_str
= vfs_path_to_str (vpath
);
1203 ret_vpath
= vfs_path_build_filename (result_str
, str_path
, NULL
);
1204 g_free (result_str
);
1211 /* --------------------------------------------------------------------------------------------- */
1214 * Append vpath_t tokens to path object
1216 * @param ... NULL-terminated vpath objects
1218 * @return newly allocated path object
1222 vfs_path_append_vpath_new (const vfs_path_t
* first_vpath
, ...)
1225 vfs_path_t
*ret_vpath
;
1226 const vfs_path_t
*current_vpath
= first_vpath
;
1228 if (first_vpath
== NULL
)
1231 ret_vpath
= vfs_path_new ();
1233 va_start (args
, first_vpath
);
1238 for (vindex
= 0; vindex
< vfs_path_elements_count (current_vpath
); vindex
++)
1240 vfs_path_element_t
*path_element
;
1242 path_element
= vfs_path_element_clone (vfs_path_get_by_index (current_vpath
, vindex
));
1243 g_array_append_val (ret_vpath
->path
, path_element
);
1245 current_vpath
= va_arg (args
, const vfs_path_t
*);
1247 while (current_vpath
!= NULL
);
1253 /* --------------------------------------------------------------------------------------------- */
1255 * get tockens count in path.
1257 * @param vpath path object
1259 * @return count of tokens
1263 vfs_path_tokens_count (const vfs_path_t
* vpath
)
1265 size_t count_tokens
= 0;
1271 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1273 const vfs_path_element_t
*element
;
1274 char **path_tokens
, **iterator
;
1276 element
= vfs_path_get_by_index (vpath
, element_index
);
1277 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1279 while (*iterator
!= NULL
)
1281 if (**iterator
!= '\0')
1285 g_strfreev (path_tokens
);
1287 return count_tokens
;
1290 /* --------------------------------------------------------------------------------------------- */
1292 * Get subpath by tokens
1294 * @param vpath path object
1295 * @param start_position first token for got/ Started from 0.
1296 * If negative, then position will be relative to end of path
1297 * @param length count of tokens
1299 * @return newly allocated string with path tokens separated by slash
1303 vfs_path_tokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1305 GString
*ret_tokens
, *element_tokens
;
1307 size_t tokens_count
= vfs_path_tokens_count (vpath
);
1313 length
= tokens_count
;
1316 length
= tokens_count
+ length
;
1318 if (start_position
< 0)
1319 start_position
= (ssize_t
) tokens_count
+ start_position
;
1321 if (start_position
< 0)
1324 if (start_position
>= (ssize_t
) tokens_count
)
1327 if (start_position
+ (ssize_t
) length
> (ssize_t
) tokens_count
)
1328 length
= tokens_count
- start_position
;
1330 ret_tokens
= g_string_sized_new (32);
1331 element_tokens
= g_string_sized_new (32);
1333 for (element_index
= 0; element_index
< vfs_path_elements_count (vpath
); element_index
++)
1335 const vfs_path_element_t
*element
;
1336 char **path_tokens
, **iterator
;
1338 g_string_assign (element_tokens
, "");
1339 element
= vfs_path_get_by_index (vpath
, element_index
);
1340 path_tokens
= iterator
= g_strsplit (element
->path
, PATH_SEP_STR
, -1);
1342 while (*iterator
!= NULL
)
1344 if (**iterator
!= '\0')
1346 if (start_position
== 0)
1350 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1351 g_string_free (element_tokens
, TRUE
);
1352 g_strfreev (path_tokens
);
1353 return g_string_free (ret_tokens
, FALSE
);
1356 if (element_tokens
->len
!= 0)
1357 g_string_append_c (element_tokens
, PATH_SEP
);
1358 g_string_append (element_tokens
, *iterator
);
1365 g_strfreev (path_tokens
);
1366 vfs_path_tokens_add_class_info (element
, ret_tokens
, element_tokens
);
1369 g_string_free (element_tokens
, TRUE
);
1370 return g_string_free (ret_tokens
, !(start_position
== 0 && length
== 0));
1373 /* --------------------------------------------------------------------------------------------- */
1375 * Get subpath by tokens
1377 * @param vpath path object
1378 * @param start_position first token for got/ Started from 0.
1379 * If negative, then position will be relative to end of path
1380 * @param length count of tokens
1382 * @return newly allocated path object with path tokens separated by slash
1386 vfs_path_vtokens_get (const vfs_path_t
* vpath
, ssize_t start_position
, ssize_t length
)
1389 vfs_path_t
*ret_vpath
= NULL
;
1391 str_tokens
= vfs_path_tokens_get (vpath
, start_position
, length
);
1392 if (str_tokens
!= NULL
)
1394 ret_vpath
= vfs_path_from_str_flags (str_tokens
, VPF_NO_CANON
);
1395 g_free (str_tokens
);
1400 /* --------------------------------------------------------------------------------------------- */
1402 * Build URL parameters (such as user:pass@host:port) from one path element object
1404 * @param element path element
1406 * @return newly allocated string
1410 vfs_path_build_url_params_str (const vfs_path_element_t
* element
, gboolean keep_password
)
1414 if (element
== NULL
)
1417 buffer
= g_string_new ("");
1419 if (element
->user
!= NULL
)
1420 g_string_append (buffer
, element
->user
);
1422 if (element
->password
!= NULL
&& keep_password
)
1424 g_string_append_c (buffer
, ':');
1425 g_string_append (buffer
, element
->password
);
1428 if (element
->host
!= NULL
)
1430 if ((element
->user
!= NULL
) || (element
->password
!= NULL
))
1431 g_string_append_c (buffer
, '@');
1433 g_string_append_c (buffer
, '[');
1434 g_string_append (buffer
, element
->host
);
1436 g_string_append_c (buffer
, ']');
1439 if ((element
->port
) != 0 && (element
->host
!= NULL
))
1441 g_string_append_c (buffer
, ':');
1442 g_string_append_printf (buffer
, "%d", element
->port
);
1445 return g_string_free (buffer
, FALSE
);
1448 /* --------------------------------------------------------------------------------------------- */
1450 * Build pretty string representation of one path_element_t object
1452 * @param element path element
1454 * @return newly allocated string
1458 vfs_path_element_build_pretty_path_str (const vfs_path_element_t
* element
)
1461 GString
*pretty_path
;
1463 pretty_path
= g_string_new (element
->class->prefix
);
1464 g_string_append (pretty_path
, VFS_PATH_URL_DELIMITER
);
1466 url_params
= vfs_path_build_url_params_str (element
, FALSE
);
1467 g_string_append (pretty_path
, url_params
);
1468 g_free (url_params
);
1470 if (*element
->path
!= PATH_SEP
)
1471 g_string_append_c (pretty_path
, PATH_SEP
);
1473 g_string_append (pretty_path
, element
->path
);
1474 return g_string_free (pretty_path
, FALSE
);
1477 /* --------------------------------------------------------------------------------------------- */
1479 * Compare two path objects as strings
1481 * @param vpath1 first path object
1482 * @param vpath2 second vpath object
1484 * @return integer value like to strcmp.
1488 vfs_path_cmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
)
1494 if (vpath1
== NULL
|| vpath2
== NULL
)
1497 path1
= vfs_path_to_str (vpath1
);
1498 path2
= vfs_path_to_str (vpath2
);
1500 ret_val
= strcmp (path1
, path2
);
1508 /* --------------------------------------------------------------------------------------------- */
1510 * Compare two path objects as strings
1512 * @param vpath1 first path object
1513 * @param vpath2 second vpath object
1514 * @param len number of first 'len' characters
1516 * @return integer value like to strcmp.
1520 vfs_path_ncmp (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, size_t len
)
1526 if (vpath1
== NULL
|| vpath2
== NULL
)
1529 path1
= vfs_path_to_str (vpath1
);
1530 path2
= vfs_path_to_str (vpath2
);
1532 ret_val
= strncmp (path1
, path2
, len
);
1540 /* --------------------------------------------------------------------------------------------- */
1542 * Calculate path length in string representation
1544 * @param vpath path object
1546 * @return length of path
1550 vfs_path_len (const vfs_path_t
* vpath
)
1558 path
= vfs_path_to_str (vpath
);
1559 ret_val
= strlen (path
);
1564 /* --------------------------------------------------------------------------------------------- */
1566 * Convert relative vpath object to absolute
1568 * @param vpath path object
1570 * @return absolute path object
1574 vfs_path_to_absolute (const vfs_path_t
* vpath
)
1576 vfs_path_t
*absolute_vpath
;
1579 if (!vpath
->relative
)
1580 return vfs_path_clone (vpath
);
1582 path_str
= vfs_path_to_str (vpath
);
1583 absolute_vpath
= vfs_path_from_str (path_str
);
1585 return absolute_vpath
;
1588 /* --------------------------------------------------------------------------------------------- */