2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Virtual File System path handlers
|
|
|
|
|
|
|
|
Copyright (C) 2011
|
|
|
|
The Free Software Foundation, Inc.
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2011
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is free software: you can redistribute it
|
2011-04-14 16:21:30 +04:00
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
2011-10-15 14:56:47 +04:00
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-04-14 16:21:30 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Source: Virtual File System: path handlers
|
|
|
|
* \author Slava Zanko
|
|
|
|
* \date 2011
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
2011-04-28 15:39:59 +04:00
|
|
|
#include "lib/strutil.h"
|
2011-05-01 14:30:58 +04:00
|
|
|
#include "lib/util.h" /* concat_dir_and_file */
|
2011-06-07 13:22:31 +04:00
|
|
|
#include "lib/serialize.h"
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
#include "vfs.h"
|
|
|
|
#include "utilvfs.h"
|
2011-06-14 12:55:27 +04:00
|
|
|
#include "xdirentry.h"
|
2011-04-14 16:21:30 +04:00
|
|
|
#include "path.h"
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
extern GPtrArray *vfs__classes_list;
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
static gboolean
|
|
|
|
path_magic (const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
return (stat (path, &buf) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Splits path extracting vfs part.
|
|
|
|
*
|
|
|
|
* Splits path
|
|
|
|
* \verbatim /p1#op/inpath \endverbatim
|
|
|
|
* into
|
|
|
|
* \verbatim inpath,op; \endverbatim
|
|
|
|
* returns which vfs it is.
|
|
|
|
* What is left in path is p1. You still want to g_free(path), you DON'T
|
|
|
|
* want to free neither *inpath nor *op
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct vfs_class *
|
|
|
|
_vfs_split_with_semi_skip_count (char *path, const char **inpath, const char **op,
|
|
|
|
size_t skip_count)
|
|
|
|
{
|
|
|
|
char *semi;
|
|
|
|
char *slash;
|
|
|
|
struct vfs_class *ret;
|
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
vfs_die ("Cannot split NULL");
|
|
|
|
|
|
|
|
semi = strrstr_skip_count (path, "#", skip_count);
|
|
|
|
|
|
|
|
if ((semi == NULL) || (!path_magic (path)))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
slash = strchr (semi, PATH_SEP);
|
|
|
|
*semi = '\0';
|
|
|
|
|
|
|
|
if (op != NULL)
|
|
|
|
*op = NULL;
|
|
|
|
|
|
|
|
if (inpath != NULL)
|
|
|
|
*inpath = NULL;
|
|
|
|
|
|
|
|
if (slash != NULL)
|
|
|
|
*slash = '\0';
|
|
|
|
|
|
|
|
ret = vfs_prefix_to_class (semi + 1);
|
|
|
|
if (ret != NULL)
|
|
|
|
{
|
|
|
|
if (op != NULL)
|
|
|
|
*op = semi + 1;
|
|
|
|
if (inpath != NULL)
|
|
|
|
*inpath = slash != NULL ? slash + 1 : NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (slash != NULL)
|
|
|
|
*slash = PATH_SEP;
|
|
|
|
|
|
|
|
*semi = '#';
|
|
|
|
ret = _vfs_split_with_semi_skip_count (path, inpath, op, skip_count + 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-05-01 14:30:58 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* remove //, /./ and /../
|
|
|
|
*
|
|
|
|
* @return newly allocated string
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *
|
|
|
|
vfs_canon (const char *path)
|
|
|
|
{
|
2012-03-12 11:48:41 +04:00
|
|
|
if (path == NULL)
|
2011-05-01 14:30:58 +04:00
|
|
|
vfs_die ("Cannot canonicalize NULL");
|
|
|
|
|
|
|
|
/* Relative to current directory */
|
|
|
|
if (*path != PATH_SEP)
|
|
|
|
{
|
2012-03-12 11:48:41 +04:00
|
|
|
char *result, *local;
|
|
|
|
|
|
|
|
local = tilde_expand (path);
|
|
|
|
if (*local != PATH_SEP)
|
|
|
|
{
|
|
|
|
char *curr_dir;
|
2011-05-01 14:30:58 +04:00
|
|
|
|
2012-03-12 11:48:41 +04:00
|
|
|
g_free (local);
|
|
|
|
curr_dir = vfs_get_current_dir ();
|
|
|
|
local = concat_dir_and_file (curr_dir, path);
|
|
|
|
g_free (curr_dir);
|
|
|
|
}
|
2011-05-01 14:30:58 +04:00
|
|
|
|
|
|
|
result = vfs_canon (local);
|
|
|
|
g_free (local);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* So we have path of following form:
|
|
|
|
* /p1/p2#op/.././././p3#op/p4. Good luck.
|
|
|
|
*/
|
|
|
|
{
|
2012-03-12 11:48:41 +04:00
|
|
|
char *result;
|
|
|
|
|
|
|
|
result = g_strdup (path);
|
2011-05-01 14:30:58 +04:00
|
|
|
canonicalize_pathname (result);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-14 12:28:51 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Build URL parameters (such as user:pass@host:port) from one path element object
|
|
|
|
*
|
|
|
|
* @param element path element
|
|
|
|
*
|
|
|
|
* @return newly allocated string
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *
|
|
|
|
vfs_path_build_url_params_str (vfs_path_element_t * element)
|
|
|
|
{
|
|
|
|
GString *buffer;
|
|
|
|
|
|
|
|
if (element == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buffer = g_string_new ("");
|
|
|
|
|
|
|
|
if (element->user != NULL)
|
|
|
|
g_string_append (buffer, element->user);
|
|
|
|
|
|
|
|
if (element->password != NULL)
|
|
|
|
{
|
|
|
|
g_string_append_c (buffer, ':');
|
|
|
|
g_string_append (buffer, element->password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (element->host != NULL)
|
|
|
|
{
|
|
|
|
if ((element->user != NULL) || (element->password != NULL))
|
|
|
|
g_string_append_c (buffer, '@');
|
2011-07-05 17:05:15 +04:00
|
|
|
if (element->ipv6)
|
|
|
|
g_string_append_c (buffer, '[');
|
2011-06-14 12:28:51 +04:00
|
|
|
g_string_append (buffer, element->host);
|
2011-07-05 17:05:15 +04:00
|
|
|
if (element->ipv6)
|
|
|
|
g_string_append_c (buffer, ']');
|
2011-06-14 12:28:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((element->port) != 0 && (element->host != NULL))
|
|
|
|
{
|
|
|
|
g_string_append_c (buffer, ':');
|
|
|
|
g_string_append_printf (buffer, "%d", element->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_string_free (buffer, FALSE);
|
|
|
|
}
|
|
|
|
|
2011-05-30 11:49:10 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** get encoding after last #enc: or NULL, if part does not contain #enc:
|
|
|
|
*
|
|
|
|
* @param path string
|
|
|
|
*
|
|
|
|
* @return newly allocated string.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *
|
|
|
|
vfs_get_encoding (const char *path)
|
|
|
|
{
|
|
|
|
char result[16];
|
|
|
|
char *work;
|
|
|
|
char *semi;
|
|
|
|
char *slash;
|
|
|
|
work = g_strdup (path);
|
2011-05-30 17:54:45 +04:00
|
|
|
|
2011-06-02 14:20:14 +04:00
|
|
|
/* try found #enc: */
|
|
|
|
semi = g_strrstr (work, VFS_ENCODING_PREFIX);
|
2011-05-30 11:49:10 +04:00
|
|
|
|
2011-06-02 14:20:14 +04:00
|
|
|
if (semi != NULL && (semi == work || *(semi - 1) == PATH_SEP))
|
2011-05-30 11:49:10 +04:00
|
|
|
{
|
2011-06-02 14:20:14 +04:00
|
|
|
semi += strlen (VFS_ENCODING_PREFIX); /* skip "#enc:" */
|
2011-05-30 11:49:10 +04:00
|
|
|
slash = strchr (semi, PATH_SEP);
|
|
|
|
if (slash != NULL)
|
|
|
|
slash[0] = '\0';
|
|
|
|
|
|
|
|
g_strlcpy (result, semi, sizeof (result));
|
|
|
|
g_free (work);
|
|
|
|
return g_strdup (result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g_free (work);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-13 14:19:47 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Extract the hostname and username from the path
|
|
|
|
*
|
|
|
|
* Format of the path is [user@]hostname:port/remote-dir, e.g.:
|
|
|
|
*
|
|
|
|
* ftp://sunsite.unc.edu/pub/linux
|
|
|
|
* ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
|
|
|
|
* ftp://tsx-11.mit.edu:8192/
|
|
|
|
* ftp://joe@foo.edu:11321/private
|
|
|
|
* ftp://joe:password@foo.se
|
|
|
|
*
|
|
|
|
* @param path_element is an input string to be parsed
|
|
|
|
* @param path is an input string to be parsed
|
|
|
|
*
|
|
|
|
* @return g_malloc()ed url info.
|
|
|
|
* If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
|
|
|
|
* is not set, then the current login name is supplied.
|
|
|
|
* Return value is a g_malloc()ed structure with the pathname relative to the
|
|
|
|
* host.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
vfs_path_url_split (vfs_path_element_t * path_element, const char *path)
|
|
|
|
{
|
|
|
|
char *pcopy;
|
|
|
|
const char *pend;
|
|
|
|
char *dir, *colon, *inner_colon, *at, *rest;
|
|
|
|
|
|
|
|
path_element->port = 0;
|
|
|
|
|
|
|
|
pcopy = g_strdup (path);
|
|
|
|
pend = pcopy + strlen (pcopy);
|
|
|
|
dir = pcopy;
|
|
|
|
|
|
|
|
/* search for any possible user */
|
|
|
|
at = strrchr (pcopy, '@');
|
|
|
|
|
|
|
|
/* We have a username */
|
|
|
|
if (at == NULL)
|
|
|
|
rest = pcopy;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*at = '\0';
|
|
|
|
inner_colon = strchr (pcopy, ':');
|
|
|
|
if (inner_colon != NULL)
|
|
|
|
{
|
|
|
|
*inner_colon = '\0';
|
|
|
|
inner_colon++;
|
|
|
|
path_element->password = g_strdup (inner_colon);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*pcopy != '\0')
|
|
|
|
path_element->user = g_strdup (pcopy);
|
|
|
|
|
|
|
|
if (pend == at + 1)
|
|
|
|
rest = at;
|
|
|
|
else
|
|
|
|
rest = at + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the host comes with a port spec, if so, chop it */
|
|
|
|
if (*rest != '[')
|
|
|
|
colon = strchr (rest, ':');
|
|
|
|
else
|
|
|
|
{
|
|
|
|
colon = strchr (++rest, ']');
|
|
|
|
if (colon != NULL)
|
|
|
|
{
|
|
|
|
colon[0] = '\0';
|
|
|
|
colon[1] = '\0';
|
|
|
|
colon++;
|
2011-07-05 17:05:15 +04:00
|
|
|
path_element->ipv6 = TRUE;
|
2011-06-13 14:19:47 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (colon != NULL)
|
|
|
|
{
|
|
|
|
*colon = '\0';
|
|
|
|
if (sscanf (colon + 1, "%d", &path_element->port) == 1)
|
|
|
|
{
|
|
|
|
if (path_element->port <= 0 || path_element->port >= 65536)
|
|
|
|
path_element->port = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
while (*(++colon) != '\0')
|
|
|
|
{
|
|
|
|
switch (*colon)
|
|
|
|
{
|
|
|
|
case 'C':
|
|
|
|
path_element->port = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
path_element->port = 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path_element->host = g_strdup (rest);
|
|
|
|
g_free (pcopy);
|
|
|
|
}
|
|
|
|
|
2011-06-07 13:22:31 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* get VFS class for the given name
|
|
|
|
*
|
|
|
|
* @param class_name name of class
|
|
|
|
*
|
|
|
|
* @return pointer to class structure or NULL if class not found
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct vfs_class *
|
|
|
|
vfs_get_class_by_name (const char *class_name)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (class_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < vfs__classes_list->len; i++)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
|
|
|
|
if ((vfs->name != NULL) && (strcmp (vfs->name, class_name) == 0))
|
|
|
|
return vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-06-14 12:55:27 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Check if path string contain URL-like elements
|
|
|
|
*
|
|
|
|
* @param path_str path
|
|
|
|
*
|
|
|
|
* @return TRUE if path is deprecated or FALSE otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
vfs_path_is_str_path_deprecated (const char *path_str)
|
|
|
|
{
|
2011-06-14 16:09:59 +04:00
|
|
|
return strstr (path_str, VFS_PATH_URL_DELIMITER) == NULL;
|
2011-06-14 12:55:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Split path string to path elements by deprecated algorithm.
|
|
|
|
*
|
|
|
|
* @param path_str VFS-path
|
|
|
|
*
|
|
|
|
* @return pointer to newly created vfs_path_t object with filled path elements array.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static vfs_path_t *
|
|
|
|
vfs_path_from_str_deprecated_parser (char *path)
|
|
|
|
{
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *element;
|
|
|
|
struct vfs_class *class;
|
|
|
|
const char *local, *op;
|
|
|
|
|
|
|
|
vpath = vfs_path_new ();
|
|
|
|
|
|
|
|
while ((class = _vfs_split_with_semi_skip_count (path, &local, &op, 0)) != NULL)
|
|
|
|
{
|
|
|
|
char *url_params;
|
|
|
|
element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
element->class = class;
|
|
|
|
if (local == NULL)
|
|
|
|
local = "";
|
|
|
|
element->path = vfs_translate_path_n (local);
|
|
|
|
|
|
|
|
element->encoding = vfs_get_encoding (local);
|
2011-08-15 15:37:59 +04:00
|
|
|
element->dir.converter =
|
|
|
|
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
2011-06-14 12:55:27 +04:00
|
|
|
|
|
|
|
url_params = strchr (op, ':'); /* skip VFS prefix */
|
|
|
|
if (url_params != NULL)
|
|
|
|
{
|
|
|
|
*url_params = '\0';
|
|
|
|
url_params++;
|
|
|
|
vfs_path_url_split (element, url_params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*op != '\0')
|
|
|
|
element->vfs_prefix = g_strdup (op);
|
|
|
|
|
|
|
|
vpath->path = g_list_prepend (vpath->path, element);
|
|
|
|
}
|
|
|
|
if (path[0] != '\0')
|
|
|
|
{
|
|
|
|
element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
element->class = g_ptr_array_index (vfs__classes_list, 0);
|
|
|
|
element->path = vfs_translate_path_n (path);
|
|
|
|
|
|
|
|
element->encoding = vfs_get_encoding (path);
|
2011-08-15 15:37:59 +04:00
|
|
|
element->dir.converter =
|
|
|
|
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
2011-06-14 12:55:27 +04:00
|
|
|
vpath->path = g_list_prepend (vpath->path, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Split path string to path elements by URL algorithm.
|
|
|
|
*
|
|
|
|
* @param path_str VFS-path
|
|
|
|
*
|
|
|
|
* @return pointer to newly created vfs_path_t object with filled path elements array.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static vfs_path_t *
|
|
|
|
vfs_path_from_str_uri_parser (char *path)
|
|
|
|
{
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *element;
|
|
|
|
|
|
|
|
char *url_delimiter;
|
|
|
|
|
|
|
|
vpath = vfs_path_new ();
|
|
|
|
|
2011-06-14 16:09:59 +04:00
|
|
|
while ((url_delimiter = g_strrstr (path, VFS_PATH_URL_DELIMITER)) != NULL)
|
2011-06-14 12:55:27 +04:00
|
|
|
{
|
|
|
|
char *vfs_prefix_start;
|
|
|
|
char *real_vfs_prefix_start = url_delimiter;
|
|
|
|
char *slash_pointer;
|
2011-06-14 16:09:59 +04:00
|
|
|
struct vfs_s_subclass *sub = NULL;
|
2011-06-14 12:55:27 +04:00
|
|
|
|
|
|
|
while (real_vfs_prefix_start > path && *(real_vfs_prefix_start) != PATH_SEP)
|
|
|
|
real_vfs_prefix_start--;
|
|
|
|
vfs_prefix_start = real_vfs_prefix_start;
|
|
|
|
|
|
|
|
if (*(vfs_prefix_start) == PATH_SEP)
|
|
|
|
vfs_prefix_start += 1;
|
2011-06-14 16:09:59 +04:00
|
|
|
|
|
|
|
*url_delimiter = '\0';
|
2011-06-14 12:55:27 +04:00
|
|
|
|
|
|
|
element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
element->class = vfs_prefix_to_class (vfs_prefix_start);
|
|
|
|
element->vfs_prefix = g_strdup (vfs_prefix_start);
|
|
|
|
|
2011-06-14 16:09:59 +04:00
|
|
|
url_delimiter += strlen (VFS_PATH_URL_DELIMITER);
|
2011-06-14 12:55:27 +04:00
|
|
|
sub = VFSDATA (element);
|
2011-06-14 16:09:59 +04:00
|
|
|
if (sub != NULL && sub->flags & VFS_S_REMOTE)
|
2011-06-14 12:55:27 +04:00
|
|
|
{
|
|
|
|
slash_pointer = strchr (url_delimiter, PATH_SEP);
|
|
|
|
if (slash_pointer == NULL)
|
|
|
|
{
|
|
|
|
element->path = g_strdup ("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
element->path = vfs_translate_path_n (slash_pointer + 1);
|
|
|
|
element->encoding = vfs_get_encoding (slash_pointer);
|
2011-08-15 15:37:59 +04:00
|
|
|
|
2011-06-14 12:55:27 +04:00
|
|
|
*slash_pointer = '\0';
|
|
|
|
}
|
|
|
|
vfs_path_url_split (element, url_delimiter);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
element->path = vfs_translate_path_n (url_delimiter);
|
|
|
|
element->encoding = vfs_get_encoding (url_delimiter);
|
|
|
|
}
|
2011-08-15 15:37:59 +04:00
|
|
|
element->dir.converter =
|
|
|
|
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
2011-06-14 12:55:27 +04:00
|
|
|
vpath->path = g_list_prepend (vpath->path, element);
|
|
|
|
|
2011-06-14 16:09:59 +04:00
|
|
|
if (real_vfs_prefix_start > path && *(real_vfs_prefix_start) == PATH_SEP)
|
2011-06-14 12:55:27 +04:00
|
|
|
*real_vfs_prefix_start = '\0';
|
|
|
|
else
|
|
|
|
*(real_vfs_prefix_start + 1) = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path[0] != '\0')
|
|
|
|
{
|
|
|
|
element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
element->class = g_ptr_array_index (vfs__classes_list, 0);
|
|
|
|
element->path = vfs_translate_path_n (path);
|
|
|
|
element->encoding = vfs_get_encoding (path);
|
2011-08-15 15:37:59 +04:00
|
|
|
element->dir.converter =
|
|
|
|
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
2011-06-14 12:55:27 +04:00
|
|
|
vpath->path = g_list_prepend (vpath->path, element);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpath;
|
|
|
|
}
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/**
|
|
|
|
* Convert first elements_count elements from vfs_path_t to string representation.
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
* @param elements_count count of first elements for convert
|
2011-08-15 15:37:59 +04:00
|
|
|
* @param flags flags for parser
|
2011-04-28 15:39:59 +04:00
|
|
|
*
|
|
|
|
* @return pointer to newly created string.
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-08-15 15:37:59 +04:00
|
|
|
#define vfs_append_from_path(appendfrom) \
|
|
|
|
{ \
|
|
|
|
if ((*appendfrom != PATH_SEP) && (*appendfrom != '\0') \
|
|
|
|
&& (buffer->str[buffer->len - 1] != PATH_SEP)) \
|
|
|
|
g_string_append_c (buffer, PATH_SEP); \
|
|
|
|
g_string_append (buffer, appendfrom); \
|
|
|
|
}
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
char *
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs_path_to_str_elements_count (const vfs_path_t * vpath, int elements_count)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
int element_index;
|
2011-04-14 16:21:30 +04:00
|
|
|
GString *buffer;
|
2011-08-15 15:37:59 +04:00
|
|
|
GString *recode_buffer;
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
if (vpath == NULL)
|
2011-04-14 16:21:30 +04:00
|
|
|
return NULL;
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
if (elements_count > vfs_path_elements_count (vpath))
|
|
|
|
elements_count = vfs_path_elements_count (vpath);
|
|
|
|
|
|
|
|
if (elements_count < 0)
|
|
|
|
elements_count = vfs_path_elements_count (vpath) + elements_count;
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
buffer = g_string_new ("");
|
2011-08-15 15:37:59 +04:00
|
|
|
recode_buffer = g_string_new ("");
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
for (element_index = 0; element_index < elements_count; element_index++)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
|
|
|
|
|
2011-06-14 12:28:51 +04:00
|
|
|
if (element->vfs_prefix != NULL)
|
2011-04-28 15:39:59 +04:00
|
|
|
{
|
2011-06-14 12:28:51 +04:00
|
|
|
char *url_str;
|
2011-06-14 16:09:59 +04:00
|
|
|
|
|
|
|
if (buffer->str[buffer->len - 1] != '/')
|
|
|
|
g_string_append_c (buffer, '/');
|
|
|
|
|
2011-06-14 12:28:51 +04:00
|
|
|
g_string_append (buffer, element->vfs_prefix);
|
2011-06-14 16:09:59 +04:00
|
|
|
g_string_append (buffer, VFS_PATH_URL_DELIMITER);
|
2011-06-14 12:28:51 +04:00
|
|
|
|
|
|
|
url_str = vfs_path_build_url_params_str (element);
|
|
|
|
if (*url_str != '\0')
|
|
|
|
g_string_append (buffer, url_str);
|
2011-06-14 16:09:59 +04:00
|
|
|
|
2011-06-14 12:28:51 +04:00
|
|
|
g_free (url_str);
|
2011-04-28 15:39:59 +04:00
|
|
|
}
|
2011-06-14 12:28:51 +04:00
|
|
|
|
2011-08-15 15:37:59 +04:00
|
|
|
if (vfs_path_element_need_cleanup_converter (element))
|
2011-05-30 17:54:45 +04:00
|
|
|
{
|
2011-06-14 16:09:59 +04:00
|
|
|
if (buffer->str[buffer->len - 1] != PATH_SEP)
|
|
|
|
g_string_append (buffer, PATH_SEP_STR);
|
|
|
|
g_string_append (buffer, VFS_ENCODING_PREFIX);
|
2011-05-30 17:54:45 +04:00
|
|
|
g_string_append (buffer, element->encoding);
|
2011-08-15 15:37:59 +04:00
|
|
|
str_vfs_convert_from (element->dir.converter, element->path, recode_buffer);
|
|
|
|
vfs_append_from_path (recode_buffer->str);
|
|
|
|
g_string_set_size (recode_buffer, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vfs_append_from_path (element->path);
|
2011-05-30 17:54:45 +04:00
|
|
|
}
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
2011-08-15 15:37:59 +04:00
|
|
|
g_string_free (recode_buffer, TRUE);
|
2011-04-18 16:08:13 +04:00
|
|
|
return g_string_free (buffer, FALSE);
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
2011-08-15 15:37:59 +04:00
|
|
|
#undef vfs_append_from_path
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/**
|
|
|
|
* Convert vfs_path_t to string representation.
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
*
|
|
|
|
* @return pointer to newly created string.
|
|
|
|
*/
|
2011-08-15 15:37:59 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
char *
|
|
|
|
vfs_path_to_str (const vfs_path_t * vpath)
|
|
|
|
{
|
|
|
|
return vfs_path_to_str_elements_count (vpath, vfs_path_elements_count (vpath));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
2011-06-29 14:12:19 +04:00
|
|
|
* Split path string to path elements with flags for change parce process.
|
2011-04-28 15:39:59 +04:00
|
|
|
*
|
|
|
|
* @param path_str VFS-path
|
2011-06-29 14:12:19 +04:00
|
|
|
* @param flags flags for parser
|
2011-04-28 15:39:59 +04:00
|
|
|
*
|
2011-05-01 14:30:58 +04:00
|
|
|
* @return pointer to newly created vfs_path_t object with filled path elements array.
|
2011-04-28 15:39:59 +04:00
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
vfs_path_t *
|
2011-06-29 14:12:19 +04:00
|
|
|
vfs_path_from_str_flags (const char *path_str, vfs_path_flag_t flags)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
char *path;
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
if (path_str == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-06-29 14:12:19 +04:00
|
|
|
if ((flags & VPF_NO_CANON) == 0)
|
|
|
|
path = vfs_canon (path_str);
|
|
|
|
else
|
|
|
|
path = g_strdup (path_str);
|
|
|
|
|
2011-06-01 15:53:08 +04:00
|
|
|
if (path == NULL)
|
2011-04-20 16:59:06 +04:00
|
|
|
return NULL;
|
2011-05-30 11:16:40 +04:00
|
|
|
|
2011-06-29 14:41:14 +04:00
|
|
|
if ((flags & VPF_USE_DEPRECATED_PARSER) != 0 && vfs_path_is_str_path_deprecated (path))
|
2011-06-14 12:55:27 +04:00
|
|
|
vpath = vfs_path_from_str_deprecated_parser (path);
|
|
|
|
else
|
|
|
|
vpath = vfs_path_from_str_uri_parser (path);
|
2011-06-01 14:56:52 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
g_free (path);
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
return vpath;
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
2011-06-29 14:12:19 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Split path string to path elements.
|
|
|
|
*
|
|
|
|
* @param path_str VFS-path
|
|
|
|
*
|
|
|
|
* @return pointer to newly created vfs_path_t object with filled path elements array.
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_path_from_str (const char *path_str)
|
|
|
|
{
|
|
|
|
return vfs_path_from_str_flags (path_str, VPF_NONE);
|
|
|
|
}
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/*
|
|
|
|
* Create new vfs_path_t object.
|
|
|
|
*
|
|
|
|
* @return pointer to newly created vfs_path_t object.
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
vfs_path_t *
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_new (void)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-04-26 17:29:34 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vpath = g_new0 (vfs_path_t, 1);
|
|
|
|
return vpath;
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/*
|
|
|
|
* Get count of path elements.
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
*
|
|
|
|
* @return count of path elements.
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
int
|
|
|
|
vfs_path_elements_count (const vfs_path_t * vpath)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-06-29 21:52:01 +04:00
|
|
|
return (vpath != NULL && vpath->path != NULL) ? g_list_length (vpath->path) : 0;
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/*
|
|
|
|
* Get one path element by index.
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
* @param element_index element index. May have negative value (in this case count was started at the end of list).
|
|
|
|
*
|
|
|
|
* @return path element.
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
vfs_path_element_t *
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs_path_get_by_index (const vfs_path_t * vpath, int element_index)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
2011-06-08 17:40:36 +04:00
|
|
|
if (element_index < 0)
|
2011-06-29 21:52:01 +04:00
|
|
|
element_index += vfs_path_elements_count (vpath);
|
2011-04-28 15:39:59 +04:00
|
|
|
|
2011-06-29 21:52:01 +04:00
|
|
|
if (element_index < 0)
|
2011-06-19 15:42:15 +04:00
|
|
|
vfs_die ("vfs_path_get_by_index: incorrect index!");
|
2011-06-08 17:40:36 +04:00
|
|
|
|
2011-06-29 21:52:01 +04:00
|
|
|
return g_list_nth_data (vpath->path, element_index);
|
2011-06-08 17:40:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* Clone one path element
|
|
|
|
*
|
|
|
|
* @param element pointer to vfs_path_element_t object
|
|
|
|
*
|
|
|
|
* @return Newly allocated path element
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_element_t *
|
|
|
|
vfs_path_element_clone (const vfs_path_element_t * element)
|
|
|
|
{
|
|
|
|
vfs_path_element_t *new_element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
memcpy (new_element, element, sizeof (vfs_path_element_t));
|
|
|
|
|
|
|
|
new_element->user = g_strdup (element->user);
|
|
|
|
new_element->password = g_strdup (element->password);
|
|
|
|
new_element->host = g_strdup (element->host);
|
|
|
|
new_element->path = g_strdup (element->path);
|
|
|
|
new_element->encoding = g_strdup (element->encoding);
|
2011-06-19 15:33:04 +04:00
|
|
|
if (vfs_path_element_need_cleanup_converter (element) && new_element->encoding != NULL)
|
|
|
|
new_element->dir.converter = str_crt_conv_from (new_element->encoding);
|
2011-06-13 14:19:47 +04:00
|
|
|
new_element->vfs_prefix = g_strdup (element->vfs_prefix);
|
2011-06-08 17:40:36 +04:00
|
|
|
|
|
|
|
return new_element;
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/*
|
|
|
|
* Free one path element.
|
|
|
|
*
|
|
|
|
* @param element pointer to vfs_path_element_t object
|
|
|
|
*
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
void
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_element_free (vfs_path_element_t * element)
|
2011-04-14 16:21:30 +04:00
|
|
|
{
|
|
|
|
if (element == NULL)
|
|
|
|
return;
|
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
g_free (element->user);
|
|
|
|
g_free (element->password);
|
|
|
|
g_free (element->host);
|
2011-04-18 16:08:13 +04:00
|
|
|
g_free (element->path);
|
|
|
|
g_free (element->encoding);
|
2011-06-13 14:19:47 +04:00
|
|
|
g_free (element->vfs_prefix);
|
2011-06-01 14:56:52 +04:00
|
|
|
|
|
|
|
if (vfs_path_element_need_cleanup_converter (element))
|
|
|
|
{
|
|
|
|
str_close_conv (element->dir.converter);
|
|
|
|
}
|
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
g_free (element);
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
2011-06-08 17:40:36 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* Clone path
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
*
|
|
|
|
* @return Newly allocated path object
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_path_clone (const vfs_path_t * vpath)
|
|
|
|
{
|
|
|
|
vfs_path_t *new_vpath;
|
|
|
|
int vpath_element_index;
|
|
|
|
if (vpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
new_vpath = vfs_path_new ();
|
|
|
|
for (vpath_element_index = 0; vpath_element_index < vfs_path_elements_count (vpath);
|
|
|
|
vpath_element_index++)
|
|
|
|
{
|
|
|
|
new_vpath->path =
|
|
|
|
g_list_append (new_vpath->path,
|
|
|
|
vfs_path_element_clone (vfs_path_get_by_index
|
|
|
|
(vpath, vpath_element_index)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_vpath;
|
|
|
|
}
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/*
|
|
|
|
* Free vfs_path_t object.
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
*
|
|
|
|
*/
|
2011-04-14 16:21:30 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
vfs_path_free (vfs_path_t * path)
|
|
|
|
{
|
|
|
|
if (path == NULL)
|
|
|
|
return;
|
2011-04-28 15:39:59 +04:00
|
|
|
g_list_foreach (path->path, (GFunc) vfs_path_element_free, NULL);
|
|
|
|
g_list_free (path->path);
|
2011-04-18 16:08:13 +04:00
|
|
|
g_free (path);
|
2011-04-14 16:21:30 +04:00
|
|
|
}
|
|
|
|
|
2011-06-08 17:40:36 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*
|
|
|
|
* Remove one path element by index
|
|
|
|
*
|
|
|
|
* @param vpath pointer to vfs_path_t object
|
|
|
|
* @param element_index element index. May have negative value (in this case count was started at the end of list).
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
vfs_path_remove_element_by_index (vfs_path_t * vpath, int element_index)
|
|
|
|
{
|
|
|
|
vfs_path_element_t *element;
|
|
|
|
|
|
|
|
if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (element_index < 0)
|
|
|
|
element_index = vfs_path_elements_count (vpath) + element_index;
|
|
|
|
|
|
|
|
element = g_list_nth_data (vpath->path, element_index);
|
|
|
|
vpath->path = g_list_remove (vpath->path, element);
|
|
|
|
vfs_path_element_free (element);
|
|
|
|
}
|
|
|
|
|
2011-04-14 16:21:30 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-28 15:39:59 +04:00
|
|
|
/** Return VFS class for the given prefix */
|
2011-06-14 12:55:27 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
struct vfs_class *
|
|
|
|
vfs_prefix_to_class (const char *prefix)
|
|
|
|
{
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
/* Avoid first class (localfs) that would accept any prefix */
|
|
|
|
for (i = 1; i < vfs__classes_list->len; i++)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
|
|
|
|
if (vfs->which != NULL)
|
|
|
|
{
|
|
|
|
if (vfs->which (vfs, prefix) == -1)
|
|
|
|
continue;
|
|
|
|
return vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vfs->prefix != NULL && strncmp (prefix, vfs->prefix, strlen (vfs->prefix)) == 0)
|
|
|
|
return vfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-06-01 14:56:52 +04:00
|
|
|
/**
|
|
|
|
* Check if need cleanup charset converter for vfs_path_element_t
|
|
|
|
*
|
|
|
|
* @param element part of path
|
|
|
|
*
|
|
|
|
* @return TRUE if need cleanup converter or FALSE otherwise
|
|
|
|
*/
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
vfs_path_element_need_cleanup_converter (const vfs_path_element_t * element)
|
|
|
|
{
|
|
|
|
return (element->dir.converter != str_cnv_from_term && element->dir.converter != INVALID_CONV);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-06-07 13:22:31 +04:00
|
|
|
/**
|
|
|
|
* Serialize vfs_path_t object to string
|
|
|
|
*
|
|
|
|
* @param vpath data for serialization
|
|
|
|
* @param error contain pointer to object for handle error code and message
|
|
|
|
*
|
|
|
|
* @return serialized vpath as newly allocated string
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
vfs_path_serialize (const vfs_path_t * vpath, GError ** error)
|
|
|
|
{
|
|
|
|
mc_config_t *cpath = mc_config_init (NULL);
|
|
|
|
ssize_t element_index;
|
|
|
|
char *ret_value;
|
|
|
|
|
|
|
|
if ((vpath == NULL) || (vfs_path_elements_count (vpath) == 0))
|
|
|
|
{
|
|
|
|
g_set_error (error, MC_ERROR, -1, "vpath object is empty");
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
for (element_index = 0; element_index < vfs_path_elements_count (vpath); element_index++)
|
|
|
|
{
|
|
|
|
char *groupname = g_strdup_printf ("path-element-%zd", element_index);
|
|
|
|
vfs_path_element_t *element = vfs_path_get_by_index (vpath, element_index);
|
|
|
|
|
|
|
|
/* convert one element to config group */
|
|
|
|
|
|
|
|
mc_config_set_string_raw (cpath, groupname, "path", element->path);
|
|
|
|
mc_config_set_string_raw (cpath, groupname, "class-name", element->class->name);
|
|
|
|
mc_config_set_string_raw (cpath, groupname, "encoding", element->encoding);
|
|
|
|
|
2011-06-13 14:19:47 +04:00
|
|
|
mc_config_set_string_raw (cpath, groupname, "vfs_prefix", element->vfs_prefix);
|
2011-06-07 13:22:31 +04:00
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
mc_config_set_string_raw (cpath, groupname, "user", element->user);
|
|
|
|
mc_config_set_string_raw (cpath, groupname, "password", element->password);
|
|
|
|
mc_config_set_string_raw (cpath, groupname, "host", element->host);
|
2011-06-13 14:19:47 +04:00
|
|
|
if (element->port != 0)
|
|
|
|
mc_config_set_int (cpath, groupname, "port", element->port);
|
2011-06-07 13:22:31 +04:00
|
|
|
|
|
|
|
g_free (groupname);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_value = mc_serialize_config (cpath, error);
|
|
|
|
mc_config_deinit (cpath);
|
|
|
|
return ret_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Deserialize string to vfs_path_t object
|
|
|
|
*
|
|
|
|
* @param data data for serialization
|
|
|
|
* @param error contain pointer to object for handle error code and message
|
|
|
|
*
|
|
|
|
* @return newly allocated vfs_path_t object
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_path_deserialize (const char *data, GError ** error)
|
|
|
|
{
|
|
|
|
mc_config_t *cpath = mc_deserialize_config (data, error);
|
|
|
|
size_t element_index = 0;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
|
|
|
|
if (cpath == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
vpath = vfs_path_new ();
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
vfs_path_element_t *element;
|
|
|
|
char *cfg_value;
|
|
|
|
char *groupname;
|
|
|
|
|
|
|
|
groupname = g_strdup_printf ("path-element-%zd", element_index);
|
|
|
|
if (!mc_config_has_group (cpath, groupname))
|
|
|
|
{
|
|
|
|
g_free (groupname);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
element = g_new0 (vfs_path_element_t, 1);
|
|
|
|
|
|
|
|
cfg_value = mc_config_get_string_raw (cpath, groupname, "class-name", NULL);
|
|
|
|
element->class = vfs_get_class_by_name (cfg_value);
|
|
|
|
if (element->class == NULL)
|
|
|
|
{
|
|
|
|
g_free (element);
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
g_set_error (error, MC_ERROR, -1, "Unable to find VFS class by name '%s'", cfg_value);
|
|
|
|
g_free (cfg_value);
|
|
|
|
mc_config_deinit (cpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
g_free (cfg_value);
|
|
|
|
|
|
|
|
element->path = mc_config_get_string_raw (cpath, groupname, "path", NULL);
|
|
|
|
element->encoding = mc_config_get_string_raw (cpath, groupname, "encoding", NULL);
|
2011-08-15 15:37:59 +04:00
|
|
|
element->dir.converter =
|
|
|
|
(element->encoding != NULL) ? str_crt_conv_from (element->encoding) : INVALID_CONV;
|
2011-06-07 13:22:31 +04:00
|
|
|
|
2011-06-13 14:19:47 +04:00
|
|
|
element->vfs_prefix = mc_config_get_string_raw (cpath, groupname, "vfs_prefix", NULL);
|
2011-06-07 13:22:31 +04:00
|
|
|
|
2011-06-07 14:18:08 +04:00
|
|
|
element->user = mc_config_get_string_raw (cpath, groupname, "user", NULL);
|
|
|
|
element->password = mc_config_get_string_raw (cpath, groupname, "password", NULL);
|
|
|
|
element->host = mc_config_get_string_raw (cpath, groupname, "host", NULL);
|
|
|
|
element->port = mc_config_get_int (cpath, groupname, "port", 0);
|
|
|
|
|
2011-06-07 13:22:31 +04:00
|
|
|
vpath->path = g_list_append (vpath->path, element);
|
|
|
|
|
|
|
|
g_free (groupname);
|
|
|
|
element_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
mc_config_deinit (cpath);
|
|
|
|
if (vfs_path_elements_count (vpath) == 0)
|
|
|
|
{
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
g_set_error (error, MC_ERROR, -1, "No any path elements found");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return vpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-07-17 20:59:47 +04:00
|
|
|
/**
|
|
|
|
* Build vfs_path_t object from arguments.
|
|
|
|
*
|
|
|
|
* return newly allocated vfs_path_t object
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_path_build_filename (const char *first_element, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *str_path;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
|
|
|
|
if (first_element == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start (args, first_element);
|
|
|
|
str_path = mc_build_filenamev (first_element, args);
|
|
|
|
va_end (args);
|
|
|
|
vpath = vfs_path_from_str (str_path);
|
|
|
|
g_free (str_path);
|
|
|
|
return vpath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-07-18 22:07:39 +04:00
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_path_append_new (const vfs_path_t * vpath, const char *first_element, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char *str_path, *result_str;
|
|
|
|
vfs_path_t *ret_vpath;
|
|
|
|
|
|
|
|
if (vpath == NULL || first_element == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
va_start (args, first_element);
|
|
|
|
str_path = mc_build_filenamev (first_element, args);
|
|
|
|
va_end (args);
|
|
|
|
|
|
|
|
result_str = vfs_path_to_str (vpath);
|
|
|
|
ret_vpath = vfs_path_build_filename (result_str, str_path, NULL);
|
|
|
|
g_free (result_str);
|
|
|
|
g_free (str_path);
|
|
|
|
|
|
|
|
return ret_vpath;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|