]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/file_utils.h
file_utils: add open_at()
[mirror_lxc.git] / src / lxc / file_utils.h
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
37ef15bb
CB
2
3#ifndef __LXC_FILE_UTILS_H
4#define __LXC_FILE_UTILS_H
5
37ef15bb
CB
6#include <fcntl.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <sys/stat.h>
10#include <sys/types.h>
99d03dec 11#include <sys/uio.h>
37ef15bb
CB
12#include <sys/vfs.h>
13#include <unistd.h>
14
674c9692 15#include "compiler.h"
7166ab75 16#include "syscall_wrappers.h"
674c9692 17
37ef15bb 18/* read and write whole files */
4a9ee78a
CB
19__hidden extern int lxc_write_to_file(const char *filename, const void *buf, size_t count,
20 bool add_newline, mode_t mode) __access_r(2, 3);
674c9692 21
4a9ee78a
CB
22__hidden extern int lxc_readat(int dirfd, const char *filename, void *buf, size_t count)
23 __access_w(3, 4);
674c9692 24
4a9ee78a
CB
25__hidden extern int lxc_writeat(int dirfd, const char *filename, const void *buf, size_t count)
26 __access_r(3, 4);
674c9692 27
4a9ee78a
CB
28__hidden extern int lxc_write_openat(const char *dir, const char *filename, const void *buf,
29 size_t count) __access_r(3, 4);
674c9692 30
4a9ee78a
CB
31__hidden extern int lxc_read_from_file(const char *filename, void *buf, size_t count)
32 __access_w(2, 3);
37ef15bb
CB
33
34/* send and receive buffers completely */
4a9ee78a 35__hidden extern ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) __access_r(2, 3);
674c9692 36
4a9ee78a
CB
37__hidden extern ssize_t lxc_pwrite_nointr(int fd, const void *buf, size_t count, off_t offset)
38 __access_r(2, 3);
674c9692 39
4a9ee78a
CB
40__hidden extern ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
41 __access_r(2, 3);
674c9692 42
4a9ee78a 43__hidden extern ssize_t lxc_read_nointr(int fd, void *buf, size_t count) __access_w(2, 3);
674c9692 44
4a9ee78a
CB
45__hidden extern ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count,
46 const void *expected_buf) __access_w(2, 3);
674c9692 47
4a9ee78a
CB
48__hidden extern ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count,
49 const void *expected_buf) __access_w(2, 3);
674c9692 50
4a9ee78a
CB
51__hidden extern ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
52 __access_w(2, 3);
674c9692 53
4a9ee78a
CB
54__hidden extern ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
55 int flags);
de69edd1 56
4a9ee78a
CB
57__hidden extern bool file_exists(const char *f);
58__hidden extern int print_to_file(const char *file, const char *content);
59__hidden extern int is_dir(const char *path);
60__hidden extern int lxc_count_file_lines(const char *fn);
61__hidden extern int lxc_make_tmpfile(char *template, bool rm);
37ef15bb
CB
62
63/* __typeof__ should be safe to use with all compilers. */
64typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic;
4a9ee78a
CB
65__hidden extern bool has_fs_type(const char *path, fs_type_magic magic_val);
66__hidden extern bool fhas_fs_type(int fd, fs_type_magic magic_val);
67__hidden extern bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val);
68__hidden extern FILE *fopen_cloexec(const char *path, const char *mode);
69__hidden extern ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count);
70__hidden extern char *file_to_buf(const char *path, size_t *length);
71__hidden extern int fd_to_buf(int fd, char **buf, size_t *length);
26dffd82
CB
72__hidden extern ssize_t __fd_to_fd(int from, int to);
73static inline int fd_to_fd(int from, int to)
74{
75 return __fd_to_fd(from, to) >= 0;
76}
a60d8c4e 77__hidden extern int fd_cloexec(int fd, bool cloexec);
4a9ee78a
CB
78__hidden extern int lxc_open_dirfd(const char *dir);
79__hidden extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
a60d8c4e 80__hidden extern FILE *fdopenat(int dfd, const char *path, const char *mode);
4a9ee78a
CB
81__hidden extern FILE *fopen_cached(const char *path, const char *mode, void **caller_freed_buffer);
82__hidden extern int timens_offset_write(clockid_t clk_id, int64_t s_offset, int64_t ns_offset);
6f61472b 83__hidden extern bool exists_dir_at(int dir_fd, const char *path);
953db219 84__hidden extern bool exists_file_at(int dir_fd, const char *path);
7166ab75
CB
85__hidden extern int open_at(int dfd, const char *path, mode_t mode,
86 unsigned int o_flags, unsigned int resolve_flags);
87static inline int open_beneath(int dfd, const char *path, unsigned int flags)
88{
89 return open_at(dfd, path, 0, flags,
90 RESOLVE_NO_XDEV |
91 RESOLVE_NO_SYMLINKS |
92 RESOLVE_NO_MAGICLINKS |
93 RESOLVE_BENEATH);
94}
a60c98aa 95__hidden int fd_make_nonblocking(int fd);
d23cb29e 96__hidden extern char *read_file_at(int dfd, const char *fnam);
37ef15bb
CB
97
98#endif /* __LXC_FILE_UTILS_H */