05ad79
From e4f26d51bd06910634c2aaeccfecc7e6cd61ef0a Mon Sep 17 00:00:00 2001
05ad79
From: Karel Zak <kzak@redhat.com>
05ad79
Date: Fri, 17 Mar 2017 11:47:00 +0100
05ad79
Subject: [PATCH 093/116] fdisk: improve -l error handling
05ad79
05ad79
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1362662
05ad79
Signed-off-by: Karel Zak <kzak@redhat.com>
05ad79
---
05ad79
 fdisks/fdisk.c | 23 ++++++++++++++++-------
05ad79
 1 file changed, 16 insertions(+), 7 deletions(-)
05ad79
05ad79
diff --git a/fdisks/fdisk.c b/fdisks/fdisk.c
05ad79
index 974d735..177921a 100644
05ad79
--- a/fdisks/fdisk.c
05ad79
+++ b/fdisks/fdisk.c
05ad79
@@ -924,11 +924,15 @@ static int is_ide_cdrom_or_tape(char *device)
05ad79
 }
05ad79
 
05ad79
 /* Print disk geometry and partition table of a specified device (-l option) */
05ad79
-static void print_partition_table_from_option(struct fdisk_context *cxt,
05ad79
-				char *device, unsigned long sector_size)
05ad79
+static int print_partition_table_from_option(struct fdisk_context *cxt,
05ad79
+				char *device, unsigned long sector_size,
05ad79
+				int warnme)
05ad79
 {
05ad79
-	if (fdisk_context_assign_device(cxt, device, 1) != 0)	/* read-only */
05ad79
-		err(EXIT_FAILURE, _("cannot open %s"), device);
05ad79
+	if (fdisk_context_assign_device(cxt, device, 1) != 0) {	/* read-only */
05ad79
+		if (warnme || errno == EACCES)
05ad79
+			warn(_("cannot open %s"), device);
05ad79
+		return -1;
05ad79
+	}
05ad79
 
05ad79
 	if (sector_size) /* passed -b option, override autodiscovery */
05ad79
 		fdisk_override_sector_size(cxt, sector_size);
05ad79
@@ -941,6 +945,8 @@ static void print_partition_table_from_option(struct fdisk_context *cxt,
05ad79
 		list_table(cxt, 0);
05ad79
 	else
05ad79
 		list_disk_geometry(cxt);
05ad79
+
05ad79
+	return 0;
05ad79
 }
05ad79
 
05ad79
 /*
05ad79
@@ -971,7 +977,7 @@ print_all_partition_table_from_option(struct fdisk_context *cxt,
05ad79
 			char *cn = canonicalize_path(devname);
05ad79
 			if (cn) {
05ad79
 				if (!is_ide_cdrom_or_tape(cn))
05ad79
-					print_partition_table_from_option(cxt, cn, sector_size);
05ad79
+					print_partition_table_from_option(cxt, cn, sector_size, 0);
05ad79
 				free(cn);
05ad79
 			}
05ad79
 		}
05ad79
@@ -1223,14 +1229,17 @@ int main(int argc, char **argv)
05ad79
 			 " be used with one specified device\n"));
05ad79
 
05ad79
 	if (optl) {
05ad79
+		int rc = 0;
05ad79
+
05ad79
 		nowarn = 1;
05ad79
 		if (argc > optind) {
05ad79
 			int k;
05ad79
 			for (k = optind; k < argc; k++)
05ad79
-				print_partition_table_from_option(cxt, argv[k], sector_size);
05ad79
+				rc += print_partition_table_from_option(cxt, argv[k], sector_size, 1);
05ad79
 		} else
05ad79
 			print_all_partition_table_from_option(cxt, sector_size);
05ad79
-		exit(EXIT_SUCCESS);
05ad79
+
05ad79
+		exit(rc ? EXIT_FAILURE : EXIT_SUCCESS);
05ad79
 	}
05ad79
 
05ad79
 	if (opts) {
05ad79
-- 
05ad79
2.9.3
05ad79