File lp_845788_glance_client_zero_length.patch of Package openstack-glance

Origin: upstream, https://github.com/openstack/glance/commit/6cfff16f2dc22a870bfe3808a7895dfbbaa11369
Bug: https://bugs.launchpad.net/glance/+bug/845788
commit 6cfff16f2dc22a870bfe3808a7895dfbbaa11369
Author: Jay Pipes <jaypipes@gmail.com>
Date:   Fri Sep 9 13:03:19 2011 -0400

    Fixes LP Bug#845788
    
    glance.client.image_update needed to calculate size so that Glance's Swift
    driver can do chunking properly for large objects.
    
    Change-Id: Iafe8034a710cff53a0caa3ae5e9ee3a3adda19f8

--- a/glance/client.py
+++ b/glance/client.py
@@ -96,6 +96,33 @@ class V1Client(base_client.BaseClient):
         image = utils.get_image_meta_from_headers(res)
         return image
 
+    def _get_image_size(self, image_data):
+        """
+        Analyzes the incoming image file and attempts to determine
+        its size.
+
+        :param image_data: The input to the client, typically a file
+                           redirected from stdin.
+        :retval The image file's size or None if it cannot be determined.
+        """
+        # For large images, we need to supply the size of the
+        # image file. See LP Bugs #827660 and #845788.
+        if hasattr(image_data, 'seek') and hasattr(image_data, 'tell'):
+            try:
+                image_data.seek(0, os.SEEK_END)
+                image_size = image_data.tell()
+                image_data.seek(0)
+                return image_size
+            except IOError, e:
+                if e.errno == errno.ESPIPE:
+                    # Illegal seek. This means the user is trying
+                    # to pipe image data to the client, e.g.
+                    # echo testdata | bin/glance add blah..., or
+                    # that stdin is empty
+                    return None
+                else:
+                    raise
+
     def add_image(self, image_meta=None, image_data=None):
         """
         Tells Glance about an image's metadata as well
@@ -114,24 +141,10 @@ class V1Client(base_client.BaseClient):
         if image_data:
             body = image_data
             headers['content-type'] = 'application/octet-stream'
-            # For large images, we need to supply the size of the
-            # image file. See LP Bug #827660.
-            if hasattr(image_data, 'seek') and hasattr(image_data, 'tell'):
-                try:
-                    image_data.seek(0, os.SEEK_END)
-                    image_size = image_data.tell()
-                    image_data.seek(0)
-                    headers['x-image-meta-size'] = image_size
-                    headers['content-length'] = image_size
-                except IOError, e:
-                    if e.errno == errno.ESPIPE:
-                        # Illegal seek. This means the user is trying
-                        # to pipe image data to the client, e.g.
-                        # echo testdata | bin/glance add blah..., or
-                        # that stdin is empty
-                        pass
-                    else:
-                        raise
+            image_size = self._get_image_size(image_data)
+            if image_size:
+                headers['x-image-meta-size'] = image_size
+                headers['content-length'] = image_size
         else:
             body = None
 
@@ -151,6 +164,10 @@ class V1Client(base_client.BaseClient):
         if image_data:
             body = image_data
             headers['content-type'] = 'application/octet-stream'
+            image_size = self._get_image_size(image_data)
+            if image_size:
+                headers['x-image-meta-size'] = image_size
+                headers['content-length'] = image_size
         else:
             body = None
 
openSUSE Build Service is sponsored by