mc/lib/vfs/vfs.c

774 lines
20 KiB
C
Raw Normal View History

1998-02-27 07:54:42 +03:00
/* Virtual File System switch code
Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2007 Free Software Foundation, Inc.
1998-02-27 07:54:42 +03:00
Written by: 1995 Miguel de Icaza
1995 Jakub Jelinek
1998 Pavel Machek
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.
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
GNU Library General Public License for more details.
1998-02-27 07:54:42 +03: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
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
1998-02-27 07:54:42 +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
* strings you pass to them. This is acutally ok as you g_strdup what
* 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-02-27 07:54:42 +03:00
#include <errno.h>
#include "lib/global.h"
#include "lib/strutil.h"
#include "lib/util.h"
#include "lib/widget.h" /* message() */
#include "lib/event.h"
#ifdef HAVE_CHARSET
#include "lib/charsets.h"
#endif
#include "vfs.h"
#include "utilvfs.h"
#include "gc.h"
extern struct dirent *mc_readdir_result;
/*** global variables ****************************************************************************/
GPtrArray *vfs__classes_list = NULL;
GString *vfs_str_buffer = NULL;
struct vfs_class *current_vfs = NULL;
/*** file scope macro definitions ****************************************************************/
#if defined(_AIX) && !defined(NAME_MAX)
#define NAME_MAX FILENAME_MAX
#endif
#define VFS_FIRST_HANDLE 100
#define ISSLASH(a) (!a || (a == '/'))
/*** file scope type declarations ****************************************************************/
1998-02-27 07:54:42 +03:00
struct vfs_openfile
{
int handle;
struct vfs_class *vclass;
void *fsinfo;
};
/*** file scope variables ************************************************************************/
/** They keep track of the current directory */
static vfs_path_t *current_path = NULL;
static GPtrArray *vfs_openfiles;
static long vfs_free_handle_list = -1;
1998-02-27 07:54:42 +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 ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
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;
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
/** Free open file data for given file handle */
void
vfs_free_handle (int handle)
{
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;
}
}
/* --------------------------------------------------------------------------------------------- */
/** Find private file data by file handle */
void *
vfs_class_data_find_by_handle (int handle)
{
struct vfs_openfile *h;
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->fsinfo;
}
/* --------------------------------------------------------------------------------------------- */
/** Find VFS class by file handle */
struct vfs_class *
vfs_class_find_by_handle (int handle)
{
struct vfs_openfile *h;
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;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Create new VFS handle and put it to the list
*/
int
vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
{
struct vfs_openfile *h;
h = g_new (struct vfs_openfile, 1);
h->fsinfo = fsinfo;
h->vclass = vclass;
/* Allocate the first free handle */
h->handle = vfs_free_handle_list;
if (h->handle == -1)
{
/* No free allocated handles, allocate one */
h->handle = vfs_openfiles->len;
g_ptr_array_add (vfs_openfiles, h);
}
else
{
vfs_free_handle_list = (long) g_ptr_array_index (vfs_openfiles, vfs_free_handle_list);
g_ptr_array_index (vfs_openfiles, h->handle) = h;
}
h->handle += VFS_FIRST_HANDLE;
return h->handle;
}
/* --------------------------------------------------------------------------------------------- */
int
vfs_ferrno (struct vfs_class *vfs)
{
return vfs->ferrno ? (*vfs->ferrno) (vfs) : E_UNKNOWN;
/* Hope that error message is obscure enough ;-) */
}
/* --------------------------------------------------------------------------------------------- */
gboolean
vfs_register_class (struct vfs_class * vfs)
{
if (vfs->init != NULL) /* vfs has own initialization function */
if (!vfs->init (vfs)) /* but it failed */
return FALSE;
g_ptr_array_add (vfs__classes_list, vfs);
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** Strip known vfs suffixes from a filename (possible improvement: strip
* suffix from last path component).
* \return a malloced string which has to be freed.
*/
char *
vfs_strip_suffix_from_filename (const char *filename)
{
guint i;
char *semi;
char *p;
if (filename == NULL)
vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
p = g_strdup (filename);
semi = strrchr (p, '#');
if (semi == NULL)
return p;
/* 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, semi + 1) == -1)
continue;
*semi = '\0'; /* Found valid suffix */
break;
}
if (vfs->prefix != NULL && strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix)) == 0)
{
*semi = '\0'; /* Found valid suffix */
break;
}
}
return p;
}
/* --------------------------------------------------------------------------------------------- */
struct vfs_class *
vfs_get_class (const char *pathname)
{
char *path;
struct vfs_class *vfs;
path = g_strdup (pathname);
vfs = _vfs_get_class (path);
g_free (path);
if (vfs == NULL)
vfs = g_ptr_array_index (vfs__classes_list, 0); /* localfs */
return vfs;
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
const char *
vfs_get_encoding (const char *path)
{
static char result[16];
char *work;
char *semi;
char *slash;
work = g_strdup (path);
/* try found /#enc: */
semi = g_strrstr (work, PATH_SEP_STR VFS_ENCODING_PREFIX);
if (semi != NULL)
{
semi += strlen (VFS_ENCODING_PREFIX) + 1; /* skip "/#enc:" */
slash = strchr (semi, PATH_SEP);
if (slash != NULL)
slash[0] = '\0';
g_strlcpy (result, semi, sizeof (result));
g_free (work);
return result;
}
else
{
g_free (work);
return NULL;
}
}
/* --------------------------------------------------------------------------------------------- */
char *
vfs_translate_path (const char *path)
{
estr_t state;
g_string_set_size (vfs_str_buffer, 0);
state = _vfs_translate_path (path, -1, str_cnv_from_term, vfs_str_buffer);
/*
strict version
return (state == 0) ? vfs_str_buffer->data : NULL;
*/
return (state != ESTR_FAILURE) ? vfs_str_buffer->str : NULL;
}
/* --------------------------------------------------------------------------------------------- */
char *
vfs_translate_path_n (const char *path)
{
char *result;
result = vfs_translate_path (path);
return (result != NULL) ? g_strdup (result) : NULL;
}
/* --------------------------------------------------------------------------------------------- */
char *
vfs_canon_and_translate (const char *path)
{
char *canon;
char *result;
if (path == NULL)
canon = g_strdup ("");
else
canon = vfs_canon (path);
result = vfs_translate_path_n (canon);
g_free (canon);
return result;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Get current directory without any OS calls.
*
* @return string contain current path
*/
char *
vfs_get_current_dir (void)
1998-02-27 07:54:42 +03: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;
}
/* --------------------------------------------------------------------------------------------- */
/**
* remove //, /./ and /../
*/
char *
vfs_canon (const char *path)
{
if (!path)
vfs_die ("Cannot canonicalize NULL");
/* Relative to current directory */
if (*path != PATH_SEP)
{
char *local, *result, *curr_dir;
curr_dir = vfs_get_current_dir ();
local = concat_dir_and_file (curr_dir, path);
g_free (curr_dir);
result = vfs_canon (local);
g_free (local);
return result;
}
/*
* So we have path of following form:
* /p1/p2#op/.././././p3#op/p4. Good luck.
*/
{
char *result = g_strdup (path);
canonicalize_pathname (result);
return result;
}
}
1998-02-27 07:54:42 +03:00
/* --------------------------------------------------------------------------------------------- */
/* Return TRUE is the current VFS class is local */
gboolean
vfs_current_is_local (void)
1998-02-27 07:54:42 +03:00
{
return (current_vfs->flags & VFSF_LOCAL) != 0;
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
/* Return flags of the VFS class of the given filename */
vfs_class_flags_t
vfs_file_class_flags (const char *filename)
1998-02-27 07:54:42 +03:00
{
struct vfs_class *vfs;
char *fname;
1998-02-27 07:54:42 +03:00
fname = vfs_canon_and_translate (filename);
if (fname == NULL)
return VFSF_UNKNOWN;
vfs = vfs_get_class (fname);
g_free (fname);
return vfs->flags;
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
1998-02-27 07:54:42 +03:00
void
vfs_init (void)
1998-02-27 07:54:42 +03:00
{
/* create the VFS handle arrays */
vfs__classes_list = g_ptr_array_new ();
/* create the VFS handle array */
vfs_openfiles = g_ptr_array_new ();
vfs_str_buffer = g_string_new ("");
}
/* --------------------------------------------------------------------------------------------- */
void
vfs_setup_work_dir (void)
{
vfs_path_element_t *path_element;
g_free (_vfs_get_cwd ());
/* FIXME: is we really need for this check? */
/*
if (strlen (current_dir) > MC_MAXPATHLEN - 2)
vfs_die ("Current dir too long.\n");
*/
path_element = vfs_path_get_by_index (current_path, -1);
current_vfs = path_element->class;
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
void
vfs_shut (void)
1998-02-27 07:54:42 +03:00
{
guint i;
1998-02-27 07:54:42 +03:00
vfs_gc_done ();
1998-02-27 07:54:42 +03:00
vfs_set_raw_current_dir (NULL);
1998-02-27 07:54:42 +03:00
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->done != NULL)
vfs->done (vfs);
}
g_ptr_array_free (vfs_openfiles, TRUE);
g_ptr_array_free (vfs__classes_list, TRUE);
g_string_free (vfs_str_buffer, TRUE);
g_free (mc_readdir_result);
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
/**
* These ones grab information from the VFS
1998-02-27 07:54:42 +03:00
* and handles them to an upper layer
*/
void
vfs_fill_names (fill_names_f func)
1998-02-27 07:54:42 +03:00
{
guint i;
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->fill_names != NULL)
vfs->fill_names (vfs, func);
}
1998-02-27 07:54:42 +03:00
}
/* --------------------------------------------------------------------------------------------- */
/**
* Returns vfs path corresponding to given url. If passed string is
* not recognized as url, g_strdup(url) is returned.
*/
1998-10-13 02:07:53 +04:00
char *
vfs_translate_url (const char *url)
1998-10-13 02:07:53 +04:00
{
size_t i;
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);
return g_strdup (url);
1998-10-13 02:07:53 +04:00
}
/* --------------------------------------------------------------------------------------------- */
gboolean
vfs_file_is_local (const char *filename)
{
return (vfs_file_class_flags (filename) & VFSF_LOCAL) != 0;
}
/* --------------------------------------------------------------------------------------------- */
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);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Return current directory. If it's local, reread the current directory
* from the OS. You must g_strdup() whatever this function returns.
*/
char *
_vfs_get_cwd (void)
{
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);
}
curr_dir = vfs_get_current_dir ();
trans = vfs_translate_path_n (curr_dir);
if (_vfs_get_class (trans) == NULL)
{
const char *encoding = vfs_get_encoding (curr_dir);
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);
if (state == ESTR_SUCCESS)
{
struct stat my_stat, my_stat2;
/* Check if it is O.K. to use the current_dir */
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))
{
vfs_set_raw_current_dir (vfs_path_from_str (vfs_str_buffer->str));
}
}
}
}
}
g_free (trans);
return curr_dir;
}
/* --------------------------------------------------------------------------------------------- */