From mboxrd@z Thu Jan 1 00:00:00 1970 From: Matthew Daley Subject: [PATCH 10/13 v2] libxl: don't try to fclose file twice on error in libxl_userdata_store Date: Tue, 3 Dec 2013 12:56:58 +1300 Message-ID: <1386028618-6316-1-git-send-email-mattd@bugfuzz.com> References: <21148.41345.663155.201835@mariner.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <21148.41345.663155.201835@mariner.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: xen-devel@lists.xen.org Cc: Andrew Cooper , Matthew Daley , Ian Jackson , Ian Campbell , Stefano Stabellini List-Id: xen-devel@lists.xenproject.org Do this by changing the function to not use stdio file operations, but just use write(2) directly. While at it, tidy up the function's style issues. Coverity-ID: 1056195 Signed-off-by: Matthew Daley --- 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