2011-04-07 16:27:46 +04:00
|
|
|
/* Virtual File System: interface functions
|
|
|
|
Copyright (C) 2011 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by:
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2011
|
|
|
|
|
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software; you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation; either version 2 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be
|
|
|
|
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Source: Virtual File System: path handlers
|
|
|
|
* \author Slava Zanko
|
|
|
|
* \date 2011
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* For atol() */
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <ctype.h> /* is_digit() */
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "lib/global.h"
|
|
|
|
|
|
|
|
#include "lib/widget.h" /* message() */
|
2011-04-18 16:08:13 +04:00
|
|
|
#include "lib/strutil.h" /* str_crt_conv_from() */
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
#include "vfs.h"
|
|
|
|
#include "utilvfs.h"
|
2011-04-18 16:08:13 +04:00
|
|
|
#include "path.h"
|
2011-04-07 16:27:46 +04:00
|
|
|
#include "gc.h"
|
|
|
|
|
|
|
|
extern GString *vfs_str_buffer;
|
|
|
|
extern struct vfs_class *current_vfs;
|
|
|
|
|
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
struct dirent *mc_readdir_result = NULL;
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static char *
|
|
|
|
mc_def_getlocalcopy (const char *filename)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
int fdin, fdout;
|
|
|
|
ssize_t i;
|
|
|
|
char buffer[8192];
|
|
|
|
struct stat mystat;
|
|
|
|
|
|
|
|
fdin = mc_open (filename, O_RDONLY | O_LINEAR);
|
|
|
|
if (fdin == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
fdout = vfs_mkstemps (&tmp, "vfs", filename);
|
|
|
|
|
|
|
|
if (fdout == -1)
|
|
|
|
goto fail;
|
|
|
|
while ((i = mc_read (fdin, buffer, sizeof (buffer))) > 0)
|
|
|
|
{
|
|
|
|
if (write (fdout, buffer, i) != i)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (i == -1)
|
|
|
|
goto fail;
|
|
|
|
i = mc_close (fdin);
|
|
|
|
fdin = -1;
|
|
|
|
if (i == -1)
|
|
|
|
goto fail;
|
|
|
|
if (close (fdout) == -1)
|
|
|
|
{
|
|
|
|
fdout = -1;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mc_stat (filename, &mystat) != -1)
|
|
|
|
chmod (tmp, mystat.st_mode);
|
|
|
|
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
if (fdout != -1)
|
|
|
|
close (fdout);
|
|
|
|
if (fdin != -1)
|
|
|
|
mc_close (fdin);
|
|
|
|
g_free (tmp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
|
|
|
|
const char *local, int has_changed)
|
|
|
|
{
|
|
|
|
int fdin = -1, fdout = -1;
|
|
|
|
if (has_changed)
|
|
|
|
{
|
|
|
|
char buffer[8192];
|
|
|
|
ssize_t i;
|
|
|
|
|
|
|
|
if (!vfs->write)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
fdin = open (local, O_RDONLY);
|
|
|
|
if (fdin == -1)
|
|
|
|
goto failed;
|
|
|
|
fdout = mc_open (filename, O_WRONLY | O_TRUNC);
|
|
|
|
if (fdout == -1)
|
|
|
|
goto failed;
|
|
|
|
while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
|
|
|
|
if (mc_write (fdout, buffer, (size_t) i) != i)
|
|
|
|
goto failed;
|
|
|
|
if (i == -1)
|
|
|
|
goto failed;
|
|
|
|
|
|
|
|
if (close (fdin) == -1)
|
|
|
|
{
|
|
|
|
fdin = -1;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
fdin = -1;
|
|
|
|
if (mc_close (fdout) == -1)
|
|
|
|
{
|
|
|
|
fdout = -1;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unlink (local);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
message (D_ERROR, _("Changes to file lost"), "%s", filename);
|
|
|
|
if (fdout != -1)
|
|
|
|
mc_close (fdout);
|
|
|
|
if (fdin != -1)
|
|
|
|
close (fdin);
|
|
|
|
unlink (local);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_open (const char *filename, int flags, ...)
|
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
int mode = 0, result = -1;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vpath = vfs_path_from_str (filename);
|
|
|
|
if (vpath == NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Get the mode flag */
|
|
|
|
if (flags & O_CREAT)
|
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
va_list ap;
|
2011-04-07 16:27:46 +04:00
|
|
|
va_start (ap, flags);
|
|
|
|
mode = va_arg (ap, int);
|
|
|
|
va_end (ap);
|
|
|
|
}
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element) && path_element->class->open != NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
void *info;
|
|
|
|
/* open must be supported */
|
2011-04-18 18:08:55 +04:00
|
|
|
info = path_element->class->open (vpath, flags, mode);
|
2011-04-18 16:08:13 +04:00
|
|
|
if (info == NULL)
|
|
|
|
errno = vfs_ferrno (path_element->class);
|
|
|
|
else
|
|
|
|
result = vfs_new_handle (path_element->class, info);
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2011-04-18 16:08:13 +04:00
|
|
|
else
|
|
|
|
errno = -EOPNOTSUPP;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
|
|
|
return result;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
|
|
|
|
#define MC_NAMEOP(name, inarg, callarg) \
|
|
|
|
int mc_##name inarg \
|
|
|
|
{ \
|
|
|
|
int result; \
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_t *vpath; \
|
|
|
|
vfs_path_element_t *path_element; \
|
|
|
|
\
|
|
|
|
vpath = vfs_path_from_str (path); \
|
|
|
|
if (vpath == NULL) \
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1; \
|
2011-04-18 16:08:13 +04:00
|
|
|
\
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1); \
|
2011-06-17 14:28:37 +04:00
|
|
|
if (!vfs_path_element_valid (path_element)) \
|
2011-04-18 16:08:13 +04:00
|
|
|
{ \
|
|
|
|
vfs_path_free(vpath); \
|
|
|
|
return -1; \
|
|
|
|
} \
|
|
|
|
\
|
2011-06-02 14:43:05 +04:00
|
|
|
result = path_element->class->name != NULL ? path_element->class->name callarg : -1; \
|
2011-04-07 16:27:46 +04:00
|
|
|
if (result == -1) \
|
2011-04-18 16:08:13 +04:00
|
|
|
errno = path_element->class->name != NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP; \
|
|
|
|
vfs_path_free(vpath); \
|
2011-04-07 16:27:46 +04:00
|
|
|
return result; \
|
|
|
|
}
|
|
|
|
|
2011-04-20 13:03:56 +04:00
|
|
|
MC_NAMEOP (chmod, (const char *path, mode_t mode), (vpath, mode))
|
|
|
|
MC_NAMEOP (chown, (const char *path, uid_t owner, gid_t group), (vpath, owner, group))
|
|
|
|
MC_NAMEOP (utime, (const char *path, struct utimbuf * times), (vpath, times))
|
2011-04-21 11:46:20 +04:00
|
|
|
MC_NAMEOP (readlink, (const char *path, char *buf, size_t bufsiz), (vpath, buf, bufsiz))
|
|
|
|
MC_NAMEOP (unlink, (const char *path), (vpath))
|
2011-04-21 14:27:56 +04:00
|
|
|
MC_NAMEOP (mkdir, (const char *path, mode_t mode), (vpath, mode))
|
|
|
|
MC_NAMEOP (rmdir, (const char *path), (vpath))
|
2011-04-21 11:46:20 +04:00
|
|
|
MC_NAMEOP (mknod, (const char *path, mode_t mode, dev_t dev), (vpath, mode, dev))
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_symlink (const char *name1, const char *path)
|
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
int result = -1;
|
2011-04-21 11:46:20 +04:00
|
|
|
vfs_path_t *vpath1, *vpath2;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-21 11:46:20 +04:00
|
|
|
vpath1 = vfs_path_from_str (path);
|
|
|
|
if (vpath1 == NULL)
|
2011-04-18 16:08:13 +04:00
|
|
|
return -1;
|
|
|
|
|
2011-04-21 11:46:20 +04:00
|
|
|
vpath2 = vfs_path_from_str (name1);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-21 11:46:20 +04:00
|
|
|
if (vpath2 != NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-21 11:46:20 +04:00
|
|
|
vfs_path_element_t *path_element =
|
2011-04-28 15:39:59 +04:00
|
|
|
vfs_path_get_by_index (vpath1, vfs_path_elements_count (vpath1) - 1);
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-21 11:46:20 +04:00
|
|
|
{
|
|
|
|
result =
|
|
|
|
path_element->class->symlink !=
|
|
|
|
NULL ? path_element->class->symlink (vpath2, vpath1) : -1;
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
errno =
|
|
|
|
path_element->class->symlink !=
|
|
|
|
NULL ? vfs_ferrno (path_element->class) : E_NOTSUPP;
|
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2011-04-21 11:46:20 +04:00
|
|
|
vfs_path_free (vpath1);
|
|
|
|
vfs_path_free (vpath2);
|
2011-04-18 16:08:13 +04:00
|
|
|
return result;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* *INDENT-OFF* */
|
|
|
|
|
|
|
|
#define MC_HANDLEOP(name, inarg, callarg) \
|
|
|
|
ssize_t mc_##name inarg \
|
|
|
|
{ \
|
|
|
|
struct vfs_class *vfs; \
|
|
|
|
int result; \
|
|
|
|
if (handle == -1) \
|
|
|
|
return -1; \
|
|
|
|
vfs = vfs_class_find_by_handle (handle); \
|
|
|
|
if (vfs == NULL) \
|
|
|
|
return -1; \
|
|
|
|
result = vfs->name != NULL ? vfs->name callarg : -1; \
|
|
|
|
if (result == -1) \
|
|
|
|
errno = vfs->name != NULL ? vfs_ferrno (vfs) : E_NOTSUPP; \
|
|
|
|
return result; \
|
|
|
|
}
|
|
|
|
|
|
|
|
MC_HANDLEOP (read, (int handle, void *buffer, size_t count), (vfs_class_data_find_by_handle (handle), buffer, count))
|
|
|
|
MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data_find_by_handle (handle), buf, nbyte))
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
#define MC_RENAMEOP(name) \
|
|
|
|
int mc_##name (const char *fname1, const char *fname2) \
|
|
|
|
{ \
|
|
|
|
int result; \
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_t *vpath1, *vpath2; \
|
|
|
|
vfs_path_element_t *path_element1, *path_element2; \
|
|
|
|
\
|
|
|
|
vpath1 = vfs_path_from_str (fname1); \
|
|
|
|
if (vpath1 == NULL) \
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1; \
|
2011-04-18 16:08:13 +04:00
|
|
|
\
|
|
|
|
vpath2 = vfs_path_from_str (fname2); \
|
|
|
|
if (vpath2 == NULL) \
|
|
|
|
{ \
|
|
|
|
vfs_path_free(vpath1); \
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1; \
|
2011-04-18 16:08:13 +04:00
|
|
|
}\
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element1 = vfs_path_get_by_index (vpath1, vfs_path_elements_count (vpath1) - 1); \
|
|
|
|
path_element2 = vfs_path_get_by_index (vpath2, vfs_path_elements_count (vpath2) - 1); \
|
2011-04-18 16:08:13 +04:00
|
|
|
\
|
2011-06-17 14:28:37 +04:00
|
|
|
if (!vfs_path_element_valid (path_element1) || !vfs_path_element_valid (path_element2) || \
|
|
|
|
path_element1->class != path_element2->class) \
|
2011-04-07 16:27:46 +04:00
|
|
|
{ \
|
|
|
|
errno = EXDEV; \
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free(vpath1); \
|
|
|
|
vfs_path_free(vpath2); \
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1; \
|
2011-04-18 16:08:13 +04:00
|
|
|
}\
|
|
|
|
\
|
|
|
|
result = path_element1->class->name != NULL \
|
2011-04-21 11:46:20 +04:00
|
|
|
? path_element1->class->name (vpath1, vpath2) \
|
2011-04-18 16:08:13 +04:00
|
|
|
: -1; \
|
2011-04-07 16:27:46 +04:00
|
|
|
if (result == -1) \
|
2011-04-18 16:08:13 +04:00
|
|
|
errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
|
|
|
|
vfs_path_free(vpath1); \
|
|
|
|
vfs_path_free(vpath2); \
|
2011-04-07 16:27:46 +04:00
|
|
|
return result; \
|
|
|
|
}
|
|
|
|
|
|
|
|
MC_RENAMEOP (link)
|
|
|
|
MC_RENAMEOP (rename)
|
|
|
|
|
|
|
|
/* *INDENT-ON* */
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_ctl (int handle, int ctlop, void *arg)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs = vfs_class_find_by_handle (handle);
|
|
|
|
|
|
|
|
if (vfs == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return vfs->ctl ? (*vfs->ctl) (vfs_class_data_find_by_handle (handle), ctlop, arg) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_setctl (const char *path, int ctlop, void *arg)
|
|
|
|
{
|
|
|
|
int result = -1;
|
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
|
|
|
|
|
|
|
vpath = vfs_path_from_str (path);
|
|
|
|
if (vpath == NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
vfs_die ("You don't want to pass NULL to mc_setctl.");
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-18 16:08:13 +04:00
|
|
|
result =
|
2011-04-21 14:27:56 +04:00
|
|
|
path_element->class->setctl != NULL ? path_element->class->setctl (vpath,
|
2011-04-18 16:08:13 +04:00
|
|
|
ctlop, arg) : 0;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_close (int handle)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (handle == -1 || !vfs_class_data_find_by_handle (handle))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_class_find_by_handle (handle);
|
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (handle < 3)
|
|
|
|
return close (handle);
|
|
|
|
|
|
|
|
if (!vfs->close)
|
|
|
|
vfs_die ("VFS must support close.\n");
|
|
|
|
result = (*vfs->close) (vfs_class_data_find_by_handle (handle));
|
|
|
|
vfs_free_handle (handle);
|
|
|
|
if (result == -1)
|
|
|
|
errno = vfs_ferrno (vfs);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
DIR *
|
|
|
|
mc_opendir (const char *dirname)
|
|
|
|
{
|
|
|
|
int handle, *handlep;
|
|
|
|
void *info;
|
2011-04-20 16:59:06 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
vpath = vfs_path_from_str (dirname);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
if (vpath == NULL)
|
|
|
|
return NULL;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-06-17 14:28:37 +04:00
|
|
|
if (!vfs_path_element_valid (path_element))
|
|
|
|
{
|
|
|
|
errno = E_NOTSUPP;
|
|
|
|
vfs_path_free (vpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
info = path_element->class->opendir ? (*path_element->class->opendir) (vpath) : NULL;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
if (info == NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-20 16:59:06 +04:00
|
|
|
errno = path_element->class->opendir ? vfs_ferrno (path_element->class) : E_NOTSUPP;
|
2011-04-07 16:27:46 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2011-04-20 16:59:06 +04:00
|
|
|
|
2011-05-27 19:28:46 +04:00
|
|
|
path_element->dir.info = info;
|
2011-04-20 16:59:06 +04:00
|
|
|
|
2011-05-27 19:28:46 +04:00
|
|
|
path_element->dir.converter =
|
2011-05-30 11:16:40 +04:00
|
|
|
(path_element->encoding !=
|
|
|
|
NULL) ? str_crt_conv_from (path_element->encoding) : str_cnv_from_term;
|
2011-05-27 19:28:46 +04:00
|
|
|
if (path_element->dir.converter == INVALID_CONV)
|
|
|
|
path_element->dir.converter = str_cnv_from_term;
|
2011-04-20 16:59:06 +04:00
|
|
|
|
2011-05-27 19:28:46 +04:00
|
|
|
handle = vfs_new_handle (path_element->class, path_element);
|
2011-04-20 16:59:06 +04:00
|
|
|
|
|
|
|
handlep = g_new (int, 1);
|
|
|
|
*handlep = handle;
|
|
|
|
return (DIR *) handlep;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct dirent *
|
|
|
|
mc_readdir (DIR * dirp)
|
|
|
|
{
|
|
|
|
int handle;
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
struct dirent *entry = NULL;
|
2011-05-27 19:28:46 +04:00
|
|
|
vfs_path_element_t *vfs_path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
estr_t state;
|
|
|
|
|
|
|
|
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
|
|
|
|
* (MAXNAMLEN + 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.
|
|
|
|
*/
|
|
|
|
mc_readdir_result = (struct dirent *) g_malloc (sizeof (struct dirent) + MAXNAMLEN + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dirp)
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
handle = *(int *) dirp;
|
|
|
|
|
|
|
|
vfs = vfs_class_find_by_handle (handle);
|
|
|
|
if (vfs == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2011-05-27 19:28:46 +04:00
|
|
|
vfs_path_element = vfs_class_data_find_by_handle (handle);
|
2011-04-07 16:27:46 +04:00
|
|
|
if (vfs->readdir)
|
|
|
|
{
|
2011-05-27 19:28:46 +04:00
|
|
|
entry = (*vfs->readdir) (vfs_path_element->dir.info);
|
2011-04-07 16:27:46 +04:00
|
|
|
if (entry == NULL)
|
|
|
|
return NULL;
|
|
|
|
g_string_set_size (vfs_str_buffer, 0);
|
2011-05-30 11:16:40 +04:00
|
|
|
state =
|
|
|
|
str_vfs_convert_from (vfs_path_element->dir.converter, entry->d_name, vfs_str_buffer);
|
2011-04-07 16:27:46 +04:00
|
|
|
mc_readdir_result->d_ino = entry->d_ino;
|
|
|
|
g_strlcpy (mc_readdir_result->d_name, vfs_str_buffer->str, MAXNAMLEN + 1);
|
|
|
|
}
|
|
|
|
if (entry == NULL)
|
|
|
|
errno = vfs->readdir ? vfs_ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return (entry != NULL) ? mc_readdir_result : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_closedir (DIR * dirp)
|
|
|
|
{
|
|
|
|
int handle = *(int *) dirp;
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
int result = -1;
|
|
|
|
|
|
|
|
vfs = vfs_class_find_by_handle (handle);
|
|
|
|
if (vfs != NULL)
|
|
|
|
{
|
2011-05-30 11:16:40 +04:00
|
|
|
vfs_path_element_t *vfs_path_element;
|
2011-05-27 19:28:46 +04:00
|
|
|
vfs_path_element = vfs_class_data_find_by_handle (handle);
|
|
|
|
if (vfs_path_element->dir.converter != str_cnv_from_term)
|
2011-06-01 14:56:52 +04:00
|
|
|
{
|
2011-05-27 19:28:46 +04:00
|
|
|
str_close_conv (vfs_path_element->dir.converter);
|
2011-06-01 14:56:52 +04:00
|
|
|
vfs_path_element->dir.converter = INVALID_CONV;
|
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-05-27 19:28:46 +04:00
|
|
|
result = vfs->closedir ? (*vfs->closedir) (vfs_path_element->dir.info) : -1;
|
2011-04-07 16:27:46 +04:00
|
|
|
vfs_free_handle (handle);
|
2011-05-27 19:28:46 +04:00
|
|
|
vfs_path_element_free (vfs_path_element);
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
g_free (dirp);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_stat (const char *filename, struct stat *buf)
|
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
int result = -1;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vpath = vfs_path_from_str (filename);
|
|
|
|
if (vpath == NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-20 16:59:06 +04:00
|
|
|
result = path_element->class->stat ? (*path_element->class->stat) (vpath, buf) : -1;
|
2011-04-18 16:08:13 +04:00
|
|
|
if (result == -1)
|
|
|
|
errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_lstat (const char *filename, struct stat *buf)
|
|
|
|
{
|
2011-04-18 16:08:13 +04:00
|
|
|
int result = -1;
|
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vpath = vfs_path_from_str (filename);
|
|
|
|
if (vpath == NULL)
|
2011-04-07 16:27:46 +04:00
|
|
|
return -1;
|
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-18 16:08:13 +04:00
|
|
|
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-20 16:59:06 +04:00
|
|
|
result = path_element->class->lstat ? (*path_element->class->lstat) (vpath, buf) : -1;
|
2011-04-18 16:08:13 +04:00
|
|
|
if (result == -1)
|
|
|
|
errno = path_element->class->name ? vfs_ferrno (path_element->class) : E_NOTSUPP;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_fstat (int handle, struct stat *buf)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
if (handle == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_class_find_by_handle (handle);
|
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = vfs->fstat ? (*vfs->fstat) (vfs_class_data_find_by_handle (handle), buf) : -1;
|
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->name ? vfs_ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Return current directory. If it's local, reread the current directory
|
|
|
|
* from the OS. Put directory to the provided buffer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
mc_get_current_wd (char *buffer, size_t size)
|
|
|
|
{
|
2011-04-29 15:13:29 +04:00
|
|
|
char *cwd = _vfs_get_cwd ();
|
2011-04-07 16:27:46 +04:00
|
|
|
|
|
|
|
g_strlcpy (buffer, cwd, size);
|
2011-04-29 15:13:29 +04:00
|
|
|
g_free (cwd);
|
|
|
|
|
2011-04-07 16:27:46 +04:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
char *
|
|
|
|
mc_getlocalcopy (const char *pathname)
|
|
|
|
{
|
|
|
|
char *result = NULL;
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vpath = vfs_path_from_str (pathname);
|
|
|
|
if (vpath == NULL)
|
|
|
|
return NULL;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-06-17 14:28:37 +04:00
|
|
|
|
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-18 16:08:13 +04:00
|
|
|
{
|
|
|
|
result = path_element->class->getlocalcopy != NULL ?
|
2011-06-01 15:53:08 +04:00
|
|
|
path_element->class->getlocalcopy (vpath) : mc_def_getlocalcopy (pathname);
|
2011-04-07 16:27:46 +04:00
|
|
|
if (result == NULL)
|
2011-04-18 16:08:13 +04:00
|
|
|
errno = vfs_ferrno (path_element->class);
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed)
|
|
|
|
{
|
|
|
|
int return_value = -1;
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-18 16:08:13 +04:00
|
|
|
vpath = vfs_path_from_str (pathname);
|
|
|
|
if (vpath == NULL)
|
|
|
|
return -1;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-06-17 14:28:37 +04:00
|
|
|
|
|
|
|
if (vfs_path_element_valid (path_element))
|
2011-04-18 16:08:13 +04:00
|
|
|
{
|
|
|
|
return_value = path_element->class->ungetlocalcopy != NULL ?
|
2011-04-18 18:08:55 +04:00
|
|
|
path_element->class->ungetlocalcopy (vpath, local,
|
2011-04-18 16:08:13 +04:00
|
|
|
has_changed) :
|
2011-06-01 15:53:08 +04:00
|
|
|
mc_def_ungetlocalcopy (path_element->class, path_element->path, local, has_changed);
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2011-04-18 16:08:13 +04:00
|
|
|
vfs_path_free (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
return return_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* VFS chdir.
|
|
|
|
* Return 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
mc_chdir (const char *path)
|
|
|
|
{
|
2011-04-20 16:59:06 +04:00
|
|
|
struct vfs_class *old_vfs;
|
2011-04-07 16:27:46 +04:00
|
|
|
vfsid old_vfsid;
|
|
|
|
int result;
|
2011-04-20 16:59:06 +04:00
|
|
|
vfs_path_t *vpath;
|
|
|
|
vfs_path_element_t *path_element;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
vpath = vfs_path_from_str (path);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
if (vpath == NULL)
|
|
|
|
return -1;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-06-17 14:28:37 +04:00
|
|
|
if (!vfs_path_element_valid (path_element) || path_element->class->chdir == NULL)
|
|
|
|
{
|
|
|
|
vfs_path_free (vpath);
|
2011-04-20 16:59:06 +04:00
|
|
|
return -1;
|
2011-06-17 14:28:37 +04:00
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
result = (*path_element->class->chdir) (vpath);
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
if (result == -1)
|
|
|
|
{
|
|
|
|
errno = vfs_ferrno (path_element->class);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
old_vfsid = vfs_getid (vfs_get_raw_current_dir ());
|
|
|
|
old_vfs = current_vfs;
|
2011-04-07 16:27:46 +04:00
|
|
|
|
2011-04-20 16:59:06 +04:00
|
|
|
/* Actually change directory */
|
2011-04-29 15:13:29 +04:00
|
|
|
vfs_set_raw_current_dir (vpath);
|
2011-04-20 16:59:06 +04:00
|
|
|
current_vfs = path_element->class;
|
|
|
|
|
|
|
|
/* This function uses the new current_dir implicitly */
|
|
|
|
vfs_stamp_create (old_vfs, old_vfsid);
|
|
|
|
|
|
|
|
/* Sometimes we assume no trailing slash on cwd */
|
2011-04-29 15:13:29 +04:00
|
|
|
path_element = vfs_path_get_by_index (vfs_get_raw_current_dir (), -1);
|
2011-06-17 14:28:37 +04:00
|
|
|
if (vfs_path_element_valid (path_element) && (*path_element->path != '\0'))
|
2011-04-07 16:27:46 +04:00
|
|
|
{
|
2011-04-20 16:59:06 +04:00
|
|
|
char *p;
|
2011-04-29 15:13:29 +04:00
|
|
|
p = strchr (path_element->path, 0) - 1;
|
|
|
|
if (*p == PATH_SEP && p > path_element->path)
|
2011-04-20 16:59:06 +04:00
|
|
|
*p = 0;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
2011-04-20 16:59:06 +04:00
|
|
|
return 0;
|
2011-04-07 16:27:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
off_t
|
|
|
|
mc_lseek (int fd, off_t offset, int whence)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
off_t result;
|
|
|
|
|
|
|
|
if (fd == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
vfs = vfs_class_find_by_handle (fd);
|
|
|
|
if (vfs == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
result = vfs->lseek ? (*vfs->lseek) (vfs_class_data_find_by_handle (fd), offset, whence) : -1;
|
|
|
|
if (result == -1)
|
|
|
|
errno = vfs->lseek ? vfs_ferrno (vfs) : E_NOTSUPP;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|