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