[Tarantool-patches] [PATCH v2 1/1] fio: respect $TMPDIR in fio.tempdir(), when it is set

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Wed Mar 4 02:01:32 MSK 2020


TMPDIR is an environment variable used to tell what a directory
should be used to create temporary files. It is described in the
POSIX standard, and should be used by programs creating temporary
files.

Closes #4794

@TarantoolBot document
Title: fio.tempdir() $TMPDIR

fio.tempdir() stores created temporary directory into /tmp by
default. This can be changed by setting TMPDIR environment
variable. Before starting Tarantool, or at runtime by
os.setenv().
---
Branch: http://github.com/tarantool/tarantool/tree/gerold103/gh-4794-tmpdir
Issue: https://github.com/tarantool/tarantool/issues/4794

@ChangeLog
- fio.tempdir() uses TMPDIR as a path to a place where to create
  temporary directories (gh-4794).

Changes in V2:
- Dropped commit about custom template instead of XXXXXX.

 src/lib/core/coio_file.c | 11 ++++---
 test/app/fio.result      | 68 ++++++++++++++++++++++++++++++++++++++++
 test/app/fio.test.lua    | 28 +++++++++++++++++
 3 files changed, 103 insertions(+), 4 deletions(-)

diff --git a/src/lib/core/coio_file.c b/src/lib/core/coio_file.c
index 62388344e..e2345567c 100644
--- a/src/lib/core/coio_file.c
+++ b/src/lib/core/coio_file.c
@@ -466,13 +466,16 @@ coio_tempdir(char *path, size_t path_len)
 {
 	INIT_COEIO_FILE(eio);
 
-	if (path_len < sizeof("/tmp/XXXXXX") + 1) {
+	const char *tmpdir = getenv("TMPDIR");
+	if (tmpdir == NULL)
+		tmpdir = "/tmp";
+	int rc = snprintf(path, path_len, "%s/XXXXXX", tmpdir);
+	if (rc < 0)
+		return -1;
+	if ((size_t) rc >= path_len) {
 		errno = ENOMEM;
 		return -1;
 	}
-
-	snprintf(path, path_len, "/tmp/XXXXXX");
-
 	eio.tempdir.tpl = path;
 	eio_req *req =
 		eio_custom(coio_do_tempdir, 0, coio_complete, &eio);
diff --git a/test/app/fio.result b/test/app/fio.result
index f83c43f44..cd18ed32c 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -1456,3 +1456,71 @@ fio.mktree('/dev/null/dir')
 - false
 - 'Error creating directory /dev/null: File exists'
 ...
+--
+-- gh-4794: fio.tempdir() should use $TMPDIR.
+--
+cwd = fio.cwd()
+---
+...
+old_tmpdir = os.getenv('TMPDIR')
+---
+...
+tmpdir = cwd..'/tmp'
+---
+...
+fio.mkdir(tmpdir)
+---
+- true
+...
+os.setenv('TMPDIR', tmpdir)
+---
+...
+dir = fio.tempdir()
+---
+...
+dir:find(tmpdir) ~= nil or {dir, tmpdir}
+---
+- true
+...
+fio.stat(dir) ~= nil or fio.stat(dir)
+---
+- true
+...
+tmpdir = cwd..'/tmp2'
+---
+...
+os.setenv('TMPDIR', tmpdir)
+---
+...
+fio.tempdir()
+---
+- null
+- 'fio: No such file or directory'
+...
+os.setenv('TMPDIR', nil)
+---
+...
+dir = fio.tempdir()
+---
+...
+dir:find('/tmp') ~= nil or dir
+---
+- true
+...
+tmpdir = cwd..'/'..string.rep('t', 5000)
+---
+...
+os.setenv('TMPDIR', tmpdir)
+---
+...
+fio.tempdir()
+---
+- null
+- 'fio: Cannot allocate memory'
+...
+tmpdir = nil
+---
+...
+os.setenv('TMPDIR', old_tmpdir)
+---
+...
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index 56c957d8a..242ff08ad 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -474,3 +474,31 @@ test_run:cmd("clear filter")
 --
 fio.mktree('/dev/null')
 fio.mktree('/dev/null/dir')
+
+--
+-- gh-4794: fio.tempdir() should use $TMPDIR.
+--
+cwd = fio.cwd()
+old_tmpdir = os.getenv('TMPDIR')
+
+tmpdir = cwd..'/tmp'
+fio.mkdir(tmpdir)
+os.setenv('TMPDIR', tmpdir)
+dir = fio.tempdir()
+dir:find(tmpdir) ~= nil or {dir, tmpdir}
+fio.stat(dir) ~= nil or fio.stat(dir)
+
+tmpdir = cwd..'/tmp2'
+os.setenv('TMPDIR', tmpdir)
+fio.tempdir()
+
+os.setenv('TMPDIR', nil)
+dir = fio.tempdir()
+dir:find('/tmp') ~= nil or dir
+
+tmpdir = cwd..'/'..string.rep('t', 5000)
+os.setenv('TMPDIR', tmpdir)
+fio.tempdir()
+tmpdir = nil
+
+os.setenv('TMPDIR', old_tmpdir)
-- 
2.21.1 (Apple Git-122.3)



More information about the Tarantool-patches mailing list