[Toybox] [PATCH] mktemp: more tests, more fixes.

enh enh at google.com
Tue Dec 4 11:03:20 PST 2018


I realized (after being questioned about my motivation) that I hadn't
added a test for the -u behavior. Adding the missing test confirmed the
usual "if there isn't a test, the code is broken", but now I think I
actually understand how we're supposed to choose between DIR, $TMPDIR,
and /tmp. I've added more tests to back this up, and rewritten the code
one more time so that we pass all the tests.
---
 tests/mktemp.test | 19 ++++++++++++++-----
 toys/lsb/mktemp.c | 22 ++++++++++++++++------
 2 files changed, 30 insertions(+), 11 deletions(-)

diff --git a/tests/mktemp.test b/tests/mktemp.test
index d09d2c4c..ee7702dc 100755
--- a/tests/mktemp.test
+++ b/tests/mktemp.test
@@ -6,16 +6,25 @@

 # mktemp by default should use tmp.XXXXXXXXXX as the template,
 # and $TMPDIR as the directory.
-testing "mktemp" "TMPDIR=/t mktemp -u | grep -q
'^/t/tmp\...........$' && echo yes" "yes\n" "" ""
+testing "default" "TMPDIR=/t mktemp -u | grep -q
'^/t/tmp\...........$' && echo yes" "yes\n" "" ""

 # mktemp with a template should *not* use $TMPDIR.
-testing "mktemp TEMPLATE" "TMPDIR=/t mktemp -u hello.XXXXXXXX | grep
-q '^hello\.........$' && echo yes" "yes\n" "" ""
+testing "TEMPLATE" "TMPDIR=/t mktemp -u hello.XXXXXXXX | grep -q
'^hello\.........$' && echo yes" "yes\n" "" ""

 # mktemp with -t and a template should use $TMPDIR.
-testing "mktemp -t TEMPLATE" "TMPDIR=/t mktemp -u -t hello.XXXXXXXX |
grep -q '^/t/hello\.........$' && echo yes" "yes\n" "" ""
+testing "-t TEMPLATE" "TMPDIR=/t mktemp -u -t hello.XXXXXXXX | grep
-q '^/t/hello\.........$' && echo yes" "yes\n" "" ""
+
+# mktemp with -t and a template should use $TMPDIR ... or /tmp if no $TMPDIR.
+testing "-t TEMPLATE but no TMPDIR" "TMPDIR= mktemp -u -t
hello.XXXXXXXX | grep -q '^/tmp/hello\.........$' && echo yes" "yes\n"
"" ""

 # mktemp with -p DIR and a template should use DIR.
-testing "mktemp -p DIR TEMPLATE" "TMPDIR=/t mktemp -u -p DIR
hello.XXXXXXXX | grep -q '^DIR/hello\.........$' && echo yes" "yes\n"
"" ""
+testing "-p DIR TEMPLATE" "TMPDIR=/t mktemp -u -p DIR hello.XXXXXXXX
| grep -q '^DIR/hello\.........$' && echo yes" "yes\n" "" ""

 # mktemp -p DIR and -t: -t wins.
-testing "mktemp -p DIR -t TEMPLATE" "TMPDIR=/t mktemp -u -p DIR -t
hello.XXXXXXXX | grep -q '^/t/hello\.........$' && echo yes" "yes\n"
"" ""
+testing "-p DIR -t TEMPLATE" "TMPDIR=/t mktemp -u -p DIR -t
hello.XXXXXXXX | grep -q '^/t/hello\.........$' && echo yes" "yes\n"
"" ""
+
+# mktemp -p DIR and -t but no $TMPDIR: DIR wins.
+testing "-p DIR -t TEMPLATE but no TMPDIR" "TMPDIR= mktemp -u -p DIR
-t hello.XXXXXXXX | grep -q '^DIR/hello\.........$' && echo yes"
"yes\n" "" ""
+
+# mktemp -u doesn't need to be able to write to the directory.
+testing "-u" "mktemp -u -p /proc | grep -q '^/proc/tmp\...........$'
&& echo yes" "yes\n" "" ""
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
index ae97e494..112f84c4 100644
--- a/toys/lsb/mktemp.c
+++ b/toys/lsb/mktemp.c
@@ -17,7 +17,7 @@ config MKTEMP
     -d Create directory instead of file (--directory)
     -p Put new file in DIR (--tmpdir)
     -q Quiet, no error messages
-    -t Prepend $TMPDIR or /tmp if unset
+    -t Prefer $TMPDIR > DIR > /tmp (default DIR > $TMPDIR > /tmp)
     -u Don't create anything, just print what would be created

     Each X in TEMPLATE is replaced with a random printable character. The
@@ -34,18 +34,28 @@ GLOBALS(
 void mktemp_main(void)
 {
   char *template = *toys.optargs;
+  int use_dir = (toys.optflags & (FLAG_p|FLAG_t));

   if (!template) {
-    toys.optflags |= FLAG_t;
     template = "tmp.XXXXXXXXXX";
+    use_dir = 1;
   }

-  if (!TT.p || (toys.optflags & FLAG_t)) TT.p = getenv("TMPDIR");
-  if (!TT.p || !*TT.p) TT.p = "/tmp";
+  // Normally, the precedence is DIR (if set), $TMPDIR (if set), /tmp.
+  // With -t it's $TMPDIR, DIR, /tmp.
+  if (use_dir) {
+    char *tmpdir = getenv("TMPDIR");
+
+    if (toys.optflags & FLAG_t) {
+      if (tmpdir && *tmpdir) TT.p = tmpdir;
+    } else {
+      if (!TT.p || !*TT.p) TT.p = tmpdir;
+    }
+    if (!TT.p || !*TT.p) TT.p = "/tmp";
+  }

   // TODO: coreutils cleans paths, so -p /t/// would result in /t/xxx...
-  template = (strchr(template, '/') || !(toys.optflags & (FLAG_p|FLAG_t)))
-      ? xstrdup(template) : xmprintf("%s/%s", TT.p, template);
+  template = use_dir ? xmprintf("%s/%s", TT.p, template) : xstrdup(template);

   if (toys.optflags & FLAG_u) {
     xputs(mktemp(template));
-- 
2.19.1.930.g4563a0d9d0-goog
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-mktemp-more-tests-more-fixes.patch
Type: text/x-patch
Size: 4654 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20181204/6d351605/attachment-0002.bin>


More information about the Toybox mailing list