Subject: [PATCH 2/3] block: Use error codes from lower levels for error message

From: Markus Armbruster <[email-masked]>
Date: Thu, 17 Feb 2011 12:57:02 -0200

RH-Author: Markus Armbruster <[email-masked]>
Message-id: <[email-masked]>
Patchwork-id: 18400
O-Subject: [PATCH RHEL6.1 qemu-kvm 2/2] block: Use error codes from lower levels
	for error message
Bugzilla: 607598
RH-Acked-by: Avi Kivity <[email-masked]>
RH-Acked-by: Jes Sorensen <[email-masked]>
RH-Acked-by: Kevin Wolf <[email-masked]>

"No such file or directory" is a misleading error message
when a user tries to open a file with wrong permissions.

Cc: Kevin Wolf <[email-masked]>
Signed-off-by: Stefan Weil <[email-masked]>
Signed-off-by: Kevin Wolf <[email-masked]>
(cherry picked from commit c98ac35d87fbd41618c1f02c64bcd4019e42513e)

Conflicts:

	block.c
Signed-off-by: Markus Armbruster <[email-masked]>
---
 block.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)

Signed-off-by: Luiz Capitulino <[email-masked]>
---
 block.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/block.c b/block.c
index ec231da..17a7457 100644
--- a/block.c
+++ b/block.c
@@ -327,7 +327,7 @@ BlockDriver *bdrv_find_protocol(const char *filename)
     return NULL;
 }
 
-static BlockDriver *find_image_format(const char *filename)
+static int find_image_format(const char *filename, BlockDriver **pdrv)
 {
     int ret, score, score_max;
     BlockDriver *drv1, *drv;
@@ -335,19 +335,27 @@ static BlockDriver *find_image_format(const char *filename)
     BlockDriverState *bs;
 
     ret = bdrv_file_open(&bs, filename, 0);
-    if (ret < 0)
-        return NULL;
+    if (ret < 0) {
+        *pdrv = NULL;
+        return ret;
+    }
 
     /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
     if (bs->sg || !bdrv_is_inserted(bs)) {
         bdrv_delete(bs);
-        return bdrv_find_format("raw");
+        drv = bdrv_find_format("raw");
+        if (!drv) {
+            ret = -ENOENT;
+        }
+        *pdrv = drv;
+        return ret;
     }
 
     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
     bdrv_delete(bs);
     if (ret < 0) {
-        return NULL;
+        *pdrv = NULL;
+        return ret;
     }
 
     score_max = 0;
@@ -361,7 +369,11 @@ static BlockDriver *find_image_format(const char *filename)
             }
         }
     }
-    return drv;
+    if (!drv) {
+        ret = -ENOENT;
+    }
+    *pdrv = drv;
+    return ret;
 }
 
 /**
@@ -562,11 +574,10 @@ int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
 
     /* Find the right image format driver */
     if (!drv) {
-        drv = find_image_format(filename);
+        ret = find_image_format(filename, &drv);
     }
 
     if (!drv) {
-        ret = -ENOENT;
         goto unlink_and_fail;
     }
 
-- 
1.7.4.rc1.16.gd2f15e