file_utils: add open_at()

parent 42673edd
......@@ -621,22 +621,24 @@ bool exists_file_at(int dir_fd, const char *path)
return fstatat(dir_fd, path, &sb, 0) == 0;
}
int open_beneath(int dir_fd, const char *path, unsigned int flags)
int open_at(int dfd, const char *path, mode_t mode, unsigned int o_flags,
unsigned int resolve_flags)
{
__do_close int fd = -EBADF;
struct lxc_open_how how = {
.flags = flags,
.resolve = RESOLVE_NO_XDEV | RESOLVE_NO_SYMLINKS | RESOLVE_NO_MAGICLINKS | RESOLVE_BENEATH,
.flags = o_flags,
.mode = mode,
.resolve = resolve_flags,
};
fd = openat2(dir_fd, path, &how, sizeof(how));
fd = openat2(dfd, path, &how, sizeof(how));
if (fd >= 0)
return move_fd(fd);
if (errno != ENOSYS)
return -errno;
return openat(dir_fd, path, O_NOFOLLOW | flags);
return openat(dfd, path, O_NOFOLLOW | o_flags);
}
int fd_make_nonblocking(int fd)
......
......@@ -13,6 +13,7 @@
#include <unistd.h>
#include "compiler.h"
#include "syscall_wrappers.h"
/* read and write whole files */
__hidden extern int lxc_write_to_file(const char *filename, const void *buf, size_t count,
......@@ -81,7 +82,16 @@ __hidden extern FILE *fopen_cached(const char *path, const char *mode, void **ca
__hidden extern int timens_offset_write(clockid_t clk_id, int64_t s_offset, int64_t ns_offset);
__hidden extern bool exists_dir_at(int dir_fd, const char *path);
__hidden extern bool exists_file_at(int dir_fd, const char *path);
__hidden extern int open_beneath(int dir_fd, const char *path, unsigned int flags);
__hidden extern int open_at(int dfd, const char *path, mode_t mode,
unsigned int o_flags, unsigned int resolve_flags);
static inline int open_beneath(int dfd, const char *path, unsigned int flags)
{
return open_at(dfd, path, 0, flags,
RESOLVE_NO_XDEV |
RESOLVE_NO_SYMLINKS |
RESOLVE_NO_MAGICLINKS |
RESOLVE_BENEATH);
}
__hidden int fd_make_nonblocking(int fd);
__hidden extern char *read_file_at(int dfd, const char *fnam);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment