Skip to content

Commit

Permalink
file_utils: cleanup macros and improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Mar 11, 2020
1 parent 133d960 commit 1dc5160
Showing 1 changed file with 50 additions and 76 deletions.
126 changes: 50 additions & 76 deletions src/lxc/file_utils.c
Expand Up @@ -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,
bool add_newline, mode_t mode)
{
int fd, saved_errno;
__do_close_prot_errno int fd = -EBADF;
ssize_t ret;

fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
Expand All @@ -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);
if (ret < 0)
goto out_error;
return -1;

if ((size_t)ret != count)
goto out_error;
return -1;

if (add_newline) {
ret = lxc_write_nointr(fd, "\n", 1);
if (ret != 1)
goto out_error;
return -1;
}

close(fd);
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 fd = -1, saved_errno;
__do_close_prot_errno int fd = -EBADF;
ssize_t ret;

fd = open(filename, O_RDONLY | O_CLOEXEC);
Expand All @@ -126,52 +119,49 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count)
ret = lxc_read_nointr(fd, buf, count);
}

saved_errno = errno;
close(fd);
errno = saved_errno;
return ret;
}

ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
{
ssize_t ret;
again:
ret = write(fd, buf, count);
if (ret < 0 && errno == EINTR)
goto again;

do {
ret = write(fd, buf, count);
} while (ret < 0 && errno == EINTR);

return ret;
}

ssize_t lxc_send_nointr(int sockfd, void *buf, size_t len, int flags)
{
ssize_t ret;
again:
ret = send(sockfd, buf, len, flags);
if (ret < 0 && errno == EINTR)
goto again;

do {
ret = send(sockfd, buf, len, flags);
} while (ret < 0 && errno == EINTR);

return ret;
}

ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
{
ssize_t ret;
again:
ret = read(fd, buf, count);
if (ret < 0 && errno == EINTR)
goto again;

do {
ret = read(fd, buf, count);
} while (ret < 0 && errno == EINTR);

return ret;
}

ssize_t lxc_recv_nointr(int sockfd, void *buf, size_t len, int flags)
{
ssize_t ret;
again:
ret = recv(sockfd, buf, len, flags);
if (ret < 0 && errno == EINTR)
goto again;

do {
ret = recv(sockfd, buf, len, flags);
} while (ret < 0 && errno == EINTR);

return ret;
}
Expand All @@ -180,21 +170,20 @@ ssize_t lxc_recvmsg_nointr_iov(int sockfd, struct iovec *iov, size_t iovlen,
int flags)
{
ssize_t ret;
struct msghdr msg;
struct msghdr msg = {
.msg_iov = iov,
.msg_iovlen = iovlen,
};

memset(&msg, 0, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = iovlen;

again:
ret = recvmsg(sockfd, &msg, flags);
if (ret < 0 && errno == EINTR)
goto again;
do {
ret = recvmsg(sockfd, &msg, flags);
} while (ret < 0 && errno == EINTR);

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;

Expand All @@ -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)
return -1;

if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
errno = EINVAL;
return -1;
}
if (expected_buf && memcmp(buf, expected_buf, count) != 0)
return ret_set_errno(-1, EINVAL);

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;

Expand All @@ -233,7 +221,7 @@ bool file_exists(const char *f)

int print_to_file(const char *file, const char *content)
{
FILE *f;
__do_fclose FILE *f = NULL;
int ret = 0;

f = fopen(file, "we");
Expand All @@ -243,14 +231,13 @@ int print_to_file(const char *file, const char *content)
if (fprintf(f, "%s", content) != strlen(content))
ret = -1;

fclose(f);
return ret;
}

int is_dir(const char *path)
{
struct stat statbuf;
int ret;
struct stat statbuf;

ret = stat(path, &statbuf);
if (ret == 0 && S_ISDIR(statbuf.st_mode))
Expand All @@ -264,21 +251,18 @@ int is_dir(const char *path)
*/
int lxc_count_file_lines(const char *fn)
{
FILE *f;
char *line = NULL;
__do_free char *line = NULL;
__do_fclose FILE *f = NULL;
size_t sz = 0;
int n = 0;

f = fopen_cloexec(fn, "r");
if (!f)
return -1;

while (getline(&line, &sz, f) != -1) {
while (getline(&line, &sz, f) != -1)
n++;
}

free(line);
fclose(f);
return n;
}

Expand Down Expand Up @@ -338,11 +322,9 @@ bool fhas_fs_type(int fd, fs_type_magic magic_val)

FILE *fopen_cloexec(const char *path, const char *mode)
{
int open_mode = 0;
int step = 0;
int fd;
int saved_errno = 0;
FILE *ret;
__do_close_prot_errno int fd = -EBADF;
int open_mode = 0, step = 0;
FILE *f;

if (!strncmp(mode, "r+", 2)) {
open_mode = O_RDWR;
Expand All @@ -366,32 +348,24 @@ FILE *fopen_cloexec(const char *path, const char *mode)
for (; mode[step]; step++)
if (mode[step] == 'x')
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)
return NULL;

ret = fdopen(fd, mode);
saved_errno = errno;
if (!ret)
close(fd);
errno = saved_errno;
return ret;
f = fdopen(fd, mode);
if (f)
move_fd(fd);
return f;
}

ssize_t lxc_sendfile_nointr(int out_fd, int in_fd, off_t *offset, size_t count)
{
ssize_t ret;

again:
ret = sendfile(out_fd, in_fd, offset, count);
if (ret < 0) {
if (errno == EINTR)
goto again;

return -1;
}
do {
ret = sendfile(out_fd, in_fd, offset, count);
} while (ret < 0 && errno == EINTR);

return ret;
}
Expand Down

0 comments on commit 1dc5160

Please sign in to comment.