Commit 51d7c00c14550334ec140ce8f40e04ed4c88de57

Authored by aliguori
1 parent a80bf99f

block: Polish error handling of brdv_open2 (Jan Kiszka)

Make sure that we always delete temporary disk images on error, remove
obsolete malloc error checks and return proper error codes.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6702 c046a42c-6fe2-441c-8c8c-71466251a162
Showing 1 changed file with 21 additions and 25 deletions
... ... @@ -311,8 +311,6 @@ int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
311 311 int ret;
312 312  
313 313 bs = bdrv_new("");
314   - if (!bs)
315   - return -ENOMEM;
316 314 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
317 315 if (ret < 0) {
318 316 bdrv_delete(bs);
... ... @@ -349,12 +347,10 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
349 347  
350 348 /* if there is a backing file, use it */
351 349 bs1 = bdrv_new("");
352   - if (!bs1) {
353   - return -ENOMEM;
354   - }
355   - if (bdrv_open(bs1, filename, 0) < 0) {
  350 + ret = bdrv_open(bs1, filename, 0);
  351 + if (ret < 0) {
356 352 bdrv_delete(bs1);
357   - return -1;
  353 + return ret;
358 354 }
359 355 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
360 356  
... ... @@ -372,9 +368,10 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
372 368 else
373 369 realpath(filename, backing_filename);
374 370  
375   - if (bdrv_create(&bdrv_qcow2, tmp_filename,
376   - total_size, backing_filename, 0) < 0) {
377   - return -1;
  371 + ret = bdrv_create(&bdrv_qcow2, tmp_filename,
  372 + total_size, backing_filename, 0);
  373 + if (ret < 0) {
  374 + return ret;
378 375 }
379 376 filename = tmp_filename;
380 377 bs->is_temporary = 1;
... ... @@ -383,14 +380,12 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
383 380 pstrcpy(bs->filename, sizeof(bs->filename), filename);
384 381 if (flags & BDRV_O_FILE) {
385 382 drv = find_protocol(filename);
386   - if (!drv)
387   - return -ENOENT;
388   - } else {
389   - if (!drv) {
390   - drv = find_image_format(filename);
391   - if (!drv)
392   - return -1;
393   - }
  383 + } else if (!drv) {
  384 + drv = find_image_format(filename);
  385 + }
  386 + if (!drv) {
  387 + ret = -ENOENT;
  388 + goto unlink_and_fail;
394 389 }
395 390 bs->drv = drv;
396 391 bs->opaque = qemu_mallocz(drv->instance_size);
... ... @@ -409,6 +404,9 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
409 404 qemu_free(bs->opaque);
410 405 bs->opaque = NULL;
411 406 bs->drv = NULL;
  407 + unlink_and_fail:
  408 + if (bs->is_temporary)
  409 + unlink(filename);
412 410 return ret;
413 411 }
414 412 if (drv->bdrv_getlength) {
... ... @@ -422,15 +420,13 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
422 420 if (bs->backing_file[0] != '\0') {
423 421 /* if there is a backing file, use it */
424 422 bs->backing_hd = bdrv_new("");
425   - if (!bs->backing_hd) {
426   - fail:
427   - bdrv_close(bs);
428   - return -ENOMEM;
429   - }
430 423 path_combine(backing_filename, sizeof(backing_filename),
431 424 filename, bs->backing_file);
432   - if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0)
433   - goto fail;
  425 + ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
  426 + if (ret < 0) {
  427 + bdrv_close(bs);
  428 + return ret;
  429 + }
434 430 }
435 431  
436 432 /* call the change callback */
... ...