Changed interface of functions mc_link() and mc_rename()

...to 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 00:51:42 +03:00
parent 3f6f89bc49
commit b27f686f87
5 changed files with 53 additions and 60 deletions

View File

@ -313,22 +313,14 @@ MC_HANDLEOP (write, (int handle, const void *buf, size_t nbyte), (vfs_class_data
/* --------------------------------------------------------------------------------------------- */
#define MC_RENAMEOP(name) \
int mc_##name (const char *fname1, const char *fname2) \
int mc_##name (const vfs_path_t *vpath1, const vfs_path_t *vpath2) \
{ \
int result; \
vfs_path_t *vpath1, *vpath2; \
vfs_path_element_t *path_element1, *path_element2; \
\
vpath1 = vfs_path_from_str (fname1); \
if (vpath1 == NULL) \
if (vpath1 == NULL || vpath2 == NULL) \
return -1; \
\
vpath2 = vfs_path_from_str (fname2); \
if (vpath2 == NULL) \
{ \
vfs_path_free(vpath1); \
return -1; \
}\
path_element1 = vfs_path_get_by_index (vpath1, - 1); \
path_element2 = vfs_path_get_by_index (vpath2, - 1); \
\
@ -336,8 +328,6 @@ int mc_##name (const char *fname1, const char *fname2) \
path_element1->class != path_element2->class) \
{ \
errno = EXDEV; \
vfs_path_free(vpath1); \
vfs_path_free(vpath2); \
return -1; \
}\
\
@ -346,8 +336,6 @@ int mc_##name (const char *fname1, const char *fname2) \
: -1; \
if (result == -1) \
errno = path_element1->class->name != NULL ? vfs_ferrno (path_element1->class) : E_NOTSUPP; \
vfs_path_free(vpath1); \
vfs_path_free(vpath2); \
return result; \
}

View File

@ -284,13 +284,13 @@ struct dirent *mc_readdir (DIR * dirp);
int mc_closedir (DIR * dir);
int mc_stat (const vfs_path_t * vpath, struct stat *buf);
int mc_mknod (const vfs_path_t * vpath, mode_t mode, dev_t dev);
int mc_link (const char *name1, const char *name2);
int mc_link (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
int mc_mkdir (const vfs_path_t * vpath, mode_t mode);
int mc_rmdir (const vfs_path_t * vpath);
int mc_fstat (int fd, struct stat *buf);
int mc_lstat (const vfs_path_t * vpath, struct stat *buf);
int mc_symlink (const char *name1, const char *name2);
int mc_rename (const char *original, const char *target);
int mc_rename (const vfs_path_t * vpath1, const vfs_path_t * vpath2);
int mc_chmod (const vfs_path_t * vpath, mode_t mode);
int mc_chown (const vfs_path_t * vpath, uid_t owner, gid_t group);
int mc_chdir (const vfs_path_t * vpath);

View File

@ -125,6 +125,7 @@ edit_save_file (WEdit * edit, const char *filename)
gchar *real_filename;
int this_save_mode, fd = -1;
vfs_path_t *real_filename_vpath;
vfs_path_t *savename_vpath;
if (!filename)
return 0;
@ -234,15 +235,10 @@ edit_save_file (WEdit * edit, const char *filename)
else
savename = g_strdup (real_filename);
{
int ret;
vfs_path_t *savename_vpath;
savename_vpath = vfs_path_from_str (savename);
savename_vpath = vfs_path_from_str (savename);
ret = mc_chown (savename_vpath, edit->stat1.st_uid, edit->stat1.st_gid);
ret = mc_chmod (savename_vpath, edit->stat1.st_mode);
vfs_path_free (savename_vpath);
}
(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);
if (fd == -1)
@ -285,7 +281,6 @@ edit_save_file (WEdit * edit, const char *filename)
}
else if (edit->lb == LB_ASIS)
{ /* do not change line breaks */
vfs_path_t *savename_vpath;
long buf;
buf = 0;
filelen = edit->last_byte;
@ -335,11 +330,7 @@ edit_save_file (WEdit * edit, const char *filename)
/* Update the file information, especially the mtime. */
savename_vpath = vfs_path_from_str (savename);
if (mc_stat (savename_vpath, &edit->stat1) == -1)
{
vfs_path_free (savename_vpath);
goto error_save;
}
vfs_path_free (savename_vpath);
}
else
{ /* change line breaks */
@ -370,21 +361,24 @@ edit_save_file (WEdit * edit, const char *filename)
if (this_save_mode == EDIT_DO_BACKUP)
{
vfs_path_t *tmp_vpath;
gboolean ok;
assert (option_backup_ext != NULL);
tmp = g_strconcat (real_filename, option_backup_ext, (char *) NULL);
if (mc_rename (real_filename, tmp) == -1)
{
g_free (tmp);
tmp_vpath = vfs_path_append_new (real_filename_vpath, option_backup_ext, (char *) NULL);
ok = (mc_rename (real_filename_vpath, tmp_vpath) != -1);
vfs_path_free (tmp_vpath);
if (!ok)
goto error_save;
}
}
if (this_save_mode != EDIT_QUICK_SAVE)
if (mc_rename (savename, real_filename) == -1)
if (mc_rename (savename_vpath, real_filename_vpath) == -1)
goto error_save;
g_free (savename);
g_free (real_filename);
g_free (savename);
vfs_path_free (real_filename_vpath);
vfs_path_free (savename_vpath);
return 1;
error_save:
/* FIXME: Is this safe ?
@ -392,8 +386,9 @@ edit_save_file (WEdit * edit, const char *filename)
* mc_unlink (savename);
*/
g_free (real_filename);
vfs_path_free (real_filename_vpath);
g_free (savename);
vfs_path_free (real_filename_vpath);
vfs_path_free (savename_vpath);
return 0;
}

View File

@ -440,7 +440,9 @@ static void
do_link (link_type_t link_type, const char *fname)
{
char *dest = NULL, *src = NULL;
vfs_path_t *fname_vpath, *dest_vpath = NULL;
fname_vpath = vfs_path_from_str (fname);
if (link_type == LINK_HARDLINK)
{
src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
@ -448,7 +450,8 @@ do_link (link_type_t link_type, const char *fname)
if (!dest || !*dest)
goto cleanup;
save_cwds_stat ();
if (-1 == mc_link (fname, dest))
dest_vpath = vfs_path_from_str (dest);
if (-1 == mc_link (fname_vpath, dest_vpath))
message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
}
else
@ -483,6 +486,8 @@ do_link (link_type_t link_type, const char *fname)
repaint_screen ();
cleanup:
vfs_path_free (fname_vpath);
vfs_path_free (dest_vpath);
g_free (src);
g_free (dest);
}

View File

@ -271,7 +271,7 @@ static gboolean
check_hardlinks (const char *src_name, const char *dst_name, struct stat *pstat)
{
struct link *lp;
vfs_path_t *vpath;
vfs_path_t *src_vpath, *dst_vpath;
struct vfs_class *my_vfs;
ino_t ino = pstat->st_ino;
@ -279,26 +279,27 @@ check_hardlinks (const char *src_name, const char *dst_name, struct stat *pstat)
struct stat link_stat;
const char *p;
vpath = vfs_path_from_str (src_name);
src_vpath = vfs_path_from_str (src_name);
if ((vfs_file_class_flags (vpath) & VFSF_NOLINKS) != 0)
if ((vfs_file_class_flags (src_vpath) & VFSF_NOLINKS) != 0)
{
vfs_path_free (vpath);
vfs_path_free (src_vpath);
return FALSE;
}
my_vfs = vfs_path_get_by_index (vpath, -1)->class;
vfs_path_free (vpath);
my_vfs = vfs_path_get_by_index (src_vpath, -1)->class;
dst_vpath = vfs_path_from_str (dst_name);
for (lp = linklist; lp != NULL; lp = lp->next)
if (lp->vfs == my_vfs && lp->ino == ino && lp->dev == dev)
{
struct vfs_class *lp_name_class;
int stat_result;
vfs_path_t *tmp_vpath;
vpath = vfs_path_from_str (lp->name);
lp_name_class = vfs_path_get_by_index (vpath, -1)->class;
stat_result = mc_stat (vpath, &link_stat);
vfs_path_free (vpath);
tmp_vpath = vfs_path_from_str (lp->name);
lp_name_class = vfs_path_get_by_index (tmp_vpath, -1)->class;
stat_result = mc_stat (tmp_vpath, &link_stat);
vfs_path_free (tmp_vpath);
if (!stat_result && link_stat.st_ino == ino
&& link_stat.st_dev == dev && lp_name_class == my_vfs)
@ -308,28 +309,30 @@ check_hardlinks (const char *src_name, const char *dst_name, struct stat *pstat)
p = strchr (lp->name, 0) + 1; /* i.e. where the `name' file
was copied to */
vpath = vfs_path_from_str (dst_name);
dst_name_class = vfs_path_get_by_index (vpath, -1)->class;
vfs_path_free (vpath);
dst_name_class = vfs_path_get_by_index (dst_vpath, -1)->class;
vpath = vfs_path_from_str (p);
p_class = vfs_path_get_by_index (vpath, -1)->class;
tmp_vpath = vfs_path_from_str (p);
p_class = vfs_path_get_by_index (tmp_vpath, -1)->class;
if (dst_name_class == p_class)
{
if (!mc_stat (vpath, &link_stat))
if (!mc_stat (tmp_vpath, &link_stat))
{
if (!mc_link (p, dst_name))
if (!mc_link (tmp_vpath, dst_vpath))
{
vfs_path_free (vpath);
vfs_path_free (tmp_vpath);
vfs_path_free (src_vpath);
vfs_path_free (dst_vpath);
return TRUE;
}
}
}
vfs_path_free (vpath);
vfs_path_free (tmp_vpath);
}
message (D_ERROR, MSG_ERROR, _("Cannot make the hardlink"));
vfs_path_free (src_vpath);
vfs_path_free (dst_vpath);
return FALSE;
}
lp = (struct link *) g_try_malloc (sizeof (struct link) + strlen (src_name)
@ -346,6 +349,8 @@ check_hardlinks (const char *src_name, const char *dst_name, struct stat *pstat)
lp->next = linklist;
linklist = lp;
}
vfs_path_free (src_vpath);
vfs_path_free (dst_vpath);
return FALSE;
}
@ -862,7 +867,7 @@ move_file_file (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s, c
}
}
if (mc_rename (s, d) == 0)
if (mc_rename (src_vpath, dst_vpath) == 0)
{
vfs_path_free (src_vpath);
vfs_path_free (dst_vpath);
@ -1991,7 +1996,7 @@ copy_dir_dir (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s, con
/* Here the dir doesn't exist : make it ! */
if (move_over)
{
if (mc_rename (s, d) == 0)
if (mc_rename (src_vpath, dst_vpath) == 0)
{
return_status = FILE_CONT;
goto ret;
@ -2260,7 +2265,7 @@ move_dir_dir (FileOpTotalContext * tctx, FileOpContext * ctx, const char *s, con
}
retry_rename:
if (mc_rename (s, destdir) == 0)
if (mc_rename (src_vpath, destdir_vpath) == 0)
{
return_status = FILE_CONT;
goto ret;