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 5480fe5f80
commit d8f91d556a
5 changed files with 74 additions and 66 deletions

View File

@ -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;
}

View File

@ -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 ****************************************************************************/

View File

@ -4373,12 +4373,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;
}
@ -4388,12 +4388,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;
}

View File

@ -501,11 +501,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;
@ -1689,28 +1689,29 @@ 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;
struct stat sb;
tmp_vpath = vfs_path_from_str (exp);
if (mc_stat (tmp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
if (mc_stat (exp_vpath, &sb) == 0 && !S_ISREG (sb.st_mode))
{
edit_error_dialog (_("Save as"),
get_sys_error (_
("Cannot save: destination is not a regular file")));
g_free (exp);
edit->force |= REDRAW_COMPLETELY;
vfs_path_free (tmp_vpath);
vfs_path_free (exp_vpath);
return 0;
}
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 */
@ -1722,6 +1723,7 @@ edit_save_as_cmd (WEdit * edit)
{
edit->force |= REDRAW_COMPLETELY;
g_free (exp);
vfs_path_free (exp_vpath);
return 0;
}
}
@ -1729,13 +1731,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)
@ -1755,7 +1757,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);
}
@ -1769,6 +1771,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)
@ -1781,8 +1784,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;
}

View File

@ -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;
}