Refactor portable support

This commit is contained in:
Andrea Mazzoleni 2026-06-12 09:21:18 +02:00
parent bb39c6ca3d
commit db20376bca
17 changed files with 6036 additions and 5835 deletions

View file

@ -12,11 +12,6 @@
#define O_BINARY 0 /**< Not used in Unix. */
#define O_SEQUENTIAL 0 /**< In Unix posix_fadvise() shall be used. */
#if HAVE_POSIX_FADVISE
int posix_fadvise_wrapper(int fd, off_t offset, off_t len, int advice);
#endif
/**
* If nanoseconds are not supported, we report the special STAT_NSEC_INVALID value,
* to mark that it's undefined.
@ -34,6 +29,63 @@ int posix_fadvise_wrapper(int fd, off_t offset, off_t len, int advice);
#define STAT_NSEC(st) STAT_NSEC_INVALID
#endif
#if HAVE_POSIX_FADVISE
/**
* Wrapper around posix_fadvise() that handles ENOENT by converting it to
* ENOSYS.
*
* Why can posix_fadvise() return ENOENT?
*
* According to POSIX and the Linux kernel's standard fadvise64 implementation
* (mm/fadvise.c), posix_fadvise() should only return EBADF (bad file
* descriptor), ESPIPE (fd refers to a pipe or FIFO), or EINVAL (invalid
* advice value or arguments). ENOENT is never generated by the kernel's
* generic path.
*
* However, the VFS layer allows individual filesystems to provide their own
* .fadvise() method, which is called instead of (or in addition to) the
* generic implementation. Certain filesystem drivers can return ENOENT in
* this method for reasons that are completely unrelated to the file not
* existing (the file is clearly open, we have a valid fd):
*
* - NFS: A stale NFS inode can be revalidated against the server while
* fadvise is being processed. If the server-side file has been removed or
* renamed, the revalidation discovers the inode is gone and returns
* -ENOENT, even though the client still holds an open file descriptor.
*
* - FUSE-based filesystems (mergerfs, sshfs, rclone, etc.): The FUSE daemon
* receives the fadvise request and may forward it to an underlying backend.
* If that backend cannot find the file (e.g., the backing path has changed,
* the FUSE daemon is partially initialised, or the daemon translates the
* request and the target layer returns an error), the daemon may return
* ENOENT to the kernel.
*
* - Overlayfs (used by Docker, Podman, LXC): The overlayfs .fadvise()
* implementation delegates to the lower or upper layer. If the lookup
* through the overlay stack fails to find the real inode (e.g., the upper
* directory entry is absent and the lower copy is in a weird state), the
* delegation can return ENOENT.
*
* - Other network or virtual filesystems: Any out-of-tree or in-tree
* filesystem that implements .fadvise() and performs a path or inode
* lookup as part of processing the hint can propagate ENOENT if that
* internal lookup fails.
*
* Because posix_fadvise() is always an advisory hint, it never affects
* correctness, only performance, it is safe to treat unexpected errors,
* including ENOENT, as "not supported" (ENOSYS) and continue.
*/
int posix_fadvise_wrapper(int fd, off_t offset, off_t len, int advice);
#endif
#if !HAVE_EACCESS
/**
* Check effective user's permissions for a file.
* Conceptually identical to access(), but uses the effective UID/GID.
*/
int eaccess(const char* pathname, int mode);
#endif
/**
* Open a file with the O_NOATIME flag to avoid to update the access time.
*/