From fd459d7c6ccaa14600b88d2beca5730b8cfb5162 Mon Sep 17 00:00:00 2001
From: Hannes Reinecke <hare@suse.de>
Date: Thu, 15 May 2014 12:27:22 +0200
Subject: [PATCH 1/3] sg_inq: suppress warning messages for --export

When called with --export we should just print out the resulting
strings. Printing out warnings in addition just confuses the
caller. And if required the warnings can be enabled by specifying
--verbose.

References: bnc#876454

Signed-off-by: Hannes Reinecke <hare@suse.de>
---
 src/sg_inq.c | 93 ++++++++++++++++++++++++++++++++++++++----------------------
 1 file changed, 59 insertions(+), 34 deletions(-)

Index: sg3_utils-1.35/src/sg_inq.c
===================================================================
--- sg3_utils-1.35.orig/src/sg_inq.c
+++ sg3_utils-1.35/src/sg_inq.c
@@ -1348,7 +1348,7 @@ decode_dev_ids(const char * leadin, unsi
 }
 
 static void
-export_dev_ids(unsigned char * buff, int len)
+export_dev_ids(unsigned char * buff, int len, int verbose)
 {
     int u, j, m, id_len, c_set, assoc, desig_type, i_len;
     int off, d_id, naa, k;
@@ -1377,9 +1377,12 @@ export_dev_ids(unsigned char * buff, int
         i_len = ucp[3];
         id_len = i_len + 4;
         if ((off + id_len) > len) {
-            fprintf(stderr, "Device Identification VPD page error: designator "
-                    "length longer than\n     remaining response length=%d\n",
-                    (len - off));
+            if (verbose) {
+                fprintf(stderr, "Device Identification VPD page error: "
+                        "designator length longer than\n     "
+                        "remaining response length=%d\n",
+                        (len - off));
+            }
             return;
         }
         ip = ucp + 4;
@@ -1398,7 +1401,8 @@ export_dev_ids(unsigned char * buff, int
                 assoc_str = "TARGET";
                 break;
             default:
-                fprintf(stderr, "    Invalid association %d\n", assoc);
+                if (verbose)
+                    fprintf(stderr, "    Invalid association %d\n", assoc);
                 return;
         }
         switch (desig_type) {
@@ -1425,8 +1429,10 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 2: /* EUI-64 based */
             if (1 != c_set) {
-                fprintf(stderr, "      << expected binary code_set (1)>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set (1)>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             printf("SCSI_IDENT_%s_EUI64=", assoc_str);
@@ -1436,21 +1442,27 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 3: /* NAA */
             if (1 != c_set) {
-                fprintf(stderr, "      << expected binary code_set (1)>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set (1)>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             naa = (ip[0] >> 4) & 0xff;
             if ((naa < 2) || (naa > 6) || (4 == naa)) {
-                fprintf(stderr, "      << unexpected naa [0x%x]>>\n", naa);
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << unexpected naa [0x%x]>>\n", naa);
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             if (6 != naa) {
                 if (8 != i_len) {
-                    fprintf(stderr, "      << unexpected NAA 2 identifier "
-                            "length: 0x%x>>\n", i_len);
-                    dStrHex((const char *)ip, i_len, 0);
+                    if (verbose) {
+                        fprintf(stderr, "      << unexpected NAA 2 identifier "
+                                "length: 0x%x>>\n", i_len);
+                        dStrHex((const char *)ip, i_len, 0);
+                    }
                     break;
                 }
                 printf("SCSI_IDENT_%s_NAA=", assoc_str);
@@ -1459,9 +1471,11 @@ export_dev_ids(unsigned char * buff, int
                 printf("\n");
             } else {      /* NAA IEEE Registered extended */
                 if (16 != i_len) {
-                    fprintf(stderr, "      << unexpected NAA 6 identifier "
-                            "length: 0x%x>>\n", i_len);
-                    dStrHex((const char *)ip, i_len, 0);
+                    if (verbose) {
+                        fprintf(stderr, "      << unexpected NAA 6 identifier "
+                                "length: 0x%x>>\n", i_len);
+                        dStrHex((const char *)ip, i_len, 0);
+                    }
                     break;
                 }
                 printf("SCSI_IDENT_%s_NAA=", assoc_str);
@@ -1472,9 +1486,11 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 4: /* Relative target port */
             if ((1 != c_set) || (1 != assoc) || (4 != i_len)) {
-                fprintf(stderr, "      << expected binary code_set, target "
-                        "port association, length 4>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set, target "
+                            "port association, length 4>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             d_id = ((ip[2] << 8) | ip[3]);
@@ -1482,9 +1498,11 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 5: /* (primary) Target port group */
             if ((1 != c_set) || (1 != assoc) || (4 != i_len)) {
-                fprintf(stderr, "      << expected binary code_set, target "
-                        "port association, length 4>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set, target "
+                            "port association, length 4>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             d_id = ((ip[2] << 8) | ip[3]);
@@ -1492,9 +1510,11 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 6: /* Logical unit group */
             if ((1 != c_set) || (0 != assoc) || (4 != i_len)) {
-                fprintf(stderr, "      << expected binary code_set, logical "
-                        "unit association, length 4>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set, logical "
+                            "unit association, length 4>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             d_id = ((ip[2] << 8) | ip[3]);
@@ -1502,9 +1522,11 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 7: /* MD5 logical unit identifier */
             if ((1 != c_set) || (0 != assoc)) {
-                fprintf(stderr, "      << expected binary code_set, logical "
-                        "unit association>>\n");
-                dStrHex((const char *)ip, i_len, 0);
+                if (verbose) {
+                    fprintf(stderr, "      << expected binary code_set, logical "
+                            "unit association>>\n");
+                    dStrHex((const char *)ip, i_len, 0);
+                }
                 break;
             }
             printf("SCSI_IDENT_%s_MD5=", assoc_str);
@@ -1512,8 +1534,10 @@ export_dev_ids(unsigned char * buff, int
             break;
         case 8: /* SCSI name string */
             if (3 != c_set) {
-                fprintf(stderr, "      << expected UTF-8 code_set>>\n");
-                dStrHex((const char *)ip, i_len, -1);
+                if (verbose) {
+                    fprintf(stderr, "      << expected UTF-8 code_set>>\n");
+                    dStrHex((const char *)ip, i_len, -1);
+                }
                 break;
             }
             printf("SCSI_IDENT_%s_NAME=%.*s\n", assoc_str, i_len,
@@ -1522,11 +1546,12 @@ export_dev_ids(unsigned char * buff, int
         case 9: /* PCIe Routing ID */
             /* new in spc4r34, looks under-specified, drop through now */
         default: /* reserved */
-            dStrHex((const char *)ip, i_len, -1);
+            if (verbose)
+                dStrHex((const char *)ip, i_len, -1);
             break;
         }
     }
-    if (-2 == u)
+    if (-2 == u && verbose)
         fprintf(stderr, "Device identification VPD page error: "
                 "around offset=%d\n", off);
 }
@@ -2713,7 +2738,7 @@ decode_vpd(int sg_fd, const struct opts_
                     fprintf(stderr, "Device identification page length too "
                             "short=%d\n", len);
                 } else {
-                    export_dev_ids(rsp_buff + 4, len - 4);
+                    export_dev_ids(rsp_buff + 4, len - 4, optsp->do_verbose);
                 }
             } else
                 decode_id_vpd(rsp_buff, len, optsp->do_hex);
