Changed interface of function mc_open() for handle vfs_path_t object as parameter

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-07-21 23:33:45 +03:00
parent 0a7caff306
commit 4945d0f9a6
12 changed files with 124 additions and 67 deletions

View File

@ -56,21 +56,25 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
gboolean ret;
int fd;
ssize_t cur_written;
vfs_path_t *ini_vpath;
ini_vpath = vfs_path_from_str (ini_path);
data = g_key_file_to_data (mc_config->handle, &len, NULL);
if (!exist_file (ini_path))
{
ret = g_file_set_contents (ini_path, data, len, error);
g_free (data);
vfs_path_free (ini_vpath);
return ret;
}
mc_util_make_backup_if_possible (ini_path, "~");
fd = mc_open (ini_path, O_WRONLY | O_TRUNC, 0);
fd = mc_open (ini_vpath, O_WRONLY | O_TRUNC, 0);
if (fd == -1)
{
g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
g_free (data);
vfs_path_free (ini_vpath);
return FALSE;
}
@ -84,10 +88,12 @@ mc_config_new_or_override_file (mc_config_t * mc_config, const gchar * ini_path,
{
mc_util_restore_from_backup_if_possible (ini_path, "~");
g_propagate_error (error, g_error_new (MC_ERROR, 0, "%s", unix_error_string (errno)));
vfs_path_free (ini_vpath);
return FALSE;
}
mc_util_unlink_backup_if_possible (ini_path, "~");
vfs_path_free (ini_vpath);
return TRUE;
}

View File

