chase-symlinks: Rework open() functions and some chase flags
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 13 Mar 2023 15:17:21 +0000 (16:17 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 15 Mar 2023 11:38:11 +0000 (12:38 +0100)
commit47f0e1b5e04c27572b540ae4a86e522d268ffd3c
treeb01e0e1f57c7646a619087e3d74217d6215bec8b
parent06344330a16b2bc6daeeef55bf77eea937446755
chase-symlinks: Rework open() functions and some chase flags

Currently, when CHASE_PARENT is specified, we chase the parent directory
of the symlink itself. Let's change this and chase the parent directory
of the symlink target so that trying to open the actual file later with
O_NOFOLLOW doesn't fail with ELOOP.

To get the current behavior, callers can add CHASE_NOFOLLOW to chase
the parent directory of the symlink itself.

Currently, when CHASE_MKDIR_0755 is specified, we create all components
of the path as directories. Instead, let's change the flag to only create
parent directories and leave the final component of the PATH untouched.
Also, allow CHASE_NONEXISTENT with CHASE_MKDIR_0755 now that it doesn't
create all components anymore.

Finally, rework chase_symlinks_and_open() and chase_symlinkat_at_and_open()
to always chase the parent directory and use xopenat() to open the final
component of the path. This allows us to pass O_CREAT to create the file or
directory (O_DIRECTORY) if it is missing. If CHASE_PARENT is configured, we
just reopen the parent directory that we chased.
src/basic/chase-symlinks.c
src/basic/chase-symlinks.h
src/test/test-fs-util.c