Changed lock_file() and unlock_file() functions

...to handle vfs_path_t objects.

Signed-off-by: Slava Zanko <slavazanko@gmail.com>
This commit is contained in:
Slava Zanko 2011-11-03 00:34:35 +03:00
parent 3529890d0c
commit 12a9390524
5 changed files with 72 additions and 64 deletions

View File

@ -193,45 +193,44 @@ lock_get_info (const char *lockfname)
Warning: Might do screen refresh and lose edit->force */ Warning: Might do screen refresh and lose edit->force */
int 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 stat statbuf;
struct lock_s *lockinfo; struct lock_s *lockinfo;
gboolean symlink_ok; gboolean is_local;
vfs_path_t *vpath; gboolean symlink_ok = FALSE;
char *fname_tmp, *fname;
/* Just to be sure (and don't lock new file) */ /* Just to be sure (and don't lock new file) */
if (fname == NULL || *fname == '\0') fname_tmp = vfs_path_to_str (fname_vpath);
return 0; if (fname_tmp == NULL || *fname_tmp == '\0')
fname = tilde_expand (fname);
vpath = vfs_path_from_str (fname);
/* Locking on VFS is not supported */
if (!vfs_file_is_local (vpath))
{ {
g_free ((gpointer) fname); g_free (fname_tmp);
vfs_path_free (vpath);
return 0; return 0;
} }
vfs_path_free (vpath);
/* Check if already locked */ fname = tilde_expand (fname_tmp);
lockfname = lock_build_symlink_name (fname); g_free (fname_tmp);
g_free ((gpointer) fname);
if (lockfname == NULL) /* 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; return 0;
if (lstat (lockfname, &statbuf) == 0) if (lstat (lockfname, &statbuf) == 0)
{ {
lock = lock_get_info (lockfname); lock = lock_get_info (lockfname);
if (lock == NULL) if (lock == NULL)
{ goto ret;
g_free (lockfname);
return 0;
}
lockinfo = lock_extract_info (lock); lockinfo = lock_extract_info (lock);
/* Check if locking process alive, ask user if required */ /* Check if locking process alive, ask user if required */
@ -250,9 +249,9 @@ lock_file (const char *fname)
break; break;
case 1: case 1:
case -1: case -1:
g_free (lockfname);
g_free (msg); g_free (msg);
return 0; goto ret;
break; /* FIXME: unneeded? */
} }
g_free (msg); g_free (msg);
} }
@ -263,8 +262,9 @@ lock_file (const char *fname)
newlock = lock_build_name (); newlock = lock_build_name ();
symlink_ok = (symlink (newlock, lockfname) != -1); symlink_ok = (symlink (newlock, lockfname) != -1);
g_free (newlock); g_free (newlock);
g_free (lockfname);
ret:
g_free (lockfname);
return symlink_ok ? 1 : 0; return symlink_ok ? 1 : 0;
} }
@ -275,42 +275,44 @@ lock_file (const char *fname)
*/ */
int int
unlock_file (const char *fname) unlock_file (const vfs_path_t * fname_vpath)
{ {
char *lockfname, *lock; char *lockfname, *lock;
struct stat statbuf; struct stat statbuf;
char *fname_tmp, *fname;
/* Just to be sure */ /* 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; return 0;
}
fname = tilde_expand (fname); fname = tilde_expand (fname_tmp);
g_free (fname_tmp);
lockfname = lock_build_symlink_name (fname); lockfname = lock_build_symlink_name (fname);
g_free ((gpointer) fname); g_free (fname);
if (lockfname == NULL) if (lockfname == NULL)
return 0; return 0;
/* Check if lock exists */ /* Check if lock exists */
if (lstat (lockfname, &statbuf) == -1) if (lstat (lockfname, &statbuf) == -1)
{ goto ret;
g_free (lockfname);
return 0;
}
lock = lock_get_info (lockfname); lock = lock_get_info (lockfname);
if (lock != NULL) if (lock != NULL)
{ {
/* Don't touch if lock is not ours */ /* Don't touch if lock is not ours */
if (lock_extract_info (lock)->pid != getpid ()) if (lock_extract_info (lock)->pid != getpid ())
{ goto ret;
g_free (lockfname);
return 0;
}
} }
/* Remove lock */ /* Remove lock */
unlink (lockfname); unlink (lockfname);
ret:
g_free (lockfname); g_free (lockfname);
return 0; return 0;
} }

View File

@ -9,6 +9,8 @@
#ifndef MC_LOCK_H #ifndef MC_LOCK_H
#define MC_LOCK_H #define MC_LOCK_H
#include "lib/vfs/vfs.h" /* vfs_path_t */
/*** typedefs(not structures) and defined constants **********************************************/ /*** typedefs(not structures) and defined constants **********************************************/
/*** enums ***************************************************************************************/ /*** enums ***************************************************************************************/
@ -19,8 +21,8 @@
/*** declarations of public functions ************************************************************/ /*** declarations of public functions ************************************************************/
int lock_file (const char *fname); int lock_file (const vfs_path_t * fname_vpath);
int unlock_file (const char *fname); int unlock_file (const vfs_path_t * fname_vpath);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/

