From cbcd340c67c914e38342471c8a1a0aba984c15ff Mon Sep 17 00:00:00 2001 From: Vishvananda Ishaya Date: Thu, 20 Dec 2012 13:43:09 -0800 Subject: [PATCH] Make sure we don't double remove tmp on exception The fetch_to_raw code would fail if an exception is thrown during fetch because it would attempt to delete the same file twice. Fix this by using mkstemp and our wrapper which only deletes the tmp file if it exists. Change-Id: I7bb3171d3c7dc023fc743578c2ce6e804bbc49f5 --- cinder/image/image_utils.py | 62 ++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/cinder/image/image_utils.py b/cinder/image/image_utils.py index e38c59a64..8f1251f17 100644 --- a/cinder/image/image_utils.py +++ b/cinder/image/image_utils.py @@ -208,34 +208,34 @@ def fetch_to_raw(context, image_service, os.path.exists(FLAGS.image_conversion_dir)): os.makedirs(FLAGS.image_conversion_dir) - with tempfile.NamedTemporaryFile(dir=FLAGS.image_conversion_dir) as tmp: - fetch(context, image_service, image_id, tmp.name, user_id, project_id) - tmp.flush() - - data = qemu_img_info(tmp.name) - with utils.remove_path_on_error(tmp.name): - fmt = data.file_format - if fmt is None: - raise exception.ImageUnacceptable( - reason=_("'qemu-img info' parsing failed."), - image_id=image_id) - - backing_file = data.backing_file - if backing_file is not None: - raise exception.ImageUnacceptable( - image_id=image_id, - reason=_("fmt=%(fmt)s backed by:" - "%(backing_file)s") % locals()) - - # NOTE(jdg): I'm using qemu-img convert to write - # to the volume regardless if it *needs* conversion or not - LOG.debug("%s was %s, converting to raw" % (image_id, fmt)) - convert_image(tmp.name, dest, 'raw') - tmp.close() - - data = qemu_img_info(dest) - if data.file_format != "raw": - raise exception.ImageUnacceptable( - image_id=image_id, - reason=_("Converted to raw, but format is now %s") % - data.file_format) + fd, tmp = tempfile.mkstemp(dir=FLAGS.image_conversion_dir) + fd.close() + with utils.remove_path_on_error(tmp): + fetch(context, image_service, image_id, tmp, user_id, project_id) + + data = qemu_img_info(tmp) + fmt = data.file_format + if fmt is None: + raise exception.ImageUnacceptable( + reason=_("'qemu-img info' parsing failed."), + image_id=image_id) + + backing_file = data.backing_file + if backing_file is not None: + raise exception.ImageUnacceptable( + image_id=image_id, + reason=_("fmt=%(fmt)s backed by:" + "%(backing_file)s") % locals()) + + # NOTE(jdg): I'm using qemu-img convert to write + # to the volume regardless if it *needs* conversion or not + LOG.debug("%s was %s, converting to raw" % (image_id, fmt)) + convert_image(tmp, dest, 'raw') + + data = qemu_img_info(dest) + if data.file_format != "raw": + raise exception.ImageUnacceptable( + image_id=image_id, + reason=_("Converted to raw, but format is now %s") % + data.file_format) + os.unlink(tmp) -- 2.45.2