]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/file_utils.c
spelling: keepdata
[mirror_lxc.git] / src / lxc / file_utils.c
CommitLineData
37ef15bb
CB
1/* liblxcapi
2 *
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
d38dd64a
CB
20#ifndef _GNU_SOURCE
21#define _GNU_SOURCE 1
22#endif
37ef15bb
CB
23#include <errno.h>
24#include <fcntl.h>
25#include <linux/magic.h>
26#include <stdlib.h>
27#include <sys/stat.h>
7c4d9466 28#include <sys/sendfile.h>
37ef15bb
CB
29#include <sys/types.h>
30
d38dd64a 31#include "config.h"
37ef15bb
CB
32#include "file_utils.h"
33#include "log.h"
34#include "macro.h"
35#include "string.h"
36
37ef15bb 37lxc_log_define(file_utils, lxc);
37ef15bb
CB
38
39int lxc_write_to_file(const char *filename, const void *buf, size_t count,
40 bool add_newline, mode_t mode)
41{
42 int fd, saved_errno;
43 ssize_t ret;
44
45 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
46 if (fd < 0)
47 return -1;
48
49 ret = lxc_write_nointr(fd, buf, count);
50 if (ret < 0)
51 goto out_error;
52
53 if ((size_t)ret != count)
54 goto out_error;
55
56 if (add_newline) {
57 ret = lxc_write_nointr(fd, "\n", 1);
58 if (ret != 1)
59 goto out_error;
60 }
61
62 close(fd);
63 return 0;
64
65out_error:
66 saved_errno = errno;
67 close(fd);
68 errno = saved_errno;
69 return -1;
70}
71
72int lxc_read_from_file(const char *filename, void *buf, size_t count)
73{
74 int fd = -1, saved_errno;
75 ssize_t ret;
76
77 fd = open(filename, O_RDONLY | O_CLOEXEC);
78 if (fd < 0)
79 return -1;
80
81 if (!buf || !count) {
82 char buf2[100];
83 size_t count2 = 0;
84
85 while ((ret = lxc_read_nointr(fd, buf2, 100)) > 0)
86 count2 += ret;
87
88 if (ret >= 0)
89 ret = count2;
90 } else {
91 memset(buf, 0, count);
92 ret = lxc_read_nointr(fd, buf, count);
93 }
94
95 saved_errno = errno;
96 close(fd);
97 errno = saved_errno;
98 return ret;
99}
100
101ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
102{
103 ssize_t ret;
104again:
105 ret = write(fd, buf, count);
106 if (ret < 0 && errno == EINTR)
107 goto again;
108
109 return ret;
110}
111
28143f88
CB
112ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
113{
114 ssize_t ret;
115again:
116 ret = send(sockfd, buf, len, flags);
117 if (ret < 0 && errno == EINTR)
118 goto again;
119
120 return ret;
121}
122
37ef15bb
CB
123ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
124{
125 ssize_t ret;
126again:
127 ret = read(fd, buf, count);
128 if (ret < 0 && errno == EINTR)
129 goto again;
130
131 return ret;
132}
133
de69edd1
CB
134ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
135{
136 ssize_t ret;
137again:
138 ret = recv(sockfd, buf, len, flags);
139 if (ret < 0 && errno == EINTR)
140 goto again;
141
142 return ret;
143}
144
37ef15bb
CB
145ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
146{
147 ssize_t ret;
148
149 ret = lxc_read_nointr(fd, buf, count);
150 if (ret <= 0)
151 return ret;
152
153 if ((size_t)ret != count)
154 return -1;
155
156 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
157 errno = EINVAL;
158 return -1;
159 }
160
161 return ret;
162}
163
164bool file_exists(const char *f)
165{
166 struct stat statbuf;
167
168 return stat(f, &statbuf) == 0;
169}
170
171int print_to_file(const char *file, const char *content)
172{
173 FILE *f;
174 int ret = 0;
175
176 f = fopen(file, "w");
177 if (!f)
178 return -1;
179
180 if (fprintf(f, "%s", content) != strlen(content))
181 ret = -1;
182
183 fclose(f);
184 return ret;
185}
186
187int is_dir(const char *path)
188{
189 struct stat statbuf;
190 int ret;
191
192 ret = stat(path, &statbuf);
193 if (ret == 0 && S_ISDIR(statbuf.st_mode))
194 return 1;
195
196 return 0;
197}
198
199/*
200 * Return the number of lines in file @fn, or -1 on error
201 */
202int lxc_count_file_lines(const char *fn)
203{
204 FILE *f;
205 char *line = NULL;
206 size_t sz = 0;
207 int n = 0;
208
209 f = fopen_cloexec(fn, "r");
210 if (!f)
211 return -1;
212
213 while (getline(&line, &sz, f) != -1) {
214 n++;
215 }
216
217 free(line);
218 fclose(f);
219 return n;
220}
221
222int lxc_make_tmpfile(char *template, bool rm)
223{
224 int fd, ret;
225 mode_t msk;
226
227 msk = umask(0022);
228 fd = mkstemp(template);
229 umask(msk);
230 if (fd < 0)
231 return -1;
232
233 if (!rm)
234 return fd;
235
236 ret = unlink(template);
237 if (ret < 0) {
238 close(fd);
239 return -1;
240 }
241
242 return fd;
243}
244
37ef15bb
CB
245bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
246{
247 return (fs->f_type == (fs_type_magic)magic_val);
248}
249
250bool has_fs_type(const char *path, fs_type_magic magic_val)
251{
37ef15bb
CB
252 int ret;
253 struct statfs sb;
254
255 ret = statfs(path, &sb);
256 if (ret < 0)
257 return false;
258
259 return is_fs_type(&sb, magic_val);
260}
261
262bool fhas_fs_type(int fd, fs_type_magic magic_val)
263{
264 int ret;
265 struct statfs sb;
266
267 ret = fstatfs(fd, &sb);
268 if (ret < 0)
269 return false;
270
271 return is_fs_type(&sb, magic_val);
272}
273
274FILE *fopen_cloexec(const char *path, const char *mode)
275{
276 int open_mode = 0;
277 int step = 0;
278 int fd;
279 int saved_errno = 0;
280 FILE *ret;
281
282 if (!strncmp(mode, "r+", 2)) {
283 open_mode = O_RDWR;
284 step = 2;
285 } else if (!strncmp(mode, "r", 1)) {
286 open_mode = O_RDONLY;
287 step = 1;
288 } else if (!strncmp(mode, "w+", 2)) {
289 open_mode = O_RDWR | O_TRUNC | O_CREAT;
290 step = 2;
291 } else if (!strncmp(mode, "w", 1)) {
292 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
293 step = 1;
294 } else if (!strncmp(mode, "a+", 2)) {
295 open_mode = O_RDWR | O_CREAT | O_APPEND;
296 step = 2;
297 } else if (!strncmp(mode, "a", 1)) {
298 open_mode = O_WRONLY | O_CREAT | O_APPEND;
299 step = 1;
300 }
301 for (; mode[step]; step++)
302 if (mode[step] == 'x')
303 open_mode |= O_EXCL;
304 open_mode |= O_CLOEXEC;
305
3e9bde7e 306 fd = open(path, open_mode, 0660);
37ef15bb
CB
307 if (fd < 0)
308 return NULL;
309
310 ret = fdopen(fd, mode);
311 saved_errno = errno;
312 if (!ret)
313 close(fd);
314 errno = saved_errno;
315 return ret;
316}
7c4d9466
CB
317
318ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
319{
320 ssize_t ret;
321
322again:
323 ret = sendfile(out_fd, in_fd, offset, count);
324 if (ret < 0) {
325 if (errno == EINTR)
326 goto again;
327
328 return -1;
329 }
330
331 return ret;
332}