From 890f1f9242f28d5746a224ebb6a35473b9c07a72 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 21 Jul 2011 00:51:42 +0300 Subject: [PATCH] Changed interface of functions mc_link() and mc_rename() ...to handle vfs_path_t object as parameter. Signed-off-by: Slava Zanko --- lib/vfs/interface.c | 16 ++-------------- lib/vfs/vfs.h | 4 ++-- src/editor/editcmd.c | 37 ++++++++++++++++--------------------- src/filemanager/cmd.c | 7 ++++++- src/filemanager/file.c | 49 +++++++++++++++++++++++++++---------------------- 5 files changed, 53 insertions(+), 60 deletions(-) diff --git a/lib/vfs/interface.c b/lib/vfs/interface.c index f4e47aec9..fa3d41db7 100644 --- a/lib/vfs/interface.c +++ b/lib/vfs/interface.c @@ -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; \ } diff --git a/lib/vfs/vfs.h b/lib/vfs/vfs.h index fcc05f243..aac6ec55c 100644 --- a/lib/vfs/vfs.h +++ b/lib/vfs/vfs.h @@ -286,13 +286,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); diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index e06708225..a9f139bd6 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -127,6 +127,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; @@ -236,15 +237,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) @@ -287,7 +283,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; @@ -337,11 +332,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 */ @@ -372,23 +363,26 @@ edit_save_file (WEdit * edit, const char *filename) if (this_save_mode == EDIT_DO_BACKUP) { + vfs_path_t *tmp_vpath; + gboolean ok; + #ifdef HAVE_ASSERT_H assert (option_backup_ext != NULL); #endif - 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 ? @@ -396,8 +390,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; } diff --git a/src/filemanager/cmd.c b/src/filemanager/cmd.c index 79bd743e0..c8debbeea 100644 --- a/src/filemanager/cmd.c +++ b/src/filemanager/cmd.c @@ -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); } diff --git a/src/filemanager/file.c b/src/filemanager/file.c index 06821a5da..d1682146b 100644 --- a/src/filemanager/file.c +++ b/src/filemanager/file.c @@ -273,7 +273,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; @@ -281,26 +281,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) @@ -310,28 +311,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) @@ -348,6 +351,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; } @@ -864,7 +869,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); @@ -1993,7 +1998,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; @@ -2262,7 +2267,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; -- 2.11.4.GIT