summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorreyk <reyk@openbsd.org>2016-01-26 07:58:35 +0000
committerreyk <reyk@openbsd.org>2016-01-26 07:58:35 +0000
commite7d3405c174ea7227d9cc787159ec249c549f2bf (patch)
tree564fb8b3e0b6c9f30ffd90b8fed8b0206b5ce12b
parentThe division "res->size /= 1024 / 1024" is a no-op: 1024 / 1024 is (diff)
downloadwireguard-openbsd-e7d3405c174ea7227d9cc787159ec249c549f2bf.tar.xz
wireguard-openbsd-e7d3405c174ea7227d9cc787159ec249c549f2bf.zip
lseek() + write() can be replaced by a slightly shorter ftruncate()
call. Note that using ftruncate() to extend a file is not portable (POSIX allows either zero-filling until the given size is reached, or alternatively erroring out), but that shouldn't be a proble as vmm(4) isn't cross-platform either. unlink() the image file when extending it fails for consistency with the other error case (the file can't be created). From Martin Natano OK mlarkin@
-rw-r--r--usr.sbin/vmctl/vmctl.c20
1 files changed, 5 insertions, 15 deletions
diff --git a/usr.sbin/vmctl/vmctl.c b/usr.sbin/vmctl/vmctl.c
index b28d2d369ee..7f5e8829b97 100644
--- a/usr.sbin/vmctl/vmctl.c
+++ b/usr.sbin/vmctl/vmctl.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: vmctl.c,v 1.11 2016/01/13 13:08:20 reyk Exp $ */
+/* $OpenBSD: vmctl.c,v 1.12 2016/01/26 07:58:35 reyk Exp $ */
/*
* Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
@@ -390,7 +390,7 @@ vm_console(struct vmop_info_result *list, size_t ct)
*
* Parameters:
* imgfile_path: path to the image file to create
- * imgsize : size of the image file to create
+ * imgsize : size of the image file to create (in MB)
*
* Return:
* EEXIST: The requested image file already exists
@@ -401,8 +401,6 @@ int
create_imagefile(const char *imgfile_path, long imgsize)
{
int fd, ret;
- off_t ofs;
- char ch = '\0';
/* Refuse to overwrite an existing image */
fd = open(imgfile_path, O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
@@ -410,19 +408,11 @@ create_imagefile(const char *imgfile_path, long imgsize)
if (fd == -1)
return (errno);
- ofs = (imgsize * 1024 * 1024) - 1;
-
- /* Set fd pos at desired size */
- if (lseek(fd, ofs, SEEK_SET) == -1) {
- ret = errno;
- close(fd);
- return (ret);
- }
-
- /* Write one byte to fill out the extent */
- if (write(fd, &ch, 1) == -1) {
+ /* Extend to desired size */
+ if (ftruncate(fd, (off_t)imgsize * 1024 * 1024) == -1) {
ret = errno;
close(fd);
+ unlink(imgfile_path);
return (ret);
}