From 37c9148a3ae17665e75f99b459b6ff2b461d6739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= Date: Tue, 5 May 2020 08:49:17 +0200 Subject: block: simplify check_extroot() a bit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Avoid too big code indent. Signed-off-by: Rafał Miłecki --- block.c | 89 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/block.c b/block.c index a7631c4..fd35d6b 100644 --- a/block.c +++ b/block.c @@ -1386,7 +1386,12 @@ static int test_fs_support(const char *name) static int check_extroot(char *path) { struct probe_info *pr = NULL; + struct probe_info *tmp; + struct stat s; + char uuid[64] = { 0 }; char devpath[32]; + char tag[64]; + FILE *fp; #ifdef UBIFS_EXTROOT if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) { @@ -1408,53 +1413,53 @@ static int check_extroot(char *path) } #endif - list_for_each_entry(pr, &devices, list) { - if (!strcmp(pr->dev, devpath)) { - struct stat s; - FILE *fp = NULL; - char tag[64]; - char uuid[64] = { 0 }; - - snprintf(tag, sizeof(tag), "%s/etc", path); - if (stat(tag, &s)) - mkdir_p(tag); - - snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path); - if (stat(tag, &s)) { - fp = fopen(tag, "w+"); - if (!fp) { - ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n", - tag, errno); - /* return 0 to continue boot regardless of error */ - return 0; - } - fputs(pr->uuid, fp); - fclose(fp); - return 0; - } - - fp = fopen(tag, "r"); - if (!fp) { - ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", - tag, errno); - return -1; - } + /* Find root device probe_info so we know its UUID */ + list_for_each_entry(tmp, &devices, list) { + if (!strcmp(tmp->dev, devpath)) { + pr = tmp; + break; + } + } + if (!pr) { + ULOG_ERR("extroot: unable to lookup root device %s\n", devpath); + return -1; + } - if (!fgets(uuid, sizeof(uuid), fp)) - ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", - tag, errno); - fclose(fp); + snprintf(tag, sizeof(tag), "%s/etc", path); + if (stat(tag, &s)) + mkdir_p(tag); - if (*uuid && !strcasecmp(uuid, pr->uuid)) - return 0; - - ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n", - pr->uuid, basename(path), uuid); - return -1; + snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path); + if (stat(tag, &s)) { + fp = fopen(tag, "w+"); + if (!fp) { + ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n", + tag, errno); + /* return 0 to continue boot regardless of error */ + return 0; } + fputs(pr->uuid, fp); + fclose(fp); + return 0; } - ULOG_ERR("extroot: unable to lookup root device %s\n", devpath); + fp = fopen(tag, "r"); + if (!fp) { + ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag, + errno); + return -1; + } + + if (!fgets(uuid, sizeof(uuid), fp)) + ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag, + errno); + fclose(fp); + + if (*uuid && !strcasecmp(uuid, pr->uuid)) + return 0; + + ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n", pr->uuid, + basename(path), uuid); return -1; } -- cgit v1.2.1