[PATCH] misc: Add mkostempat (BZ 19866)

Paul Eggert eggert@cs.ucla.edu
Fri Jun 19 01:42:22 GMT 2026


On 2026-06-18 14:32, Adhemerval Zanella Netto wrote:

>> Come to think of it, why not address this issue with a callback instead? That is, replace the dirfd, oflags and mode argument with a closure, namely, a function f and a void *arg. mkostemp calls f (name, arg) to create the file. Then the caller can use whatever fancy flags they like.
> 
> Well, we will need to come up with a semantic what we will be safe in the
> callback interface.  And callback has other complications when mixed with
> different runtime (like this issue with qsort [1]). I am not really sure
> if this would really simplify things.

The semantic rules should be pretty simple: no side effects other than 
creating the temp files and changing errno, and no longjmps or throws 
out of callbacks. If a callback crashes or infloops, obviously 
mkostempat (or whatever) does the same.


> That's why I posed the question of what kind of openat2 flags would be
> good to support.  These can guide if extending these interface even more
> would really pay off.

It'd be easier to list the openat2 flags that would *not* be good to 
support. These are O_DIRECTORY, O_TMPFILE, and (in the patches queued 
for Linux 7.2) O_EMPTYPATH. Maybe RESOLVE_CACHED too, but only in the 
sense that I've never figured out a use for RESOLVE_CACHED and if 
someone else can think of one it could well apply to this new function.


Now that I'm thinking about it another advantage of callbacks, as 
opposed to trying to figure out all the openat2 options and complicating 
the API, is that the caller can create file types other than regular 
files. For example, the caller can create a directory (this rendering 
mkdtemp unnecessary).

GNU 'ln -fs a b', for example, uses this capability when ensuring that 
if 'b' already exists, there is never an instant when b does not exist. 
It does so by doing the equivalent of:

    ln -s a CuEeskzX
    mv -f CuEeskzX b

and the first step is done by calling try_tempname with a callback that 
creates a symlink rather than a regular file. This sort of thing is a 
nice capability to have, and we have it already with the Gnulib API.


More information about the Libc-alpha mailing list