]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/file_utils.h
rename functions which clash with libsystemd's
[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
1160ce89
CB
6#include "config.h"
7
37ef15bb
CB
8#include <fcntl.h>
9#include <stdbool.h>
10#include <stdio.h>
11#include <sys/stat.h>
12#include <sys/types.h>
99d03dec 13#include <sys/uio.h>
37ef15bb
CB
14#include <sys/vfs.h>
15#include <unistd.h>
16
674c9692 17#include "compiler.h"
c55fe36d 18#include "memory_utils.h"
7166ab75 19#include "syscall_wrappers.h"
674c9692 20
37ef15bb 21/* read and write whole files */
4a9ee78a
CB
22__hidden extern int lxc_write_to_file(const char *filename, const void *buf, size_t count,
23 bool add_newline, mode_t mode) __access_r(2, 3);
674c9692 24
4a9ee78a
CB
25__hidden extern int lxc_readat(int dirfd, const char *filename, void *buf, size_t count)
26 __access_w(3, 4);
674c9692 27
4a9ee78a
CB
28__hidden extern int lxc_writeat(int dirfd, const char *filename, const void *buf, size_t count)
29 __access_r(3, 4);
674c9692 30
4a9ee78a
CB
31__hidden extern int lxc_write_openat(const char *dir, const char *filename, const void *buf,
32 size_t count) __access_r(3, 4);
674c9692 33
4a9ee78a
CB
34__hidden extern int lxc_read_from_file(const char *filename, void *buf, size_t count)
35 __access_w(2, 3);
37ef15bb
CB
36
37/* send and receive buffers completely */
4a9ee78a 38__hidden extern ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) __access_r(2, 3);
674c9692 39
4a9ee78a
CB
40__hidden extern ssize_t lxc_pwrite_nointr(int fd, const void *buf, size_t count, off_t offset)
41 __access_r(2, 3);
674c9692 42
4a9ee78a
CB
43__hidden extern ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
44 __access_r(2, 3);
674c9692 45
4a9ee78a 46__hidden extern ssize_t lxc_read_nointr(int fd, void *buf, size_t count) __access_w(2, 3);
674c9692 47
a031a4e1
CB
48__access_w(2, 3) static inline int lxc_read_string_nointr(int fd, char *buf,
49 size_t count)
50{
51 ssize_t ret;
52
53 ret = lxc_read_nointr(fd, buf, STRARRAYLEN(buf));
54 if (ret < 0)
55 return -errno;
56 if (ret == 0)
57 return ret_errno(ENODATA);
58 if ((size_t)ret >= sizeof(buf))
59 return ret_errno(E2BIG);
60 buf[ret] = '\0';
61 return 0;
62}
63
4a9ee78a
CB
64__hidden extern ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count,
65 const void *expected_buf) __access_w(2, 3);
674c9692 66
4a9ee78a
CB
67__hidden extern ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count,
68 const void *expected_buf) __access_w(2, 3);
674c9692 69
4a9ee78a
CB
70__hidden extern ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
71 __access_w(2, 3);
674c9692 72
4a9ee78a
CB
73__hidden extern ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
74 int flags);
de69edd1 75
4a9ee78a
CB
76__hidden extern bool file_exists(const char *f);
77__hidden extern int print_to_file(const char *file, const char *content);
539c3977 78__hidden extern int lxc_is_dir(const char *path);
4a9ee78a
CB
79__hidden extern int lxc_count_file_lines(const char *fn);
80__hidden extern int lxc_make_tmpfile(char *template, bool rm);
37ef15bb
CB
81
82/* __typeof__ should be safe to use with all compilers. */
83typedef __typeof__(((struct statfs *)NULL)->f_type) fs_type_magic;
4a9ee78a
CB
84__hidden extern bool has_fs_type(const char *path, fs_type_magic magic_val);
85__hidden extern bool fhas_fs_type(int fd, fs_type_magic magic_val);
539c3977 86__hidden extern bool lxc_is_fs_type(const struct statfs *fs, fs_type_magic magic_val);
4a9ee78a
CB
87__hidden extern FILE *fopen_cloexec(const char *path, const char *mode);
88__hidden extern ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count);
89__hidden extern char *file_to_buf(const char *path, size_t *length);
90__hidden extern int fd_to_buf(int fd, char **buf, size_t *length);
26dffd82
CB
91__hidden extern ssize_t __fd_to_fd(int from, int to);
92static inline int fd_to_fd(int from, int to)
93{
94 return __fd_to_fd(from, to) >= 0;
95}
539c3977 96__hidden extern int lxc_fd_cloexec(int fd, bool cloexec);
4a9ee78a
CB
97__hidden extern int lxc_open_dirfd(const char *dir);
98__hidden extern FILE *fdopen_cached(int fd, const char *mode, void **caller_freed_buffer);
5129b2d3
CB
99__hidden extern FILE *fdopen_at(int dfd, const char *path, const char *mode,
100 unsigned int o_flags,
101 unsigned int resolve_flags);
4a9ee78a
CB
102__hidden extern FILE *fopen_cached(const char *path, const char *mode, void **caller_freed_buffer);
103__hidden extern int timens_offset_write(clockid_t clk_id, int64_t s_offset, int64_t ns_offset);
6f61472b 104__hidden extern bool exists_dir_at(int dir_fd, const char *path);
953db219 105__hidden extern bool exists_file_at(int dir_fd, const char *path);
cce677d1
CB
106__hidden extern int open_at(int dfd, const char *path, unsigned int o_flags,
107 unsigned int resolve_flags, mode_t mode);
9910185a
CB
108__hidden extern int open_at_same(int fd_same, int dfd, const char *path,
109 unsigned int o_flags,
110 unsigned int resolve_flags, mode_t mode);
589a930f 111__hidden extern int open_beneath(int dfd, const char *path, unsigned int flags);
747c4df3 112__hidden int fd_make_blocking(int fd);
7eff16cc 113__hidden int fd_make_nonblocking(int fd);
46bf13b7
CB
114__hidden extern char *read_file_at(int dfd, const char *fnam,
115 unsigned int o_flags,
116 unsigned resolve_flags);
2b5e0b8b
CB
117__hidden extern ssize_t lxc_read_try_buf_at(int dfd, const char *path,
118 void *buf, size_t count);
37ef15bb 119
8e40762d
CB
120/*
121 * Check if two fds refer to the same file.
122 * The function is "lax" in so far, as it doesn't care whether fda and fdb have
123 * the same flags or whether they share the same device context when they refer
124 * to devices.
125 */
126__hidden extern bool same_file_lax(int fda, int fdb);
127
6347774b
CB
128static inline int dup_cloexec(int fd)
129{
130 __do_close int fd_dup = -EBADF;
131
132 fd_dup = dup(fd);
133 if (fd_dup < 0)
134 return -errno;
135
539c3977 136 if (lxc_fd_cloexec(fd_dup, true))
6347774b
CB
137 return -errno;
138
139 return move_fd(fd_dup);
140}
141
d06abe2f
CB
142__hidden extern bool same_device(int fda, const char *patha, int fdb,
143 const char *pathb);
144
37ef15bb 145#endif /* __LXC_FILE_UTILS_H */