Skip to content

Commit

Permalink
block/vdi: Error out immediately in vdi_create()
Browse files Browse the repository at this point in the history
Currently, if an error occurs during the part of vdi_create() which
actually writes the image, the function stores -errno, but continues
anyway.

Instead of trying to write data which (if it can be written at all) does
not make any sense without the operations before succeeding (e.g.,
writing the image header), just error out immediately.

Signed-off-by: Max Reitz <mreitz@redhat.com>
Reviewed-by: Stefan Weil <sw@weilnetz.de>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
XanClic authored and kevmw committed Apr 30, 2014
1 parent e1b42f4 commit 0549ea8
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion block/vdi.c
Expand Up @@ -756,6 +756,7 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
vdi_header_to_le(&header);
if (write(fd, &header, sizeof(header)) < 0) {
result = -errno;
goto close_and_exit;
}

if (bmap_size > 0) {
Expand All @@ -769,17 +770,21 @@ static int vdi_create(const char *filename, QEMUOptionParameter *options,
}
if (write(fd, bmap, bmap_size) < 0) {
result = -errno;
g_free(bmap);
goto close_and_exit;
}
g_free(bmap);
}

if (image_type == VDI_TYPE_STATIC) {
if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
result = -errno;
goto close_and_exit;
}
}

if (close(fd) < 0) {
close_and_exit:
if ((close(fd) < 0) && !result) {
result = -errno;
}

Expand Down

0 comments on commit 0549ea8

Please sign in to comment.