From bc6bb3a3e8c6cad0bc9cb72f3e549eeff1de2f99 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Wed, 8 Feb 2012 14:03:29 +0000 Subject: [PATCH] Replace truncate() with ftruncate() Mingw32 does not have any truncate() API defined, but it does have ftruncate(). So replace use of the former with the latter --- src/util/storage_file.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/util/storage_file.c b/src/util/storage_file.c index 8260adbd1f..a8661e3b79 100644 --- a/src/util/storage_file.c +++ b/src/util/storage_file.c @@ -939,12 +939,29 @@ virStorageFileFreeMetadata(virStorageFileMetadata *meta) int virStorageFileResize(const char *path, unsigned long long capacity) { - if (truncate(path, capacity) < 0) { - virReportSystemError(errno, _("Failed to truncate file '%s'"), path); - return -1; + int fd = -1; + int ret = -1; + + if ((fd = open(path, O_RDWR)) < 0) { + virReportSystemError(errno, _("Unable to open '%s'"), path); + goto cleanup; } - return 0; + if (ftruncate(fd, capacity) < 0) { + virReportSystemError(errno, _("Failed to truncate file '%s'"), path); + goto cleanup; + } + + if (VIR_CLOSE(fd) < 0) { + virReportSystemError(errno, _("Unable to save '%s'"), path); + goto cleanup; + } + + ret = 0; + +cleanup: + VIR_FORCE_CLOSE(fd); + return ret; } #ifdef __linux__