]>
Commit | Line | Data |
---|---|---|
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. */ | |
64 | typedef __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); |
73 | static 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); | |
87 | static 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 */ |