View File

@ -4371,12 +4371,12 @@ edit_move_down (WEdit * edit, unsigned long i, int do_scroll)
unsigned int unsigned int
edit_unlock_file (WEdit * edit) edit_unlock_file (WEdit * edit)
{ {
char *fullpath; vfs_path_t *fullpath;
unsigned int ret; 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); ret = unlock_file (fullpath);
g_free (fullpath); vfs_path_free (fullpath);
return ret; return ret;
} }
@ -4386,12 +4386,12 @@ edit_unlock_file (WEdit * edit)
unsigned int unsigned int
edit_lock_file (WEdit * edit) edit_lock_file (WEdit * edit)
{ {
char *fullpath; vfs_path_t *fullpath;
unsigned int ret; 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); ret = lock_file (fullpath);
g_free (fullpath); vfs_path_free (fullpath);
return ret; return ret;
} }

View File

@ -497,11 +497,11 @@ edit_load_file_from_filename (WEdit * edit, char *exp)
if (prev_locked) 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); unlock_file (fullpath);
g_free (fullpath); vfs_path_free (fullpath);
} }
g_free (prev_filename); g_free (prev_filename);
return 0; return 0;
@ -1489,16 +1489,17 @@ edit_save_as_cmd (WEdit * edit)
else else
{ {
int rv; int rv;
vfs_path_t *exp_vpath;
exp_vpath = vfs_path_from_str (exp);
if (strcmp (edit->filename, exp)) if (strcmp (edit->filename, exp))
{ {
int file; int file;
vfs_path_t *tmp_vpath;
tmp_vpath = vfs_path_from_str (exp);
different_filename = 1; different_filename = 1;
file = mc_open (tmp_vpath, O_RDONLY | O_BINARY); file = mc_open (exp_vpath, O_RDONLY | O_BINARY);
vfs_path_free (tmp_vpath);
if (file != -1) if (file != -1)
{ {
/* the file exists */ /* the file exists */
@ -1510,6 +1511,7 @@ edit_save_as_cmd (WEdit * edit)
{ {
edit->force |= REDRAW_COMPLETELY; edit->force |= REDRAW_COMPLETELY;
g_free (exp); g_free (exp);
vfs_path_free (exp_vpath);
return 0; return 0;
} }
} }
@ -1517,13 +1519,13 @@ edit_save_as_cmd (WEdit * edit)
{ {
edit->stat1.st_mode |= S_IWUSR; edit->stat1.st_mode |= S_IWUSR;
} }
save_lock = lock_file (exp); save_lock = lock_file (exp_vpath);
} }
else else
{ {
/* filenames equal, check if already locked */ /* filenames equal, check if already locked */
if (!edit->locked && !edit->delete_file) if (!edit->locked && !edit->delete_file)
save_lock = lock_file (exp); save_lock = lock_file (exp_vpath);
} }
if (different_filename) if (different_filename)
@ -1543,7 +1545,7 @@ edit_save_as_cmd (WEdit * edit)
if (different_filename) if (different_filename)
{ {
if (save_lock) if (save_lock)
unlock_file (exp); unlock_file (exp_vpath);
if (edit->locked) if (edit->locked)
edit->locked = edit_unlock_file (edit); edit->locked = edit_unlock_file (edit);
} }
@ -1557,6 +1559,7 @@ edit_save_as_cmd (WEdit * edit)
if (edit->lb != LB_ASIS) if (edit->lb != LB_ASIS)
edit_reload (edit, exp); edit_reload (edit, exp);
g_free (exp); g_free (exp);
vfs_path_free (exp_vpath);
edit->modified = 0; edit->modified = 0;
edit->delete_file = 0; edit->delete_file = 0;
if (different_filename) if (different_filename)
@ -1569,8 +1572,9 @@ edit_save_as_cmd (WEdit * edit)
case -1: case -1:
/* Failed, so maintain modify (not save) lock */ /* Failed, so maintain modify (not save) lock */
if (save_lock) if (save_lock)
unlock_file (exp); unlock_file (exp_vpath);
g_free (exp); g_free (exp);
vfs_path_free (exp_vpath);
edit->force |= REDRAW_COMPLETELY; edit->force |= REDRAW_COMPLETELY;
return 0; return 0;
} }

View File

@ -435,12 +435,12 @@ mcview_get_title (const Dlg_head * h, size_t len)
gboolean gboolean
mcview_lock_file (mcview_t * view) mcview_lock_file (mcview_t * view)
{ {
char *fullpath; vfs_path_t *fullpath;
gboolean ret; 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); ret = lock_file (fullpath);
g_free (fullpath); vfs_path_free (fullpath);
return ret; return ret;
} }
@ -450,12 +450,12 @@ mcview_lock_file (mcview_t * view)
gboolean gboolean
mcview_unlock_file (mcview_t * view) mcview_unlock_file (mcview_t * view)
{ {
char *fullpath; vfs_path_t *fullpath;
gboolean ret; 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); ret = unlock_file (fullpath);
g_free (fullpath); vfs_path_free (fullpath);
return ret; return ret;
} }