mktemp: don't leave file behind on write failure

* src/mktemp.c (main): Remove just-created file if stdout had
problems.
* bootstrap.conf (gnulib_modules): Add remove.
* tests/misc/close-stdout: Test it.
* NEWS: Document it.
This commit is contained in:
Eric Blake 2009-11-04 14:02:20 -07:00
parent cd65f11c4f
commit 41b3a8ed8b
4 changed files with 23 additions and 1 deletions

4
NEWS
View File

@ -20,6 +20,10 @@ GNU coreutils NEWS -*- outline -*-
This also affected sum, sha1sum, sha224sum, sha384sum and sha512sum.
[the bug dates back to the initial implementation]
mktemp no longer leaves a temporary file behind if it was unable to
output the name of the file to stdout.
[the bug dates back to the initial implementation]
nice -n -1 PROGRAM now runs PROGRAM even when its internal setpriority
call fails with errno == EACCES.
[the bug dates back to the initial implementation]

View File

@ -180,6 +180,7 @@ gnulib_modules="
readutmp
realloc
regex
remove
rename
rmdir
root-dev-ino

View File

@ -23,6 +23,7 @@
#include "system.h"
#include "close-stream.h"
#include "error.h"
#include "filenamecat.h"
#include "quote.h"
@ -277,7 +278,17 @@ main (int argc, char **argv)
}
if (status == EXIT_SUCCESS)
puts (dest_name);
{
puts (dest_name);
/* If we created a file, but then failed to output the file
name, we should clean up the mess before failing. */
if (!dry_run && close_stream (stdout))
{
int saved_errno = errno;
remove (dest_name);
error (EXIT_FAILURE, saved_errno, _("write error"));
}
}
#ifdef lint
free (dest_name);

View File

@ -50,12 +50,18 @@ if "$p/src/test" -w /dev/stdout >/dev/null &&
"$p/src/test" ! -w /dev/stdout >&-; then
"$p/src/printf" 'foo' >&- 2>/dev/null && fail=1
cp --verbose a b >&- 2>/dev/null && fail=1
rm -Rf tmpfile-?????? || fail=1
mktemp tmpfile-XXXXXX >&- 2>/dev/null && fail=1
test -e tmpfile-?????? && fail=1
fi
# Likewise for /dev/full, if /dev/full works.
if test -w /dev/full && test -c /dev/full; then
"$p/src/printf" 'foo' >/dev/full 2>/dev/null && fail=1
cp --verbose a b >/dev/full 2>/dev/null && fail=1
rm -Rf tmpdir-?????? || fail=1
mktemp -d tmpdir-XXXXXX >/dev/full 2>/dev/null && fail=1
test -e tmpdir-?????? && fail=1
fi
Exit $fail