提交 d2ac46f2 编写于 作者: J 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.
上级 3348a97b
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,
......
...@@ -510,40 +510,63 @@ fread_file_lim (FILE *stream, size_t max_len, size_t *length) ...@@ -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)
return -1;
if (len > maxlen || (int)len != len) {
VIR_FREE(s);
/* 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;
}
if (!(fh = fopen(path, "r"))) { 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", virLog("Failed to open file '%s': %s\n",
path, strerror(errno)); path, strerror(errno));
goto error; return -1;
} }
s = fread_file_lim(fh, maxlen+1, &len); int len = virFileReadLimFP (fh, maxlen, buf);
if (s == NULL) { fclose(fh);
if (len < 0) {
virLog("Failed to read '%s': %s\n", path, strerror (errno)); virLog("Failed to read '%s': %s\n", path, strerror (errno));
goto error; 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;
} }
*buf = s; return len;
ret = len;
error:
if (fh)
fclose(fh);
return ret;
} }
int virFileMatchesNameSuffix(const char *file, int virFileMatchesNameSuffix(const char *file,
......
...@@ -45,9 +45,10 @@ int virExec(virConnectPtr conn, ...@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册