From d2ac46f2b08a6d0af1b47ea8d43b0c8eaf01a226 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Tue, 2 Sep 2008 10:29:51 +0000 Subject: [PATCH] util.c: add a file-descriptor-based wrapper for fread_file_lim * src/util.c (virFileReadLimFP): New function. (__virFileReadLimFD): New function. * src/util.h (__virFileReadLimFD): Declare. (virFileReadLimFD): Define. (virFileReadAll): Rewrite to use virFileReadLimFP. --- ChangeLog | 9 +++++++ src/util.c | 77 +++++++++++++++++++++++++++++++++++------------------- src/util.h | 7 ++--- 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/ChangeLog b/ChangeLog index fbdf485521..b21088840d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Tue Sep 2 12:28:54 CEST 2008 Jim Meyering + + util.c: add a file-descriptor-based wrapper for fread_file_lim + * src/util.c (virFileReadLimFP): New function. + (__virFileReadLimFD): New function. + * src/util.h (__virFileReadLimFD): Declare. + (virFileReadLimFD): Define. + (virFileReadAll): Rewrite to use virFileReadLimFP. + Fri Aug 29 08:04:15 BST 2008 Daniel P. Berrange * src/bridge.c, src/proxy_internal.c, src/qemu_conf.c, diff --git a/src/util.c b/src/util.c index cb03e7bbb8..c7242686f4 100644 --- a/src/util.c +++ b/src/util.c @@ -510,40 +510,63 @@ fread_file_lim (FILE *stream, size_t max_len, size_t *length) return NULL; } -int __virFileReadAll(const char *path, int maxlen, char **buf) +/* A wrapper around fread_file_lim that maps a failure due to + exceeding the maximum size limitation to EOVERFLOW. */ +static int virFileReadLimFP(FILE *fp, int maxlen, char **buf) { - FILE *fh; - int ret = -1; size_t len; - char *s; - - if (!(fh = fopen(path, "r"))) { - virLog("Failed to open file '%s': %s\n", - path, strerror(errno)); - goto error; - } - - s = fread_file_lim(fh, maxlen+1, &len); - if (s == NULL) { - virLog("Failed to read '%s': %s\n", path, strerror (errno)); - goto error; - } - + char *s = fread_file_lim (fp, maxlen+1, &len); + if (s == NULL) + return -1; if (len > maxlen || (int)len != len) { VIR_FREE(s); - virLog("File '%s' is too large %d, max %d\n", - path, (int)len, maxlen); - goto error; + /* There was at least one byte more than MAXLEN. + Set errno accordingly. */ + errno = EOVERFLOW; + return -1; + } + *buf = s; + return len; +} + +/* Like virFileReadLimFP, but use a file descriptor rather than a FILE*. */ +int __virFileReadLimFD(int fd_arg, int maxlen, char **buf) +{ + int fd = dup (fd_arg); + if (fd >= 0) { + FILE *fp = fdopen (fd, "r"); + if (fp) { + int len = virFileReadLimFP (fp, maxlen, buf); + int saved_errno = errno; + fclose (fp); + errno = saved_errno; + return len; + } else { + int saved_errno = errno; + close (fd); + errno = saved_errno; + } + } + return -1; +} + +int __virFileReadAll(const char *path, int maxlen, char **buf) +{ + FILE *fh = fopen(path, "r"); + if (fh == NULL) { + virLog("Failed to open file '%s': %s\n", + path, strerror(errno)); + return -1; } - *buf = s; - ret = len; + int len = virFileReadLimFP (fh, maxlen, buf); + fclose(fh); + if (len < 0) { + virLog("Failed to read '%s': %s\n", path, strerror (errno)); + return -1; + } - error: - if (fh) - fclose(fh); - - return ret; + return len; } int virFileMatchesNameSuffix(const char *file, diff --git a/src/util.h b/src/util.h index 51515821f9..f2da006772 100644 --- a/src/util.h +++ b/src/util.h @@ -45,9 +45,10 @@ int virExec(virConnectPtr conn, int flags); int virRun(virConnectPtr conn, const char *const*argv, int *status); -int __virFileReadAll(const char *path, - int maxlen, - char **buf); +int __virFileReadLimFD(int fd, int maxlen, char **buf); +#define virFileReadLimFD(fd,m,b) __virFileReadLimFD((fd),(m),(b)) + +int __virFileReadAll(const char *path, int maxlen, char **buf); #define virFileReadAll(p,m,b) __virFileReadAll((p),(m),(b)) int virFileMatchesNameSuffix(const char *file,