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.
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
Written by: 1995 Miguel de Icaza
|
|
|
|
1995 Jakub Jelinek
|
1998-05-26 04:53:24 +04:00
|
|
|
1998 Pavel Machek
|
1998-02-27 07:54:42 +03:00
|
|
|
|
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.
|
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 <stdio.h>
|
2009-05-08 14:01:05 +04:00
|
|
|
#include <stdlib.h> /* For atol() */
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
2003-10-11 13:36:29 +04:00
|
|
|
#include <ctype.h> /* is_digit() */
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
1999-01-21 01:01:11 +03:00
|
|
|
|
2005-02-19 19:36:38 +03:00
|
|
|
#include "../src/global.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2005-02-19 19:36:38 +03:00
|
|
|
#include "../src/wtools.h" /* message() */
|
|
|
|
#include "../src/main.h" /* print_vfs_message */
|
2008-12-29 02:54:37 +03:00
|
|
|
#include "../src/strutil.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
#include "utilvfs.h"
|
2003-11-08 02:43:55 +03:00
|
|
|
#include "gc.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
#include "vfs.h"
|
|
|
|
#ifdef USE_NETCODE
|
|
|
|
# include "tcputil.h"
|
|
|
|
#endif
|
2004-08-17 13:17:43 +04:00
|
|
|
#include "ftpfs.h"
|
|
|
|
#include "mcfs.h"
|
|
|
|
#include "smbfs.h"
|
|
|
|
#include "local.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** They keep track of the current directory */
|
2003-10-12 02:17:52 +04:00
|
|
|
static struct vfs_class *current_vfs;
|
|
|
|
static char *current_dir;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
struct vfs_openfile {
|
|
|
|
int handle;
|
|
|
|
struct vfs_class *vclass;
|
|
|
|
void *fsinfo;
|
|
|
|
};
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
struct vfs_dirinfo{
|
|
|
|
DIR *info;
|
2009-04-15 18:11:00 +04:00
|
|
|
GIConv converter;
|
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;
|
2003-11-05 03:12:25 +03:00
|
|
|
#define VFS_FIRST_HANDLE 100
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-10-12 23:57:27 +04:00
|
|
|
static struct vfs_class *localfs_class;
|
2009-04-14 14:29:01 +04:00
|
|
|
static GString *vfs_str_buffer;
|
2008-12-29 02:54:37 +03:00
|
|
|
|
|
|
|
static const char *supported_encodings[] = {
|
|
|
|
"UTF8",
|
|
|
|
"UTF-8",
|
|
|
|
"BIG5",
|
|
|
|
"ASCII",
|
|
|
|
"ISO8859",
|
|
|
|
"ISO-8859",
|
|
|
|
"ISO_8859",
|
|
|
|
"KOI8",
|
|
|
|
"CP852",
|
|
|
|
"CP866",
|
|
|
|
"CP125",
|
|
|
|
NULL
|
|
|
|
};
|
2003-10-12 23:57:27 +04:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Create new VFS handle and put it to the list */
|
1998-10-14 06:56:18 +04:00
|
|
|
static int
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_new_handle (struct vfs_class *vclass, void *fsinfo)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-11-05 03:12:25 +03:00
|
|
|
struct vfs_openfile *h;
|
|
|
|
|
|
|
|
h = g_new (struct vfs_openfile, 1);
|
|
|
|
h->fsinfo = fsinfo;
|
|
|
|
h->vclass = vclass;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
/* 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;
|
2003-11-05 03:12:25 +03:00
|
|
|
}
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Find VFS class by file handle */
|
2009-08-31 05:54:10 +04:00
|
|
|
static struct vfs_class *
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_op (int handle)
|
|
|
|
{
|
|
|
|
struct vfs_openfile *h;
|
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
if (handle < VFS_FIRST_HANDLE ||
|
2009-06-30 15:10:42 +04:00
|
|
|
(guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
|
2009-05-14 18:52:10 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
h = (struct vfs_openfile *) g_ptr_array_index (
|
|
|
|
vfs_openfiles, handle - VFS_FIRST_HANDLE);
|
2003-11-05 03:12:25 +03:00
|
|
|
if (!h)
|
|
|
|
return NULL;
|
2009-05-14 18:52:10 +04:00
|
|
|
|
|
|
|
g_assert (h->handle == handle);
|
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
return h->vclass;
|
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Find private file data by file handle */
|
2009-08-31 05:54:10 +04:00
|
|
|
static void *
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_info (int handle)
|
|
|
|
{
|
|
|
|
struct vfs_openfile *h;
|
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
if (handle < VFS_FIRST_HANDLE ||
|
2009-06-30 15:10:42 +04:00
|
|
|
(guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
|
2009-05-14 18:52:10 +04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
h = (struct vfs_openfile *) g_ptr_array_index (
|
|
|
|
vfs_openfiles, handle - VFS_FIRST_HANDLE);
|
2003-11-05 03:12:25 +03:00
|
|
|
if (!h)
|
|
|
|
return NULL;
|
2009-05-14 18:52:10 +04:00
|
|
|
|
|
|
|
g_assert (h->handle == handle);
|
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
return h->fsinfo;
|
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Free open file data for given file handle */
|
2009-08-31 05:54:10 +04:00
|
|
|
static void
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_free_handle (int handle)
|
|
|
|
{
|
2009-05-14 18:52:10 +04:00
|
|
|
if (handle < VFS_FIRST_HANDLE ||
|
2009-06-30 15:10:42 +04:00
|
|
|
(guint)(handle - VFS_FIRST_HANDLE) >= vfs_openfiles->len)
|
2009-05-14 18:52:10 +04:00
|
|
|
return;
|
2003-11-05 03:12:25 +03:00
|
|
|
|
2009-05-14 18:52:10 +04:00
|
|
|
g_ptr_array_index (vfs_openfiles, handle - VFS_FIRST_HANDLE) =
|
|
|
|
(void *) vfs_free_handle_list;
|
|
|
|
vfs_free_handle_list = handle - VFS_FIRST_HANDLE;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-10-12 04:24:00 +04:00
|
|
|
static struct vfs_class *vfs_list;
|
1998-09-27 23:27:58 +04:00
|
|
|
|
2003-10-11 03:13:09 +04:00
|
|
|
int
|
2003-10-11 20:54:46 +04:00
|
|
|
vfs_register_class (struct vfs_class *vfs)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2000-02-24 15:00:30 +03:00
|
|
|
if (vfs->init) /* vfs has own initialization function */
|
|
|
|
if (!(*vfs->init)(vfs)) /* but it failed */
|
|
|
|
return 0;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
vfs->next = vfs_list;
|
|
|
|
vfs_list = vfs;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/** Return VFS class for the given prefix */
|
2003-10-12 02:17:52 +04:00
|
|
|
static struct vfs_class *
|
2004-09-26 15:06:38 +04:00
|
|
|
vfs_prefix_to_class (char *prefix)
|
1998-09-27 23:27:58 +04:00
|
|
|
{
|
2003-10-12 02:17:52 +04:00
|
|
|
struct vfs_class *vfs;
|
2003-09-29 00:35:08 +04:00
|
|
|
|
2004-11-08 03:04:32 +03:00
|
|
|
/* Avoid last class (localfs) that would accept any prefix */
|
|
|
|
for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
|
2003-09-29 00:35:08 +04:00
|
|
|
if (vfs->which) {
|
2003-10-12 02:17:52 +04:00
|
|
|
if ((*vfs->which) (vfs, prefix) == -1)
|
1998-12-09 23:22:53 +03:00
|
|
|
continue;
|
|
|
|
return vfs;
|
1998-09-27 23:27:58 +04:00
|
|
|
}
|
2003-09-29 00:35:08 +04:00
|
|
|
if (vfs->prefix
|
2004-09-26 15:06:38 +04:00
|
|
|
&& !strncmp (prefix, vfs->prefix, strlen (vfs->prefix)))
|
1998-09-27 23:27:58 +04:00
|
|
|
return vfs;
|
|
|
|
}
|
2003-09-29 00:35:08 +04:00
|
|
|
return NULL;
|
1998-02-27 07:54:42 +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.
|
|
|
|
*/
|
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
|
|
|
{
|
2003-10-12 02:17:52 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-12-16 09:16:13 +03:00
|
|
|
char *semi;
|
|
|
|
char *p;
|
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!filename)
|
2003-09-29 00:35:08 +04:00
|
|
|
vfs_die ("vfs_strip_suffix_from_path got NULL: impossible");
|
|
|
|
|
2009-02-06 01:40:32 +03:00
|
|
|
p = g_strdup (filename);
|
1998-12-16 09:16:13 +03:00
|
|
|
if (!(semi = strrchr (p, '#')))
|
|
|
|
return p;
|
|
|
|
|
2003-10-12 02:17:52 +04:00
|
|
|
/* Avoid last class (localfs) that would accept any prefix */
|
|
|
|
for (vfs = vfs_list; vfs->next; vfs = vfs->next) {
|
2003-09-29 00:35:08 +04:00
|
|
|
if (vfs->which) {
|
1998-12-16 09:16:13 +03:00
|
|
|
if ((*vfs->which) (vfs, semi + 1) == -1)
|
|
|
|
continue;
|
2003-09-29 00:35:08 +04:00
|
|
|
*semi = '\0'; /* Found valid suffix */
|
1998-12-16 09:16:13 +03:00
|
|
|
return p;
|
|
|
|
}
|
2003-09-29 00:35:08 +04:00
|
|
|
if (vfs->prefix
|
|
|
|
&& !strncmp (semi + 1, vfs->prefix, strlen (vfs->prefix))) {
|
|
|
|
*semi = '\0'; /* Found valid suffix */
|
1998-12-16 09:16:13 +03:00
|
|
|
return p;
|
2003-09-29 00:35:08 +04:00
|
|
|
}
|
1998-12-16 09:16:13 +03:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2009-08-31 05:54:10 +04:00
|
|
|
static int
|
2001-03-05 09:00:27 +03:00
|
|
|
path_magic (const char *path)
|
1998-08-25 20:00:16 +04:00
|
|
|
{
|
1998-10-05 22:32:53 +04:00
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
if (!stat(path, &buf))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
1998-08-25 20:00:16 +04:00
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
1998-08-25 20:00:16 +04:00
|
|
|
* Splits path '/p1#op/inpath' into inpath,op; returns which vfs it is.
|
2009-02-06 01:27:37 +03:00
|
|
|
* What is left in path is p1. You still want to g_free(path), you DON'T
|
1998-05-26 04:53:24 +04:00
|
|
|
* want to free neither *inpath nor *op
|
|
|
|
*/
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *
|
2003-11-14 11:25:50 +03:00
|
|
|
vfs_split (char *path, char **inpath, char **op)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1998-10-14 06:56:18 +04:00
|
|
|
char *semi;
|
1998-05-26 04:53:24 +04:00
|
|
|
char *slash;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *ret;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!path)
|
2002-12-26 05:21:37 +03:00
|
|
|
vfs_die("Cannot split NULL");
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
semi = strrchr (path, '#');
|
1998-10-05 22:32:53 +04:00
|
|
|
if (!semi || !path_magic(path))
|
1998-05-26 04:53:24 +04:00
|
|
|
return NULL;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
slash = strchr (semi, PATH_SEP);
|
1998-05-26 04:53:24 +04:00
|
|
|
*semi = 0;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
if (op)
|
|
|
|
*op = NULL;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
if (inpath)
|
|
|
|
*inpath = NULL;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
if (slash)
|
|
|
|
*slash = 0;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2004-09-26 15:06:38 +04:00
|
|
|
if ((ret = vfs_prefix_to_class (semi+1))){
|
1998-05-26 04:53:24 +04:00
|
|
|
if (op)
|
1998-10-14 06:56:18 +04:00
|
|
|
*op = semi + 1;
|
1998-05-26 04:53:24 +04:00
|
|
|
if (inpath)
|
1998-10-14 06:56:18 +04:00
|
|
|
*inpath = slash ? slash + 1 : NULL;
|
1998-05-26 04:53:24 +04:00
|
|
|
return ret;
|
|
|
|
}
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2000-04-16 23:13:00 +04:00
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
if (slash)
|
1999-01-21 01:01:11 +03:00
|
|
|
*slash = PATH_SEP;
|
1998-05-26 04:53:24 +04:00
|
|
|
ret = vfs_split (path, inpath, op);
|
|
|
|
*semi = '#';
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-10-12 04:24:00 +04:00
|
|
|
static struct vfs_class *
|
2004-10-25 16:22:52 +04:00
|
|
|
_vfs_get_class (char *path)
|
1998-05-26 04:53:24 +04:00
|
|
|
{
|
2004-09-26 15:06:38 +04:00
|
|
|
char *semi;
|
|
|
|
char *slash;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *ret;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2000-04-16 23:13:00 +04:00
|
|
|
g_return_val_if_fail(path, NULL);
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
semi = strrchr (path, '#');
|
|
|
|
if (!semi || !path_magic (path))
|
1998-05-26 04:53:24 +04:00
|
|
|
return NULL;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
slash = strchr (semi, PATH_SEP);
|
2004-09-26 15:06:38 +04:00
|
|
|
*semi = 0;
|
|
|
|
if (slash)
|
|
|
|
*slash = 0;
|
1998-05-26 04:53:24 +04:00
|
|
|
|
2004-09-26 15:06:38 +04:00
|
|
|
ret = vfs_prefix_to_class (semi+1);
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2004-09-26 15:06:38 +04:00
|
|
|
if (slash)
|
|
|
|
*slash = PATH_SEP;
|
1998-05-26 04:53:24 +04:00
|
|
|
if (!ret)
|
2003-10-11 10:25:29 +04:00
|
|
|
ret = _vfs_get_class (path);
|
1998-10-14 06:56:18 +04:00
|
|
|
|
2004-09-26 15:06:38 +04:00
|
|
|
*semi = '#';
|
1998-05-26 04:53:24 +04:00
|
|
|
return ret;
|
|
|
|
}
|
1998-02-27 07:54:42 +03: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
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
2009-02-06 01:40:32 +03:00
|
|
|
char *path = g_strdup (pathname);
|
1998-10-14 06:56:18 +04:00
|
|
|
|
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
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
if (!vfs)
|
2003-10-12 23:57:27 +04:00
|
|
|
vfs = localfs_class;
|
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
|
|
|
}
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
const char *
|
|
|
|
vfs_get_encoding (const char *path)
|
|
|
|
{
|
|
|
|
static char result[16];
|
|
|
|
char *work;
|
|
|
|
char *semi;
|
|
|
|
char *slash;
|
|
|
|
|
|
|
|
work = g_strdup (path);
|
|
|
|
semi = g_strrstr (work, "#enc:");
|
|
|
|
|
|
|
|
if (semi != NULL) {
|
|
|
|
semi+= 5 * sizeof (char);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return if encoding can by used in vfs (is ascci full compactible) */
|
|
|
|
/* contains only a few encoding now */
|
|
|
|
static int
|
|
|
|
vfs_supported_enconding (const char *encoding) {
|
|
|
|
int t;
|
|
|
|
int result = 0;
|
|
|
|
|
|
|
|
for (t = 0; supported_encodings[t] != NULL; t++) {
|
|
|
|
result+= (g_ascii_strncasecmp (encoding, supported_encodings[t],
|
|
|
|
strlen (supported_encodings[t])) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
*/
|
2009-04-22 20:35:32 +04:00
|
|
|
static estr_t
|
2009-04-24 02:47:22 +04:00
|
|
|
_vfs_translate_path (const char *path, int size,
|
2009-04-15 18:11:00 +04:00
|
|
|
GIConv defcnv, GString *buffer)
|
2008-12-29 02:54:37 +03:00
|
|
|
{
|
|
|
|
const char *semi;
|
|
|
|
const char *ps;
|
|
|
|
const char *slash;
|
2009-04-22 20:35:32 +04:00
|
|
|
estr_t state = ESTR_SUCCESS;
|
2008-12-29 02:54:37 +03:00
|
|
|
static char encoding[16];
|
2009-04-15 18:11:00 +04:00
|
|
|
GIConv coder;
|
2008-12-29 02:54:37 +03:00
|
|
|
int ms;
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2009-04-24 02:47:22 +04:00
|
|
|
if (size == 0) return 0;
|
|
|
|
size = ( size > 0) ? size : (signed int)strlen (path);
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
/* try found #end: */
|
|
|
|
semi = g_strrstr_len (path, size, "#enc:");
|
|
|
|
if (semi != NULL) {
|
|
|
|
/* first must be translated part before #enc: */
|
|
|
|
ms = semi - path;
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
/* remove '/' before #enc */
|
|
|
|
ps = str_cget_prev_char (semi);
|
|
|
|
if (ps[0] == PATH_SEP) ms = ps - path;
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
state = _vfs_translate_path (path, ms, defcnv, buffer);
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2009-04-22 20:35:32 +04:00
|
|
|
if (state != ESTR_SUCCESS)
|
|
|
|
return state;
|
2008-12-29 02:54:37 +03:00
|
|
|
/* now can be translated part after #enc: */
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
semi+= 5;
|
|
|
|
slash = strchr (semi, PATH_SEP);
|
2009-04-24 02:47:22 +04:00
|
|
|
/* ignore slashes after size; */
|
2008-12-29 02:54:37 +03:00
|
|
|
if (slash - path >= size) slash = NULL;
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2009-04-24 02:47:22 +04:00
|
|
|
ms = (slash != NULL) ? slash - semi : (int) strlen (semi);
|
|
|
|
ms = min ((unsigned int) ms, sizeof (encoding) - 1);
|
|
|
|
/* limit encoding size (ms) to path size (size) */
|
2008-12-29 02:54:37 +03:00
|
|
|
if (semi + ms > path + size) ms = path + size - semi;
|
|
|
|
memcpy (encoding, semi, ms);
|
|
|
|
encoding[ms] = '\0';
|
2009-04-14 14:29:01 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
switch (vfs_supported_enconding (encoding)) {
|
|
|
|
case 1:
|
|
|
|
coder = str_crt_conv_to (encoding);
|
2009-04-19 23:11:49 +04:00
|
|
|
if (coder != INVALID_CONV) {
|
2008-12-29 02:54:37 +03:00
|
|
|
if (slash != NULL) {
|
|
|
|
state = str_vfs_convert_to (coder, slash,
|
|
|
|
path + size - slash, buffer);
|
2009-04-14 14:29:01 +04:00
|
|
|
} else if (buffer->str[0] == '\0') {
|
2008-12-29 02:54:37 +03:00
|
|
|
/* exmaple "/#enc:utf-8" */
|
2009-04-14 14:29:01 +04:00
|
|
|
g_string_append_c(buffer, PATH_SEP);
|
2008-12-29 02:54:37 +03:00
|
|
|
}
|
|
|
|
str_close_conv (coder);
|
|
|
|
return state;
|
|
|
|
} else {
|
|
|
|
errno = EINVAL;
|
|
|
|
return ESTR_FAILURE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return ESTR_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* path can be translated whole at once */
|
|
|
|
state = str_vfs_convert_to (defcnv, path, size, buffer);
|
|
|
|
return state;
|
|
|
|
}
|
2009-04-22 20:35:32 +04:00
|
|
|
|
|
|
|
return ESTR_SUCCESS;
|
2008-12-29 02:54:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
vfs_translate_path (const char *path)
|
|
|
|
{
|
2009-04-22 20:35:32 +04:00
|
|
|
estr_t state;
|
|
|
|
|
2009-04-14 14:29:01 +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
|
|
|
/*
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
vfs_translate_path_n (const char *path)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
vfs_canon_and_translate (const char *path)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1998-05-26 04:53:24 +04:00
|
|
|
static int
|
2003-10-12 04:24:00 +04:00
|
|
|
ferrno (struct vfs_class *vfs)
|
1998-05-26 04:53:24 +04:00
|
|
|
{
|
1998-11-21 22:36:01 +03:00
|
|
|
return vfs->ferrno ? (*vfs->ferrno)(vfs) : E_UNKNOWN;
|
1998-05-26 04:53:24 +04:00
|
|
|
/* Hope that error message is obscure enough ;-) */
|
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
2000-02-24 15:00:30 +03:00
|
|
|
mc_open (const char *filename, int flags, ...)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int mode;
|
|
|
|
void *info;
|
|
|
|
va_list ap;
|
2000-02-24 15:00:30 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
char *file = vfs_canon_and_translate (filename);
|
|
|
|
if (file != NULL) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs = vfs_get_class (file);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2004-09-24 20:03:24 +04:00
|
|
|
/* Get the mode flag */
|
|
|
|
if (flags & O_CREAT) {
|
|
|
|
va_start (ap, flags);
|
|
|
|
mode = va_arg (ap, int);
|
|
|
|
va_end (ap);
|
|
|
|
} else
|
|
|
|
mode = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2000-04-16 23:13:00 +04:00
|
|
|
if (!vfs->open) {
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (file);
|
2000-04-16 23:13:00 +04:00
|
|
|
errno = -EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
1998-10-14 06:56:18 +04:00
|
|
|
|
1998-09-27 23:27:58 +04:00
|
|
|
info = (*vfs->open) (vfs, file, flags, mode); /* open must be supported */
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (file);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (!info){
|
1998-05-26 04:53:24 +04:00
|
|
|
errno = ferrno (vfs);
|
1998-02-27 07:54:42 +03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
return vfs_new_handle (vfs, info);
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return -1;
|
2003-11-05 03:12:25 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
|
1998-08-31 14:02:52 +04:00
|
|
|
#define MC_NAMEOP(name, inarg, callarg) \
|
2003-11-05 09:34:20 +03:00
|
|
|
int mc_##name inarg \
|
|
|
|
{ \
|
|
|
|
struct vfs_class *vfs; \
|
|
|
|
int result; \
|
2008-12-29 02:54:37 +03:00
|
|
|
char *mpath = vfs_canon_and_translate (path); \
|
|
|
|
if (mpath != NULL) { \
|
2004-08-17 02:40:31 +04:00
|
|
|
vfs = vfs_get_class (mpath); \
|
2009-07-14 19:11:52 +04:00
|
|
|
if (vfs == NULL){ \
|
|
|
|
g_free (mpath); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
2003-11-05 09:34:20 +03:00
|
|
|
result = vfs->name ? (*vfs->name)callarg : -1; \
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (mpath); \
|
2003-11-05 09:34:20 +03:00
|
|
|
if (result == -1) \
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
|
|
|
|
return result; \
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return -1; \
|
2003-11-05 09:34:20 +03:00
|
|
|
}
|
|
|
|
|
2004-11-16 19:16:08 +03:00
|
|
|
MC_NAMEOP (chmod, (const char *path, mode_t mode), (vfs, mpath, mode))
|
|
|
|
MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vfs, mpath, owner, group))
|
2004-11-16 01:55:50 +03:00
|
|
|
MC_NAMEOP (utime, (const char *path, struct utimbuf *times), (vfs, mpath, times))
|
|
|
|
MC_NAMEOP (readlink, (const char *path, char *buf, int bufsiz), (vfs, mpath, buf, bufsiz))
|
|
|
|
MC_NAMEOP (unlink, (const char *path), (vfs, mpath))
|
|
|
|
MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vfs, mpath, mode))
|
|
|
|
MC_NAMEOP (rmdir, (const char *path), (vfs, mpath))
|
2004-11-16 19:16:08 +03:00
|
|
|
MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vfs, mpath, mode, dev))
|
2003-11-05 09:34:20 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
int
|
|
|
|
mc_symlink (const char *name1, const char *path)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
int result;
|
|
|
|
char *mpath;
|
|
|
|
char *lpath;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
mpath = vfs_canon_and_translate (path);
|
|
|
|
if (mpath != NULL) {
|
|
|
|
tmp = g_strdup (name1);
|
|
|
|
lpath = vfs_translate_path_n (tmp);
|
|
|
|
g_free (tmp);
|
|
|
|
|
|
|
|
if (lpath != NULL) {
|
|
|
|
vfs = vfs_get_class (mpath);
|
|
|
|
result = vfs->symlink ? (*vfs->symlink) (vfs, lpath, mpath) : -1;
|
|
|
|
g_free (lpath);
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->symlink ? ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
g_free (mpath);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2003-11-05 09:34:20 +03:00
|
|
|
|
1998-08-31 14:02:52 +04:00
|
|
|
#define MC_HANDLEOP(name, inarg, callarg) \
|
2009-01-24 06:38:30 +03:00
|
|
|
ssize_t mc_##name inarg \
|
2003-11-05 09:34:20 +03:00
|
|
|
{ \
|
|
|
|
struct vfs_class *vfs; \
|
|
|
|
int result; \
|
|
|
|
if (handle == -1) \
|
|
|
|
return -1; \
|
|
|
|
vfs = vfs_op (handle); \
|
2009-07-14 19:11:52 +04:00
|
|
|
if (vfs == NULL) \
|
|
|
|
return -1; \
|
2003-11-05 09:34:20 +03:00
|
|
|
result = vfs->name ? (*vfs->name)callarg : -1; \
|
|
|
|
if (result == -1) \
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
|
|
|
|
return result; \
|
|
|
|
}
|
1998-05-26 04:53:24 +04:00
|
|
|
|
2009-01-24 06:38:30 +03:00
|
|
|
MC_HANDLEOP(read, (int handle, void *buffer, int count), (vfs_info (handle), buffer, count))
|
|
|
|
MC_HANDLEOP(write, (int handle, const void *buf, int nbyte), (vfs_info (handle), buf, nbyte))
|
2003-11-05 09:34:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
#define MC_RENAMEOP(name) \
|
|
|
|
int mc_##name (const char *fname1, const char *fname2) \
|
|
|
|
{ \
|
|
|
|
struct vfs_class *vfs; \
|
|
|
|
int result; \
|
2008-12-29 02:54:37 +03:00
|
|
|
char *name2, *name1; \
|
|
|
|
name1 = vfs_canon_and_translate (fname1); \
|
|
|
|
if (name1 != NULL) { \
|
|
|
|
name2 = vfs_canon_and_translate (fname2); \
|
|
|
|
if (name2 != NULL) { \
|
2003-11-05 09:34:20 +03:00
|
|
|
vfs = vfs_get_class (name1); \
|
|
|
|
if (vfs != vfs_get_class (name2)){ \
|
|
|
|
errno = EXDEV; \
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (name1); \
|
|
|
|
g_free (name2); \
|
2003-11-05 09:34:20 +03:00
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
result = vfs->name ? (*vfs->name)(vfs, name1, name2) : -1; \
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (name1); \
|
|
|
|
g_free (name2); \
|
2003-11-05 09:34:20 +03:00
|
|
|
if (result == -1) \
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP; \
|
|
|
|
return result; \
|
2008-12-29 02:54:37 +03:00
|
|
|
} else { \
|
|
|
|
g_free (name1); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
} else return -1; \
|
2003-11-05 09:34:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MC_RENAMEOP (link)
|
|
|
|
MC_RENAMEOP (rename)
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
2003-10-16 20:12:19 +04:00
|
|
|
mc_ctl (int handle, int ctlop, void *arg)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs = vfs_op (handle);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
1999-12-16 15:55:16 +03:00
|
|
|
return vfs->ctl ? (*vfs->ctl)(vfs_info (handle), ctlop, arg) : 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
2004-08-17 02:40:31 +04:00
|
|
|
mc_setctl (const char *path, int ctlop, void *arg)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-02-27 07:54:42 +03:00
|
|
|
int result;
|
2004-08-17 02:40:31 +04:00
|
|
|
char *mpath;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-12-09 23:22:53 +03:00
|
|
|
if (!path)
|
1999-01-11 03:48:23 +03:00
|
|
|
vfs_die("You don't want to pass NULL to mc_setctl.");
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
mpath = vfs_canon_and_translate (path);
|
|
|
|
if (mpath != NULL) {
|
2004-08-17 02:40:31 +04:00
|
|
|
vfs = vfs_get_class (mpath);
|
|
|
|
result = vfs->setctl ? (*vfs->setctl)(vfs, mpath, ctlop, arg) : 0;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (mpath);
|
1998-02-27 07:54:42 +03:00
|
|
|
return result;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
|
|
|
mc_close (int handle)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-02-27 07:54:42 +03:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (handle == -1 || !vfs_info (handle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_op (handle);
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (handle < 3)
|
|
|
|
return close (handle);
|
|
|
|
|
1998-08-31 14:02:52 +04:00
|
|
|
if (!vfs->close)
|
1998-10-14 06:56:18 +04:00
|
|
|
vfs_die ("VFS must support close.\n");
|
1998-08-31 14:02:52 +04:00
|
|
|
result = (*vfs->close)(vfs_info (handle));
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_free_handle (handle);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (result == -1)
|
1998-05-26 04:53:24 +04:00
|
|
|
errno = ferrno (vfs);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
DIR *
|
2003-11-13 11:29:37 +03:00
|
|
|
mc_opendir (const char *dirname)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int handle, *handlep;
|
|
|
|
void *info;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
2008-12-29 02:54:37 +03:00
|
|
|
char *canon;
|
2003-11-13 11:29:37 +03:00
|
|
|
char *dname;
|
2008-12-29 02:54:37 +03:00
|
|
|
struct vfs_dirinfo *dirinfo;
|
|
|
|
const char *encoding;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
canon = vfs_canon (dirname);
|
|
|
|
dname = vfs_translate_path_n (canon);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
if (dname != NULL) {
|
|
|
|
vfs = vfs_get_class (dname);
|
2003-11-13 11:29:37 +03:00
|
|
|
info = vfs->opendir ? (*vfs->opendir)(vfs, dname) : NULL;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (dname);
|
2008-12-29 02:54:37 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
if (!info){
|
1998-11-21 22:36:01 +03:00
|
|
|
errno = vfs->opendir ? ferrno (vfs) : E_NOTSUPP;
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (canon);
|
1998-02-27 07:54:42 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-29 02:54:37 +03:00
|
|
|
|
|
|
|
dirinfo = g_new (struct vfs_dirinfo, 1);
|
|
|
|
dirinfo->info = info;
|
|
|
|
|
|
|
|
encoding = vfs_get_encoding (canon);
|
|
|
|
g_free (canon);
|
|
|
|
dirinfo->converter = (encoding != NULL) ? str_crt_conv_from (encoding) :
|
|
|
|
str_cnv_from_term;
|
2009-04-19 23:11:49 +04:00
|
|
|
if (dirinfo->converter == INVALID_CONV) dirinfo->converter =str_cnv_from_term;
|
2008-12-29 02:54:37 +03:00
|
|
|
|
|
|
|
handle = vfs_new_handle (vfs, dirinfo);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1999-01-21 01:01:11 +03:00
|
|
|
handlep = g_new (int, 1);
|
1998-02-27 07:54:42 +03:00
|
|
|
*handlep = handle;
|
|
|
|
return (DIR *) handlep;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else {
|
|
|
|
g_free (canon);
|
|
|
|
return NULL;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-04-22 22:49:27 +04:00
|
|
|
static struct dirent * mc_readdir_result = NULL;
|
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
struct dirent *
|
|
|
|
mc_readdir (DIR *dirp)
|
|
|
|
{
|
|
|
|
int handle;
|
|
|
|
struct vfs_class *vfs;
|
2008-12-29 02:54:37 +03:00
|
|
|
struct dirent *entry = NULL;
|
|
|
|
struct vfs_dirinfo *dirinfo;
|
2009-04-22 20:35:32 +04:00
|
|
|
estr_t state;
|
1998-05-26 23:46:37 +04:00
|
|
|
|
2009-04-22 22:49:27 +04:00
|
|
|
if (!mc_readdir_result)
|
|
|
|
{
|
|
|
|
/* We can't just allocate struct dirent as (see man dirent.h)
|
|
|
|
* struct dirent has VERY nonnaive semantics of allocating
|
|
|
|
* d_name in it. Moreover, linux's glibc-2.9 allocates dirents _less_,
|
|
|
|
* than 'sizeof (struct dirent)' making full bitwise (sizeof dirent) copy
|
|
|
|
* heap corrupter. So, allocate longliving dirent with at least
|
|
|
|
* (NAME_MAX + 1) for d_name in it.
|
|
|
|
* Strictly saying resulting dirent is unusable as we don't adjust internal
|
|
|
|
* structures, holding dirent size. But we don't use it in libc infrastructure.
|
|
|
|
* TODO: to make simpler homemade dirent-alike structure.
|
|
|
|
*/
|
2009-08-12 13:37:32 +04:00
|
|
|
mc_readdir_result = (struct dirent *)malloc(sizeof(struct dirent) + NAME_MAX + 1);
|
2009-04-22 22:49:27 +04:00
|
|
|
}
|
1998-05-26 23:46:37 +04:00
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
if (!dirp) {
|
|
|
|
errno = EFAULT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
handle = *(int *) dirp;
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs = vfs_op (handle);
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
dirinfo = vfs_info (handle);
|
|
|
|
if (vfs->readdir) {
|
2009-04-24 02:47:22 +04:00
|
|
|
entry = (*vfs->readdir) (dirinfo->info);
|
|
|
|
if (entry == NULL) return NULL;
|
|
|
|
g_string_set_size(vfs_str_buffer,0);
|
|
|
|
state = str_vfs_convert_from (dirinfo->converter,
|
2008-12-29 02:54:37 +03:00
|
|
|
entry->d_name, vfs_str_buffer);
|
2009-04-22 22:49:27 +04:00
|
|
|
mc_readdir_result->d_ino = entry->d_ino;
|
|
|
|
g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, NAME_MAX + 1);
|
2008-12-29 02:54:37 +03:00
|
|
|
}
|
|
|
|
if (entry == NULL) errno = vfs->readdir ? ferrno (vfs) : E_NOTSUPP;
|
2009-04-22 22:49:27 +04:00
|
|
|
return (entry != NULL) ? mc_readdir_result : NULL;
|
2003-11-05 03:12:25 +03:00
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
|
|
|
mc_closedir (DIR *dirp)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
int handle = *(int *) dirp;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs = vfs_op (handle);
|
1998-02-27 07:54:42 +03:00
|
|
|
int result;
|
2008-12-29 02:54:37 +03:00
|
|
|
struct vfs_dirinfo *dirinfo;
|
|
|
|
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
dirinfo = vfs_info (handle);
|
|
|
|
if (dirinfo->converter != str_cnv_from_term) str_close_conv (dirinfo->converter);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
result = vfs->closedir ? (*vfs->closedir)(dirinfo->info) : -1;
|
2003-11-05 03:12:25 +03:00
|
|
|
vfs_free_handle (handle);
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (dirinfo);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (dirp);
|
1998-02-27 07:54:42 +03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-12-11 12:36:50 +03:00
|
|
|
int mc_stat (const char *filename, struct stat *buf) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
2001-09-18 01:29:51 +04:00
|
|
|
int result;
|
2002-12-11 12:36:50 +03:00
|
|
|
char *path;
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
path = vfs_canon_and_translate (filename);
|
2009-10-12 17:06:22 +04:00
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_get_class (path);
|
|
|
|
|
|
|
|
if (vfs == NULL) {
|
|
|
|
g_free (path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-12-25 05:47:21 +03:00
|
|
|
result = vfs->stat ? (*vfs->stat) (vfs, path, buf) : -1;
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (path);
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2001-09-18 01:29:51 +04:00
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-12-11 12:36:50 +03:00
|
|
|
int mc_lstat (const char *filename, struct stat *buf) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
2001-09-18 01:29:51 +04:00
|
|
|
int result;
|
2002-12-11 12:36:50 +03:00
|
|
|
char *path;
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
path = vfs_canon_and_translate (filename);
|
2009-10-12 17:06:22 +04:00
|
|
|
|
|
|
|
if (path == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_get_class (path);
|
|
|
|
if (vfs == NULL) {
|
|
|
|
g_free (path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-12-25 05:47:21 +03:00
|
|
|
result = vfs->lstat ? (*vfs->lstat) (vfs, path, buf) : -1;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (path);
|
2001-09-18 01:29:51 +04:00
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mc_fstat (int handle, struct stat *buf) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
2001-09-18 01:29:51 +04:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (handle == -1)
|
|
|
|
return -1;
|
2009-10-12 17:06:22 +04:00
|
|
|
|
2001-09-18 01:29:51 +04:00
|
|
|
vfs = vfs_op (handle);
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2001-09-18 01:29:51 +04:00
|
|
|
result = vfs->fstat ? (*vfs->fstat) (vfs_info (handle), buf) : -1;
|
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->name ? ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
2003-06-06 04:50:22 +04:00
|
|
|
* Return current directory. If it's local, reread the current directory
|
2009-02-06 01:40:32 +03:00
|
|
|
* from the OS. You must g_strdup() whatever this function returns.
|
1998-05-26 04:53:24 +04:00
|
|
|
*/
|
2002-01-21 14:52:25 +03:00
|
|
|
static const char *
|
2003-06-06 04:50:22 +04:00
|
|
|
_vfs_get_cwd (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2008-12-29 02:54:37 +03:00
|
|
|
char *sys_cwd;
|
|
|
|
char *trans;
|
|
|
|
const char *encoding;
|
|
|
|
char *tmp;
|
2009-04-22 20:35:32 +04:00
|
|
|
estr_t state;
|
1998-02-27 07:54:42 +03:00
|
|
|
struct stat my_stat, my_stat2;
|
1998-05-26 04:53:24 +04:00
|
|
|
|
2009-04-24 02:47:22 +04:00
|
|
|
trans = vfs_translate_path_n (current_dir); /* add check if NULL */
|
2008-12-29 02:54:37 +03:00
|
|
|
|
|
|
|
if (!_vfs_get_class (trans)) {
|
|
|
|
encoding = vfs_get_encoding (current_dir);
|
|
|
|
if (encoding == NULL) {
|
|
|
|
tmp = g_get_current_dir ();
|
|
|
|
if (tmp != NULL) { /* One of the directories in the path is not readable */
|
2009-04-14 14:29:01 +04:00
|
|
|
g_string_set_size(vfs_str_buffer,0);
|
2008-12-29 02:54:37 +03:00
|
|
|
state = str_vfs_convert_from (str_cnv_from_term, tmp, vfs_str_buffer);
|
|
|
|
g_free (tmp);
|
2009-04-22 20:35:32 +04:00
|
|
|
sys_cwd = (state == ESTR_SUCCESS) ? g_strdup (vfs_str_buffer->str) : NULL;
|
|
|
|
if (!sys_cwd)
|
|
|
|
return current_dir;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
/* Otherwise check if it is O.K. to use the current_dir */
|
2008-12-29 02:54:37 +03:00
|
|
|
if (!cd_symlinks || mc_stat (sys_cwd, &my_stat)
|
2003-06-06 04:50:22 +04:00
|
|
|
|| mc_stat (current_dir, &my_stat2)
|
|
|
|
|| my_stat.st_ino != my_stat2.st_ino
|
|
|
|
|| my_stat.st_dev != my_stat2.st_dev) {
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (current_dir);
|
2008-12-29 02:54:37 +03:00
|
|
|
current_dir = sys_cwd;
|
|
|
|
return sys_cwd;
|
|
|
|
}/* Otherwise we return current_dir below */
|
|
|
|
}
|
|
|
|
}
|
2003-06-06 04:50:22 +04:00
|
|
|
}
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (trans);
|
1998-05-26 04:53:24 +04:00
|
|
|
return current_dir;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-06-06 04:50:22 +04:00
|
|
|
static void
|
|
|
|
vfs_setup_wd (void)
|
|
|
|
{
|
2009-02-06 01:40:32 +03:00
|
|
|
current_dir = g_strdup (PATH_SEP_STR);
|
2003-10-11 03:49:44 +04:00
|
|
|
_vfs_get_cwd ();
|
2003-06-06 04:50:22 +04:00
|
|
|
|
|
|
|
if (strlen (current_dir) > MC_MAXPATHLEN - 2)
|
|
|
|
vfs_die ("Current dir too long.\n");
|
2003-10-12 02:17:52 +04:00
|
|
|
|
|
|
|
current_vfs = vfs_get_class (current_dir);
|
2003-06-06 04:50:22 +04:00
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
|
|
|
* Return current directory. If it's local, reread the current directory
|
|
|
|
* from the OS. Put directory to the provided buffer.
|
2003-06-06 04:50:22 +04:00
|
|
|
*/
|
1998-10-14 06:56:18 +04:00
|
|
|
char *
|
|
|
|
mc_get_current_wd (char *buffer, int size)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-06-06 04:50:22 +04:00
|
|
|
const char *cwd = _vfs_get_cwd ();
|
1999-01-15 22:02:39 +03:00
|
|
|
|
2004-09-01 22:39:48 +04:00
|
|
|
g_strlcpy (buffer, cwd, size);
|
1998-05-26 04:53:24 +04:00
|
|
|
return buffer;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
2003-06-06 04:50:22 +04:00
|
|
|
* Return current directory without any OS calls.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
vfs_get_current_dir (void)
|
|
|
|
{
|
|
|
|
return current_dir;
|
|
|
|
}
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
off_t mc_lseek (int fd, off_t offset, int whence)
|
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-08-31 14:02:52 +04:00
|
|
|
int result;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_op (fd);
|
2009-10-12 17:06:22 +04:00
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
1998-08-31 14:02:52 +04:00
|
|
|
result = vfs->lseek ? (*vfs->lseek)(vfs_info (fd), offset, whence) : -1;
|
|
|
|
if (result == -1)
|
1998-11-21 22:36:01 +03:00
|
|
|
errno = vfs->lseek ? ferrno (vfs) : E_NOTSUPP;
|
1998-08-31 14:02:52 +04:00
|
|
|
return result;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
2004-10-25 16:22:52 +04:00
|
|
|
* remove //, /./ and /../
|
1998-05-26 04:53:24 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define ISSLASH(a) (!a || (a == '/'))
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
char *
|
2000-02-24 15:00:30 +03:00
|
|
|
vfs_canon (const char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
1999-01-11 03:48:23 +03:00
|
|
|
if (!path)
|
2001-06-02 10:36:00 +04:00
|
|
|
vfs_die("Cannot canonicalize NULL");
|
1998-10-14 06:56:18 +04:00
|
|
|
|
|
|
|
/* Relative to current directory */
|
1999-01-21 01:01:11 +03:00
|
|
|
if (*path != PATH_SEP){
|
1998-02-27 07:54:42 +03:00
|
|
|
char *local, *result;
|
|
|
|
|
2009-02-06 13:17:03 +03:00
|
|
|
local = concat_dir_and_file (current_dir, path);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
|
|
|
result = vfs_canon (local);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (local);
|
1998-02-27 07:54:42 +03:00
|
|
|
return result;
|
|
|
|
}
|
1998-10-14 06:56:18 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* So we have path of following form:
|
1998-08-25 20:00:16 +04:00
|
|
|
* /p1/p2#op/.././././p3#op/p4. Good luck.
|
1998-05-26 04:53:24 +04:00
|
|
|
*/
|
2000-02-24 15:00:30 +03:00
|
|
|
{
|
2009-02-06 01:40:32 +03:00
|
|
|
char *result = g_strdup (path);
|
2000-02-24 15:00:30 +03:00
|
|
|
canonicalize_pathname (result);
|
|
|
|
return result;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
2002-12-25 05:47:21 +03:00
|
|
|
* VFS chdir.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
2003-11-13 11:29:37 +03:00
|
|
|
mc_chdir (const char *path)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-11-13 11:29:37 +03:00
|
|
|
char *new_dir;
|
2008-12-29 02:54:37 +03:00
|
|
|
char *trans_dir;
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *old_vfs, *new_vfs;
|
2002-12-25 07:02:34 +03:00
|
|
|
vfsid old_vfsid;
|
2002-12-25 07:38:50 +03:00
|
|
|
int result;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-25 07:02:34 +03:00
|
|
|
new_dir = vfs_canon (path);
|
2008-12-29 02:54:37 +03:00
|
|
|
trans_dir = vfs_translate_path_n (new_dir);
|
|
|
|
if (trans_dir != NULL) {
|
|
|
|
new_vfs = vfs_get_class (trans_dir);
|
2004-08-26 04:17:36 +04:00
|
|
|
if (!new_vfs->chdir) {
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (new_dir);
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (trans_dir);
|
2003-09-29 00:35:08 +04:00
|
|
|
return -1;
|
2004-08-26 04:17:36 +04:00
|
|
|
}
|
2002-12-25 07:02:34 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
result = (*new_vfs->chdir) (new_vfs, trans_dir);
|
2002-12-25 07:38:50 +03:00
|
|
|
|
|
|
|
if (result == -1) {
|
2002-12-25 07:02:34 +03:00
|
|
|
errno = ferrno (new_vfs);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (new_dir);
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (trans_dir);
|
2002-12-25 07:02:34 +03:00
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
1998-05-26 04:53:24 +04:00
|
|
|
|
2003-11-27 00:25:30 +03:00
|
|
|
old_vfsid = vfs_getid (current_vfs, current_dir);
|
2002-12-25 07:02:34 +03:00
|
|
|
old_vfs = current_vfs;
|
|
|
|
|
|
|
|
/* Actually change directory */
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (current_dir);
|
2002-12-25 07:02:34 +03:00
|
|
|
current_dir = new_dir;
|
|
|
|
current_vfs = new_vfs;
|
|
|
|
|
2002-12-25 07:38:50 +03:00
|
|
|
/* This function uses the new current_dir implicitly */
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_stamp_create (old_vfs, old_vfsid);
|
2002-12-25 07:02:34 +03:00
|
|
|
|
|
|
|
/* Sometimes we assume no trailing slash on cwd */
|
2002-12-25 05:47:21 +03:00
|
|
|
if (*current_dir) {
|
2002-12-25 07:02:34 +03:00
|
|
|
char *p;
|
1998-02-27 07:54:42 +03:00
|
|
|
p = strchr (current_dir, 0) - 1;
|
1999-01-21 01:01:11 +03:00
|
|
|
if (*p == PATH_SEP && p > current_dir)
|
2002-12-25 07:02:34 +03:00
|
|
|
*p = 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2002-12-25 07:02:34 +03:00
|
|
|
|
2008-12-29 02:54:37 +03:00
|
|
|
g_free (trans_dir);
|
2002-12-25 07:02:34 +03:00
|
|
|
return 0;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else {
|
|
|
|
g_free (new_dir);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-10-11 12:21:16 +04:00
|
|
|
/* Return 1 is the current VFS class is local */
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2003-10-11 12:21:16 +04:00
|
|
|
/* Return flags of the VFS class of the given filename */
|
1998-10-14 06:56:18 +04:00
|
|
|
int
|
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);
|
|
|
|
if (fname != NULL) {
|
2003-10-11 11:38:05 +04:00
|
|
|
vfs = vfs_get_class (fname);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (fname);
|
2003-10-11 12:21:16 +04:00
|
|
|
return vfs->flags;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-10-28 10:09:39 +03:00
|
|
|
static char *
|
2005-02-22 21:35:22 +03:00
|
|
|
mc_def_getlocalcopy (const char *filename)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-29 03:50:36 +03:00
|
|
|
char *tmp;
|
1998-02-27 07:54:42 +03:00
|
|
|
int fdin, fdout, i;
|
|
|
|
char buffer[8192];
|
|
|
|
struct stat mystat;
|
|
|
|
|
2003-10-28 00:55:06 +03:00
|
|
|
fdin = mc_open (filename, O_RDONLY | O_LINEAR);
|
1998-02-27 07:54:42 +03:00
|
|
|
if (fdin == -1)
|
2003-02-26 20:43:18 +03:00
|
|
|
return NULL;
|
2001-05-21 20:21:07 +04:00
|
|
|
|
2003-10-29 03:50:36 +03:00
|
|
|
fdout = vfs_mkstemps (&tmp, "vfs", filename);
|
2002-08-16 02:30:05 +04:00
|
|
|
|
1999-11-11 17:23:40 +03:00
|
|
|
if (fdout == -1)
|
|
|
|
goto fail;
|
2003-02-26 20:43:18 +03:00
|
|
|
while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0) {
|
2000-05-25 18:47:48 +04:00
|
|
|
if (write (fdout, buffer, i) != i)
|
|
|
|
goto fail;
|
|
|
|
}
|
1999-11-11 17:23:40 +03:00
|
|
|
if (i == -1)
|
|
|
|
goto fail;
|
|
|
|
i = mc_close (fdin);
|
|
|
|
fdin = -1;
|
2003-02-26 20:43:18 +03:00
|
|
|
if (i == -1)
|
1999-11-11 17:23:40 +03:00
|
|
|
goto fail;
|
2004-10-25 16:22:52 +04:00
|
|
|
if (close (fdout) == -1) {
|
|
|
|
fdout = -1;
|
1999-11-11 17:23:40 +03:00
|
|
|
goto fail;
|
2004-10-25 16:22:52 +04:00
|
|
|
}
|
1999-11-11 17:23:40 +03:00
|
|
|
|
2003-02-26 20:43:18 +03:00
|
|
|
if (mc_stat (filename, &mystat) != -1) {
|
|
|
|
chmod (tmp, mystat.st_mode);
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
return tmp;
|
1999-11-11 17:23:40 +03:00
|
|
|
|
2003-02-26 20:43:18 +03:00
|
|
|
fail:
|
2004-10-25 16:22:52 +04:00
|
|
|
if (fdout != -1)
|
2003-02-26 20:43:18 +03:00
|
|
|
close (fdout);
|
2004-10-25 16:22:52 +04:00
|
|
|
if (fdin != -1)
|
2003-02-26 20:43:18 +03:00
|
|
|
mc_close (fdin);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (tmp);
|
1999-11-11 17:23:40 +03:00
|
|
|
return NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
char *
|
2000-02-24 15:00:30 +03:00
|
|
|
mc_getlocalcopy (const char *pathname)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
|
|
|
char *result;
|
2008-12-29 02:54:37 +03:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = vfs_canon_and_translate (pathname);
|
|
|
|
if (path != NULL) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs = vfs_get_class (path);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2002-12-25 05:47:21 +03:00
|
|
|
result = vfs->getlocalcopy ? (*vfs->getlocalcopy)(vfs, path) :
|
2005-02-22 21:35:22 +03:00
|
|
|
mc_def_getlocalcopy (path);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (path);
|
1998-08-31 14:02:52 +04:00
|
|
|
if (!result)
|
1998-05-26 04:53:24 +04:00
|
|
|
errno = ferrno (vfs);
|
1998-02-27 07:54:42 +03:00
|
|
|
return result;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return NULL;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2003-10-28 10:09:39 +03:00
|
|
|
static int
|
2003-10-28 05:10:33 +03:00
|
|
|
mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
|
2003-10-29 00:52:06 +03:00
|
|
|
const char *local, int has_changed)
|
2003-10-28 00:01:00 +03:00
|
|
|
{
|
1999-11-11 17:23:40 +03:00
|
|
|
int fdin = -1, fdout = -1, i;
|
2003-10-28 00:01:00 +03:00
|
|
|
if (has_changed) {
|
|
|
|
char buffer[8192];
|
|
|
|
|
|
|
|
if (!vfs->write)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
fdin = open (local, O_RDONLY);
|
|
|
|
if (fdin == -1)
|
1999-11-11 17:23:40 +03:00
|
|
|
goto failed;
|
2003-10-28 00:01:00 +03:00
|
|
|
fdout = mc_open (filename, O_WRONLY | O_TRUNC);
|
|
|
|
if (fdout == -1)
|
1999-11-11 17:23:40 +03:00
|
|
|
goto failed;
|
2003-10-28 00:01:00 +03:00
|
|
|
while ((i = read (fdin, buffer, sizeof (buffer))) > 0) {
|
2000-05-25 18:47:48 +04:00
|
|
|
if (mc_write (fdout, buffer, i) != i)
|
|
|
|
goto failed;
|
|
|
|
}
|
1999-11-11 17:23:40 +03:00
|
|
|
if (i == -1)
|
|
|
|
goto failed;
|
|
|
|
|
2003-10-28 00:01:00 +03:00
|
|
|
if (close (fdin) == -1) {
|
2001-03-01 00:50:57 +03:00
|
|
|
fdin = -1;
|
1999-11-11 17:23:40 +03:00
|
|
|
goto failed;
|
2001-03-01 00:50:57 +03:00
|
|
|
}
|
|
|
|
fdin = -1;
|
2003-10-28 00:01:00 +03:00
|
|
|
if (mc_close (fdout) == -1) {
|
1999-11-11 17:23:40 +03:00
|
|
|
fdout = -1;
|
|
|
|
goto failed;
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
unlink (local);
|
1999-11-11 17:23:40 +03:00
|
|
|
return 0;
|
|
|
|
|
2003-10-28 00:01:00 +03:00
|
|
|
failed:
|
2009-01-10 16:13:56 +03:00
|
|
|
message (D_ERROR, _("Changes to file lost"), "%s", filename);
|
2003-10-28 00:01:00 +03:00
|
|
|
if (fdout != -1)
|
|
|
|
mc_close (fdout);
|
|
|
|
if (fdin != -1)
|
|
|
|
close (fdin);
|
1999-11-11 17:23:40 +03:00
|
|
|
unlink (local);
|
|
|
|
return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-11-11 17:23:40 +03:00
|
|
|
int
|
2004-08-16 19:18:17 +04:00
|
|
|
mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2001-02-28 01:19:04 +03:00
|
|
|
int return_value = 0;
|
2008-12-29 02:54:37 +03:00
|
|
|
char *path;
|
|
|
|
|
|
|
|
path = vfs_canon_and_translate (pathname);
|
|
|
|
if (path != NULL) {
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs = vfs_get_class (path);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2001-02-28 01:19:04 +03:00
|
|
|
return_value = vfs->ungetlocalcopy ?
|
2002-12-25 05:47:21 +03:00
|
|
|
(*vfs->ungetlocalcopy)(vfs, path, local, has_changed) :
|
|
|
|
mc_def_ungetlocalcopy (vfs, path, local, has_changed);
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (path);
|
2001-02-28 01:19:04 +03:00
|
|
|
return return_value;
|
2008-12-29 02:54:37 +03:00
|
|
|
} else return -1;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
1999-11-11 17:23:40 +03:00
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
void
|
|
|
|
vfs_init (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2009-05-14 18:52:10 +04:00
|
|
|
/* create the VFS handle array */
|
|
|
|
vfs_openfiles = g_ptr_array_new ();
|
|
|
|
|
2009-04-14 14:29:01 +04:00
|
|
|
vfs_str_buffer = g_string_new("");
|
2003-10-12 23:57:27 +04:00
|
|
|
/* localfs needs to be the first one */
|
|
|
|
init_localfs();
|
|
|
|
/* fallback value for vfs_get_class() */
|
|
|
|
localfs_class = vfs_list;
|
1998-09-27 23:27:58 +04:00
|
|
|
|
2003-10-13 02:25:53 +04:00
|
|
|
init_extfs ();
|
|
|
|
init_sfs ();
|
|
|
|
init_tarfs ();
|
|
|
|
init_cpiofs ();
|
|
|
|
|
|
|
|
#ifdef USE_EXT2FSLIB
|
|
|
|
init_undelfs ();
|
|
|
|
#endif /* USE_EXT2FSLIB */
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#ifdef USE_NETCODE
|
|
|
|
tcp_init();
|
2003-10-11 13:27:37 +04:00
|
|
|
init_ftpfs ();
|
2003-10-11 03:38:03 +04:00
|
|
|
init_fish ();
|
1999-06-23 01:02:08 +04:00
|
|
|
#ifdef WITH_SMBFS
|
2003-10-13 02:25:53 +04:00
|
|
|
init_smbfs ();
|
2002-09-06 05:27:45 +04:00
|
|
|
#endif /* WITH_SMBFS */
|
2009-05-11 11:28:57 +04:00
|
|
|
#ifdef ENABLE_VFS_MCFS
|
2003-10-13 02:25:53 +04:00
|
|
|
init_mcfs ();
|
2009-05-11 11:28:57 +04:00
|
|
|
#endif /* ENABLE_VFS_MCFS */
|
2002-09-06 05:27:45 +04:00
|
|
|
#endif /* USE_NETCODE */
|
1998-09-27 23:27:58 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
vfs_setup_wd ();
|
|
|
|
}
|
|
|
|
|
1998-10-14 06:56:18 +04:00
|
|
|
void
|
|
|
|
vfs_shut (void)
|
1998-02-27 07:54:42 +03:00
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
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
|
|
|
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (current_dir);
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
for (vfs = vfs_list; vfs; vfs = vfs->next)
|
|
|
|
if (vfs->done)
|
|
|
|
(*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);
|
2009-04-15 18:11:00 +04:00
|
|
|
|
2009-04-14 14:29:01 +04:00
|
|
|
g_string_free (vfs_str_buffer, TRUE);
|
1998-02-27 07:54:42 +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
|
|
|
|
*/
|
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
|
|
|
{
|
2003-10-12 04:24:00 +04:00
|
|
|
struct vfs_class *vfs;
|
1998-10-14 06:56:18 +04:00
|
|
|
|
|
|
|
for (vfs=vfs_list; vfs; vfs=vfs->next)
|
1998-09-27 23:27:58 +04:00
|
|
|
if (vfs->fill_names)
|
|
|
|
(*vfs->fill_names) (vfs, func);
|
1998-02-27 07:54:42 +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
|
|
|
|
|
|
|
static const struct {
|
|
|
|
const char *name;
|
|
|
|
size_t name_len;
|
|
|
|
const char *substitute;
|
|
|
|
} url_table[] = { {"ftp://", 6, "/#ftp:"},
|
|
|
|
{"mc://", 5, "/#mc:"},
|
|
|
|
{"smb://", 6, "/#smb:"},
|
|
|
|
{"sh://", 5, "/#sh:"},
|
|
|
|
{"ssh://", 6, "/#sh:"},
|
|
|
|
{"a:", 2, "/#a"}
|
|
|
|
};
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
|
|
|
int vfs_file_is_local (const char *filename)
|
|
|
|
{
|
|
|
|
return vfs_file_class_flags (filename) & VFSF_LOCAL;
|
|
|
|
}
|