file_utils: cleanup macros and improvements

parent 133d9608
...@@ -73,7 +73,7 @@ int lxc_write_openat(const char *dir, const char *filename, const void *buf, ...@@ -73,7 +73,7 @@ int lxc_write_openat(const char *dir, const char *filename, const void *buf,
int lxc_write_to_file(const char *filename, const void *buf, size_t count, int lxc_write_to_file(const char *filename, const void *buf, size_t count,
bool add_newline, mode_t mode) bool add_newline, mode_t mode)
{ {
int fd, saved_errno; __do_close_prot_errno int fd = -EBADF;
ssize_t ret; ssize_t ret;
fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
...@@ -82,30 +82,23 @@ int lxc_write_to_file(const char *filename, const void *buf, size_t count, ...@@ -82,30 +82,23 @@ int lxc_write_to_file(const char *filename, const void *buf, size_t count,
ret = lxc_write_nointr(fd, buf, count); ret = lxc_write_nointr(fd, buf, count);
if (ret < 0) if (ret < 0)
goto out_error; return -1;
if ((size_t)ret != count) if ((size_t)ret != count)
goto out_error; return -1;
if (add_newline) { if (add_newline) {
ret = lxc_write_nointr(fd, "\n", 1); ret = lxc_write_nointr(fd, "\n", 1);
if (ret != 1) if (ret != 1)
goto out_error; return -1;
} }
close(fd);
return 0; return 0;
out_error:
saved_errno = errno;
close(fd);
errno = saved_errno;
return -1;
} }
int lxc_read_from_file(const char *filename, void *buf, size_t count) int lxc_read_from_file(const char *filename, void *buf, size_t count)
{ {
int fd = -1, saved_errno; __do_close_prot_errno int fd = -EBADF;
ssize_t ret; ssize_t ret;
fd = open(filename, O_RDONLY | O_CLOEXEC); fd = open(filename, O_RDONLY | O_CLOEXEC);
...@@ -126,19 +119,16 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count) ...@@ -126,19 +119,16 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count)
ret = lxc_read_nointr(fd, buf, count); ret = lxc_read_nointr(fd, buf, count);
} }
saved_errno = errno;
close(fd);
errno = saved_errno;
return ret; return ret;
} }
ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
{ {
ssize_t ret; ssize_t ret;
again:
ret = write(fd, buf, count); do {
if (ret < 0 && errno == EINTR) ret = write(fd, buf, count);
goto again; } while (ret < 0 && errno == EINTR);
return ret; return ret;
} }
...@@ -146,10 +136,10 @@ again: ...@@ -146,10 +136,10 @@ again:
ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags) ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
{ {
ssize_t ret; ssize_t ret;
again:
ret = send(sockfd, buf, len, flags); do {
if (ret < 0 && errno == EINTR) ret = send(sockfd, buf, len, flags);
goto again; } while (ret < 0 && errno == EINTR);
return ret; return ret;
} }
...@@ -157,10 +147,10 @@ again: ...@@ -157,10 +147,10 @@ again:
ssize_t lxc_read_nointr(int fd, void *buf, size_t count) ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
{ {
ssize_t ret; ssize_t ret;
again:
ret = read(fd, buf, count); do {
if (ret < 0 && errno == EINTR) ret = read(fd, buf, count);
goto again; } while (ret < 0 && errno == EINTR);
return ret; return ret;
} }
...@@ -168,10 +158,10 @@ again: ...@@ -168,10 +158,10 @@ again:
ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags) ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
{ {
ssize_t ret; ssize_t ret;
again:
ret = recv(sockfd, buf, len, flags); do {
if (ret < 0 && errno == EINTR) ret = recv(sockfd, buf, len, flags);
goto again; } while (ret < 0 && errno == EINTR);
return ret; return ret;
} }
...@@ -180,21 +170,20 @@ ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen, ...@@ -180,21 +170,20 @@ ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
int flags) int flags)
{ {
ssize_t ret; ssize_t ret;
struct msghdr msg; struct msghdr msg = {
.msg_iov = iov,
.msg_iovlen = iovlen,
};
memset(&msg, 0, sizeof(msg)); do {
msg.msg_iov = iov; ret = recvmsg(sockfd, &msg, flags);
msg.msg_iovlen = iovlen; } while (ret < 0 && errno == EINTR);
again:
ret = recvmsg(sockfd, &msg, flags);
if (ret < 0 && errno == EINTR)
goto again;
return ret; return ret;
} }
ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf) ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count,
const void *expected_buf)
{ {
ssize_t ret; ssize_t ret;
...@@ -205,15 +194,14 @@ ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expe ...@@ -205,15 +194,14 @@ ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expe
if ((size_t)ret != count) if ((size_t)ret != count)
return -1; return -1;
if (expected_buf && memcmp(buf, expected_buf, count) != 0) { if (expected_buf && memcmp(buf, expected_buf, count) != 0)
errno = EINVAL; return ret_set_errno(-1, EINVAL);
return -1;
}
return 0; return 0;
} }
ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count, const void *expected_buf) ssize_t lxc_read_file_expect(const char *path, void *buf, size_t count,
const void *expected_buf)
{ {
__do_close_prot_errno int fd = -EBADF; __do_close_prot_errno int fd = -EBADF;
...@@ -233,7 +221,7 @@ bool file_exists(const char *f) ...@@ -233,7 +221,7 @@ bool file_exists(const char *f)
int print_to_file(const char *file, const char *content) int print_to_file(const char *file, const char *content)
{ {
FILE *f; __do_fclose FILE *f = NULL;
int ret = 0; int ret = 0;
f = fopen(file, "we"); f = fopen(file, "we");
...@@ -243,14 +231,13 @@ int print_to_file(const char *file, const char *content) ...@@ -243,14 +231,13 @@ int print_to_file(const char *file, const char *content)
if (fprintf(f, "%s", content) != strlen(content)) if (fprintf(f, "%s", content) != strlen(content))
ret = -1; ret = -1;
fclose(f);
return ret; return ret;
} }
int is_dir(const char *path) int is_dir(const char *path)
{ {
struct stat statbuf;
int ret; int ret;
struct stat statbuf;
ret = stat(path, &statbuf); ret = stat(path, &statbuf);
if (ret == 0 && S_ISDIR(statbuf.st_mode)) if (ret == 0 && S_ISDIR(statbuf.st_mode))
...@@ -264,8 +251,8 @@ int is_dir(const char *path) ...@@ -264,8 +251,8 @@ int is_dir(const char *path)
*/ */
int lxc_count_file_lines(const char *fn) int lxc_count_file_lines(const char *fn)
{ {
FILE *f; __do_free char *line = NULL;
char *line = NULL; __do_fclose FILE *f = NULL;
size_t sz = 0; size_t sz = 0;
int n = 0; int n = 0;
...@@ -273,12 +260,9 @@ int lxc_count_file_lines(const char *fn) ...@@ -273,12 +260,9 @@ int lxc_count_file_lines(const char *fn)
if (!f) if (!f)
return -1; return -1;
while (getline(&line, &sz, f) != -1) { while (getline(&line, &sz, f) != -1)
n++; n++;
}
free(line);
fclose(f);
return n; return n;
} }
...@@ -338,11 +322,9 @@ bool fhas_fs_type(int fd, fs_type_magic magic_val) ...@@ -338,11 +322,9 @@ bool fhas_fs_type(int fd, fs_type_magic magic_val)
FILE *fopen_cloexec(const char *path, const char *mode) FILE *fopen_cloexec(const char *path, const char *mode)
{ {
int open_mode = 0; __do_close_prot_errno int fd = -EBADF;
int step = 0; int open_mode = 0, step = 0;
int fd; FILE *f;
int saved_errno = 0;
FILE *ret;
if (!strncmp(mode, "r+", 2)) { if (!strncmp(mode, "r+", 2)) {
open_mode = O_RDWR; open_mode = O_RDWR;
...@@ -366,32 +348,24 @@ FILE *fopen_cloexec(const char *path, const char *mode) ...@@ -366,32 +348,24 @@ FILE *fopen_cloexec(const char *path, const char *mode)
for (; mode[step]; step++) for (; mode[step]; step++)
if (mode[step] == 'x') if (mode[step] == 'x')
open_mode |= O_EXCL; open_mode |= O_EXCL;
open_mode |= O_CLOEXEC;
fd = open(path, open_mode, 0660); fd = open(path, open_mode | O_CLOEXEC, 0660);
if (fd < 0) if (fd < 0)
return NULL; return NULL;
ret = fdopen(fd, mode); f = fdopen(fd, mode);
saved_errno = errno; if (f)
if (!ret) move_fd(fd);
close(fd); return f;
errno = saved_errno;
return ret;
} }
ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count) ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
{ {
ssize_t ret; ssize_t ret;
again: do {
ret = sendfile(out_fd, in_fd, offset, count); ret = sendfile(out_fd, in_fd, offset, count);
if (ret < 0) { } while (ret < 0 && errno == EINTR);
if (errno == EINTR)
goto again;
return -1;
}
return ret; return ret;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment