To: vim_dev@googlegroups.com Subject: Patch 8.2.0711 Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ------------ Patch 8.2.0711 Problem: With a long running Vim the temp directory might be cleared on some systems. Solution: Lock the temp directory. (closes #6044) Files: src/config.h.in, src/configure.ac, src/auto/configure, src/fileio.c, src/globals.h, src/os_unix.h *** ../vim-8.2.0710/src/config.h.in 2020-05-02 18:14:33.920146062 +0200 --- src/config.h.in 2020-05-07 18:27:54.757717798 +0200 *************** *** 484,488 **** --- 484,494 ---- /* Define if we have isnan() */ #undef HAVE_ISNAN + /* Define if we have dirfd() */ + #undef HAVE_DIRFD + + /* Define if we have flock() */ + #undef HAVE_FLOCK + /* Define to inline symbol or empty */ #undef inline *** ../vim-8.2.0710/src/configure.ac 2020-05-02 18:14:33.916146073 +0200 --- src/configure.ac 2020-05-07 18:27:54.757717798 +0200 *************** *** 4060,4065 **** --- 4060,4080 ---- AC_MSG_RESULT(yes); AC_DEFINE(HAVE_RENAME), AC_MSG_RESULT(no)) + dnl check for dirfd() + AC_MSG_CHECKING(for dirfd) + AC_TRY_COMPILE( + [#include + #include ], + [DIR * dir=opendir("dirname"); dirfd(dir);], + AC_MSG_RESULT(yes); AC_DEFINE(HAVE_DIRFD), AC_MSG_RESULT(not usable)) + + dnl check for flock() + AC_MSG_CHECKING(for flock) + AC_TRY_COMPILE( + [#include ], + [flock(10, LOCK_SH);], + AC_MSG_RESULT(yes); AC_DEFINE(HAVE_FLOCK), AC_MSG_RESULT(not usable)) + dnl sysctl() may exist but not the arguments we use AC_MSG_CHECKING(for sysctl) AC_TRY_COMPILE( *** ../vim-8.2.0710/src/auto/configure 2020-05-02 18:14:33.920146062 +0200 --- src/auto/configure 2020-05-07 18:29:41.361535086 +0200 *************** *** 13823,13828 **** --- 13823,13875 ---- rm -f core conftest.err conftest.$ac_objext \ conftest$ac_exeext conftest.$ac_ext + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for dirfd" >&5 + $as_echo_n "checking for dirfd... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext + /* end confdefs.h. */ + #include + #include + int + main () + { + DIR * dir=opendir("dirname"); dirfd(dir); + ; + return 0; + } + _ACEOF + if ac_fn_c_try_compile "$LINENO"; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 + $as_echo "yes" >&6; }; $as_echo "#define HAVE_DIRFD 1" >>confdefs.h + + else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: not usable" >&5 + $as_echo "not usable" >&6; } + fi + rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for flock" >&5 + $as_echo_n "checking for flock... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext + /* end confdefs.h. */ + #include + int + main () + { + flock(10, LOCK_SH); + ; + return 0; + } + _ACEOF + if ac_fn_c_try_compile "$LINENO"; then : + { $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5 + $as_echo "yes" >&6; }; $as_echo "#define HAVE_FLOCK 1" >>confdefs.h + + else + { $as_echo "$as_me:${as_lineno-$LINENO}: result: not usable" >&5 + $as_echo "not usable" >&6; } + fi + rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + { $as_echo "$as_me:${as_lineno-$LINENO}: checking for sysctl" >&5 $as_echo_n "checking for sysctl... " >&6; } cat confdefs.h - <<_ACEOF >conftest.$ac_ext *** ../vim-8.2.0710/src/fileio.c 2020-03-29 16:06:25.477382704 +0200 --- src/fileio.c 2020-05-07 18:36:14.268634131 +0200 *************** *** 4620,4625 **** --- 4620,4661 ---- #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,4633 **** --- 4664,4672 ---- { 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,4657 **** --- 4691,4699 ---- 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); } } *** ../vim-8.2.0710/src/globals.h 2020-04-30 22:29:36.626024141 +0200 --- src/globals.h 2020-05-07 18:36:47.236547021 +0200 *************** *** 758,763 **** --- 758,766 ---- EXTERN int sc_col; // column for shown command #ifdef TEMPDIRNAMES + # if defined(UNIX) && defined(HAVE_FLOCK) && defined(HAVE_DIRFD) + EXTERN DIR *vim_tempdir_dp INIT(= NULL); // File descriptor of temp dir + # endif EXTERN char_u *vim_tempdir INIT(= NULL); // Name of Vim's own temp dir. // Ends in a slash. #endif *** ../vim-8.2.0710/src/os_unix.h 2020-02-26 16:15:31.072386953 +0100 --- src/os_unix.h 2020-05-07 18:27:54.757717798 +0200 *************** *** 204,209 **** --- 204,213 ---- # endif #endif + #ifdef HAVE_FLOCK + # include + #endif + #endif // PROTO #ifdef VMS *** ../vim-8.2.0710/src/version.c 2020-05-07 18:16:26.915943785 +0200 --- src/version.c 2020-05-07 18:29:36.565544021 +0200 *************** *** 748,749 **** --- 748,751 ---- { /* Add new patch number below this line */ + /**/ + 711, /**/ -- hundred-and-one symptoms of being an internet addict: 62. If your doorbell rings, you think that new mail has arrived. And then you're disappointed that it's only someone at the door. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\ \\\ an exciting new programming language -- http://www.Zimbu.org /// \\\ help me help AIDS victims -- http://ICCF-Holland.org ///