Re: [PATCH] tools/nolibc: Add dirfd()

From: Thomas Weißschuh

Date: Fri Aug 28 2026 - 08:21:47 EST


On 2026-08-28 20:22:04+0900, Daniel Palmer wrote:
> This is useful for things like fstatat() on children of directory
> opened with opendir().
>
> At the same time use it to replace the two places that are converting
> the DIR point back into an fd.
>
> Signed-off-by: Daniel Palmer <daniel@xxxxxxxxx>
> ---
> tools/include/nolibc/dirent.h | 38 +++++++++++++++++++++++++++--------
> 1 file changed, 30 insertions(+), 8 deletions(-)
>
> diff --git a/tools/include/nolibc/dirent.h b/tools/include/nolibc/dirent.h
> index 4e02ef25e72d..ba229b215a0b 100644
> --- a/tools/include/nolibc/dirent.h
> +++ b/tools/include/nolibc/dirent.h
> @@ -27,6 +27,18 @@ typedef struct {
> char dummy[1];
> } DIR;
>
> +/* Internal dirfd() that does not set errno */
> +static __attribute__((unused))
> +int nolibc_dirfd(DIR *dirp)

This pollutes the applications symbol namespace.
It should begin with underscores.
But IMO we could also just keep it inlined in the two users.

> +{
> + intptr_t i = (intptr_t)dirp;
> +
> + if (i >= 0)
> + return -1;
> +
> + return ~i;
> +}

Could you also add a test for an fdopendir() -> dirfd() roundtrip?


Thomas