Added new fnction for manipulate vpath objects:
[midnight-commander.git] / lib / vfs / path.c
blobb9eab52c167eb5c1a0b82ba3fe2eb573a377a041
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 == path && *(real_vfs_prefix_start) != PATH_SEP))
529 *real_vfs_prefix_start = '\0';
530 else
531 *(real_vfs_prefix_start + 1) = '\0';
534 if (path[0] != '\0')
536 element = g_new0 (vfs_path_element_t, 1);
537 element->class = g_ptr_array_index (vfs__classes_list, 0);
538 element->path = vfs_translate_path_n (path);
539 element->encoding = vfs_get_encoding (path);
540 element->dir.converter =
541 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
542 vpath->path = g_list_prepend (vpath->path, element);
545 return vpath;
548 /* --------------------------------------------------------------------------------------------- */
550 * Add element's class info to result string (such as VFS name, host, encoding etc)
551 * This function used as helper only in vfs_path_tokens_get() function
553 * @param element current path element
554 * @param ret_tokens total tikens for return
555 * @param element_tokens accumulated element-only tokens
558 static void
559 vfs_path_tokens_add_class_info (vfs_path_element_t * element, GString * ret_tokens,
560 GString * element_tokens)
562 if (((element->class->flags & VFSF_LOCAL) == 0 || ret_tokens->len > 0)
563 && element_tokens->len > 0)
565 char *url_str;
567 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
568 g_string_append_c (ret_tokens, PATH_SEP);
570 g_string_append (ret_tokens, element->vfs_prefix);
571 g_string_append (ret_tokens, VFS_PATH_URL_DELIMITER);
573 url_str = vfs_path_build_url_params_str (element);
574 if (*url_str != '\0')
576 g_string_append (ret_tokens, url_str);
577 g_string_append_c (ret_tokens, PATH_SEP);
580 g_free (url_str);
582 if (element->encoding != NULL)
584 if (ret_tokens->len > 0 && ret_tokens->str[ret_tokens->len - 1] != PATH_SEP)
585 g_string_append (ret_tokens, PATH_SEP_STR);
586 g_string_append (ret_tokens, VFS_ENCODING_PREFIX);
587 g_string_append (ret_tokens, element->encoding);
588 g_string_append (ret_tokens, PATH_SEP_STR);
591 g_string_append (ret_tokens, element_tokens->str);
594 /* --------------------------------------------------------------------------------------------- */
595 /*** public functions ****************************************************************************/
596 /* --------------------------------------------------------------------------------------------- */
598 * Convert first elements_count elements from vfs_path_t to string representation.
600 * @param vpath pointer to vfs_path_t object
601 * @param elements_count count of first elements for convert
602 * @param flags flags for parser
604 * @return pointer to newly created string.
607 #define vfs_append_from_path(appendfrom) \
609 if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
610 && (buffer->str[buffer->len - 1] != PATH_SEP)) \
611 g_string_append_c (buffer, PATH_SEP); \
612 g_string_append (buffer, appendfrom); \
615 char *
616 vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
618 int element_index;
619 GString *buffer;
620 GString *recode_buffer;
622 if (vpath == NULL)
623 return NULL;
625 if (elements_count > vfs_path_elements_count (vpath))
626 elements_count = vfs_path_elements_count (vpath);
628 if (elements_count < 0)
629 elements_count = vfs_path_elements_count (vpath) + elements_count;
631 buffer = g_string_new ("");
632 recode_buffer = g_string_new ("");
634 for (element_index = 0; element_index < elements_count; element_index++)
636 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
638 if (element->vfs_prefix != NULL)
640 char *url_str;
642 if (buffer->len == 0 || buffer->str[buffer->len - 1] != PATH_SEP)
643 g_string_append_c (buffer, PATH_SEP);
645 g_string_append (buffer, element->vfs_prefix);
646 g_string_append (buffer, VFS_PATH_URL_DELIMITER);
648 url_str = vfs_path_build_url_params_str (element);
649 if (*url_str != '\0')
650 g_string_append (buffer, url_str);
652 g_free (url_str);
655 if (vfs_path_element_need_cleanup_converter (element))
657 if (buffer->str[buffer->len - 1] != PATH_SEP)
658 g_string_append (buffer, PATH_SEP_STR);
659 g_string_append (buffer, VFS_ENCODING_PREFIX);
660 g_string_append (buffer, element->encoding);
661 str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
662 vfs_append_from_path (recode_buffer->str);
663 g_string_set_size (recode_buffer, 0);
665 else
667 vfs_append_from_path (element->path);
670 g_string_free (recode_buffer, TRUE);
671 return g_string_free (buffer, FALSE);
674 #undef vfs_append_from_path
676 /* --------------------------------------------------------------------------------------------- */
678 * Convert vfs_path_t to string representation.
680 * @param vpath pointer to vfs_path_t object
682 * @return pointer to newly created string.
685 char *
686 vfs_path_to_str (const vfs_path_t * vpath)
688 return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
691 /* --------------------------------------------------------------------------------------------- */
693 * Split path string to path elements with flags for change parce process.
695 * @param path_str VFS-path
696 * @param flags flags for parser
698 * @return pointer to newly created vfs_path_t object with filled path elements array.
701 vfs_path_t *
702 vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
704 vfs_path_t *vpath;
705 char *path;
707 if (path_str == NULL)
708 return NULL;
710 if ((flags & VPF_NO_CANON) == 0)
711 path = vfs_canon (path_str);
712 else
713 path = g_strdup (path_str);
715 if (path == NULL)
716 return NULL;
718 if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
719 vpath = vfs_path_from_str_deprecated_parser (path);
720 else
721 vpath = vfs_path_from_str_uri_parser (path);
723 g_free (path);
725 return vpath;
728 /* --------------------------------------------------------------------------------------------- */
730 * Split path string to path elements.
732 * @param path_str VFS-path
734 * @return pointer to newly created vfs_path_t object with filled path elements array.
737 vfs_path_t *
738 vfs_path_from_str (const char *path_str)
740 return vfs_path_from_str_flags (path_str, VPF_NONE);
743 /* --------------------------------------------------------------------------------------------- */
745 * Create new vfs_path_t object.
747 * @return pointer to newly created vfs_path_t object.
750 vfs_path_t *
751 vfs_path_new (void)
753 vfs_path_t *vpath;
754 vpath = g_new0 (vfs_path_t, 1);
755 return vpath;
758 /* --------------------------------------------------------------------------------------------- */
760 * Get count of path elements.
762 * @param vpath pointer to vfs_path_t object
764 * @return count of path elements.
768 vfs_path_elements_count (const vfs_path_t * vpath)
770 return (vpath != NULL && vpath->path != NULL) ? g_list_length (vpath->path) : 0;
773 /* --------------------------------------------------------------------------------------------- */
775 * Get one path element by index.
777 * @param vpath pointer to vfs_path_t object
778 * @param element_index element index. May have negative value (in this case count was started at the end of list).
780 * @return path element.
783 vfs_path_element_t *
784 vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
786 if (element_index < 0)
787 element_index += vfs_path_elements_count (vpath);
789 if (element_index < 0)
790 vfs_die ("vfs_path_get_by_index: incorrect index!");
792 return g_list_nth_data (vpath->path, element_index);
795 /* --------------------------------------------------------------------------------------------- */
797 * Clone one path element
799 * @param element pointer to vfs_path_element_t object
801 * @return Newly allocated path element
804 vfs_path_element_t *
805 vfs_path_element_clone (const vfs_path_element_t * element)
807 vfs_path_element_t *new_element = g_new0 (vfs_path_element_t, 1);
808 memcpy (new_element, element, sizeof (vfs_path_element_t));
810 new_element->user = g_strdup (element->user);
811 new_element->password = g_strdup (element->password);
812 new_element->host = g_strdup (element->host);
813 new_element->path = g_strdup (element->path);
814 new_element->encoding = g_strdup (element->encoding);
815 if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
816 new_element->dir.converter = str_crt_conv_from (new_element->encoding);
817 new_element->vfs_prefix = g_strdup (element->vfs_prefix);
819 return new_element;
822 /* --------------------------------------------------------------------------------------------- */
824 * Free one path element.
826 * @param element pointer to vfs_path_element_t object
830 void
831 vfs_path_element_free (vfs_path_element_t * element)
833 if (element == NULL)
834 return;
836 g_free (element->user);
837 g_free (element->password);
838 g_free (element->host);
839 g_free (element->path);
840 g_free (element->encoding);
841 g_free (element->vfs_prefix);
843 if (vfs_path_element_need_cleanup_converter (element))
845 str_close_conv (element->dir.converter);
848 g_free (element);
851 /* --------------------------------------------------------------------------------------------- */
853 * Clone path
855 * @param vpath pointer to vfs_path_t object
857 * @return Newly allocated path object
860 vfs_path_t *
861 vfs_path_clone (const vfs_path_t * vpath)
863 vfs_path_t *new_vpath;
864 int vpath_element_index;
865 if (vpath == NULL)
866 return NULL;
868 new_vpath = vfs_path_new ();
869 for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
870 vpath_element_index++)
872 new_vpath->path =
873 g_list_append (new_vpath->path,
874 vfs_path_element_clone (vfs_path_get_by_index
875 (vpath, vpath_element_index)));
878 return new_vpath;
881 /* --------------------------------------------------------------------------------------------- */
883 * Free vfs_path_t object.
885 * @param vpath pointer to vfs_path_t object
889 void
890 vfs_path_free (vfs_path_t * path)
892 if (path == NULL)
893 return;
894 g_list_foreach (path->path, (GFunc) vfs_path_element_free, NULL);
895 g_list_free (path->path);
896 g_free (path);
899 /* --------------------------------------------------------------------------------------------- */
901 * Remove one path element by index
903 * @param vpath pointer to vfs_path_t object
904 * @param element_index element index. May have negative value (in this case count was started at the end of list).
908 void
909 vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
911 vfs_path_element_t *element;
913 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
914 return;
916 if (element_index < 0)
917 element_index = vfs_path_elements_count (vpath) + element_index;
919 element = g_list_nth_data (vpath->path, element_index);
920 vpath->path = g_list_remove (vpath->path, element);
921 vfs_path_element_free (element);
924 /* --------------------------------------------------------------------------------------------- */
925 /** Return VFS class for the given prefix */
927 struct vfs_class *
928 vfs_prefix_to_class (const char *prefix)
930 guint i;
932 /* Avoid first class (localfs) that would accept any prefix */
933 for (i = 1; i < vfs__classes_list->len; i++)
935 struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
936 if (vfs->which != NULL)
938 if (vfs->which (vfs, prefix) == -1)
939 continue;
940 return vfs;
943 if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
944 return vfs;
947 return NULL;
950 /* --------------------------------------------------------------------------------------------- */
952 * Check if need cleanup charset converter for vfs_path_element_t
954 * @param element part of path
956 * @return TRUE if need cleanup converter or FALSE otherwise
959 gboolean
960 vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
962 return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
965 /* --------------------------------------------------------------------------------------------- */
967 * Serialize vfs_path_t object to string
969 * @param vpath data for serialization
970 * @param error contain pointer to object for handle error code and message
972 * @return serialized vpath as newly allocated string
975 char *
976 vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
978 mc_config_t *cpath = mc_config_init (NULL);
979 ssize_t element_index;
980 char *ret_value;
982 if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
984 g_set_error (error, MC_ERROR, -1, "vpath object is empty");
985 return NULL;
988 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
990 char *groupname = g_strdup_printf ("path-element-%zd", element_index);
991 vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
993 /* convert one element to config group */
995 mc_config_set_string_raw (cpath, groupname, "path", element->path);
996 mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
997 mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
999 mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
1001 mc_config_set_string_raw (cpath, groupname, "user", element->user);
1002 mc_config_set_string_raw (cpath, groupname, "password", element->password);
1003 mc_config_set_string_raw (cpath, groupname, "host", element->host);
1004 if (element->port != 0)
1005 mc_config_set_int (cpath, groupname, "port", element->port);
1007 g_free (groupname);
1010 ret_value = mc_serialize_config (cpath, error);
1011 mc_config_deinit (cpath);
1012 return ret_value;
1015 /* --------------------------------------------------------------------------------------------- */
1017 * Deserialize string to vfs_path_t object
1019 * @param data data for serialization
1020 * @param error contain pointer to object for handle error code and message
1022 * @return newly allocated vfs_path_t object
1025 vfs_path_t *
1026 vfs_path_deserialize (const char *data, GError ** error)
1028 mc_config_t *cpath = mc_deserialize_config (data, error);
1029 size_t element_index = 0;
1030 vfs_path_t *vpath;
1032 if (cpath == NULL)
1033 return NULL;
1035 vpath = vfs_path_new ();
1037 while (TRUE)
1039 vfs_path_element_t *element;
1040 char *cfg_value;
1041 char *groupname;
1043 groupname = g_strdup_printf ("path-element-%zd", element_index);
1044 if (!mc_config_has_group (cpath, groupname))
1046 g_free (groupname);
1047 break;
1050 element = g_new0 (vfs_path_element_t, 1);
1052 cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
1053 element->class = vfs_get_class_by_name (cfg_value);
1054 if (element->class == NULL)
1056 g_free (element);
1057 vfs_path_free (vpath);
1058 g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
1059 g_free (cfg_value);
1060 mc_config_deinit (cpath);
1061 return NULL;
1063 g_free (cfg_value);
1065 element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
1066 element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
1067 element->dir.converter =
1068 (element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
1070 element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
1072 element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
1073 element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
1074 element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
1075 element->port = mc_config_get_int (cpath, groupname, "port", 0);
1077 vpath->path = g_list_append (vpath->path, element);
1079 g_free (groupname);
1080 element_index++;
1083 mc_config_deinit (cpath);
1084 if (vfs_path_elements_count (vpath) == 0)
1086 vfs_path_free (vpath);
1087 g_set_error (error, MC_ERROR, -1, "No any path elements found");
1088 return NULL;
1091 return vpath;
1094 /* --------------------------------------------------------------------------------------------- */
1096 * Build vfs_path_t object from arguments.
1098 * @param ... path tokens, terminated by NULL
1100 * @return newly allocated vfs_path_t object
1103 vfs_path_t *
1104 vfs_path_build_filename (const char *first_element, ...)
1106 va_list args;
1107 char *str_path;
1108 vfs_path_t *vpath;
1110 if (first_element == NULL)
1111 return NULL;
1113 va_start (args, first_element);
1114 str_path = mc_build_filenamev (first_element, args);
1115 va_end (args);
1116 vpath = vfs_path_from_str (str_path);
1117 g_free (str_path);
1118 return vpath;
1121 /* --------------------------------------------------------------------------------------------- */
1123 * Append tokens to path object
1125 * @param vpath path object
1126 * @param ... NULL-terminated strings
1128 * @return newly allocated path object
1131 vfs_path_t *
1132 vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
1134 va_list args;
1135 char *str_path, *result_str;
1136 vfs_path_t *ret_vpath;
1138 if (vpath == NULL || first_element == NULL)
1139 return NULL;
1141 va_start (args, first_element);
1142 str_path = mc_build_filenamev (first_element, args);
1143 va_end (args);
1145 result_str = vfs_path_to_str (vpath);
1146 ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
1147 g_free (result_str);
1148 g_free (str_path);
1150 return ret_vpath;
1154 /* --------------------------------------------------------------------------------------------- */
1157 * Append vpath_t tokens to path object
1159 * @param ... NULL-terminated vpath objects
1161 * @return newly allocated path object
1164 vfs_path_t *
1165 vfs_path_append_vpath_new (const vfs_path_t * first_vpath, ...)
1167 va_list args;
1168 vfs_path_t *ret_vpath;
1169 const vfs_path_t *current_vpath = first_vpath;
1171 if (first_vpath == NULL)
1172 return NULL;
1174 ret_vpath = vfs_path_new ();
1176 va_start (args, first_vpath);
1179 int vindex;
1181 for (vindex = 0; vindex < vfs_path_elements_count (current_vpath); vindex++)
1182 ret_vpath->path =
1183 g_list_append (ret_vpath->path,
1184 vfs_path_element_clone (vfs_path_get_by_index
1185 (current_vpath, vindex)));
1186 current_vpath = va_arg (args, const vfs_path_t *);
1188 while (current_vpath != NULL);
1189 va_end (args);
1191 return ret_vpath;
1194 /* --------------------------------------------------------------------------------------------- */
1196 * get tockens count in path.
1198 * @param vpath path object
1200 * @return count of tokens
1203 size_t
1204 vfs_path_tokens_count (const vfs_path_t * vpath)
1206 size_t count_tokens = 0;
1207 int element_index;
1209 if (vpath == NULL)
1210 return 0;
1212 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1214 vfs_path_element_t *element;
1215 char **path_tokens, **iterator;
1217 element = vfs_path_get_by_index (vpath, element_index);
1218 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1220 while (*iterator != NULL)
1222 if (**iterator != '\0')
1223 count_tokens++;
1224 iterator++;
1226 g_strfreev (path_tokens);
1228 return count_tokens;
1231 /* --------------------------------------------------------------------------------------------- */
1233 * Get subpath by tokens
1235 * @param vpath path object
1236 * @param start_position first token for got/ Started from 0.
1237 * If negative, then position will be relative to end of path
1238 * @param length count of tokens
1240 * @return newly allocated string with path tokens separated by slash
1243 char *
1244 vfs_path_tokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t length)
1246 GString *ret_tokens, *element_tokens;
1247 int element_index;
1248 size_t tokens_count = vfs_path_tokens_count (vpath);
1250 if (vpath == NULL)
1251 return NULL;
1253 if (length == 0)
1254 length = tokens_count;
1256 if (start_position < 0)
1257 start_position = (ssize_t) tokens_count + start_position;
1259 if (start_position < 0)
1260 return NULL;
1262 if (start_position >= (ssize_t) tokens_count)
1263 return NULL;
1265 if (start_position + (ssize_t) length > (ssize_t) tokens_count)
1266 length = tokens_count - start_position;
1268 ret_tokens = g_string_sized_new (32);
1269 element_tokens = g_string_sized_new (32);
1271 for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
1273 vfs_path_element_t *element;
1274 char **path_tokens, **iterator;
1276 g_string_assign (element_tokens, "");
1277 element = vfs_path_get_by_index (vpath, element_index);
1278 path_tokens = iterator = g_strsplit (element->path, PATH_SEP_STR, -1);
1280 while (*iterator != NULL)
1282 if (**iterator != '\0')
1284 if (start_position == 0)
1286 if (length == 0)
1288 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1289 g_string_free (element_tokens, TRUE);
1290 g_strfreev (path_tokens);
1291 return g_string_free (ret_tokens, FALSE);
1293 length--;
1294 if (element_tokens->len != 0)
1295 g_string_append_c (element_tokens, PATH_SEP);
1296 g_string_append (element_tokens, *iterator);
1298 else
1299 start_position--;
1301 iterator++;
1303 g_strfreev (path_tokens);
1304 vfs_path_tokens_add_class_info (element, ret_tokens, element_tokens);
1307 g_string_free (element_tokens, TRUE);
1308 return g_string_free (ret_tokens, !(start_position == 0 && length == 0));
1311 /* --------------------------------------------------------------------------------------------- */
1313 * Get subpath by tokens
1315 * @param vpath path object
1316 * @param start_position first token for got/ Started from 0.
1317 * If negative, then position will be relative to end of path
1318 * @param length count of tokens
1320 * @return newly allocated path object with path tokens separated by slash
1323 vfs_path_t *
1324 vfs_path_vtokens_get (const vfs_path_t * vpath, ssize_t start_position, size_t length)
1326 char *str_tokens;
1327 vfs_path_t *ret_vpath = NULL;
1329 str_tokens = vfs_path_tokens_get (vpath, start_position, length);
1330 if (str_tokens != NULL)
1332 ret_vpath = vfs_path_from_str_flags (str_tokens, VPF_NO_CANON);
1333 g_free (str_tokens);
1335 return ret_vpath;
1338 /* --------------------------------------------------------------------------------------------- */