]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/file_utils.c
Merge pull request #2608 from brauner/2018-09-11/netns_get_nsid
[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_send_nointr(int sockfd, void *buf, size_t len, int flags)
110 {
111 ssize_t ret;
112 again:
113 ret = send(sockfd, buf, len, flags);
114 if (ret < 0 && errno == EINTR)
115 goto again;
116
117 return ret;
118 }
119
120 ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
121 {
122 ssize_t ret;
123 again:
124 ret = read(fd, buf, count);
125 if (ret < 0 && errno == EINTR)
126 goto again;
127
128 return ret;
129 }
130
131 ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
132 {
133 ssize_t ret;
134 again:
135 ret = recv(sockfd, buf, len, flags);
136 if (ret < 0 && errno == EINTR)
137 goto again;
138
139 return ret;
140 }
141
142 ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
143 {
144 ssize_t ret;
145
146 ret = lxc_read_nointr(fd, buf, count);
147 if (ret <= 0)
148 return ret;
149
150 if ((size_t)ret != count)
151 return -1;
152
153 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
154 errno = EINVAL;
155 return -1;
156 }
157
158 return ret;
159 }
160
161 bool file_exists(const char *f)
162 {
163 struct stat statbuf;
164
165 return stat(f, &statbuf) == 0;
166 }
167
168 int print_to_file(const char *file, const char *content)
169 {
170 FILE *f;
171 int ret = 0;
172
173 f = fopen(file, "w");
174 if (!f)
175 return -1;
176
177 if (fprintf(f, "%s", content) != strlen(content))
178 ret = -1;
179
180 fclose(f);
181 return ret;
182 }
183
184 int is_dir(const char *path)
185 {
186 struct stat statbuf;
187 int ret;
188
189 ret = stat(path, &statbuf);
190 if (ret == 0 && S_ISDIR(statbuf.st_mode))
191 return 1;
192
193 return 0;
194 }
195
196 /*
197 * Return the number of lines in file @fn, or -1 on error
198 */
199 int lxc_count_file_lines(const char *fn)
200 {
201 FILE *f;
202 char *line = NULL;
203 size_t sz = 0;
204 int n = 0;
205
206 f = fopen_cloexec(fn, "r");
207 if (!f)
208 return -1;
209
210 while (getline(&line, &sz, f) != -1) {
211 n++;
212 }
213
214 free(line);
215 fclose(f);
216 return n;
217 }
218
219 int lxc_make_tmpfile(char *template, bool rm)
220 {
221 int fd, ret;
222 mode_t msk;
223
224 msk = umask(0022);
225 fd = mkstemp(template);
226 umask(msk);
227 if (fd < 0)
228 return -1;
229
230 if (!rm)
231 return fd;
232
233 ret = unlink(template);
234 if (ret < 0) {
235 close(fd);
236 return -1;
237 }
238
239 return fd;
240 }
241
242 bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
243 {
244 return (fs->f_type == (fs_type_magic)magic_val);
245 }
246
247 bool has_fs_type(const char *path, fs_type_magic magic_val)
248 {
249 int ret;
250 struct statfs sb;
251
252 ret = statfs(path, &sb);
253 if (ret < 0)
254 return false;
255
256 return is_fs_type(&sb, magic_val);
257 }
258
259 bool fhas_fs_type(int fd, fs_type_magic magic_val)
260 {
261 int ret;
262 struct statfs sb;
263
264 ret = fstatfs(fd, &sb);
265 if (ret < 0)
266 return false;
267
268 return is_fs_type(&sb, magic_val);
269 }
270
271 FILE *fopen_cloexec(const char *path, const char *mode)
272 {
273 int open_mode = 0;
274 int step = 0;
275 int fd;
276 int saved_errno = 0;
277 FILE *ret;
278
279 if (!strncmp(mode, "r+", 2)) {
280 open_mode = O_RDWR;
281 step = 2;
282 } else if (!strncmp(mode, "r", 1)) {
283 open_mode = O_RDONLY;
284 step = 1;
285 } else if (!strncmp(mode, "w+", 2)) {
286 open_mode = O_RDWR | O_TRUNC | O_CREAT;
287 step = 2;
288 } else if (!strncmp(mode, "w", 1)) {
289 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
290 step = 1;
291 } else if (!strncmp(mode, "a+", 2)) {
292 open_mode = O_RDWR | O_CREAT | O_APPEND;
293 step = 2;
294 } else if (!strncmp(mode, "a", 1)) {
295 open_mode = O_WRONLY | O_CREAT | O_APPEND;
296 step = 1;
297 }
298 for (; mode[step]; step++)
299 if (mode[step] == 'x')
300 open_mode |= O_EXCL;
301 open_mode |= O_CLOEXEC;
302
303 fd = open(path, open_mode, 0666);
304 if (fd < 0)
305 return NULL;
306
307 ret = fdopen(fd, mode);
308 saved_errno = errno;
309 if (!ret)
310 close(fd);
311 errno = saved_errno;
312 return ret;
313 }