[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 14/31] block: Unlink temporary files in raw-posix/win
From: |
Kevin Wolf |
Subject: |
[Qemu-devel] [PULL 14/31] block: Unlink temporary files in raw-posix/win32 |
Date: |
Wed, 30 Apr 2014 20:23:46 +0200 |
Instead of having unlink() calls in the generic block layer, where we
aren't even guarateed to have a file name, move them to those block
drivers that are actually used and that always have a filename. Gets us
rid of some #ifdefs as well.
The patch also converts bs->is_temporary to a new BDRV_O_TEMPORARY open
flag so that it is inherited in the protocol layer and the raw-posix and
raw-win32 drivers can unlink the file.
Signed-off-by: Kevin Wolf <address@hidden>
Reviewed-by: Max Reitz <address@hidden>
---
block.c | 36 ++++++++++--------------------------
block/raw-posix.c | 5 ++++-
block/raw-win32.c | 3 +++
include/block/block.h | 1 +
include/block/block_int.h | 1 -
5 files changed, 18 insertions(+), 28 deletions(-)
diff --git a/block.c b/block.c
index 9f88bcc..c094075 100644
--- a/block.c
+++ b/block.c
@@ -808,7 +808,7 @@ static int bdrv_backing_flags(int flags)
flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ);
/* snapshot=on is handled on the top layer */
- flags &= ~BDRV_O_SNAPSHOT;
+ flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY);
return flags;
}
@@ -831,7 +831,7 @@ static int bdrv_open_flags(BlockDriverState *bs, int flags)
/*
* Snapshots should be writable.
*/
- if (bs->is_temporary) {
+ if (flags & BDRV_O_TEMPORARY) {
open_flags |= BDRV_O_RDWR;
}
@@ -990,13 +990,6 @@ static int bdrv_open_common(BlockDriverState *bs,
BlockDriverState *file,
bdrv_refresh_limits(bs);
assert(bdrv_opt_mem_align(bs) != 0);
assert((bs->request_alignment != 0) || bs->sg);
-
-#ifndef _WIN32
- if (bs->is_temporary) {
- assert(bs->filename[0] != '\0');
- unlink(bs->filename);
- }
-#endif
return 0;
free_and_fail:
@@ -1267,10 +1260,10 @@ void bdrv_append_temp_snapshot(BlockDriverState *bs,
Error **errp)
qstring_from_str(tmp_filename));
bs_snapshot = bdrv_new("", &error_abort);
- bs_snapshot->is_temporary = 1;
ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options,
- bs->open_flags & ~BDRV_O_SNAPSHOT, bdrv_qcow2, &local_err);
+ (bs->open_flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY,
+ bdrv_qcow2, &local_err);
if (ret < 0) {
error_propagate(errp, local_err);
goto out;
@@ -1371,7 +1364,7 @@ int bdrv_open(BlockDriverState **pbs, const char
*filename,
bdrv_inherited_flags(flags),
true, &local_err);
if (ret < 0) {
- goto unlink_and_fail;
+ goto fail;
}
/* Find the right image format driver */
@@ -1382,7 +1375,7 @@ int bdrv_open(BlockDriverState **pbs, const char
*filename,
if (!drv) {
error_setg(errp, "Invalid driver: '%s'", drvname);
ret = -EINVAL;
- goto unlink_and_fail;
+ goto fail;
}
}
@@ -1392,18 +1385,18 @@ int bdrv_open(BlockDriverState **pbs, const char
*filename,
} else {
error_setg(errp, "Must specify either driver or file");
ret = -EINVAL;
- goto unlink_and_fail;
+ goto fail;
}
}
if (!drv) {
- goto unlink_and_fail;
+ goto fail;
}
/* Open the image */
ret = bdrv_open_common(bs, file, options, flags, drv, &local_err);
if (ret < 0) {
- goto unlink_and_fail;
+ goto fail;
}
if (file && (bs->file != file)) {
@@ -1465,14 +1458,10 @@ done:
*pbs = bs;
return 0;
-unlink_and_fail:
+fail:
if (file != NULL) {
bdrv_unref(file);
}
- if (bs->is_temporary) {
- unlink(filename);
- }
-fail:
QDECREF(bs->options);
QDECREF(options);
bs->options = NULL;
@@ -1752,11 +1741,6 @@ void bdrv_close(BlockDriverState *bs)
}
bs->drv->bdrv_close(bs);
g_free(bs->opaque);
-#ifdef _WIN32
- if (bs->is_temporary) {
- unlink(bs->filename);
- }
-#endif
bs->opaque = NULL;
bs->drv = NULL;
bs->copy_on_read = 0;
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 1688e16..3ce026d 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -366,7 +366,7 @@ static int raw_open_common(BlockDriverState *bs, QDict
*options,
BDRVRawState *s = bs->opaque;
QemuOpts *opts;
Error *local_err = NULL;
- const char *filename;
+ const char *filename = NULL;
int fd, ret;
struct stat st;
@@ -446,6 +446,9 @@ static int raw_open_common(BlockDriverState *bs, QDict
*options,
ret = 0;
fail:
+ if (filename && (bdrv_flags & BDRV_O_TEMPORARY)) {
+ unlink(filename);
+ }
qemu_opts_del(opts);
return ret;
}
diff --git a/block/raw-win32.c b/block/raw-win32.c
index 48cb2c2..064ea31 100644
--- a/block/raw-win32.c
+++ b/block/raw-win32.c
@@ -390,6 +390,9 @@ static void raw_close(BlockDriverState *bs)
{
BDRVRawState *s = bs->opaque;
CloseHandle(s->hfile);
+ if (bs->open_flags & BDRV_O_TEMPORARY) {
+ unlink(bs->filename);
+ }
}
static int raw_truncate(BlockDriverState *bs, int64_t offset)
diff --git a/include/block/block.h b/include/block/block.h
index c12808a..467fb2b 100644
--- a/include/block/block.h
+++ b/include/block/block.h
@@ -92,6 +92,7 @@ typedef enum {
#define BDRV_O_RDWR 0x0002
#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes
in a snapshot */
+#define BDRV_O_TEMPORARY 0x0010 /* delete the file after use */
#define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */
#define BDRV_O_CACHE_WB 0x0040 /* use write-back caching */
#define BDRV_O_NATIVE_AIO 0x0080 /* use native AIO instead of the thread pool
*/
diff --git a/include/block/block_int.h b/include/block/block_int.h
index cd5bc73..9ffcb69 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -299,7 +299,6 @@ struct BlockDriverState {
char backing_file[1024]; /* if non zero, the image is a diff of
this file image */
char backing_format[16]; /* if non-zero and backing_file exists */
- int is_temporary;
BlockDriverState *backing_hd;
BlockDriverState *file;
--
1.8.3.1
- [Qemu-devel] [PULL 04/31] block: Ignore duplicate or NULL format_name in bdrv_iterate_format, (continued)
- [Qemu-devel] [PULL 04/31] block: Ignore duplicate or NULL format_name in bdrv_iterate_format, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 07/31] block: qemu-iotests - fix image cleanup when using spaced pathnames, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 06/31] mirror: Check for bdrv_get_info result, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 08/31] block: qemu-iotests: make test 019 and 086 work with spaced pathnames, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 05/31] mirror: Fix resource leak when bdrv_getlength fails, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 10/31] iotests: Discarding compressed clusters on qcow2, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 09/31] qcow2: Fix discard, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 11/31] block: Create bdrv_inherited_flags(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 12/31] block: Create bdrv_backing_flags(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 13/31] block: Remove BDRV_O_COPY_ON_READ for bs->file, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 14/31] block: Unlink temporary files in raw-posix/win32,
Kevin Wolf <=
- [Qemu-devel] [PULL 15/31] Revert "block: another bdrv_append fix", Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 16/31] block: Fix open_flags in bdrv_reopen(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 17/31] block: Use error_abort in bdrv_image_info_specific_dump(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 19/31] block: Use correct width in format strings, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 18/31] qcow2: Avoid overflow in alloc_clusters_noref(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 20/31] qcow2: Catch bdrv_getlength() error, Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 23/31] block/vdi: Error out immediately in vdi_create(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 22/31] block/bochs: Fix error handling for seek_to_sector(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 21/31] qcow2: Check min_size in qcow2_grow_l1_table(), Kevin Wolf, 2014/04/30
- [Qemu-devel] [PULL 24/31] curl: Fix long line, Kevin Wolf, 2014/04/30