]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
mktemp: prefer rmdir and unlink to remove
authorCollin Funk <collin.funk1@gmail.com>
Sat, 11 Apr 2026 03:29:54 +0000 (20:29 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Sat, 11 Apr 2026 16:58:13 +0000 (09:58 -0700)
This avoids the following behavior:

    $ strace -e silence=exit -e trace=unlink,rmdir \
        mktemp -d > /dev/full
    unlink("/tmp/tmp.ZBuPmS9ZGD") = -1 EISDIR (Is a directory)
    rmdir("/tmp/tmp.ZBuPmS9ZGD")  = 0
    mktemp: write error: No space left on device

In the above invocation we know that we created a directory, so we
should not remove a regular file that must have been created by another
process:

    $ strace -e silence=exit -e trace=unlink,rmdir \
        ./src/mktemp -d > /dev/full
    rmdir("/tmp/tmp.hGbME1HmJr") = 0
    mktemp: write error: No space left on device

* src/mktemp.c (main): Prefer rmdir and unlink depending on whether we
created a directory or regular file.
* bootstrap.conf (gnulib_modules): Remove the remove module.

bootstrap.conf
src/mktemp.c

index bc31a58623ad80538d01f479a5666128bd18a264..aee0c4f3374d3fcb4ffa17419265b86edc716a28 100644 (file)
@@ -248,7 +248,6 @@ gnulib_modules="
   readtokens0
   readutmp
   regex
-  remove
   renameat
   renameatu
   rmdir
index 72523be64200d1dfe6b4cf36973d2dc7e77e7c3b..1fa797266c8ed50cf9e23d0fdaa01951179d0903 100644 (file)
@@ -347,7 +347,7 @@ main (int argc, char **argv)
       if (!dry_run && ((stdout_closed = true), close_stream (stdout) != 0))
         {
           int saved_errno = errno;
-          remove (dest_name);
+          (create_directory ? rmdir : unlink) (dest_name);
           if (!suppress_file_err)
             error (0, saved_errno, _("write error"));
           status = EXIT_FAILURE;