From 12a9390524f6ff9782ce05bad7ff99c756eb9721 Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Thu, 3 Nov 2011 00:34:35 +0300 Subject: [PATCH] Changed lock_file() and unlock_file() functions ...to handle vfs_path_t objects. Signed-off-by: Slava Zanko --- lib/lock.c | 80 +++++++++++++++++++++++--------------------- lib/lock.h | 6 ++-- src/editor/edit.c | 12 +++---- src/editor/editcmd.c | 26 ++++++++------ src/viewer/lib.c | 12 +++---- 5 files changed, 72 insertions(+), 64 deletions(-) diff --git a/lib/lock.c b/lib/lock.c index 43dd126c6..a9db6a260 100644 --- a/lib/lock.c +++ b/lib/lock.c @@ -193,45 +193,44 @@ lock_get_info (const char *lockfname) Warning: Might do screen refresh and lose edit->force */ int -lock_file (const char *fname) +lock_file (const vfs_path_t * fname_vpath) { - char *lockfname, *newlock, *msg, *lock; + char *lockfname = NULL, *newlock, *msg, *lock; struct stat statbuf; struct lock_s *lockinfo; - gboolean symlink_ok; - vfs_path_t *vpath; + gboolean is_local; + gboolean symlink_ok = FALSE; + char *fname_tmp, *fname; /* Just to be sure (and don't lock new file) */ - if (fname == NULL || *fname == '\0') - return 0; - - fname = tilde_expand (fname); - - vpath = vfs_path_from_str (fname); - - /* Locking on VFS is not supported */ - if (!vfs_file_is_local (vpath)) + fname_tmp = vfs_path_to_str (fname_vpath); + if (fname_tmp == NULL || *fname_tmp == '\0') { - g_free ((gpointer) fname); - vfs_path_free (vpath); + g_free (fname_tmp); return 0; } - vfs_path_free (vpath); - /* Check if already locked */ - lockfname = lock_build_symlink_name (fname); - g_free ((gpointer) fname); - if (lockfname == NULL) + fname = tilde_expand (fname_tmp); + g_free (fname_tmp); + + /* Locking on VFS is not supported */ + is_local = vfs_file_is_local (fname_vpath); + if (is_local) + { + /* Check if already locked */ + lockfname = lock_build_symlink_name (fname); + } + + g_free (fname); + + if (!is_local || lockfname == NULL) return 0; if (lstat (lockfname, &statbuf) == 0) { lock = lock_get_info (lockfname); if (lock == NULL) - { - g_free (lockfname); - return 0; - } + goto ret; lockinfo = lock_extract_info (lock); /* Check if locking process alive, ask user if required */ @@ -250,9 +249,9 @@ lock_file (const char *fname) break; case 1: case -1: - g_free (lockfname); g_free (msg); - return 0; + goto ret; + break; /* FIXME: unneeded? */ } g_free (msg); } @@ -263,8 +262,9 @@ lock_file (const char *fname) newlock = lock_build_name (); symlink_ok = (symlink (newlock, lockfname) != -1); g_free (newlock); - g_free (lockfname); + ret: + g_free (lockfname); return symlink_ok ? 1 : 0; } @@ -275,42 +275,44 @@ lock_file (const char *fname) */ int -unlock_file (const char *fname) +unlock_file (const vfs_path_t * fname_vpath) { char *lockfname, *lock; struct stat statbuf; + char *fname_tmp, *fname; /* Just to be sure */ - if (fname == NULL || *fname == '\0') + fname_tmp = vfs_path_to_str (fname_vpath); + if (fname_tmp == NULL || *fname_tmp == '\0') + { + g_free (fname_tmp); return 0; + } - fname = tilde_expand (fname); + fname = tilde_expand (fname_tmp); + g_free (fname_tmp); lockfname = lock_build_symlink_name (fname); - g_free ((gpointer) fname); + g_free (fname); if (lockfname == NULL) return 0; /* Check if lock exists */ if (lstat (lockfname, &statbuf) == -1) - { - g_free (lockfname); - return 0; - } + goto ret; lock = lock_get_info (lockfname); if (lock != NULL) { /* Don't touch if lock is not ours */ if (lock_extract_info (lock)->pid != getpid ()) - { - g_free (lockfname); - return 0; - } + goto ret; } /* Remove lock */ unlink (lockfname); + + ret: g_free (lockfname); return 0; } diff --git a/lib/lock.h b/lib/lock.h index 412f38c0b..7cca65fa2 100644 --- a/lib/lock.h +++ b/lib/lock.h @@ -9,6 +9,8 @@ #ifndef MC_LOCK_H #define MC_LOCK_H +#include "lib/vfs/vfs.h" /* vfs_path_t */ + /*** typedefs(not structures) and defined constants **********************************************/ /*** enums ***************************************************************************************/ @@ -19,8 +21,8 @@ /*** declarations of public functions ************************************************************/ -int lock_file (const char *fname); -int unlock_file (const char *fname); +int lock_file (const vfs_path_t * fname_vpath); +int unlock_file (const vfs_path_t * fname_vpath); /*** inline functions ****************************************************************************/ diff --git a/src/editor/edit.c b/src/editor/edit.c index 1d19588a8..3ca9c2b9c 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -4371,12 +4371,12 @@ edit_move_down (WEdit * edit, unsigned long i, int do_scroll) unsigned int edit_unlock_file (WEdit * edit) { - char *fullpath; + vfs_path_t *fullpath; unsigned int ret; - fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL); + fullpath = vfs_path_build_filename (edit->dir, edit->filename, (char *) NULL); ret = unlock_file (fullpath); - g_free (fullpath); + vfs_path_free (fullpath); return ret; } @@ -4386,12 +4386,12 @@ edit_unlock_file (WEdit * edit) unsigned int edit_lock_file (WEdit * edit) { - char *fullpath; + vfs_path_t *fullpath; unsigned int ret; - fullpath = mc_build_filename (edit->dir, edit->filename, (char *) NULL); + fullpath = vfs_path_build_filename (edit->dir, edit->filename, (char *) NULL); ret = lock_file (fullpath); - g_free (fullpath); + vfs_path_free (fullpath); return ret; } diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 2d43be84a..62fc6cc08 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -497,11 +497,11 @@ edit_load_file_from_filename (WEdit * edit, char *exp) if (prev_locked) { - char *fullpath; + vfs_path_t *fullpath; - fullpath = mc_build_filename (edit->dir, prev_filename, (char *) NULL); + fullpath = vfs_path_build_filename (edit->dir, prev_filename, (char *) NULL); unlock_file (fullpath); - g_free (fullpath); + vfs_path_free (fullpath); } g_free (prev_filename); return 0; @@ -1489,16 +1489,17 @@ edit_save_as_cmd (WEdit * edit) else { int rv; + vfs_path_t *exp_vpath; + + exp_vpath = vfs_path_from_str (exp); 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 (tmp_vpath, O_RDONLY | O_BINARY); - vfs_path_free (tmp_vpath); + file = mc_open (exp_vpath, O_RDONLY | O_BINARY); + if (file != -1) { /* the file exists */ @@ -1510,6 +1511,7 @@ edit_save_as_cmd (WEdit * edit) { edit->force |= REDRAW_COMPLETELY; g_free (exp); + vfs_path_free (exp_vpath); return 0; } } @@ -1517,13 +1519,13 @@ edit_save_as_cmd (WEdit * edit) { edit->stat1.st_mode |= S_IWUSR; } - save_lock = lock_file (exp); + save_lock = lock_file (exp_vpath); } else { /* filenames equal, check if already locked */ if (!edit->locked && !edit->delete_file) - save_lock = lock_file (exp); + save_lock = lock_file (exp_vpath); } if (different_filename) @@ -1543,7 +1545,7 @@ edit_save_as_cmd (WEdit * edit) if (different_filename) { if (save_lock) - unlock_file (exp); + unlock_file (exp_vpath); if (edit->locked) edit->locked = edit_unlock_file (edit); } @@ -1557,6 +1559,7 @@ edit_save_as_cmd (WEdit * edit) if (edit->lb != LB_ASIS) edit_reload (edit, exp); g_free (exp); + vfs_path_free (exp_vpath); edit->modified = 0; edit->delete_file = 0; if (different_filename) @@ -1569,8 +1572,9 @@ edit_save_as_cmd (WEdit * edit) case -1: /* Failed, so maintain modify (not save) lock */ if (save_lock) - unlock_file (exp); + unlock_file (exp_vpath); g_free (exp); + vfs_path_free (exp_vpath); edit->force |= REDRAW_COMPLETELY; return 0; } diff --git a/src/viewer/lib.c b/src/viewer/lib.c index a8d204820..404a50de6 100644 --- a/src/viewer/lib.c +++ b/src/viewer/lib.c @@ -435,12 +435,12 @@ mcview_get_title (const Dlg_head * h, size_t len) gboolean mcview_lock_file (mcview_t * view) { - char *fullpath; + vfs_path_t *fullpath; gboolean ret; - fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL); + fullpath = vfs_path_build_filename (view->workdir, view->filename, (char *) NULL); ret = lock_file (fullpath); - g_free (fullpath); + vfs_path_free (fullpath); return ret; } @@ -450,12 +450,12 @@ mcview_lock_file (mcview_t * view) gboolean mcview_unlock_file (mcview_t * view) { - char *fullpath; + vfs_path_t *fullpath; gboolean ret; - fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL); + fullpath = vfs_path_build_filename (view->workdir, view->filename, (char *) NULL); ret = unlock_file (fullpath); - g_free (fullpath); + vfs_path_free (fullpath); return ret; }