]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/file_utils.c
Merge pull request #2629 from ssup2/master
[mirror_lxc.git] / src / lxc / file_utils.c
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
20 #ifndef _GNU_SOURCE
21 #define _GNU_SOURCE 1
22 #endif
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <linux/magic.h>
26 #include <stdlib.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include "config.h"
31 #include "file_utils.h"
32 #include "log.h"
33 #include "macro.h"
34 #include "string.h"
35
36 lxc_log_define(file_utils, lxc);
37
38 int lxc_write_to_file(const char *filename, const void *buf, size_t count,
39 bool add_newline, mode_t mode)
40 {
41 int fd, saved_errno;
42 ssize_t ret;
43
44 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
45 if (fd < 0)
46 return -1;
47
48 ret = lxc_write_nointr(fd, buf, count);
49 if (ret < 0)
50 goto out_error;
51
52 if ((size_t)ret != count)
53 goto out_error;
54
55 if (add_newline) {
56 ret = lxc_write_nointr(fd, "\n", 1);
57 if (ret != 1)
58 goto out_error;
59 }
60
61 close(fd);
62 return 0;
63
64 out_error:
65 saved_errno = errno;
66 close(fd);
67 errno = saved_errno;
68 return -1;
69 }
70
71 int lxc_read_from_file(const char *filename, void *buf, size_t count)
72 {
73 int fd = -1, saved_errno;
74 ssize_t ret;
75
76 fd = open(filename, O_RDONLY | O_CLOEXEC);
77 if (fd < 0)
78 return -1;
79
80 if (!buf || !count) {
81 char buf2[100];
82 size_t count2 = 0;
83
84 while ((ret = lxc_read_nointr(fd, buf2, 100)) > 0)
85 count2 += ret;
86
87 if (ret >= 0)
88 ret = count2;
89 } else {
90 memset(buf, 0, count);
91 ret = lxc_read_nointr(fd, buf, count);
92 }
93
94 saved_errno = errno;
95 close(fd);
96 errno = saved_errno;
97 return ret;
98 }
99
100 ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
101 {
102 ssize_t ret;
103 again:
104 ret = write(fd, buf, count);
105 if (ret < 0 && errno == EINTR)
106 goto again;
107
108 return ret;
109 }
110
111 ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
112 {
113 ssize_t ret;
114 again:
115 ret = send(sockfd, buf, len, flags);
116 if (ret < 0 && errno == EINTR)
117 goto again;
118
119 return ret;
120 }
121
122 ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
123 {
124 ssize_t ret;
125 again:
126 ret = read(fd, buf, count);
127 if (ret < 0 && errno == EINTR)
128 goto again;
129
130 return ret;
131 }
132
133 ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
134 {
135 ssize_t ret;
136 again:
137 ret = recv(sockfd, buf, len, flags);
138 if (ret < 0 && errno == EINTR)
139 goto again;
140
141 return ret;
142 }
143
144 ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
145 {
146 ssize_t ret;
147
148 ret = lxc_read_nointr(fd, buf, count);
149 if (ret <= 0)
150 return ret;
151
152 if ((size_t)ret != count)
153 return -1;
154
155 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
156 errno = EINVAL;
157 return -1;
158 }
159
160 return ret;
161 }
162
163 bool file_exists(const char *f)
164 {
165 struct stat statbuf;
166
167 return stat(f, &statbuf) == 0;
168 }
169
170 int print_to_file(const char *file, const char *content)
171 {
172 FILE *f;
173 int ret = 0;
174
175 f = fopen(file, "w");
176 if (!f)
177 return -1;
178
179 if (fprintf(f, "%s", content) != strlen(content))
180 ret = -1;
181
182 fclose(f);
183 return ret;
184 }
185
186 int is_dir(const char *path)
187 {
188 struct stat statbuf;
189 int ret;
190
191 ret = stat(path, &statbuf);
192 if (ret == 0 && S_ISDIR(statbuf.st_mode))
193 return 1;
194
195 return 0;
196 }
197
198 /*
199 * Return the number of lines in file @fn, or -1 on error
200 */
201 int lxc_count_file_lines(const char *fn)
202 {
203 FILE *f;
204 char *line = NULL;
205 size_t sz = 0;
206 int n = 0;
207
208 f = fopen_cloexec(fn, "r");
209 if (!f)
210 return -1;
211
212 while (getline(&line, &sz, f) != -1) {
213 n++;
214 }
215
216 free(line);
217 fclose(f);
218 return n;
219 }
220
221 int lxc_make_tmpfile(char *template, bool rm)
222 {
223 int fd, ret;
224 mode_t msk;
225
226 msk = umask(0022);
227 fd = mkstemp(template);
228 umask(msk);
229 if (fd < 0)
230 return -1;
231
232 if (!rm)
233 return fd;
234
235 ret = unlink(template);
236 if (ret < 0) {
237 close(fd);
238 return -1;
239 }
240
241 return fd;
242 }
243
244 bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
245 {
246 return (fs->f_type == (fs_type_magic)magic_val);
247 }
248
249 bool has_fs_type(const char *path, fs_type_magic magic_val)
250 {
251 int ret;
252 struct statfs sb;
253
254 ret = statfs(path, &sb);
255 if (ret < 0)
256 return false;
257
258 return is_fs_type(&sb, magic_val);
259 }
260
261 bool fhas_fs_type(int fd, fs_type_magic magic_val)
262 {
263 int ret;
264 struct statfs sb;
265
266 ret = fstatfs(fd, &sb);
267 if (ret < 0)
268 return false;
269
270 return is_fs_type(&sb, magic_val);
271 }
272
273 FILE *fopen_cloexec(const char *path, const char *mode)
274 {
275 int open_mode = 0;
276 int step = 0;
277 int fd;
278 int saved_errno = 0;
279 FILE *ret;
280
281 if (!strncmp(mode, "r+", 2)) {
282 open_mode = O_RDWR;
283 step = 2;
284 } else if (!strncmp(mode, "r", 1)) {
285 open_mode = O_RDONLY;
286 step = 1;
287 } else if (!strncmp(mode, "w+", 2)) {
288 open_mode = O_RDWR | O_TRUNC | O_CREAT;
289 step = 2;
290 } else if (!strncmp(mode, "w", 1)) {
291 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
292 step = 1;
293 } else if (!strncmp(mode, "a+", 2)) {
294 open_mode = O_RDWR | O_CREAT | O_APPEND;
295 step = 2;
296 } else if (!strncmp(mode, "a", 1)) {
297 open_mode = O_WRONLY | O_CREAT | O_APPEND;
298 step = 1;
299 }
300 for (; mode[step]; step++)
301 if (mode[step] == 'x')
302 open_mode |= O_EXCL;
303 open_mode |= O_CLOEXEC;
304
305 fd = open(path, open_mode, 0666);
306 if (fd < 0)
307 return NULL;
308
309 ret = fdopen(fd, mode);
310 saved_errno = errno;
311 if (!ret)
312 close(fd);
313 errno = saved_errno;
314 return ret;
315 }