1998-02-27 07:54:42 +03:00
|
|
|
/* Virtual File System switch code
|
2007-09-25 19:33:35 +04:00
|
|
|
Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
2007 Free Software Foundation, Inc.
|
2010-03-30 12:10:25 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
Written by: 1995 Miguel de Icaza
|
2010-03-30 12:10:25 +04:00
|
|
|
1995 Jakub Jelinek
|
|
|
|
1998 Pavel Machek
|
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
2010-03-30 12:10:25 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
This program 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
|
1998-09-27 23:27:58 +04:00
|
|
|
GNU Library General Public License for more details.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Source: Virtual File System switch code
|
|
|
|
* \author Miguel de Icaza
|
|
|
|
* \author Jakub Jelinek
|
|
|
|
* \author Pavel Machek
|
|
|
|
* \date 1995, 1998
|
|
|
|
* \warning funtions like extfs_lstat() have right to destroy any
|
2009-02-06 01:40:32 +03:00
|
|
|
* strings you pass to them. This is acutally ok as you g_strdup what
|
2009-02-07 15:54:58 +03:00
|
|
|
* you are passing to them, anyway; still, beware.
|
|
|
|
*
|
|
|
|
* Namespace: exports *many* functions with vfs_ prefix; exports
|
|
|
|
* parse_ls_lga and friends which do not have that prefix.
|
|
|
|
*/
|
1998-10-13 02:07:53 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <errno.h>
|
1999-01-21 01:01:11 +03:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2010-01-21 15:17:26 +03:00
|
|
|
#include "lib/strutil.h"
|
2010-11-11 16:58:29 +03:00
|
|
|
#include "lib/util.h"
|
2010-11-12 11:03:57 +03:00
|
|
|
#include "lib/widget.h" /* message() */
|
2011-02-16 14:26:59 +03:00
|
|
|
#include "lib/event.h"
|
2010-07-22 12:07:07 +04:00
|
|
|
|
2010-05-19 10:30:37 +04:00
|
|
|
#ifdef HAVE_CHARSET
|
2010-11-18 11:31:09 +03:00
|
|
|
#include "lib/charsets.h"
|
2010-05-19 10:30:37 +04:00
|
|
|
#endif
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
#include "vfs.h"
|
1999-01-21 01:01:11 +03:00
|
|
|
#include "utilvfs.h"
|
2003-11-08 02:43:55 +03:00
|
|
|
#include "gc.h"
|
2010-07-22 12:07:07 +04:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
extern struct dirent *mc_readdir_result;
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
GPtrArray *vfs__classes_list = NULL;
|
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
GString *vfs_str_buffer = NULL;
|
|
|
|
struct vfs_class *current_vfs = NULL;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2010-01-17 15:51:07 +03:00
|
|
|
#if defined(_AIX) && !defined(NAME_MAX)
|
2010-11-08 13:21:45 +03:00
|
|
|
#define NAME_MAX FILENAME_MAX
|
2010-01-17 15:51:07 +03:00
|
|
|
#endif
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
#define VFS_FIRST_HANDLE 100
|
|
|
|
|
|
|
|
#define ISSLASH(a) (!a || (a == '/'))
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
struct vfs_openfile
|
|
|
|
{
|
2003-11-05 03:12:25 +03:00
|
|
|
int handle;
|
|
|
|
struct vfs_class *vclass;
|
|
|
|
void *fsinfo;
|
|
|
|
};
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
/** They keep track of the current directory */
|
|
|
|
static vfs_path_t *current_path = NULL;
|
2008-12-29 02:54:37 +03:00
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
static GPtrArray *vfs_openfiles;
|
|
|
|
static long vfs_free_handle_list = -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
static const struct
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
size_t name_len;
|
|
|
|
const char *substitute;
|
|
|
|
} url_table[] =
|
|
|
|
{
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
#ifdef ENABLE_VFS_FTP
|
|
|
|
{ "ftp://", 6, "/#ftp:" },
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_VFS_FISH
|
|
|
|
{ "sh://", 5, "/#sh:" },
|
|
|
|
{ "ssh://", 6, "/#sh:" },
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_VFS_SMB
|
|
|
|
{ "smb://", 6, "/#smb:" },
|
|
|
|
#endif
|
|
|
|
{ "a:", 2, "/#a" }
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
};
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
1998-09-27 23:27:58 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
static gboolean
|
|
|
|
path_magic (const char *path)
|
|
|
|
{
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
return (stat (path, &buf) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static struct vfs_class *
|
|
|
|
_vfs_get_class (char *path)
|
|
|
|
{
|
|
|
|
char *semi;
|
|
|
|
char *slash;
|
|
|
|
struct vfs_class *ret;
|
|
|
|
|
|
|
|
g_return_val_if_fail (path, NULL);
|
|
|
|
|
|
|
|
semi = strrchr (path, '#');
|
|
|
|
if (semi == NULL || !path_magic (path))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
slash = strchr (semi, PATH_SEP);
|
|
|
|
*semi = '\0';
|
|
|
|
if (slash != NULL)
|
|
|
|
*slash = '\0';
|
|
|
|
|
|
|
|
ret = vfs_prefix_to_class (semi + 1);
|
|
|
|
|
|
|
|
if (slash != NULL)
|
|
|
|
*slash = PATH_SEP;
|
|
|
|
if (ret == NULL)
|
|
|
|
ret = _vfs_get_class (path);
|
|
|
|
|
|
|
|
*semi = '#';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/* now used only by vfs_translate_path, but could be used in other vfs
|
|
|
|
* plugin to automatic detect encoding
|
|
|
|
* path - path to translate
|
|
|
|
* size - how many bytes from path translate
|
|
|
|
* defcnv - convertor, that is used as default, when path does not contain any
|
|
|
|
* #enc: subtring
|
|
|
|
* buffer - used to store result of translation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static estr_t
|
|
|
|
_vfs_translate_path (const char *path, int size, GIConv defcnv, GString * buffer)
|
|
|
|
{
|
|
|
|
const char *semi;
|
|
|
|
const char *slash;
|
|
|
|
estr_t state = ESTR_SUCCESS;
|
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
return ESTR_SUCCESS;
|
|
|
|
|
|
|
|
size = (size > 0) ? size : (signed int) strlen (path);
|
|
|
|
|
|
|
|
/* try found /#enc: */
|
|
|
|
semi = g_strrstr_len (path, size, PATH_SEP_STR VFS_ENCODING_PREFIX);
|
|
|
|
if (semi != NULL)
|
|
|
|
{
|
|
|
|
char encoding[16];
|
|
|
|
GIConv coder = INVALID_CONV;
|
|
|
|
int ms;
|
|
|
|
|
|
|
|
/* first must be translated part before /#enc: */
|
|
|
|
ms = semi - path;
|
|
|
|
|
|
|
|
state = _vfs_translate_path (path, ms, defcnv, buffer);
|
|
|
|
|
|
|
|
if (state != ESTR_SUCCESS)
|
|
|
|
return state;
|
|
|
|
|
|
|
|
/* now can be translated part after #enc: */
|
|
|
|
semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */
|
|
|
|
slash = strchr (semi, PATH_SEP);
|
|
|
|
/* ignore slashes after size; */
|
|
|
|
if (slash - path >= size)
|
|
|
|
slash = NULL;
|
|
|
|
|
|
|
|
ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
|
|
|
|
ms = min ((unsigned int) ms, sizeof (encoding) - 1);
|
|
|
|
/* limit encoding size (ms) to path size (size) */
|
|
|
|
if (semi + ms > path + size)
|
|
|
|
ms = path + size - semi;
|
|
|
|
memcpy (encoding, semi, ms);
|
|
|
|
encoding[ms] = '\0';
|
|
|
|
|
|
|
|
#if HAVE_CHARSET
|
|
|
|
if (is_supported_encoding (encoding))
|
|
|
|
coder = str_crt_conv_to (encoding);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (coder != INVALID_CONV)
|
|
|
|
{
|
|
|
|
if (slash != NULL)
|
|
|
|
state = str_vfs_convert_to (coder, slash, path + size - slash, buffer);
|
|
|
|
else if (buffer->len == 0)
|
|
|
|
g_string_append_c (buffer, PATH_SEP);
|
|
|
|
str_close_conv (coder);
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = EINVAL;
|
|
|
|
state = ESTR_FAILURE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* path can be translated whole at once */
|
|
|
|
state = str_vfs_convert_to (defcnv, path, size, buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-07 16:27:46 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Free open file data for given file handle */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
void
|
|
|
|
vfs_free_handle (int handle)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
const int idx = handle - VFS_FIRST_HANDLE;
|
|
|
|
|
|
|
|
if (handle >= VFS_FIRST_HANDLE && (guint) idx < vfs_openfiles->len)
|
|
|
|
{
|
|
|
|
struct vfs_openfile *h;
|
|
|
|
|
|
|
|
h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, idx);
|
|
|
|
g_free (h);
|
|
|
|
g_ptr_array_index (vfs_openfiles, idx) = (void *) vfs_free_handle_list;
|
|
|
|
vfs_free_handle_list = idx;
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-07 16:27:46 +04:00
|
|
|
/** Find private file data by file handle */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
void *
|
|
|
|
vfs_class_data_find_by_handle (int handle)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
struct vfs_openfile *h;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
|
|
|
|
return NULL;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
|
|
|
|
if (!h)
|
|
|
|
return NULL;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
g_assert (h->handle == handle);
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
return h->fsinfo;
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Find VFS class by file handle */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
struct vfs_class *
|
|
|
|
vfs_class_find_by_handle (int handle)
|
|
|
|
{
|
|
|
|
struct vfs_openfile *h;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
if (handle < VFS_FIRST_HANDLE || (guint) (handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
h = (struct vfs_openfile *) g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE);
|
|
|
|
if (!h)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
g_assert (h->handle == handle);
|
|
|
|
|
|
|
|
return h->vclass;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/**
|
|
|
|
* Create new VFS handle and put it to the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
struct vfs_openfile *h;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
h = g_new (struct vfs_openfile, 1);
|
|
|
|
h->fsinfo = fsinfo;
|
|
|
|
h->vclass = vclass;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/* Allocate the first free handle */
|
|
|
|
h->handle = vfs_free_handle_list;
|
|
|
|
if (h->handle == -1)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
/* No free allocated handles, allocate one */
|
|
|
|
h->handle = vfs_openfiles->len;
|
|
|
|
g_ptr_array_add (vfs_openfiles, h);
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
|
|
|
|
g_ptr_array_index (vfs_openfiles, h->handle) = h;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
h->handle += VFS_FIRST_HANDLE;
|
|
|
|
return h->handle;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
int
|
|
|
|
vfs_ferrno (struct vfs_class *vfs)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
|
|
|
|
/* Hope that error message is obscure enough ;-) */
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
gboolean
|
|
|
|
vfs_register_class (struct vfs_class * vfs)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
if (vfs->init != NULL) /* vfs has own initialization function */
|
2011-02-15 16:44:17 +03:00
|
|
|
if (!vfs->init (vfs)) /* but it failed */
|
|
|
|
return FALSE;
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
g_ptr_array_add (vfs__classes_list, vfs);
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
return TRUE;
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Strip known vfs suffixes from a filename (possible improvement: strip
|
|
|
|
* suffix from last path component).
|
|
|
|
* \return a malloced string which has to be freed.
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
1998-12-16 09:16:13 +03:00
|
|
|
char *
|
2001-03-05 09:00:27 +03:00
|
|
|
vfs_strip_suffix_from_filename (const char *filename)
|
1998-12-16 09:16:13 +03:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
guint i;
|
1998-12-16 09:16:13 +03:00
|
|
|
char *semi;
|
|
|
|
char *p;
|
|
|
|
|
2010-03-30 22:59:41 +04:00
|
|
|
if (filename == NULL)
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
|
2003-09-29 00:35:08 +04:00
|
|
|
|
2009-02-06 01:40:32 +03:00
|
|
|
p = g_strdup (filename);
|
2010-03-30 22:59:41 +04:00
|
|
|
semi = strrchr (p, '#');
|
|
|
|
if (semi == NULL)
|
2010-03-30 12:10:25 +04:00
|
|
|
return p;
|
1998-12-16 09:16:13 +03:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
/* Avoid first class (localfs) that would accept any prefix */
|
2011-04-28 15:39:59 +04:00
|
|
|
for (i = 1; i < vfs__classes_list->len; i++)
|
2010-03-30 12:10:25 +04:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
|
2011-02-15 16:44:17 +03:00
|
|
|
|
2010-03-30 22:59:41 +04:00
|
|
|
if (vfs->which != NULL)
|
2010-03-30 12:10:25 +04:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
if (vfs->which (vfs, semi + 1) == -1)
|
2010-03-30 12:10:25 +04:00
|
|
|
continue;
|
|
|
|
*semi = '\0'; /* Found valid suffix */
|
2011-02-15 16:44:17 +03:00
|
|
|
break;
|
2010-03-30 12:10:25 +04:00
|
|
|
}
|
2011-02-15 16:44:17 +03:00
|
|
|
|
2010-03-30 22:59:41 +04:00
|
|
|
if (vfs->prefix != NULL && strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix)) == 0)
|
2010-03-30 12:10:25 +04:00
|
|
|
{
|
|
|
|
*semi = '\0'; /* Found valid suffix */
|
2011-02-15 16:44:17 +03:00
|
|
|
break;
|
2010-03-30 12:10:25 +04:00
|
|
|
}
|
1998-12-16 09:16:13 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
1998-08-25 20:00:16 +04:00
|
|
|
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *
|
2004-10-25 16:22:52 +04:00
|
|
|
vfs_get_class (const char *pathname)
|
1998-05-26 04:53:24 +04:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
char *path;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
path = g_strdup (pathname);
|
2004-10-25 16:22:52 +04:00
|
|
|
vfs = _vfs_get_class (path);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (path);
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
if (vfs == NULL)
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs = g_ptr_array_index (vfs__classes_list, 0); /* localfs */
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
return vfs;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-03-30 12:10:25 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
const char *
|
|
|
|
vfs_get_encoding (const char *path)
|
|
|
|
{
|
|
|
|
static char result[16];
|
|
|
|
char *work;
|
|
|
|
char *semi;
|
|
|
|
char *slash;
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
work = g_strdup (path);
|
|
|
|
/* try found /#enc: */
|
|
|
|
semi = g_strrstr (work, PATH_SEP_STR VFS_ENCODING_PREFIX);
|
2010-05-19 10:30:37 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (semi != NULL)
|
|
|
|
{
|
|
|
|
semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */
|
|
|
|
slash = strchr (semi, PATH_SEP);
|
|
|
|
if (slash != NULL)
|
|
|
|
slash[0] = '\0';
|
2010-05-19 10:30:37 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
g_strlcpy (result, semi, sizeof (result));
|
|
|
|
g_free (work);
|
|
|
|
return result;
|
2010-03-30 12:10:25 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
g_free (work);
|
|
|
|
return NULL;
|
2008-12-29 02:54:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
char *
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_translate_path (const char *path)
|
2008-12-29 02:54:37 +03:00
|
|
|
{
|
2009-04-22 20:35:32 +04:00
|
|
|
estr_t state;
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
g_string_set_size (vfs_str_buffer, 0);
|
2008-12-29 02:54:37 +03:00
|
|
|
state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
|
2009-04-24 02:47:22 +04:00
|
|
|
/*
|
2010-03-30 12:10:25 +04:00
|
|
|
strict version
|
|
|
|
return (state == 0) ? vfs_str_buffer->data : NULL;
|
|
|
|
*/
|
2009-04-14 14:29:01 +04:00
|
|
|
return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
|
2008-12-29 02:54:37 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
char *
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_translate_path_n (const char *path)
|
2008-12-29 02:54:37 +03:00
|
|
|
{
|
|
|
|
char *result;
|
2009-08-03 19:37:55 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
result = vfs_translate_path (path);
|
|
|
|
return (result != NULL) ? g_strdup (result) : NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
char *
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_canon_and_translate (const char *path)
|
2008-12-29 02:54:37 +03:00
|
|
|
{
|
|
|
|
char *canon;
|
|
|
|
char *result;
|
2009-08-03 19:37:55 +04:00
|
|
|
if (path == NULL)
|
|
|
|
canon = g_strdup ("");
|
|
|
|
else
|
|
|
|
canon = vfs_canon (path);
|
2008-12-29 02:54:37 +03:00
|
|
|
result = vfs_translate_path_n (canon);
|
|
|
|
g_free (canon);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-07 16:27:46 +04:00
|
|
|
/**
|
2011-04-29 15:13:29 +04:00
|
|
|
* Get current directory without any OS calls.
|
|
|
|
*
|
|
|
|
* @return string contain current path
|
2011-04-07 16:27:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
vfs_get_current_dir (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
return vfs_path_to_str (current_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Get raw current directory object without any OS calls.
|
|
|
|
*
|
|
|
|
* @return object contain current path
|
|
|
|
*/
|
|
|
|
|
|
|
|
vfs_path_t *
|
|
|
|
vfs_get_raw_current_dir (void)
|
|
|
|
{
|
|
|
|
return current_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Set current directory object.
|
|
|
|
*
|
|
|
|
* @param vpath new path
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vfs_set_raw_current_dir (const vfs_path_t * vpath)
|
|
|
|
{
|
|
|
|
if (current_path != NULL)
|
|
|
|
vfs_path_free (current_path);
|
|
|
|
current_path = (vfs_path_t *) vpath;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2000-02-24 15:00:30 +03:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* remove //, /./ and /../
|
|
|
|
*/
|
2010-03-30 12:10:25 +04:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
char *
|
|
|
|
vfs_canon (const char *path)
|
|
|
|
{
|
|
|
|
if (!path)
|
|
|
|
vfs_die ("Cannot canonicalize NULL");
|
2010-03-30 12:10:25 +04:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/* Relative to current directory */
|
|
|
|
if (*path != PATH_SEP)
|
2010-03-31 13:07:39 +04:00
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
char *local, *result, *curr_dir;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
curr_dir = vfs_get_current_dir ();
|
|
|
|
local = concat_dir_and_file (curr_dir, path);
|
|
|
|
g_free (curr_dir);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
result = vfs_canon (local);
|
|
|
|
g_free (local);
|
|
|
|
return result;
|
2010-03-30 12:10:25 +04:00
|
|
|
}
|
2010-03-31 13:07:39 +04:00
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
/*
|
|
|
|
* So we have path of following form:
|
|
|
|
* /p1/p2#op/.././././p3#op/p4. Good luck.
|
|
|
|
*/
|
2010-03-31 13:07:39 +04:00
|
|
|
{
|
2011-04-07 16:27:46 +04:00
|
|
|
char *result = g_strdup (path);
|
|
|
|
canonicalize_pathname (result);
|
|
|
|
return result;
|
2010-03-31 13:07:39 +04:00
|
|
|
}
|
2003-11-05 03:12:25 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-07 16:27:46 +04:00
|
|
|
/* Return TRUE is the current VFS class is local */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2010-07-08 14:24:58 +04:00
|
|
|
gboolean
|
1998-10-14 06:56:18 +04:00
|
|
|
vfs_current_is_local (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-11 12:21:16 +04:00
|
|
|
return (current_vfs->flags & VFSF_LOCAL) != 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-10-11 12:21:16 +04:00
|
|
|
/* Return flags of the VFS class of the given filename */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2010-07-08 14:24:58 +04:00
|
|
|
vfs_class_flags_t
|
2003-10-11 12:21:16 +04:00
|
|
|
vfs_file_class_flags (const char *filename)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-11 12:21:16 +04:00
|
|
|
struct vfs_class *vfs;
|
|
|
|
char *fname;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
fname = vfs_canon_and_translate (filename);
|
2010-03-31 13:07:39 +04:00
|
|
|
if (fname == NULL)
|
2010-07-08 14:24:58 +04:00
|
|
|
return VFSF_UNKNOWN;
|
2010-03-31 13:07:39 +04:00
|
|
|
|
|
|
|
vfs = vfs_get_class (fname);
|
|
|
|
g_free (fname);
|
|
|
|
return vfs->flags;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
void
|
|
|
|
vfs_init (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
/* create the VFS handle arrays */
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs__classes_list = g_ptr_array_new ();
|
2011-02-15 16:44:17 +03:00
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
/* create the VFS handle array */
|
|
|
|
vfs_openfiles = g_ptr_array_new ();
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_str_buffer = g_string_new ("");
|
2003-10-13 02:25:53 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
1998-09-27 23:27:58 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
void
|
|
|
|
vfs_setup_work_dir (void)
|
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
vfs_path_element_t *path_element;
|
|
|
|
|
|
|
|
g_free (_vfs_get_cwd ());
|
2011-02-15 16:44:17 +03:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
/* FIXME: is we really need for this check? */
|
|
|
|
/*
|
|
|
|
if (strlen (current_dir) > MC_MAXPATHLEN - 2)
|
|
|
|
vfs_die ("Current dir too long.\n");
|
|
|
|
*/
|
2011-02-15 16:44:17 +03:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
path_element = vfs_path_get_by_index (current_path, -1);
|
|
|
|
current_vfs = path_element->class;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
void
|
|
|
|
vfs_shut (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
guint i;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
vfs_gc_done ();
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
vfs_set_raw_current_dir (NULL);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
for (i = 0; i < vfs__classes_list->len; i++)
|
2011-02-15 16:44:17 +03:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
|
2011-02-15 16:44:17 +03:00
|
|
|
|
|
|
|
if (vfs->done != NULL)
|
|
|
|
vfs->done (vfs);
|
|
|
|
}
|
2003-11-05 03:12:25 +03:00
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
g_ptr_array_free (vfs_openfiles, TRUE);
|
2011-04-28 15:39:59 +04:00
|
|
|
g_ptr_array_free (vfs__classes_list, TRUE);
|
2009-04-14 14:29:01 +04:00
|
|
|
g_string_free (vfs_str_buffer, TRUE);
|
2009-10-29 22:04:44 +03:00
|
|
|
g_free (mc_readdir_result);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
1998-10-14 06:56:18 +04:00
|
|
|
* These ones grab information from the VFS
|
1998-02-27 07:54:42 +03:00
|
|
|
* and handles them to an upper layer
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
void
|
2004-08-16 20:34:11 +04:00
|
|
|
vfs_fill_names (fill_names_f func)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2011-02-15 16:44:17 +03:00
|
|
|
guint i;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
for (i = 0; i < vfs__classes_list->len; i++)
|
2011-02-15 16:44:17 +03:00
|
|
|
{
|
2011-04-28 15:39:59 +04:00
|
|
|
struct vfs_class *vfs = (struct vfs_class *) g_ptr_array_index (vfs__classes_list, i);
|
2011-02-15 16:44:17 +03:00
|
|
|
|
|
|
|
if (vfs->fill_names != NULL)
|
|
|
|
vfs->fill_names (vfs, func);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
1998-10-14 06:56:18 +04:00
|
|
|
* Returns vfs path corresponding to given url. If passed string is
|
2009-02-06 01:40:32 +03:00
|
|
|
* not recognized as url, g_strdup(url) is returned.
|
1998-10-14 06:56:18 +04:00
|
|
|
*/
|
2005-02-12 17:30:48 +03:00
|
|
|
|
1998-10-13 02:07:53 +04:00
|
|
|
char *
|
2003-10-11 11:38:05 +04:00
|
|
|
vfs_translate_url (const char *url)
|
1998-10-13 02:07:53 +04:00
|
|
|
{
|
2005-02-12 17:30:48 +03:00
|
|
|
size_t i;
|
|
|
|
|
2010-03-30 12:10:25 +04:00
|
|
|
for (i = 0; i < sizeof (url_table) / sizeof (url_table[0]); i++)
|
|
|
|
if (strncmp (url, url_table[i].name, url_table[i].name_len) == 0)
|
|
|
|
return g_strconcat (url_table[i].substitute, url + url_table[i].name_len,
|
|
|
|
(char *) NULL);
|
2005-02-12 17:30:48 +03:00
|
|
|
|
2009-02-06 01:40:32 +03:00
|
|
|
return g_strdup (url);
|
1998-10-13 02:07:53 +04:00
|
|
|
}
|
2004-08-17 03:25:49 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-07-08 14:24:58 +04:00
|
|
|
gboolean
|
2010-03-30 12:10:25 +04:00
|
|
|
vfs_file_is_local (const char *filename)
|
2004-08-17 03:25:49 +04:00
|
|
|
{
|
2010-07-08 14:24:58 +04:00
|
|
|
return (vfs_file_class_flags (filename) & VFSF_LOCAL) != 0;
|
2004-08-17 03:25:49 +04:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-02-16 14:26:59 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
vfs_print_message (const char *msg, ...)
|
|
|
|
{
|
|
|
|
ev_vfs_print_message_t event_data;
|
|
|
|
|
|
|
|
va_start (event_data.ap, msg);
|
|
|
|
event_data.msg = msg;
|
|
|
|
|
|
|
|
mc_event_raise (MCEVENT_GROUP_CORE, "vfs_print_message", (gpointer) & event_data);
|
|
|
|
va_end (event_data.ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-04-07 16:27:46 +04:00
|
|
|
/**
|
|
|
|
* Return current directory. If it's local, reread the current directory
|
|
|
|
* from the OS. You must g_strdup() whatever this function returns.
|
|
|
|
*/
|
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
char *
|
2011-04-07 16:27:46 +04:00
|
|
|
_vfs_get_cwd (void)
|
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
char *trans, *curr_dir;
|
|
|
|
|
|
|
|
if (vfs_get_raw_current_dir() == NULL)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
tmp = g_get_current_dir ();
|
|
|
|
vfs_set_raw_current_dir (vfs_path_from_str (tmp));
|
|
|
|
g_free(tmp);
|
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
curr_dir = vfs_get_current_dir ();
|
|
|
|
trans = vfs_translate_path_n (curr_dir);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
if (_vfs_get_class (trans) == NULL)
|
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
const char *encoding = vfs_get_encoding (curr_dir);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
if (encoding == NULL)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
tmp = g_get_current_dir ();
|
|
|
|
if (tmp != NULL)
|
|
|
|
{ /* One of the directories in the path is not readable */
|
|
|
|
estr_t state;
|
|
|
|
|
|
|
|
g_string_set_size (vfs_str_buffer, 0);
|
|
|
|
state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
|
|
|
|
g_free (tmp);
|
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
if (state == ESTR_SUCCESS)
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
|
|
|
struct stat my_stat, my_stat2;
|
|
|
|
/* Check if it is O.K. to use the current_dir */
|
2011-04-29 15:13:29 +04:00
|
|
|
if (!(mc_global.vfs.cd_symlinks
|
|
|
|
&& mc_stat (vfs_str_buffer->str, &my_stat) == 0
|
|
|
|
&& mc_stat (curr_dir, &my_stat2) == 0
|
|
|
|
&& my_stat.st_ino == my_stat2.st_ino
|
|
|
|
&& my_stat.st_dev == my_stat2.st_dev))
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
vfs_set_raw_current_dir (vfs_path_from_str (vfs_str_buffer->str));
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (trans);
|
2011-04-29 15:13:29 +04:00
|
|
|
return curr_dir;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|