diff src/fileio.c @ 20311:05b4efb062a7 v8.2.0711

patch 8.2.0711: temp directory might be cleared Commit: https://github.com/vim/vim/commit/b2d0e51366dea6843f991f31a457f5456d162678 Author: Bram Moolenaar <Bram@vim.org> Date: Thu May 7 18:37:03 2020 +0200 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)
author Bram Moolenaar <Bram@vim.org>
date Thu, 07 May 2020 18:45:03 +0200
parents c32b295af9c5
children d571231175b4
line wrap: on
line diff
--- 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);
     }
 }