diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-05-07 18:37:03 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-05-07 18:37:03 +0200 |
commit | b2d0e51366dea6843f991f31a457f5456d162678 (patch) | |
tree | c220d988cb585427bf0e4eae0fc648ec3d0524d5 /src/fileio.c | |
parent | 4a070cc82e00618db279526797564cb2b4e9b060 (diff) |
patch 8.2.0711: temp directory might be clearedv8.2.0711
Problem: With a long running Vim the temp directory might be cleared on
some systems.
Solution: Lock the temp directory. (closes #6044)
Diffstat (limited to 'src/fileio.c')
-rw-r--r-- | src/fileio.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/fileio.c b/src/fileio.c index f05abe68c9..613f92359d 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -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); } } |