diff --git a/elem.h b/elem.h index 5a91af36..0066d8ae 100644 --- a/elem.h +++ b/elem.h @@ -680,12 +680,39 @@ struct snapraid_map* map_alloc(const char* name, unsigned position, const char* void map_free(struct snapraid_map* map); /** - * Make an info. + * Makes an info. */ static inline snapraid_info info_make(time_t last_access) { - /* clear the low bit to keep some free bits */ - return last_access & ~0xFF; + /* clear the lowest bit as reserved for the hash type */ + return last_access & ~0x1; +} + +/** + * Sets the info at the specified position. + * The position is allocated if not yet done. + */ +static inline void info_set(tommy_arrayof* array, unsigned pos, snapraid_info info) +{ + tommy_arrayof_grow(array, pos + 1); + + memcpy(tommy_arrayof_ref(array, pos), &info, sizeof(snapraid_info)); +} + +/** + * Gets the info at the specified position. + * For not allocated position, 0 is returned. + */ +static inline snapraid_info info_get(tommy_arrayof* array, unsigned pos) +{ + snapraid_info info; + + if (pos >= tommy_arrayof_size(array)) + return 0; + + memcpy(&info, tommy_arrayof_ref(array, pos), sizeof(snapraid_info)); + + return info; } #endif diff --git a/state.c b/state.c index 620b6fab..649fd358 100644 --- a/state.c +++ b/state.c @@ -1040,8 +1040,7 @@ void state_read(struct snapraid_state* state) info = info_make(t); /* insert the block in the block array */ - tommy_arrayof_grow(&state->infoarr, v_pos + 1); - memcpy(tommy_arrayof_ref(&state->infoarr, v_pos), &info, sizeof(snapraid_info)); + info_set(&state->infoarr, v_pos, info); } else if (strcmp(tag, "off") == 0) { /* "off" command */ block_off_t v_pos; @@ -1963,15 +1962,11 @@ void state_write(struct snapraid_state* state) /* note that here infomax contains the maximum block for which we wrote something */ /* tha could be smaller than of blocks we have in memory, in case of deleted blocks */ - /* don't try to write more info than stored */ - if (infomax > tommy_arrayof_size(&state->infoarr)) - infomax = tommy_arrayof_size(&state->infoarr); - /* for each block */ for(b=0;binfoarr, b), sizeof(snapraid_info)); + info = info_get(&state->infoarr, b); /* save only stuff different than 0 */ if (info != 0) { diff --git a/sync.c b/sync.c index 65245ba9..48624a95 100644 --- a/sync.c +++ b/sync.c @@ -45,6 +45,10 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari block_off_t autosavemissing; int ret; unsigned unrecoverable_error; + time_t now; + + /* get the present time */ + now = time(0); /* maps the disks to handles */ handle = handle_map(state, &diskmax); @@ -309,6 +313,9 @@ static int state_sync_process(struct snapraid_state* state, struct snapraid_pari /* now all the blocks have the hash and the parity computed */ block_state_set(block, BLOCK_STATE_BLK); } + + /* update the time info of the block */ + info_set(&state->infoarr, i, info_make(now)); } /* mark the state as needing write */