]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Make sure we don't double remove tmp on exception
authorVishvananda Ishaya <vishvananda@gmail.com>
Thu, 20 Dec 2012 21:43:09 +0000 (13:43 -0800)
committerVishvananda Ishaya <vishvananda@gmail.com>
Thu, 20 Dec 2012 21:49:47 +0000 (13:49 -0800)
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

index e38c59a64a4cb753ffbca73869eaea1b5d305d42..8f1251f17ac4b5664eb39adab2aabbe063ab5317 100644 (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()
-
-        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)