0
0
mirror of https://github.com/vim/vim.git synced 2025-09-28 04:24:06 -04:00

patch 8.2.0711: temp directory might be cleared

Problem:    With a long running Vim the temp directory might be cleared on
            some systems.
Solution:   Lock the temp directory. (closes #6044)
This commit is contained in:
Bram Moolenaar
2020-05-07 18:37:03 +02:00
parent 4a070cc82e
commit b2d0e51366
7 changed files with 119 additions and 0 deletions

View File

@@ -4620,6 +4620,42 @@ delete_recursive(char_u *name)
#if defined(TEMPDIRNAMES) || defined(PROTO)
static long temp_count = 0; // Temp filename counter.
# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
/*
* Open temporary directory and take file lock to prevent
* to be auto-cleaned.
*/
static void
vim_opentempdir(void)
{
DIR *dp = NULL;
if (vim_tempdir_dp != NULL)
return;
dp = opendir((const char*)vim_tempdir);
if (dp != NULL)
{
vim_tempdir_dp = dp;
flock(dirfd(vim_tempdir_dp), LOCK_SH);
}
}
/*
* Close temporary directory - it automatically release file lock.
*/
static void
vim_closetempdir(void)
{
if (vim_tempdir_dp != NULL)
{
closedir(vim_tempdir_dp);
vim_tempdir_dp = NULL;
}
}
# endif
/*
* Delete the temp directory and all files it contains.
*/
@@ -4628,6 +4664,9 @@ vim_deltempdir(void)
{
if (vim_tempdir != NULL)
{
# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
vim_closetempdir();
# endif
// remove the trailing path separator
gettail(vim_tempdir)[-1] = NUL;
delete_recursive(vim_tempdir);
@@ -4652,6 +4691,9 @@ vim_settempdir(char_u *tempdir)
STRCPY(buf, tempdir);
add_pathsep(buf);
vim_tempdir = vim_strsave(buf);
# if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD)
vim_opentempdir();
# endif
vim_free(buf);
}
}