@ -75,21 +75,22 @@ struct dirent *mc_readdir_result = NULL;
static char *
mc_def_getlocalcopy (const char *filename)
{
char *tmp;
int fdin, fdout;
char *tmp = NULL;
int fdin = -1, fdout = -1;
ssize_t i;
char buffer[8192];
char buffer[BUF_1K * 8];
struct stat mystat;
vfs_path_t *vpath = vfs_path_from_str (filename);
vfs_path_t *vpath;
fdin = mc_open (filename, O_RDONLY | O_LINEAR);
vpath = vfs_path_from_str (filename);
fdin = mc_open (vpath, O_RDONLY | O_LINEAR);
if (fdin == -1)
return NULL;
goto fail;
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)
@ -129,10 +130,14 @@ static int
mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
const char *local, int has_changed)
{
vfs_path_t *vpath;
int fdin = -1, fdout = -1;
vpath = vfs_path_from_str (filename);
if (has_changed)
{
char buffer[8192];
char buffer[BUF_1K * 8];
ssize_t i;
if (!vfs->write)
@ -141,7 +146,7 @@ mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
fdin = open (local, O_RDONLY);
if (fdin == -1)
goto failed;
fdout = mc_open (filename, O_WRONLY | O_TRUNC);
fdout = mc_open (vpath, O_WRONLY | O_TRUNC);
if (fdout == -1)
goto failed;
while ((i = read (fdin, buffer, sizeof (buffer))) > 0)
@ -163,6 +168,7 @@ mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
}
}
unlink (local);
vfs_path_free (vpath);
return 0;
failed:
@ -172,6 +178,7 @@ mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
if (fdin != -1)
close (fdin);
unlink (local);
vfs_path_free (vpath);
return -1;
}
@ -180,13 +187,11 @@ mc_def_ungetlocalcopy (struct vfs_class *vfs, const char *filename,
/* --------------------------------------------------------------------------------------------- */
int
mc_open (const char *filename, int flags, ...)
mc_open (const vfs_path_t * vpath, int flags, ...)
{
int mode = 0, result = -1;
vfs_path_t *vpath;
vfs_path_element_t *path_element;
vpath = vfs_path_from_str (filename);
if (vpath == NULL)
return -1;
@ -213,7 +218,6 @@ mc_open (const char *filename, int flags, ...)
else
errno = -EOPNOTSUPP;
vfs_path_free (vpath);
return result;
}
@ -256,7 +260,7 @@ MC_NAMEOP (mknod, (const vfs_path_t *vpath, mode_t mode, dev_t dev), (vpath, mod
/* --------------------------------------------------------------------------------------------- */
int
mc_symlink (const vfs_path_t *vpath1, const vfs_path_t *vpath2)
mc_symlink (const vfs_path_t * vpath1, const vfs_path_t * vpath2)
{
int result = -1;

View File

@ -297,7 +297,7 @@ int mc_chdir (const vfs_path_t * vpath);
int mc_unlink (const vfs_path_t * vpath);
int mc_ctl (int fd, int ctlop, void *arg);
int mc_setctl (const vfs_path_t * vpath, int ctlop, void *arg);
int mc_open (const char *filename, int flags, ...);
int mc_open (const vfs_path_t * vpath, int flags, ...);
char *mc_get_current_wd (char *buffer, size_t bufsize);
char *mc_getlocalcopy (const char *pathname);
int mc_ungetlocalcopy (const char *pathname, const char *local, int has_changed);

View File

@ -126,7 +126,7 @@ clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name
gpointer init_data, gpointer data)
{
int file;
char *fname = NULL;
vfs_path_t *fname_vpath = NULL;
ssize_t ret;
size_t str_len;
const char *text = (const char *) data;
@ -138,10 +138,10 @@ clipboard_text_to_file (const gchar * event_group_name, const gchar * event_name
if (text == NULL)
return FALSE;
fname = mc_config_get_full_path (EDIT_CLIP_FILE);
file = mc_open (fname, O_CREAT | O_WRONLY | O_TRUNC,
fname_vpath = mc_config_get_full_vpath (EDIT_CLIP_FILE);
file = mc_open (fname_vpath, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
g_free (fname);
vfs_path_free (fname_vpath);
if (file == -1)
return TRUE;

View File

@ -253,11 +253,14 @@ edit_load_file_fast (WEdit * edit, const char *filename)
long buf, buf2;
int file = -1;
int ret = 1;
vfs_path_t *vpath;
edit->curs2 = edit->last_byte;
buf2 = edit->curs2 >> S_EDIT_BUF_SIZE;
file = mc_open (filename, O_RDONLY | O_BINARY);
vpath = vfs_path_from_str (filename);
file = mc_open (vpath, O_RDONLY | O_BINARY);
vfs_path_free (vpath);
if (file == -1)
{
gchar *errmsg;
@ -362,9 +365,11 @@ check_file_access (WEdit * edit, const char *filename, struct stat *st)
{
int file;
gchar *errmsg = NULL;
vfs_path_t *vpath;
/* Try opening an existing file */
file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
vpath = vfs_path_from_str (filename);
file = mc_open (vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
if (file < 0)
{
@ -372,7 +377,7 @@ check_file_access (WEdit * edit, const char *filename, struct stat *st)
* Try creating the file. O_EXCL prevents following broken links
* and opening existing files.
*/
file = mc_open (filename, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
file = mc_open (vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
if (file < 0)
{
errmsg = g_strdup_printf (_("Cannot open %s for reading"), filename);
@ -411,6 +416,7 @@ check_file_access (WEdit * edit, const char *filename, struct stat *st)
cleanup:
(void) mc_close (file);
vfs_path_free (vpath);
if (errmsg != NULL)
{
@ -2076,40 +2082,44 @@ edit_write_stream (WEdit * edit, FILE * f)
long
edit_insert_file (WEdit * edit, const char *filename)
{
char *p;
char *p = NULL;
long ins_len = 0;
vfs_path_t *vpath;
vpath = vfs_path_from_str (filename);
p = edit_get_filter (filename);
if (p != NULL)
{
FILE *f;
long current = edit->curs1;
f = (FILE *) popen (p, "r");
if (f != NULL)
{
edit_insert_stream (edit, f);
ins_len = edit->curs1 - current;
edit_cursor_move (edit, current - edit->curs1);
edit_cursor_move (edit, -ins_len);
if (pclose (f) > 0)
{
char *errmsg;
errmsg = g_strdup_printf (_("Error reading from pipe: %s"), p);
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
g_free (p);
return -1;
ins_len = -1;
goto ret;
}
}
else
{
char *errmsg;
errmsg = g_strdup_printf (_("Cannot open pipe for reading: %s"), p);
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
g_free (p);
return -1;
ins_len = -1;
goto ret;
}
g_free (p);
}
else
{
@ -2117,9 +2127,13 @@ edit_insert_file (WEdit * edit, const char *filename)
long current = edit->curs1;
int vertical_insertion = 0;
char *buf;
file = mc_open (filename, O_RDONLY | O_BINARY);
file = mc_open (vpath, O_RDONLY | O_BINARY);
if (file == -1)
return -1;
{
ins_len = -1;
goto ret;
}
buf = g_malloc0 (TEMP_BUF_LEN);
blocklen = mc_read (file, buf, sizeof (VERTICAL_MAGIC));
if (blocklen > 0)
@ -2130,10 +2144,12 @@ edit_insert_file (WEdit * edit, const char *filename)
else
mc_lseek (file, 0, SEEK_SET);
}
if (vertical_insertion)
{
long mark1, mark2;
int c1, c2;
blocklen = edit_insert_column_of_text_from_file (edit, file, &mark1, &mark2, &c1, &c2);
edit_set_markers (edit, edit->curs1, mark2, c1, c2);
/* highlight inserted text then not persistent blocks */
@ -2160,14 +2176,19 @@ edit_insert_file (WEdit * edit, const char *filename)
edit->column_highlight = 0;
}
}
edit->force |= REDRAW_PAGE;
ins_len = edit->curs1 - current;
edit_cursor_move (edit, current - edit->curs1);
edit_cursor_move (edit, -ins_len);
g_free (buf);
mc_close (file);
if (blocklen != 0)
return 0;
ins_len = 0;
}
ret:
g_free (p);
vfs_path_free (vpath);
return ins_len;
}

View File

@ -146,7 +146,7 @@ edit_save_file (WEdit * edit, const char *filename)
if (this_save_mode != EDIT_QUICK_SAVE)
{
if (!vfs_file_is_local (real_filename_vpath)
|| (fd = mc_open (real_filename, O_RDONLY | O_BINARY)) == -1)
|| (fd = mc_open (real_filename_vpath, O_RDONLY | O_BINARY)) == -1)
{
/*
* The file does not exists yet, so no safe save or
@ -240,7 +240,7 @@ edit_save_file (WEdit * edit, const char *filename)
(void) mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
(void) mc_chmod (savename_vpath, edit->stat1.st_mode);
fd = mc_open (savename, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
fd = mc_open (savename_vpath, O_CREAT | O_WRONLY | O_TRUNC | O_BINARY, edit->stat1.st_mode);
if (fd == -1)
goto error_save;
@ -1493,11 +1493,16 @@ edit_save_as_cmd (WEdit * edit)
else
{
int rv;
if (strcmp (edit->filename, exp))
{
int file;
vfs_path_t *tmp_vpath;
tmp_vpath = vfs_path_from_str (exp);
different_filename = 1;
file = mc_open (exp, O_RDONLY | O_BINARY);
file = mc_open (tmp_vpath, O_RDONLY | O_BINARY);
vfs_path_free (tmp_vpath);
if (file != -1)
{
/* the file exists */
@ -2591,19 +2596,24 @@ int
edit_save_block (WEdit * edit, const char *filename, long start, long finish)
{
int len, file;
vfs_path_t *vpath;
file = mc_open (filename, O_CREAT | O_WRONLY | O_TRUNC,
vpath = vfs_path_from_str (filename);
file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY);
vfs_path_free (vpath);
if (file == -1)
return 0;
if (edit->column_highlight)
{
int r;
r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC));
if (r > 0)
{
unsigned char *block, *p;
p = block = edit_get_block (edit, start, finish, &len);
while (len)
{
@ -2620,6 +2630,7 @@ edit_save_block (WEdit * edit, const char *filename, long start, long finish)
{
unsigned char *buf;
int i = start, end;
len = finish - start;
buf = g_malloc0 (TEMP_BUF_LEN);
while (start != finish)

View File

@ -1560,7 +1560,7 @@ copy_file_file (FileOpTotalContext * tctx, FileOpContext * ctx,
gettimeofday (&tv_transfer_start, (struct timezone *) NULL);
while ((src_desc = mc_open (src_path, O_RDONLY | O_LINEAR)) < 0 && !ctx->skip_all)
while ((src_desc = mc_open (src_vpath, O_RDONLY | O_LINEAR)) < 0 && !ctx->skip_all)
{
return_status = file_error (_("Cannot open source file \"%s\"\n%s"), src_path);
if (return_status == FILE_RETRY)
@ -1618,7 +1618,7 @@ copy_file_file (FileOpTotalContext * tctx, FileOpContext * ctx,
open_flags |= O_CREAT | O_EXCL;
}
while ((dest_desc = mc_open (dst_path, open_flags, src_mode)) < 0)
while ((dest_desc = mc_open (dst_vpath, open_flags, src_mode)) < 0)
{
if (errno != EEXIST)
{

View File

@ -979,23 +979,19 @@ search_content (Dlg_head * h, const char *directory, const char *filename)
{
struct stat s;
char buffer[BUF_4K];
char *fname = NULL;
int file_fd;
gboolean ret_val = FALSE;
vfs_path_t *vpath;
fname = mc_build_filename (directory, filename, (char *) NULL);
vpath = vfs_path_from_str (fname);
vpath = vfs_path_build_filename (directory, filename, (char *) NULL);
if (mc_stat (vpath, &s) != 0 || !S_ISREG (s.st_mode))
{
g_free (fname);
vfs_path_free (vpath);
return FALSE;
}
file_fd = mc_open (fname, O_RDONLY);
g_free (fname);
file_fd = mc_open (vpath, O_RDONLY);
vfs_path_free (vpath);
if (file_fd == -1)

View File

@ -221,17 +221,19 @@ cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const vfs_
cpio_super_data_t *arch;
mode_t mode;
struct vfs_s_inode *root;
char *name = vfs_path_to_str (vpath);
fd = mc_open (name, O_RDONLY);
fd = mc_open (vpath, O_RDONLY);
if (fd == -1)
{
char *name;
name = vfs_path_to_str (vpath);
message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), name);
g_free (name);
return -1;
}
super->name = g_strdup (name);
super->name = vfs_path_to_str (vpath);
super->data = g_new (cpio_super_data_t, 1);
arch = (cpio_super_data_t *) super->data;
arch->fd = -1; /* for now */
@ -239,19 +241,22 @@ cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const vfs_
arch->type = CPIO_UNKNOWN;
arch->deferred = NULL;
type = get_compression_type (fd, name);
type = get_compression_type (fd, super->name);
if (type != COMPRESSION_NONE)
{
char *s;
vfs_path_t *tmp_vpath;
mc_close (fd);
s = g_strconcat (name, decompress_extension (type), (char *) NULL);
fd = mc_open (s, O_RDONLY);
s = g_strconcat (super->name, decompress_extension (type), (char *) NULL);
tmp_vpath = vfs_path_from_str (s);
fd = mc_open (tmp_vpath, O_RDONLY);
vfs_path_free (tmp_vpath);
if (fd == -1)
{
message (D_ERROR, MSG_ERROR, _("Cannot open cpio archive\n%s"), s);
g_free (s);
g_free (name);
g_free (super->name);
return -1;
}
g_free (s);
@ -271,7 +276,6 @@ cpio_open_cpio_file (struct vfs_class *me, struct vfs_s_super *super, const vfs_
super->root = root;
CPIO_SEEK_SET (super, 0);
g_free (name);
return fd;
}

View File

@ -287,17 +287,19 @@ tar_open_archive_int (struct vfs_class *me, const vfs_path_t * vpath, struct vfs
tar_super_data_t *arch;
mode_t mode;
struct vfs_s_inode *root;
char *archive_name = vfs_path_to_str (vpath);
result = mc_open (archive_name, O_RDONLY);
result = mc_open (vpath, O_RDONLY);
if (result == -1)
{
message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), archive_name);
g_free (archive_name);
char *name;
name = vfs_path_to_str (vpath);
message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), name);
g_free (name);
ERRNOR (ENOENT, -1);
}
archive->name = archive_name;
archive->name = vfs_path_to_str (vpath);
archive->data = g_new (tar_super_data_t, 1);
arch = (tar_super_data_t *) archive->data;
mc_stat (vpath, &arch->st);
@ -305,19 +307,26 @@ tar_open_archive_int (struct vfs_class *me, const vfs_path_t * vpath, struct vfs
arch->type = TAR_UNKNOWN;
/* Find out the method to handle this tar file */
type = get_compression_type (result, archive_name);
type = get_compression_type (result, archive->name);
mc_lseek (result, 0, SEEK_SET);
if (type != COMPRESSION_NONE)
{
char *s;
vfs_path_t *tmp_vpath;
mc_close (result);
s = g_strconcat (archive->name, decompress_extension (type), (char *) NULL);
result = mc_open (s, O_RDONLY);
tmp_vpath = vfs_path_from_str (s);
result = mc_open (tmp_vpath, O_RDONLY);
vfs_path_free (tmp_vpath);
if (result == -1)
message (D_ERROR, MSG_ERROR, _("Cannot open tar archive\n%s"), s);
g_free (s);
if (result == -1)
{
g_free (archive->name);
ERRNOR (ENOENT, -1);
}
}
arch->fd = result;

View File

@ -347,10 +347,13 @@ mcview_hexedit_save_changes (mcview_t * view)
int fp;
char *text;
struct hexedit_change_node *curr, *next;
vfs_path_t *tmp_vpath;
assert (view->filename != NULL);
fp = mc_open (view->filename, O_WRONLY);
tmp_vpath = vfs_path_from_str (view->filename);
fp = mc_open (tmp_vpath, O_WRONLY);
vfs_path_free (tmp_vpath);
if (fp != -1)
{
for (curr = view->change_list; curr != NULL; curr = next)

View File

@ -273,6 +273,7 @@ gboolean
mcview_load (mcview_t * view, const char *command, const char *file, int start_line)
{
gboolean retval = FALSE;
vfs_path_t *vpath = NULL;
assert (view->bytes_per_line != 0);
@ -312,7 +313,8 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
struct stat st;
/* Open the file */
fd = mc_open (file, O_RDONLY | O_NONBLOCK);
vpath = vfs_path_from_str (file);
fd = mc_open (vpath, O_RDONLY | O_NONBLOCK);
if (fd == -1)
{
g_snprintf (tmp, sizeof (tmp), _("Cannot open \"%s\"\n%s"),
@ -386,10 +388,10 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
char *canon_fname;
long line, col;
off_t new_offset, max_offset;
vfs_path_t *vpath;
vfs_path_t *tmp_vpath;
vpath = vfs_path_from_str (view->filename);
canon_fname = vfs_path_to_str (vpath);
tmp_vpath = vfs_path_from_str (view->filename);
canon_fname = vfs_path_to_str (tmp_vpath);
load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks);
max_offset = mcview_get_filesize (view) - 1;
if (max_offset < 0)
@ -404,7 +406,7 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
view->hex_cursor = new_offset;
}
g_free (canon_fname);
vfs_path_free (vpath);
vfs_path_free (tmp_vpath);
}
else if (start_line > 0)
mcview_moveto (view, start_line - 1, 0);
@ -412,6 +414,7 @@ mcview_load (mcview_t * view, const char *command, const char *file, int start_l
view->hexedit_lownibble = FALSE;
view->hexview_in_text = FALSE;
view->change_list = NULL;
vfs_path_free (vpath);
return retval;
}