Re: [PATCH] vfs: relax linkat() AT_EMPTY_PATH - aka flink() - requirements

From: Linus Torvalds
Date: Thu Apr 11 2024 - 16:22:58 EST


On Thu, 11 Apr 2024 at 13:08, Charles Mirabile <cmirabil@xxxxxxxxxx> wrote:
>
> The problem with this is that another process might be able to access
> the file during via that name during the brief period before it is
> unlinked. If I am not using NFS, I am always going to prefer using
> O_TMPFILE. I would rather be able to do that without restriction even
> if it isn't the most robust solution by your definition.


Oh, absolutely. I think the right pattern is basically some variation of

fd = open(filename, O_TMPFILE | O_WRONLY, 0600);
if (fd < 0) {
char template{...] = ".tmpfileXXXXXX";
fd = mkstmp(template);
unlink(template);
}
.. now act on fd to initialize it ..
linkat(fd, "", AT_FDCWD, "finalname", AT_EMPTY_PATH);

which should work reasonably well in various environments.

Clearly O_TMPFILE is the superior option when it exists. I'm just
saying that anything that *relies* on it existing is dubious.

Linus