// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once

#include <stdbool.h>

/* Symlink-safe path resolution against an alternate filesystem root.
 *
 * chase() walks `path` component by component, refusing to follow symlinks
 * whose targets would escape `root`. Returned paths are host-absolute
 */

typedef enum {
	CHASE_PREFIX_ROOT = 1 << 0,
	CHASE_NONEXISTENT = 1 << 1,
	CHASE_NOFOLLOW = 1 << 2,
	CHASE_WARN = 1 << 3,
} ChaseFlags;

#define CHASE_MAX 128U

int chase(const char *path, const char *root, ChaseFlags flags, char **ret_path,
		  int *ret_fd);

int chase_full(const char *path, const char *root, ChaseFlags flags,
               char **ret_path, int *ret_fd,
               int *ret_parent_fd, char **ret_basename);

int chase_and_open(const char *path, const char *root, ChaseFlags flags,
                   int open_flags, char **ret_path);
