From 91b689a3a469547c77cbbd3c3cc33e5adc85ba75 Mon Sep 17 00:00:00 2001 From: Rodrigo Bruno Date: Sat, 12 Dec 2015 01:18:00 +0300 Subject: [PATCH] Introduce the read_into_buffer helper This will be required for page-cache and page-proxy set. Signed-off-by: Rodrigo Bruno Signed-off-by: Pavel Emelyanov --- include/util.h | 1 + util.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/util.h b/include/util.h index 2af679894..df708c830 100644 --- a/include/util.h +++ b/include/util.h @@ -267,6 +267,7 @@ FILE *fopenat(int dirfd, char *path, char *cflags); void split(char *str, char token, char ***out, int *n); int fd_has_data(int lfd); +size_t read_into_buffer(int fd, char *buff, size_t size); int make_yard(char *path); diff --git a/util.c b/util.c index c8dfaa37b..d56f97339 100644 --- a/util.c +++ b/util.c @@ -872,6 +872,21 @@ int fd_has_data(int lfd) return ret; } +size_t read_into_buffer(int fd, char *buff, size_t size) +{ + size_t n = 0; + size_t curr = 0; + + while (1) { + n = read(fd, buff + curr, size - curr); + if (n < 1) + return n; + curr += n; + if (curr == size) + return size; + } +} + int make_yard(char *path) { if (mount("none", path, "tmpfs", 0, NULL)) {