From 344f8a1814b8df2b4222b3aa96a0a0a2743ba496 Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Tue, 15 Jan 2019 16:10:11 +1100 Subject: [PATCH] Linux use O_TMPFILE for create_temp_file O_TMPFILE creates a tempfile not attached to a filename. We preserve a state O_TMPFILE_works because kernel version or filesystem could cause failure, most likely permanently. --- include/my_sys.h | 1 + mysys/mf_tempfile.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/my_sys.h b/include/my_sys.h index 63a0aede28d..8f3660a721b 100644 --- a/include/my_sys.h +++ b/include/my_sys.h @@ -304,6 +304,7 @@ enum file_type { STREAM_BY_FOPEN, STREAM_BY_FDOPEN, FILE_BY_MKSTEMP, + FILE_BY_O_TMPFILE, FILE_BY_DUP }; diff --git a/mysys/mf_tempfile.cc b/mysys/mf_tempfile.cc index f2bf1b18036..12790ffefcd 100644 --- a/mysys/mf_tempfile.cc +++ b/mysys/mf_tempfile.cc @@ -118,6 +118,32 @@ File create_temp_file(char *to, const char *dir, const char *prefix, } #else /* mkstemp() is available on all non-Windows supported platforms. */ +#ifdef O_TMPFILE + { + static int O_TMPFILE_works = 1; + + if (O_TMPFILE_works) + { + /* explictly don't use O_EXCL here has it has a different + meaning with O_TMPFILE + */ + if ((file = open(dir, O_RDWR | O_TMPFILE | O_CLOEXEC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)) >= 0) + { + snprintf(to, FN_REFLEN, "%s/#sql/fd=%d", dir, file); + file = my_register_filename(file, to, FILE_BY_O_TMPFILE, + EE_CANTCREATEFILE, MyFlags); + } + else if (errno == EOPNOTSUPP || errno == EINVAL) + { + my_printf_error(EE_CANTCREATEFILE, "O_TMPFILE is not supported on %s " + "(disabling future attempts)", MYF(0), dir); + O_TMPFILE_works= 0; + } + } + } + if (file == -1) +#endif /* O_TMPFILE */ { char prefix_buff[30]; uint pfx_len;