]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
file_utils: cleanup macros and improvements
authorChristian Brauner <christian.brauner@ubuntu.com>
Wed, 11 Mar 2020 17:56:54 +0000 (18:56 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Wed, 11 Mar 2020 18:06:19 +0000 (19:06 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/file_utils.c

index ad970577294981ff652320da7bf6b362840fef26..747e5c18cf31b1f12a2c3bd198e4d61f9394cc23 100644 (file)
@@ -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);
@@ -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);
@@ -126,19 +119,16 @@ 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;
 }
@@ -146,10 +136,10 @@ again:
 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;
 }
@@ -157,10 +147,10 @@ again:
 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;
 }
@@ -168,10 +158,10 @@ again:
 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;
 }
@@ -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;
 
@@ -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;
 
@@ -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");
@@ -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))
@@ -264,8 +251,8 @@ 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;
 
@@ -273,12 +260,9 @@ int lxc_count_file_lines(const char *fn)
        if (!f)
                return -1;
 
-       while (getline(&line, &sz, f) != -1) {
+       while (getline(&line, &sz, f) != -1)
                n++;
-       }
 
-       free(line);
-       fclose(f);
        return n;
 }
 
@@ -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;
@@ -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;
 }