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
This commit is contained in:
Vishvananda Ishaya
2012-12-20 13:43:09 -08:00
parent 3b02985805
commit cbcd340c67

View File

@@ -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()
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.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)
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())
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()
# 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)
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)