1 /*
2    Virtual File System path handlers
3 
4    Copyright (C) 2011-2021
5    Free Software Foundation, Inc.
6 
7    Written by:
8    Slava Zanko <slavazanko@gmail.com>, 2011, 2013
9    Andrew Borodin <aborodin@vmail.ru>, 2013
10 
11    This file is part of the Midnight Commander.
12 
13    The Midnight Commander is free software: you can redistribute it
14    and/or modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation, either version 3 of the License,
16    or (at your option) any later version.
17 
18    The Midnight Commander is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21    GNU General Public License for more details.
22 
23    You should have received a copy of the GNU General Public License
24    along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 /**
28  * \file
29  * \brief Source: Virtual File System: path handlers
30  * \author Slava Zanko
31  * \date 2011
32  */
33 
34 
35 #include <config.h>
36 
37 #include "lib/global.h"
38 #include "lib/strutil.h"
39 #include "lib/util.h"           /* mc_build_filename() */
40 #include "lib/serialize.h"
41 
42 #include "vfs.h"
43 #include "utilvfs.h"
44 #include "xdirentry.h"
45 #include "path.h"
46 
47 extern GPtrArray *vfs__classes_list;
48 
49 /*** global variables ****************************************************************************/
50 
51 /*** file scope macro definitions ****************************************************************/
52 
53 /*** file scope type declarations ****************************************************************/
54 
55 /*** file scope variables ************************************************************************/
56 
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
59 
60 static gboolean
path_magic(const char * path)61 path_magic (const char *path)
62 {
63     struct stat buf;
64 
65     return (stat (path, &buf) != 0);
66 }
67 
68 /* --------------------------------------------------------------------------------------------- */
69 
70 /**
71  * Splits path extracting vfs part.
72  *
73  * Splits path
74  * \verbatim /p1#op/inpath \endverbatim
75  * into
76  * \verbatim inpath,op; \endverbatim
77  * returns which vfs it is.
78  * What is left in path is p1. You still want to g_free(path), you DON'T
79  * want to free neither *inpath nor *op
80  */
81 
82 static struct vfs_class *
_vfs_split_with_semi_skip_count(char * path,const char ** inpath,const char ** op,size_t skip_count)83 _vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
84                                  size_t skip_count)
85 {
86     char *semi;
87     char *slash;
88     struct vfs_class *ret;
89 
90     if (path == NULL)
91         vfs_die ("Cannot split NULL");
92 
93     semi = strrstr_skip_count (path, "#", skip_count);
94 
95     if ((semi == NULL) || (!path_magic (path)))
96         return NULL;
97 
98     slash = strchr (semi, PATH_SEP);
99     *semi = '\0';
100 
101     if (op != NULL)
102         *op = NULL;
103 
104     if (inpath != NULL)
105         *inpath = NULL;
106 
107     if (slash != NULL)
108         *slash = '\0';
109 
110     ret = vfs_prefix_to_class (semi + 1);
111     if (ret != NULL)
112     {
113         if (op != NULL)
114             *op = semi + 1;
115         if (inpath != NULL)
116             *inpath = slash != NULL ? slash + 1 : NULL;
117         return ret;
118     }
119 
120     if (slash != NULL)
121         *slash = PATH_SEP;
122 
123     *semi = '#';
124     ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
125     return ret;
126 }
127 
128 /* --------------------------------------------------------------------------------------------- */
129 /**
130  * remove //, /./ and /../
131  *
132  * @return newly allocated string
133  */
134 
135 static char *
vfs_canon(const char * path)136 vfs_canon (const char *path)
137 {
138     char *result;
139 
140     if (path == NULL)
141         vfs_die ("Cannot canonicalize NULL");
142 
143     if (!IS_PATH_SEP (*path))
144     {
145         /* Relative to current directory */
146 
147         char *local;
148 
149         if (g_str_has_prefix (path, VFS_ENCODING_PREFIX))
150         {
151             /*
152                encoding prefix placed at start of string without the leading slash
153                should be autofixed by adding the leading slash
154              */
155             local = mc_build_filename (PATH_SEP_STR, path, (char *) NULL);
156         }
157         else
158         {
159             const char *curr_dir;
160 
161             curr_dir = vfs_get_current_dir ();
162             local = mc_build_filename (curr_dir, path, (char *) NULL);
163         }
164         result = vfs_canon (local);
165         g_free (local);
166     }
167     else
168     {
169         /* Absolute path */
170 
171         result = g_strdup (path);
172         canonicalize_pathname (result);
173     }
174 
175     return result;
176 }
177 
178 /* --------------------------------------------------------------------------------------------- */
179 
180 #ifdef HAVE_CHARSET
181 /** get encoding after last #enc: or NULL, if part does not contain #enc:
182  *
183  * @param path null-terminated string
184  * @param len the maximum length of path, where #enc: should be searched
185  *
186  * @return newly allocated string.
187  */
188 
189 static char *
vfs_get_encoding(const char * path,ssize_t len)190 vfs_get_encoding (const char *path, ssize_t len)
191 {
192     char *semi;
193 
194     /* try found #enc: */
195     semi = g_strrstr_len (path, len, VFS_ENCODING_PREFIX);
196     if (semi == NULL)
197         return NULL;
198 
199     if (semi == path || IS_PATH_SEP (semi[-1]))
200     {
201         char *slash;
202 
203         semi += strlen (VFS_ENCODING_PREFIX);   /* skip "#enc:" */
204         slash = strchr (semi, PATH_SEP);
205         if (slash != NULL)
206             return g_strndup (semi, slash - semi);
207         return g_strdup (semi);
208     }
209 
210     return vfs_get_encoding (path, semi - path);
211 }
212 #endif
213 
214 /* --------------------------------------------------------------------------------------------- */
215 /**  Extract the hostname and username from the path
216  *
217  * Format of the path is [user@]hostname:port/remote-dir, e.g.:
218  *
219  * ftp://sunsite.unc.edu/pub/linux
220  * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
221  * ftp://tsx-11.mit.edu:8192/
222  * ftp://joe@foo.edu:11321/private
223  * ftp://joe:password@foo.se
224  *
225  * @param path_element is an input string to be parsed
226  * @param path is an input string to be parsed
227  *
228  * @return g_malloc()ed url info.
229  *         If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
230  *         is not set, then the current login name is supplied.
231  *         Return value is a g_malloc()ed structure with the pathname relative to the
232  *         host.
233  */
234 
235 static void
vfs_path_url_split(vfs_path_element_t * path_element,const char * path)236 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
237 {
238     char *pcopy;
239     char *colon, *at, *rest;
240 
241     path_element->port = 0;
242 
243     pcopy = g_strdup (path);
244 
245     /* search for any possible user */
246     at = strrchr (pcopy, '@');
247 
248     /* We have a username */
249     if (at == NULL)
250         rest = pcopy;
251     else
252     {
253         const char *pend;
254         char *inner_colon;
255 
256         pend = strchr (at, '\0');
257         *at = '\0';
258 
259         inner_colon = strchr (pcopy, ':');
260         if (inner_colon != NULL)
261         {
262             *inner_colon = '\0';
263             inner_colon++;
264             path_element->password = g_strdup (inner_colon);
265         }
266 
267         if (*pcopy != '\0')
268             path_element->user = g_strdup (pcopy);
269 
270         if (pend == at + 1)
271             rest = at;
272         else
273             rest = at + 1;
274     }
275 
276     /* Check if the host comes with a port spec, if so, chop it */
277     if (*rest != '[')
278         colon = strchr (rest, ':');
279     else
280     {
281         colon = strchr (++rest, ']');
282         if (colon != NULL)
283         {
284             *colon = '\0';
285             colon++;
286             *colon = '\0';
287             path_element->ipv6 = TRUE;
288         }
289     }
290 
291     if (colon != NULL)
292     {
293         *colon = '\0';
294         /* cppcheck-suppress invalidscanf */
295         if (sscanf (colon + 1, "%d", &path_element->port) == 1)
296         {
297             if (path_element->port <= 0 || path_element->port >= 65536)
298                 path_element->port = 0;
299         }
300         else
301             while (*(++colon) != '\0')
302             {
303                 switch (*colon)
304                 {
305                 case 'C':
306                     path_element->port = 1;
307                     break;
308                 case 'r':
309                     path_element->port = 2;
310                     break;
311                 default:
312                     break;
313                 }
314             }
315     }
316     path_element->host = g_strdup (rest);
317     g_free (pcopy);
318 }
319 
320 /* --------------------------------------------------------------------------------------------- */
321 /**
322  * get VFS class for the given name
323  *
324  * @param class_name name of class
325  *
326  * @return pointer to class structure or NULL if class not found
327  */
328 
329 static struct vfs_class *
vfs_get_class_by_name(const char * class_name)330 vfs_get_class_by_name (const char *class_name)
331 {
332     guint i;
333 
334     if (class_name == NULL)
335         return NULL;
336 
337     for (i = 0; i < vfs__classes_list->len; i++)
338     {
339         struct vfs_class *vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
340         if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
341             return vfs;
342     }
343 
344     return NULL;
345 }
346 
347 /* --------------------------------------------------------------------------------------------- */
348 /**
349  * Check if path string contain URL-like elements
350  *
351  * @param path_str path
352  *
353  * @return TRUE if path is deprecated or FALSE otherwise
354  */
355 
356 static gboolean
vfs_path_is_str_path_deprecated(const char * path_str)357 vfs_path_is_str_path_deprecated (const char *path_str)
358 {
359     return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
360 }
361 
362 /* --------------------------------------------------------------------------------------------- */
363 /** Split path string to path elements by deprecated algorithm.
364  *
365  * @param path_str VFS-path
366  *
367  * @return pointer to newly created vfs_path_t object with filled path elements array.
368 */
369 
370 static vfs_path_t *
vfs_path_from_str_deprecated_parser(char * path)371 vfs_path_from_str_deprecated_parser (char *path)
372 {
373     vfs_path_t *vpath;
374     vfs_path_element_t *element;
375     struct vfs_class *class;
376     const char *local, *op;
377 
378     vpath = vfs_path_new ();
379 
380     while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
381     {
382         char *url_params;
383         element = g_new0 (vfs_path_element_t, 1);
384         element->class = class;
385         if (local == NULL)
386             local = "";
387         element->path = vfs_translate_path_n (local);
388 
389 #ifdef HAVE_CHARSET
390         element->encoding = vfs_get_encoding (local, -1);
391         element->dir.converter =
392             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
393 #endif
394 
395         url_params = strchr (op, ':');  /* skip VFS prefix */
396         if (url_params != NULL)
397         {
398             *url_params = '\0';
399             url_params++;
400             vfs_path_url_split (element, url_params);
401         }
402 
403         if (*op != '\0')
404             element->vfs_prefix = g_strdup (op);
405 
406         g_array_prepend_val (vpath->path, element);
407     }
408     if (path[0] != '\0')
409     {
410         element = g_new0 (vfs_path_element_t, 1);
411         element->class = g_ptr_array_index (vfs__classes_list, 0);
412         element->path = vfs_translate_path_n (path);
413 
414 #ifdef HAVE_CHARSET
415         element->encoding = vfs_get_encoding (path, -1);
416         element->dir.converter =
417             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
418 #endif
419         g_array_prepend_val (vpath->path, element);
420     }
421 
422     return vpath;
423 }
424 
425 /* --------------------------------------------------------------------------------------------- */
426 /** Split path string to path elements by URL algorithm.
427  *
428  * @param path_str VFS-path
429  * @param flags    flags for converter
430  *
431  * @return pointer to newly created vfs_path_t object with filled path elements array.
432 */
433 
434 static vfs_path_t *
vfs_path_from_str_uri_parser(char * path)435 vfs_path_from_str_uri_parser (char *path)
436 {
437     vfs_path_t *vpath;
438     vfs_path_element_t *element;
439     char *url_delimiter;
440 
441     vpath = vfs_path_new ();
442     vpath->relative = path != NULL && !IS_PATH_SEP (*path);
443 
444     while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
445     {
446         char *vfs_prefix_start;
447         char *real_vfs_prefix_start = url_delimiter;
448 
449         while (real_vfs_prefix_start > path && !IS_PATH_SEP (*real_vfs_prefix_start))
450             real_vfs_prefix_start--;
451         vfs_prefix_start = real_vfs_prefix_start;
452 
453         if (IS_PATH_SEP (*vfs_prefix_start))
454             vfs_prefix_start += 1;
455 
456         *url_delimiter = '\0';
457 
458         element = g_new0 (vfs_path_element_t, 1);
459         element->class = vfs_prefix_to_class (vfs_prefix_start);
460         element->vfs_prefix = g_strdup (vfs_prefix_start);
461 
462         url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
463 
464         if (element->class != NULL && (element->class->flags & VFSF_REMOTE) != 0)
465         {
466             char *slash_pointer;
467 
468             slash_pointer = strchr (url_delimiter, PATH_SEP);
469             if (slash_pointer == NULL)
470             {
471                 element->path = g_strdup ("");
472             }
473             else
474             {
475                 element->path = vfs_translate_path_n (slash_pointer + 1);
476 #ifdef HAVE_CHARSET
477                 element->encoding = vfs_get_encoding (slash_pointer, -1);
478 #endif
479                 *slash_pointer = '\0';
480             }
481             vfs_path_url_split (element, url_delimiter);
482         }
483         else
484         {
485             element->path = vfs_translate_path_n (url_delimiter);
486 #ifdef HAVE_CHARSET
487             element->encoding = vfs_get_encoding (url_delimiter, -1);
488 #endif
489         }
490 #ifdef HAVE_CHARSET
491         element->dir.converter =
492             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
493 #endif
494         g_array_prepend_val (vpath->path, element);
495 
496         if ((real_vfs_prefix_start > path && IS_PATH_SEP (*real_vfs_prefix_start)) ||
497             (real_vfs_prefix_start == path && !IS_PATH_SEP (*real_vfs_prefix_start)))
498             *real_vfs_prefix_start = '\0';
499         else
500             *(real_vfs_prefix_start + 1) = '\0';
501     }
502 
503     if (path[0] != '\0')
504     {
505         element = g_new0 (vfs_path_element_t, 1);
506         element->class = g_ptr_array_index (vfs__classes_list, 0);
507         element->path = vfs_translate_path_n (path);
508 #ifdef HAVE_CHARSET
509         element->encoding = vfs_get_encoding (path, -1);
510         element->dir.converter =
511             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
512 #endif
513         g_array_prepend_val (vpath->path, element);
514     }
515 
516     return vpath;
517 }
518 
519 /* --------------------------------------------------------------------------------------------- */
520 /**
521  * Add element's class info to result string (such as VFS name, host, encoding etc)
522  * This function used as helper only in vfs_path_tokens_get() function
523  *
524  * @param element current path element
525  * @param ret_tokens total tikens for return
526  * @param element_tokens accumulated element-only tokens
527  */
528 
529 static void
vfs_path_tokens_add_class_info(const vfs_path_element_t * element,GString * ret_tokens,GString * element_tokens)530 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
531                                 GString * element_tokens)
532 {
533     if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
534         && element_tokens->len > 0)
535     {
536         char *url_str;
537 
538         if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
539             g_string_append_c (ret_tokens, PATH_SEP);
540 
541         g_string_append (ret_tokens, element->vfs_prefix);
542         g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
543 
544         url_str = vfs_path_build_url_params_str (element, TRUE);
545         if (*url_str != '\0')
546         {
547             g_string_append (ret_tokens, url_str);
548             g_string_append_c (ret_tokens, PATH_SEP);
549         }
550 
551         g_free (url_str);
552     }
553 
554 #ifdef HAVE_CHARSET
555     if (element->encoding != NULL)
556     {
557         if (ret_tokens->len > 0 && !IS_PATH_SEP (ret_tokens->str[ret_tokens->len - 1]))
558             g_string_append (ret_tokens, PATH_SEP_STR);
559         g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
560         g_string_append (ret_tokens, element->encoding);
561         g_string_append (ret_tokens, PATH_SEP_STR);
562     }
563 #endif
564 
565     g_string_append (ret_tokens, element_tokens->str);
566 }
567 
568 /* --------------------------------------------------------------------------------------------- */
569 /**
570  * Strip path to home dir.
571  * @param dir pointer to string contains full path
572  */
573 
574 static char *
vfs_path_strip_home(const char * dir)575 vfs_path_strip_home (const char *dir)
576 {
577     const char *home_dir = mc_config_get_home_dir ();
578 
579     if (home_dir != NULL)
580     {
581         size_t len;
582 
583         len = strlen (home_dir);
584 
585         if (strncmp (dir, home_dir, len) == 0 && (IS_PATH_SEP (dir[len]) || dir[len] == '\0'))
586             return g_strdup_printf ("~%s", dir + len);
587     }
588 
589     return g_strdup (dir);
590 }
591 
592 /* --------------------------------------------------------------------------------------------- */
593 /*** public functions ****************************************************************************/
594 /* --------------------------------------------------------------------------------------------- */
595 
596 #define vfs_append_from_path(appendfrom, is_relative) \
597 { \
598     if ((flags & VPF_STRIP_HOME) && element_index == 0 && \
599         (element->class->flags & VFSF_LOCAL) != 0) \
600     { \
601         char *stripped_home_str; \
602         stripped_home_str = vfs_path_strip_home (appendfrom); \
603         g_string_append (buffer, stripped_home_str); \
604         g_free (stripped_home_str); \
605     } \
606     else \
607     { \
608         if (!is_relative && !IS_PATH_SEP (*appendfrom) && *appendfrom != '\0' \
609             && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1]))) \
610             g_string_append_c (buffer, PATH_SEP); \
611         g_string_append (buffer, appendfrom); \
612     } \
613 }
614 
615 /**
616  * Convert first elements_count elements from vfs_path_t to string representation with flags.
617  *
618  * @param vpath pointer to vfs_path_t object
619  * @param elements_count count of first elements for convert
620  * @param flags for converter
621  *
622  * @return pointer to newly created string.
623  */
624 
625 char *
vfs_path_to_str_flags(const vfs_path_t * vpath,int elements_count,vfs_path_flag_t flags)626 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
627 {
628     int element_index;
629     GString *buffer;
630     GString *recode_buffer;
631 
632     if (vpath == NULL)
633         return NULL;
634 
635     if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
636         elements_count = vfs_path_elements_count (vpath);
637 
638     if (elements_count < 0)
639         elements_count = vfs_path_elements_count (vpath) + elements_count;
640 
641     buffer = g_string_new ("");
642     recode_buffer = g_string_new ("");
643 
644     for (element_index = 0; element_index < elements_count; element_index++)
645     {
646         const vfs_path_element_t *element;
647         gboolean is_relative = vpath->relative && (element_index == 0);
648 
649         element = vfs_path_get_by_index (vpath, element_index);
650         if (element->vfs_prefix != NULL)
651         {
652             char *url_str;
653             if (!is_relative && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
654                 g_string_append_c (buffer, PATH_SEP);
655 
656             g_string_append (buffer, element->vfs_prefix);
657             g_string_append (buffer, VFS_PATH_URL_DELIMITER);
658 
659             url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
660 
661             if (*url_str != '\0')
662             {
663                 g_string_append (buffer, url_str);
664                 g_string_append_c (buffer, PATH_SEP);
665             }
666 
667             g_free (url_str);
668         }
669 
670 #ifdef HAVE_CHARSET
671         if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
672         {
673             if ((flags & VPF_HIDE_CHARSET) == 0)
674             {
675                 if ((!is_relative)
676                     && (buffer->len == 0 || !IS_PATH_SEP (buffer->str[buffer->len - 1])))
677                     g_string_append (buffer, PATH_SEP_STR);
678                 g_string_append (buffer, VFS_ENCODING_PREFIX);
679                 g_string_append (buffer, element->encoding);
680             }
681             str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
682             vfs_append_from_path (recode_buffer->str, is_relative);
683             g_string_set_size (recode_buffer, 0);
684         }
685         else
686 #endif
687         {
688             vfs_append_from_path (element->path, is_relative);
689         }
690     }
691     g_string_free (recode_buffer, TRUE);
692     return g_string_free (buffer, FALSE);
693 }
694 
695 #undef vfs_append_from_path
696 
697 /* --------------------------------------------------------------------------------------------- */
698 /**
699  * Convert first elements_count elements from vfs_path_t to string representation.
700  *
701  * @param vpath pointer to vfs_path_t object
702  * @param elements_count count of first elements for convert
703  *
704  * @return pointer to newly created string.
705  */
706 
707 char *
vfs_path_to_str_elements_count(const vfs_path_t * vpath,int elements_count)708 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
709 {
710     return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
711 }
712 
713 /* --------------------------------------------------------------------------------------------- */
714 /**
715  * Split path string to path elements with flags for change parce process.
716  *
717  * @param path_str VFS-path
718  * @param flags flags for parser
719  *
720  * @return pointer to newly created vfs_path_t object with filled path elements array.
721  */
722 
723 vfs_path_t *
vfs_path_from_str_flags(const char * path_str,vfs_path_flag_t flags)724 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
725 {
726     vfs_path_t *vpath;
727     char *path;
728 
729     if (path_str == NULL)
730         return NULL;
731 
732     if ((flags & VPF_NO_CANON) == 0)
733         path = vfs_canon (path_str);
734     else
735         path = g_strdup (path_str);
736 
737     if (path == NULL)
738         return NULL;
739 
740     if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
741         vpath = vfs_path_from_str_deprecated_parser (path);
742     else
743         vpath = vfs_path_from_str_uri_parser (path);
744 
745     vpath->str = vfs_path_to_str_flags (vpath, 0, flags);
746     g_free (path);
747 
748     return vpath;
749 }
750 
751 /* --------------------------------------------------------------------------------------------- */
752 /**
753  * Split path string to path elements.
754  *
755  * @param path_str VFS-path
756  *
757  * @return pointer to newly created vfs_path_t object with filled path elements array.
758  */
759 
760 vfs_path_t *
vfs_path_from_str(const char * path_str)761 vfs_path_from_str (const char *path_str)
762 {
763     return vfs_path_from_str_flags (path_str, VPF_NONE);
764 }
765 
766 /* --------------------------------------------------------------------------------------------- */
767 /*
768  * Create new vfs_path_t object.
769  *
770  * @return pointer to newly created vfs_path_t object.
771  */
772 
773 vfs_path_t *
vfs_path_new(void)774 vfs_path_new (void)
775 {
776     vfs_path_t *vpath;
777 
778     vpath = g_new0 (vfs_path_t, 1);
779     vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
780 
781     return vpath;
782 }
783 
784 /* --------------------------------------------------------------------------------------------- */
785 /*
786  * Get count of path elements.
787  *
788  * @param vpath pointer to vfs_path_t object
789  *
790  * @return count of path elements.
791  */
792 
793 int
vfs_path_elements_count(const vfs_path_t * vpath)794 vfs_path_elements_count (const vfs_path_t * vpath)
795 {
796     return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
797 }
798 
799 /* --------------------------------------------------------------------------------------------- */
800 /**
801  * Add vfs_path_element_t object to end of list in vfs_path_t object
802  * @param vpath pointer to vfs_path_t object
803  * @param path_element pointer to vfs_path_element_t object
804  */
805 
806 void
vfs_path_add_element(vfs_path_t * vpath,const vfs_path_element_t * path_element)807 vfs_path_add_element (vfs_path_t * vpath, const vfs_path_element_t * path_element)
808 {
809     g_array_append_val (vpath->path, path_element);
810     g_free (vpath->str);
811     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
812 }
813 
814 /* --------------------------------------------------------------------------------------------- */
815 /*
816  * Get one path element by index.
817  *
818  * @param vpath pointer to vfs_path_t object
819  * @param element_index element index. May have negative value (in this case count was started at the end of list).
820  *
821  * @return path element.
822  */
823 
824 const vfs_path_element_t *
vfs_path_get_by_index(const vfs_path_t * vpath,int element_index)825 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
826 {
827     if (vpath == NULL)
828         return NULL;
829 
830     if (element_index < 0)
831         element_index += vfs_path_elements_count (vpath);
832 
833     if (element_index < 0)
834         vfs_die ("vfs_path_get_by_index: incorrect index!");
835 
836     return g_array_index (vpath->path, vfs_path_element_t *, element_index);
837 }
838 
839 /* --------------------------------------------------------------------------------------------- */
840 /*
841  * Clone one path element
842  *
843  * @param element pointer to vfs_path_element_t object
844  *
845  * @return Newly allocated path element
846  */
847 
848 vfs_path_element_t *
vfs_path_element_clone(const vfs_path_element_t * element)849 vfs_path_element_clone (const vfs_path_element_t * element)
850 {
851     vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
852 
853     new_element->user = g_strdup (element->user);
854     new_element->password = g_strdup (element->password);
855     new_element->host = g_strdup (element->host);
856     new_element->ipv6 = element->ipv6;
857     new_element->port = element->port;
858     new_element->path = g_strdup (element->path);
859     new_element->class = element->class;
860     new_element->vfs_prefix = g_strdup (element->vfs_prefix);
861 #ifdef HAVE_CHARSET
862     new_element->encoding = g_strdup (element->encoding);
863     if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
864         new_element->dir.converter = str_crt_conv_from (new_element->encoding);
865     else
866         new_element->dir.converter = element->dir.converter;
867 #endif
868     new_element->dir.info = element->dir.info;
869 
870     return new_element;
871 }
872 
873 /* --------------------------------------------------------------------------------------------- */
874 /*
875  * Free one path element.
876  *
877  * @param element pointer to vfs_path_element_t object
878  *
879  */
880 
881 void
vfs_path_element_free(vfs_path_element_t * element)882 vfs_path_element_free (vfs_path_element_t * element)
883 {
884     if (element == NULL)
885         return;
886 
887     g_free (element->user);
888     g_free (element->password);
889     g_free (element->host);
890     g_free (element->path);
891     g_free (element->vfs_prefix);
892 
893 #ifdef HAVE_CHARSET
894     g_free (element->encoding);
895 
896     if (vfs_path_element_need_cleanup_converter (element))
897         str_close_conv (element->dir.converter);
898 #endif
899 
900     g_free (element);
901 }
902 
903 /* --------------------------------------------------------------------------------------------- */
904 /*
905  * Clone path
906  *
907  * @param vpath pointer to vfs_path_t object
908  *
909  * @return Newly allocated path object
910  */
911 
912 vfs_path_t *
vfs_path_clone(const vfs_path_t * vpath)913 vfs_path_clone (const vfs_path_t * vpath)
914 {
915     vfs_path_t *new_vpath;
916     int vpath_element_index;
917 
918     if (vpath == NULL)
919         return NULL;
920 
921     new_vpath = vfs_path_new ();
922     new_vpath->relative = vpath->relative;
923 
924     for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
925          vpath_element_index++)
926     {
927         vfs_path_element_t *path_element;
928 
929         path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
930         g_array_append_val (new_vpath->path, path_element);
931     }
932     new_vpath->str = g_strdup (vpath->str);
933 
934     return new_vpath;
935 }
936 
937 /* --------------------------------------------------------------------------------------------- */
938 /*
939  * Free vfs_path_t object.
940  *
941  * @param vpath pointer to vfs_path_t object
942  * @param free_str if TRUE the string representation of vpath is freed as well
943  *
944  * @return the string representation of vpath (i.e. NULL if free_str is TRUE)
945  */
946 
947 char *
vfs_path_free(vfs_path_t * vpath,gboolean free_str)948 vfs_path_free (vfs_path_t * vpath, gboolean free_str)
949 {
950     int vpath_element_index;
951     char *ret;
952 
953     if (vpath == NULL)
954         return NULL;
955 
956     for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
957          vpath_element_index++)
958     {
959         vfs_path_element_t *path_element;
960 
961         path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
962         vfs_path_element_free (path_element);
963     }
964 
965     g_array_free (vpath->path, TRUE);
966 
967     if (!free_str)
968         ret = vpath->str;
969     else
970     {
971         g_free (vpath->str);
972         ret = NULL;
973     }
974 
975     g_free (vpath);
976 
977     return ret;
978 }
979 
980 /* --------------------------------------------------------------------------------------------- */
981 /*
982  * Remove one path element by index
983  *
984  * @param vpath pointer to vfs_path_t object
985  * @param element_index element index. May have negative value (in this case count was started at the end of list).
986  *
987  */
988 
989 void
vfs_path_remove_element_by_index(vfs_path_t * vpath,int element_index)990 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
991 {
992     vfs_path_element_t *element;
993 
994     if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
995         return;
996 
997     if (element_index < 0)
998         element_index = vfs_path_elements_count (vpath) + element_index;
999 
1000     element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
1001     vpath->path = g_array_remove_index (vpath->path, element_index);
1002     vfs_path_element_free (element);
1003     g_free (vpath->str);
1004     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1005 }
1006 
1007 /* --------------------------------------------------------------------------------------------- */
1008 /** Return VFS class for the given prefix */
1009 
1010 struct vfs_class *
vfs_prefix_to_class(const char * prefix)1011 vfs_prefix_to_class (const char *prefix)
1012 {
1013     guint i;
1014 
1015     /* Avoid first class (localfs) that would accept any prefix */
1016     for (i = 1; i < vfs__classes_list->len; i++)
1017     {
1018         struct vfs_class *vfs;
1019 
1020         vfs = VFS_CLASS (g_ptr_array_index (vfs__classes_list, i));
1021         if (vfs->which != NULL)
1022         {
1023             if (vfs->which (vfs, prefix) == -1)
1024                 continue;
1025             return vfs;
1026         }
1027 
1028         if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1029             return vfs;
1030     }
1031 
1032     return NULL;
1033 }
1034 
1035 /* --------------------------------------------------------------------------------------------- */
1036 
1037 #ifdef HAVE_CHARSET
1038 
1039 /**
1040  * Check if need cleanup charset converter for vfs_path_element_t
1041  *
1042  * @param element part of path
1043  *
1044  * @return TRUE if need cleanup converter or FALSE otherwise
1045  */
1046 
1047 gboolean
vfs_path_element_need_cleanup_converter(const vfs_path_element_t * element)1048 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1049 {
1050     return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1051 }
1052 
1053 /* --------------------------------------------------------------------------------------------- */
1054 /**
1055  * Change encoding for last part (vfs_path_element_t) of vpath
1056  *
1057  * @param vpath pointer to path structure
1058  * encoding name of charset
1059  *
1060  * @return pointer to path structure (for use function in anoter functions)
1061  */
1062 vfs_path_t *
vfs_path_change_encoding(vfs_path_t * vpath,const char * encoding)1063 vfs_path_change_encoding (vfs_path_t * vpath, const char *encoding)
1064 {
1065     vfs_path_element_t *path_element;
1066 
1067     path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, -1);
1068     /* don't add current encoding */
1069     if ((path_element->encoding != NULL) && (strcmp (encoding, path_element->encoding) == 0))
1070         return vpath;
1071 
1072     g_free (path_element->encoding);
1073     path_element->encoding = g_strdup (encoding);
1074 
1075     if (vfs_path_element_need_cleanup_converter (path_element))
1076         str_close_conv (path_element->dir.converter);
1077 
1078     path_element->dir.converter = str_crt_conv_from (path_element->encoding);
1079 
1080     g_free (vpath->str);
1081     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1082     return vpath;
1083 }
1084 
1085 #endif
1086 
1087 /* --------------------------------------------------------------------------------------------- */
1088 
1089 /**
1090  * Serialize vfs_path_t object to string
1091  *
1092  * @param vpath data for serialization
1093  * @param error contain pointer to object for handle error code and message
1094  *
1095  * @return serialized vpath as newly allocated string
1096  */
1097 
1098 char *
vfs_path_serialize(const vfs_path_t * vpath,GError ** mcerror)1099 vfs_path_serialize (const vfs_path_t * vpath, GError ** mcerror)
1100 {
1101     mc_config_t *cpath;
1102     ssize_t element_index;
1103     char *ret_value;
1104 
1105     mc_return_val_if_error (mcerror, FALSE);
1106 
1107     if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1108     {
1109         mc_propagate_error (mcerror, 0, "%s", "vpath object is empty");
1110         return NULL;
1111     }
1112 
1113     cpath = mc_config_init (NULL, FALSE);
1114 
1115     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1116     {
1117         char groupname[BUF_TINY];
1118         const vfs_path_element_t *element;
1119 
1120         g_snprintf (groupname, sizeof (groupname), "path-element-%zd", element_index);
1121         element = vfs_path_get_by_index (vpath, element_index);
1122         /* convert one element to config group */
1123 
1124         mc_config_set_string_raw (cpath, groupname, "path", element->path);
1125         mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1126 #ifdef HAVE_CHARSET
1127         mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1128 #endif
1129         mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1130 
1131         mc_config_set_string_raw (cpath, groupname, "user", element->user);
1132         mc_config_set_string_raw (cpath, groupname, "password", element->password);
1133         mc_config_set_string_raw (cpath, groupname, "host", element->host);
1134         if (element->port != 0)
1135             mc_config_set_int (cpath, groupname, "port", element->port);
1136     }
1137 
1138     ret_value = mc_serialize_config (cpath, mcerror);
1139     mc_config_deinit (cpath);
1140     return ret_value;
1141 }
1142 
1143 /* --------------------------------------------------------------------------------------------- */
1144 /**
1145  * Deserialize string to vfs_path_t object
1146  *
1147  * @param data data for serialization
1148  * @param error contain pointer to object for handle error code and message
1149  *
1150  * @return newly allocated vfs_path_t object
1151  */
1152 
1153 vfs_path_t *
vfs_path_deserialize(const char * data,GError ** mcerror)1154 vfs_path_deserialize (const char *data, GError ** mcerror)
1155 {
1156     mc_config_t *cpath;
1157     size_t element_index;
1158     vfs_path_t *vpath;
1159 
1160     mc_return_val_if_error (mcerror, FALSE);
1161 
1162     cpath = mc_deserialize_config (data, mcerror);
1163     if (cpath == NULL)
1164         return NULL;
1165 
1166     vpath = vfs_path_new ();
1167 
1168     for (element_index = 0;; element_index++)
1169     {
1170         struct vfs_class *eclass;
1171         vfs_path_element_t *element;
1172         char *cfg_value;
1173         char groupname[BUF_TINY];
1174 
1175         g_snprintf (groupname, sizeof (groupname), "path-element-%zu", element_index);
1176         if (!mc_config_has_group (cpath, groupname))
1177             break;
1178 
1179         cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1180         eclass = vfs_get_class_by_name (cfg_value);
1181         if (eclass == NULL)
1182         {
1183             vfs_path_free (vpath, TRUE);
1184             g_set_error (mcerror, MC_ERROR, 0, "Unable to find VFS class by name '%s'", cfg_value);
1185             g_free (cfg_value);
1186             mc_config_deinit (cpath);
1187             return NULL;
1188         }
1189         g_free (cfg_value);
1190 
1191         element = g_new0 (vfs_path_element_t, 1);
1192         element->class = eclass;
1193         element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1194 
1195 #ifdef HAVE_CHARSET
1196         element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1197         element->dir.converter =
1198             (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1199 #endif
1200 
1201         element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1202 
1203         element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1204         element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1205         element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1206         element->port = mc_config_get_int (cpath, groupname, "port", 0);
1207 
1208         vpath->path = g_array_append_val (vpath->path, element);
1209     }
1210 
1211     mc_config_deinit (cpath);
1212     if (vfs_path_elements_count (vpath) == 0)
1213     {
1214         vfs_path_free (vpath, TRUE);
1215         g_set_error (mcerror, MC_ERROR, 0, "No any path elements found");
1216         return NULL;
1217     }
1218     vpath->str = vfs_path_to_str_flags (vpath, 0, VPF_NONE);
1219 
1220     return vpath;
1221 }
1222 
1223 /* --------------------------------------------------------------------------------------------- */
1224 /**
1225  * Build vfs_path_t object from arguments.
1226  *
1227  * @param first_element of path
1228  * @param ... path tokens, terminated by NULL
1229  *
1230  * @return newly allocated vfs_path_t object
1231  */
1232 
1233 vfs_path_t *
vfs_path_build_filename(const char * first_element,...)1234 vfs_path_build_filename (const char *first_element, ...)
1235 {
1236     va_list args;
1237     char *str_path;
1238     vfs_path_t *vpath;
1239 
1240     if (first_element == NULL)
1241         return NULL;
1242 
1243     va_start (args, first_element);
1244     str_path = mc_build_filenamev (first_element, args);
1245     va_end (args);
1246     vpath = vfs_path_from_str (str_path);
1247     g_free (str_path);
1248     return vpath;
1249 }
1250 
1251 /* --------------------------------------------------------------------------------------------- */
1252 /**
1253  * Append tokens to path object
1254  *
1255  * @param vpath path object
1256  * @param first_element of path
1257  * @param ... NULL-terminated strings
1258  *
1259  * @return newly allocated path object
1260  */
1261 
1262 vfs_path_t *
vfs_path_append_new(const vfs_path_t * vpath,const char * first_element,...)1263 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1264 {
1265     va_list args;
1266     char *str_path;
1267     const char *result_str;
1268     vfs_path_t *ret_vpath;
1269 
1270     if (vpath == NULL || first_element == NULL)
1271         return NULL;
1272 
1273     va_start (args, first_element);
1274     str_path = mc_build_filenamev (first_element, args);
1275     va_end (args);
1276 
1277     result_str = vfs_path_as_str (vpath);
1278     ret_vpath = vfs_path_build_filename (result_str, str_path, (char *) NULL);
1279     g_free (str_path);
1280 
1281     return ret_vpath;
1282 
1283 }
1284 
1285 /* --------------------------------------------------------------------------------------------- */
1286 
1287 /**
1288  * Append vpath_t tokens to path object
1289  *
1290  * @param first_vpath vpath objects
1291  * @param ... NULL-terminated vpath objects
1292  *
1293  * @return newly allocated path object
1294  */
1295 
1296 vfs_path_t *
vfs_path_append_vpath_new(const vfs_path_t * first_vpath,...)1297 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1298 {
1299     va_list args;
1300     vfs_path_t *ret_vpath;
1301     const vfs_path_t *current_vpath = first_vpath;
1302 
1303     if (first_vpath == NULL)
1304         return NULL;
1305 
1306     ret_vpath = vfs_path_new ();
1307 
1308     va_start (args, first_vpath);
1309     do
1310     {
1311         int vindex;
1312 
1313         for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1314         {
1315             vfs_path_element_t *path_element;
1316 
1317             path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1318             g_array_append_val (ret_vpath->path, path_element);
1319         }
1320         current_vpath = va_arg (args, const vfs_path_t *);
1321     }
1322     while (current_vpath != NULL);
1323     va_end (args);
1324 
1325     ret_vpath->str = vfs_path_to_str_flags (ret_vpath, 0, VPF_NONE);
1326 
1327     return ret_vpath;
1328 }
1329 
1330 /* --------------------------------------------------------------------------------------------- */
1331 
1332 /**
1333  * get tockens count in path.
1334  *
1335  * @param vpath path object
1336  *
1337  * @return count of tokens
1338  */
1339 
1340 size_t
vfs_path_tokens_count(const vfs_path_t * vpath)1341 vfs_path_tokens_count (const vfs_path_t * vpath)
1342 {
1343     size_t count_tokens = 0;
1344     int element_index;
1345 
1346     if (vpath == NULL)
1347         return 0;
1348 
1349     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1350     {
1351         const vfs_path_element_t *element;
1352         const char *token, *prev_token;
1353 
1354         element = vfs_path_get_by_index (vpath, element_index);
1355 
1356         for (prev_token = element->path; (token = strchr (prev_token, PATH_SEP)) != NULL;
1357              prev_token = token + 1)
1358         {
1359             /* skip empty substring */
1360             if (token != prev_token)
1361                 count_tokens++;
1362         }
1363 
1364         if (*prev_token != '\0')
1365             count_tokens++;
1366     }
1367 
1368     return count_tokens;
1369 }
1370 
1371 /* --------------------------------------------------------------------------------------------- */
1372 
1373 /**
1374  * Get subpath by tokens
1375  *
1376  * @param vpath path object
1377  * @param start_position first token for got/ Started from 0.
1378  *        If negative, then position will be relative to end of path
1379  * @param length count of tokens
1380  *
1381  * @return newly allocated string with path tokens separated by slash
1382  */
1383 
1384 char *
vfs_path_tokens_get(const vfs_path_t * vpath,ssize_t start_position,ssize_t length)1385 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1386 {
1387     GString *ret_tokens, *element_tokens;
1388     int element_index;
1389     size_t tokens_count = vfs_path_tokens_count (vpath);
1390 
1391     if (vpath == NULL)
1392         return NULL;
1393 
1394     if (length == 0)
1395         length = tokens_count;
1396 
1397     if (length < 0)
1398         length = tokens_count + length;
1399 
1400     if (start_position < 0)
1401         start_position = (ssize_t) tokens_count + start_position;
1402 
1403     if (start_position < 0)
1404         return NULL;
1405 
1406     if (start_position >= (ssize_t) tokens_count)
1407         return NULL;
1408 
1409     if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1410         length = tokens_count - start_position;
1411 
1412     ret_tokens = g_string_sized_new (32);
1413     element_tokens = g_string_sized_new (32);
1414 
1415     for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1416     {
1417         const vfs_path_element_t *element;
1418         char **path_tokens, **iterator;
1419 
1420         g_string_assign (element_tokens, "");
1421         element = vfs_path_get_by_index (vpath, element_index);
1422         path_tokens = g_strsplit (element->path, PATH_SEP_STR, -1);
1423 
1424         for (iterator = path_tokens; *iterator != NULL; iterator++)
1425         {
1426             if (**iterator != '\0')
1427             {
1428                 if (start_position == 0)
1429                 {
1430                     if (length == 0)
1431                     {
1432                         vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1433                         g_string_free (element_tokens, TRUE);
1434                         g_strfreev (path_tokens);
1435                         return g_string_free (ret_tokens, FALSE);
1436                     }
1437                     length--;
1438                     if (element_tokens->len != 0)
1439                         g_string_append_c (element_tokens, PATH_SEP);
1440                     g_string_append (element_tokens, *iterator);
1441                 }
1442                 else
1443                     start_position--;
1444             }
1445         }
1446         g_strfreev (path_tokens);
1447         vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1448     }
1449 
1450     g_string_free (element_tokens, TRUE);
1451     return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1452 }
1453 
1454 /* --------------------------------------------------------------------------------------------- */
1455 /**
1456  * Get subpath by tokens
1457  *
1458  * @param vpath path object
1459  * @param start_position first token for got/ Started from 0.
1460  *        If negative, then position will be relative to end of path
1461  * @param length count of tokens
1462  *
1463  * @return newly allocated path object with path tokens separated by slash
1464  */
1465 
1466 vfs_path_t *
vfs_path_vtokens_get(const vfs_path_t * vpath,ssize_t start_position,ssize_t length)1467 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1468 {
1469     char *str_tokens;
1470     vfs_path_t *ret_vpath = NULL;
1471 
1472     str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1473     if (str_tokens != NULL)
1474     {
1475         ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1476         g_free (str_tokens);
1477     }
1478     return ret_vpath;
1479 }
1480 
1481 /* --------------------------------------------------------------------------------------------- */
1482 
1483 /**
1484  * Build URL parameters (such as user:pass @ host:port) from one path element object
1485  *
1486  * @param element path element
1487  * @param keep_password TRUE or FALSE
1488  *
1489  * @return newly allocated string
1490  */
1491 
1492 char *
vfs_path_build_url_params_str(const vfs_path_element_t * element,gboolean keep_password)1493 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1494 {
1495     GString *buffer;
1496 
1497     if (element == NULL)
1498         return NULL;
1499 
1500     buffer = g_string_new ("");
1501 
1502     if (element->user != NULL)
1503         g_string_append (buffer, element->user);
1504 
1505     if (element->password != NULL && keep_password)
1506     {
1507         g_string_append_c (buffer, ':');
1508         g_string_append (buffer, element->password);
1509     }
1510 
1511     if (element->host != NULL)
1512     {
1513         if ((element->user != NULL) || (element->password != NULL))
1514             g_string_append_c (buffer, '@');
1515         if (element->ipv6)
1516             g_string_append_c (buffer, '[');
1517         g_string_append (buffer, element->host);
1518         if (element->ipv6)
1519             g_string_append_c (buffer, ']');
1520     }
1521 
1522     if ((element->port) != 0 && (element->host != NULL))
1523     {
1524         g_string_append_c (buffer, ':');
1525         g_string_append_printf (buffer, "%d", element->port);
1526     }
1527 
1528     return g_string_free (buffer, FALSE);
1529 }
1530 
1531 /* --------------------------------------------------------------------------------------------- */
1532 /**
1533  * Build pretty string representation of one path_element_t object
1534  *
1535  * @param element path element
1536  *
1537  * @return newly allocated string
1538  */
1539 
1540 char *
vfs_path_element_build_pretty_path_str(const vfs_path_element_t * element)1541 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1542 {
1543     char *url_params;
1544     GString *pretty_path;
1545 
1546     pretty_path = g_string_new (element->class->prefix);
1547     g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1548 
1549     url_params = vfs_path_build_url_params_str (element, FALSE);
1550     g_string_append (pretty_path, url_params);
1551     g_free (url_params);
1552 
1553     if (!IS_PATH_SEP (*element->path))
1554         g_string_append_c (pretty_path, PATH_SEP);
1555 
1556     g_string_append (pretty_path, element->path);
1557     return g_string_free (pretty_path, FALSE);
1558 }
1559 
1560 /* --------------------------------------------------------------------------------------------- */
1561 /**
1562  * Compare two path objects as strings
1563  *
1564  * @param vpath1 first path object
1565  * @param vpath2 second vpath object
1566  *
1567  * @return integer value like to strcmp.
1568  */
1569 
1570 gboolean
vfs_path_equal(const vfs_path_t * vpath1,const vfs_path_t * vpath2)1571 vfs_path_equal (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1572 {
1573     const char *path1, *path2;
1574     gboolean ret_val;
1575 
1576     if (vpath1 == NULL || vpath2 == NULL)
1577         return FALSE;
1578 
1579     path1 = vfs_path_as_str (vpath1);
1580     path2 = vfs_path_as_str (vpath2);
1581 
1582     ret_val = strcmp (path1, path2) == 0;
1583 
1584     return ret_val;
1585 }
1586 
1587 /* --------------------------------------------------------------------------------------------- */
1588 /**
1589  * Compare two path objects as strings
1590  *
1591  * @param vpath1 first path object
1592  * @param vpath2 second vpath object
1593  * @param len number of first 'len' characters
1594  *
1595  * @return integer value like to strcmp.
1596  */
1597 
1598 gboolean
vfs_path_equal_len(const vfs_path_t * vpath1,const vfs_path_t * vpath2,size_t len)1599 vfs_path_equal_len (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1600 {
1601     const char *path1, *path2;
1602     gboolean ret_val;
1603 
1604     if (vpath1 == NULL || vpath2 == NULL)
1605         return FALSE;
1606 
1607     path1 = vfs_path_as_str (vpath1);
1608     path2 = vfs_path_as_str (vpath2);
1609 
1610     ret_val = strncmp (path1, path2, len) == 0;
1611 
1612     return ret_val;
1613 }
1614 
1615 /* --------------------------------------------------------------------------------------------- */
1616 /**
1617  * Calculate path length in string representation
1618  *
1619  * @param vpath path object
1620  *
1621  * @return length of path
1622  */
1623 
1624 size_t
vfs_path_len(const vfs_path_t * vpath)1625 vfs_path_len (const vfs_path_t * vpath)
1626 {
1627     if (vpath == NULL)
1628         return 0;
1629 
1630     return strlen (vpath->str);
1631 }
1632 
1633 /* --------------------------------------------------------------------------------------------- */
1634 /**
1635  * Convert relative vpath object to absolute
1636  *
1637  * @param vpath path object
1638  *
1639  * @return absolute path object
1640  */
1641 
1642 vfs_path_t *
vfs_path_to_absolute(const vfs_path_t * vpath)1643 vfs_path_to_absolute (const vfs_path_t * vpath)
1644 {
1645     vfs_path_t *absolute_vpath;
1646     const char *path_str;
1647 
1648     if (!vpath->relative)
1649         return vfs_path_clone (vpath);
1650 
1651     path_str = vfs_path_as_str (vpath);
1652     absolute_vpath = vfs_path_from_str (path_str);
1653     return absolute_vpath;
1654 }
1655 
1656 /* --------------------------------------------------------------------------------------------- */
1657