dcavalca / rpms / util-linux

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