Ticket #2862: fix syncronization with filelist and tree panels
[midnight-commander.git] / lib / vfs / path.c
blob25696e1eaa359e424b6d657fc108a6b21afce4b4
1 /*
2 Virtual File System path handlers
4 Copyright (C) 2011
5 The Free Software Foundation, Inc.
7 Written by:
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/>.
26 /**
27 * \file
28 * \brief Source: Virtual File System: path handlers
29 * \author Slava Zanko
30 * \date 2011
34 #include <config.h>
36 #include "lib/global.h"
37 #include "lib/strutil.h"
38 #include "lib/util.h" /* mc_build_filename() */
39 #include "lib/serialize.h"
41 #include "vfs.h"
42 #include "utilvfs.h"
43 #include "xdirentry.h"
44 #include "path.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 /* --------------------------------------------------------------------------------------------- */
59 static gboolean
60 path_magic (const char *path)
62 struct stat buf;
64 return (stat (path, &buf) != 0);
67 /* --------------------------------------------------------------------------------------------- */
69 /**
70 * Splits path extracting vfs part.
72 * Splits path
73 * \verbatim /p1#op/inpath \endverbatim
74 * into
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,
83 size_t skip_count)
85 char *semi;
86 char *slash;
87 struct vfs_class *ret;
89 if (path == NULL)
90 vfs_die ("Cannot split NULL");
92 semi = strrstr_skip_count (path, "#", skip_count);
94 if ((semi == NULL) || (!path_magic (path)))
95 return NULL;
97 slash = strchr (semi, PATH_SEP);
98 *semi = '\0';
100 if (op != NULL)
101 *op = NULL;
103 if (inpath != NULL)
104 *inpath = NULL;
106 if (slash != NULL)
107 *slash = '\0';
109 ret = vfs_prefix_to_class (semi + 1);
110 if (ret != NULL)
112 if (op != NULL)
113 *op = semi + 1;
114 if (inpath != NULL)
115 *inpath = slash != NULL ? slash + 1 : NULL;
116 return ret;
119 if (slash != NULL)
120 *slash = PATH_SEP;
122 *semi = '#';
123 ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
124 return ret;
127 /* --------------------------------------------------------------------------------------------- */
129 * remove //, /./ and /../
131 * @return newly allocated string
134 static char *
135 vfs_canon (const char *path)
137 if (path == NULL)
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)
148 char *curr_dir;
150 g_free (local);
151 curr_dir = vfs_get_current_dir ();
152 local = mc_build_filename (curr_dir, path, NULL);
153 g_free (curr_dir);
156 result = vfs_canon (local);
157 g_free (local);
158 return result;
162 * So we have path of following form:
163 * /p1/p2#op/.././././p3#op/p4. Good luck.
166 char *result;
168 result = g_strdup (path);
169 canonicalize_pathname (result);
170 return result;
174 /* --------------------------------------------------------------------------------------------- */
176 #ifdef HAVE_CHARSET
177 /** get encoding after last #enc: or NULL, if part does not contain #enc:
179 * @param path string
181 * @return newly allocated string.
184 static char *
185 vfs_get_encoding (const char *path)
187 char result[16];
188 char *work;
189 char *semi;
190 char *slash;
191 work = g_strdup (path);
193 /* try found #enc: */
194 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
196 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
198 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
199 slash = strchr (semi, PATH_SEP);
200 if (slash != NULL)
201 slash[0] = '\0';
203 g_strlcpy (result, semi, sizeof (result));
204 g_free (work);
205 return g_strdup (result);
207 else
209 g_free (work);
210 return NULL;
213 #endif
215 /* --------------------------------------------------------------------------------------------- */
216 /** Extract the hostname and username from the path
218 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
220 * ftp://sunsite.unc.edu/pub/linux
221 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
222 * ftp://tsx-11.mit.edu:8192/
223 * ftp://joe@foo.edu:11321/private
224 * ftp://joe:password@foo.se
226 * @param path_element is an input string to be parsed
227 * @param path is an input string to be parsed
229 * @return g_malloc()ed url info.
230 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
231 * is not set, then the current login name is supplied.
232 * Return value is a g_malloc()ed structure with the pathname relative to the
233 * host.
236 static void
237 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
239 char *pcopy;
240 const char *pend;
241 char *dir, *colon, *inner_colon, *at, *rest;
243 path_element->port = 0;
245 pcopy = g_strdup (path);
246 pend = pcopy + strlen (pcopy);
247 dir = pcopy;
249 /* search for any possible user */
250 at = strrchr (pcopy, '@');
252 /* We have a username */
253 if (at == NULL)
254 rest = pcopy;
255 else
257 *at = '\0';
258 inner_colon = strchr (pcopy, ':');
259 if (inner_colon != NULL)
261 *inner_colon = '\0';
262 inner_colon++;
263 path_element->password = g_strdup (inner_colon);
266 if (*pcopy != '\0')
267 path_element->user = g_strdup (pcopy);
269 if (pend == at + 1)
270 rest = at;
271 else
272 rest = at + 1;
275 /* Check if the host comes with a port spec, if so, chop it */
276 if (*rest != '[')
277 colon = strchr (rest, ':');
278 else
280 colon = strchr (++rest, ']');
281 if (colon != NULL)
283 colon[0] = '\0';
284 colon[1] = '\0';
285 colon++;
286 path_element->ipv6 = TRUE;
290 if (colon != NULL)
292 *colon = '\0';
293 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
295 if (path_element->port <= 0 || path_element->port >= 65536)
296 path_element->port = 0;
298 else
299 while (*(++colon) != '\0')
301 switch (*colon)
303 case 'C':
304 path_element->port = 1;
305 break;
306 case 'r':
307 path_element->port = 2;
308 break;
312 path_element->host = g_strdup (rest);
313 g_free (pcopy);
316 /* --------------------------------------------------------------------------------------------- */
318 * get VFS class for the given name
320 * @param class_name name of class
322 * @return pointer to class structure or NULL if class not found
325 static struct vfs_class *
326 vfs_get_class_by_name (const char *class_name)
328 guint i;
330 if (class_name == NULL)
331 return NULL;
333 for (i = 0; i < vfs__classes_list->len; i++)
335 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
336 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
337 return vfs;
340 return NULL;
343 /* --------------------------------------------------------------------------------------------- */
345 * Check if path string contain URL-like elements
347 * @param path_str path
349 * @return TRUE if path is deprecated or FALSE otherwise
352 static gboolean
353 vfs_path_is_str_path_deprecated (const char *path_str)
355 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
358 /* --------------------------------------------------------------------------------------------- */
359 /** Split path string to path elements by deprecated algorithm.
361 * @param path_str VFS-path
363 * @return pointer to newly created vfs_path_t object with filled path elements array.
366 static vfs_path_t *
367 vfs_path_from_str_deprecated_parser (char *path, vfs_path_flag_t flags)
369 vfs_path_t *vpath;
370 vfs_path_element_t *element;
371 struct vfs_class *class;
372 const char *local, *op;
374 (void) flags;
375 vpath = vfs_path_new ();
377 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
379 char *url_params;
380 element = g_new0 (vfs_path_element_t, 1);
381 element->class = class;
382 if (local == NULL)
383 local = "";
384 element->path = vfs_translate_path_n (local);
386 #ifdef HAVE_CHARSET
387 element->encoding = vfs_get_encoding (local);
388 element->dir.converter =
389 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
390 #endif
392 url_params = strchr (op, ':'); /* skip VFS prefix */
393 if (url_params != NULL)
395 *url_params = '\0';
396 url_params++;
397 vfs_path_url_split (element, url_params);
400 if (*op != '\0')
401 element->vfs_prefix = g_strdup (op);
403 g_array_prepend_val (vpath->path, element);
405 if (path[0] != '\0')
407 element = g_new0 (vfs_path_element_t, 1);
408 element->class = g_ptr_array_index (vfs__classes_list, 0);
409 element->path = vfs_translate_path_n (path);
411 #ifdef HAVE_CHARSET
412 element->encoding = vfs_get_encoding (path);
413 element->dir.converter =
414 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
415 #endif
416 g_array_prepend_val (vpath->path, element);
419 return vpath;
422 /* --------------------------------------------------------------------------------------------- */
423 /** Split path string to path elements by URL algorithm.
425 * @param path_str VFS-path
427 * @return pointer to newly created vfs_path_t object with filled path elements array.
430 static vfs_path_t *
431 vfs_path_from_str_uri_parser (char *path, vfs_path_flag_t flags)
433 vfs_path_t *vpath;
434 vfs_path_element_t *element;
436 char *url_delimiter;
438 vpath = vfs_path_new ();
439 vpath->relative = (flags & VPF_NO_CANON) != 0;
441 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
443 char *vfs_prefix_start;
444 char *real_vfs_prefix_start = url_delimiter;
445 char *slash_pointer;
446 struct vfs_s_subclass *sub = NULL;
448 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
449 real_vfs_prefix_start--;
450 vfs_prefix_start = real_vfs_prefix_start;
452 if (*(vfs_prefix_start) == PATH_SEP)
453 vfs_prefix_start += 1;
455 *url_delimiter = '\0';
457 element = g_new0 (vfs_path_element_t, 1);
458 element->class = vfs_prefix_to_class (vfs_prefix_start);
459 element->vfs_prefix = g_strdup (vfs_prefix_start);
461 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
462 sub = VFSDATA (element);
463 if (sub != NULL && (sub->flags & VFS_S_REMOTE) != 0)
465 slash_pointer = strchr (url_delimiter, PATH_SEP);
466 if (slash_pointer == NULL)
468 element->path = g_strdup ("");
470 else
472 element->path = vfs_translate_path_n (slash_pointer + 1);
473 #ifdef HAVE_CHARSET
474 element->encoding = vfs_get_encoding (slash_pointer);
475 #endif
476 *slash_pointer = '\0';
478 vfs_path_url_split (element, url_delimiter);
480 else
482 element->path = vfs_translate_path_n (url_delimiter);
483 #ifdef HAVE_CHARSET
484 element->encoding = vfs_get_encoding (url_delimiter);
485 #endif
487 #ifdef HAVE_CHARSET
488 element->dir.converter =
489 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
490 #endif
491 g_array_prepend_val (vpath->path, element);
493 if ((real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP) ||
494 (real_vfs_prefix_start == path && *(real_vfs_prefix_start) != PATH_SEP))
495 *real_vfs_prefix_start = '\0';
496 else
497 *(real_vfs_prefix_start + 1) = '\0';
500 if (path[0] != '\0')
502 element = g_new0 (vfs_path_element_t, 1);
503 element->class = g_ptr_array_index (vfs__classes_list, 0);
504 element->path = vfs_translate_path_n (path);
505 #ifdef HAVE_CHARSET
506 element->encoding = vfs_get_encoding (path);
507 element->dir.converter =
508 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
509 #endif
510 g_array_prepend_val (vpath->path, element);
513 return vpath;
516 /* --------------------------------------------------------------------------------------------- */
518 * Add element's class info to result string (such as VFS name, host, encoding etc)
519 * This function used as helper only in vfs_path_tokens_get() function
521 * @param element current path element
522 * @param ret_tokens total tikens for return
523 * @param element_tokens accumulated element-only tokens
526 static void
527 vfs_path_tokens_add_class_info (const vfs_path_element_t * element, GString * ret_tokens,
528 GString * element_tokens)
530 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
531 && element_tokens->len > 0)
533 char *url_str;
535 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
536 g_string_append_c (ret_tokens, PATH_SEP);
538 g_string_append (ret_tokens, element->vfs_prefix);
539 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
541 url_str = vfs_path_build_url_params_str (element, TRUE);
542 if (*url_str != '\0')
544 g_string_append (ret_tokens, url_str);
545 g_string_append_c (ret_tokens, PATH_SEP);
548 g_free (url_str);
551 #ifdef HAVE_CHARSET
552 if (element->encoding != NULL)
554 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
555 g_string_append (ret_tokens, PATH_SEP_STR);
556 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
557 g_string_append (ret_tokens, element->encoding);
558 g_string_append (ret_tokens, PATH_SEP_STR);
560 #endif
562 g_string_append (ret_tokens, element_tokens->str);
565 /* --------------------------------------------------------------------------------------------- */
567 * Strip path to home dir.
568 * @param dir pointer to string contains full path
571 static char *
572 vfs_path_strip_home (const char *dir)
574 const char *home_dir = mc_config_get_home_dir ();
576 if (home_dir != NULL)
578 size_t len;
580 len = strlen (home_dir);
582 if (strncmp (dir, home_dir, len) == 0 && (dir[len] == PATH_SEP || dir[len] == '\0'))
583 return g_strdup_printf ("~%s", dir + len);
586 return g_strdup (dir);
589 /* --------------------------------------------------------------------------------------------- */
591 /* --------------------------------------------------------------------------------------------- */
592 /*** public functions ****************************************************************************/
593 /* --------------------------------------------------------------------------------------------- */
595 * Convert first elements_count elements from vfs_path_t to string representation with flags.
597 * @param vpath pointer to vfs_path_t object
598 * @param elements_count count of first elements for convert
599 * @param flags flags for converter
601 * @return pointer to newly created string.
604 #define vfs_append_from_path(appendfrom, is_relative) \
606 if ((flags & VPF_STRIP_HOME) && element_index == 0 && (element->class->flags & VFSF_LOCAL) != 0) \
608 char *stripped_home_str; \
609 stripped_home_str = vfs_path_strip_home (appendfrom); \
610 g_string_append (buffer, stripped_home_str); \
611 g_free (stripped_home_str); \
613 else \
615 if ((!is_relative) && (*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
616 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)) \
617 g_string_append_c (buffer, PATH_SEP); \
618 g_string_append (buffer, appendfrom); \
622 char *
623 vfs_path_to_str_flags (const vfs_path_t * vpath, int elements_count, vfs_path_flag_t flags)
625 int element_index;
626 GString *buffer;
627 GString *recode_buffer;
629 if (vpath == NULL)
630 return NULL;
632 if (elements_count == 0 || elements_count > vfs_path_elements_count (vpath))
633 elements_count = vfs_path_elements_count (vpath);
635 if (elements_count < 0)
636 elements_count = vfs_path_elements_count (vpath) + elements_count;
638 buffer = g_string_new ("");
639 recode_buffer = g_string_new ("");
641 for (element_index = 0; element_index < elements_count; element_index++)
643 const vfs_path_element_t *element;
644 gboolean is_relative = vpath->relative && (element_index == 0);
646 element = vfs_path_get_by_index (vpath, element_index);
647 if (element->vfs_prefix != NULL)
649 char *url_str;
650 if ((!is_relative) && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
651 g_string_append_c (buffer, PATH_SEP);
653 g_string_append (buffer, element->vfs_prefix);
654 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
656 url_str = vfs_path_build_url_params_str (element, !(flags & VPF_STRIP_PASSWORD));
658 if (*url_str != '\0')
660 g_string_append (buffer, url_str);
661 g_string_append_c (buffer, PATH_SEP);
664 g_free (url_str);
667 #ifdef HAVE_CHARSET
668 if ((flags & VPF_RECODE) == 0 && vfs_path_element_need_cleanup_converter (element))
670 if ((flags & VPF_HIDE_CHARSET) == 0)
672 if ((!is_relative)
673 && (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP))
674 g_string_append (buffer, PATH_SEP_STR);
675 g_string_append (buffer, VFS_ENCODING_PREFIX);
676 g_string_append (buffer, element->encoding);
678 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
679 vfs_append_from_path (recode_buffer->str, is_relative);
680 g_string_set_size (recode_buffer, 0);
682 else
683 #endif
685 vfs_append_from_path (element->path, is_relative);
688 g_string_free (recode_buffer, TRUE);
689 return g_string_free (buffer, FALSE);
692 #undef vfs_append_from_path
694 /* --------------------------------------------------------------------------------------------- */
696 * Convert first elements_count elements from vfs_path_t to string representation.
698 * @param vpath pointer to vfs_path_t object
699 * @param elements_count count of first elements for convert
701 * @return pointer to newly created string.
704 char *
705 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
707 return vfs_path_to_str_flags (vpath, elements_count, VPF_NONE);
710 /* --------------------------------------------------------------------------------------------- */
712 * Convert vfs_path_t to string representation.
714 * @param vpath pointer to vfs_path_t object
716 * @return pointer to newly created string.
719 char *
720 vfs_path_to_str (const vfs_path_t * vpath)
722 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
725 /* --------------------------------------------------------------------------------------------- */
727 * Split path string to path elements with flags for change parce process.
729 * @param path_str VFS-path
730 * @param flags flags for parser
732 * @return pointer to newly created vfs_path_t object with filled path elements array.
735 vfs_path_t *
736 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
738 vfs_path_t *vpath;
739 char *path;
741 if (path_str == NULL)
742 return NULL;
744 if ((flags & VPF_NO_CANON) == 0)
745 path = vfs_canon (path_str);
746 else
747 path = g_strdup (path_str);
749 if (path == NULL)
750 return NULL;
752 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
753 vpath = vfs_path_from_str_deprecated_parser (path, flags);
754 else
755 vpath = vfs_path_from_str_uri_parser (path, flags);
757 g_free (path);
759 return vpath;
762 /* --------------------------------------------------------------------------------------------- */
764 * Split path string to path elements.
766 * @param path_str VFS-path
768 * @return pointer to newly created vfs_path_t object with filled path elements array.
771 vfs_path_t *
772 vfs_path_from_str (const char *path_str)
774 return vfs_path_from_str_flags (path_str, VPF_NONE);
777 /* --------------------------------------------------------------------------------------------- */
779 * Create new vfs_path_t object.
781 * @return pointer to newly created vfs_path_t object.
784 vfs_path_t *
785 vfs_path_new (void)
787 vfs_path_t *vpath;
789 vpath = g_new0 (vfs_path_t, 1);
790 vpath->path = g_array_new (FALSE, TRUE, sizeof (vfs_path_element_t *));
792 return vpath;
795 /* --------------------------------------------------------------------------------------------- */
797 * Get count of path elements.
799 * @param vpath pointer to vfs_path_t object
801 * @return count of path elements.
805 vfs_path_elements_count (const vfs_path_t * vpath)
807 return (vpath != NULL && vpath->path != NULL) ? vpath->path->len : 0;
810 /* --------------------------------------------------------------------------------------------- */
812 * Add vfs_path_element_t object to end of list in vfs_path_t object
813 * @param vpath pointer to vfs_path_t object
814 * @param path_element pointer to vfs_path_element_t object
817 void
818 vfs_path_add_element (const vfs_path_t * vpath, const vfs_path_element_t * path_element)
820 g_array_append_val (vpath->path, path_element);
823 /* --------------------------------------------------------------------------------------------- */
825 * Get one path element by index.
827 * @param vpath pointer to vfs_path_t object
828 * @param element_index element index. May have negative value (in this case count was started at the end of list).
830 * @return path element.
833 const vfs_path_element_t *
834 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
836 if (vpath == NULL)
837 return NULL;
839 if (element_index < 0)
840 element_index += vfs_path_elements_count (vpath);
842 if (element_index < 0)
843 vfs_die ("vfs_path_get_by_index: incorrect index!");
845 return g_array_index (vpath->path, vfs_path_element_t *, element_index);
848 /* --------------------------------------------------------------------------------------------- */
850 * Clone one path element
852 * @param element pointer to vfs_path_element_t object
854 * @return Newly allocated path element
857 vfs_path_element_t *
858 vfs_path_element_clone (const vfs_path_element_t * element)
860 vfs_path_element_t *new_element = g_new (vfs_path_element_t, 1);
862 new_element->user = g_strdup (element->user);
863 new_element->password = g_strdup (element->password);
864 new_element->host = g_strdup (element->host);
865 new_element->ipv6 = element->ipv6;
866 new_element->port = element->port;
867 new_element->path = g_strdup (element->path);
868 new_element->class = element->class;
869 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
870 #ifdef HAVE_CHARSET
871 new_element->encoding = g_strdup (element->encoding);
872 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
873 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
874 else
875 new_element->dir.converter = element->dir.converter;
876 #endif
877 new_element->dir.info = element->dir.info;
879 return new_element;
882 /* --------------------------------------------------------------------------------------------- */
884 * Free one path element.
886 * @param element pointer to vfs_path_element_t object
890 void
891 vfs_path_element_free (vfs_path_element_t * element)
893 if (element == NULL)
894 return;
896 g_free (element->user);
897 g_free (element->password);
898 g_free (element->host);
899 g_free (element->path);
900 g_free (element->vfs_prefix);
902 #ifdef HAVE_CHARSET
903 g_free (element->encoding);
905 if (vfs_path_element_need_cleanup_converter (element))
906 str_close_conv (element->dir.converter);
907 #endif
909 g_free (element);
912 /* --------------------------------------------------------------------------------------------- */
914 * Clone path
916 * @param vpath pointer to vfs_path_t object
918 * @return Newly allocated path object
921 vfs_path_t *
922 vfs_path_clone (const vfs_path_t * vpath)
924 vfs_path_t *new_vpath;
925 int vpath_element_index;
927 if (vpath == NULL)
928 return NULL;
930 new_vpath = vfs_path_new ();
931 new_vpath->relative = vpath->relative;
933 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
934 vpath_element_index++)
936 vfs_path_element_t *path_element;
938 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, vpath_element_index));
939 g_array_append_val (new_vpath->path, path_element);
942 return new_vpath;
945 /* --------------------------------------------------------------------------------------------- */
947 * Free vfs_path_t object.
949 * @param vpath pointer to vfs_path_t object
953 void
954 vfs_path_free (vfs_path_t * vpath)
956 int vpath_element_index;
958 if (vpath == NULL)
959 return;
961 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
962 vpath_element_index++)
964 vfs_path_element_t *path_element;
966 path_element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, vpath_element_index);
967 vfs_path_element_free (path_element);
970 g_array_free (vpath->path, TRUE);
971 g_free (vpath);
974 /* --------------------------------------------------------------------------------------------- */
976 * Remove one path element by index
978 * @param vpath pointer to vfs_path_t object
979 * @param element_index element index. May have negative value (in this case count was started at the end of list).
983 void
984 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
986 vfs_path_element_t *element;
988 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
989 return;
991 if (element_index < 0)
992 element_index = vfs_path_elements_count (vpath) + element_index;
994 element = (vfs_path_element_t *) vfs_path_get_by_index (vpath, element_index);
995 vpath->path = g_array_remove_index (vpath->path, element_index);
996 vfs_path_element_free (element);
999 /* --------------------------------------------------------------------------------------------- */
1000 /** Return VFS class for the given prefix */
1002 struct vfs_class *
1003 vfs_prefix_to_class (const char *prefix)
1005 guint i;
1007 /* Avoid first class (localfs) that would accept any prefix */
1008 for (i = 1; i < vfs__classes_list->len; i++)
1010 struct vfs_class *vfs;
1012 vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
1013 if (vfs->which != NULL)
1015 if (vfs->which (vfs, prefix) == -1)
1016 continue;
1017 return vfs;
1020 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
1021 return vfs;
1024 return NULL;
1027 /* --------------------------------------------------------------------------------------------- */
1029 * Check if need cleanup charset converter for vfs_path_element_t
1031 * @param element part of path
1033 * @return TRUE if need cleanup converter or FALSE otherwise
1035 #ifdef HAVE_CHARSET
1036 gboolean
1037 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
1039 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
1041 #endif
1043 /* --------------------------------------------------------------------------------------------- */
1045 * Serialize vfs_path_t object to string
1047 * @param vpath data for serialization
1048 * @param error contain pointer to object for handle error code and message
1050 * @return serialized vpath as newly allocated string
1053 char *
1054 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
1056 mc_config_t *cpath;
1057 ssize_t element_index;
1058 char *ret_value;
1060 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
1062 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
1063 return NULL;
1067 cpath = mc_config_init (NULL, FALSE);
1069 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1071 char *groupname;
1072 const vfs_path_element_t *element;
1074 groupname = g_strdup_printf ("path-element-%zd", element_index);
1075 element = vfs_path_get_by_index (vpath, element_index);
1076 /* convert one element to config group */
1078 mc_config_set_string_raw (cpath, groupname, "path", element->path);
1079 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
1080 #ifdef HAVE_CHARSET
1081 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
1082 #endif
1083 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1085 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1086 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1087 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1088 if (element->port != 0)
1089 mc_config_set_int (cpath, groupname, "port", element->port);
1091 g_free (groupname);
1094 ret_value = mc_serialize_config (cpath, error);
1095 mc_config_deinit (cpath);
1096 return ret_value;
1099 /* --------------------------------------------------------------------------------------------- */
1101 * Deserialize string to vfs_path_t object
1103 * @param data data for serialization
1104 * @param error contain pointer to object for handle error code and message
1106 * @return newly allocated vfs_path_t object
1109 vfs_path_t *
1110 vfs_path_deserialize (const char *data, GError ** error)
1112 mc_config_t *cpath;
1113 size_t element_index = 0;
1114 vfs_path_t *vpath;
1116 cpath = mc_deserialize_config (data, error);
1117 if (cpath == NULL)
1118 return NULL;
1120 vpath = vfs_path_new ();
1122 while (TRUE)
1124 vfs_path_element_t *element;
1125 char *cfg_value;
1126 char *groupname;
1128 groupname = g_strdup_printf ("path-element-%zd", element_index);
1129 if (!mc_config_has_group (cpath, groupname))
1131 g_free (groupname);
1132 break;
1135 element = g_new0 (vfs_path_element_t, 1);
1137 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1138 element->class = vfs_get_class_by_name (cfg_value);
1139 if (element->class == NULL)
1141 g_free (element);
1142 vfs_path_free (vpath);
1143 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1144 g_free (cfg_value);
1145 mc_config_deinit (cpath);
1146 return NULL;
1148 g_free (cfg_value);
1150 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1152 #ifdef HAVE_CHARSET
1153 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1154 element->dir.converter =
1155 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1156 #endif
1158 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1160 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1161 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1162 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1163 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1165 vpath->path = g_array_append_val (vpath->path, element);
1167 g_free (groupname);
1168 element_index++;
1171 mc_config_deinit (cpath);
1172 if (vfs_path_elements_count (vpath) == 0)
1174 vfs_path_free (vpath);
1175 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1176 return NULL;
1179 return vpath;
1182 /* --------------------------------------------------------------------------------------------- */
1184 * Build vfs_path_t object from arguments.
1186 * @param ... path tokens, terminated by NULL
1188 * @return newly allocated vfs_path_t object
1191 vfs_path_t *
1192 vfs_path_build_filename (const char *first_element, ...)
1194 va_list args;
1195 char *str_path;
1196 vfs_path_t *vpath;
1198 if (first_element == NULL)
1199 return NULL;
1201 va_start (args, first_element);
1202 str_path = mc_build_filenamev (first_element, args);
1203 va_end (args);
1204 vpath = vfs_path_from_str (str_path);
1205 g_free (str_path);
1206 return vpath;
1209 /* --------------------------------------------------------------------------------------------- */
1211 * Append tokens to path object
1213 * @param vpath path object
1214 * @param ... NULL-terminated strings
1216 * @return newly allocated path object
1219 vfs_path_t *
1220 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1222 va_list args;
1223 char *str_path, *result_str;
1224 vfs_path_t *ret_vpath;
1226 if (vpath == NULL || first_element == NULL)
1227 return NULL;
1229 va_start (args, first_element);
1230 str_path = mc_build_filenamev (first_element, args);
1231 va_end (args);
1233 result_str = vfs_path_to_str (vpath);
1234 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1235 g_free (result_str);
1236 g_free (str_path);
1238 return ret_vpath;
1242 /* --------------------------------------------------------------------------------------------- */
1245 * Append vpath_t tokens to path object
1247 * @param ... NULL-terminated vpath objects
1249 * @return newly allocated path object
1252 vfs_path_t *
1253 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1255 va_list args;
1256 vfs_path_t *ret_vpath;
1257 const vfs_path_t *current_vpath = first_vpath;
1259 if (first_vpath == NULL)
1260 return NULL;
1262 ret_vpath = vfs_path_new ();
1264 va_start (args, first_vpath);
1267 int vindex;
1269 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1271 vfs_path_element_t *path_element;
1273 path_element = vfs_path_element_clone (vfs_path_get_by_index (current_vpath, vindex));
1274 g_array_append_val (ret_vpath->path, path_element);
1276 current_vpath = va_arg (args, const vfs_path_t *);
1278 while (current_vpath != NULL);
1279 va_end (args);
1281 return ret_vpath;
1284 /* --------------------------------------------------------------------------------------------- */
1286 * get tockens count in path.
1288 * @param vpath path object
1290 * @return count of tokens
1293 size_t
1294 vfs_path_tokens_count (const vfs_path_t * vpath)
1296 size_t count_tokens = 0;
1297 int element_index;
1299 if (vpath == NULL)
1300 return 0;
1302 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1304 const vfs_path_element_t *element;
1305 char **path_tokens, **iterator;
1307 element = vfs_path_get_by_index (vpath, element_index);
1308 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1310 while (*iterator != NULL)
1312 if (**iterator != '\0')
1313 count_tokens++;
1314 iterator++;
1316 g_strfreev (path_tokens);
1318 return count_tokens;
1321 /* --------------------------------------------------------------------------------------------- */
1323 * Get subpath by tokens
1325 * @param vpath path object
1326 * @param start_position first token for got/ Started from 0.
1327 * If negative, then position will be relative to end of path
1328 * @param length count of tokens
1330 * @return newly allocated string with path tokens separated by slash
1333 char *
1334 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1336 GString *ret_tokens, *element_tokens;
1337 int element_index;
1338 size_t tokens_count = vfs_path_tokens_count (vpath);
1340 if (vpath == NULL)
1341 return NULL;
1343 if (length == 0)
1344 length = tokens_count;
1346 if (length < 0)
1347 length = tokens_count + length;
1349 if (start_position < 0)
1350 start_position = (ssize_t) tokens_count + start_position;
1352 if (start_position < 0)
1353 return NULL;
1355 if (start_position >= (ssize_t) tokens_count)
1356 return NULL;
1358 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1359 length = tokens_count - start_position;
1361 ret_tokens = g_string_sized_new (32);
1362 element_tokens = g_string_sized_new (32);
1364 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1366 const vfs_path_element_t *element;
1367 char **path_tokens, **iterator;
1369 g_string_assign (element_tokens, "");
1370 element = vfs_path_get_by_index (vpath, element_index);
1371 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1373 while (*iterator != NULL)
1375 if (**iterator != '\0')
1377 if (start_position == 0)
1379 if (length == 0)
1381 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1382 g_string_free (element_tokens, TRUE);
1383 g_strfreev (path_tokens);
1384 return g_string_free (ret_tokens, FALSE);
1386 length--;
1387 if (element_tokens->len != 0)
1388 g_string_append_c (element_tokens, PATH_SEP);
1389 g_string_append (element_tokens, *iterator);
1391 else
1392 start_position--;
1394 iterator++;
1396 g_strfreev (path_tokens);
1397 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1400 g_string_free (element_tokens, TRUE);
1401 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1404 /* --------------------------------------------------------------------------------------------- */
1406 * Get subpath by tokens
1408 * @param vpath path object
1409 * @param start_position first token for got/ Started from 0.
1410 * If negative, then position will be relative to end of path
1411 * @param length count of tokens
1413 * @return newly allocated path object with path tokens separated by slash
1416 vfs_path_t *
1417 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, ssize_t length)
1419 char *str_tokens;
1420 vfs_path_t *ret_vpath = NULL;
1422 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1423 if (str_tokens != NULL)
1425 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1426 g_free (str_tokens);
1428 return ret_vpath;
1431 /* --------------------------------------------------------------------------------------------- */
1433 * Build URL parameters (such as user:pass@host:port) from one path element object
1435 * @param element path element
1437 * @return newly allocated string
1440 char *
1441 vfs_path_build_url_params_str (const vfs_path_element_t * element, gboolean keep_password)
1443 GString *buffer;
1445 if (element == NULL)
1446 return NULL;
1448 buffer = g_string_new ("");
1450 if (element->user != NULL)
1451 g_string_append (buffer, element->user);
1453 if (element->password != NULL && keep_password)
1455 g_string_append_c (buffer, ':');
1456 g_string_append (buffer, element->password);
1459 if (element->host != NULL)
1461 if ((element->user != NULL) || (element->password != NULL))
1462 g_string_append_c (buffer, '@');
1463 if (element->ipv6)
1464 g_string_append_c (buffer, '[');
1465 g_string_append (buffer, element->host);
1466 if (element->ipv6)
1467 g_string_append_c (buffer, ']');
1470 if ((element->port) != 0 && (element->host != NULL))
1472 g_string_append_c (buffer, ':');
1473 g_string_append_printf (buffer, "%d", element->port);
1476 return g_string_free (buffer, FALSE);
1479 /* --------------------------------------------------------------------------------------------- */
1481 * Build pretty string representation of one path_element_t object
1483 * @param element path element
1485 * @return newly allocated string
1488 char *
1489 vfs_path_element_build_pretty_path_str (const vfs_path_element_t * element)
1491 char *url_params;
1492 GString *pretty_path;
1494 pretty_path = g_string_new (element->class->prefix);
1495 g_string_append (pretty_path, VFS_PATH_URL_DELIMITER);
1497 url_params = vfs_path_build_url_params_str (element, FALSE);
1498 g_string_append (pretty_path, url_params);
1499 g_free (url_params);
1501 if (*element->path != PATH_SEP)
1502 g_string_append_c (pretty_path, PATH_SEP);
1504 g_string_append (pretty_path, element->path);
1505 return g_string_free (pretty_path, FALSE);
1508 /* --------------------------------------------------------------------------------------------- */
1510 * Compare two path objects as strings
1512 * @param vpath1 first path object
1513 * @param vpath2 second vpath object
1515 * @return integer value like to strcmp.
1519 vfs_path_cmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
1521 char *path1;
1522 char *path2;
1523 int ret_val;
1525 if (vpath1 == NULL || vpath2 == NULL)
1526 return -1;
1528 path1 = vfs_path_to_str (vpath1);
1529 path2 = vfs_path_to_str (vpath2);
1531 ret_val = strcmp (path1, path2);
1533 g_free (path1);
1534 g_free (path2);
1536 return ret_val;
1539 /* --------------------------------------------------------------------------------------------- */
1541 * Compare two path objects as strings
1543 * @param vpath1 first path object
1544 * @param vpath2 second vpath object
1545 * @param len number of first 'len' characters
1547 * @return integer value like to strcmp.
1551 vfs_path_ncmp (const vfs_path_t * vpath1, const vfs_path_t * vpath2, size_t len)
1553 char *path1;
1554 char *path2;
1555 int ret_val;
1557 if (vpath1 == NULL || vpath2 == NULL)
1558 return -1;
1560 path1 = vfs_path_to_str (vpath1);
1561 path2 = vfs_path_to_str (vpath2);
1563 ret_val = strncmp (path1, path2, len);
1565 g_free (path1);
1566 g_free (path2);
1568 return ret_val;
1571 /* --------------------------------------------------------------------------------------------- */
1573 * Calculate path length in string representation
1575 * @param vpath path object
1577 * @return length of path
1580 size_t
1581 vfs_path_len (const vfs_path_t * vpath)
1583 char *path;
1584 size_t ret_val;
1586 if (vpath == NULL)
1587 return 0;
1589 path = vfs_path_to_str (vpath);
1590 ret_val = strlen (path);
1591 g_free (path);
1592 return ret_val;
1595 /* --------------------------------------------------------------------------------------------- */
1597 * Convert relative vpath object to absolute
1599 * @param vpath path object
1601 * @return absolute path object
1604 vfs_path_t *
1605 vfs_path_to_absolute (const vfs_path_t * vpath)
1607 vfs_path_t *absolute_vpath;
1608 char *path_str;
1610 if (!vpath->relative)
1611 return vfs_path_clone (vpath);
1613 path_str = vfs_path_to_str (vpath);
1614 absolute_vpath = vfs_path_from_str (path_str);
1615 g_free (path_str);
1616 return absolute_vpath;
1619 /* --------------------------------------------------------------------------------------------- */