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.
This commit is contained in:
parent
3348a97bd3
commit
d2ac46f2b0
@ -1,3 +1,12 @@
|
|||||||
|
Tue Sep 2 12:28:54 CEST 2008 Jim Meyering <meyering@redhat.com>
|
||||||
|
|
||||||
|
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 <berrange@redhat.com>
|
Fri Aug 29 08:04:15 BST 2008 Daniel P. Berrange <berrange@redhat.com>
|
||||||
|
|
||||||
* src/bridge.c, src/proxy_internal.c, src/qemu_conf.c,
|
* src/bridge.c, src/proxy_internal.c, src/qemu_conf.c,
|
||||||
|
77
src/util.c
77
src/util.c
@ -510,40 +510,63 @@ fread_file_lim (FILE *stream, size_t max_len, size_t *length)
|
|||||||
return NULL;
|
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;
|
size_t len;
|
||||||
char *s;
|
char *s = fread_file_lim (fp, maxlen+1, &len);
|
||||||
|
if (s == NULL)
|
||||||
if (!(fh = fopen(path, "r"))) {
|
return -1;
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len > maxlen || (int)len != len) {
|
if (len > maxlen || (int)len != len) {
|
||||||
VIR_FREE(s);
|
VIR_FREE(s);
|
||||||
virLog("File '%s' is too large %d, max %d\n",
|
/* There was at least one byte more than MAXLEN.
|
||||||
path, (int)len, maxlen);
|
Set errno accordingly. */
|
||||||
goto error;
|
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;
|
int len = virFileReadLimFP (fh, maxlen, buf);
|
||||||
ret = len;
|
fclose(fh);
|
||||||
|
if (len < 0) {
|
||||||
|
virLog("Failed to read '%s': %s\n", path, strerror (errno));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
error:
|
return len;
|
||||||
if (fh)
|
|
||||||
fclose(fh);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int virFileMatchesNameSuffix(const char *file,
|
int virFileMatchesNameSuffix(const char *file,
|
||||||
|
@ -45,9 +45,10 @@ int virExec(virConnectPtr conn,
|
|||||||
int flags);
|
int flags);
|
||||||
int virRun(virConnectPtr conn, const char *const*argv, int *status);
|
int virRun(virConnectPtr conn, const char *const*argv, int *status);
|
||||||
|
|
||||||
int __virFileReadAll(const char *path,
|
int __virFileReadLimFD(int fd, int maxlen, char **buf);
|
||||||
int maxlen,
|
#define virFileReadLimFD(fd,m,b) __virFileReadLimFD((fd),(m),(b))
|
||||||
char **buf);
|
|
||||||
|
int __virFileReadAll(const char *path, int maxlen, char **buf);
|
||||||
#define virFileReadAll(p,m,b) __virFileReadAll((p),(m),(b))
|
#define virFileReadAll(p,m,b) __virFileReadAll((p),(m),(b))
|
||||||
|
|
||||||
int virFileMatchesNameSuffix(const char *file,
|
int virFileMatchesNameSuffix(const char *file,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user