|
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Xen-devel] [PATCH 10/13 v3] libxl: don't try to fclose file twice on error in libxl_userdata_store
Do this by changing the function to not use stdio file operations, but
just use the fd directly with libxl_write_exactly.
While at it, tidy up the function's style issues.
Coverity-ID: 1056195
Signed-off-by: Matthew Daley <mattd@xxxxxxxxxxx>
---
v3: Fix the commit message
v2: Use libxl_write_exactly instead of write to ensure all the userdata is
written out
tools/libxl/libxl_dom.c | 35 ++++++++++++++---------------------
1 file changed, 14 insertions(+), 21 deletions(-)
diff --git a/tools/libxl/libxl_dom.c b/tools/libxl/libxl_dom.c
index faba32f..55f74b2 100644
--- a/tools/libxl/libxl_dom.c
+++ b/tools/libxl/libxl_dom.c
@@ -1603,8 +1603,6 @@ int libxl_userdata_store(libxl_ctx *ctx, uint32_t domid,
const char *newfilename;
int e, rc;
int fd = -1;
- FILE *f = NULL;
- size_t rs;
filename = userdata_path(gc, domid, userdata_userid, "d");
if (!filename) {
@@ -1625,38 +1623,33 @@ int libxl_userdata_store(libxl_ctx *ctx, uint32_t domid,
rc = ERROR_FAIL;
- fd= open(newfilename, O_RDWR|O_CREAT|O_TRUNC, 0600);
- if (fd<0)
+ fd = open(newfilename, O_RDWR | O_CREAT | O_TRUNC, 0600);
+ if (fd < 0)
goto err;
- f= fdopen(fd, "wb");
- if (!f)
+ if (libxl_write_exactly(ctx, fd, data, datalen, "userdata", newfilename))
goto err;
- fd = -1;
- rs = fwrite(data, 1, datalen, f);
- if (rs != datalen) {
- assert(ferror(f));
+ if (close(fd) < 0) {
+ fd = -1;
goto err;
}
+ fd = -1;
- if (fclose(f))
- goto err;
- f = 0;
-
- if (rename(newfilename,filename))
+ if (rename(newfilename, filename))
goto err;
rc = 0;
err:
- e = errno;
- if (f) fclose(f);
- if (fd>=0) close(fd);
+ if (fd >= 0) {
+ e = errno;
+ close(fd);
+ errno = e;
+ }
- errno = e;
- if ( rc )
- LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot write %s for %s",
+ if (rc)
+ LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "cannot write/rename %s for
%s",
newfilename, filename);
out:
GC_FREE;
--
1.7.10.4
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxx
http://lists.xen.org/xen-devel
|
![]() |
Lists.xenproject.org is hosted with RackSpace, monitoring our |