[Toybox] [PATCH] mktemp: fix the tests and the logic.
enh
enh at google.com
Wed Dec 5 15:03:51 PST 2018
Multiple bugs:
* We weren't outputting anything in the case where we actually create a
file or directory (but all the tests were for the -u case).
* There are more gnarls to the behavior if TEMPLATE contains a '/'. The
new tests cover these.
Additionally, I've realized that glibc's mktemp(3) doesn't seem to do
exactly what we want (it only seems to randomize 6 'X's), so we'll need
to implement our own mktemp(3) replacement anyway. I'll do that in a
separate patch, though, because folks are understandably upset that I
broke mktemp...
---
tests/mktemp.test | 11 ++++++++++-
toys/lsb/mktemp.c | 9 +++++++--
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/tests/mktemp.test b/tests/mktemp.test
index ee7702dc..ee023d6b 100755
--- a/tests/mktemp.test
+++ b/tests/mktemp.test
@@ -6,11 +6,20 @@
# mktemp by default should use tmp.XXXXXXXXXX as the template,
# and $TMPDIR as the directory.
-testing "default" "TMPDIR=/t mktemp -u | grep -q '^/t/tmp\...........$' &&
echo yes" "yes\n" "" ""
+testing "default" "TMPDIR=. mktemp | grep -q '^./tmp\...........$' && echo
yes" "yes\n" "" ""
+
+# Test that -d creates a directory and the default is a file.
+testing "dir" "test -d `TMPDIR=. mktemp -d` && echo yes" "yes\n" "" ""
+testing "file" "test -f `TMPDIR=. mktemp` && echo yes" "yes\n" "" ""
# mktemp with a template should *not* use $TMPDIR.
testing "TEMPLATE" "TMPDIR=/t mktemp -u hello.XXXXXXXX | grep -q
'^hello\.........$' && echo yes" "yes\n" "" ""
+# mktemp with a template that includes a '/' should ignore $TMPDIR
+testing "/ TEMPLATE" "TMPDIR=/t mktemp -u /x/hello.XXXXXXXX | grep -q
'^/x/hello\.........$' && echo yes" "yes\n" "" ""
+# ...and setting DIR is an error.
+testing "/ TEMPLATE -p DIR" "TMPDIR=/t mktemp -p DIR -u /x/hello.XXXXXXXX
|| echo error" "error\n" "" ""
+
# mktemp with -t and a template should use $TMPDIR.
testing "-t TEMPLATE" "TMPDIR=/t mktemp -u -t hello.XXXXXXXX | grep -q
'^/t/hello\.........$' && echo yes" "yes\n" "" ""
diff --git a/toys/lsb/mktemp.c b/toys/lsb/mktemp.c
index 112f84c4..57d1d118 100644
--- a/toys/lsb/mktemp.c
+++ b/toys/lsb/mktemp.c
@@ -34,7 +34,11 @@ GLOBALS(
void mktemp_main(void)
{
char *template = *toys.optargs;
- int use_dir = (toys.optflags & (FLAG_p|FLAG_t));
+ int template_dir = template && !!strchr(template, '/');
+ int flags_dir = (toys.optflags & (FLAG_p|FLAG_t));
+ int use_dir = flags_dir && !template_dir;
+
+ if (template_dir && flags_dir) error_exit("conflicting directories
given");
if (!template) {
template = "tmp.XXXXXXXXXX";
@@ -58,12 +62,13 @@ void mktemp_main(void)
template = use_dir ? xmprintf("%s/%s", TT.p, template) :
xstrdup(template);
if (toys.optflags & FLAG_u) {
- xputs(mktemp(template));
+ template = mktemp(template);
} else if (toys.optflags & FLAG_d ? !mkdtemp(template) :
mkstemp(template) == -1) {
if (toys.optflags & FLAG_q) toys.exitval = 1;
else perror_exit("Failed to create %s %s/%s",
toys.optflags & FLAG_d ? "directory" : "file", TT.p, template);
}
+ xputs(template);
if (CFG_TOYBOX_FREE) free(template);
}
--
2.20.0.rc1.387.gf8505762e3-goog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20181205/bad30739/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-mktemp-fix-the-tests-and-the-logic.patch
Type: text/x-patch
Size: 3359 bytes
Desc: not available
URL: <http://lists.landley.net/pipermail/toybox-landley.net/attachments/20181205/bad30739/attachment-0002.bin>
More information about the Toybox
mailing list