Changed interface of function mc_opendir()
[midnight-commander.git] / lib / vfs / path.c
blob3e9fd9d381d1a3b38fd945f76a05b91aadc6c1ea
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" /* concat_dir_and_file */
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 = concat_dir_and_file (curr_dir, path);
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 * Build URL parameters (such as user:pass@host:port) from one path element object
178 * @param element path element
180 * @return newly allocated string
183 static char *
184 vfs_path_build_url_params_str (vfs_path_element_t * element)
186 GString *buffer;
188 if (element == NULL)
189 return NULL;
191 buffer = g_string_new ("");
193 if (element->user != NULL)
194 g_string_append (buffer, element->user);
196 if (element->password != NULL)
198 g_string_append_c (buffer, ':');
199 g_string_append (buffer, element->password);
202 if (element->host != NULL)
204 if ((element->user != NULL) || (element->password != NULL))
205 g_string_append_c (buffer, '@');
206 if (element->ipv6)
207 g_string_append_c (buffer, '[');
208 g_string_append (buffer, element->host);
209 if (element->ipv6)
210 g_string_append_c (buffer, ']');
213 if ((element->port) != 0 && (element->host != NULL))
215 g_string_append_c (buffer, ':');
216 g_string_append_printf (buffer, "%d", element->port);
219 return g_string_free (buffer, FALSE);
222 /* --------------------------------------------------------------------------------------------- */
223 /** get encoding after last #enc: or NULL, if part does not contain #enc:
225 * @param path string
227 * @return newly allocated string.
230 static char *
231 vfs_get_encoding (const char *path)
233 char result[16];
234 char *work;
235 char *semi;
236 char *slash;
237 work = g_strdup (path);
239 /* try found #enc: */
240 semi = g_strrstr (work, VFS_ENCODING_PREFIX);
242 if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
244 semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
245 slash = strchr (semi, PATH_SEP);
246 if (slash != NULL)
247 slash[0] = '\0';
249 g_strlcpy (result, semi, sizeof (result));
250 g_free (work);
251 return g_strdup (result);
253 else
255 g_free (work);
256 return NULL;
260 /* --------------------------------------------------------------------------------------------- */
261 /** Extract the hostname and username from the path
263 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
265 * ftp://sunsite.unc.edu/pub/linux
266 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
267 * ftp://tsx-11.mit.edu:8192/
268 * ftp://joe@foo.edu:11321/private
269 * ftp://joe:password@foo.se
271 * @param path_element is an input string to be parsed
272 * @param path is an input string to be parsed
274 * @return g_malloc()ed url info.
275 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
276 * is not set, then the current login name is supplied.
277 * Return value is a g_malloc()ed structure with the pathname relative to the
278 * host.
281 static void
282 vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
284 char *pcopy;
285 const char *pend;
286 char *dir, *colon, *inner_colon, *at, *rest;
288 path_element->port = 0;
290 pcopy = g_strdup (path);
291 pend = pcopy + strlen (pcopy);
292 dir = pcopy;
294 /* search for any possible user */
295 at = strrchr (pcopy, '@');
297 /* We have a username */
298 if (at == NULL)
299 rest = pcopy;
300 else
302 *at = '\0';
303 inner_colon = strchr (pcopy, ':');
304 if (inner_colon != NULL)
306 *inner_colon = '\0';
307 inner_colon++;
308 path_element->password = g_strdup (inner_colon);
311 if (*pcopy != '\0')
312 path_element->user = g_strdup (pcopy);
314 if (pend == at + 1)
315 rest = at;
316 else
317 rest = at + 1;
320 /* Check if the host comes with a port spec, if so, chop it */
321 if (*rest != '[')
322 colon = strchr (rest, ':');
323 else
325 colon = strchr (++rest, ']');
326 if (colon != NULL)
328 colon[0] = '\0';
329 colon[1] = '\0';
330 colon++;
331 path_element->ipv6 = TRUE;
335 if (colon != NULL)
337 *colon = '\0';
338 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
340 if (path_element->port <= 0 || path_element->port >= 65536)
341 path_element->port = 0;
343 else
344 while (*(++colon) != '\0')
346 switch (*colon)
348 case 'C':
349 path_element->port = 1;
350 break;
351 case 'r':
352 path_element->port = 2;
353 break;
357 path_element->host = g_strdup (rest);
358 g_free (pcopy);
361 /* --------------------------------------------------------------------------------------------- */
363 * get VFS class for the given name
365 * @param class_name name of class
367 * @return pointer to class structure or NULL if class not found
370 static struct vfs_class *
371 vfs_get_class_by_name (const char *class_name)
373 guint i;
375 if (class_name == NULL)
376 return NULL;
378 for (i = 0; i < vfs__classes_list->len; i++)
380 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
381 if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
382 return vfs;
385 return NULL;
388 /* --------------------------------------------------------------------------------------------- */
390 * Check if path string contain URL-like elements
392 * @param path_str path
394 * @return TRUE if path is deprecated or FALSE otherwise
397 static gboolean
398 vfs_path_is_str_path_deprecated (const char *path_str)
400 return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
403 /* --------------------------------------------------------------------------------------------- */
404 /** Split path string to path elements by deprecated algorithm.
406 * @param path_str VFS-path
408 * @return pointer to newly created vfs_path_t object with filled path elements array.
411 static vfs_path_t *
412 vfs_path_from_str_deprecated_parser (char *path)
414 vfs_path_t *vpath;
415 vfs_path_element_t *element;
416 struct vfs_class *class;
417 const char *local, *op;
419 vpath = vfs_path_new ();
421 while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
423 char *url_params;
424 element = g_new0 (vfs_path_element_t, 1);
425 element->class = class;
426 if (local == NULL)
427 local = "";
428 element->path = vfs_translate_path_n (local);
430 element->encoding = vfs_get_encoding (local);
431 element->dir.converter =
432 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
434 url_params = strchr (op, ':'); /* skip VFS prefix */
435 if (url_params != NULL)
437 *url_params = '\0';
438 url_params++;
439 vfs_path_url_split (element, url_params);
442 if (*op != '\0')
443 element->vfs_prefix = g_strdup (op);
445 vpath->path = g_list_prepend (vpath->path, element);
447 if (path[0] != '\0')
449 element = g_new0 (vfs_path_element_t, 1);
450 element->class = g_ptr_array_index (vfs__classes_list, 0);
451 element->path = vfs_translate_path_n (path);
453 element->encoding = vfs_get_encoding (path);
454 element->dir.converter =
455 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
456 vpath->path = g_list_prepend (vpath->path, element);
459 return vpath;
462 /* --------------------------------------------------------------------------------------------- */
463 /** Split path string to path elements by URL algorithm.
465 * @param path_str VFS-path
467 * @return pointer to newly created vfs_path_t object with filled path elements array.
470 static vfs_path_t *
471 vfs_path_from_str_uri_parser (char *path)
473 vfs_path_t *vpath;
474 vfs_path_element_t *element;
476 char *url_delimiter;
478 vpath = vfs_path_new ();
480 while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
482 char *vfs_prefix_start;
483 char *real_vfs_prefix_start = url_delimiter;
484 char *slash_pointer;
485 struct vfs_s_subclass *sub = NULL;
487 while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
488 real_vfs_prefix_start--;
489 vfs_prefix_start = real_vfs_prefix_start;
491 if (*(vfs_prefix_start) == PATH_SEP)
492 vfs_prefix_start += 1;
494 *url_delimiter = '\0';
496 element = g_new0 (vfs_path_element_t, 1);
497 element->class = vfs_prefix_to_class (vfs_prefix_start);
498 element->vfs_prefix = g_strdup (vfs_prefix_start);
500 url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
501 sub = VFSDATA (element);
502 if (sub != NULL && sub->flags & VFS_S_REMOTE)
504 slash_pointer = strchr (url_delimiter, PATH_SEP);
505 if (slash_pointer == NULL)
507 element->path = g_strdup ("");
509 else
511 element->path = vfs_translate_path_n (slash_pointer + 1);
512 element->encoding = vfs_get_encoding (slash_pointer);
514 *slash_pointer = '\0';
516 vfs_path_url_split (element, url_delimiter);
518 else
520 element->path = vfs_translate_path_n (url_delimiter);
521 element->encoding = vfs_get_encoding (url_delimiter);
523 element->dir.converter =
524 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
525 vpath->path = g_list_prepend (vpath->path, element);
527 if (real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP)
528 *real_vfs_prefix_start = '\0';
529 else
530 *(real_vfs_prefix_start + 1) = '\0';
533 if (path[0] != '\0')
535 element = g_new0 (vfs_path_element_t, 1);
536 element->class = g_ptr_array_index (vfs__classes_list, 0);
537 element->path = vfs_translate_path_n (path);
538 element->encoding = vfs_get_encoding (path);
539 element->dir.converter =
540 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
541 vpath->path = g_list_prepend (vpath->path, element);
544 return vpath;
547 /* --------------------------------------------------------------------------------------------- */
548 /*** public functions ****************************************************************************/
549 /* --------------------------------------------------------------------------------------------- */
551 * Convert first elements_count elements from vfs_path_t to string representation.
553 * @param vpath pointer to vfs_path_t object
554 * @param elements_count count of first elements for convert
555 * @param flags flags for parser
557 * @return pointer to newly created string.
560 #define vfs_append_from_path(appendfrom) \
562 if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
563 && (buffer->str[buffer->len - 1] != PATH_SEP)) \
564 g_string_append_c (buffer, PATH_SEP); \
565 g_string_append (buffer, appendfrom); \
568 char *
569 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
571 int element_index;
572 GString *buffer;
573 GString *recode_buffer;
575 if (vpath == NULL)
576 return NULL;
578 if (elements_count > vfs_path_elements_count (vpath))
579 elements_count = vfs_path_elements_count (vpath);
581 if (elements_count < 0)
582 elements_count = vfs_path_elements_count (vpath) + elements_count;
584 buffer = g_string_new ("");
585 recode_buffer = g_string_new ("");
587 for (element_index = 0; element_index < elements_count; element_index++)
589 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
591 if (element->vfs_prefix != NULL)
593 char *url_str;
595 if (buffer->str[buffer->len - 1] != '/')
596 g_string_append_c (buffer, '/');
598 g_string_append (buffer, element->vfs_prefix);
599 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
601 url_str = vfs_path_build_url_params_str (element);
602 if (*url_str != '\0')
603 g_string_append (buffer, url_str);
605 g_free (url_str);
608 if (vfs_path_element_need_cleanup_converter (element))
610 if (buffer->str[buffer->len - 1] != PATH_SEP)
611 g_string_append (buffer, PATH_SEP_STR);
612 g_string_append (buffer, VFS_ENCODING_PREFIX);
613 g_string_append (buffer, element->encoding);
614 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
615 vfs_append_from_path (recode_buffer->str);
616 g_string_set_size (recode_buffer, 0);
618 else
620 vfs_append_from_path (element->path);
623 g_string_free (recode_buffer, TRUE);
624 return g_string_free (buffer, FALSE);
627 #undef vfs_append_from_path
629 /* --------------------------------------------------------------------------------------------- */
631 * Convert vfs_path_t to string representation.
633 * @param vpath pointer to vfs_path_t object
635 * @return pointer to newly created string.
638 char *
639 vfs_path_to_str (const vfs_path_t * vpath)
641 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
644 /* --------------------------------------------------------------------------------------------- */
646 * Split path string to path elements with flags for change parce process.
648 * @param path_str VFS-path
649 * @param flags flags for parser
651 * @return pointer to newly created vfs_path_t object with filled path elements array.
654 vfs_path_t *
655 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
657 vfs_path_t *vpath;
658 char *path;
660 if (path_str == NULL)
661 return NULL;
663 if ((flags & VPF_NO_CANON) == 0)
664 path = vfs_canon (path_str);
665 else
666 path = g_strdup (path_str);
668 if (path == NULL)
669 return NULL;
671 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
672 vpath = vfs_path_from_str_deprecated_parser (path);
673 else
674 vpath = vfs_path_from_str_uri_parser (path);
676 g_free (path);
678 return vpath;
681 /* --------------------------------------------------------------------------------------------- */
683 * Split path string to path elements.
685 * @param path_str VFS-path
687 * @return pointer to newly created vfs_path_t object with filled path elements array.
690 vfs_path_t *
691 vfs_path_from_str (const char *path_str)
693 return vfs_path_from_str_flags (path_str, VPF_NONE);
696 /* --------------------------------------------------------------------------------------------- */
698 * Create new vfs_path_t object.
700 * @return pointer to newly created vfs_path_t object.
703 vfs_path_t *
704 vfs_path_new (void)
706 vfs_path_t *vpath;
707 vpath = g_new0 (vfs_path_t, 1);
708 return vpath;
711 /* --------------------------------------------------------------------------------------------- */
713 * Get count of path elements.
715 * @param vpath pointer to vfs_path_t object
717 * @return count of path elements.
721 vfs_path_elements_count (const vfs_path_t * vpath)
723 return (vpath != NULL && vpath->path != NULL) ? g_list_length (vpath->path) : 0;
726 /* --------------------------------------------------------------------------------------------- */
728 * Get one path element by index.
730 * @param vpath pointer to vfs_path_t object
731 * @param element_index element index. May have negative value (in this case count was started at the end of list).
733 * @return path element.
736 vfs_path_element_t *
737 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
739 if (element_index < 0)
740 element_index += vfs_path_elements_count (vpath);
742 if (element_index < 0)
743 vfs_die ("vfs_path_get_by_index: incorrect index!");
745 return g_list_nth_data (vpath->path, element_index);
748 /* --------------------------------------------------------------------------------------------- */
750 * Clone one path element
752 * @param element pointer to vfs_path_element_t object
754 * @return Newly allocated path element
757 vfs_path_element_t *
758 vfs_path_element_clone (const vfs_path_element_t * element)
760 vfs_path_element_t *new_element = g_new0 (vfs_path_element_t, 1);
761 memcpy (new_element, element, sizeof (vfs_path_element_t));
763 new_element->user = g_strdup (element->user);
764 new_element->password = g_strdup (element->password);
765 new_element->host = g_strdup (element->host);
766 new_element->path = g_strdup (element->path);
767 new_element->encoding = g_strdup (element->encoding);
768 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
769 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
770 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
772 return new_element;
775 /* --------------------------------------------------------------------------------------------- */
777 * Free one path element.
779 * @param element pointer to vfs_path_element_t object
783 void
784 vfs_path_element_free (vfs_path_element_t * element)
786 if (element == NULL)
787 return;
789 g_free (element->user);
790 g_free (element->password);
791 g_free (element->host);
792 g_free (element->path);
793 g_free (element->encoding);
794 g_free (element->vfs_prefix);
796 if (vfs_path_element_need_cleanup_converter (element))
798 str_close_conv (element->dir.converter);
801 g_free (element);
804 /* --------------------------------------------------------------------------------------------- */
806 * Clone path
808 * @param vpath pointer to vfs_path_t object
810 * @return Newly allocated path object
813 vfs_path_t *
814 vfs_path_clone (const vfs_path_t * vpath)
816 vfs_path_t *new_vpath;
817 int vpath_element_index;
818 if (vpath == NULL)
819 return NULL;
821 new_vpath = vfs_path_new ();
822 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
823 vpath_element_index++)
825 new_vpath->path =
826 g_list_append (new_vpath->path,
827 vfs_path_element_clone (vfs_path_get_by_index
828 (vpath, vpath_element_index)));
831 return new_vpath;
834 /* --------------------------------------------------------------------------------------------- */
836 * Free vfs_path_t object.
838 * @param vpath pointer to vfs_path_t object
842 void
843 vfs_path_free (vfs_path_t * path)
845 if (path == NULL)
846 return;
847 g_list_foreach (path->path, (GFunc) vfs_path_element_free, NULL);
848 g_list_free (path->path);
849 g_free (path);
852 /* --------------------------------------------------------------------------------------------- */
854 * Remove one path element by index
856 * @param vpath pointer to vfs_path_t object
857 * @param element_index element index. May have negative value (in this case count was started at the end of list).
861 void
862 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
864 vfs_path_element_t *element;
866 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
867 return;
869 if (element_index < 0)
870 element_index = vfs_path_elements_count (vpath) + element_index;
872 element = g_list_nth_data (vpath->path, element_index);
873 vpath->path = g_list_remove (vpath->path, element);
874 vfs_path_element_free (element);
877 /* --------------------------------------------------------------------------------------------- */
878 /** Return VFS class for the given prefix */
880 struct vfs_class *
881 vfs_prefix_to_class (const char *prefix)
883 guint i;
885 /* Avoid first class (localfs) that would accept any prefix */
886 for (i = 1; i < vfs__classes_list->len; i++)
888 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
889 if (vfs->which != NULL)
891 if (vfs->which (vfs, prefix) == -1)
892 continue;
893 return vfs;
896 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
897 return vfs;
900 return NULL;
903 /* --------------------------------------------------------------------------------------------- */
905 * Check if need cleanup charset converter for vfs_path_element_t
907 * @param element part of path
909 * @return TRUE if need cleanup converter or FALSE otherwise
912 gboolean
913 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
915 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
918 /* --------------------------------------------------------------------------------------------- */
920 * Serialize vfs_path_t object to string
922 * @param vpath data for serialization
923 * @param error contain pointer to object for handle error code and message
925 * @return serialized vpath as newly allocated string
928 char *
929 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
931 mc_config_t *cpath = mc_config_init (NULL);
932 ssize_t element_index;
933 char *ret_value;
935 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
937 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
938 return NULL;
941 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
943 char *groupname = g_strdup_printf ("path-element-%zd", element_index);
944 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
946 /* convert one element to config group */
948 mc_config_set_string_raw (cpath, groupname, "path", element->path);
949 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
950 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
952 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
954 mc_config_set_string_raw (cpath, groupname, "user", element->user);
955 mc_config_set_string_raw (cpath, groupname, "password", element->password);
956 mc_config_set_string_raw (cpath, groupname, "host", element->host);
957 if (element->port != 0)
958 mc_config_set_int (cpath, groupname, "port", element->port);
960 g_free (groupname);
963 ret_value = mc_serialize_config (cpath, error);
964 mc_config_deinit (cpath);
965 return ret_value;
968 /* --------------------------------------------------------------------------------------------- */
970 * Deserialize string to vfs_path_t object
972 * @param data data for serialization
973 * @param error contain pointer to object for handle error code and message
975 * @return newly allocated vfs_path_t object
978 vfs_path_t *
979 vfs_path_deserialize (const char *data, GError ** error)
981 mc_config_t *cpath = mc_deserialize_config (data, error);
982 size_t element_index = 0;
983 vfs_path_t *vpath;
985 if (cpath == NULL)
986 return NULL;
988 vpath = vfs_path_new ();
990 while (TRUE)
992 vfs_path_element_t *element;
993 char *cfg_value;
994 char *groupname;
996 groupname = g_strdup_printf ("path-element-%zd", element_index);
997 if (!mc_config_has_group (cpath, groupname))
999 g_free (groupname);
1000 break;
1003 element = g_new0 (vfs_path_element_t, 1);
1005 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1006 element->class = vfs_get_class_by_name (cfg_value);
1007 if (element->class == NULL)
1009 g_free (element);
1010 vfs_path_free (vpath);
1011 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1012 g_free (cfg_value);
1013 mc_config_deinit (cpath);
1014 return NULL;
1016 g_free (cfg_value);
1018 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1019 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1020 element->dir.converter =
1021 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1023 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1025 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1026 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1027 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1028 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1030 vpath->path = g_list_append (vpath->path, element);
1032 g_free (groupname);
1033 element_index++;
1036 mc_config_deinit (cpath);
1037 if (vfs_path_elements_count (vpath) == 0)
1039 vfs_path_free (vpath);
1040 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1041 return NULL;
1044 return vpath;
1047 /* --------------------------------------------------------------------------------------------- */
1049 * Build vfs_path_t object from arguments.
1051 * return newly allocated vfs_path_t object
1054 vfs_path_t *
1055 vfs_path_build_filename (const char *first_element, ...)
1057 va_list args;
1058 char *str_path;
1059 vfs_path_t *vpath;
1061 if (first_element == NULL)
1062 return NULL;
1064 va_start (args, first_element);
1065 str_path = mc_build_filenamev (first_element, args);
1066 va_end (args);
1067 vpath = vfs_path_from_str (str_path);
1068 g_free (str_path);
1069 return vpath;
1072 /* --------------------------------------------------------------------------------------------- */
1074 vfs_path_t *
1075 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1077 va_list args;
1078 char *str_path, *result_str;
1079 vfs_path_t *ret_vpath;
1081 if (vpath == NULL || first_element == NULL)
1082 return NULL;
1084 va_start (args, first_element);
1085 str_path = mc_build_filenamev (first_element, args);
1086 va_end (args);
1088 result_str = vfs_path_to_str (vpath);
1089 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1090 g_free (result_str);
1091 g_free (str_path);
1093 return ret_vpath;
1097 /* --------------------------------------------------------------------------------------------- */