fix: make the preload helper correct on FreeBSD and safe on reopen

- The helper called Linux fgetxattr on FreeBSD. Read mergerfs.fullpath
  there through extattr_get_fd in EXTATTR_NAMESPACE_USER; Linux keeps
  user.mergerfs.fullpath.
- FreeBSD mode_t is promoted when passed through an ellipsis, so
  reading it back as mode_t was undefined. Read the promoted type.
- O_TMPFILE includes O_DIRECTORY on Linux, so a plain directory open
  consumed a mode argument that wasn't passed. Require O_CREAT or the
  whole nonzero O_TMPFILE mask.
- The backing path is returned without a terminating NUL. Reject empty
  or oversized values and terminate it before reopening; keep the
  already-open descriptor when no usable path is available.
- The reopen of the backing file repeated O_TRUNC, and creat() ran
  creat() again. The open through mergerfs had already truncated that
  file, so the second truncation could only destroy data another
  process wrote in between. Drop O_TRUNC from the reopen and reopen
  creat()ed files O_WRONLY.
- fopen and fopen64 reopened the branch path with the caller's mode,
  so w truncated a second time and a could recreate a name removed in
  between. Redirect only modes starting with r, including r+, as
  given, and keep the original stream otherwise; tooling.md says so.
  TEST_preload_fopen_modes checks the bytes and that a missing branch
  name is not created.
- fs_fstatat.hpp used AT_SYMLINK_NOFOLLOW without including fcntl.h,
  which failed against FreeBSD headers.
This commit is contained in:
Antonio SJ Musumeci 2026-11-25 12:59:47 -05:00
parent eea1c1bc09
commit 11e759101d
4 changed files with 195 additions and 90 deletions

View file

@ -97,6 +97,12 @@ this also means certain mergerfs features that work by interrupting
the read/write workflow, such as
[moveonenospc](config/moveonenospc.md), will no longer work.
`fopen` and `fopen64` redirect modes beginning with `r`, including
`r+`, without rewriting the mode. They keep the original stream for
`w`, `a`, or any mode containing `x`: opening the branch path a second
time could truncate a file twice or recreate a name that no longer
exists. Writes on a retained stream still go through mergerfs.
Also, understand that this will only work on dynamically linked
software (that is dynamically linked with the same general libc version
as the software being used with it.) Anything statically compiled will