From 2b5e0b8bd2f2d33383e81b9fc157d1855de90e23 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Tue, 2 Feb 2021 16:59:14 +0100 Subject: [PATCH] file_utils: add lxc_read_try_buf_at() Signed-off-by: Christian Brauner --- src/lxc/file_utils.c | 26 ++++++++++++++++++++++++++ src/lxc/file_utils.h | 2 ++ 2 files changed, 28 insertions(+) diff --git a/src/lxc/file_utils.c b/src/lxc/file_utils.c index d871190d7..e2a3837f9 100644 --- a/src/lxc/file_utils.c +++ b/src/lxc/file_utils.c @@ -122,6 +122,32 @@ int lxc_read_from_file(const char *filename, void *buf, size_t count) return ret; } +ssize_t lxc_read_try_buf_at(int dfd, const char *path, void *buf, size_t count) +{ + __do_close int fd = -EBADF; + ssize_t ret; + + fd = open_at(dfd, path, PROTECT_OPEN_W, PROTECT_LOOKUP_BENEATH, 0); + if (fd < 0) + return -errno; + + if (!buf || !count) { + char buf2[100]; + size_t count2 = 0; + + while ((ret = lxc_read_nointr(fd, buf2, 100)) > 0) + count2 += ret; + + if (ret >= 0) + ret = count2; + } else { + memset(buf, 0, count); + ret = lxc_read_nointr(fd, buf, count); + } + + return ret; +} + ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) { ssize_t ret; diff --git a/src/lxc/file_utils.h b/src/lxc/file_utils.h index 0250038b3..76a48edb3 100644 --- a/src/lxc/file_utils.h +++ b/src/lxc/file_utils.h @@ -94,5 +94,7 @@ __hidden int fd_make_nonblocking(int fd); __hidden extern char *read_file_at(int dfd, const char *fnam, unsigned int o_flags, unsigned resolve_flags); +__hidden extern ssize_t lxc_read_try_buf_at(int dfd, const char *path, + void *buf, size_t count); #endif /* __LXC_FILE_UTILS_H */ -- 2.47.3