// SPDX-License-Identifier: GPL-3.0-or-later

#include <acl/libacl.h>
#include <fcntl.h>
#include <unistd.h>
#include "platform.h"

int fchmod_opath(int fd, mode_t mode) {
	if (fchmod(fd, mode) < 0)
		return -errno;
	return 0;
}

int parse_attribute_from_arg(Item *item) {
	log_warning("Setting file attributes is not supported on this platform, ignoring for path '%s'.", item->path);
	return 0;
}

int fd_set_attribute(Item *i, int fd, const char *path, const struct stat *st) {
	(void)i;
	(void)fd;
	(void)path;
	(void)st;
	if (i->attribute_set)
		log_debug("File attributes not supported on this platform, ignoring.");
	return 0;
}

int path_set_attribute(Item *i, const char *path) {
	(void)i;
	(void)path;
	if (i->attribute_set)
		log_debug("File attributes not supported on this platform, ignoring.");
	return 0;
}

int fd_reopen(int fd, int parent_fd, const char *basename,
              const char *resolved_path, int open_flags) {
	(void)fd;
	(void)resolved_path;

	if (parent_fd < 0 || !basename)
		return -EINVAL;

	int new_fd = openat(parent_fd, basename, open_flags | O_NOFOLLOW);
	if (new_fd < 0)
		return -errno;
	return new_fd;
}

int acl_get_for_fd(int fd, int parent_fd, const char *basename,
               const char *resolved_path, acl_type_t type, acl_t *ret) {
	(void)fd;
	(void)parent_fd;
	(void)basename;

	if (!resolved_path)
		return -EINVAL;

	acl_t a = acl_get_file(resolved_path, type);
	if (!a)
		return -errno;
	*ret = a;
	return 0;
}

int acl_set_for_fd(int fd, int parent_fd, const char *basename,
               const char *resolved_path, acl_type_t type, acl_t acl) {
	(void)fd;
	(void)parent_fd;
	(void)basename;

	if (!resolved_path)
		return -EINVAL;

	if (acl_set_file(resolved_path, type, acl) < 0)
		return -errno;
	return 0;
}
