]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/file_utils.c
3 * Copyright © 2018 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2018 Canonical Ltd.
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.
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.
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.
25 #include <linux/magic.h>
28 #include <sys/types.h>
31 #include "file_utils.h"
36 lxc_log_define(file_utils
, lxc
);
38 int lxc_write_to_file(const char *filename
, const void *buf
, size_t count
,
39 bool add_newline
, mode_t mode
)
44 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
| O_CLOEXEC
, mode
);
48 ret
= lxc_write_nointr(fd
, buf
, count
);
52 if ((size_t)ret
!= count
)
56 ret
= lxc_write_nointr(fd
, "\n", 1);
71 int lxc_read_from_file(const char *filename
, void *buf
, size_t count
)
73 int fd
= -1, saved_errno
;
76 fd
= open(filename
, O_RDONLY
| O_CLOEXEC
);
84 while ((ret
= lxc_read_nointr(fd
, buf2
, 100)) > 0)
90 memset(buf
, 0, count
);
91 ret
= lxc_read_nointr(fd
, buf
, count
);
100 ssize_t
lxc_write_nointr(int fd
, const void *buf
, size_t count
)
104 ret
= write(fd
, buf
, count
);
105 if (ret
< 0 && errno
== EINTR
)
111 ssize_t
lxc_send_nointr(int sockfd
, void *buf
, size_t len
, int flags
)
115 ret
= send(sockfd
, buf
, len
, flags
);
116 if (ret
< 0 && errno
== EINTR
)
122 ssize_t
lxc_read_nointr(int fd
, void *buf
, size_t count
)
126 ret
= read(fd
, buf
, count
);
127 if (ret
< 0 && errno
== EINTR
)
133 ssize_t
lxc_recv_nointr(int sockfd
, void *buf
, size_t len
, int flags
)
137 ret
= recv(sockfd
, buf
, len
, flags
);
138 if (ret
< 0 && errno
== EINTR
)
144 ssize_t
lxc_read_nointr_expect(int fd
, void *buf
, size_t count
, const void *expected_buf
)
148 ret
= lxc_read_nointr(fd
, buf
, count
);
152 if ((size_t)ret
!= count
)
155 if (expected_buf
&& memcmp(buf
, expected_buf
, count
) != 0) {
163 bool file_exists(const char *f
)
167 return stat(f
, &statbuf
) == 0;
170 int print_to_file(const char *file
, const char *content
)
175 f
= fopen(file
, "w");
179 if (fprintf(f
, "%s", content
) != strlen(content
))
186 int is_dir(const char *path
)
191 ret
= stat(path
, &statbuf
);
192 if (ret
== 0 && S_ISDIR(statbuf
.st_mode
))
199 * Return the number of lines in file @fn, or -1 on error
201 int lxc_count_file_lines(const char *fn
)
208 f
= fopen_cloexec(fn
, "r");
212 while (getline(&line
, &sz
, f
) != -1) {
221 int lxc_make_tmpfile(char *template, bool rm
)
227 fd
= mkstemp(template);
235 ret
= unlink(template);
244 bool is_fs_type(const struct statfs
*fs
, fs_type_magic magic_val
)
246 return (fs
->f_type
== (fs_type_magic
)magic_val
);
249 bool has_fs_type(const char *path
, fs_type_magic magic_val
)
254 ret
= statfs(path
, &sb
);
258 return is_fs_type(&sb
, magic_val
);
261 bool fhas_fs_type(int fd
, fs_type_magic magic_val
)
266 ret
= fstatfs(fd
, &sb
);
270 return is_fs_type(&sb
, magic_val
);
273 FILE *fopen_cloexec(const char *path
, const char *mode
)
281 if (!strncmp(mode
, "r+", 2)) {
284 } else if (!strncmp(mode
, "r", 1)) {
285 open_mode
= O_RDONLY
;
287 } else if (!strncmp(mode
, "w+", 2)) {
288 open_mode
= O_RDWR
| O_TRUNC
| O_CREAT
;
290 } else if (!strncmp(mode
, "w", 1)) {
291 open_mode
= O_WRONLY
| O_TRUNC
| O_CREAT
;
293 } else if (!strncmp(mode
, "a+", 2)) {
294 open_mode
= O_RDWR
| O_CREAT
| O_APPEND
;
296 } else if (!strncmp(mode
, "a", 1)) {
297 open_mode
= O_WRONLY
| O_CREAT
| O_APPEND
;
300 for (; mode
[step
]; step
++)
301 if (mode
[step
] == 'x')
303 open_mode
|= O_CLOEXEC
;
305 fd
= open(path
, open_mode
, 0660);
309 ret
= fdopen(fd
, mode
);