Subject: Re: [PATCH v2] wrapper: NetBSD gives EFTYPE and FreeBSD gives EMFILE where POSIX uses ELOOP

From: Patrick Steinhardt <[email-masked]>
Date: Mon, 5 May 2025 08:43:39 +0200

On Fri, May 02, 2025 at 09:16:51PM -0700, Collin Funk wrote:
> As documented on NetBSD's man page, open with the O_NOFOLLOW flag and a
> symlink returns -1 and sets errno to EFTYPE which differs from POSIX.
> 
> This patch fixes the following test failure:
> 
>     $ sh t0602-reffiles-fsck.sh --verbose
>     --- expect	2025-05-02 23:05:23.920890147 +0000
>     +++ err	2025-05-02 23:05:23.916794959 +0000
>     @@ -1 +1 @@
>     -error: packed-refs: badRefFiletype: not a regular file but a symlink
>     +error: unable to open '.git/packed-refs': Inappropriate file type or format
>     not ok 12 - the filetype of packed-refs should be checked
> 
> FreeBSD has the same issue for EMLINK instead of EFTYPE.
> 
> This portability issue was introduced in cfea2f2da8 (packed-backend:
> check whether the "packed-refs" is regular file, 2025-02-28)

Ok, makes sense.

> diff --git a/wrapper.c b/wrapper.c
> index 3c79778055..f74e3f7747 100644
> --- a/wrapper.c
> +++ b/wrapper.c
> @@ -737,7 +737,26 @@ int is_empty_or_missing_file(const char *filename)
>  int open_nofollow(const char *path, int flags)
>  {
>  #ifdef O_NOFOLLOW
> -	return open(path, flags | O_NOFOLLOW);
> +	int ret = open(path, flags | O_NOFOLLOW);
> +	/*
> +	 * NetBSD sets errno to EFTYPE when path is a symlink. The only other
> +	 * time this errno occurs when O_REGULAR is used. Since we don't use
> +	 * it anywhere we can avoid an lstat here. FreeBSD does the same with
> +	 * EMLINK.
> +	 */
> +#ifdef __NetBSD__
> +#define SYMLINK_ERRNO EFTYPE
> +#elif defined(__FreeBSD__)
> +#define SYMLINK_ERRNO EMLINK
> +#endif

Nit, to make this a bit easier to read: our style guide says that nested
preprocessor directives should be indented by one spaces. So this would
become:

    # ifdef __NetBSD__
    #  define SYMLINK_ERRNO EFTYPE
    # elif defined(__FreeBSD__)
    #  define SYMLINK_ERRNO EMLINK
    # endif

Note that the `ifdef` itself would also be indented because we already
have a surrounding `#ifdef O_NOFOLLOW`.

> +#if SYMLINK_ERRNO
> +	if (ret < 0 && errno == SYMLINK_ERRNO) {
> +		errno = ELOOP;
> +		return -1;
> +	}
> +#undef SYMLINK_ERRNO
> +#endif

These three preprocessor defines should be indented, as well.

Patrick