From b1b5cf7ee83a7f70c23f015a53a34948bf23dd11 Mon Sep 17 00:00:00 2001 From: Jeffy Chen Date: Mon, 20 Feb 2017 21:26:58 +0800 Subject: [PATCH] cgpt: find: filter out more devices before touching them A partition's name would always start with the disk name. And in /proc/partitions, the partitions are always listed right after the disk. Let's filter out devices which are not followed by partitions when go through the /proc/partitions. BUG=chrome-os-partner:62955 TEST=run "cgpt find -t kernel" on kevin, no more this warning: blk_update_request: I/O error, dev mmcblk0rpmb Change-Id: If200a2476d26b1beaf644838d47ea2e60552855e Signed-off-by: Jeffy Chen Reviewed-on: https://chromium-review.googlesource.com/444492 Reviewed-by: Julius Werner --- cgpt/cgpt_find.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/cgpt/cgpt_find.c b/cgpt/cgpt_find.c index c35aade2be..a02ad7ae86 100644 --- a/cgpt/cgpt_find.c +++ b/cgpt/cgpt_find.c @@ -177,6 +177,7 @@ static int do_search(CgptFindParams *params, char *fileName) { #define PROC_PARTITIONS "/proc/partitions" #define DEV_DIR "/dev" #define SYS_BLOCK_DIR "/sys/block" +#define MAX_PARTITION_NAME_LEN 128 static const char *devdirs[] = { "/dev", "/devices", "/devfs", 0 }; @@ -219,7 +220,8 @@ static char *is_wholedev(const char *basename) { // returns true if any matches were found, false otherwise. static int scan_real_devs(CgptFindParams *params) { int found = 0; - char partname[128]; // max size for /proc/partition lines? + char partname[MAX_PARTITION_NAME_LEN]; + char partname_prev[MAX_PARTITION_NAME_LEN]; FILE *fp; char *pathname; @@ -231,6 +233,7 @@ static int scan_real_devs(CgptFindParams *params) { size_t line_length = 0; char *line = NULL; + partname_prev[0] = '\0'; while (getline(&line, &line_length, fp) != -1) { int ma, mi; long long unsigned int sz; @@ -238,11 +241,19 @@ static int scan_real_devs(CgptFindParams *params) { if (sscanf(line, " %d %d %llu %127[^\n ]", &ma, &mi, &sz, partname) != 4) continue; - if ((pathname = is_wholedev(partname))) { - if (do_search(params, pathname)) { - found++; + /* Only check devices that have partitions under them. + * We can tell by checking that an entry like "sda" is immediately + * followed by one like "sda0". */ + if (!strncmp(partname_prev, partname, strlen(partname_prev)) && + strlen(partname_prev)) { + if ((pathname = is_wholedev(partname_prev))) { + if (do_search(params, pathname)) { + found++; + } } } + + strcpy(partname_prev, partname); } fclose(fp);