LCOV - code coverage report
Current view: top level - cmdline - state.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1547 1705 90.7 %
Date: 2015-05-12 Functions: 31 32 96.9 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (C) 2011 Andrea Mazzoleni
       3             :  *
       4             :  * This program is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 3 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * This program is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "portable.h"
      19             : 
      20             : #include "elem.h"
      21             : #include "import.h"
      22             : #include "search.h"
      23             : #include "state.h"
      24             : #include "support.h"
      25             : #include "parity.h"
      26             : #include "stream.h"
      27             : #include "raid/raid.h"
      28             : #include "raid/cpu.h"
      29             : 
      30           7 : const char* lev_name(unsigned l)
      31             : {
      32           7 :         switch (l) {
      33           2 :         case 0 : return "Parity";
      34           1 :         case 1 : return "2-Parity";
      35           1 :         case 2 : return "3-Parity";
      36           1 :         case 3 : return "4-Parity";
      37           1 :         case 4 : return "5-Parity";
      38           1 :         case 5 : return "6-Parity";
      39             :         }
      40             : 
      41           0 :         return 0;
      42             : }
      43             : 
      44      178682 : const char* lev_config_name(unsigned l)
      45             : {
      46      178682 :         switch (l) {
      47       70474 :         case 0 : return "parity";
      48       57403 :         case 1 : return "2-parity";
      49       12706 :         case 2 : return "3-parity";
      50       12703 :         case 3 : return "4-parity";
      51       12700 :         case 4 : return "5-parity";
      52       12696 :         case 5 : return "6-parity";
      53             :         }
      54             : 
      55           0 :         return 0;
      56             : }
      57             : 
      58        5612 : static int lev_config_scan(const char* s, unsigned* level, unsigned* mode)
      59             : {
      60        5612 :         if (strcmp(s, "parity") == 0 || strcmp(s, "1-parity") == 0) {
      61         397 :                 *level = 0;
      62         397 :                 return 0;
      63             :         }
      64             : 
      65        5215 :         if (strcmp(s, "q-parity") == 0 || strcmp(s, "2-parity") == 0) {
      66         202 :                 *level = 1;
      67         202 :                 return 0;
      68             :         }
      69             : 
      70        5013 :         if (strcmp(s, "r-parity") == 0 || strcmp(s, "3-parity") == 0) {
      71         177 :                 *level = 2;
      72         177 :                 return 0;
      73             :         }
      74             : 
      75        4836 :         if (strcmp(s, "4-parity") == 0) {
      76         174 :                 *level = 3;
      77         174 :                 return 0;
      78             :         }
      79             : 
      80        4662 :         if (strcmp(s, "5-parity") == 0) {
      81         171 :                 *level = 4;
      82         171 :                 return 0;
      83             :         }
      84             : 
      85        4491 :         if (strcmp(s, "6-parity") == 0) {
      86         167 :                 *level = 5;
      87         167 :                 return 0;
      88             :         }
      89             : 
      90        4324 :         if (strcmp(s, "z-parity") == 0) {
      91           0 :                 *level = 2;
      92           0 :                 if (mode)
      93           0 :                         *mode = RAID_MODE_VANDERMONDE;
      94           0 :                 return 0;
      95             :         }
      96             : 
      97        4324 :         return -1;
      98             : }
      99             : 
     100         235 : const char* lev_raid_name(unsigned mode, unsigned n)
     101             : {
     102         235 :         switch (n) {
     103          33 :         case 1 : return "par1";
     104          25 :         case 2 : return "par2";
     105           3 :         case 3 : if (mode == RAID_MODE_CAUCHY)
     106           3 :                         return "par3";
     107             :                 else
     108           0 :                         return "parz";
     109           3 :         case 4 : return "par4";
     110           4 :         case 5 : return "par5";
     111         167 :         case 6 : return "par6";
     112             :         }
     113             : 
     114           0 :         return 0;
     115             : }
     116             : 
     117         236 : void state_init(struct snapraid_state* state)
     118             : {
     119             :         unsigned l;
     120             : 
     121         236 :         memset(&state->opt, 0, sizeof(state->opt));
     122         236 :         state->filter_hidden = 0;
     123         236 :         state->autosave = 0;
     124         236 :         state->need_write = 0;
     125         236 :         state->checked_read = 0;
     126         236 :         state->block_size = 256 * 1024; /* default 256 KiB */
     127         236 :         state->raid_mode = RAID_MODE_CAUCHY;
     128         236 :         state->file_mode = MODE_SEQUENTIAL;
     129        1652 :         for (l = 0; l < LEV_MAX; ++l) {
     130        1416 :                 state->parity[l].path[0] = 0;
     131        1416 :                 state->parity[l].smartctl[0] = 0;
     132        1416 :                 state->parity[l].uuid[0] = 0;
     133        1416 :                 state->parity[l].device = 0;
     134        1416 :                 state->parity[l].total_blocks = 0;
     135        1416 :                 state->parity[l].free_blocks = 0;
     136        1416 :                 state->parity[l].tick = 0;
     137             :         }
     138         236 :         state->tick_io = 0;
     139         236 :         state->tick_cpu = 0;
     140         236 :         state->tick_last = tick();
     141         236 :         state->share[0] = 0;
     142         236 :         state->pool[0] = 0;
     143         236 :         state->pool_device = 0;
     144         236 :         state->lockfile[0] = 0;
     145         236 :         state->level = 1; /* default is the lowest protection */
     146         236 :         state->clear_past_hash = 0;
     147         236 :         state->no_conf = 0;
     148             : 
     149         236 :         tommy_list_init(&state->disklist);
     150         236 :         tommy_list_init(&state->maplist);
     151         236 :         tommy_list_init(&state->contentlist);
     152         236 :         tommy_list_init(&state->filterlist);
     153         236 :         tommy_list_init(&state->importlist);
     154         236 :         tommy_hashdyn_init(&state->importset);
     155         236 :         tommy_hashdyn_init(&state->previmportset);
     156         236 :         tommy_hashdyn_init(&state->searchset);
     157         236 :         tommy_arrayblkof_init(&state->infoarr, sizeof(snapraid_info));
     158         236 : }
     159             : 
     160         218 : void state_done(struct snapraid_state* state)
     161             : {
     162         218 :         tommy_list_foreach(&state->disklist, (tommy_foreach_func*)disk_free);
     163         218 :         tommy_list_foreach(&state->maplist, (tommy_foreach_func*)map_free);
     164         218 :         tommy_list_foreach(&state->contentlist, (tommy_foreach_func*)content_free);
     165         218 :         tommy_list_foreach(&state->filterlist, (tommy_foreach_func*)filter_free);
     166         218 :         tommy_list_foreach(&state->importlist, (tommy_foreach_func*)import_file_free);
     167         218 :         tommy_hashdyn_foreach(&state->searchset, (tommy_foreach_func*)search_file_free);
     168         218 :         tommy_hashdyn_done(&state->importset);
     169         218 :         tommy_hashdyn_done(&state->previmportset);
     170         218 :         tommy_hashdyn_done(&state->searchset);
     171         218 :         tommy_arrayblkof_done(&state->infoarr);
     172         218 : }
     173             : 
     174             : /**
     175             :  * Checks the configuration.
     176             :  */
     177         235 : static void state_config_check(struct snapraid_state* state, const char* path, tommy_list* filterlist_disk)
     178             : {
     179             :         tommy_node* i;
     180             :         unsigned l;
     181             : 
     182             :         /* check for parity level */
     183         235 :         if (state->raid_mode == RAID_MODE_VANDERMONDE) {
     184           0 :                 if (state->level > 3) {
     185             :                         /* LCOV_EXCL_START */
     186             :                         log_fatal("If you use the z-parity you cannot have more than 3 parities.\n");
     187             :                         exit(EXIT_FAILURE);
     188             :                         /* LCOV_EXCL_STIO */
     189             :                 }
     190             :         }
     191             : 
     192             :         for (l = 0; l < state->level; ++l) {
     193             :                 if (state->parity[l].path[0] == 0) {
     194             :                         /* LCOV_EXCL_START */
     195             :                         log_fatal("No '%s' specification in '%s'\n", lev_config_name(l), path);
     196             :                         exit(EXIT_FAILURE);
     197             :                         /* LCOV_EXCL_STOP */
     198             :                 }
     199             :         }
     200             : 
     201         235 :         if (tommy_list_empty(&state->contentlist)) {
     202             :                 /* LCOV_EXCL_START */
     203             :                 log_fatal("No 'content' specification in '%s'\n", path);
     204             :                 exit(EXIT_FAILURE);
     205             :                 /* LCOV_EXCL_STOP */
     206             :         }
     207             : 
     208             :         /* checks for equal paths */
     209        1596 :         for (i = state->contentlist; i != 0; i = i->next) {
     210        1361 :                 struct snapraid_content* content = i->data;
     211             : 
     212        8807 :                 for (l = 0; l < state->level; ++l) {
     213        7446 :                         if (pathcmp(state->parity[l].path, content->content) == 0) {
     214             :                                 /* LCOV_EXCL_START */
     215             :                                 log_fatal("Same path used for '%s' and 'content' as '%s'\n", lev_config_name(l), content->content);
     216             :                                 exit(EXIT_FAILURE);
     217             :                                 /* LCOV_EXCL_STOP */
     218             :                         }
     219             :                 }
     220             :         }
     221             : 
     222             :         /* check device of data disks */
     223         235 :         if (!state->opt.skip_device && !state->opt.skip_disk_access) {
     224           0 :                 for (i = state->disklist; i != 0; i = i->next) {
     225             :                         tommy_node* j;
     226           0 :                         struct snapraid_disk* disk = i->data;
     227             : 
     228             : #ifdef _WIN32
     229             :                         if (disk->device == 0) {
     230             :                                 /* LCOV_EXCL_START */
     231             :                                 log_fatal("Disk '%s' has a zero serial number.\n", disk->dir);
     232             :                                 log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
     233             :                                 log_fatal("it's better to change the serial number of the disk.\n");
     234             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     235             :                                 exit(EXIT_FAILURE);
     236             :                                 /* LCOV_EXCL_STOP */
     237             :                         }
     238             : #endif
     239             : 
     240           0 :                         if (!state->opt.force_device) {
     241           0 :                                 for (j = i->next; j != 0; j = j->next) {
     242           0 :                                         struct snapraid_disk* other = j->data;
     243           0 :                                         if (disk->device == other->device) {
     244             :                                                 /* LCOV_EXCL_START */
     245             :                                                 log_fatal("Disks '%s' and '%s' are on the same device.\n", disk->dir, other->dir);
     246             : #ifdef _WIN32
     247             :                                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     248             :                                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     249             :                                                 log_fatal("to change one of the disk serial.\n");
     250             : #endif
     251             :                                                 /* in "fix" we allow to continue anyway */
     252             :                                                 if (strcmp(state->command, "fix") == 0) {
     253             :                                                         log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
     254             :                                                 }
     255             :                                                 exit(EXIT_FAILURE);
     256             :                                                 /* LCOV_EXCL_STOP */
     257             :                                         }
     258             :                                 }
     259             :                         }
     260             : 
     261           0 :                         if (!state->opt.skip_parity_access) {
     262           0 :                                 for (l = 0; l < state->level; ++l) {
     263           0 :                                         if (disk->device == state->parity[l].device) {
     264             :                                                 /* LCOV_EXCL_START */
     265             :                                                 log_fatal("Disk '%s' and %s '%s' are on the same device.\n", disk->dir, lev_name(l), state->parity[l].path);
     266             : #ifdef _WIN32
     267             :                                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     268             :                                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     269             :                                                 log_fatal("to change one of the disk serial.\n");
     270             : #endif
     271             :                                                 exit(EXIT_FAILURE);
     272             :                                                 /* LCOV_EXCL_STOP */
     273             :                                         }
     274             :                                 }
     275             :                         }
     276             : 
     277           0 :                         if (state->pool[0] != 0 && disk->device == state->pool_device) {
     278             :                                 /* LCOV_EXCL_START */
     279             :                                 log_fatal("Disk '%s' and pool '%s' are on the same device.\n", disk->dir, state->pool);
     280             : #ifdef _WIN32
     281             :                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     282             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     283             :                                 log_fatal("to change one of the disk serial.\n");
     284             : #endif
     285             :                                 exit(EXIT_FAILURE);
     286             :                                 /* LCOV_EXCL_STOP */
     287             :                         }
     288             :                 }
     289             :         }
     290             : 
     291             :         /* check device of parity disks */
     292         235 :         if (!state->opt.skip_device && !state->opt.skip_parity_access) {
     293             : #ifdef _WIN32
     294             :                 for (l = 0; l < state->level; ++l) {
     295             :                         if (state->parity[l].device == 0) {
     296             :                                 /* LCOV_EXCL_START */
     297             :                                 log_fatal("Disk '%s' has a zero serial number.\n", state->parity[l].path);
     298             :                                 log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
     299             :                                 log_fatal("it's better to change the serial number of the disk.\n");
     300             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     301             :                                 exit(EXIT_FAILURE);
     302             :                                 /* LCOV_EXCL_STOP */
     303             :                         }
     304             :                 }
     305             : #endif
     306             : 
     307           0 :                 for (l = 0; l < state->level; ++l) {
     308             :                         unsigned j;
     309           0 :                         for (j = l + 1; j < state->level; ++j) {
     310           0 :                                 if (state->parity[l].device == state->parity[j].device) {
     311             :                                         /* LCOV_EXCL_START */
     312             :                                         log_fatal("Parity '%s' and '%s' are on the same device.\n", state->parity[l].path, state->parity[j].path);
     313             : #ifdef _WIN32
     314             :                                         log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->parity[l].device);
     315             :                                         log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     316             :                                         log_fatal("to change one of the disk serial.\n");
     317             : #endif
     318             :                                         exit(EXIT_FAILURE);
     319             :                                         /* LCOV_EXCL_STOP */
     320             :                                 }
     321             :                         }
     322             :                 }
     323             : 
     324           0 :                 for (l = 0; l < state->level; ++l) {
     325           0 :                         if (state->pool[0] != 0 && state->pool_device == state->parity[l].device) {
     326             :                                 /* LCOV_EXCL_START */
     327             :                                 log_fatal("Pool '%s' and parity '%s' are on the same device.\n", state->pool, state->parity[l].path);
     328             : #ifdef _WIN32
     329             :                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->pool_device);
     330             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     331             :                                 log_fatal("to change one of the disk serial.\n");
     332             : #endif
     333             :                                 exit(EXIT_FAILURE);
     334             :                                 /* LCOV_EXCL_STOP */
     335             :                         }
     336             :                 }
     337             :         }
     338             : 
     339             :         /* check device of pool disk */
     340             : #ifdef _WIN32
     341             :         if (!state->opt.skip_device) {
     342             :                 if (state->pool[0] != 0 && state->pool_device == 0) {
     343             :                         /* LCOV_EXCL_START */
     344             :                         log_fatal("Disk '%s' has a zero serial number.\n", state->pool);
     345             :                         log_fatal("This is not necessarely wrong, but for using SnapRAID\n");
     346             :                         log_fatal("it's better to change the serial number of the disk.\n");
     347             :                         log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     348             :                         exit(EXIT_FAILURE);
     349             :                         /* LCOV_EXCL_STOP */
     350             :                 }
     351             :         }
     352             : #endif
     353             : 
     354             :         /* count the content files */
     355         235 :         if (!state->opt.skip_device) {
     356             :                 unsigned content_count;
     357             : 
     358           0 :                 content_count = 0;
     359           0 :                 for (i = state->contentlist; i != 0; i = i->next) {
     360             :                         tommy_node* j;
     361           0 :                         struct snapraid_content* content = i->data;
     362             : 
     363             :                         /* check if there are others in the same disk */
     364           0 :                         for (j = i->next; j != 0; j = j->next) {
     365           0 :                                 struct snapraid_content* other = j->data;
     366           0 :                                 if (content->device == other->device) {
     367           0 :                                         break;
     368             :                                 }
     369             :                         }
     370           0 :                         if (j != 0) {
     371             :                                 /* skip it */
     372           0 :                                 continue;
     373             :                         }
     374             : 
     375           0 :                         ++content_count;
     376             :                 }
     377             : 
     378           0 :                 if (content_count < state->level + 1) {
     379             :                         /* LCOV_EXCL_START */
     380             :                         log_fatal("You must have at least %d 'content' files in different disks.\n", state->level + 1);
     381             :                         exit(EXIT_FAILURE);
     382             :                         /* LCOV_EXCL_STOP */
     383             :                 }
     384             :         }
     385             : 
     386             :         /* check for speed */
     387             : #ifdef CONFIG_X86
     388         235 :         if (!raid_cpu_has_ssse3())
     389             : #endif
     390           0 :         if (state->raid_mode == RAID_MODE_CAUCHY) {
     391           0 :                 if (state->level == 3) {
     392           0 :                         log_fatal("WARNING! Your CPU doesn't have a fast implementation for triple parity.\n");
     393           0 :                         log_fatal("WARNING! It's recommended to switch to 'z-parity' instead than '3-parity'.\n");
     394           0 :                 } else if (state->level > 3) {
     395           0 :                         log_fatal("WARNING! Your CPU doesn't have a fast implementation beyond triple parity.\n");
     396           0 :                         log_fatal("WARNING! It's recommended to reduce the parity levels to triple parity.\n");
     397             :                 }
     398             :         }
     399             : 
     400             :         /* ensure that specified filter disks are valid ones */
     401         238 :         for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
     402             :                 tommy_node* j;
     403           3 :                 struct snapraid_filter* filter = i->data;
     404           8 :                 for (j = state->disklist; j != 0; j = j->next) {
     405           8 :                         struct snapraid_disk* disk = j->data;
     406           8 :                         if (fnmatch(filter->pattern, disk->name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     407           3 :                                 break;
     408             :                 }
     409           3 :                 if (j == 0) {
     410             :                         /* LCOV_EXCL_START */
     411             :                         log_fatal("Option -d, --filter-disk %s doesn't match any disk.\n", filter->pattern);
     412             :                         exit(EXIT_FAILURE);
     413             :                         /* LCOV_EXCL_STOP */
     414             :                 }
     415             :         }
     416         235 : }
     417             : 
     418             : /**
     419             :  * Validate the smartctl command.
     420             :  *
     421             :  * It must contains only one %s string, and not other % chars.
     422             :  */
     423         324 : static int validate_smartctl(const char* custom)
     424             : {
     425         324 :         const char* s = custom;
     426         324 :         int arg = 0;
     427             : 
     428        2268 :         while (*s) {
     429        1620 :                 if (s[0] == '%' && s[1] == 's') {
     430         162 :                         if (arg) {
     431             :                                 /* LCOV_EXCL_START */
     432             :                                 return -1;
     433             :                                 /* LCOV_EXCL_STOP */
     434             :                         }
     435         162 :                         arg = 1;
     436        1458 :                 } else if (s[0] == '%') {
     437             :                         /* LCOV_EXCL_START */
     438             :                         return -1;
     439             :                         /* LCOV_EXCL_STOP */
     440             :                 }
     441             : 
     442        1620 :                 ++s;
     443             :         }
     444             : 
     445         324 :         return 0;
     446             : }
     447             : 
     448         235 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk)
     449             : {
     450             :         STREAM* f;
     451             :         unsigned line;
     452             :         tommy_node* i;
     453             :         unsigned l;
     454             : 
     455             :         /* copy the options */
     456         235 :         state->opt = *opt;
     457             : 
     458             :         /* if unset, sort by physical order */
     459         235 :         if (!state->opt.force_order)
     460           1 :                 state->opt.force_order = SORT_PHYSICAL;
     461             : 
     462             :         /* adjust file mode */
     463         235 :         if (state->opt.skip_sequential)
     464           1 :                 state->file_mode &= ~MODE_SEQUENTIAL;
     465             : 
     466             :         /* store current command */
     467         235 :         state->command = command;
     468             : 
     469         235 :         log_tag("conf:%s\n", path);
     470             : 
     471         235 :         f = sopen_read(path);
     472         235 :         if (!f) {
     473             :                 /* LCOV_EXCL_START */
     474             :                 if (errno == ENOENT) {
     475             :                         log_fatal("No configuration file found at '%s'\n", path);
     476             :                 } else if (errno == EACCES) {
     477             :                         log_fatal("You do not have rights to access the configuration file '%s'\n", path);
     478             :                 } else {
     479             :                         log_fatal("Error opening the configuration file '%s'. %s.\n", path, strerror(errno));
     480             :                 }
     481             :                 exit(EXIT_FAILURE);
     482             :                 /* LCOV_EXCL_STOP */
     483             :         }
     484             : 
     485         235 :         line = 1;
     486             :         while (1) {
     487             :                 char tag[PATH_MAX];
     488             :                 char buffer[PATH_MAX];
     489             :                 int ret;
     490             :                 int c;
     491             :                 unsigned level;
     492             : 
     493             :                 /* skip initial spaces */
     494        5523 :                 sgetspace(f);
     495             : 
     496             :                 /* read the command */
     497        5523 :                 ret = sgettok(f, tag, sizeof(tag));
     498        5523 :                 if (ret < 0) {
     499             :                         /* LCOV_EXCL_START */
     500             :                         log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
     501             :                         exit(EXIT_FAILURE);
     502             :                         /* LCOV_EXCL_STOP */
     503             :                 }
     504             : 
     505             :                 /* skip spaces after the command */
     506        5523 :                 sgetspace(f);
     507             : 
     508        5523 :                 if (strcmp(tag, "blocksize") == 0
     509             :                         /* block_size is the old format of the option */
     510        5288 :                         || strcmp(tag, "block_size") == 0) {
     511             : 
     512         235 :                         ret = sgetu32(f, &state->block_size);
     513         235 :                         if (ret < 0) {
     514             :                                 /* LCOV_EXCL_START */
     515             :                                 log_fatal("Invalid 'blocksize' specification in '%s' at line %u\n", path, line);
     516             :                                 exit(EXIT_FAILURE);
     517             :                                 /* LCOV_EXCL_STOP */
     518             :                         }
     519         235 :                         if (state->block_size < 1) {
     520             :                                 /* LCOV_EXCL_START */
     521             :                                 log_fatal("Too small 'blocksize' specification in '%s' at line %u\n", path, line);
     522             :                                 exit(EXIT_FAILURE);
     523             :                                 /* LCOV_EXCL_STOP */
     524             :                         }
     525         235 :                         if (state->block_size > 16 * 1024) {
     526             :                                 /* LCOV_EXCL_START */
     527             :                                 log_fatal("Too big 'blocksize' specification in '%s' at line %u\n", path, line);
     528             :                                 exit(EXIT_FAILURE);
     529             :                                 /* LCOV_EXCL_STOP */
     530             :                         }
     531             :                         /* check if it's a power of 2 */
     532         235 :                         if ((state->block_size & (state->block_size - 1)) != 0) {
     533             :                                 /* LCOV_EXCL_START */
     534             :                                 log_fatal("Not power of 2 'blocksize' specification in '%s' at line %u\n", path, line);
     535             :                                 exit(EXIT_FAILURE);
     536             :                                 /* LCOV_EXCL_STOP */
     537             :                         }
     538         235 :                         state->block_size *= 1024;
     539        5288 :                 } else if (lev_config_scan(tag, &level, &state->raid_mode) == 0) {
     540             :                         char device[PATH_MAX];
     541             :                         char* slash;
     542             : 
     543        1126 :                         if (*state->parity[level].path) {
     544             :                                 /* LCOV_EXCL_START */
     545             :                                 log_fatal("Multiple '%s' specification in '%s' at line %u\n", tag, path, line);
     546             :                                 exit(EXIT_FAILURE);
     547             :                                 /* LCOV_EXCL_STOP */
     548             :                         }
     549             : 
     550        1126 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     551        1126 :                         if (ret < 0) {
     552             :                                 /* LCOV_EXCL_START */
     553             :                                 log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
     554             :                                 exit(EXIT_FAILURE);
     555             :                                 /* LCOV_EXCL_STOP */
     556             :                         }
     557             : 
     558        1126 :                         if (!*buffer) {
     559             :                                 /* LCOV_EXCL_START */
     560             :                                 log_fatal("Empty '%s' specification in '%s' at line %u\n", tag, path, line);
     561             :                                 exit(EXIT_FAILURE);
     562             :                                 /* LCOV_EXCL_STOP */
     563             :                         }
     564             : 
     565        1126 :                         pathimport(state->parity[level].path, sizeof(state->parity[level].path), buffer);
     566             : 
     567        1126 :                         if (!state->opt.skip_parity_access) {
     568             :                                 struct stat st;
     569             : 
     570             :                                 /* get the device of the directory containing the parity file */
     571         940 :                                 pathimport(device, sizeof(device), buffer);
     572         940 :                                 slash = strrchr(device, '/');
     573         940 :                                 if (slash)
     574         940 :                                         *slash = 0;
     575             :                                 else
     576           0 :                                         pathcpy(device, sizeof(device), ".");
     577         940 :                                 if (stat(device, &st) != 0) {
     578             :                                         /* LCOV_EXCL_START */
     579             :                                         log_fatal("Error accessing 'parity' dir '%s' specification in '%s' at line %u\n", device, path, line);
     580             :                                         exit(EXIT_FAILURE);
     581             :                                         /* LCOV_EXCL_STOP */
     582             :                                 }
     583             : 
     584         940 :                                 state->parity[level].device = st.st_dev;
     585             :                         } else {
     586             :                                 /* if parity is skipped, uses a fake device */
     587         186 :                                 state->parity[level].device = 0;
     588             :                         }
     589             : 
     590             :                         /* adjust the level */
     591        1126 :                         if (state->level < level + 1)
     592         891 :                                 state->level = level + 1;
     593        4162 :                 } else if (strcmp(tag, "share") == 0) {
     594          33 :                         if (*state->share) {
     595             :                                 /* LCOV_EXCL_START */
     596             :                                 log_fatal("Multiple 'share' specification in '%s' at line %u\n", path, line);
     597             :                                 exit(EXIT_FAILURE);
     598             :                                 /* LCOV_EXCL_STOP */
     599             :                         }
     600             : 
     601          33 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     602          33 :                         if (ret < 0) {
     603             :                                 /* LCOV_EXCL_START */
     604             :                                 log_fatal("Invalid 'share' specification in '%s' at line %u\n", path, line);
     605             :                                 exit(EXIT_FAILURE);
     606             :                                 /* LCOV_EXCL_STOP */
     607             :                         }
     608             : 
     609          33 :                         if (!*buffer) {
     610             :                                 /* LCOV_EXCL_START */
     611             :                                 log_fatal("Empty 'share' specification in '%s' at line %u\n", path, line);
     612             :                                 exit(EXIT_FAILURE);
     613             :                                 /* LCOV_EXCL_STOP */
     614             :                         }
     615             : 
     616          33 :                         pathimport(state->share, sizeof(state->share), buffer);
     617        4129 :                 } else if (strcmp(tag, "pool") == 0) {
     618             :                         struct stat st;
     619             : 
     620          33 :                         if (*state->pool) {
     621             :                                 /* LCOV_EXCL_START */
     622             :                                 log_fatal("Multiple 'pool' specification in '%s' at line %u\n", path, line);
     623             :                                 exit(EXIT_FAILURE);
     624             :                                 /* LCOV_EXCL_STOP */
     625             :                         }
     626             : 
     627          33 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     628          33 :                         if (ret < 0) {
     629             :                                 /* LCOV_EXCL_START */
     630             :                                 log_fatal("Invalid 'pool' specification in '%s' at line %u\n", path, line);
     631             :                                 exit(EXIT_FAILURE);
     632             :                                 /* LCOV_EXCL_STOP */
     633             :                         }
     634             : 
     635          33 :                         if (!*buffer) {
     636             :                                 /* LCOV_EXCL_START */
     637             :                                 log_fatal("Empty 'pool' specification in '%s' at line %u\n", path, line);
     638             :                                 exit(EXIT_FAILURE);
     639             :                                 /* LCOV_EXCL_STOP */
     640             :                         }
     641             : 
     642          33 :                         pathimport(state->pool, sizeof(state->pool), buffer);
     643             : 
     644             :                         /* get the device of the directory containing the pool tree */
     645          33 :                         if (stat(buffer, &st) != 0) {
     646             :                                 /* LCOV_EXCL_START */
     647             :                                 log_fatal("Error accessing 'pool' dir '%s' specification in '%s' at line %u\n", buffer, path, line);
     648             :                                 exit(EXIT_FAILURE);
     649             :                                 /* LCOV_EXCL_STOP */
     650             :                         }
     651             : 
     652          33 :                         state->pool_device = st.st_dev;
     653        4096 :                 } else if (strcmp(tag, "content") == 0) {
     654             :                         struct snapraid_content* content;
     655             :                         char device[PATH_MAX];
     656             :                         char* slash;
     657             :                         struct stat st;
     658             : 
     659        1361 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     660        1361 :                         if (ret < 0) {
     661             :                                 /* LCOV_EXCL_START */
     662             :                                 log_fatal("Invalid 'content' specification in '%s' at line %u\n", path, line);
     663             :                                 exit(EXIT_FAILURE);
     664             :                                 /* LCOV_EXCL_STOP */
     665             :                         }
     666             : 
     667        1361 :                         if (pathcmp(buffer, "/dev/null") == 0 || pathcmp(buffer, "NUL") == 0) {
     668             :                                 /* LCOV_EXCL_START */
     669             :                                 log_fatal("You cannot use the null device as 'content' specification in '%s' at line %u\n", path, line);
     670             :                                 exit(EXIT_FAILURE);
     671             :                                 /* LCOV_EXCL_STOP */
     672             :                         }
     673             : 
     674        1361 :                         if (!*buffer) {
     675             :                                 /* LCOV_EXCL_START */
     676             :                                 log_fatal("Empty 'content' specification in '%s' at line %u\n", path, line);
     677             :                                 exit(EXIT_FAILURE);
     678             :                                 /* LCOV_EXCL_STOP */
     679             :                         }
     680             : 
     681             :                         /* check if the content file is already specified */
     682        5084 :                         for (i = state->contentlist; i != 0; i = i->next) {
     683        3723 :                                 content = i->data;
     684        3723 :                                 if (pathcmp(content->content, buffer) == 0)
     685           0 :                                         break;
     686             :                         }
     687        1361 :                         if (i) {
     688             :                                 /* LCOV_EXCL_START */
     689             :                                 log_fatal("Duplicate 'content' specification in '%s' at line %u\n", path, line);
     690             :                                 exit(EXIT_FAILURE);
     691             :                                 /* LCOV_EXCL_STOP */
     692             :                         }
     693             : 
     694             :                         /* get the device of the directory containing the content file */
     695        1361 :                         pathimport(device, sizeof(device), buffer);
     696        1361 :                         slash = strrchr(device, '/');
     697        1361 :                         if (slash)
     698        1361 :                                 *slash = 0;
     699             :                         else
     700           0 :                                 pathcpy(device, sizeof(device), ".");
     701        1361 :                         if (stat(device, &st) != 0) {
     702             :                                 /* LCOV_EXCL_START */
     703             :                                 log_fatal("Error accessing 'content' dir '%s' specification in '%s' at line %u\n", device, path, line);
     704             :                                 exit(EXIT_FAILURE);
     705             :                                 /* LCOV_EXCL_STOP */
     706             :                         }
     707             : 
     708             :                         /* set the lock file at the first content file */
     709        1361 :                         if (tommy_list_empty(&state->contentlist)) {
     710         235 :                                 pathcpy(state->lockfile, sizeof(state->lockfile), buffer);
     711         235 :                                 pathcat(state->lockfile, sizeof(state->lockfile), ".lock");
     712             :                         }
     713             : 
     714        1361 :                         content = content_alloc(buffer, st.st_dev);
     715             : 
     716        1361 :                         tommy_list_insert_tail(&state->contentlist, &content->node, content);
     717        2735 :                 } else if (strcmp(tag, "disk") == 0) {
     718             :                         char dir[PATH_MAX];
     719             :                         char device[PATH_MAX];
     720             :                         struct snapraid_disk* disk;
     721             :                         uint64_t dev;
     722             : 
     723        1405 :                         ret = sgettok(f, buffer, sizeof(buffer));
     724        1405 :                         if (ret < 0) {
     725             :                                 /* LCOV_EXCL_START */
     726             :                                 log_fatal("Invalid 'disk' name specification in '%s' at line %u\n", path, line);
     727             :                                 exit(EXIT_FAILURE);
     728             :                                 /* LCOV_EXCL_STOP */
     729             :                         }
     730             : 
     731        1405 :                         if (!*buffer) {
     732             :                                 /* LCOV_EXCL_START */
     733             :                                 log_fatal("Empty 'disk' name specification in '%s' at line %u\n", path, line);
     734             :                                 exit(EXIT_FAILURE);
     735             :                                 /* LCOV_EXCL_STOP */
     736             :                         }
     737             : 
     738        1405 :                         sgetspace(f);
     739             : 
     740        1405 :                         ret = sgetlasttok(f, dir, sizeof(dir));
     741        1405 :                         if (ret < 0) {
     742             :                                 /* LCOV_EXCL_START */
     743             :                                 log_fatal("Invalid 'disk' dir specification in '%s' at line %u\n", path, line);
     744             :                                 exit(EXIT_FAILURE);
     745             :                                 /* LCOV_EXCL_STOP */
     746             :                         }
     747             : 
     748        1405 :                         if (!*dir) {
     749             :                                 /* LCOV_EXCL_START */
     750             :                                 log_fatal("Empty 'disk' dir specification in '%s' at line %u\n", path, line);
     751             :                                 exit(EXIT_FAILURE);
     752             :                                 /* LCOV_EXCL_STOP */
     753             :                         }
     754             : 
     755             :                         /* get the device of the dir */
     756        1405 :                         pathimport(device, sizeof(device), dir);
     757             : 
     758             :                         /* check if the disk name already exists */
     759        4905 :                         for (i = state->disklist; i != 0; i = i->next) {
     760        3500 :                                 disk = i->data;
     761        3500 :                                 if (strcmp(disk->name, buffer) == 0)
     762           0 :                                         break;
     763             :                         }
     764        1405 :                         if (i) {
     765             :                                 /* LCOV_EXCL_START */
     766             :                                 log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
     767             :                                 exit(EXIT_FAILURE);
     768             :                                 /* LCOV_EXCL_STOP */
     769             :                         }
     770             : 
     771        1405 :                         if (!state->opt.skip_disk_access) {
     772             :                                 struct stat st;
     773             : 
     774        1249 :                                 if (stat(device, &st) != 0) {
     775             :                                         /* LCOV_EXCL_START */
     776             :                                         log_fatal("Error accessing 'disk' '%s' specification in '%s' at line %u\n", dir, device, line);
     777             :                                         exit(EXIT_FAILURE);
     778             :                                         /* LCOV_EXCL_STOP */
     779             :                                 }
     780             : 
     781        1249 :                                 dev = st.st_dev;
     782             :                         } else {
     783             :                                 /* if data disks are skipped, uses a fake device */
     784         156 :                                 dev = 0;
     785             :                         }
     786             : 
     787        1405 :                         disk = disk_alloc(buffer, dir, dev);
     788             : 
     789        1405 :                         tommy_list_insert_tail(&state->disklist, &disk->node, disk);
     790        1330 :                 } else if (strcmp(tag, "smartctl") == 0) {
     791             :                         char custom[PATH_MAX];
     792             : 
     793         324 :                         ret = sgettok(f, buffer, sizeof(buffer));
     794         324 :                         if (ret < 0) {
     795             :                                 /* LCOV_EXCL_START */
     796             :                                 log_fatal("Invalid 'smartctl' name specification in '%s' at line %u\n", path, line);
     797             :                                 exit(EXIT_FAILURE);
     798             :                                 /* LCOV_EXCL_STOP */
     799             :                         }
     800             : 
     801         324 :                         if (!*buffer) {
     802             :                                 /* LCOV_EXCL_START */
     803             :                                 log_fatal("Empty 'smartctl' name specification in '%s' at line %u\n", path, line);
     804             :                                 exit(EXIT_FAILURE);
     805             :                                 /* LCOV_EXCL_STOP */
     806             :                         }
     807             : 
     808         324 :                         sgetspace(f);
     809             : 
     810         324 :                         ret = sgetlasttok(f, custom, sizeof(custom));
     811         324 :                         if (ret < 0) {
     812             :                                 /* LCOV_EXCL_START */
     813             :                                 log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
     814             :                                 exit(EXIT_FAILURE);
     815             :                                 /* LCOV_EXCL_STOP */
     816             :                         }
     817             : 
     818         324 :                         if (!*custom) {
     819             :                                 /* LCOV_EXCL_START */
     820             :                                 log_fatal("Empty 'smartctl' option specification in '%s' at line %u\n", path, line);
     821             :                                 exit(EXIT_FAILURE);
     822             :                                 /* LCOV_EXCL_STOP */
     823             :                         }
     824             : 
     825         324 :                         if (validate_smartctl(custom) != 0) {
     826             :                                 /* LCOV_EXCL_START */
     827             :                                 log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
     828             :                                 exit(EXIT_FAILURE);
     829             :                                 /* LCOV_EXCL_STOP */
     830             :                         }
     831             : 
     832             :                         /* search for parity */
     833         324 :                         if (lev_config_scan(buffer, &level, 0) == 0) {
     834         162 :                                 if (state->parity[level].smartctl[0] != 0) {
     835             :                                         /* LCOV_EXCL_START */
     836             :                                         log_fatal("Duplicate parity smartctl '%s' at line %u\n", buffer, line);
     837             :                                         exit(EXIT_FAILURE);
     838             :                                         /* LCOV_EXCL_STOP */
     839             :                                 }
     840             : 
     841         162 :                                 pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), custom);
     842             :                         } else {
     843             :                                 /* search the disk */
     844             :                                 struct snapraid_disk* disk;
     845         162 :                                 for (i = state->disklist; i != 0; i = i->next) {
     846         162 :                                         disk = i->data;
     847         162 :                                         if (strcmp(disk->name, buffer) == 0)
     848         162 :                                                 break;
     849             :                                 }
     850         162 :                                 if (!i) {
     851             :                                         /* LCOV_EXCL_START */
     852             :                                         log_fatal("Missing disk smartctl '%s' at line %u\n", buffer, line);
     853             :                                         exit(EXIT_FAILURE);
     854             :                                         /* LCOV_EXCL_STOP */
     855             :                                 }
     856         162 :                                 if (disk->smartctl[0] != 0) {
     857             :                                         /* LCOV_EXCL_START */
     858             :                                         log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
     859             :                                         exit(EXIT_FAILURE);
     860             :                                         /* LCOV_EXCL_STOP */
     861             :                                 }
     862             : 
     863         162 :                                 pathcpy(disk->smartctl, sizeof(disk->smartctl), custom);
     864             :                         }
     865        1006 :                 } else if (strcmp(tag, "nohidden") == 0) {
     866           0 :                         state->filter_hidden = 1;
     867        1006 :                 } else if (strcmp(tag, "exclude") == 0) {
     868             :                         struct snapraid_filter* filter;
     869             : 
     870         235 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     871         235 :                         if (ret < 0) {
     872             :                                 /* LCOV_EXCL_START */
     873             :                                 log_fatal("Invalid 'exclude' specification in '%s' at line %u\n", path, line);
     874             :                                 exit(EXIT_FAILURE);
     875             :                                 /* LCOV_EXCL_STOP */
     876             :                         }
     877             : 
     878         235 :                         if (!*buffer) {
     879             :                                 /* LCOV_EXCL_START */
     880             :                                 log_fatal("Empty 'exclude' specification in '%s' at line %u\n", path, line);
     881             :                                 exit(EXIT_FAILURE);
     882             :                                 /* LCOV_EXCL_STOP */
     883             :                         }
     884             : 
     885         235 :                         filter = filter_alloc_file(-1, buffer);
     886         235 :                         if (!filter) {
     887             :                                 /* LCOV_EXCL_START */
     888             :                                 log_fatal("Invalid 'exclude' specification '%s' in '%s' at line %u\n", buffer, path, line);
     889             :                                 exit(EXIT_FAILURE);
     890             :                                 /* LCOV_EXCL_STOP */
     891             :                         }
     892         235 :                         tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
     893         771 :                 } else if (strcmp(tag, "include") == 0) {
     894             :                         struct snapraid_filter* filter;
     895             : 
     896         235 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     897         235 :                         if (ret < 0) {
     898             :                                 /* LCOV_EXCL_START */
     899             :                                 log_fatal("Invalid 'include' specification in '%s' at line %u\n", path, line);
     900             :                                 exit(EXIT_FAILURE);
     901             :                                 /* LCOV_EXCL_STOP */
     902             :                         }
     903             : 
     904         235 :                         if (!*buffer) {
     905             :                                 /* LCOV_EXCL_START */
     906             :                                 log_fatal("Empty 'include' specification in '%s' at line %u\n", path, line);
     907             :                                 exit(EXIT_FAILURE);
     908             :                                 /* LCOV_EXCL_STOP */
     909             :                         }
     910             : 
     911         235 :                         filter = filter_alloc_file(1, buffer);
     912         235 :                         if (!filter) {
     913             :                                 /* LCOV_EXCL_START */
     914             :                                 log_fatal("Invalid 'include' specification '%s' in '%s' at line %u\n", buffer, path, line);
     915             :                                 exit(EXIT_FAILURE);
     916             :                                 /* LCOV_EXCL_STOP */
     917             :                         }
     918         235 :                         tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
     919         536 :                 } else if (strcmp(tag, "autosave") == 0) {
     920             :                         char* e;
     921             : 
     922          33 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     923          33 :                         if (ret < 0) {
     924             :                                 /* LCOV_EXCL_START */
     925             :                                 log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
     926             :                                 exit(EXIT_FAILURE);
     927             :                                 /* LCOV_EXCL_STOP */
     928             :                         }
     929             : 
     930          33 :                         if (!*buffer) {
     931             :                                 /* LCOV_EXCL_START */
     932             :                                 log_fatal("Empty 'autosave' specification in '%s' at line %u\n", path, line);
     933             :                                 exit(EXIT_FAILURE);
     934             :                                 /* LCOV_EXCL_STOP */
     935             :                         }
     936             : 
     937          33 :                         state->autosave = strtoul(buffer, &e, 0);
     938             : 
     939          33 :                         if (!e || *e) {
     940             :                                 /* LCOV_EXCL_START */
     941             :                                 log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
     942             :                                 exit(EXIT_FAILURE);
     943             :                                 /* LCOV_EXCL_STOP */
     944             :                         }
     945             : 
     946             :                         /* convert to GB */
     947          33 :                         state->autosave *= GIGA;
     948         503 :                 } else if (tag[0] == 0) {
     949             :                         /* allow empty lines */
     950          33 :                 } else if (tag[0] == '#') {
     951          33 :                         ret = sgetline(f, buffer, sizeof(buffer));
     952          33 :                         if (ret < 0) {
     953             :                                 /* LCOV_EXCL_START */
     954             :                                 log_fatal("Invalid comment in '%s' at line %u\n", path, line);
     955             :                                 exit(EXIT_FAILURE);
     956             :                                 /* LCOV_EXCL_STOP */
     957             :                         }
     958             :                 } else {
     959             :                         /* LCOV_EXCL_START */
     960             :                         log_fatal("Invalid command '%s' in '%s' at line %u\n", tag, path, line);
     961             :                         exit(EXIT_FAILURE);
     962             :                         /* LCOV_EXCL_STOP */
     963             :                 }
     964             : 
     965             :                 /* skip final spaces */
     966        5523 :                 sgetspace(f);
     967             : 
     968             :                 /* next line */
     969        5523 :                 c = sgeteol(f);
     970        5523 :                 if (c == EOF) {
     971         235 :                         break;
     972             :                 }
     973        5288 :                 if (c != '\n') {
     974             :                         /* LCOV_EXCL_START */
     975             :                         log_fatal("Extra data in '%s' at line %u\n", path, line);
     976             :                         exit(EXIT_FAILURE);
     977             :                         /* LCOV_EXCL_STOP */
     978             :                 }
     979        5288 :                 ++line;
     980        5288 :         }
     981             : 
     982         235 :         if (serror(f)) {
     983             :                 /* LCOV_EXCL_START */
     984             :                 log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
     985             :                 exit(EXIT_FAILURE);
     986             :                 /* LCOV_EXCL_STOP */
     987             :         }
     988             : 
     989         235 :         sclose(f);
     990             : 
     991         235 :         state_config_check(state, path, filterlist_disk);
     992             : 
     993             :         /* select the default hash */
     994         235 :         if (state->opt.force_murmur3) {
     995           1 :                 state->besthash = HASH_MURMUR3;
     996         234 :         } else if (state->opt.force_spooky2) {
     997           1 :                 state->besthash = HASH_SPOOKY2;
     998             :         } else {
     999             : #ifdef CONFIG_X86
    1000         233 :                 if (sizeof(void*) == 4 && !raid_cpu_has_slowmult())
    1001         233 :                         state->besthash = HASH_MURMUR3;
    1002             :                 else
    1003           0 :                         state->besthash = HASH_SPOOKY2;
    1004             : #else
    1005             :                 if (sizeof(void*) == 4)
    1006             :                         state->besthash = HASH_MURMUR3;
    1007             :                 else
    1008             :                         state->besthash = HASH_SPOOKY2;
    1009             : #endif
    1010             :         }
    1011             : 
    1012             :         /* by default use the best hash */
    1013         235 :         state->hash = state->besthash;
    1014             : 
    1015             :         /* by default use a random hash seed */
    1016         235 :         if (randomize(state->hashseed, HASH_SIZE) != 0) {
    1017             :                 /* LCOV_EXCL_START */
    1018             :                 log_fatal("Failed to get random values.\n");
    1019             :                 exit(EXIT_FAILURE);
    1020             :                 /* LCOV_EXCL_STOP */
    1021             :         }
    1022             : 
    1023             :         /* no previous hash by default */
    1024         235 :         state->prevhash = HASH_UNDEFINED;
    1025             : 
    1026             :         /* intentionally not set the prevhashseed, if used valgrind will warn about it */
    1027             : 
    1028         235 :         log_tag("blocksize:%u\n", state->block_size);
    1029        1640 :         for (i = state->disklist; i != 0; i = i->next) {
    1030        1405 :                 struct snapraid_disk* disk = i->data;
    1031        1405 :                 log_tag("disk:%s:%s\n", disk->name, disk->dir);
    1032             :         }
    1033             : 
    1034         235 :         log_tag("mode:%s\n", lev_raid_name(state->raid_mode, state->level));
    1035        1361 :         for (l = 0; l < state->level; ++l)
    1036        1126 :                 log_tag("%s:%s\n", lev_config_name(l), state->parity[l].path);
    1037         235 :         if (state->pool[0] != 0)
    1038          33 :                 log_tag("pool:%s\n", state->pool);
    1039         235 :         if (state->share[0] != 0)
    1040          33 :                 log_tag("share:%s\n", state->share);
    1041         235 :         if (state->autosave != 0)
    1042          33 :                 log_tag("autosave:%" PRIu64 "\n", state->autosave);
    1043         705 :         for (i = tommy_list_head(&state->filterlist); i != 0; i = i->next) {
    1044             :                 char out[PATH_MAX];
    1045         470 :                 struct snapraid_filter* filter = i->data;
    1046         470 :                 log_tag("filter:%s\n", filter_type(filter, out, sizeof(out)));
    1047             :         }
    1048         235 :         if (state->filter_hidden)
    1049           0 :                 log_tag("filter:nohidden:\n");
    1050         235 :         log_flush();
    1051         235 : }
    1052             : 
    1053             : /**
    1054             :  * Finds a disk by name.
    1055             :  */
    1056      102384 : static struct snapraid_disk* find_disk(struct snapraid_state* state, const char* name)
    1057             : {
    1058             :         tommy_node* i;
    1059             : 
    1060      359771 :         for (i = state->disklist; i != 0; i = i->next) {
    1061      359765 :                 struct snapraid_disk* disk = i->data;
    1062      359765 :                 if (strcmp(disk->name, name) == 0)
    1063      102378 :                         return disk;
    1064             :         }
    1065             : 
    1066           6 :         if (state->no_conf) {
    1067             :                 /* without a configuration file, add disks automatically */
    1068             :                 struct snapraid_disk* disk;
    1069             : 
    1070           6 :                 disk = disk_alloc(name, "DUMMY/", -1);
    1071             : 
    1072           6 :                 tommy_list_insert_tail(&state->disklist, &disk->node, disk);
    1073             : 
    1074           6 :                 return disk;
    1075             :         }
    1076             : 
    1077           0 :         return 0;
    1078             : }
    1079             : 
    1080             : /**
    1081             :  * Updates the disk mapping if required.
    1082             :  */
    1083         230 : static void state_map(struct snapraid_state* state)
    1084             : {
    1085             :         unsigned hole;
    1086             :         tommy_node* i;
    1087             :         unsigned uuid_mismatch;
    1088             :         unsigned diskcount;
    1089             :         unsigned l;
    1090             : 
    1091             :         /* removes all the mapping without a disk */
    1092             :         /* this happens when a disk is removed from the configuration file */
    1093             :         /* From SnapRAID 4.0 mappings are automatically removed if a disk is not used */
    1094             :         /* when saving the content file, but we keep this code to import older content files. */
    1095        1731 :         for (i = state->maplist; i != 0; ) {
    1096        1271 :                 struct snapraid_map* map = i->data;
    1097             :                 struct snapraid_disk* disk;
    1098             : 
    1099        1271 :                 disk = find_disk(state, map->name);
    1100             : 
    1101             :                 /* go to the next mapping before removing */
    1102        1271 :                 i = i->next;
    1103             : 
    1104        1271 :                 if (disk == 0) {
    1105             :                         /* disk not found, remove the mapping */
    1106           0 :                         tommy_list_remove_existing(&state->maplist, &map->node);
    1107           0 :                         map_free(map);
    1108             :                 }
    1109             :         }
    1110             : 
    1111             :         /* maps each unmapped disk present in the configuration file in the first available hole */
    1112             :         /* this happens when you add disks for the first time in the configuration file */
    1113         230 :         hole = 0; /* first position to try */
    1114        1605 :         for (i = state->disklist; i != 0; i = i->next) {
    1115        1375 :                 struct snapraid_disk* disk = i->data;
    1116             :                 struct snapraid_map* map;
    1117             :                 tommy_node* j;
    1118             : 
    1119             :                 /* check if the disk is already mapped */
    1120        4800 :                 for (j = state->maplist; j != 0; j = j->next) {
    1121        4696 :                         map = j->data;
    1122        4696 :                         if (strcmp(disk->name, map->name) == 0) {
    1123             :                                 /* mapping found */
    1124        1271 :                                 break;
    1125             :                         }
    1126             :                 }
    1127        1375 :                 if (j != 0) {
    1128             :                         /* mapping is present, then copy the free blocks into to disk */
    1129        1271 :                         disk->total_blocks = map->total_blocks;
    1130        1271 :                         disk->free_blocks = map->free_blocks;
    1131        1271 :                         continue;
    1132             :                 }
    1133             : 
    1134             :                 /* mapping not found, search for an hole */
    1135             :                 while (1) {
    1136         626 :                         for (j = state->maplist; j != 0; j = j->next) {
    1137         522 :                                 map = j->data;
    1138         522 :                                 if (map->position == hole) {
    1139             :                                         /* position already used */
    1140          87 :                                         break;
    1141             :                                 }
    1142             :                         }
    1143         191 :                         if (j == 0) {
    1144             :                                 /* hole found */
    1145         104 :                                 break;
    1146             :                         }
    1147             : 
    1148             :                         /* try with the next one */
    1149          87 :                         ++hole;
    1150          87 :                 }
    1151             : 
    1152             :                 /* insert the new mapping */
    1153         104 :                 map = map_alloc(disk->name, hole, 0, 0, "");
    1154             : 
    1155         104 :                 tommy_list_insert_tail(&state->maplist, &map->node, map);
    1156             :         }
    1157             : 
    1158             :         /* without configuration don't check for number of data disks or uuid changes */
    1159         230 :         if (state->no_conf)
    1160         231 :                 return;
    1161             : 
    1162             :         /* counter for the number of UUID mismatches */
    1163         229 :         uuid_mismatch = 0;
    1164             : 
    1165             :         /* checks if mapping match the disk uuid */
    1166         229 :         if (!state->opt.skip_disk_access) {
    1167        1423 :                 for (i = state->maplist; i != 0; i = i->next) {
    1168        1219 :                         struct snapraid_map* map = i->data;
    1169             :                         struct snapraid_disk* disk;
    1170             :                         char uuid[UUID_MAX];
    1171             :                         int ret;
    1172             : 
    1173        1219 :                         disk = find_disk(state, map->name);
    1174        1219 :                         if (disk == 0) {
    1175             :                                 /* LCOV_EXCL_START */
    1176             :                                 log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
    1177             :                                 exit(EXIT_FAILURE);
    1178             :                                 /* LCOV_EXCL_STOP */
    1179             :                         }
    1180             : 
    1181        1219 :                         ret = devuuid(disk->device, uuid, sizeof(uuid));
    1182        1219 :                         if (ret != 0) {
    1183             :                                 /* uuid not available, just ignore but marks the disk with unsupported UUID */
    1184           0 :                                 disk->has_unsupported_uuid = 1;
    1185           0 :                                 continue;
    1186             :                         }
    1187             : 
    1188             :                         /* if the uuid is changed */
    1189        1219 :                         if (strcmp(uuid, map->uuid) != 0) {
    1190             :                                 /* mark the disk as with an UUID change */
    1191          56 :                                 disk->has_different_uuid = 1;
    1192             : 
    1193             :                                 /* if the previous uuid is available */
    1194          56 :                                 if (map->uuid[0] != 0) {
    1195             :                                         /* count the number of uuid change */
    1196           0 :                                         ++uuid_mismatch;
    1197           0 :                                         log_fatal("UUID change for disk '%s' from '%s' to '%s'\n", disk->name, map->uuid, uuid);
    1198             :                                 } else {
    1199             :                                         /* no message here, because having a disk without */
    1200             :                                         /* UUID is the normal state of an empty disk */
    1201          56 :                                         disk->had_empty_uuid = 1;
    1202             :                                 }
    1203             : 
    1204             :                                 /* update the uuid in the mapping, */
    1205          56 :                                 pathcpy(map->uuid, sizeof(map->uuid), uuid);
    1206             : 
    1207             :                                 /* write the new state with the new uuid */
    1208          56 :                                 state->need_write = 1;
    1209             :                         }
    1210             :                 }
    1211             :         }
    1212             : 
    1213             :         /* checks the parity uuid */
    1214         229 :         if (!state->opt.skip_parity_access) {
    1215        1104 :                 for (l = 0; l < state->level; ++l) {
    1216             :                         char uuid[UUID_MAX];
    1217             :                         int ret;
    1218             : 
    1219         910 :                         ret = devuuid(state->parity[l].device, uuid, sizeof(uuid));
    1220         910 :                         if (ret != 0) {
    1221             :                                 /* uuid not available, just ignore */
    1222           0 :                                 continue;
    1223             :                         }
    1224             : 
    1225             :                         /* if the uuid is changed */
    1226         910 :                         if (strcmp(uuid, state->parity[l].uuid) != 0) {
    1227             :                                 /* if the previous uuid is available */
    1228          42 :                                 if (state->parity[l].uuid[0] != 0) {
    1229             :                                         /* count the number of uuid change */
    1230           0 :                                         ++uuid_mismatch;
    1231           0 :                                         log_fatal("UUID change for parity '%s' from '%s' to '%s'\n", lev_config_name(l), state->parity[l].uuid, uuid);
    1232             :                                 }
    1233             : 
    1234             :                                 /* update the uuid */
    1235          42 :                                 pathcpy(state->parity[l].uuid, sizeof(state->parity[l].uuid), uuid);
    1236             : 
    1237             :                                 /* write the new state with the new uuid */
    1238          42 :                                 state->need_write = 1;
    1239             :                         }
    1240             :                 }
    1241             :         }
    1242             : 
    1243         229 :         if (!state->opt.force_uuid && uuid_mismatch > state->level) {
    1244             :                 /* LCOV_EXCL_START */
    1245             :                 log_fatal("Too many disks have UUID changed from the latest 'sync'.\n");
    1246             :                 log_fatal("If this happens because you really replaced them,\n");
    1247             :                 log_fatal("you can '%s' anyway, using 'snapraid --force-uuid %s'.\n", state->command, state->command);
    1248             :                 log_fatal("Instead, it's possible that you messed up the disk mount points,\n");
    1249             :                 log_fatal("and you have to restore the mount points at the state of the latest sync.\n");
    1250             :                 exit(EXIT_FAILURE);
    1251             :                 /* LCOV_EXCL_STOP */
    1252             :         }
    1253             : 
    1254             :         /* count the number of data disks, including holes left after removing some */
    1255         229 :         diskcount = 0;
    1256        1598 :         for (i = state->maplist; i != 0; i = i->next) {
    1257        1369 :                 struct snapraid_map* map = i->data;
    1258             : 
    1259        1369 :                 if (map->position + 1 > diskcount)
    1260        1234 :                         diskcount = map->position + 1;
    1261             :         }
    1262             : 
    1263             :         /* ensure to don't go over the limit of the RAID engine */
    1264         229 :         if (diskcount > RAID_DATA_MAX) {
    1265             :                 /* LCOV_EXCL_START */
    1266             :                 log_fatal("Too many data disks. No more than %u.\n", RAID_DATA_MAX);
    1267             :                 exit(EXIT_FAILURE);
    1268             :                 /* LCOV_EXCL_STOP */
    1269             :         }
    1270             : 
    1271             :         /* now count the real number of data disks, excluding holes left after removing some */
    1272         229 :         diskcount = tommy_list_count(&state->maplist);
    1273             : 
    1274             :         /* recommend number of parities */
    1275         229 :         if (diskcount >= 36 && state->level < 6) {
    1276           0 :                 log_fatal("WARNING! With %u disks it's recommended to use six parity levels.\n", diskcount);
    1277         229 :         } else if (diskcount >= 29 && state->level < 5) {
    1278           0 :                 log_fatal("WARNING! With %u disks it's recommended to use five parity levels.\n", diskcount);
    1279         229 :         } else if (diskcount >= 22 && state->level < 4) {
    1280           0 :                 log_fatal("WARNING! With %u disks it's recommended to use four parity levels.\n", diskcount);
    1281         229 :         } else if (diskcount >= 15 && state->level < 3) {
    1282           0 :                 log_fatal("WARNING! With %u disks it's recommended to use three parity levels.\n", diskcount);
    1283         229 :         } else if (diskcount >= 5 && state->level < 2) {
    1284          33 :                 log_fatal("WARNING! With %u disks it's recommended to use two parity levels.\n", diskcount);
    1285             :         }
    1286             : }
    1287             : 
    1288         126 : void state_refresh(struct snapraid_state* state)
    1289             : {
    1290             :         tommy_node* i;
    1291             :         unsigned l;
    1292             : 
    1293             :         /* for all disks */
    1294         880 :         for (i = state->maplist; i != 0; i = i->next) {
    1295         754 :                 struct snapraid_map* map = i->data;
    1296             :                 struct snapraid_disk* disk;
    1297             :                 uint64_t total_space;
    1298             :                 uint64_t free_space;
    1299             :                 int ret;
    1300             : 
    1301         754 :                 disk = find_disk(state, map->name);
    1302         754 :                 if (disk == 0) {
    1303             :                         /* LCOV_EXCL_START */
    1304             :                         log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
    1305             :                         exit(EXIT_FAILURE);
    1306             :                         /* LCOV_EXCL_STOP */
    1307             :                 }
    1308             : 
    1309         754 :                 ret = fsinfo(disk->dir, 0, &total_space, &free_space);
    1310         754 :                 if (ret != 0) {
    1311             :                         /* LCOV_EXCL_START */
    1312             :                         log_fatal("Error accessing disk '%s' to get filesystem info. %s.\n", disk->dir, strerror(errno));
    1313             :                         exit(EXIT_FAILURE);
    1314             :                         /* LCOV_EXCL_STOP */
    1315             :                 }
    1316             : 
    1317             :                 /* set the new free blocks */
    1318         754 :                 map->total_blocks = total_space / state->block_size;
    1319         754 :                 map->free_blocks = free_space / state->block_size;
    1320             : 
    1321             :                 /* also update the disk info */
    1322         754 :                 disk->total_blocks = map->total_blocks;
    1323         754 :                 disk->free_blocks = map->free_blocks;
    1324             :         }
    1325             : 
    1326             :         /* for all parities */
    1327         862 :         for (l = 0; l < state->level; ++l) {
    1328             :                 uint64_t total_space;
    1329             :                 uint64_t free_space;
    1330             :                 int ret;
    1331             : 
    1332         736 :                 ret = fsinfo(state->parity[l].path, 0, &total_space, &free_space);
    1333         736 :                 if (ret != 0) {
    1334             :                         /* LCOV_EXCL_START */
    1335             :                         log_fatal("Error accessing file '%s' to get filesystem info. %s.\n", state->parity[l].path, strerror(errno));
    1336             :                         exit(EXIT_FAILURE);
    1337             :                         /* LCOV_EXCL_STOP */
    1338             :                 }
    1339             : 
    1340             :                 /* set the new free blocks */
    1341         736 :                 state->parity[l].total_blocks = total_space / state->block_size;
    1342         736 :                 state->parity[l].free_blocks = free_space / state->block_size;
    1343             :         }
    1344             : 
    1345             :         /* note what we don't set need_write = 1, because we don't want */
    1346             :         /* to update the content file only for the free space info. */
    1347         126 : }
    1348             : 
    1349             : /**
    1350             :  * Checks the content.
    1351             :  */
    1352         223 : static void state_content_check(struct snapraid_state* state, const char* path)
    1353             : {
    1354             :         tommy_node* i;
    1355             : 
    1356             :         /* checks that any map has different name and position */
    1357        1556 :         for (i = state->maplist; i != 0; i = i->next) {
    1358        1333 :                 struct snapraid_map* map = i->data;
    1359             :                 tommy_node* j;
    1360        4653 :                 for (j = i->next; j != 0; j = j->next) {
    1361        3320 :                         struct snapraid_map* other = j->data;
    1362        3320 :                         if (strcmp(map->name, other->name) == 0) {
    1363             :                                 /* LCOV_EXCL_START */
    1364             :                                 log_fatal("Colliding 'map' disk specification in '%s'\n", path);
    1365             :                                 exit(EXIT_FAILURE);
    1366             :                                 /* LCOV_EXCL_STOP */
    1367             :                         }
    1368        3320 :                         if (map->position == other->position) {
    1369             :                                 /* LCOV_EXCL_START */
    1370             :                                 log_fatal("Colliding 'map' index specification in '%s'\n", path);
    1371             :                                 exit(EXIT_FAILURE);
    1372             :                                 /* LCOV_EXCL_STOP */
    1373             :                         }
    1374             :                 }
    1375             :         }
    1376         223 : }
    1377             : 
    1378             : /**
    1379             :  * Checks if the position is REQUIRED, or we can completely clear it from the state.
    1380             :  *
    1381             :  * Note that position with only DELETED blocks are discarged.
    1382             :  */
    1383      738011 : static int position_is_required(struct snapraid_state* state, block_off_t pos)
    1384             : {
    1385             :         tommy_node* i;
    1386             : 
    1387             :         /* check for each disk */
    1388      783686 :         for (i = state->disklist; i != 0; i = i->next) {
    1389      783686 :                 struct snapraid_disk* disk = i->data;
    1390      783686 :                 struct snapraid_block* block = disk_block_get(disk, pos);
    1391             : 
    1392             :                 /* if we have at least one file, the position is needed */
    1393      783686 :                 if (block_has_file(block))
    1394      738011 :                         return 1;
    1395             :         }
    1396             : 
    1397           0 :         return 0;
    1398             : }
    1399             : 
    1400             : /**
    1401             :  * Checks if the info block is REQUIREQ.
    1402             :  *
    1403             :  * This is used to ensure that we keep the last check used for scrubbing.
    1404             :  * and the we add it when importing old context files.
    1405             :  *
    1406             :  * Note that you can have position without info blocks, for example
    1407             :  * if all the blocks are not synced.
    1408             :  *
    1409             :  * Note also that not requiring an info block, doesn't mean that if present it
    1410             :  * can be discarded.
    1411             :  */
    1412     1498181 : static int info_is_required(struct snapraid_state* state, block_off_t pos)
    1413             : {
    1414             :         tommy_node* i;
    1415             : 
    1416             :         /* check for each disk */
    1417     2020186 :         for (i = state->disklist; i != 0; i = i->next) {
    1418     1957309 :                 struct snapraid_disk* disk = i->data;
    1419     1957309 :                 struct snapraid_block* block = disk_block_get(disk, pos);
    1420             : 
    1421             :                 /* if we have at least one synced file, the info is required */
    1422     1957309 :                 if (block_state_get(block) == BLOCK_STATE_BLK)
    1423     1435304 :                         return 1;
    1424             :         }
    1425             : 
    1426       62877 :         return 0;
    1427             : }
    1428             : 
    1429           0 : static void position_clear_deleted(struct snapraid_state* state, block_off_t pos)
    1430             : {
    1431             :         tommy_node* i;
    1432             : 
    1433             :         /* check for each disk if block is really used */
    1434           0 :         for (i = state->disklist; i != 0; i = i->next) {
    1435           0 :                 struct snapraid_disk* disk = i->data;
    1436           0 :                 struct snapraid_block* block = disk_block_get(disk, pos);
    1437             : 
    1438             :                 /* if the block is deleted */
    1439           0 :                 if (block_state_get(block) == BLOCK_STATE_DELETED) {
    1440             :                         /* set it to empty */
    1441           0 :                         tommy_arrayblk_set(&disk->blockarr, pos, BLOCK_EMPTY);
    1442             :                 }
    1443             :         }
    1444           0 : }
    1445             : 
    1446             : /**
    1447             :  * Checks if a block position in a disk is deleted.
    1448             :  */
    1449     4423464 : static int is_block_deleted(struct snapraid_disk* disk, block_off_t pos)
    1450             : {
    1451     4423464 :         struct snapraid_block* block = disk_block_get(disk, pos);
    1452             : 
    1453     4423464 :         return block_state_get(block) == BLOCK_STATE_DELETED;
    1454             : }
    1455             : 
    1456           5 : static void state_read_text(struct snapraid_state* state, const char* path, STREAM* f, time_t save_time)
    1457             : {
    1458             :         struct snapraid_disk* disk;
    1459             :         struct snapraid_file* file;
    1460             :         block_off_t blockidx;
    1461             :         block_off_t blockmax;
    1462             :         unsigned line;
    1463             :         unsigned count_file;
    1464             :         unsigned count_hardlink;
    1465             :         unsigned count_symlink;
    1466             :         unsigned count_dir;
    1467             :         block_off_t b;
    1468             : 
    1469           5 :         disk = 0;
    1470           5 :         file = 0;
    1471           5 :         line = 1;
    1472           5 :         blockidx = 0;
    1473           5 :         blockmax = 0;
    1474           5 :         count_file = 0;
    1475           5 :         count_hardlink = 0;
    1476           5 :         count_symlink = 0;
    1477           5 :         count_dir = 0;
    1478             : 
    1479             :         while (1) {
    1480             :                 char buffer[PATH_MAX];
    1481             :                 char tag[PATH_MAX];
    1482             :                 int ret;
    1483             :                 int c;
    1484             : 
    1485             :                 /* read the command */
    1486      364527 :                 ret = sgettok(f, tag, sizeof(tag));
    1487      364527 :                 if (ret < 0) {
    1488             :                         /* LCOV_EXCL_START */
    1489             :                         log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
    1490             :                         exit(EXIT_FAILURE);
    1491             :                         /* LCOV_EXCL_STOP */
    1492             :                 }
    1493             : 
    1494             :                 /* skip only one space if present */
    1495      364527 :                 c = sgetc(f);
    1496      364527 :                 if (c != ' ') {
    1497           5 :                         sungetc(c, f);
    1498             :                 }
    1499             : 
    1500      364527 :                 if (strcmp(tag, "blk") == 0 || strcmp(tag, "new") == 0
    1501      161439 :                         || strcmp(tag, "chg") == 0 || strcmp(tag, "rep") == 0
    1502      231282 :                 ) {
    1503             :                         /* "blk"/""new"/"chg"/"rep" command */
    1504             :                         block_off_t v_pos;
    1505             :                         struct snapraid_block* block;
    1506             : 
    1507      231282 :                         if (!file) {
    1508             :                                 /* LCOV_EXCL_START */
    1509             :                                 log_fatal("Unexpected '%s' specification in '%s' at line %u\n", tag, path, line);
    1510             :                                 exit(EXIT_FAILURE);
    1511             :                                 /* LCOV_EXCL_STOP */
    1512             :                         }
    1513             : 
    1514      231282 :                         ret = sgetu32(f, &v_pos);
    1515      231282 :                         if (ret < 0) {
    1516             :                                 /* LCOV_EXCL_START */
    1517             :                                 log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
    1518             :                                 exit(EXIT_FAILURE);
    1519             :                                 /* LCOV_EXCL_STOP */
    1520             :                         }
    1521             : 
    1522      231282 :                         if (blockidx >= file->blockmax) {
    1523             :                                 /* LCOV_EXCL_START */
    1524             :                                 log_fatal("Internal inconsistency in '%s' specification in '%s' at line %u\n", tag, path, line);
    1525             :                                 exit(EXIT_FAILURE);
    1526             :                                 /* LCOV_EXCL_STOP */
    1527             :                         }
    1528             : 
    1529      231282 :                         block = &file->blockvec[blockidx];
    1530             : 
    1531      231282 :                         if (block->parity_pos != POS_INVALID) {
    1532             :                                 /* LCOV_EXCL_START */
    1533             :                                 log_fatal("Internal inconsistency in '%s' specification in '%s' at line %u\n", tag, path, line);
    1534             :                                 exit(EXIT_FAILURE);
    1535             :                                 /* LCOV_EXCL_STOP */
    1536             :                         }
    1537             : 
    1538             :                         /* set the parity only if present */
    1539      231282 :                         switch (tag[0]) {
    1540             :                         case 'b' :
    1541      203088 :                                 block_state_set(block, BLOCK_STATE_BLK);
    1542      203088 :                                 break;
    1543             :                         case 'n' :
    1544             :                                 /* deprecated NEW blocks are converted to CHG ones */
    1545           0 :                                 block_state_set(block, BLOCK_STATE_CHG);
    1546           0 :                                 break;
    1547             :                         case 'c' :
    1548       28194 :                                 block_state_set(block, BLOCK_STATE_CHG);
    1549       28194 :                                 break;
    1550             :                         case 'r' :
    1551           0 :                                 block_state_set(block, BLOCK_STATE_REP);
    1552           0 :                                 break;
    1553             :                         }
    1554             : 
    1555      231282 :                         block->parity_pos = v_pos;
    1556             : 
    1557             :                         /* keep track of the max block number used in parity */
    1558      231282 :                         if (v_pos + 1 > blockmax)
    1559       38008 :                                 blockmax = v_pos + 1;
    1560             : 
    1561             :                         /* read the hash only for 'blk/chg/rep', and not for 'new' */
    1562      231282 :                         if (tag[0] != 'n') {
    1563      231282 :                                 c = sgetc(f);
    1564      231282 :                                 if (c != ' ') {
    1565             :                                         /* LCOV_EXCL_START */
    1566             :                                         log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
    1567             :                                         exit(EXIT_FAILURE);
    1568             :                                         /* LCOV_EXCL_STOP */
    1569             :                                 }
    1570             : 
    1571      231282 :                                 ret = sgethex(f, block->hash, HASH_SIZE);
    1572      231282 :                                 if (ret < 0) {
    1573             :                                         /* LCOV_EXCL_START */
    1574             :                                         log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
    1575             :                                         exit(EXIT_FAILURE);
    1576             :                                         /* LCOV_EXCL_STOP */
    1577             :                                 }
    1578             :                         } else {
    1579             :                                 /* set the ZERO hash for deprecated NEW blocks */
    1580           0 :                                 hash_zero_set(block->hash);
    1581             :                         }
    1582             : 
    1583             :                         /* if the block contains a hash of past data */
    1584             :                         /* and we are clearing such undeterminated hashes */
    1585      231282 :                         if (state->clear_past_hash
    1586       95231 :                                 && block_has_past_hash(block)
    1587             :                         ) {
    1588             :                                 /* set the hash value to INVALID */
    1589       14097 :                                 hash_invalid_set(block->hash);
    1590             :                         }
    1591             : 
    1592             :                         /* if we are disabling the copy optimization */
    1593             :                         /* we want also to clear any already previously stored information */
    1594             :                         /* in other sync commands */
    1595             :                         /* note that this is required only in sync, and we detect */
    1596             :                         /* this using the clear_past_hash flag */
    1597      231282 :                         if (state->clear_past_hash
    1598       95231 :                                 && state->opt.force_nocopy
    1599           0 :                                 && block_state_get(block) == BLOCK_STATE_REP
    1600             :                         ) {
    1601             :                                 /* set the hash value to INVALID */
    1602           0 :                                 hash_invalid_set(block->hash);
    1603             :                                 /* convert from REP to CHG block */
    1604           0 :                                 block_state_set(block, BLOCK_STATE_CHG);
    1605             :                         }
    1606             : 
    1607             :                         /* if we want a full sync, marks block as invalid parity */
    1608             :                         /* note that we do this after the force_nocopy option */
    1609             :                         /* to avoid to mixup the two things */
    1610      231282 :                         if (state->opt.force_full
    1611           0 :                                 && block_state_get(block) == BLOCK_STATE_BLK) {
    1612             :                                 /* convert from BLK to REP */
    1613           0 :                                 block_state_set(block, BLOCK_STATE_REP);
    1614             :                         }
    1615             : 
    1616             :                         /* we must not overwrite existing blocks */
    1617      231282 :                         if (disk_block_get(disk, v_pos) != BLOCK_EMPTY) {
    1618             :                                 /* LCOV_EXCL_START */
    1619             :                                 log_fatal("Internal inconsistency for '%s' specification in '%s' at line %u\n", tag, path, line);
    1620             :                                 exit(EXIT_FAILURE);
    1621             :                                 /* LCOV_EXCL_STOP */
    1622             :                         }
    1623             : 
    1624             :                         /* insert the block in the block array */
    1625      231282 :                         tommy_arrayblk_grow(&disk->blockarr, v_pos + 1);
    1626      231282 :                         tommy_arrayblk_set(&disk->blockarr, v_pos, block);
    1627             : 
    1628             :                         /* check for termination of the block list */
    1629      231282 :                         ++blockidx;
    1630      231282 :                         if (blockidx == file->blockmax) {
    1631       93967 :                                 file = 0;
    1632       93967 :                                 disk = 0;
    1633             :                         }
    1634      133245 :                 } else if (strcmp(tag, "inf") == 0) {
    1635             :                         /* "inf" command */
    1636             :                         block_off_t v_pos;
    1637             :                         snapraid_info info;
    1638             :                         int rehash;
    1639             :                         int bad;
    1640             :                         uint32_t t;
    1641             : 
    1642       34905 :                         ret = sgetu32(f, &v_pos);
    1643       34905 :                         if (ret < 0) {
    1644             :                                 /* LCOV_EXCL_START */
    1645             :                                 log_fatal("Invalid 'inf' specification in '%s' at line %u\n", path, line);
    1646             :                                 exit(EXIT_FAILURE);
    1647             :                                 /* LCOV_EXCL_STOP */
    1648             :                         }
    1649             : 
    1650       34905 :                         if (v_pos >= blockmax) {
    1651             :                                 /* LCOV_EXCL_START */
    1652             :                                 log_fatal("Internal inconsistency in position for 'inf' specification in '%s' at line %u\n", path, line);
    1653             :                                 exit(EXIT_FAILURE);
    1654             :                                 /* LCOV_EXCL_STOP */
    1655             :                         }
    1656             : 
    1657       34905 :                         c = sgetc(f);
    1658       34905 :                         if (c != ' ') {
    1659             :                                 /* LCOV_EXCL_START */
    1660             :                                 log_fatal("Invalid 'inf' specification in '%s' at line %u\n", path, line);
    1661             :                                 exit(EXIT_FAILURE);
    1662             :                                 /* LCOV_EXCL_STOP */
    1663             :                         }
    1664             : 
    1665       34905 :                         ret = sgetu32(f, &t);
    1666       34905 :                         if (ret < 0) {
    1667             :                                 /* LCOV_EXCL_START */
    1668             :                                 log_fatal("Invalid 'inf' specification in '%s' at line %u\n", path, line);
    1669             :                                 exit(EXIT_FAILURE);
    1670             :                                 /* LCOV_EXCL_STOP */
    1671             :                         }
    1672             : 
    1673             :                         /* read extra tags if present */
    1674       34905 :                         rehash = 0;
    1675       34905 :                         bad = 0;
    1676       34905 :                         c = sgetc(f);
    1677       69810 :                         while (c == ' ') {
    1678           0 :                                 ret = sgettok(f, buffer, sizeof(buffer));
    1679           0 :                                 if (ret < 0) {
    1680             :                                         /* LCOV_EXCL_START */
    1681             :                                         log_fatal("Invalid 'inf' specification in '%s' at line %u\n", path, line);
    1682             :                                         exit(EXIT_FAILURE);
    1683             :                                         /* LCOV_EXCL_STOP */
    1684             :                                 }
    1685             : 
    1686           0 :                                 if (strcmp(buffer, "bad") == 0) {
    1687           0 :                                         bad = 1;
    1688           0 :                                 } else if (strcmp(buffer, "rehash") == 0) {
    1689           0 :                                         rehash = 1;
    1690             : 
    1691           0 :                                         if (state->prevhash == HASH_UNDEFINED) {
    1692             :                                                 /* LCOV_EXCL_START */
    1693             :                                                 log_fatal("Internal inconsistency for missing previous checksum in '%s' at line %u\n", path, line);
    1694             :                                                 exit(EXIT_FAILURE);
    1695             :                                                 /* LCOV_EXCL_STOP */
    1696             :                                         }
    1697             :                                 } else {
    1698             :                                         /* LCOV_EXCL_START */
    1699             :                                         log_fatal("Invalid 'inf' specification '%s' in '%s' at line %u\n", buffer, path, line);
    1700             :                                         exit(EXIT_FAILURE);
    1701             :                                         /* LCOV_EXCL_STOP */
    1702             :                                 }
    1703             : 
    1704           0 :                                 c = sgetc(f);
    1705             :                         }
    1706       34905 :                         sungetc(c, f);
    1707             : 
    1708       34905 :                         info = info_make(t, bad, rehash);
    1709             : 
    1710             :                         /* insert the info in the array */
    1711       34905 :                         info_set(&state->infoarr, v_pos, info);
    1712       98340 :                 } else if (strcmp(tag, "off") == 0) {
    1713             :                         /* "off" command */
    1714             :                         block_off_t v_pos;
    1715             :                         struct snapraid_deleted* deleted;
    1716             : 
    1717        1098 :                         if (!disk) {
    1718             :                                 /* LCOV_EXCL_START */
    1719             :                                 log_fatal("Unexpected 'off' specification in '%s' at line %u\n", path, line);
    1720             :                                 exit(EXIT_FAILURE);
    1721             :                                 /* LCOV_EXCL_STOP */
    1722             :                         }
    1723             : 
    1724             :                         /* allocate a new deleted block */
    1725        1098 :                         deleted = deleted_alloc();
    1726             : 
    1727        1098 :                         ret = sgetu32(f, &v_pos);
    1728        1098 :                         if (ret < 0) {
    1729             :                                 /* LCOV_EXCL_START */
    1730             :                                 log_fatal("Invalid 'off' specification in '%s' at line %u\n", path, line);
    1731             :                                 exit(EXIT_FAILURE);
    1732             :                                 /* LCOV_EXCL_STOP */
    1733             :                         }
    1734             : 
    1735        1098 :                         deleted->block.parity_pos = v_pos;
    1736             : 
    1737             :                         /* read the hash */
    1738        1098 :                         c = sgetc(f);
    1739        1098 :                         if (c != ' ') {
    1740             :                                 /* LCOV_EXCL_START */
    1741             :                                 log_fatal("Invalid 'off' specification in '%s' at line %u\n", path, line);
    1742             :                                 exit(EXIT_FAILURE);
    1743             :                                 /* LCOV_EXCL_STOP */
    1744             :                         }
    1745             : 
    1746             :                         /* set the hash only if present */
    1747        1098 :                         ret = sgethex(f, deleted->block.hash, HASH_SIZE);
    1748        1098 :                         if (ret < 0) {
    1749             :                                 /* LCOV_EXCL_START */
    1750             :                                 log_fatal("Invalid 'off' specification in '%s' at line %u\n", path, line);
    1751             :                                 exit(EXIT_FAILURE);
    1752             :                                 /* LCOV_EXCL_STOP */
    1753             :                         }
    1754             : 
    1755             :                         /* if we are clearing undeterminated hashes */
    1756        1098 :                         if (state->clear_past_hash) {
    1757             :                                 /* set the hash value to INVALID */
    1758         366 :                                 hash_invalid_set(deleted->block.hash);
    1759             :                         }
    1760             : 
    1761             :                         /* we must not overwrite existing blocks */
    1762        1098 :                         if (disk_block_get(disk, v_pos) != BLOCK_EMPTY) {
    1763             :                                 /* LCOV_EXCL_START */
    1764             :                                 log_fatal("Internal inconsistency for 'off' specification in '%s' at line %u\n", path, line);
    1765             :                                 exit(EXIT_FAILURE);
    1766             :                                 /* LCOV_EXCL_STOP */
    1767             :                         }
    1768             : 
    1769             :                         /* insert it in the list of deleted blocks */
    1770        1098 :                         tommy_list_insert_tail(&disk->deletedlist, &deleted->node, deleted);
    1771             : 
    1772             :                         /* insert the block in the block array */
    1773        1098 :                         tommy_arrayblk_grow(&disk->blockarr, v_pos + 1);
    1774        1098 :                         tommy_arrayblk_set(&disk->blockarr, v_pos, &deleted->block);
    1775       97242 :                 } else if (strcmp(tag, "file") == 0) {
    1776             :                         /* file */
    1777             :                         char sub[PATH_MAX];
    1778             :                         uint64_t v_size;
    1779             :                         uint64_t v_mtime_sec;
    1780             :                         uint32_t v_mtime_nsec;
    1781             :                         uint64_t v_inode;
    1782             : 
    1783       94092 :                         if (file) {
    1784             :                                 /* LCOV_EXCL_START */
    1785             :                                 log_fatal("Missing 'blk' specification in '%s' at line %u\n", path, line);
    1786             :                                 exit(EXIT_FAILURE);
    1787             :                                 /* LCOV_EXCL_STOP */
    1788             :                         }
    1789             : 
    1790       94092 :                         ret = sgettok(f, buffer, sizeof(buffer));
    1791       94092 :                         if (ret < 0) {
    1792             :                                 /* LCOV_EXCL_START */
    1793             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1794             :                                 exit(EXIT_FAILURE);
    1795             :                                 /* LCOV_EXCL_STOP */
    1796             :                         }
    1797             : 
    1798       94092 :                         c = sgetc(f);
    1799       94092 :                         if (c != ' ') {
    1800             :                                 /* LCOV_EXCL_START */
    1801             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1802             :                                 exit(EXIT_FAILURE);
    1803             :                                 /* LCOV_EXCL_STOP */
    1804             :                         }
    1805             : 
    1806       94092 :                         ret = sgetu64(f, &v_size);
    1807       94092 :                         if (ret < 0) {
    1808             :                                 /* LCOV_EXCL_START */
    1809             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1810             :                                 exit(EXIT_FAILURE);
    1811             :                                 /* LCOV_EXCL_STOP */
    1812             :                         }
    1813             : 
    1814       94092 :                         c = sgetc(f);
    1815       94092 :                         if (c != ' ') {
    1816             :                                 /* LCOV_EXCL_START */
    1817             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1818             :                                 exit(EXIT_FAILURE);
    1819             :                                 /* LCOV_EXCL_STOP */
    1820             :                         }
    1821             : 
    1822       94092 :                         ret = sgetu64(f, &v_mtime_sec);
    1823       94092 :                         if (ret < 0) {
    1824             :                                 /* LCOV_EXCL_START */
    1825             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1826             :                                 exit(EXIT_FAILURE);
    1827             :                                 /* LCOV_EXCL_STOP */
    1828             :                         }
    1829             : 
    1830       94092 :                         c = sgetc(f);
    1831       94092 :                         if (c == '.') { /* the nanosecond field is present only from version 1.14 */
    1832       94092 :                                 ret = sgetu32(f, &v_mtime_nsec);
    1833       94092 :                                 if (ret < 0) {
    1834             :                                         /* LCOV_EXCL_START */
    1835             :                                         log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1836             :                                         exit(EXIT_FAILURE);
    1837             :                                         /* LCOV_EXCL_STOP */
    1838             :                                 }
    1839             : 
    1840       94092 :                                 c = sgetc(f);
    1841             :                         } else {
    1842             :                                 /* use a special value, meaning that we don't have this information */
    1843           0 :                                 v_mtime_nsec = STAT_NSEC_INVALID;
    1844             :                         }
    1845             : 
    1846       94092 :                         if (c != ' ') {
    1847             :                                 /* LCOV_EXCL_START */
    1848             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1849             :                                 exit(EXIT_FAILURE);
    1850             :                                 /* LCOV_EXCL_STOP */
    1851             :                         }
    1852             : 
    1853       94092 :                         ret = sgetu64(f, &v_inode);
    1854       94092 :                         if (ret < 0) {
    1855             :                                 /* LCOV_EXCL_START */
    1856             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1857             :                                 exit(EXIT_FAILURE);
    1858             :                                 /* LCOV_EXCL_STOP */
    1859             :                         }
    1860             : 
    1861       94092 :                         c = sgetc(f);
    1862       94092 :                         if (c != ' ') {
    1863             :                                 /* LCOV_EXCL_START */
    1864             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1865             :                                 exit(EXIT_FAILURE);
    1866             :                                 /* LCOV_EXCL_STOP */
    1867             :                         }
    1868             : 
    1869       94092 :                         ret = sgetline(f, sub, sizeof(sub));
    1870       94092 :                         if (ret < 0) {
    1871             :                                 /* LCOV_EXCL_START */
    1872             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1873             :                                 exit(EXIT_FAILURE);
    1874             :                                 /* LCOV_EXCL_STOP */
    1875             :                         }
    1876             : 
    1877       94092 :                         if (!*sub) {
    1878             :                                 /* LCOV_EXCL_START */
    1879             :                                 log_fatal("Invalid 'file' specification in '%s' at line %u\n", path, line);
    1880             :                                 exit(EXIT_FAILURE);
    1881             :                                 /* LCOV_EXCL_STOP */
    1882             :                         }
    1883             : 
    1884             :                         /* find the disk */
    1885       94092 :                         disk = find_disk(state, buffer);
    1886       94092 :                         if (!disk) {
    1887             :                                 /* LCOV_EXCL_START */
    1888             :                                 log_fatal("Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
    1889             :                                 exit(EXIT_FAILURE);
    1890             :                                 /* LCOV_EXCL_STOP */
    1891             :                         }
    1892             : 
    1893             :                         /* allocate the file */
    1894       94092 :                         file = file_alloc(state->block_size, sub, v_size, v_mtime_sec, v_mtime_nsec, v_inode, 0);
    1895             : 
    1896             :                         /* insert the file in the file containers */
    1897       94092 :                         tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
    1898       94092 :                         tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
    1899       94092 :                         tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
    1900       94092 :                         tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
    1901             : 
    1902             :                         /* start the block allocation of the file */
    1903       94092 :                         blockidx = 0;
    1904             : 
    1905             :                         /* check for empty file */
    1906       94092 :                         if (blockidx == file->blockmax) {
    1907         125 :                                 file = 0;
    1908         125 :                                 disk = 0;
    1909             :                         }
    1910             : 
    1911             :                         /* stat */
    1912       94092 :                         ++count_file;
    1913        3150 :                 } else if (strcmp(tag, "hole") == 0) {
    1914             :                         /* hole */
    1915             : 
    1916          30 :                         if (file) {
    1917             :                                 /* LCOV_EXCL_START */
    1918             :                                 log_fatal("Missing 'blk' specification in '%s' at line %u\n", path, line);
    1919             :                                 exit(EXIT_FAILURE);
    1920             :                                 /* LCOV_EXCL_STOP */
    1921             :                         }
    1922             : 
    1923          30 :                         ret = sgettok(f, buffer, sizeof(buffer));
    1924          30 :                         if (ret < 0) {
    1925             :                                 /* LCOV_EXCL_START */
    1926             :                                 log_fatal("Invalid 'hole' specification in '%s' at line %u\n", path, line);
    1927             :                                 exit(EXIT_FAILURE);
    1928             :                                 /* LCOV_EXCL_STOP */
    1929             :                         }
    1930             : 
    1931             :                         /* find the disk */
    1932          30 :                         disk = find_disk(state, buffer);
    1933          30 :                         if (!disk) {
    1934             :                                 /* LCOV_EXCL_START */
    1935             :                                 log_fatal("Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
    1936             :                                 exit(EXIT_FAILURE);
    1937             :                                 /* LCOV_EXCL_STOP */
    1938             :                         }
    1939        3120 :                 } else if (strcmp(tag, "symlink") == 0) {
    1940             :                         /* symlink */
    1941             :                         char sub[PATH_MAX];
    1942             :                         char linkto[PATH_MAX];
    1943             :                         char tokento[32];
    1944             :                         struct snapraid_link* link;
    1945             : 
    1946        2995 :                         ret = sgettok(f, buffer, sizeof(buffer));
    1947        2995 :                         if (ret < 0) {
    1948             :                                 /* LCOV_EXCL_START */
    1949             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1950             :                                 exit(EXIT_FAILURE);
    1951             :                                 /* LCOV_EXCL_STOP */
    1952             :                         }
    1953             : 
    1954        2995 :                         c = sgetc(f);
    1955        2995 :                         if (c != ' ') {
    1956             :                                 /* LCOV_EXCL_START */
    1957             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1958             :                                 exit(EXIT_FAILURE);
    1959             :                                 /* LCOV_EXCL_STOP */
    1960             :                         }
    1961             : 
    1962        2995 :                         ret = sgetline(f, sub, sizeof(sub));
    1963        2995 :                         if (ret < 0) {
    1964             :                                 /* LCOV_EXCL_START */
    1965             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1966             :                                 exit(EXIT_FAILURE);
    1967             :                                 /* LCOV_EXCL_STOP */
    1968             :                         }
    1969             : 
    1970        2995 :                         c = sgeteol(f);
    1971        2995 :                         if (c != '\n') {
    1972             :                                 /* LCOV_EXCL_START */
    1973             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1974             :                                 exit(EXIT_FAILURE);
    1975             :                                 /* LCOV_EXCL_STOP */
    1976             :                         }
    1977        2995 :                         ++line;
    1978             : 
    1979        2995 :                         ret = sgettok(f, tokento, sizeof(tokento));
    1980        2995 :                         if (ret < 0) {
    1981             :                                 /* LCOV_EXCL_START */
    1982             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1983             :                                 exit(EXIT_FAILURE);
    1984             :                                 /* LCOV_EXCL_STOP */
    1985             :                         }
    1986        2995 :                         if (strcmp(tokento, "to") != 0) {
    1987             :                                 /* LCOV_EXCL_START */
    1988             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1989             :                                 exit(EXIT_FAILURE);
    1990             :                                 /* LCOV_EXCL_STOP */
    1991             :                         }
    1992             : 
    1993        2995 :                         c = sgetc(f);
    1994        2995 :                         if (c != ' ') {
    1995             :                                 /* LCOV_EXCL_START */
    1996             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    1997             :                                 exit(EXIT_FAILURE);
    1998             :                                 /* LCOV_EXCL_STOP */
    1999             :                         }
    2000             : 
    2001        2995 :                         ret = sgetline(f, linkto, sizeof(linkto));
    2002        2995 :                         if (ret < 0) {
    2003             :                                 /* LCOV_EXCL_START */
    2004             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    2005             :                                 exit(EXIT_FAILURE);
    2006             :                                 /* LCOV_EXCL_STOP */
    2007             :                         }
    2008             : 
    2009        2995 :                         if (!*sub || !*linkto) {
    2010             :                                 /* LCOV_EXCL_START */
    2011             :                                 log_fatal("Invalid 'symlink' specification in '%s' at line %u\n", path, line);
    2012             :                                 exit(EXIT_FAILURE);
    2013             :                                 /* LCOV_EXCL_STOP */
    2014             :                         }
    2015             : 
    2016             :                         /* find the disk */
    2017        2995 :                         disk = find_disk(state, buffer);
    2018        2995 :                         if (!disk) {
    2019             :                                 /* LCOV_EXCL_START */
    2020             :                                 log_fatal("Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
    2021             :                                 exit(EXIT_FAILURE);
    2022             :                                 /* LCOV_EXCL_STOP */
    2023             :                         }
    2024             : 
    2025             :                         /* allocate the link as symbolic link */
    2026        2995 :                         link = link_alloc(sub, linkto, FILE_IS_SYMLINK);
    2027             : 
    2028             :                         /* insert the link in the link containers */
    2029        2995 :                         tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
    2030        2995 :                         tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
    2031             : 
    2032             :                         /* stat */
    2033        2995 :                         ++count_symlink;
    2034         125 :                 } else if (strcmp(tag, "hardlink") == 0) {
    2035             :                         /* hardlink */
    2036             :                         char sub[PATH_MAX];
    2037             :                         char linkto[PATH_MAX];
    2038             :                         char tokento[32];
    2039             :                         struct snapraid_link* link;
    2040             : 
    2041          30 :                         ret = sgettok(f, buffer, sizeof(buffer));
    2042          30 :                         if (ret < 0) {
    2043             :                                 /* LCOV_EXCL_START */
    2044             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2045             :                                 exit(EXIT_FAILURE);
    2046             :                                 /* LCOV_EXCL_STOP */
    2047             :                         }
    2048             : 
    2049          30 :                         c = sgetc(f);
    2050          30 :                         if (c != ' ') {
    2051             :                                 /* LCOV_EXCL_START */
    2052             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2053             :                                 exit(EXIT_FAILURE);
    2054             :                                 /* LCOV_EXCL_STOP */
    2055             :                         }
    2056             : 
    2057          30 :                         ret = sgetline(f, sub, sizeof(sub));
    2058          30 :                         if (ret < 0) {
    2059             :                                 /* LCOV_EXCL_START */
    2060             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2061             :                                 exit(EXIT_FAILURE);
    2062             :                                 /* LCOV_EXCL_STOP */
    2063             :                         }
    2064             : 
    2065          30 :                         c = sgeteol(f);
    2066          30 :                         if (c != '\n') {
    2067             :                                 /* LCOV_EXCL_START */
    2068             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2069             :                                 exit(EXIT_FAILURE);
    2070             :                                 /* LCOV_EXCL_STOP */
    2071             :                         }
    2072          30 :                         ++line;
    2073             : 
    2074          30 :                         ret = sgettok(f, tokento, sizeof(tokento));
    2075          30 :                         if (ret < 0) {
    2076             :                                 /* LCOV_EXCL_START */
    2077             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2078             :                                 exit(EXIT_FAILURE);
    2079             :                                 /* LCOV_EXCL_STOP */
    2080             :                         }
    2081          30 :                         if (strcmp(tokento, "to") != 0) {
    2082             :                                 /* LCOV_EXCL_START */
    2083             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2084             :                                 exit(EXIT_FAILURE);
    2085             :                                 /* LCOV_EXCL_STOP */
    2086             :                         }
    2087             : 
    2088          30 :                         c = sgetc(f);
    2089          30 :                         if (c != ' ') {
    2090             :                                 /* LCOV_EXCL_START */
    2091             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2092             :                                 exit(EXIT_FAILURE);
    2093             :                                 /* LCOV_EXCL_STOP */
    2094             :                         }
    2095             : 
    2096          30 :                         ret = sgetline(f, linkto, sizeof(linkto));
    2097          30 :                         if (ret < 0) {
    2098             :                                 /* LCOV_EXCL_START */
    2099             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2100             :                                 exit(EXIT_FAILURE);
    2101             :                                 /* LCOV_EXCL_STOP */
    2102             :                         }
    2103             : 
    2104          30 :                         if (!*sub || !*linkto) {
    2105             :                                 /* LCOV_EXCL_START */
    2106             :                                 log_fatal("Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
    2107             :                                 exit(EXIT_FAILURE);
    2108             :                                 /* LCOV_EXCL_STOP */
    2109             :                         }
    2110             : 
    2111             :                         /* find the disk */
    2112          30 :                         disk = find_disk(state, buffer);
    2113          30 :                         if (!disk) {
    2114             :                                 /* LCOV_EXCL_START */
    2115             :                                 log_fatal("Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
    2116             :                                 exit(EXIT_FAILURE);
    2117             :                                 /* LCOV_EXCL_STOP */
    2118             :                         }
    2119             : 
    2120             :                         /* allocate the link as hard link */
    2121          30 :                         link = link_alloc(sub, linkto, FILE_IS_HARDLINK);
    2122             : 
    2123             :                         /* insert the link in the link containers */
    2124          30 :                         tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
    2125          30 :                         tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
    2126             : 
    2127             :                         /* stat */
    2128          30 :                         ++count_hardlink;
    2129          95 :                 } else if (strcmp(tag, "dir") == 0) {
    2130             :                         /* dir */
    2131             :                         char sub[PATH_MAX];
    2132             :                         struct snapraid_dir* dir;
    2133             : 
    2134          20 :                         ret = sgettok(f, buffer, sizeof(buffer));
    2135          20 :                         if (ret < 0) {
    2136             :                                 /* LCOV_EXCL_START */
    2137             :                                 log_fatal("Invalid 'dir' specification in '%s' at line %u\n", path, line);
    2138             :                                 exit(EXIT_FAILURE);
    2139             :                                 /* LCOV_EXCL_STOP */
    2140             :                         }
    2141             : 
    2142          20 :                         c = sgetc(f);
    2143          20 :                         if (c != ' ') {
    2144             :                                 /* LCOV_EXCL_START */
    2145             :                                 log_fatal("Invalid 'dir' specification in '%s' at line %u\n", path, line);
    2146             :                                 exit(EXIT_FAILURE);
    2147             :                                 /* LCOV_EXCL_STOP */
    2148             :                         }
    2149             : 
    2150          20 :                         ret = sgetline(f, sub, sizeof(sub));
    2151          20 :                         if (ret < 0) {
    2152             :                                 /* LCOV_EXCL_START */
    2153             :                                 log_fatal("Invalid 'dir' specification in '%s' at line %u\n", path, line);
    2154             :                                 exit(EXIT_FAILURE);
    2155             :                                 /* LCOV_EXCL_STOP */
    2156             :                         }
    2157             : 
    2158          20 :                         if (!*sub) {
    2159             :                                 /* LCOV_EXCL_START */
    2160             :                                 log_fatal("Invalid 'dir' specification in '%s' at line %u\n", path, line);
    2161             :                                 exit(EXIT_FAILURE);
    2162             :                                 /* LCOV_EXCL_STOP */
    2163             :                         }
    2164             : 
    2165             :                         /* find the disk */
    2166          20 :                         disk = find_disk(state, buffer);
    2167          20 :                         if (!disk) {
    2168             :                                 /* LCOV_EXCL_START */
    2169             :                                 log_fatal("Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
    2170             :                                 exit(EXIT_FAILURE);
    2171             :                                 /* LCOV_EXCL_STOP */
    2172             :                         }
    2173             : 
    2174             :                         /* allocate the dir */
    2175          20 :                         dir = dir_alloc(sub);
    2176             : 
    2177             :                         /* insert the dir in the dir containers */
    2178          20 :                         tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
    2179          20 :                         tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
    2180             : 
    2181             :                         /* stat */
    2182          20 :                         ++count_dir;
    2183          75 :                 } else if (strcmp(tag, "checksum") == 0) {
    2184             : 
    2185           5 :                         ret = sgettok(f, buffer, sizeof(buffer));
    2186           5 :                         if (ret < 0) {
    2187             :                                 /* LCOV_EXCL_START */
    2188             :                                 log_fatal("Invalid 'checksum' specification in '%s' at line %u\n", path, line);
    2189             :                                 exit(EXIT_FAILURE);
    2190             :                                 /* LCOV_EXCL_STOP */
    2191             :                         }
    2192             : 
    2193           5 :                         if (strcmp(buffer, "murmur3") == 0) {
    2194           5 :                                 state->hash = HASH_MURMUR3;
    2195           0 :                         } else if (strcmp(buffer, "spooky2") == 0) {
    2196           0 :                                 state->hash = HASH_SPOOKY2;
    2197             :                         } else {
    2198             :                                 /* LCOV_EXCL_START */
    2199             :                                 log_fatal("Invalid 'checksum' specification '%s' in '%s' at line %u\n", buffer, path, line);
    2200             :                                 exit(EXIT_FAILURE);
    2201             :                                 /* LCOV_EXCL_STOP */
    2202             :                         }
    2203             : 
    2204           5 :                         c = sgetc(f);
    2205           5 :                         if (c == ' ') {
    2206             :                                 /* read the seed if present */
    2207           5 :                                 ret = sgethex(f, state->hashseed, HASH_SIZE);
    2208           5 :                                 if (ret < 0) {
    2209             :                                         /* LCOV_EXCL_START */
    2210             :                                         log_fatal("Invalid 'seed' specification in '%s' at line %u\n", path, line);
    2211             :                                         exit(EXIT_FAILURE);
    2212             :                                         /* LCOV_EXCL_STOP */
    2213             :                                 }
    2214             :                         } else {
    2215           0 :                                 sungetc(c, f);
    2216           0 :                                 memset(state->hashseed, 0, HASH_SIZE);
    2217             :                         }
    2218          70 :                 } else if (strcmp(tag, "prevchecksum") == 0) {
    2219             : 
    2220           0 :                         ret = sgettok(f, buffer, sizeof(buffer));
    2221           0 :                         if (ret < 0) {
    2222             :                                 /* LCOV_EXCL_START */
    2223             :                                 log_fatal("Invalid 'prevchecksum' specification in '%s' at line %u\n", path, line);
    2224             :                                 exit(EXIT_FAILURE);
    2225             :                                 /* LCOV_EXCL_STOP */
    2226             :                         }
    2227             : 
    2228           0 :                         if (strcmp(buffer, "murmur3") == 0) {
    2229           0 :                                 state->prevhash = HASH_MURMUR3;
    2230           0 :                         } else if (strcmp(buffer, "spooky2") == 0) {
    2231           0 :                                 state->prevhash = HASH_SPOOKY2;
    2232             :                         } else {
    2233             :                                 /* LCOV_EXCL_START */
    2234             :                                 log_fatal("Invalid 'prevchecksum' specification '%s' in '%s' at line %u\n", buffer, path, line);
    2235             :                                 exit(EXIT_FAILURE);
    2236             :                                 /* LCOV_EXCL_STOP */
    2237             :                         }
    2238             : 
    2239           0 :                         c = sgetc(f);
    2240           0 :                         if (c != ' ') {
    2241             :                                 /* LCOV_EXCL_START */
    2242             :                                 log_fatal("Invalid 'prevchecksum' specification '%s' in '%s' at line %u\n", buffer, path, line);
    2243             :                                 exit(EXIT_FAILURE);
    2244             :                                 /* LCOV_EXCL_STOP */
    2245             :                         }
    2246             : 
    2247             :                         /* read the seed */
    2248           0 :                         ret = sgethex(f, state->prevhashseed, HASH_SIZE);
    2249           0 :                         if (ret < 0) {
    2250             :                                 /* LCOV_EXCL_START */
    2251             :                                 log_fatal("Invalid 'seed' specification in '%s' at line %u\n", path, line);
    2252             :                                 exit(EXIT_FAILURE);
    2253             :                                 /* LCOV_EXCL_STOP */
    2254             :                         }
    2255          70 :                 } else if (strcmp(tag, "blksize") == 0) {
    2256             :                         block_off_t blksize;
    2257             : 
    2258           5 :                         ret = sgetu32(f, &blksize);
    2259           5 :                         if (ret < 0) {
    2260             :                                 /* LCOV_EXCL_START */
    2261             :                                 log_fatal("Invalid 'blksize' specification in '%s' at line %u\n", path, line);
    2262             :                                 exit(EXIT_FAILURE);
    2263             :                                 /* LCOV_EXCL_STOP */
    2264             :                         }
    2265             : 
    2266             :                         /* without configuration, auto assign the block size */
    2267           5 :                         if (state->no_conf) {
    2268           0 :                                 state->block_size = blksize;
    2269             :                         }
    2270             : 
    2271           5 :                         if (blksize != state->block_size) {
    2272             :                                 /* LCOV_EXCL_START */
    2273             :                                 log_fatal("Mismatching 'blksize' and 'blocksize' specification in '%s' at line %u\n", path, line);
    2274             :                                 log_fatal("Please restore the 'blocksize' value in the configuration file to '%u'\n", blksize / 1024);
    2275             :                                 exit(EXIT_FAILURE);
    2276             :                                 /* LCOV_EXCL_STOP */
    2277             :                         }
    2278          95 :                 } else if (strcmp(tag, "map") == 0 || strcmp(tag, "mapping") == 0) {
    2279             :                         struct snapraid_map* map;
    2280             :                         char uuid[UUID_MAX];
    2281             :                         uint32_t v_pos;
    2282             :                         uint32_t v_total_blocks;
    2283             :                         uint32_t v_free_blocks;
    2284             : 
    2285          30 :                         ret = sgettok(f, buffer, sizeof(buffer));
    2286          30 :                         if (ret < 0) {
    2287             :                                 /* LCOV_EXCL_START */
    2288             :                                 log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2289             :                                 exit(EXIT_FAILURE);
    2290             :                                 /* LCOV_EXCL_STOP */
    2291             :                         }
    2292             : 
    2293          30 :                         c = sgetc(f);
    2294          30 :                         if (c != ' ') {
    2295             :                                 /* LCOV_EXCL_START */
    2296             :                                 log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2297             :                                 exit(EXIT_FAILURE);
    2298             :                                 /* LCOV_EXCL_STOP */
    2299             :                         }
    2300             : 
    2301          30 :                         ret = sgetu32(f, &v_pos);
    2302          30 :                         if (ret < 0) {
    2303             :                                 /* LCOV_EXCL_START */
    2304             :                                 log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2305             :                                 exit(EXIT_FAILURE);
    2306             :                                 /* LCOV_EXCL_STOP */
    2307             :                         }
    2308             : 
    2309             :                         /* from SnapRAID 7.0 the 'mapping' command includes the free space */
    2310          30 :                         if (strcmp(tag, "mapping") == 0) {
    2311          30 :                                 c = sgetc(f);
    2312          30 :                                 if (c != ' ') {
    2313             :                                         /* LCOV_EXCL_START */
    2314             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2315             :                                         exit(EXIT_FAILURE);
    2316             :                                         /* LCOV_EXCL_STOP */
    2317             :                                 }
    2318             : 
    2319          30 :                                 ret = sgetu32(f, &v_total_blocks);
    2320          30 :                                 if (ret < 0) {
    2321             :                                         /* LCOV_EXCL_START */
    2322             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2323             :                                         exit(EXIT_FAILURE);
    2324             :                                         /* LCOV_EXCL_STOP */
    2325             :                                 }
    2326             : 
    2327          30 :                                 c = sgetc(f);
    2328          30 :                                 if (c != ' ') {
    2329             :                                         /* LCOV_EXCL_START */
    2330             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2331             :                                         exit(EXIT_FAILURE);
    2332             :                                         /* LCOV_EXCL_STOP */
    2333             :                                 }
    2334             : 
    2335          30 :                                 ret = sgetu32(f, &v_free_blocks);
    2336          30 :                                 if (ret < 0) {
    2337             :                                         /* LCOV_EXCL_START */
    2338             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2339             :                                         exit(EXIT_FAILURE);
    2340             :                                         /* LCOV_EXCL_STOP */
    2341             :                                 }
    2342             :                         } else {
    2343           0 :                                 v_total_blocks = 0;
    2344           0 :                                 v_free_blocks = 0;
    2345             :                         }
    2346             : 
    2347             :                         /* check if the uuid is present */
    2348             :                         /* uuid must be the latest one as it could be also empty */
    2349          30 :                         c = sgetc(f);
    2350          30 :                         if (c != ' ') {
    2351           0 :                                 sungetc(c, f);
    2352           0 :                                 uuid[0] = 0;
    2353             :                         } else {
    2354             :                                 /* read the uuid */
    2355          30 :                                 ret = sgettok(f, uuid, sizeof(uuid));
    2356          30 :                                 if (ret < 0) {
    2357             :                                         /* LCOV_EXCL_START */
    2358             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2359             :                                         exit(EXIT_FAILURE);
    2360             :                                         /* LCOV_EXCL_STOP */
    2361             :                                 }
    2362             :                         }
    2363             : 
    2364          30 :                         map = map_alloc(buffer, v_pos, v_total_blocks, v_free_blocks, uuid);
    2365             : 
    2366          30 :                         tommy_list_insert_tail(&state->maplist, &map->node, map);
    2367          35 :                 } else if (strcmp(tag, "parity") == 0) {
    2368             :                         /* from SnapRAID 7.0 the 'parity' command includes the free space */
    2369             :                         char uuid[UUID_MAX];
    2370             :                         uint32_t v_level;
    2371             :                         uint32_t v_total_blocks;
    2372             :                         uint32_t v_free_blocks;
    2373             : 
    2374          30 :                         ret = sgetu32(f, &v_level);
    2375          30 :                         if (ret < 0) {
    2376             :                                 /* LCOV_EXCL_START */
    2377             :                                 log_fatal("Invalid 'parity' specification in '%s' at line %u\n", path, line);
    2378             :                                 exit(EXIT_FAILURE);
    2379             :                                 /* LCOV_EXCL_STOP */
    2380             :                         }
    2381             : 
    2382          30 :                         c = sgetc(f);
    2383          30 :                         if (c != ' ') {
    2384             :                                 /* LCOV_EXCL_START */
    2385             :                                 log_fatal("Invalid 'parity' specification in '%s' at line %u\n", path, line);
    2386             :                                 exit(EXIT_FAILURE);
    2387             :                                 /* LCOV_EXCL_STOP */
    2388             :                         }
    2389             : 
    2390          30 :                         ret = sgetu32(f, &v_total_blocks);
    2391          30 :                         if (ret < 0) {
    2392             :                                 /* LCOV_EXCL_START */
    2393             :                                 log_fatal("Invalid 'parity' specification in '%s' at line %u\n", path, line);
    2394             :                                 exit(EXIT_FAILURE);
    2395             :                                 /* LCOV_EXCL_STOP */
    2396             :                         }
    2397             : 
    2398          30 :                         c = sgetc(f);
    2399          30 :                         if (c != ' ') {
    2400             :                                 /* LCOV_EXCL_START */
    2401             :                                 log_fatal("Invalid 'parity' specification in '%s' at line %u\n", path, line);
    2402             :                                 exit(EXIT_FAILURE);
    2403             :                                 /* LCOV_EXCL_STOP */
    2404             :                         }
    2405             : 
    2406          30 :                         ret = sgetu32(f, &v_free_blocks);
    2407          30 :                         if (ret < 0) {
    2408             :                                 /* LCOV_EXCL_START */
    2409             :                                 log_fatal("Invalid 'parity' specification in '%s' at line %u\n", path, line);
    2410             :                                 exit(EXIT_FAILURE);
    2411             :                                 /* LCOV_EXCL_STOP */
    2412             :                         }
    2413             : 
    2414             :                         /* check if the uuid is present */
    2415             :                         /* uuid must be the latest one as it could be also empty */
    2416          30 :                         c = sgetc(f);
    2417          30 :                         if (c != ' ') {
    2418           0 :                                 sungetc(c, f);
    2419           0 :                                 uuid[0] = 0;
    2420             :                         } else {
    2421             :                                 /* read the uuid */
    2422          30 :                                 ret = sgettok(f, uuid, sizeof(uuid));
    2423          30 :                                 if (ret < 0) {
    2424             :                                         /* LCOV_EXCL_START */
    2425             :                                         log_fatal("Invalid 'map' specification in '%s' at line %u\n", path, line);
    2426             :                                         exit(EXIT_FAILURE);
    2427             :                                         /* LCOV_EXCL_STOP */
    2428             :                                 }
    2429             :                         }
    2430             : 
    2431             :                         /* auto configure if configuration is missing */
    2432          30 :                         if (state->no_conf && v_level < LEV_MAX && v_level >= state->level)
    2433           0 :                                 state->level = v_level + 1;
    2434             : 
    2435             :                         /* if we use this parity entry */
    2436          30 :                         if (v_level < state->level) {
    2437             :                                 /* set the parity info */
    2438          30 :                                 pathcpy(state->parity[v_level].uuid, sizeof(state->parity[v_level].uuid), uuid);
    2439          30 :                                 state->parity[v_level].total_blocks = v_total_blocks;
    2440          30 :                                 state->parity[v_level].free_blocks = v_free_blocks;
    2441             :                         }
    2442           5 :                 } else if (strcmp(tag, "sign") == 0) {
    2443             :                         uint32_t sign;
    2444             : 
    2445           0 :                         ret = sgetu32(f, &sign);
    2446           0 :                         if (ret < 0) {
    2447             :                                 /* LCOV_EXCL_START */
    2448             :                                 log_fatal("Invalid 'sign' specification in '%s' at line %u\n", path, line);
    2449             :                                 exit(EXIT_FAILURE);
    2450             :                                 /* LCOV_EXCL_STOP */
    2451             :                         }
    2452             : 
    2453             :                         /* sign check not supported anymore for the text content file */
    2454           5 :                 } else if (tag[0] == 0) {
    2455             :                         /* allow empty lines */
    2456           5 :                         sgetspace(f);
    2457           0 :                 } else if (tag[0] == '#') {
    2458           0 :                         ret = sgetline(f, buffer, sizeof(buffer));
    2459           0 :                         if (ret < 0) {
    2460             :                                 /* LCOV_EXCL_START */
    2461             :                                 log_fatal("Invalid comment in '%s' at line %u\n", path, line);
    2462             :                                 exit(EXIT_FAILURE);
    2463             :                                 /* LCOV_EXCL_STOP */
    2464             :                         }
    2465             :                 } else {
    2466             :                         /* LCOV_EXCL_START */
    2467             :                         log_fatal("Invalid command '%s' in '%s' at line %u\n", tag, path, line);
    2468             :                         exit(EXIT_FAILURE);
    2469             :                         /* LCOV_EXCL_STOP */
    2470             :                 }
    2471             : 
    2472             :                 /* next line */
    2473      364527 :                 c = sgeteol(f);
    2474      364527 :                 if (c == EOF) {
    2475           5 :                         break;
    2476             :                 }
    2477      364522 :                 if (c != '\n') {
    2478             :                         /* LCOV_EXCL_START */
    2479             :                         log_fatal("Extra data in '%s' at line %u\n", path, line);
    2480             :                         exit(EXIT_FAILURE);
    2481             :                         /* LCOV_EXCL_STOP */
    2482             :                 }
    2483      364522 :                 ++line;
    2484      364522 :         }
    2485             : 
    2486           5 :         if (serror(f)) {
    2487             :                 /* LCOV_EXCL_START */
    2488             :                 log_fatal("Error reading the content file '%s' at line %u\n", path, line);
    2489             :                 exit(EXIT_FAILURE);
    2490             :                 /* LCOV_EXCL_STOP */
    2491             :         }
    2492             : 
    2493           5 :         if (file) {
    2494             :                 /* LCOV_EXCL_START */
    2495             :                 log_fatal("Missing 'blk' specification in '%s' at line %u\n", path, line);
    2496             :                 exit(EXIT_FAILURE);
    2497             :                 /* LCOV_EXCL_STOP */
    2498             :         }
    2499             : 
    2500             :         /* check that the stored parity size matches the loaded state */
    2501           5 :         if (blockmax != parity_allocated_size(state)) {
    2502             :                 /* LCOV_EXCL_START */
    2503             :                 log_fatal("Internal inconsistency in parity size in '%s' at line %u\n", path, line);
    2504             :                 exit(EXIT_FAILURE);
    2505             :                 /* LCOV_EXCL_STOP */
    2506             :         }
    2507             : 
    2508             :         /* to be backward compatible with old context files without info */
    2509             :         /* create info records if missing */
    2510       39346 :         for (b = 0; b < blockmax; ++b) {
    2511             :                 /* if the position is used */
    2512       39341 :                 if (info_is_required(state, b)) {
    2513       34905 :                         snapraid_info info = info_get(&state->infoarr, b);
    2514             : 
    2515             :                         /* if no info is present  */
    2516       34905 :                         if (!info) {
    2517             :                                 /* LCOV_EXCL_START */
    2518             :                                 /* set a fake info block */
    2519             :                                 info = info_make(save_time, 0, 0);
    2520             : 
    2521             :                                 /* insert the info in the array */
    2522             :                                 info_set(&state->infoarr, b, info);
    2523             :                                 /* LCOV_EXCL_STOP */
    2524             :                         }
    2525             :                 }
    2526             :         }
    2527             : 
    2528           5 :         msg_verbose("%8u files\n", count_file);
    2529           5 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    2530           5 :         msg_verbose("%8u symlinks\n", count_symlink);
    2531           5 :         msg_verbose("%8u empty dirs\n", count_dir);
    2532           5 : }
    2533             : 
    2534           5 : static void state_write_text(struct snapraid_state* state, STREAM* f)
    2535             : {
    2536             :         unsigned count_file;
    2537             :         unsigned count_hardlink;
    2538             :         unsigned count_symlink;
    2539             :         unsigned count_dir;
    2540             :         tommy_node* i;
    2541             :         block_off_t b;
    2542             :         block_off_t blockmax;
    2543             :         int info_has_rehash;
    2544             :         unsigned l;
    2545             : 
    2546           5 :         count_file = 0;
    2547           5 :         count_hardlink = 0;
    2548           5 :         count_symlink = 0;
    2549           5 :         count_dir = 0;
    2550             : 
    2551             :         /* blocks of all array */
    2552           5 :         blockmax = parity_allocated_size(state);
    2553             : 
    2554             :         /* clear the info for unused blocks */
    2555             :         /* and get some other info */
    2556           5 :         info_has_rehash = 0; /* if there is a rehash info */
    2557       41564 :         for (b = 0; b < blockmax; ++b) {
    2558             :                 /* if the position is used */
    2559       41559 :                 if (position_is_required(state, b)) {
    2560       41559 :                         snapraid_info info = info_get(&state->infoarr, b);
    2561             : 
    2562             :                         /* only if there is some info to store */
    2563       41559 :                         if (info) {
    2564       34905 :                                 if (info_get_rehash(info))
    2565           0 :                                         info_has_rehash = 1;
    2566             :                         }
    2567             :                 } else {
    2568             :                         /* clear any previous info */
    2569           0 :                         info_set(&state->infoarr, b, 0);
    2570             : 
    2571             :                         /* and clear any deleted blocks */
    2572           0 :                         position_clear_deleted(state, b);
    2573             :                 }
    2574             :         }
    2575             : 
    2576           5 :         sputsl("blksize ", f);
    2577           5 :         sputu32(state->block_size, f);
    2578           5 :         sputeol(f);
    2579           5 :         if (serror(f)) {
    2580             :                 /* LCOV_EXCL_START */
    2581             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2582             :                 exit(EXIT_FAILURE);
    2583             :                 /* LCOV_EXCL_STOP */
    2584             :         }
    2585             : 
    2586           5 :         if (state->hash == HASH_MURMUR3) {
    2587           5 :                 sputsl("checksum murmur3", f);
    2588           0 :         } else if (state->hash == HASH_SPOOKY2) {
    2589           0 :                 sputsl("checksum spooky2", f);
    2590             :         } else {
    2591             :                 /* LCOV_EXCL_START */
    2592             :                 log_fatal("Unexpected hash when writing the content file '%s'.\n", serrorfile(f));
    2593             :                 exit(EXIT_FAILURE);
    2594             :                 /* LCOV_EXCL_STOP */
    2595             :         }
    2596           5 :         sputc(' ', f);
    2597           5 :         sputhex(state->hashseed, HASH_SIZE, f);
    2598           5 :         sputeol(f);
    2599           5 :         if (serror(f)) {
    2600             :                 /* LCOV_EXCL_START */
    2601             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2602             :                 exit(EXIT_FAILURE);
    2603             :                 /* LCOV_EXCL_STOP */
    2604             :         }
    2605             : 
    2606             :         /* previous hash only present */
    2607           5 :         if (state->prevhash != HASH_UNDEFINED) {
    2608             :                 /* if at least one rehash tag found, we have to save the previous hash */
    2609           0 :                 if (info_has_rehash) {
    2610           0 :                         if (state->prevhash == HASH_MURMUR3) {
    2611           0 :                                 sputsl("prevchecksum murmur3", f);
    2612           0 :                         } else if (state->prevhash == HASH_SPOOKY2) {
    2613           0 :                                 sputsl("prevchecksum spooky2", f);
    2614             :                         } else {
    2615             :                                 /* LCOV_EXCL_START */
    2616             :                                 log_fatal("Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f));
    2617             :                                 exit(EXIT_FAILURE);
    2618             :                                 /* LCOV_EXCL_STOP */
    2619             :                         }
    2620           0 :                         sputc(' ', f);
    2621           0 :                         sputhex(state->prevhashseed, HASH_SIZE, f);
    2622           0 :                         sputeol(f);
    2623           0 :                         if (serror(f)) {
    2624             :                                 /* LCOV_EXCL_START */
    2625             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2626             :                                 exit(EXIT_FAILURE);
    2627             :                                 /* LCOV_EXCL_STOP */
    2628             :                         }
    2629             :                 }
    2630             :         }
    2631             : 
    2632             :         /* for each map */
    2633          35 :         for (i = state->maplist; i != 0; i = i->next) {
    2634          30 :                 struct snapraid_map* map = i->data;
    2635             :                 struct snapraid_disk* disk;
    2636             : 
    2637             :                 /* find the disk for this mapping */
    2638          30 :                 disk = find_disk(state, map->name);
    2639          30 :                 if (!disk) {
    2640             :                         /* LCOV_EXCL_START */
    2641             :                         log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
    2642             :                         exit(EXIT_FAILURE);
    2643             :                         /* LCOV_EXCL_STOP */
    2644             :                 }
    2645             : 
    2646             :                 /* save the mapping only for not empty disks */
    2647          30 :                 if (!disk_is_empty(disk, blockmax)) {
    2648          30 :                         sputsl("mapping ", f);
    2649          30 :                         sputs(map->name, f);
    2650          30 :                         sputc(' ', f);
    2651          30 :                         sputu32(map->position, f);
    2652          30 :                         sputc(' ', f);
    2653          30 :                         sputu32(map->total_blocks, f);
    2654          30 :                         sputc(' ', f);
    2655          30 :                         sputu32(map->free_blocks, f);
    2656             : 
    2657             :                         /* if there is an uuid, print it */
    2658          30 :                         if (map->uuid[0]) {
    2659          30 :                                 sputc(' ', f);
    2660          30 :                                 sputs(map->uuid, f);
    2661             :                         }
    2662             : 
    2663          30 :                         sputeol(f);
    2664          30 :                         if (serror(f)) {
    2665             :                                 /* LCOV_EXCL_START */
    2666             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2667             :                                 exit(EXIT_FAILURE);
    2668             :                                 /* LCOV_EXCL_STOP */
    2669             :                         }
    2670             : 
    2671             :                         /* mark the disk as with mapping */
    2672          30 :                         disk->mapping_idx = 0;
    2673             :                 } else {
    2674             :                         /* mark the disk as without mapping */
    2675           0 :                         disk->mapping_idx = -1;
    2676             :                 }
    2677             :         }
    2678             : 
    2679             :         /* for each parity */
    2680          35 :         for (l = 0; l < state->level; ++l) {
    2681          30 :                 sputsl("parity ", f);
    2682          30 :                 sputu32(l, f);
    2683          30 :                 sputc(' ', f);
    2684          30 :                 sputu32(state->parity[l].total_blocks, f);
    2685          30 :                 sputc(' ', f);
    2686          30 :                 sputu32(state->parity[l].free_blocks, f);
    2687             : 
    2688             :                 /* if there is an uuid, print it */
    2689          30 :                 if (state->parity[l].uuid[0]) {
    2690          30 :                         sputc(' ', f);
    2691          30 :                         sputs(state->parity[l].uuid, f);
    2692             :                 }
    2693             : 
    2694          30 :                 sputeol(f);
    2695          30 :                 if (serror(f)) {
    2696             :                         /* LCOV_EXCL_START */
    2697             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2698             :                         exit(EXIT_FAILURE);
    2699             :                         /* LCOV_EXCL_STOP */
    2700             :                 }
    2701             :         }
    2702             : 
    2703             :         /* for each disk */
    2704          35 :         for (i = state->disklist; i != 0; i = i->next) {
    2705             :                 tommy_node* j;
    2706          30 :                 struct snapraid_disk* disk = i->data;
    2707             : 
    2708             :                 /* if the disk is not mapped, skip it */
    2709          30 :                 if (disk->mapping_idx < 0)
    2710           0 :                         continue;
    2711             : 
    2712             :                 /* for each file */
    2713       99663 :                 for (j = disk->filelist; j != 0; j = j->next) {
    2714       99633 :                         struct snapraid_file* file = j->data;
    2715             :                         uint64_t size;
    2716             :                         uint64_t mtime_sec;
    2717             :                         int32_t mtime_nsec;
    2718             :                         uint64_t inode;
    2719             : 
    2720       99633 :                         size = file->size;
    2721       99633 :                         mtime_sec = file->mtime_sec;
    2722       99633 :                         mtime_nsec = file->mtime_nsec;
    2723       99633 :                         inode = file->inode;
    2724             : 
    2725       99633 :                         sputsl("file ", f);
    2726       99633 :                         sputs(disk->name, f);
    2727       99633 :                         sputc(' ', f);
    2728       99633 :                         sputu64(size, f);
    2729       99633 :                         sputc(' ', f);
    2730       99633 :                         sputu64(mtime_sec, f);
    2731       99633 :                         if (mtime_nsec != STAT_NSEC_INVALID) {
    2732       99633 :                                 sputc('.', f);
    2733       99633 :                                 sputu32(mtime_nsec, f);
    2734             :                         }
    2735       99633 :                         sputc(' ', f);
    2736       99633 :                         sputu64(inode, f);
    2737       99633 :                         sputc(' ', f);
    2738       99633 :                         sputs(file->sub, f);
    2739       99633 :                         sputeol(f);
    2740       99633 :                         if (serror(f)) {
    2741             :                                 /* LCOV_EXCL_START */
    2742             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2743             :                                 exit(EXIT_FAILURE);
    2744             :                                 /* LCOV_EXCL_STOP */
    2745             :                         }
    2746             : 
    2747             :                         /* for each block in the file */
    2748      344506 :                         for (b = 0; b < file->blockmax; ++b) {
    2749      244873 :                                 struct snapraid_block* block = &file->blockvec[b];
    2750      244873 :                                 unsigned block_state = block_state_get(block);
    2751             : 
    2752             :                                 /* consistency check */
    2753      244873 :                                 if (block != disk_block_get(disk, block->parity_pos)) {
    2754             :                                         /* LCOV_EXCL_START */
    2755             :                                         log_fatal("Internal inconsistency mismatch for block %u state %u in file '%s'\n", block->parity_pos, block_state, file->sub);
    2756             :                                         exit(EXIT_FAILURE);
    2757             :                                         /* LCOV_EXCL_STOP */
    2758             :                                 }
    2759             : 
    2760      244873 :                                 switch (block_state) {
    2761             :                                 case BLOCK_STATE_BLK :
    2762      202580 :                                         sputsl("blk ", f);
    2763      202580 :                                         break;
    2764             :                                 case BLOCK_STATE_CHG :
    2765       42293 :                                         sputsl("chg ", f);
    2766       42293 :                                         break;
    2767             :                                 case BLOCK_STATE_REP :
    2768           0 :                                         sputsl("rep ", f);
    2769           0 :                                         break;
    2770             :                                 default :
    2771             :                                         /* LCOV_EXCL_START */
    2772             :                                         log_fatal("Internal inconsistency in state for block %u state %u\n", block->parity_pos, block_state);
    2773             :                                         exit(EXIT_FAILURE);
    2774             :                                         /* LCOV_EXCL_STOP */
    2775             :                                 }
    2776             : 
    2777      244873 :                                 sputu32(block->parity_pos, f);
    2778             : 
    2779      244873 :                                 sputc(' ', f);
    2780      244873 :                                 sputhex(block->hash, HASH_SIZE, f);
    2781             : 
    2782      244873 :                                 sputeol(f);
    2783      244873 :                                 if (serror(f)) {
    2784             :                                         /* LCOV_EXCL_START */
    2785             :                                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2786             :                                         exit(EXIT_FAILURE);
    2787             :                                         /* LCOV_EXCL_STOP */
    2788             :                                 }
    2789             :                         }
    2790             : 
    2791       99633 :                         ++count_file;
    2792             :                 }
    2793             : 
    2794             :                 /* for each link */
    2795        3215 :                 for (j = disk->linklist; j != 0; j = j->next) {
    2796        3185 :                         struct snapraid_link* link = j->data;
    2797             : 
    2798        3185 :                         switch (link_flag_get(link, FILE_IS_LINK_MASK)) {
    2799             :                         case FILE_IS_HARDLINK :
    2800          30 :                                 sputsl("hardlink ", f);
    2801          30 :                                 ++count_hardlink;
    2802          30 :                                 break;
    2803             :                         case FILE_IS_SYMLINK :
    2804        3155 :                                 sputsl("symlink ", f);
    2805        3155 :                                 ++count_symlink;
    2806        3155 :                                 break;
    2807             :                         }
    2808             : 
    2809        3185 :                         sputs(disk->name, f);
    2810        3185 :                         sputc(' ', f);
    2811        3185 :                         sputs(link->sub, f);
    2812        3185 :                         sputeol(f);
    2813        3185 :                         sputsl("to ", f);
    2814        3185 :                         sputs(link->linkto, f);
    2815        3185 :                         sputeol(f);
    2816        3185 :                         if (serror(f)) {
    2817             :                                 /* LCOV_EXCL_START */
    2818             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2819             :                                 exit(EXIT_FAILURE);
    2820             :                                 /* LCOV_EXCL_STOP */
    2821             :                         }
    2822             :                 }
    2823             : 
    2824             :                 /* for each dir */
    2825          50 :                 for (j = disk->dirlist; j != 0; j = j->next) {
    2826          20 :                         struct snapraid_dir* dir = j->data;
    2827             : 
    2828          20 :                         sputsl("dir ", f);
    2829          20 :                         sputs(disk->name, f);
    2830          20 :                         sputc(' ', f);
    2831          20 :                         sputs(dir->sub, f);
    2832          20 :                         sputeol(f);
    2833          20 :                         if (serror(f)) {
    2834             :                                 /* LCOV_EXCL_START */
    2835             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2836             :                                 exit(EXIT_FAILURE);
    2837             :                                 /* LCOV_EXCL_STOP */
    2838             :                         }
    2839             : 
    2840          20 :                         ++count_dir;
    2841             :                 }
    2842             : 
    2843             :                 /* deleted blocks of the disk */
    2844          30 :                 sputsl("hole ", f);
    2845          30 :                 sputs(disk->name, f);
    2846          30 :                 sputeol(f);
    2847          30 :                 if (serror(f)) {
    2848             :                         /* LCOV_EXCL_START */
    2849             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2850             :                         exit(EXIT_FAILURE);
    2851             :                         /* LCOV_EXCL_STOP */
    2852             :                 }
    2853      249384 :                 for (b = 0; b < blockmax; ++b) {
    2854      249354 :                         if (is_block_deleted(disk, b)) {
    2855         732 :                                 struct snapraid_block* block = disk_block_get(disk, b);
    2856             : 
    2857             :                                 /* consistency check */
    2858         732 :                                 if (block->parity_pos != b) {
    2859             :                                         /* LCOV_EXCL_START */
    2860             :                                         log_fatal("Internal inconsistency in delete position %u/%u!\n", block->parity_pos, b);
    2861             :                                         exit(EXIT_FAILURE);
    2862             :                                         /* LCOV_EXCL_STOP */
    2863             :                                 }
    2864             : 
    2865         732 :                                 sputsl("off ", f);
    2866         732 :                                 sputu32(b, f);
    2867         732 :                                 sputc(' ', f);
    2868         732 :                                 sputhex(block->hash, HASH_SIZE, f);
    2869         732 :                                 sputeol(f);
    2870         732 :                                 if (serror(f)) {
    2871             :                                         /* LCOV_EXCL_START */
    2872             :                                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2873             :                                         exit(EXIT_FAILURE);
    2874             :                                         /* LCOV_EXCL_STOP */
    2875             :                                 }
    2876             :                         }
    2877             :                 }
    2878             :         }
    2879             : 
    2880             :         /* write the info for each block */
    2881       41564 :         for (b = 0; b < blockmax; ++b) {
    2882       41559 :                 snapraid_info info = info_get(&state->infoarr, b);
    2883             : 
    2884             :                 /* save only stuffs different than 0 */
    2885       41559 :                 if (info != 0) {
    2886       34905 :                         sputsl("inf ", f);
    2887             : 
    2888       34905 :                         sputu32(b, f);
    2889       34905 :                         sputc(' ', f);
    2890       34905 :                         sputu32(info, f);
    2891       34905 :                         if (info_get_bad(info)) {
    2892           0 :                                 sputsl(" bad", f);
    2893             :                         }
    2894       34905 :                         if (info_get_rehash(info)) {
    2895           0 :                                 sputsl(" rehash", f);
    2896             :                         }
    2897       34905 :                         sputeol(f);
    2898       34905 :                         if (serror(f)) {
    2899             :                                 /* LCOV_EXCL_START */
    2900             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2901             :                                 exit(EXIT_FAILURE);
    2902             :                                 /* LCOV_EXCL_STOP */
    2903             :                         }
    2904             :                 }
    2905             :         }
    2906             : 
    2907           5 :         if (serror(f)) {
    2908             :                 /* LCOV_EXCL_START */
    2909             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2910             :                 exit(EXIT_FAILURE);
    2911             :                 /* LCOV_EXCL_STOP */
    2912             :         }
    2913             : 
    2914           5 :         msg_verbose("%8u files\n", count_file);
    2915           5 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    2916           5 :         msg_verbose("%8u symlinks\n", count_symlink);
    2917           5 :         msg_verbose("%8u empty dirs\n", count_dir);
    2918           5 : }
    2919             : 
    2920             : /**
    2921             :  * Flush the file checking the final CRC.
    2922             :  * We exploit the fact that the CRC is always stored in the last 4 bytes.
    2923             :  */
    2924           1 : static void decoding_error(const char* path, STREAM* f)
    2925             : {
    2926             :         unsigned char buf[4];
    2927             :         uint32_t crc_stored;
    2928             :         uint32_t crc_computed;
    2929             : 
    2930           1 :         if (seof(f)) {
    2931             :                 /* LCOV_EXCL_START */
    2932             :                 log_fatal("Unexpected end of content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    2933             :                 log_fatal("This content file is truncated. Use an alternate copy.\n");
    2934             :                 exit(EXIT_FAILURE);
    2935             :                 /* LCOV_EXCL_STOP */
    2936             :         }
    2937             : 
    2938           1 :         if (serror(f)) {
    2939             :                 /* LCOV_EXCL_START */
    2940             :                 log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    2941             :                 exit(EXIT_FAILURE);
    2942             :                 /* LCOV_EXCL_STOP */
    2943             :         }
    2944             : 
    2945           1 :         log_fatal("Decoding error in '%s' at offset %" PRIi64 "\n", path, stell(f));
    2946             : 
    2947           1 :         buf[0] = 0;
    2948           1 :         buf[1] = 0;
    2949           1 :         buf[2] = 0;
    2950           1 :         buf[3] = 0;
    2951             : 
    2952             :         /* read until the end of the file */
    2953             :         while (1) {
    2954      855102 :                 int c = sgetc(f);
    2955      855102 :                 if (c == EOF) {
    2956           1 :                         break;
    2957             :                 }
    2958             : 
    2959             :                 /* keep the last four bytes */
    2960      855101 :                 buf[0] = buf[1];
    2961      855101 :                 buf[1] = buf[2];
    2962      855101 :                 buf[2] = buf[3];
    2963      855101 :                 buf[3] = c;
    2964      855101 :         }
    2965             : 
    2966           1 :         if (serror(f)) {
    2967             :                 /* LCOV_EXCL_START */
    2968             :                 log_fatal("Error flushing the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    2969             :                 exit(EXIT_FAILURE);
    2970             :                 /* LCOV_EXCL_STOP */
    2971             :         }
    2972             : 
    2973             :         /* get the stored crc from the last four bytes */
    2974           1 :         crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
    2975             : 
    2976             :         /* get the computed crc */
    2977           1 :         crc_computed = scrc(f);
    2978             : 
    2979             :         /* adjust the stored crc to include itself */
    2980           1 :         crc_stored = crc32c(crc_stored, buf, 4);
    2981             : 
    2982           1 :         if (crc_computed != crc_stored) {
    2983           1 :                 log_fatal("Mismatching CRC in '%s'\n", path);
    2984           1 :                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    2985           1 :                 exit(EXIT_FAILURE);
    2986             :         } else {
    2987           0 :                 log_fatal("The file CRC is correct!\n");
    2988             :         }
    2989           0 : }
    2990             : 
    2991         219 : static void state_read_binary(struct snapraid_state* state, const char* path, STREAM* f)
    2992             : {
    2993             :         block_off_t blockmax;
    2994             :         unsigned count_file;
    2995             :         unsigned count_hardlink;
    2996             :         unsigned count_symlink;
    2997             :         unsigned count_dir;
    2998             :         int crc_checked;
    2999             :         char buffer[PATH_MAX];
    3000             :         int ret;
    3001             :         tommy_array disk_mapping;
    3002             :         uint32_t mapping_max;
    3003             : 
    3004         219 :         blockmax = 0;
    3005         219 :         count_file = 0;
    3006         219 :         count_hardlink = 0;
    3007         219 :         count_symlink = 0;
    3008         219 :         count_dir = 0;
    3009         219 :         crc_checked = 0;
    3010         219 :         mapping_max = 0;
    3011         219 :         tommy_array_init(&disk_mapping);
    3012             : 
    3013         219 :         ret = sread(f, buffer, 12);
    3014         219 :         if (ret < 0) {
    3015             :                 /* LCOV_EXCL_START */
    3016             :                 decoding_error(path, f);
    3017             :                 log_fatal("Invalid header!\n");
    3018             :                 exit(EXIT_FAILURE);
    3019             :                 /* LCOV_EXCL_STOP */
    3020             :         }
    3021             : 
    3022             :         /*
    3023             :          * File format versions:
    3024             :          *  - SNAPCNT1/SnapRAID 4.0 First version.
    3025             :          *  - SNAPCNT2/SnapRAID 7.0 Adds entries 'M' and 'P', to add free_blocks support.
    3026             :          *    The previous 'm' entry is now deprecated, but supported for importing.
    3027             :          *    Similarly for text file, we add 'mapping' and 'parity' deprecating 'map'.
    3028             :          */
    3029         219 :         if (memcmp(buffer, "SNAPCNT1\n\3\0\0", 12) != 0
    3030         219 :                 && memcmp(buffer, "SNAPCNT2\n\3\0\0", 12) != 0) {
    3031             :                 /* LCOV_EXCL_START */
    3032             :                 if (memcmp(buffer, "SNAPCNT", 7) != 0) {
    3033             :                         decoding_error(path, f);
    3034             :                         log_fatal("Invalid header!\n");
    3035             :                 } else {
    3036             :                         log_fatal("The content file '%s' was generated with a newer version of SnapRAID!\n", path);
    3037             :                 }
    3038             :                 exit(EXIT_FAILURE);
    3039             :                 /* LCOV_EXCL_STOP */
    3040             :         }
    3041             : 
    3042             :         while (1) {
    3043             :                 int c;
    3044             : 
    3045             :                 /* read the command */
    3046     3523188 :                 c = sgetc(f);
    3047     3523188 :                 if (c == EOF) {
    3048         218 :                         break;
    3049             :                 }
    3050             : 
    3051     3522970 :                 if (c == 'f') {
    3052             :                         /* file */
    3053             :                         char sub[PATH_MAX];
    3054             :                         uint64_t v_size;
    3055             :                         uint64_t v_mtime_sec;
    3056             :                         uint32_t v_mtime_nsec;
    3057             :                         uint64_t v_inode;
    3058             :                         uint32_t v_idx;
    3059             :                         struct snapraid_file* file;
    3060             :                         struct snapraid_disk* disk;
    3061             :                         uint32_t mapping;
    3062             : 
    3063     3407060 :                         ret = sgetb32(f, &mapping);
    3064     3407060 :                         if (ret < 0 || mapping >= mapping_max) {
    3065             :                                 /* LCOV_EXCL_START */
    3066             :                                 decoding_error(path, f);
    3067             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    3068             :                                 exit(EXIT_FAILURE);
    3069             :                                 /* LCOV_EXCL_STOP */
    3070             :                         }
    3071     3407060 :                         disk = tommy_array_get(&disk_mapping, mapping);
    3072             : 
    3073     3407060 :                         ret = sgetb64(f, &v_size);
    3074     3407060 :                         if (ret < 0) {
    3075             :                                 /* LCOV_EXCL_START */
    3076             :                                 decoding_error(path, f);
    3077             :                                 exit(EXIT_FAILURE);
    3078             :                                 /* LCOV_EXCL_STOP */
    3079             :                         }
    3080             : 
    3081             :                         /* check for impossible file size to avoid to crash for a too big allocation */
    3082     3407060 :                         if (v_size / state->block_size > blockmax) {
    3083             :                                 /* LCOV_EXCL_START */
    3084             :                                 decoding_error(path, f);
    3085             :                                 log_fatal("Internal inconsistency in file size too big!\n");
    3086             :                                 exit(EXIT_FAILURE);
    3087             :                                 /* LCOV_EXCL_STOP */
    3088             :                         }
    3089             : 
    3090     3407060 :                         ret = sgetb64(f, &v_mtime_sec);
    3091     3407060 :                         if (ret < 0) {
    3092             :                                 /* LCOV_EXCL_START */
    3093             :                                 decoding_error(path, f);
    3094             :                                 exit(EXIT_FAILURE);
    3095             :                                 /* LCOV_EXCL_STOP */
    3096             :                         }
    3097             : 
    3098     3407060 :                         ret = sgetb32(f, &v_mtime_nsec);
    3099     3407060 :                         if (ret < 0) {
    3100             :                                 /* LCOV_EXCL_START */
    3101             :                                 decoding_error(path, f);
    3102             :                                 exit(EXIT_FAILURE);
    3103             :                                 /* LCOV_EXCL_STOP */
    3104             :                         }
    3105             : 
    3106             :                         /* STAT_NSEC_INVALID is encoded as 0 */
    3107     3407060 :                         if (v_mtime_nsec == 0)
    3108           0 :                                 v_mtime_nsec = STAT_NSEC_INVALID;
    3109             :                         else
    3110     3407060 :                                 --v_mtime_nsec;
    3111             : 
    3112     3407060 :                         ret = sgetb64(f, &v_inode);
    3113     3407060 :                         if (ret < 0) {
    3114             :                                 /* LCOV_EXCL_START */
    3115             :                                 decoding_error(path, f);
    3116             :                                 exit(EXIT_FAILURE);
    3117             :                                 /* LCOV_EXCL_STOP */
    3118             :                         }
    3119             : 
    3120     3407060 :                         ret = sgetbs(f, sub, sizeof(sub));
    3121     3407060 :                         if (ret < 0) {
    3122             :                                 /* LCOV_EXCL_START */
    3123             :                                 decoding_error(path, f);
    3124             :                                 exit(EXIT_FAILURE);
    3125             :                                 /* LCOV_EXCL_STOP */
    3126             :                         }
    3127     3407060 :                         if (!*sub) {
    3128             :                                 /* LCOV_EXCL_START */
    3129             :                                 decoding_error(path, f);
    3130             :                                 exit(EXIT_FAILURE);
    3131             :                                 /* LCOV_EXCL_STOP */
    3132             :                         }
    3133             : 
    3134             :                         /* allocate the file */
    3135     3407060 :                         file = file_alloc(state->block_size, sub, v_size, v_mtime_sec, v_mtime_nsec, v_inode, 0);
    3136             : 
    3137             :                         /* insert the file in the file containers */
    3138     3407060 :                         tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
    3139     3407060 :                         tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
    3140     3407060 :                         tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
    3141     3407060 :                         tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
    3142             : 
    3143             :                         /* reads all the blocks */
    3144     3407060 :                         v_idx = 0;
    3145    10240996 :                         while (v_idx < file->blockmax) {
    3146             :                                 block_off_t v_pos;
    3147             :                                 uint32_t v_count;
    3148             : 
    3149             :                                 /* get the "subcommand */
    3150     3426876 :                                 c = sgetc(f);
    3151             : 
    3152     3426876 :                                 ret = sgetb32(f, &v_pos);
    3153     3426876 :                                 if (ret < 0) {
    3154             :                                         /* LCOV_EXCL_START */
    3155             :                                         decoding_error(path, f);
    3156             :                                         exit(EXIT_FAILURE);
    3157             :                                         /* LCOV_EXCL_STOP */
    3158             :                                 }
    3159             : 
    3160     3426876 :                                 ret = sgetb32(f, &v_count);
    3161     3426876 :                                 if (ret < 0) {
    3162             :                                         /* LCOV_EXCL_START */
    3163             :                                         decoding_error(path, f);
    3164             :                                         exit(EXIT_FAILURE);
    3165             :                                         /* LCOV_EXCL_STOP */
    3166             :                                 }
    3167             : 
    3168     3426876 :                                 if (v_idx + v_count > file->blockmax) {
    3169             :                                         /* LCOV_EXCL_START */
    3170             :                                         decoding_error(path, f);
    3171             :                                         log_fatal("Internal inconsistency in block number!\n");
    3172             :                                         exit(EXIT_FAILURE);
    3173             :                                         /* LCOV_EXCL_STOP */
    3174             :                                 }
    3175             : 
    3176     3426876 :                                 if (v_pos + v_count > blockmax) {
    3177             :                                         /* LCOV_EXCL_START */
    3178             :                                         log_fatal("Internal inconsistency in block size %u/%u!\n", blockmax, v_pos + v_count);
    3179             :                                         decoding_error(path, f);
    3180             :                                         exit(EXIT_FAILURE);
    3181             :                                         /* LCOV_EXCL_START */
    3182             :                                 }
    3183             : 
    3184             :                                 /* grow the array */
    3185             :                                 tommy_arrayblk_grow(&disk->blockarr, v_pos + v_count);
    3186             : 
    3187             :                                 /* fill the blocks in the run */
    3188             :                                 while (v_count) {
    3189             :                                         struct snapraid_block* block = &file->blockvec[v_idx];
    3190             : 
    3191             :                                         if (block->parity_pos != POS_INVALID) {
    3192             :                                                 /* LCOV_EXCL_START */
    3193             :                                                 decoding_error(path, f);
    3194             :                                                 log_fatal("Internal inconsistency in block position!\n");
    3195             :                                                 exit(EXIT_FAILURE);
    3196             :                                                 /* LCOV_EXCL_STOP */
    3197             :                                         }
    3198             : 
    3199     8379092 :                                         switch (c) {
    3200             :                                         case 'b' :
    3201     8225914 :                                                 block_state_set(block, BLOCK_STATE_BLK);
    3202     8225914 :                                                 break;
    3203             :                                         case 'n' :
    3204             :                                                 /* deprecated NEW blocks are converted to CHG ones */
    3205           0 :                                                 block_state_set(block, BLOCK_STATE_CHG);
    3206           0 :                                                 break;
    3207             :                                         case 'g' :
    3208      109750 :                                                 block_state_set(block, BLOCK_STATE_CHG);
    3209      109750 :                                                 break;
    3210             :                                         case 'p' :
    3211       43428 :                                                 block_state_set(block, BLOCK_STATE_REP);
    3212       43428 :                                                 break;
    3213             :                                         default :
    3214             :                                                 /* LCOV_EXCL_START */
    3215             :                                                 decoding_error(path, f);
    3216             :                                                 log_fatal("Invalid block type!\n");
    3217             :                                                 exit(EXIT_FAILURE);
    3218             :                                                 /* LCOV_EXCL_STOP */
    3219             :                                         }
    3220             : 
    3221     8379092 :                                         block->parity_pos = v_pos;
    3222             : 
    3223             :                                         /* read the hash only for 'blk/chg/rep', and not for 'new' */
    3224     8379092 :                                         if (c != 'n') {
    3225     8379092 :                                                 ret = sread(f, block->hash, HASH_SIZE);
    3226     8379092 :                                                 if (ret < 0) {
    3227             :                                                         /* LCOV_EXCL_START */
    3228             :                                                         decoding_error(path, f);
    3229             :                                                         exit(EXIT_FAILURE);
    3230             :                                                         /* LCOV_EXCL_STOP */
    3231             :                                                 }
    3232             :                                         } else {
    3233             :                                                 /* set the ZERO hash for deprecated NEW blocks */
    3234           0 :                                                 hash_zero_set(block->hash);
    3235             :                                         }
    3236             : 
    3237             :                                         /* if the block contains a hash of past data */
    3238             :                                         /* and we are clearing such undeterminated hashes */
    3239     8379092 :                                         if (state->clear_past_hash
    3240     2150134 :                                                 && block_has_past_hash(block)
    3241             :                                         ) {
    3242             :                                                 /* set the hash value to INVALID */
    3243       43542 :                                                 hash_invalid_set(block->hash);
    3244             :                                         }
    3245             : 
    3246             :                                         /* if we are disabling the copy optimization */
    3247             :                                         /* we want also to clear any already previously stored information */
    3248             :                                         /* in other sync commands */
    3249             :                                         /* note that this is required only in sync, and we detect */
    3250             :                                         /* this using the clear_past_hash flag */
    3251     8379092 :                                         if (state->clear_past_hash
    3252     2150134 :                                                 && state->opt.force_nocopy
    3253       14115 :                                                 && block_state_get(block) == BLOCK_STATE_REP
    3254             :                                         ) {
    3255             :                                                 /* set the hash value to INVALID */
    3256           1 :                                                 hash_invalid_set(block->hash);
    3257             :                                                 /* convert from REP to CHG block */
    3258           1 :                                                 block_state_set(block, BLOCK_STATE_CHG);
    3259             :                                         }
    3260             : 
    3261             :                                         /* if we want a full sync, marks block as invalid parity */
    3262             :                                         /* note that we do this after the force_nocopy option */
    3263             :                                         /* to avoid to mixup the two things */
    3264     8379092 :                                         if (state->opt.force_full
    3265       27747 :                                                 && block_state_get(block) == BLOCK_STATE_BLK) {
    3266             :                                                 /* convert from BLK to REP */
    3267       27747 :                                                 block_state_set(block, BLOCK_STATE_REP);
    3268             :                                         }
    3269             : 
    3270             :                                         /* we must not overwrite existing blocks */
    3271     8379092 :                                         if (disk_block_get(disk, v_pos) != BLOCK_EMPTY) {
    3272             :                                                 /* LCOV_EXCL_START */
    3273             :                                                 decoding_error(path, f);
    3274             :                                                 log_fatal("Internal inconsistency in block existence!\n");
    3275             :                                                 exit(EXIT_FAILURE);
    3276             :                                                 /* LCOV_EXCL_STOP */
    3277             :                                         }
    3278             : 
    3279             :                                         /* insert the block in the block array */
    3280     8379092 :                                         tommy_arrayblk_set(&disk->blockarr, v_pos, block);
    3281             : 
    3282             :                                         /* go to the next block */
    3283     8379092 :                                         ++v_idx;
    3284     8379092 :                                         ++v_pos;
    3285     8379092 :                                         --v_count;
    3286             :                                 }
    3287             :                         }
    3288             : 
    3289             :                         /* stat */
    3290     3407060 :                         ++count_file;
    3291      115910 :                 } else if (c == 'i') {
    3292             :                         /* "inf" command */
    3293             :                         snapraid_info info;
    3294             :                         uint32_t v_pos;
    3295             :                         uint32_t v_oldest;
    3296             : 
    3297         218 :                         ret = sgetb32(f, &v_oldest);
    3298         218 :                         if (ret < 0) {
    3299             :                                 /* LCOV_EXCL_START */
    3300             :                                 decoding_error(path, f);
    3301             :                                 exit(EXIT_FAILURE);
    3302             :                                 /* LCOV_EXCL_STOP */
    3303             :                         }
    3304             : 
    3305         218 :                         v_pos = 0;
    3306      288233 :                         while (v_pos < blockmax) {
    3307             :                                 int rehash;
    3308             :                                 int bad;
    3309             :                                 uint32_t t;
    3310             :                                 uint32_t flag;
    3311             :                                 uint32_t v_count;
    3312             : 
    3313      287797 :                                 ret = sgetb32(f, &v_count);
    3314      287797 :                                 if (ret < 0) {
    3315             :                                         /* LCOV_EXCL_START */
    3316             :                                         decoding_error(path, f);
    3317             :                                         exit(EXIT_FAILURE);
    3318             :                                         /* LCOV_EXCL_STOP */
    3319             :                                 }
    3320             : 
    3321      287797 :                                 if (v_pos + v_count > blockmax) {
    3322             :                                         /* LCOV_EXCL_START */
    3323             :                                         log_fatal("Internal inconsistency in info size %u/%u!\n", blockmax, v_pos + v_count);
    3324             :                                         decoding_error(path, f);
    3325             :                                         exit(EXIT_FAILURE);
    3326             :                                         /* LCOV_EXCL_STOP */
    3327             :                                 }
    3328             : 
    3329      287797 :                                 ret = sgetb32(f, &flag);
    3330      287797 :                                 if (ret < 0) {
    3331             :                                         /* LCOV_EXCL_START */
    3332             :                                         decoding_error(path, f);
    3333             :                                         exit(EXIT_FAILURE);
    3334             :                                         /* LCOV_EXCL_STOP */
    3335             :                                 }
    3336             : 
    3337             :                                 /* if there is an info */
    3338      287797 :                                 if ((flag & 1) != 0) {
    3339             :                                         /* read the time */
    3340      287782 :                                         ret = sgetb32(f, &t);
    3341      287782 :                                         if (ret < 0) {
    3342             :                                                 /* LCOV_EXCL_START */
    3343             :                                                 decoding_error(path, f);
    3344             :                                                 exit(EXIT_FAILURE);
    3345             :                                                 /* LCOV_EXCL_STOP */
    3346             :                                         }
    3347             : 
    3348             :                                         /* analyze the flags */
    3349      287782 :                                         bad = (flag & 2) != 0;
    3350      287782 :                                         rehash = (flag & 4) != 0;
    3351             : 
    3352      287782 :                                         if (rehash && state->prevhash == HASH_UNDEFINED) {
    3353             :                                                 /* LCOV_EXCL_START */
    3354             :                                                 decoding_error(path, f);
    3355             :                                                 log_fatal("Internal inconsistency for missing previous checksum!\n");
    3356             :                                                 exit(EXIT_FAILURE);
    3357             :                                                 /* LCOV_EXCL_STOP */
    3358             :                                         }
    3359             : 
    3360      287782 :                                         info = info_make(t + v_oldest, bad, rehash);
    3361             :                                 } else {
    3362          15 :                                         info = 0;
    3363             :                                 }
    3364             : 
    3365     2034434 :                                 while (v_count) {
    3366             :                                         /* insert the info in the array */
    3367     1458840 :                                         info_set(&state->infoarr, v_pos, info);
    3368             : 
    3369             :                                         /* ensure that an info is present only for used positions */
    3370     1458840 :                                         if (info_is_required(state, v_pos)) {
    3371     1400399 :                                                 if (!info) {
    3372             :                                                         /* LCOV_EXCL_START */
    3373             :                                                         decoding_error(path, f);
    3374             :                                                         log_fatal("Internal inconsistency for missing info!\n");
    3375             :                                                         exit(EXIT_FAILURE);
    3376             :                                                         /* LCOV_EXCL_STOP */
    3377             :                                                 }
    3378             :                                         } else {
    3379             :                                                 /* extra info are accepted for backward compatibility */
    3380             :                                                 /* they are discarged at the first write */
    3381             :                                         }
    3382             : 
    3383             :                                         /* go to next block */
    3384     1458840 :                                         ++v_pos;
    3385     1458840 :                                         --v_count;
    3386             :                                 }
    3387             :                         }
    3388      115692 :                 } else if (c == 'h') {
    3389             :                         /* hole */
    3390             :                         uint32_t v_pos;
    3391             :                         struct snapraid_disk* disk;
    3392             :                         uint32_t mapping;
    3393             : 
    3394        1241 :                         ret = sgetb32(f, &mapping);
    3395        1241 :                         if (ret < 0 || mapping >= mapping_max) {
    3396             :                                 /* LCOV_EXCL_START */
    3397             :                                 decoding_error(path, f);
    3398             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    3399             :                                 exit(EXIT_FAILURE);
    3400             :                                 /* LCOV_EXCL_STOP */
    3401             :                         }
    3402        1241 :                         disk = tommy_array_get(&disk_mapping, mapping);
    3403             : 
    3404        1241 :                         v_pos = 0;
    3405        3765 :                         while (v_pos < blockmax) {
    3406             :                                 uint32_t v_count;
    3407             : 
    3408        1283 :                                 ret = sgetb32(f, &v_count);
    3409        1283 :                                 if (ret < 0) {
    3410             :                                         /* LCOV_EXCL_START */
    3411             :                                         decoding_error(path, f);
    3412             :                                         exit(EXIT_FAILURE);
    3413             :                                         /* LCOV_EXCL_STOP */
    3414             :                                 }
    3415             : 
    3416        1283 :                                 if (v_pos + v_count > blockmax) {
    3417             :                                         /* LCOV_EXCL_START */
    3418             :                                         log_fatal("Internal inconsistency in hole size %u/%u!\n", blockmax, v_pos + v_count);
    3419             :                                         decoding_error(path, f);
    3420             :                                         exit(EXIT_FAILURE);
    3421             :                                         /* LCOV_EXCL_STOP */
    3422             :                                 }
    3423             : 
    3424             :                                 /* get the subcommand */
    3425        1283 :                                 c = sgetc(f);
    3426             : 
    3427        1283 :                                 switch (c) {
    3428             :                                 case 'o' :
    3429             :                                         /* grow the array */
    3430          21 :                                         tommy_arrayblk_grow(&disk->blockarr, v_pos + v_count);
    3431             : 
    3432             :                                         /* if it's a run of deleted blocks */
    3433       27543 :                                         while (v_count) {
    3434             :                                                 struct snapraid_deleted* deleted;
    3435             : 
    3436             :                                                 /* allocate a new deleted block */
    3437       27501 :                                                 deleted = deleted_alloc();
    3438             : 
    3439             :                                                 /* set the position */
    3440       27501 :                                                 deleted->block.parity_pos = v_pos;
    3441             : 
    3442             :                                                 /* read the hash */
    3443       27501 :                                                 ret = sread(f, deleted->block.hash, HASH_SIZE);
    3444       27501 :                                                 if (ret < 0) {
    3445             :                                                         /* LCOV_EXCL_START */
    3446             :                                                         decoding_error(path, f);
    3447             :                                                         exit(EXIT_FAILURE);
    3448             :                                                         /* LCOV_EXCL_STOP */
    3449             :                                                 }
    3450             : 
    3451             :                                                 /* if we are clearing undeterminated hashes */
    3452       27501 :                                                 if (state->clear_past_hash) {
    3453             :                                                         /* set the hash value to INVALID */
    3454        9047 :                                                         hash_invalid_set(deleted->block.hash);
    3455             :                                                 }
    3456             : 
    3457             :                                                 /* we must not overwrite existing blocks */
    3458       27501 :                                                 if (disk_block_get(disk, v_pos) != BLOCK_EMPTY) {
    3459             :                                                         /* LCOV_EXCL_START */
    3460             :                                                         log_fatal("Internal inconsistency for used hole at pos %u!\n", v_pos);
    3461             :                                                         if (state->opt.skip_content_check) {
    3462             :                                                                 log_fatal("Overriding as used.\n");
    3463             :                                                                 deleted_free(deleted);
    3464             :                                                         } else {
    3465             :                                                                 decoding_error(path, f);
    3466             :                                                                 exit(EXIT_FAILURE);
    3467             :                                                         }
    3468             :                                                         /* LCOV_EXCL_STOP */
    3469             :                                                 } else {
    3470             :                                                         /* insert it in the list of deleted blocks */
    3471       27501 :                                                         tommy_list_insert_tail(&disk->deletedlist, &deleted->node, deleted);
    3472             : 
    3473             :                                                         /* insert the block in the block array */
    3474       27501 :                                                         tommy_arrayblk_set(&disk->blockarr, v_pos, &deleted->block);
    3475             :                                                 }
    3476             : 
    3477             :                                                 /* go to next block */
    3478       27501 :                                                 ++v_pos;
    3479       27501 :                                                 --v_count;
    3480             :                                         }
    3481          21 :                                         break;
    3482             :                                 case 'O' :
    3483             :                                         /* go to the next run */
    3484        1262 :                                         v_pos += v_count;
    3485        1262 :                                         break;
    3486             :                                 default :
    3487             :                                         /* LCOV_EXCL_START */
    3488             :                                         decoding_error(path, f);
    3489             :                                         log_fatal("Invalid hole type!\n");
    3490             :                                         exit(EXIT_FAILURE);
    3491             :                                         /* LCOV_EXCL_STOP */
    3492             :                                 }
    3493             :                         }
    3494      114451 :                 } else if (c == 's') {
    3495             :                         /* symlink */
    3496             :                         char sub[PATH_MAX];
    3497             :                         char linkto[PATH_MAX];
    3498             :                         struct snapraid_link* link;
    3499             :                         struct snapraid_disk* disk;
    3500             :                         uint32_t mapping;
    3501             : 
    3502      109172 :                         ret = sgetb32(f, &mapping);
    3503      109172 :                         if (ret < 0 || mapping >= mapping_max) {
    3504             :                                 /* LCOV_EXCL_START */
    3505             :                                 decoding_error(path, f);
    3506             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    3507             :                                 exit(EXIT_FAILURE);
    3508             :                                 /* LCOV_EXCL_STOP */
    3509             :                         }
    3510      109172 :                         disk = tommy_array_get(&disk_mapping, mapping);
    3511             : 
    3512      109172 :                         ret = sgetbs(f, sub, sizeof(sub));
    3513      109172 :                         if (ret < 0) {
    3514             :                                 /* LCOV_EXCL_START */
    3515             :                                 decoding_error(path, f);
    3516             :                                 exit(EXIT_FAILURE);
    3517             :                                 /* LCOV_EXCL_STOP */
    3518             :                         }
    3519             : 
    3520      109172 :                         ret = sgetbs(f, linkto, sizeof(linkto));
    3521      109172 :                         if (ret < 0) {
    3522             :                                 /* LCOV_EXCL_START */
    3523             :                                 decoding_error(path, f);
    3524             :                                 exit(EXIT_FAILURE);
    3525             :                                 /* LCOV_EXCL_STOP */
    3526             :                         }
    3527             : 
    3528      109172 :                         if (!*sub || !*linkto) {
    3529             :                                 /* LCOV_EXCL_START */
    3530             :                                 decoding_error(path, f);
    3531             :                                 exit(EXIT_FAILURE);
    3532             :                                 /* LCOV_EXCL_STOP */
    3533             :                         }
    3534             : 
    3535             :                         /* allocate the link as symbolic link */
    3536      109172 :                         link = link_alloc(sub, linkto, FILE_IS_SYMLINK);
    3537             : 
    3538             :                         /* insert the link in the link containers */
    3539      109172 :                         tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
    3540      109172 :                         tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
    3541             : 
    3542             :                         /* stat */
    3543      109172 :                         ++count_symlink;
    3544        5279 :                 } else if (c == 'a') {
    3545             :                         /* hardlink */
    3546             :                         char sub[PATH_MAX];
    3547             :                         char linkto[PATH_MAX];
    3548             :                         struct snapraid_link* link;
    3549             :                         struct snapraid_disk* disk;
    3550             :                         uint32_t mapping;
    3551             : 
    3552        1090 :                         ret = sgetb32(f, &mapping);
    3553        1090 :                         if (ret < 0 || mapping >= mapping_max) {
    3554             :                                 /* LCOV_EXCL_START */
    3555             :                                 decoding_error(path, f);
    3556             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    3557             :                                 exit(EXIT_FAILURE);
    3558             :                                 /* LCOV_EXCL_STOP */
    3559             :                         }
    3560        1090 :                         disk = tommy_array_get(&disk_mapping, mapping);
    3561             : 
    3562        1090 :                         ret = sgetbs(f, sub, sizeof(sub));
    3563        1090 :                         if (ret < 0) {
    3564             :                                 /* LCOV_EXCL_START */
    3565             :                                 decoding_error(path, f);
    3566             :                                 exit(EXIT_FAILURE);
    3567             :                                 /* LCOV_EXCL_STOP */
    3568             :                         }
    3569             : 
    3570        1090 :                         ret = sgetbs(f, linkto, sizeof(linkto));
    3571        1090 :                         if (ret < 0) {
    3572             :                                 /* LCOV_EXCL_START */
    3573             :                                 decoding_error(path, f);
    3574             :                                 exit(EXIT_FAILURE);
    3575             :                                 /* LCOV_EXCL_STOP */
    3576             :                         }
    3577             : 
    3578        1090 :                         if (!*sub || !*linkto) {
    3579             :                                 /* LCOV_EXCL_START */
    3580             :                                 decoding_error(path, f);
    3581             :                                 exit(EXIT_FAILURE);
    3582             :                                 /* LCOV_EXCL_STOP */
    3583             :                         }
    3584             : 
    3585             :                         /* allocate the link as hard link */
    3586        1090 :                         link = link_alloc(sub, linkto, FILE_IS_HARDLINK);
    3587             : 
    3588             :                         /* insert the link in the link containers */
    3589        1090 :                         tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
    3590        1090 :                         tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
    3591             : 
    3592             :                         /* stat */
    3593        1090 :                         ++count_hardlink;
    3594        4189 :                 } else if (c == 'r') {
    3595             :                         /* dir */
    3596             :                         char sub[PATH_MAX];
    3597             :                         struct snapraid_dir* dir;
    3598             :                         struct snapraid_disk* disk;
    3599             :                         uint32_t mapping;
    3600             : 
    3601         730 :                         ret = sgetb32(f, &mapping);
    3602         730 :                         if (ret < 0 || mapping >= mapping_max) {
    3603             :                                 /* LCOV_EXCL_START */
    3604             :                                 decoding_error(path, f);
    3605             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    3606             :                                 exit(EXIT_FAILURE);
    3607             :                                 /* LCOV_EXCL_STOP */
    3608             :                         }
    3609         730 :                         disk = tommy_array_get(&disk_mapping, mapping);
    3610             : 
    3611         730 :                         ret = sgetbs(f, sub, sizeof(sub));
    3612         730 :                         if (ret < 0) {
    3613             :                                 /* LCOV_EXCL_START */
    3614             :                                 decoding_error(path, f);
    3615             :                                 exit(EXIT_FAILURE);
    3616             :                                 /* LCOV_EXCL_STOP */
    3617             :                         }
    3618             : 
    3619         730 :                         if (!*sub) {
    3620             :                                 /* LCOV_EXCL_START */
    3621             :                                 decoding_error(path, f);
    3622             :                                 exit(EXIT_FAILURE);
    3623             :                                 /* LCOV_EXCL_STOP */
    3624             :                         }
    3625             : 
    3626             :                         /* allocate the dir */
    3627         730 :                         dir = dir_alloc(sub);
    3628             : 
    3629             :                         /* insert the dir in the dir containers */
    3630         730 :                         tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
    3631         730 :                         tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
    3632             : 
    3633             :                         /* stat */
    3634         730 :                         ++count_dir;
    3635        3459 :                 } else if (c == 'c') {
    3636             :                         /* get the subcommand */
    3637         219 :                         c = sgetc(f);
    3638             : 
    3639         219 :                         switch (c) {
    3640             :                         case 'u' :
    3641         132 :                                 state->hash = HASH_MURMUR3;
    3642         132 :                                 break;
    3643             :                         case 'k' :
    3644          87 :                                 state->hash = HASH_SPOOKY2;
    3645          87 :                                 break;
    3646             :                         default :
    3647             :                                 /* LCOV_EXCL_START */
    3648             :                                 decoding_error(path, f);
    3649             :                                 log_fatal("Invalid checksum!\n");
    3650             :                                 exit(EXIT_FAILURE);
    3651             :                                 /* LCOV_EXCL_STOP */
    3652             :                         }
    3653             : 
    3654             :                         /* read the seed */
    3655         219 :                         ret = sread(f, state->hashseed, HASH_SIZE);
    3656         219 :                         if (ret < 0) {
    3657             :                                 /* LCOV_EXCL_START */
    3658             :                                 decoding_error(path, f);
    3659             :                                 exit(EXIT_FAILURE);
    3660             :                                 /* LCOV_EXCL_STOP */
    3661             :                         }
    3662        3240 :                 } else if (c == 'C') {
    3663             :                         /* get the subcommand */
    3664          87 :                         c = sgetc(f);
    3665             : 
    3666          87 :                         switch (c) {
    3667             :                         case 'u' :
    3668          87 :                                 state->prevhash = HASH_MURMUR3;
    3669          87 :                                 break;
    3670             :                         case 'k' :
    3671           0 :                                 state->prevhash = HASH_SPOOKY2;
    3672           0 :                                 break;
    3673             :                         default :
    3674             :                                 /* LCOV_EXCL_START */
    3675             :                                 decoding_error(path, f);
    3676             :                                 log_fatal("Invalid checksum!\n");
    3677             :                                 exit(EXIT_FAILURE);
    3678             :                                 /* LCOV_EXCL_STOP */
    3679             :                         }
    3680             : 
    3681             :                         /* read the seed */
    3682          87 :                         ret = sread(f, state->prevhashseed, HASH_SIZE);
    3683          87 :                         if (ret < 0) {
    3684             :                                 /* LCOV_EXCL_START */
    3685             :                                 decoding_error(path, f);
    3686             :                                 exit(EXIT_FAILURE);
    3687             :                                 /* LCOV_EXCL_STOP */
    3688             :                         }
    3689        3153 :                 } else if (c == 'z') {
    3690             :                         block_off_t blksize;
    3691             : 
    3692         219 :                         ret = sgetb32(f, &blksize);
    3693         219 :                         if (ret < 0) {
    3694             :                                 /* LCOV_EXCL_START */
    3695             :                                 decoding_error(path, f);
    3696             :                                 exit(EXIT_FAILURE);
    3697             :                                 /* LCOV_EXCL_STOP */
    3698             :                         }
    3699             : 
    3700             :                         /* without configuration, auto assign the block size */
    3701         219 :                         if (state->no_conf) {
    3702           1 :                                 state->block_size = blksize;
    3703             :                         }
    3704             : 
    3705         219 :                         if (blksize != state->block_size) {
    3706             :                                 /* LCOV_EXCL_START */
    3707             :                                 decoding_error(path, f);
    3708             :                                 log_fatal("Mismatching 'blksize' and 'blocksize' specification!\n");
    3709             :                                 log_fatal("Please restore the 'blocksize' value in the configuration file to '%u'\n", blksize / 1024);
    3710             :                                 exit(EXIT_FAILURE);
    3711             :                                 /* LCOV_EXCL_STOP */
    3712             :                         }
    3713        2934 :                 } else if (c == 'x') {
    3714         219 :                         ret = sgetb32(f, &blockmax);
    3715         219 :                         if (ret < 0) {
    3716             :                                 /* LCOV_EXCL_START */
    3717             :                                 decoding_error(path, f);
    3718             :                                 exit(EXIT_FAILURE);
    3719             :                                 /* LCOV_EXCL_STOP */
    3720             :                         }
    3721        3962 :                 } else if (c == 'm' || c == 'M') {
    3722             :                         struct snapraid_map* map;
    3723             :                         char uuid[UUID_MAX];
    3724             :                         uint32_t v_pos;
    3725             :                         uint32_t v_total_blocks;
    3726             :                         uint32_t v_free_blocks;
    3727             :                         struct snapraid_disk* disk;
    3728             : 
    3729        1247 :                         ret = sgetbs(f, buffer, sizeof(buffer));
    3730        1247 :                         if (ret < 0) {
    3731             :                                 /* LCOV_EXCL_START */
    3732             :                                 decoding_error(path, f);
    3733             :                                 exit(EXIT_FAILURE);
    3734             :                                 /* LCOV_EXCL_STOP */
    3735             :                         }
    3736             : 
    3737        1247 :                         ret = sgetb32(f, &v_pos);
    3738        1247 :                         if (ret < 0) {
    3739             :                                 /* LCOV_EXCL_START */
    3740             :                                 decoding_error(path, f);
    3741             :                                 exit(EXIT_FAILURE);
    3742             :                                 /* LCOV_EXCL_STOP */
    3743             :                         }
    3744             : 
    3745             :                         /* from SnapRAID 7.0 the 'M' command includes the free space */
    3746        1247 :                         if (c == 'M') {
    3747        1247 :                                 ret = sgetb32(f, &v_total_blocks);
    3748        1247 :                                 if (ret < 0) {
    3749             :                                         /* LCOV_EXCL_START */
    3750             :                                         decoding_error(path, f);
    3751             :                                         exit(EXIT_FAILURE);
    3752             :                                         /* LCOV_EXCL_STOP */
    3753             :                                 }
    3754             : 
    3755        1247 :                                 ret = sgetb32(f, &v_free_blocks);
    3756        1247 :                                 if (ret < 0) {
    3757             :                                         /* LCOV_EXCL_START */
    3758             :                                         decoding_error(path, f);
    3759             :                                         exit(EXIT_FAILURE);
    3760             :                                         /* LCOV_EXCL_STOP */
    3761             :                                 }
    3762             :                         } else {
    3763           0 :                                 v_total_blocks = 0;
    3764           0 :                                 v_free_blocks = 0;
    3765             :                         }
    3766             : 
    3767             :                         /* read the uuid */
    3768        1247 :                         ret = sgetbs(f, uuid, sizeof(uuid));
    3769        1247 :                         if (ret < 0) {
    3770             :                                 /* LCOV_EXCL_START */
    3771             :                                 decoding_error(path, f);
    3772             :                                 exit(EXIT_FAILURE);
    3773             :                                 /* LCOV_EXCL_STOP */
    3774             :                         }
    3775             : 
    3776        1247 :                         map = map_alloc(buffer, v_pos, v_total_blocks, v_free_blocks, uuid);
    3777             : 
    3778        1247 :                         tommy_list_insert_tail(&state->maplist, &map->node, map);
    3779             : 
    3780             :                         /* find the disk */
    3781        1247 :                         disk = find_disk(state, buffer);
    3782        1247 :                         if (!disk) {
    3783             :                                 /* LCOV_EXCL_START */
    3784             :                                 decoding_error(path, f);
    3785             :                                 log_fatal("Disk named '%s' not present in the configuration file!\n", buffer);
    3786             :                                 log_fatal("If you have removed it from the configuration file, please restore it\n");
    3787             :                                 exit(EXIT_FAILURE);
    3788             :                                 /* LCOV_EXCL_STOP */
    3789             :                         }
    3790             : 
    3791             :                         /* insert in the mapping vector */
    3792        1247 :                         tommy_array_grow(&disk_mapping, mapping_max + 1);
    3793        1247 :                         tommy_array_set(&disk_mapping, mapping_max, disk);
    3794        1247 :                         ++mapping_max;
    3795        1468 :                 } else if (c == 'P') {
    3796             :                         /* from SnapRAID 7.0 the 'P' command includes the free space */
    3797             :                         char uuid[UUID_MAX];
    3798             :                         uint32_t v_level;
    3799             :                         uint32_t v_total_blocks;
    3800             :                         uint32_t v_free_blocks;
    3801             : 
    3802        1249 :                         ret = sgetb32(f, &v_level);
    3803        1249 :                         if (ret < 0) {
    3804             :                                 /* LCOV_EXCL_START */
    3805             :                                 decoding_error(path, f);
    3806             :                                 exit(EXIT_FAILURE);
    3807             :                                 /* LCOV_EXCL_STOP */
    3808             :                         }
    3809             : 
    3810        1249 :                         ret = sgetb32(f, &v_total_blocks);
    3811        1249 :                         if (ret < 0) {
    3812             :                                 /* LCOV_EXCL_START */
    3813             :                                 decoding_error(path, f);
    3814             :                                 exit(EXIT_FAILURE);
    3815             :                                 /* LCOV_EXCL_STOP */
    3816             :                         }
    3817             : 
    3818        1249 :                         ret = sgetb32(f, &v_free_blocks);
    3819        1249 :                         if (ret < 0) {
    3820             :                                 /* LCOV_EXCL_START */
    3821             :                                 decoding_error(path, f);
    3822             :                                 exit(EXIT_FAILURE);
    3823             :                                 /* LCOV_EXCL_STOP */
    3824             :                         }
    3825             : 
    3826        1249 :                         ret = sgetbs(f, uuid, sizeof(uuid));
    3827        1249 :                         if (ret < 0) {
    3828             :                                 /* LCOV_EXCL_START */
    3829             :                                 decoding_error(path, f);
    3830             :                                 exit(EXIT_FAILURE);
    3831             :                                 /* LCOV_EXCL_STOP */
    3832             :                         }
    3833             : 
    3834             :                         /* auto configure if configuration is missing */
    3835        1249 :                         if (state->no_conf && v_level < LEV_MAX && v_level >= state->level)
    3836           5 :                                 state->level = v_level + 1;
    3837             : 
    3838             :                         /* if we use this parity entry */
    3839        1249 :                         if (v_level < state->level) {
    3840             :                                 /* set the parity info */
    3841         995 :                                 pathcpy(state->parity[v_level].uuid, sizeof(state->parity[v_level].uuid), uuid);
    3842         995 :                                 state->parity[v_level].total_blocks = v_total_blocks;
    3843         995 :                                 state->parity[v_level].free_blocks = v_free_blocks;
    3844             :                         }
    3845         219 :                 } else if (c == 'N') {
    3846             :                         uint32_t crc_stored;
    3847             :                         uint32_t crc_computed;
    3848             : 
    3849             :                         /* get the crc before reading it from the file */
    3850         218 :                         crc_computed = scrc(f);
    3851             : 
    3852         218 :                         ret = sgetble32(f, &crc_stored);
    3853         218 :                         if (ret < 0) {
    3854             :                                 /* LCOV_EXCL_START */
    3855             :                                 /* here don't call decoding_error() because it's too late to get the crc */
    3856             :                                 log_fatal("Error reading the CRC in '%s' at offset %" PRIi64 "\n", path, stell(f));
    3857             :                                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    3858             :                                 exit(EXIT_FAILURE);
    3859             :                                 /* LCOV_EXCL_STOP */
    3860             :                         }
    3861             : 
    3862         218 :                         if (crc_stored != crc_computed) {
    3863             :                                 /* LCOV_EXCL_START */
    3864             :                                 /* here don't call decoding_error() because it's too late to get the crc */
    3865             :                                 log_fatal("Mismatching CRC in '%s'\n", path);
    3866             :                                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    3867             :                                 exit(EXIT_FAILURE);
    3868             :                                 /* LCOV_EXCL_STOP */
    3869             :                         }
    3870             : 
    3871         218 :                         crc_checked = 1;
    3872             :                 } else {
    3873             :                         /* LCOV_EXCL_START */
    3874             :                         decoding_error(path, f);
    3875             :                         log_fatal("Invalid command '%c'!\n", (char)c);
    3876             :                         exit(EXIT_FAILURE);
    3877             :                         /* LCOV_EXCL_STOP */
    3878             :                 }
    3879     3522969 :         }
    3880             : 
    3881         218 :         tommy_array_done(&disk_mapping);
    3882             : 
    3883         218 :         if (serror(f)) {
    3884             :                 /* LCOV_EXCL_START */
    3885             :                 log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    3886             :                 exit(EXIT_FAILURE);
    3887             :                 /* LCOV_EXCL_STOP */
    3888             :         }
    3889             : 
    3890         218 :         if (!crc_checked) {
    3891             :                 /* LCOV_EXCL_START */
    3892             :                 log_fatal("Finished reading '%s' without finding the CRC\n", path);
    3893             :                 log_fatal("This content file is truncated or damaged! Use an alternate copy.\n");
    3894             :                 exit(EXIT_FAILURE);
    3895             :                 /* LCOV_EXCL_STOP */
    3896             :         }
    3897             : 
    3898             :         /* check that the stored parity size matches the loaded state */
    3899         218 :         if (blockmax != parity_allocated_size(state)) {
    3900             :                 /* LCOV_EXCL_START */
    3901             :                 log_fatal("Internal inconsistency in parity size %u/%u in '%s' at offset %" PRIi64 "\n", blockmax, parity_allocated_size(state), path, stell(f));
    3902             :                 if (state->opt.skip_content_check) {
    3903             :                         log_fatal("Overriding.\n");
    3904             :                         blockmax = parity_allocated_size(state);
    3905             :                 } else {
    3906             :                         exit(EXIT_FAILURE);
    3907             :                 }
    3908             :                 /* LCOV_EXCL_STOP */
    3909             :         }
    3910             : 
    3911         218 :         msg_verbose("%8u files\n", count_file);
    3912         218 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    3913         218 :         msg_verbose("%8u symlinks\n", count_symlink);
    3914         218 :         msg_verbose("%8u empty dirs\n", count_dir);
    3915         218 : }
    3916             : 
    3917         115 : static void state_write_binary(struct snapraid_state* state, STREAM* f)
    3918             : {
    3919             :         unsigned count_file;
    3920             :         unsigned count_hardlink;
    3921             :         unsigned count_symlink;
    3922             :         unsigned count_dir;
    3923             :         tommy_node* i;
    3924             :         block_off_t b;
    3925             :         block_off_t blockmax;
    3926             :         block_off_t begin;
    3927             :         time_t info_oldest;
    3928             :         int info_has_rehash;
    3929             :         int mapping_idx;
    3930             :         unsigned l;
    3931             :         uint32_t crc;
    3932             : 
    3933         115 :         count_file = 0;
    3934         115 :         count_hardlink = 0;
    3935         115 :         count_symlink = 0;
    3936         115 :         count_dir = 0;
    3937             : 
    3938             :         /* blocks of all array */
    3939         115 :         blockmax = parity_allocated_size(state);
    3940             : 
    3941             :         /* clear the info for unused blocks */
    3942             :         /* and get some other info */
    3943         115 :         info_oldest = 0; /* oldest time in info */
    3944         115 :         info_has_rehash = 0; /* if there is a rehash info */
    3945      696567 :         for (b = 0; b < blockmax; ++b) {
    3946             :                 /* if the position is used */
    3947      696452 :                 if (position_is_required(state, b)) {
    3948      696452 :                         snapraid_info info = info_get(&state->infoarr, b);
    3949             : 
    3950             :                         /* only if there is some info to store */
    3951      696452 :                         if (info) {
    3952      650122 :                                 time_t time = info_get_time(info);
    3953             : 
    3954      650122 :                                 if (!info_oldest || time < info_oldest)
    3955         160 :                                         info_oldest = time;
    3956             : 
    3957      650122 :                                 if (info_get_rehash(info))
    3958      108415 :                                         info_has_rehash = 1;
    3959             :                         }
    3960             :                 } else {
    3961             :                         /* clear any previous info */
    3962           0 :                         info_set(&state->infoarr, b, 0);
    3963             : 
    3964             :                         /* and clear any deleted blocks */
    3965           0 :                         position_clear_deleted(state, b);
    3966             :                 }
    3967             :         }
    3968             : 
    3969             :         /* write header */
    3970         115 :         swrite("SNAPCNT2\n\3\0\0", 12, f);
    3971             : 
    3972             :         /* write block size and block max */
    3973         115 :         sputc('z', f);
    3974         115 :         sputb32(state->block_size, f);
    3975         115 :         sputc('x', f);
    3976         115 :         sputb32(blockmax, f);
    3977         115 :         if (serror(f)) {
    3978             :                 /* LCOV_EXCL_START */
    3979             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    3980             :                 exit(EXIT_FAILURE);
    3981             :                 /* LCOV_EXCL_STOP */
    3982             :         }
    3983             : 
    3984         115 :         sputc('c', f);
    3985         115 :         if (state->hash == HASH_MURMUR3) {
    3986          86 :                 sputc('u', f);
    3987          29 :         } else if (state->hash == HASH_SPOOKY2) {
    3988          29 :                 sputc('k', f);
    3989             :         } else {
    3990             :                 /* LCOV_EXCL_START */
    3991             :                 log_fatal("Unexpected hash when writing the content file '%s'.\n", serrorfile(f));
    3992             :                 exit(EXIT_FAILURE);
    3993             :                 /* LCOV_EXCL_STOP */
    3994             :         }
    3995         115 :         swrite(state->hashseed, HASH_SIZE, f);
    3996         115 :         if (serror(f)) {
    3997             :                 /* LCOV_EXCL_START */
    3998             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    3999             :                 exit(EXIT_FAILURE);
    4000             :                 /* LCOV_EXCL_STOP */
    4001             :         }
    4002             : 
    4003             :         /* previous hash only present */
    4004         115 :         if (state->prevhash != HASH_UNDEFINED) {
    4005             :                 /* if at least one rehash tag found, we have to save the previous hash */
    4006          29 :                 if (info_has_rehash) {
    4007          29 :                         sputc('C', f);
    4008          29 :                         if (state->prevhash == HASH_MURMUR3) {
    4009          29 :                                 sputc('u', f);
    4010           0 :                         } else if (state->prevhash == HASH_SPOOKY2) {
    4011           0 :                                 sputc('k', f);
    4012             :                         } else {
    4013             :                                 /* LCOV_EXCL_START */
    4014             :                                 log_fatal("Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f));
    4015             :                                 exit(EXIT_FAILURE);
    4016             :                                 /* LCOV_EXCL_STOP */
    4017             :                         }
    4018          29 :                         swrite(state->prevhashseed, HASH_SIZE, f);
    4019          29 :                         if (serror(f)) {
    4020             :                                 /* LCOV_EXCL_START */
    4021             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4022             :                                 exit(EXIT_FAILURE);
    4023             :                                 /* LCOV_EXCL_STOP */
    4024             :                         }
    4025             :                 }
    4026             :         }
    4027             : 
    4028             :         /* for each map */
    4029         115 :         mapping_idx = 0;
    4030         805 :         for (i = state->maplist; i != 0; i = i->next) {
    4031         690 :                 struct snapraid_map* map = i->data;
    4032             :                 struct snapraid_disk* disk;
    4033             : 
    4034             :                 /* find the disk for this mapping */
    4035         690 :                 disk = find_disk(state, map->name);
    4036         690 :                 if (!disk) {
    4037             :                         /* LCOV_EXCL_START */
    4038             :                         log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
    4039             :                         exit(EXIT_FAILURE);
    4040             :                         /* LCOV_EXCL_STOP */
    4041             :                 }
    4042             : 
    4043             :                 /* save the mapping only for not empty disks */
    4044         690 :                 if (!disk_is_empty(disk, blockmax)) {
    4045         683 :                         sputc('M', f);
    4046         683 :                         sputbs(map->name, f);
    4047         683 :                         sputb32(map->position, f);
    4048         683 :                         sputb32(map->total_blocks, f);
    4049         683 :                         sputb32(map->free_blocks, f);
    4050         683 :                         sputbs(map->uuid, f);
    4051         683 :                         if (serror(f)) {
    4052             :                                 /* LCOV_EXCL_START */
    4053             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4054             :                                 exit(EXIT_FAILURE);
    4055             :                                 /* LCOV_EXCL_STOP */
    4056             :                         }
    4057             : 
    4058             :                         /* assign the mapping index used to identify disks */
    4059         683 :                         disk->mapping_idx = mapping_idx;
    4060         683 :                         ++mapping_idx;
    4061             :                 } else {
    4062             :                         /* mark the disk as without mapping */
    4063           7 :                         disk->mapping_idx = -1;
    4064             :                 }
    4065             :         }
    4066             : 
    4067             :         /* for each parity */
    4068         790 :         for (l = 0; l < state->level; ++l) {
    4069         675 :                 sputc('P', f);
    4070         675 :                 sputb32(l, f);
    4071         675 :                 sputb32(state->parity[l].total_blocks, f);
    4072         675 :                 sputb32(state->parity[l].free_blocks, f);
    4073         675 :                 sputbs(state->parity[l].uuid, f);
    4074         675 :                 if (serror(f)) {
    4075             :                         /* LCOV_EXCL_START */
    4076             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4077             :                         exit(EXIT_FAILURE);
    4078             :                         /* LCOV_EXCL_STOP */
    4079             :                 }
    4080             :         }
    4081             : 
    4082             :         /* for each disk */
    4083         805 :         for (i = state->disklist; i != 0; i = i->next) {
    4084             :                 tommy_node* j;
    4085         690 :                 struct snapraid_disk* disk = i->data;
    4086             : 
    4087             :                 /* if the disk is not mapped, skip it */
    4088         690 :                 if (disk->mapping_idx < 0)
    4089           7 :                         continue;
    4090             : 
    4091             :                 /* for each file */
    4092     1629343 :                 for (j = disk->filelist; j != 0; j = j->next) {
    4093     1628660 :                         struct snapraid_file* file = j->data;
    4094     1628660 :                         struct snapraid_block* blockvec = file->blockvec;
    4095             :                         uint64_t size;
    4096             :                         uint64_t mtime_sec;
    4097             :                         int32_t mtime_nsec;
    4098             :                         uint64_t inode;
    4099             : 
    4100     1628660 :                         size = file->size;
    4101     1628660 :                         mtime_sec = file->mtime_sec;
    4102     1628660 :                         mtime_nsec = file->mtime_nsec;
    4103     1628660 :                         inode = file->inode;
    4104             : 
    4105     1628660 :                         sputc('f', f);
    4106     1628660 :                         sputb32(disk->mapping_idx, f);
    4107     1628660 :                         sputb64(size, f);
    4108     1628660 :                         sputb64(mtime_sec, f);
    4109             :                         /* encode STAT_NSEC_INVALID as 0 */
    4110     1628660 :                         if (mtime_nsec == STAT_NSEC_INVALID)
    4111           0 :                                 sputb32(0, f);
    4112             :                         else
    4113     1628660 :                                 sputb32(mtime_nsec + 1, f);
    4114     1628660 :                         sputb64(inode, f);
    4115     1628660 :                         sputbs(file->sub, f);
    4116     1628660 :                         if (serror(f)) {
    4117             :                                 /* LCOV_EXCL_START */
    4118             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4119             :                                 exit(EXIT_FAILURE);
    4120             :                                 /* LCOV_EXCL_STOP */
    4121             :                         }
    4122             : 
    4123             :                         /* for all the blocks of the file */
    4124     1628660 :                         begin = 0;
    4125     4895594 :                         while (begin < file->blockmax) {
    4126     1638274 :                                 unsigned block_state = block_state_get(blockvec + begin);
    4127     1638274 :                                 uint32_t v_pos = blockvec[begin].parity_pos;
    4128             :                                 uint32_t v_count;
    4129             : 
    4130             :                                 block_off_t end;
    4131             : 
    4132             :                                 /* find the end of run of blocks */
    4133     1638274 :                                 end = begin + 1;
    4134     5645649 :                                 while (end < file->blockmax
    4135     2380866 :                                         && block_state == block_state_get(blockvec + end)
    4136     2380856 :                                         && blockvec[end - 1].parity_pos + 1 == blockvec[end].parity_pos
    4137             :                                 ) {
    4138     2369101 :                                         ++end;
    4139             :                                 }
    4140             : 
    4141     1638274 :                                 switch (block_state) {
    4142             :                                 case BLOCK_STATE_BLK :
    4143     1537959 :                                         sputc('b', f);
    4144     1537959 :                                         break;
    4145             :                                 case BLOCK_STATE_CHG :
    4146       77650 :                                         sputc('g', f);
    4147       77650 :                                         break;
    4148             :                                 case BLOCK_STATE_REP :
    4149       22665 :                                         sputc('p', f);
    4150       22665 :                                         break;
    4151             :                                 default :
    4152             :                                         /* LCOV_EXCL_START */
    4153             :                                         log_fatal("Internal inconsistency in state for block %u state %u\n", v_pos, block_state);
    4154             :                                         exit(EXIT_FAILURE);
    4155             :                                         /* LCOV_EXCL_STOP */
    4156             :                                 }
    4157             : 
    4158     1638274 :                                 sputb32(v_pos, f);
    4159             : 
    4160     1638274 :                                 v_count = end - begin;
    4161     1638274 :                                 sputb32(v_count, f);
    4162             : 
    4163             :                                 /* write hashes */
    4164     5645649 :                                 for (b = begin; b < end; ++b) {
    4165     4007375 :                                         struct snapraid_block* block = blockvec + b;
    4166             : 
    4167             :                                         /* consistency check */
    4168     4007375 :                                         if (block != disk_block_get(disk, block->parity_pos)) {
    4169             :                                                 /* LCOV_EXCL_START */
    4170             :                                                 log_fatal("Internal inconsistency in mismatch for block %u state %u in file '%s'\n", block->parity_pos, block_state, file->sub);
    4171             :                                                 exit(EXIT_FAILURE);
    4172             :                                                 /* LCOV_EXCL_STOP */
    4173             :                                         }
    4174             : 
    4175     4007375 :                                         swrite(block->hash, HASH_SIZE, f);
    4176             :                                 }
    4177             : 
    4178     1638274 :                                 if (serror(f)) {
    4179             :                                         /* LCOV_EXCL_START */
    4180             :                                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4181             :                                         exit(EXIT_FAILURE);
    4182             :                                         /* LCOV_EXCL_STOP */
    4183             :                                 }
    4184             : 
    4185             :                                 /* next begin position */
    4186     1638274 :                                 begin = end;
    4187             :                         }
    4188             : 
    4189     1628660 :                         ++count_file;
    4190             :                 }
    4191             : 
    4192             :                 /* for each link */
    4193       54019 :                 for (j = disk->linklist; j != 0; j = j->next) {
    4194       53336 :                         struct snapraid_link* link = j->data;
    4195             : 
    4196       53336 :                         switch (link_flag_get(link, FILE_IS_LINK_MASK)) {
    4197             :                         case FILE_IS_HARDLINK :
    4198         572 :                                 sputc('a', f);
    4199         572 :                                 ++count_hardlink;
    4200         572 :                                 break;
    4201             :                         case FILE_IS_SYMLINK :
    4202       52764 :                                 sputc('s', f);
    4203       52764 :                                 ++count_symlink;
    4204       52764 :                                 break;
    4205             :                         }
    4206             : 
    4207       53336 :                         sputb32(disk->mapping_idx, f);
    4208       53336 :                         sputbs(link->sub, f);
    4209       53336 :                         sputbs(link->linkto, f);
    4210       53336 :                         if (serror(f)) {
    4211             :                                 /* LCOV_EXCL_START */
    4212             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4213             :                                 exit(EXIT_FAILURE);
    4214             :                                 /* LCOV_EXCL_STOP */
    4215             :                         }
    4216             :                 }
    4217             : 
    4218             :                 /* for each dir */
    4219        1066 :                 for (j = disk->dirlist; j != 0; j = j->next) {
    4220         383 :                         struct snapraid_dir* dir = j->data;
    4221             : 
    4222         383 :                         sputc('r', f);
    4223         383 :                         sputb32(disk->mapping_idx, f);
    4224         383 :                         sputbs(dir->sub, f);
    4225         383 :                         if (serror(f)) {
    4226             :                                 /* LCOV_EXCL_START */
    4227             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4228             :                                 exit(EXIT_FAILURE);
    4229             :                                 /* LCOV_EXCL_STOP */
    4230             :                         }
    4231             : 
    4232         383 :                         ++count_dir;
    4233             :                 }
    4234             : 
    4235             :                 /* deleted blocks of the disk */
    4236         683 :                 sputc('h', f);
    4237         683 :                 sputb32(disk->mapping_idx, f);
    4238         683 :                 if (serror(f)) {
    4239             :                         /* LCOV_EXCL_START */
    4240             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4241             :                         exit(EXIT_FAILURE);
    4242             :                         /* LCOV_EXCL_STOP */
    4243             :                 }
    4244         683 :                 begin = 0;
    4245        2118 :                 while (begin < blockmax) {
    4246             :                         int is_deleted;
    4247             :                         block_off_t end;
    4248             : 
    4249         752 :                         is_deleted = is_block_deleted(disk, begin);
    4250             : 
    4251             :                         /* find the end of run of blocks */
    4252         752 :                         end = begin + 1;
    4253     4174793 :                         while (end < blockmax
    4254     4173358 :                                 && is_deleted == is_block_deleted(disk, end)
    4255             :                         ) {
    4256     4173289 :                                 ++end;
    4257             :                         }
    4258             : 
    4259         752 :                         sputb32(end - begin, f);
    4260             : 
    4261         752 :                         if (is_deleted) {
    4262             :                                 /* write the run of deleted blocks with hash */
    4263          40 :                                 sputc('o', f);
    4264             : 
    4265             :                                 /* write all the hash */
    4266       16035 :                                 while (begin < end) {
    4267       15955 :                                         struct snapraid_block* block = disk_block_get(disk, begin);
    4268             : 
    4269             :                                         /* consistency check */
    4270       15955 :                                         if (block->parity_pos != begin) {
    4271             :                                                 /* LCOV_EXCL_START */
    4272             :                                                 log_fatal("Internal inconsistency in delete position %u/%u!\n", block->parity_pos, begin);
    4273             :                                                 exit(EXIT_FAILURE);
    4274             :                                                 /* LCOV_EXCL_STOP */
    4275             :                                         }
    4276             : 
    4277       15955 :                                         swrite(block->hash, HASH_SIZE, f);
    4278             : 
    4279       15955 :                                         ++begin;
    4280             :                                 }
    4281             :                         } else {
    4282             :                                 /* write the run of blocks without hash */
    4283             :                                 /* they can be either used or empty blocks */
    4284         712 :                                 sputc('O', f);
    4285             : 
    4286             :                                 /* next begin position */
    4287         712 :                                 begin = end;
    4288             :                         }
    4289             : 
    4290         752 :                         if (serror(f)) {
    4291             :                                 /* LCOV_EXCL_START */
    4292             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4293             :                                 exit(EXIT_FAILURE);
    4294             :                                 /* LCOV_EXCL_STOP */
    4295             :                         }
    4296             :                 }
    4297             :         }
    4298             : 
    4299             :         /* write the info for each block */
    4300         115 :         sputc('i', f);
    4301         115 :         sputb32(info_oldest, f);
    4302         115 :         begin = 0;
    4303      216979 :         while (begin < blockmax) {
    4304             :                 snapraid_info info;
    4305             :                 block_off_t end;
    4306             :                 time_t t;
    4307             :                 unsigned flag;
    4308             : 
    4309      216749 :                 info = info_get(&state->infoarr, begin);
    4310             : 
    4311             :                 /* find the end of run of blocks */
    4312      216749 :                 end = begin + 1;
    4313      913201 :                 while (end < blockmax
    4314      696338 :                         && info == info_get(&state->infoarr, end)
    4315             :                 ) {
    4316      479703 :                         ++end;
    4317             :                 }
    4318             : 
    4319      216749 :                 sputb32(end - begin, f);
    4320             : 
    4321             :                 /* if there is info */
    4322      216749 :                 if (info) {
    4323             :                         /* other flags */
    4324      216733 :                         flag = 1; /* info is present */
    4325      216733 :                         if (info_get_bad(info))
    4326        1467 :                                 flag |= 2;
    4327      216733 :                         if (info_get_rehash(info))
    4328       99829 :                                 flag |= 4;
    4329      216733 :                         sputb32(flag, f);
    4330             : 
    4331      216733 :                         t = info_get_time(info) - info_oldest;
    4332      216733 :                         sputb32(t, f);
    4333             :                 } else {
    4334             :                         /* write a special 0 flag to mark missing info */
    4335          16 :                         sputb32(0, f);
    4336             :                 }
    4337             : 
    4338      216749 :                 if (serror(f)) {
    4339             :                         /* LCOV_EXCL_START */
    4340             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4341             :                         exit(EXIT_FAILURE);
    4342             :                         /* LCOV_EXCL_STOP */
    4343             :                 }
    4344             : 
    4345             :                 /* next begin position */
    4346      216749 :                 begin = end;
    4347             :         }
    4348             : 
    4349         115 :         sputc('N', f);
    4350             : 
    4351             :         /* flush data written to the disk */
    4352         115 :         if (sflush(f)) {
    4353             :                 /* LCOV_EXCL_START */
    4354             :                 log_fatal("Error writing the content file '%s' (in flush before crc). %s.\n", serrorfile(f), strerror(errno));
    4355             :                 exit(EXIT_FAILURE);
    4356             :                 /* LCOV_EXCL_STOP */
    4357             :         }
    4358             : 
    4359             :         /* get the file crc */
    4360         115 :         crc = scrc(f);
    4361             : 
    4362             :         /* compare the crc of the data written to file */
    4363             :         /* with the one of the data written to the stream */
    4364         115 :         if (crc != scrc_stream(f)) {
    4365             :                 /* LCOV_EXCL_START */
    4366             :                 log_fatal("CRC mismatch writing the content stream.\n");
    4367             :                 log_fatal("DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
    4368             :                 log_fatal("Try running a memory test like http://www.memtest86.com/\n");
    4369             :                 exit(EXIT_FAILURE);
    4370             :                 /* LCOV_EXCL_STOP */
    4371             :         }
    4372             : 
    4373         115 :         sputble32(crc, f);
    4374         115 :         if (serror(f)) {
    4375             :                 /* LCOV_EXCL_START */
    4376             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    4377             :                 exit(EXIT_FAILURE);
    4378             :                 /* LCOV_EXCL_STOP */
    4379             :         }
    4380             : 
    4381         115 :         msg_verbose("%8u files\n", count_file);
    4382         115 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    4383         115 :         msg_verbose("%8u symlinks\n", count_symlink);
    4384         115 :         msg_verbose("%8u empty dirs\n", count_dir);
    4385         115 : }
    4386             : 
    4387         231 : void state_read(struct snapraid_state* state)
    4388             : {
    4389             :         STREAM* f;
    4390             :         char path[PATH_MAX];
    4391             :         struct stat st;
    4392             :         tommy_node* node;
    4393             :         int ret;
    4394             :         int c;
    4395             : 
    4396             :         /* iterate over all the available content files and load the first one present */
    4397         231 :         f = 0;
    4398         231 :         node = tommy_list_head(&state->contentlist);
    4399         512 :         while (node) {
    4400         274 :                 struct snapraid_content* content = node->data;
    4401         274 :                 pathcpy(path, sizeof(path), content->content);
    4402             : 
    4403         274 :                 if (!state->no_conf) {
    4404         273 :                         log_tag("content:%s\n", path);
    4405         273 :                         log_flush();
    4406             :                 }
    4407         274 :                 msg_progress("Loading state from %s...\n", path);
    4408             : 
    4409         274 :                 f = sopen_read(path);
    4410         274 :                 if (f != 0) {
    4411             :                         /* if openend stop the search */
    4412         224 :                         break;
    4413             :                 } else {
    4414             :                         /* if it's real error of an existing file, abort */
    4415          50 :                         if (errno != ENOENT) {
    4416             :                                 /* LCOV_EXCL_START */
    4417             :                                 log_fatal("Error opening the content file '%s'. %s.\n", path, strerror(errno));
    4418             :                                 exit(EXIT_FAILURE);
    4419             :                                 /* LCOV_EXCL_STOP */
    4420             :                         }
    4421             : 
    4422             :                         /* otherwise continue */
    4423          50 :                         if (node->next) {
    4424          43 :                                 log_fatal("WARNING! Content file '%s' not found, trying with another copy...\n", path);
    4425             : 
    4426             :                                 /* ensure to rewrite all the content files */
    4427          43 :                                 state->need_write = 1;
    4428             :                         }
    4429             :                 }
    4430             : 
    4431             :                 /* next content file */
    4432          50 :                 node = node->next;
    4433             :         }
    4434             : 
    4435             :         /* if not found, assume empty */
    4436         231 :         if (!f) {
    4437           7 :                 log_fatal("No content file found. Assuming empty.\n");
    4438             : 
    4439             :                 /* create the initial mapping */
    4440           7 :                 state_map(state);
    4441         237 :                 return;
    4442             :         }
    4443             : 
    4444             :         /* get the stat of the content file */
    4445         224 :         ret = fstat(shandle(f), &st);
    4446         224 :         if (ret != 0) {
    4447             :                 /* LCOV_EXCL_START */
    4448             :                 log_fatal("Error stating the content file '%s'. %s.\n", path, strerror(errno));
    4449             :                 exit(EXIT_FAILURE);
    4450             :                 /* LCOV_EXCL_STOP */
    4451             :         }
    4452             : 
    4453             :         /* go further to check other content files */
    4454        1725 :         while (node) {
    4455             :                 char other_path[PATH_MAX];
    4456             :                 struct stat other_st;
    4457        1277 :                 struct snapraid_content* content = node->data;
    4458        1277 :                 pathcpy(other_path, sizeof(other_path), content->content);
    4459             : 
    4460        1277 :                 ret = stat(other_path, &other_st);
    4461        1277 :                 if (ret != 0) {
    4462             :                         /* allow missing content files, but not any other kind of error */
    4463           0 :                         if (errno != ENOENT) {
    4464             :                                 /* LCOV_EXCL_START */
    4465             :                                 log_fatal("Error stating the content file '%s'. %s.\n", other_path, strerror(errno));
    4466             :                                 exit(EXIT_FAILURE);
    4467             :                                 /* LCOV_EXCL_STOP */
    4468             :                         }
    4469             : 
    4470             :                         /* ensure to rewrite all the content files */
    4471           0 :                         state->need_write = 1;
    4472             :                 } else {
    4473             :                         /* if the size is different */
    4474        1277 :                         if (other_st.st_size != st.st_size) {
    4475          35 :                                 log_fatal("WARNING! Content files '%s' and '%s' have a different size!\n", path, other_path);
    4476          35 :                                 log_fatal("Likely one of the two is broken!\n");
    4477             : 
    4478             :                                 /* ensure to rewrite all the content files */
    4479          35 :                                 state->need_write = 1;
    4480             :                         }
    4481             :                 }
    4482             : 
    4483             :                 /* next content file */
    4484        1277 :                 node = node->next;
    4485             :         }
    4486             : 
    4487             :         /* start with a undefined default. */
    4488             :         /* it's for compatibility with version 1.0 where MD5 was implicit. */
    4489         224 :         state->hash = HASH_UNDEFINED;
    4490             : 
    4491             :         /* start with a zero seed, it was the default in old versions */
    4492         224 :         memset(state->hashseed, 0, HASH_SIZE);
    4493             : 
    4494             :         /* previous hash, start with an undefined value */
    4495         224 :         state->prevhash = HASH_UNDEFINED;
    4496             : 
    4497             :         /* intentionally not set the prevhashseed, if used valgrind will warn about it */
    4498             : 
    4499             :         /* get the first char to detect the file type */
    4500         224 :         c = sgetc(f);
    4501         224 :         sungetc(c, f);
    4502             : 
    4503             :         /* guess the file type from the first char */
    4504         224 :         if (c == 'S') {
    4505         219 :                 state_read_binary(state, path, f);
    4506             :         } else {
    4507           5 :                 state_read_text(state, path, f, st.st_mtime);
    4508             : 
    4509             :                 /* force a rewrite to convert to binary */
    4510           5 :                 state->need_write = 1;
    4511             :         }
    4512             : 
    4513         223 :         sclose(f);
    4514             : 
    4515         223 :         if (state->hash == HASH_UNDEFINED) {
    4516             :                 /* LCOV_EXCL_START */
    4517             :                 log_fatal("The checksum to use is not specified.\n");
    4518             :                 log_fatal("This happens because you are likely upgrading from SnapRAID 1.0.\n");
    4519             :                 log_fatal("To use a new SnapRAID you must restart from scratch,\n");
    4520             :                 log_fatal("deleting all the content and parity files.\n");
    4521             :                 exit(EXIT_FAILURE);
    4522             :                 /* LCOV_EXCL_STOP */
    4523             :         }
    4524             : 
    4525             :         /* update the mapping */
    4526         223 :         state_map(state);
    4527             : 
    4528         223 :         state_content_check(state, path);
    4529             : 
    4530             :         /* mark that we read the content file, and it passed all the checks */
    4531         223 :         state->checked_read = 1;
    4532             : }
    4533             : 
    4534         120 : void state_write(struct snapraid_state* state)
    4535             : {
    4536             :         STREAM* f;
    4537             :         unsigned count_content;
    4538             :         tommy_node* i;
    4539             :         unsigned k;
    4540             : 
    4541             :         /* count the content files */
    4542         120 :         count_content = 0;
    4543         120 :         i = tommy_list_head(&state->contentlist);
    4544        1065 :         while (i) {
    4545         825 :                 struct snapraid_content* content = i->data;
    4546         825 :                 msg_progress("Saving state to %s...\n", content->content);
    4547         825 :                 ++count_content;
    4548         825 :                 i = i->next;
    4549             :         }
    4550             : 
    4551             :         /* open all the content files */
    4552         120 :         f = sopen_multi_write(count_content);
    4553         120 :         if (!f) {
    4554             :                 /* LCOV_EXCL_START */
    4555             :                 log_fatal("Error opening the content files.\n");
    4556             :                 exit(EXIT_FAILURE);
    4557             :                 /* LCOV_EXCL_STOP */
    4558             :         }
    4559             : 
    4560         120 :         k = 0;
    4561         120 :         i = tommy_list_head(&state->contentlist);
    4562        1065 :         while (i) {
    4563         825 :                 struct snapraid_content* content = i->data;
    4564             :                 char tmp[PATH_MAX];
    4565         825 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    4566         825 :                 if (sopen_multi_file(f, k, tmp) != 0) {
    4567             :                         /* LCOV_EXCL_START */
    4568             :                         log_fatal("Error opening the content file '%s'. %s.\n", tmp, strerror(errno));
    4569             :                         exit(EXIT_FAILURE);
    4570             :                         /* LCOV_EXCL_STOP */
    4571             :                 }
    4572         825 :                 ++k;
    4573         825 :                 i = i->next;
    4574             :         }
    4575             : 
    4576         120 :         if (state->opt.force_content_text)
    4577           5 :                 state_write_text(state, f);
    4578             :         else
    4579         115 :                 state_write_binary(state, f);
    4580             : 
    4581             :         /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
    4582             :         /* than even in a system crash event we have one valid copy of the file. */
    4583             : 
    4584         120 :         if (sflush(f) != 0) {
    4585             :                 /* LCOV_EXCL_START */
    4586             :                 log_fatal("Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
    4587             :                 exit(EXIT_FAILURE);
    4588             :                 /* LCOV_EXCL_STOP */
    4589             :         }
    4590             : 
    4591             : #if HAVE_FSYNC
    4592         120 :         if (ssync(f) != 0) {
    4593             :                 /* LCOV_EXCL_START */
    4594             :                 log_fatal("Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
    4595             :                 exit(EXIT_FAILURE);
    4596             :                 /* LCOV_EXCL_STOP */
    4597             :         }
    4598             : #endif
    4599             : 
    4600         120 :         if (sclose(f) != 0) {
    4601             :                 /* LCOV_EXCL_START */
    4602             :                 log_fatal("Error closing the content files in close(). %s.\n", strerror(errno));
    4603             :                 exit(EXIT_FAILURE);
    4604             :                 /* LCOV_EXCL_STOP */
    4605             :         }
    4606             : 
    4607         120 :         i = tommy_list_head(&state->contentlist);
    4608        1065 :         while (i) {
    4609         825 :                 struct snapraid_content* content = i->data;
    4610             :                 char tmp[PATH_MAX];
    4611             : 
    4612             :                 /* now renames the just written copy with the correct name */
    4613         825 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    4614         825 :                 if (rename(tmp, content->content) != 0) {
    4615             :                         /* LCOV_EXCL_START */
    4616             :                         log_fatal("Error renaming the content file '%s' to '%s' in rename(). %s.\n", tmp, content->content, strerror(errno));
    4617             :                         exit(EXIT_FAILURE);
    4618             :                         /* LCOV_EXCL_STOP */
    4619             :                 }
    4620             : 
    4621         825 :                 i = i->next;
    4622             :         }
    4623             : 
    4624         120 :         state->need_write = 0; /* no write needed anymore */
    4625         120 :         state->checked_read = 0; /* what we wrote is not checked in read */
    4626         120 : }
    4627             : 
    4628         122 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error)
    4629             : {
    4630             :         tommy_node* i;
    4631             : 
    4632             :         /* if no filter, include all */
    4633         122 :         if (!filter_missing && !filter_error && tommy_list_empty(filterlist_file) && tommy_list_empty(filterlist_disk))
    4634         236 :                 return;
    4635             : 
    4636           8 :         msg_progress("Filtering...\n");
    4637             : 
    4638          11 :         for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
    4639           3 :                 struct snapraid_filter* filter = i->data;
    4640           3 :                 msg_verbose("\t%s%s\n", filter->pattern, filter->is_disk ? "//" : "");
    4641             :         }
    4642          11 :         for (i = tommy_list_head(filterlist_file); i != 0; i = i->next) {
    4643           3 :                 struct snapraid_filter* filter = i->data;
    4644           3 :                 msg_verbose("\t%s%s\n", filter->pattern, filter->is_dir ? "/" : "");
    4645             :         }
    4646           8 :         if (filter_missing)
    4647           2 :                 msg_verbose("\t<missing>\n");
    4648           8 :         if (filter_error)
    4649           2 :                 msg_verbose("\t<error>\n");
    4650             : 
    4651             :         /* for each disk */
    4652          56 :         for (i = state->disklist; i != 0; i = i->next) {
    4653             :                 tommy_node* j;
    4654          48 :                 struct snapraid_disk* disk = i->data;
    4655             : 
    4656             :                 /* if we filter for presence, we have to access the disk, so better to print something */
    4657          48 :                 if (filter_missing)
    4658          12 :                         msg_progress("Scanning disk %s...\n", disk->name);
    4659             : 
    4660             :                 /* for each file */
    4661      122582 :                 for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
    4662      122534 :                         struct snapraid_file* file = j->data;
    4663             : 
    4664      122534 :                         if (filter_path(filterlist_disk, 0, disk->name, file->sub) != 0
    4665       80923 :                                 || filter_path(filterlist_file, 0, disk->name, file->sub) != 0
    4666       78090 :                                 || filter_existence(filter_missing, disk->dir, file->sub) != 0
    4667       38818 :                                 || filter_correctness(filter_error, &state->infoarr, file) != 0
    4668             :                         ) {
    4669       97808 :                                 file_flag_set(file, FILE_IS_EXCLUDED);
    4670             :                         }
    4671             :                 }
    4672             : 
    4673             :                 /* for each link */
    4674        4030 :                 for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
    4675        3982 :                         struct snapraid_link* link = j->data;
    4676             : 
    4677        3982 :                         if (filter_path(filterlist_disk, 0, disk->name, link->sub) != 0
    4678        2643 :                                 || filter_path(filterlist_file, 0, disk->name, link->sub) != 0
    4679        2538 :                                 || filter_existence(filter_missing, disk->dir, link->sub) != 0
    4680             :                         ) {
    4681        2841 :                                 link_flag_set(link, FILE_IS_EXCLUDED);
    4682             :                         }
    4683             :                 }
    4684             : 
    4685             :                 /* for each dir */
    4686          72 :                 for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
    4687          24 :                         struct snapraid_dir* dir = j->data;
    4688             : 
    4689          24 :                         if (filter_dir(filterlist_disk, 0, disk->name, dir->sub) != 0
    4690          24 :                                 || filter_dir(filterlist_file, 0, disk->name, dir->sub) != 0
    4691          24 :                                 || filter_existence(filter_missing, disk->dir, dir->sub) != 0
    4692             :                         ) {
    4693           8 :                                 dir_flag_set(dir, FILE_IS_EXCLUDED);
    4694             :                         }
    4695             :                 }
    4696             :         }
    4697             : }
    4698             : 
    4699         197 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax)
    4700             : {
    4701             :         time_t now;
    4702             : 
    4703         197 :         if (state->opt.gui) {
    4704           5 :                 log_tag("run:begin:%u:%u:%u\n", blockstart, blockmax, countmax);
    4705           5 :                 log_flush();
    4706             :         }
    4707             : 
    4708         197 :         now = time(0);
    4709             : 
    4710         197 :         state->progress_whole_start = now;
    4711             : 
    4712         197 :         state->progress_tick = 0;
    4713         197 :         state->progress_ptr = 0;
    4714         197 :         state->progress_wasted = 0;
    4715             : 
    4716             :         /* stop if requested */
    4717         197 :         if (global_interrupt) {
    4718             :                 /* LCOV_EXCL_START */
    4719             :                 if (!state->opt.gui) {
    4720             :                         msg_status("Not starting for interruption\n");
    4721             :                 }
    4722             :                 log_tag("sigint:0: SIGINT received\n");
    4723             :                 log_flush();
    4724             :                 return 0;
    4725             :                 /* LCOV_EXCL_STOP */
    4726             :         }
    4727             : 
    4728         197 :         return 1;
    4729             : }
    4730             : 
    4731         197 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize)
    4732             : {
    4733         197 :         if (state->opt.gui) {
    4734           5 :                 log_tag("run:end\n");
    4735           5 :                 log_flush();
    4736         192 :         } else if (countmax == 0) {
    4737          15 :                 msg_status("Nothing to do\n");
    4738             :         } else {
    4739             :                 time_t now;
    4740             :                 time_t elapsed;
    4741             : 
    4742         177 :                 unsigned countsize_MB = (countsize + MEGA - 1) / MEGA;
    4743             : 
    4744         177 :                 now = time(0);
    4745             : 
    4746         177 :                 elapsed = now - state->progress_whole_start - state->progress_wasted;
    4747             : 
    4748         177 :                 msg_bar("%u%% completed, %u MB processed", countpos * 100 / countmax, countsize_MB);
    4749         177 :                 if (elapsed >= 60)
    4750           0 :                         msg_bar(" in %u:%02u", (unsigned)(elapsed / 3600), (unsigned)((elapsed % 3600) / 60));
    4751         177 :                 msg_bar("\n");
    4752         177 :                 msg_flush();
    4753             :         }
    4754         197 : }
    4755             : 
    4756           1 : void state_progress_stop(struct snapraid_state* state)
    4757             : {
    4758             :         time_t now;
    4759             : 
    4760           1 :         now = time(0);
    4761             : 
    4762           1 :         if (!state->opt.gui) {
    4763           1 :                 msg_bar("\n");
    4764           1 :                 msg_flush();
    4765             :         }
    4766             : 
    4767           1 :         state->progress_interruption = now;
    4768           1 : }
    4769             : 
    4770           1 : void state_progress_restart(struct snapraid_state* state)
    4771             : {
    4772             :         time_t now;
    4773             : 
    4774           1 :         now = time(0);
    4775             : 
    4776             :         /* reset the progress counter */
    4777           1 :         state->progress_tick = 0;
    4778           1 :         state->progress_ptr = 0;
    4779             : 
    4780           1 :         if (now >= state->progress_interruption) /* avoid degenerated cases when the clock is manually adjusted */
    4781           1 :                 state->progress_wasted += now - state->progress_interruption;
    4782           1 : }
    4783             : 
    4784             : #define PROGRESS_CLEAR "          "
    4785             : 
    4786     1025177 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize)
    4787             : {
    4788             :         time_t now;
    4789             :         int pred;
    4790             : 
    4791     1025177 :         now = time(0);
    4792             : 
    4793             :         /* previous position */
    4794     1025177 :         pred = state->progress_ptr + PROGRESS_MAX - 1;
    4795     1025177 :         if (pred >= PROGRESS_MAX)
    4796     1024997 :                 pred -= PROGRESS_MAX;
    4797             : 
    4798             :         /* if the previous measure is different */
    4799     1025177 :         if (state->progress_tick == 0
    4800     1024997 :                 || state->progress_time[pred] != now
    4801             :         ) {
    4802             :                 uint64_t tick_total;
    4803             :                 uint64_t tick_cpu;
    4804             :                 time_t elapsed;
    4805         251 :                 unsigned out_perc = 0;
    4806         251 :                 unsigned out_speed = 0;
    4807         251 :                 unsigned out_cpu = 0;
    4808         251 :                 unsigned out_eta = 0;
    4809             : 
    4810         251 :                 tick_total = state->tick_cpu + state->tick_io;
    4811         251 :                 tick_cpu = state->tick_cpu;
    4812         251 :                 elapsed = now - state->progress_whole_start - state->progress_wasted;
    4813             : 
    4814             :                 /* completion percentage */
    4815         251 :                 if (countmax)
    4816         251 :                         out_perc = countpos * 100 / countmax;
    4817             : 
    4818             :                 /* if we have at least 5 measures */
    4819         251 :                 if (state->progress_tick >= 5
    4820             :                         /* or if we are running in test mode, with at least one measure */
    4821         251 :                         || (state->opt.force_progress && state->progress_tick >= 1)
    4822             :                 ) {
    4823             :                         int oldest;
    4824             :                         int past;
    4825             :                         time_t delta_time;
    4826             :                         block_off_t delta_pos;
    4827             :                         data_off_t delta_size;
    4828             :                         uint64_t delta_tick_total;
    4829             :                         uint64_t delta_tick_cpu;
    4830             : 
    4831             :                         /* number of past measures */
    4832          71 :                         past = state->progress_tick;
    4833             : 
    4834             :                         /* drop the oldest ones, to promptly */
    4835             :                         /* skip the startup phase */
    4836          71 :                         past -= past / 5;
    4837             : 
    4838             :                         /* check how much we can go in the past */
    4839          71 :                         if (past >= PROGRESS_MAX) {
    4840             :                                 /* the vector is filled, so we are already in position */
    4841             :                                 /* to get the possible oldest one */
    4842           0 :                                 oldest = state->progress_ptr;
    4843             :                         } else {
    4844             :                                 /* go backward the number of positions selected */
    4845          71 :                                 oldest = state->progress_ptr + PROGRESS_MAX - past;
    4846          71 :                                 if (oldest >= PROGRESS_MAX)
    4847          71 :                                         oldest -= PROGRESS_MAX;
    4848             :                         }
    4849             : 
    4850          71 :                         delta_time = now - state->progress_time[oldest];
    4851          71 :                         delta_pos = countpos - state->progress_pos[oldest];
    4852          71 :                         delta_size = countsize - state->progress_size[oldest];
    4853          71 :                         delta_tick_total = tick_total - state->progress_tick_total[oldest];
    4854          71 :                         delta_tick_cpu = tick_cpu - state->progress_tick_cpu[oldest];
    4855             : 
    4856             :                         /* estimate the speed in MiB/s */
    4857          71 :                         if (delta_time != 0)
    4858          71 :                                 out_speed = (unsigned)(delta_size / MEGA / delta_time);
    4859             : 
    4860             :                         /* estimate the cpu usage percentage */
    4861          71 :                         if (delta_tick_total != 0)
    4862           8 :                                 out_cpu = (unsigned)(delta_tick_cpu * 100U / delta_tick_total);
    4863             : 
    4864             :                         /* estimate the remaining time in minutes */
    4865          71 :                         if (delta_pos != 0)
    4866          71 :                                 out_eta = (countmax - countpos) * delta_time / (60 * delta_pos);
    4867             :                 }
    4868             : 
    4869         251 :                 if (state->opt.gui) {
    4870           6 :                         log_tag("run:pos:%u:%u:%" PRIu64 ":%u:%u:%u:%u:%" PRIu64 "\n", blockpos, countpos, countsize, out_perc, out_eta, out_speed, out_cpu, (uint64_t)elapsed);
    4871           6 :                         log_flush();
    4872             :                 } else {
    4873         245 :                         msg_bar("%u%%, %u MiB", out_perc, (unsigned)(countsize / MEGA));
    4874         245 :                         if (out_speed)
    4875          63 :                                 msg_bar(", %u MiB/s", out_speed);
    4876         245 :                         if (out_cpu)
    4877           8 :                                 msg_bar(", CPU %u%%", out_cpu);
    4878         245 :                         if (out_eta)
    4879           1 :                                 msg_bar(", %u:%02u ETA", out_eta / 60, out_eta % 60);
    4880         245 :                         msg_bar("%s\r", PROGRESS_CLEAR);
    4881         245 :                         msg_flush();
    4882             :                 }
    4883             : 
    4884             :                 /* store the new measure */
    4885         251 :                 state->progress_time[state->progress_ptr] = now;
    4886         251 :                 state->progress_pos[state->progress_ptr] = countpos;
    4887         251 :                 state->progress_size[state->progress_ptr] = countsize;
    4888         251 :                 state->progress_tick_cpu[state->progress_ptr] = tick_cpu;
    4889         251 :                 state->progress_tick_total[state->progress_ptr] = tick_total;
    4890             : 
    4891             :                 /* next position */
    4892         251 :                 ++state->progress_ptr;
    4893         251 :                 if (state->progress_ptr >= PROGRESS_MAX)
    4894           0 :                         state->progress_ptr -= PROGRESS_MAX;
    4895             : 
    4896             :                 /* one more measure */
    4897         251 :                 ++state->progress_tick;
    4898             :         }
    4899             : 
    4900             :         /* stop if requested */
    4901     1025177 :         if (global_interrupt) {
    4902             :                 /* LCOV_EXCL_START */
    4903             :                 if (!state->opt.gui) {
    4904             :                         log_fatal("\n");
    4905             :                         log_fatal("Stopping for interruption at block %u\n", blockpos);
    4906             :                 }
    4907             :                 log_tag("sigint:%u: SIGINT received\n", blockpos);
    4908             :                 log_flush();
    4909             :                 return 1;
    4910             :                 /* LCOV_EXCL_STOP */
    4911             :         }
    4912             : 
    4913     1025177 :         return 0;
    4914             : }
    4915             : 
    4916          73 : void state_usage_print(struct snapraid_state* state)
    4917             : {
    4918             :         uint64_t tick_total;
    4919             :         uint64_t tick_max;
    4920             :         tommy_node* i;
    4921             :         unsigned l;
    4922             : 
    4923          73 :         tick_max = 0;
    4924          73 :         tick_total = 0;
    4925         509 :         for (i = state->disklist; i != 0; i = i->next) {
    4926         436 :                 struct snapraid_disk* disk = i->data;
    4927         436 :                 tick_total += disk->tick;
    4928         436 :                 if (disk->tick > tick_max)
    4929          88 :                         tick_max = disk->tick;
    4930             :         }
    4931         501 :         for (l = 0; l < state->level; ++l) {
    4932         428 :                 tick_total += state->parity[l].tick;
    4933         428 :                 if (state->parity[l].tick > tick_max)
    4934           5 :                         tick_max = state->parity[l].tick;
    4935             :         }
    4936             : 
    4937          73 :         if (!tick_total)
    4938          88 :                 return;
    4939             : 
    4940          58 :         if (msg_level >= MSG_PROGRESS) {
    4941             :                 /* here we don't use msg_progress() because it doesn't allow partial output */
    4942           1 :                 printf("Time for disk:");
    4943           7 :                 for (i = state->disklist; i != 0; i = i->next) {
    4944           6 :                         struct snapraid_disk* disk = i->data;
    4945           6 :                         if (disk->tick == tick_max)
    4946           1 :                                 printf(" %s:", disk->name);
    4947             :                         else
    4948           5 :                                 printf(" ");
    4949           6 :                         printf("%" PRIu64 "%%", disk->tick * 100U / tick_total);
    4950             :                 }
    4951           1 :                 printf("\n");
    4952             : 
    4953           1 :                 printf("Time for parity:");
    4954           7 :                 for (l = 0; l < state->level; ++l) {
    4955           6 :                         if (state->parity[l].tick == tick_max)
    4956           0 :                                 printf(" %s:", lev_config_name(l));
    4957             :                         else
    4958           6 :                                 printf(" ");
    4959           6 :                         printf("%" PRIu64 "%%", state->parity[l].tick * 100U / tick_total);
    4960             :                 }
    4961           1 :                 printf("\n");
    4962             :         }
    4963             : }
    4964             : 
    4965           1 : void generate_configuration(const char* path)
    4966             : {
    4967             :         struct snapraid_state state;
    4968             :         struct snapraid_content* content;
    4969             :         unsigned i;
    4970             :         tommy_node* j;
    4971             : 
    4972           1 :         state_init(&state);
    4973             : 
    4974             :         /* mark that we are without a configuration file */
    4975           1 :         state.no_conf = 1;
    4976             : 
    4977             :         /* create the dummy content entry */
    4978           1 :         content = content_alloc(path, -1);
    4979             : 
    4980             :         /* adds the content entry */
    4981           1 :         tommy_list_insert_tail(&state.contentlist, &content->node, content);
    4982             : 
    4983             :         /* read the content file */
    4984           1 :         state_read(&state);
    4985             : 
    4986             :         /* output a dummy configuration file */
    4987           1 :         printf("# Configuration file generated from %s\n", path);
    4988           1 :         printf("\n");
    4989           1 :         printf("# Use this blocksize\n");
    4990           1 :         printf("blocksize %u\n", state.block_size / 1024);
    4991           1 :         printf("\n");
    4992           7 :         for (i = 0; i < state.level; ++i) {
    4993           6 :                 printf("# Set the correct path for the %s file\n", lev_name(i));
    4994           6 :                 if (state.parity[i].uuid[0])
    4995           6 :                         printf("# The file was in the disk with id '%s'\n", state.parity[i].uuid);
    4996           6 :                 printf("%s ENTER_HERE_THE_PARITY_FILE\n", lev_config_name(i));
    4997           6 :                 printf("\n");
    4998             :         }
    4999           1 :         printf("# Add any other content file\n");
    5000           1 :         printf("content %s\n", path);
    5001           1 :         printf("\n");
    5002           7 :         for (j = state.maplist; j; j = j->next) {
    5003           6 :                 struct snapraid_map* map = j->data;
    5004             :                 struct snapraid_disk* disk;
    5005           6 :                 printf("# Set the correct dir for disk '%s'\n", map->name);
    5006           6 :                 if (map->uuid[0])
    5007           6 :                         printf("# Disk '%s' is the one with id '%s'\n", map->name, map->uuid);
    5008           6 :                 disk = find_disk(&state, map->name);
    5009           6 :                 if (disk && disk->filelist) {
    5010           6 :                         struct snapraid_file* file = disk->filelist->data;
    5011           6 :                         if (file) {
    5012           6 :                                 printf("# and containing: %s\n", file->sub);
    5013             :                         }
    5014             :                 }
    5015           6 :                 printf("disk %s ENTER_HERE_THE_DIR\n", map->name);
    5016           6 :                 printf("\n");
    5017             :         }
    5018             : 
    5019           1 :         state_done(&state);
    5020           1 : }
    5021             : 

Generated by: LCOV version 1.10