From 8ee1131bed23501c1c813a1adb92f78d6be24af9 Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Sun, 9 Nov 2014 10:51:48 +0100 Subject: [PATCH] Refactors some mapping/find_disk uses. --- cmdline/elem.c | 2 +- cmdline/elem.h | 2 +- cmdline/state.c | 99 ++++++++++++++++++++----------------------------- 3 files changed, 43 insertions(+), 60 deletions(-) diff --git a/cmdline/elem.c b/cmdline/elem.c index d875c42a..6d6b4b70 100644 --- a/cmdline/elem.c +++ b/cmdline/elem.c @@ -655,7 +655,7 @@ struct snapraid_disk* disk_alloc(const char* name, const char* dir, uint64_t dev disk->has_different_uuid = 0; disk->has_unsupported_uuid = 0; disk->had_empty_uuid = 0; - disk->mapping = -1; + disk->mapping_idx = -1; tommy_list_init(&disk->filelist); tommy_list_init(&disk->deletedlist); tommy_hashdyn_init(&disk->inodeset); diff --git a/cmdline/elem.h b/cmdline/elem.h index 1cae074f..48751f03 100644 --- a/cmdline/elem.h +++ b/cmdline/elem.h @@ -320,7 +320,7 @@ struct snapraid_disk { int has_different_uuid; /**< If the disk has a different UUID, meaning that it is not the same filesystem. */ int has_unsupported_uuid; /**< If the disk doesn't report UUID, meaning it's not supported. */ int had_empty_uuid; /**< If the disk had an empty UUID, meaning that it's a new disk. */ - int mapping; /**< Index in the mapping vector. Used only as buffer when writing the content file. */ + int mapping_idx; /**< Index in the mapping vector. Used only as buffer when writing the content file. */ /**< * Block array of the disk. diff --git a/cmdline/state.c b/cmdline/state.c index 8df5cb87..4ff1de0b 100644 --- a/cmdline/state.c +++ b/cmdline/state.c @@ -357,8 +357,6 @@ static void state_config_check(struct snapraid_state* state, const char* path, t /* LCOV_EXCL_STOP */ } } - - } void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk) @@ -896,6 +894,33 @@ void state_config(struct snapraid_state* state, const char* path, const char* co fflush(stdlog); } +/** + * Finds a disk by name. + */ +static struct snapraid_disk* find_disk(struct snapraid_state* state, const char* name) +{ + tommy_node* i; + + for (i = state->disklist; i != 0; i = i->next) { + struct snapraid_disk* disk = i->data; + if (strcmp(disk->name, name) == 0) + return disk; + } + + if (state->no_conf) { + /* without a configuration file, add disks automatically */ + struct snapraid_disk* disk; + + disk = disk_alloc(name, "DUMMY/", -1); + + tommy_list_insert_tail(&state->disklist, &disk->node, disk); + + return disk; + } + + return 0; +} + /** * Updates the disk mapping if required. */ @@ -913,20 +938,13 @@ static void state_map(struct snapraid_state* state) for (i = state->maplist; i != 0; ) { struct snapraid_map* map = i->data; struct snapraid_disk* disk; - tommy_node* j; - for (j = state->disklist; j != 0; j = j->next) { - disk = j->data; - if (strcmp(disk->name, map->name) == 0) { - /* disk found */ - break; - } - } + disk = find_disk(state, map->name); /* go to the next mapping before removing */ i = i->next; - if (j == 0) { + if (disk == 0) { /* disk not found, remove the mapping */ tommy_list_remove_existing(&state->maplist, &map->node); map_free(map); @@ -985,19 +1003,11 @@ static void state_map(struct snapraid_state* state) for (i = state->maplist; i != 0; i = i->next) { struct snapraid_map* map = i->data; struct snapraid_disk* disk; - tommy_node* j; char uuid[UUID_MAX]; int ret; - for (j = state->disklist; j != 0; j = j->next) { - disk = j->data; - if (strcmp(disk->name, map->name) == 0) { - /* disk found */ - break; - } - } - - if (j == 0) { + disk = find_disk(state, map->name); + if (disk == 0) { /* LCOV_EXCL_START */ fprintf(stderr, "Internal inconsistency for mapping '%s'\n", map->name); exit(EXIT_FAILURE); @@ -1182,33 +1192,6 @@ static int is_block_deleted(struct snapraid_disk* disk, block_off_t pos) return block_state_get(block) == BLOCK_STATE_DELETED; } -/** - * Finds a disk by name. - */ -static struct snapraid_disk* find_disk(struct snapraid_state* state, const char* name) -{ - tommy_node* i; - - for (i = state->disklist; i != 0; i = i->next) { - struct snapraid_disk* disk = i->data; - if (strcmp(disk->name, name) == 0) - return disk; - } - - if (state->no_conf) { - /* without a configuration file, add disks automatically */ - struct snapraid_disk* disk; - - disk = disk_alloc(name, "DUMMY/", -1); - - tommy_list_insert_tail(&state->disklist, &disk->node, disk); - - return disk; - } - - return 0; -} - static void state_read_text(struct snapraid_state* state, const char* path, STREAM* f, time_t save_time) { struct snapraid_disk* disk; @@ -2313,10 +2296,10 @@ static void state_write_text(struct snapraid_state* state, STREAM* f) } /* mark the disk as with mapping */ - disk->mapping = 0; + disk->mapping_idx = 0; } else { /* mark the disk as without mapping */ - disk->mapping = -1; + disk->mapping_idx = -1; } } @@ -2326,7 +2309,7 @@ static void state_write_text(struct snapraid_state* state, STREAM* f) struct snapraid_disk* disk = i->data; /* if the disk is not mapped, skip it */ - if (disk->mapping < 0) + if (disk->mapping_idx < 0) continue; /* for each file */ @@ -3617,11 +3600,11 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) } /* assign the mapping index used to identify disks */ - disk->mapping = mapping_idx; + disk->mapping_idx = mapping_idx; ++mapping_idx; } else { /* mark the disk as without mapping */ - disk->mapping = -1; + disk->mapping_idx = -1; } } @@ -3631,7 +3614,7 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) struct snapraid_disk* disk = i->data; /* if the disk is not mapped, skip it */ - if (disk->mapping < 0) + if (disk->mapping_idx < 0) continue; /* for each file */ @@ -3649,7 +3632,7 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) inode = file->inode; sputc('f', f); - sputb32(disk->mapping, f); + sputb32(disk->mapping_idx, f); sputb64(size, f); sputb64(mtime_sec, f); /* encode STAT_NSEC_INVALID as 0 */ @@ -3750,7 +3733,7 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) break; } - sputb32(disk->mapping, f); + sputb32(disk->mapping_idx, f); sputbs(link->sub, f); sputbs(link->linkto, f); if (serror(f)) { @@ -3766,7 +3749,7 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) struct snapraid_dir* dir = j->data; sputc('r', f); - sputb32(disk->mapping, f); + sputb32(disk->mapping_idx, f); sputbs(dir->sub, f); if (serror(f)) { /* LCOV_EXCL_START */ @@ -3780,7 +3763,7 @@ static void state_write_binary(struct snapraid_state* state, STREAM* f) /* deleted blocks of the disk */ sputc('h', f); - sputb32(disk->mapping, f); + sputb32(disk->mapping_idx, f); if (serror(f)) { /* LCOV_EXCL_START */ fprintf(stderr, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));