From 4047403077860fa52750b34ac313aa9c8d94075e Mon Sep 17 00:00:00 2001 From: Slava Zanko Date: Mon, 5 Mar 2012 13:11:26 +0300 Subject: [PATCH] Fixed creation of lock files Signed-off-by: Slava Zanko --- lib/lock.c | 53 +++++++++++++++++------------------------------- src/editor/edit-impl.h | 3 --- src/editor/edit.c | 34 ++----------------------------- src/editor/editcmd.c | 8 ++++---- src/viewer/actions_cmd.c | 2 +- src/viewer/hex.c | 5 +++-- src/viewer/internal.h | 2 -- src/viewer/lib.c | 34 +------------------------------ 8 files changed, 30 insertions(+), 111 deletions(-) diff --git a/lib/lock.c b/lib/lock.c index a9db6a260..f59adfab5 100644 --- a/lib/lock.c +++ b/lib/lock.c @@ -113,20 +113,19 @@ lock_build_name (void) /* --------------------------------------------------------------------------------------------- */ static char * -lock_build_symlink_name (const char *fname) +lock_build_symlink_name (const vfs_path_t * fname_vpath) { - char *fname_copy, *symlink_name; - char absolute_fname[PATH_MAX]; + const char *elpath, *str_filename; + char *str_dirname, *symlink_name; - if (mc_realpath (fname, absolute_fname) == NULL) - return NULL; + /* get first path piece */ + elpath = vfs_path_get_by_index (fname_vpath, 0)->path; - fname = x_basename (absolute_fname); - fname_copy = g_strdup (fname); - absolute_fname[fname - absolute_fname] = '\0'; - symlink_name = g_strconcat (absolute_fname, ".#", fname_copy, (char *) NULL); - g_free (fname_copy); + str_filename = g_basename (elpath); + str_dirname = g_dirname (elpath); + symlink_name = g_strconcat (str_dirname, PATH_SEP_STR ".#", str_filename, (char *) NULL); + g_free (str_dirname); return symlink_name; } @@ -200,29 +199,21 @@ lock_file (const vfs_path_t * fname_vpath) struct lock_s *lockinfo; gboolean is_local; gboolean symlink_ok = FALSE; - char *fname_tmp, *fname; + const char *elpath; + elpath = vfs_path_get_by_index (fname_vpath, 0)->path; /* Just to be sure (and don't lock new file) */ - fname_tmp = vfs_path_to_str (fname_vpath); - if (fname_tmp == NULL || *fname_tmp == '\0') - { - g_free (fname_tmp); + if (fname_vpath == NULL || *elpath == '\0') return 0; - } - - 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); + lockfname = lock_build_symlink_name (fname_vpath); } - g_free (fname); - if (!is_local || lockfname == NULL) return 0; @@ -251,7 +242,7 @@ lock_file (const vfs_path_t * fname_vpath) case -1: g_free (msg); goto ret; - break; /* FIXME: unneeded? */ + break; /* FIXME: unneeded? */ } g_free (msg); } @@ -279,20 +270,14 @@ unlock_file (const vfs_path_t * fname_vpath) { char *lockfname, *lock; struct stat statbuf; - char *fname_tmp, *fname; + const char *elpath; - /* Just to be sure */ - fname_tmp = vfs_path_to_str (fname_vpath); - if (fname_tmp == NULL || *fname_tmp == '\0') - { - g_free (fname_tmp); + elpath = vfs_path_get_by_index (fname_vpath, 0)->path; + /* Just to be sure (and don't lock new file) */ + if (fname_vpath == NULL || *elpath == '\0') return 0; - } - fname = tilde_expand (fname_tmp); - g_free (fname_tmp); - lockfname = lock_build_symlink_name (fname); - g_free (fname); + lockfname = lock_build_symlink_name (fname_vpath); if (lockfname == NULL) return 0; diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index a30ab6342..9b965fb58 100644 --- a/src/editor/edit-impl.h +++ b/src/editor/edit-impl.h @@ -308,9 +308,6 @@ void edit_syntax_dialog (WEdit * edit); void edit_mail_dialog (WEdit * edit); void format_paragraph (WEdit * edit, int force); -unsigned int edit_unlock_file (WEdit * edit); -unsigned int edit_lock_file (WEdit * edit); - /* either command or char_for_insertion must be passed as -1 */ void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion); diff --git a/src/editor/edit.c b/src/editor/edit.c index 85ca9ced7..7cec9720b 100644 --- a/src/editor/edit.c +++ b/src/editor/edit.c @@ -687,7 +687,7 @@ edit_modification (WEdit * edit) /* raise lock when file modified */ if (!edit->modified && !edit->delete_file) - edit->locked = edit_lock_file (edit); + edit->locked = lock_file (edit->filename_vpath); edit->modified = 1; } @@ -2335,7 +2335,7 @@ edit_clean (WEdit * edit) /* a stale lock, remove it */ if (edit->locked) - edit->locked = edit_unlock_file (edit); + edit->locked = unlock_file (edit->filename_vpath); /* save cursor position */ if (option_save_position) @@ -4381,33 +4381,3 @@ edit_move_down (WEdit * edit, unsigned long i, int do_scroll) } /* --------------------------------------------------------------------------------------------- */ - -unsigned int -edit_unlock_file (WEdit * edit) -{ - vfs_path_t *fullpath; - unsigned int ret; - - fullpath = vfs_path_append_vpath_new (edit->dir_vpath, edit->filename_vpath, (char *) NULL); - ret = unlock_file (fullpath); - vfs_path_free (fullpath); - - return ret; -} - -/* --------------------------------------------------------------------------------------------- */ - -unsigned int -edit_lock_file (WEdit * edit) -{ - vfs_path_t *fullpath; - unsigned int ret; - - fullpath = vfs_path_append_vpath_new (edit->dir_vpath, edit->filename_vpath, (char *) NULL); - ret = lock_file (fullpath); - vfs_path_free (fullpath); - - return ret; -} - -/* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 469668724..a9c653ddb 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -455,12 +455,12 @@ edit_save_cmd (WEdit * edit) int res, save_lock = 0; if (!edit->locked && !edit->delete_file) - save_lock = edit_lock_file (edit); + save_lock = lock_file (edit->filename_vpath); res = edit_save_file (edit, edit->filename_vpath); /* Maintain modify (not save) lock on failure */ if ((res > 0 && edit->locked) || save_lock) - edit->locked = edit_unlock_file (edit); + edit->locked = unlock_file (edit->filename_vpath); /* On failure try 'save as', it does locking on its own */ if (!res) @@ -1531,12 +1531,12 @@ edit_save_as_cmd (WEdit * edit) if (save_lock) unlock_file (exp_vpath); if (edit->locked) - edit->locked = edit_unlock_file (edit); + edit->locked = unlock_file (edit->filename_vpath); } else { if (edit->locked || save_lock) - edit->locked = edit_unlock_file (edit); + edit->locked = unlock_file (edit->filename_vpath); } edit_set_filename (edit, exp_vpath); diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index e1e350c10..4d70f716c 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -222,7 +222,7 @@ mcview_handle_editkey (mcview_t * view, int key) if ((view->filename_vpath != NULL) && (*(vfs_path_get_last_path_str (view->filename_vpath)) != '\0') && (view->change_list == NULL)) - view->locked = mcview_lock_file (view); + view->locked = lock_file (view->filename_vpath); if (node == NULL) { diff --git a/src/viewer/hex.c b/src/viewer/hex.c index a66c24b97..b17ebf01d 100644 --- a/src/viewer/hex.c +++ b/src/viewer/hex.c @@ -44,6 +44,7 @@ #include "lib/tty/tty.h" #include "lib/skin.h" #include "lib/vfs/vfs.h" +#include "lib/lock.h" /* lock_file() and unlock_file() */ #include "lib/util.h" #include "lib/widget.h" #include "lib/charsets.h" @@ -371,7 +372,7 @@ mcview_hexedit_save_changes (mcview_t * view) view->change_list = NULL; if (view->locked) - view->locked = mcview_unlock_file (view); + view->locked = unlock_file (view->filename_vpath); if (mc_close (fp) == -1) message (D_ERROR, _("Save file"), @@ -418,7 +419,7 @@ mcview_hexedit_free_change_list (mcview_t * view) view->change_list = NULL; if (view->locked) - view->locked = mcview_unlock_file (view); + view->locked = unlock_file (view->filename_vpath); view->dirty++; } diff --git a/src/viewer/internal.h b/src/viewer/internal.h index 2cfa245ec..83ef45767 100644 --- a/src/viewer/internal.h +++ b/src/viewer/internal.h @@ -287,8 +287,6 @@ void mcview_show_error (mcview_t * view, const char *error); off_t mcview_bol (mcview_t * view, off_t current, off_t limit); off_t mcview_eol (mcview_t * view, off_t current, off_t limit); char *mcview_get_title (const Dlg_head * h, size_t len); -gboolean mcview_lock_file (mcview_t * view); -gboolean mcview_unlock_file (mcview_t * view); /* move.c */ void mcview_move_up (mcview_t *, off_t); diff --git a/src/viewer/lib.c b/src/viewer/lib.c index b0ca10c2a..bfcc32ea6 100644 --- a/src/viewer/lib.c +++ b/src/viewer/lib.c @@ -43,7 +43,6 @@ #include "lib/vfs/vfs.h" #include "lib/strutil.h" #include "lib/util.h" /* save_file_position() */ -#include "lib/lock.h" /* unlock_file() */ #include "lib/widget.h" #include "lib/charsets.h" @@ -417,8 +416,7 @@ mcview_get_title (const Dlg_head * h, size_t len) char *view_filename; char *ret_str; - view_filename = - view->filename_vpath != NULL ? vfs_path_to_str (view->filename_vpath) : NULL; + view_filename = view->filename_vpath != NULL ? vfs_path_to_str (view->filename_vpath) : NULL; len -= 4; @@ -431,33 +429,3 @@ mcview_get_title (const Dlg_head * h, size_t len) } /* --------------------------------------------------------------------------------------------- */ - -gboolean -mcview_lock_file (mcview_t * view) -{ - vfs_path_t *fullpath; - gboolean ret; - - fullpath = vfs_path_append_vpath_new (view->workdir_vpath, view->filename_vpath, (char *) NULL); - ret = lock_file (fullpath); - vfs_path_free (fullpath); - - return ret; -} - -/* --------------------------------------------------------------------------------------------- */ - -gboolean -mcview_unlock_file (mcview_t * view) -{ - vfs_path_t *fullpath; - gboolean ret; - - fullpath = vfs_path_append_vpath_new (view->workdir_vpath, view->filename_vpath, (char *) NULL); - ret = unlock_file (fullpath); - vfs_path_free (fullpath); - - return ret; -} - -/* --------------------------------------------------------------------------------------------- */ -- 2.11.4.GIT