LCOV - code coverage report
Current view: top level - cmdline - state.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 1262 1363 92.6 %
Date: 2016-01-16 Functions: 36 37 97.3 %

          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             : /**
      31             :  * Configure the multithread support.
      32             :  *
      33             :  * Multi thread for write could be either faster or slower, depending
      34             :  * on the specific conditions. With multithreads it's likely faster
      35             :  * writing to disk, but you'll need to access multiple times the same data,
      36             :  * being potentially slower.
      37             :  *
      38             :  * For upcoming SnapRAID version it's planned to add a mutex protection
      39             :  * at the file-system structure, slowing down multiple data access,
      40             :  * so we disable it.
      41             :  *
      42             :  * Multi thread for verify is instead always generally faster,
      43             :  * so we enable it if possible.
      44             :  */
      45             : #if HAVE_PTHREAD
      46             : /* #define HAVE_MT_WRITE 1 */
      47             : #define HAVE_MT_VERIFY 1
      48             : #endif
      49             : 
      50           7 : const char* lev_name(unsigned l)
      51             : {
      52           7 :         switch (l) {
      53           2 :         case 0 : return "Parity";
      54           1 :         case 1 : return "2-Parity";
      55           1 :         case 2 : return "3-Parity";
      56           1 :         case 3 : return "4-Parity";
      57           1 :         case 4 : return "5-Parity";
      58           1 :         case 5 : return "6-Parity";
      59             :         }
      60             : 
      61           0 :         return 0;
      62             : }
      63             : 
      64      179222 : const char* lev_config_name(unsigned l)
      65             : {
      66      179222 :         switch (l) {
      67       70562 :         case 0 : return "parity";
      68       57517 :         case 1 : return "2-parity";
      69       12792 :         case 2 : return "3-parity";
      70       12788 :         case 3 : return "4-parity";
      71       12784 :         case 4 : return "5-parity";
      72       12779 :         case 5 : return "6-parity";
      73             :         }
      74             : 
      75           0 :         return 0;
      76             : }
      77             : 
      78        6172 : static int lev_config_scan(const char* s, unsigned* level, unsigned* mode)
      79             : {
      80        6172 :         if (strcmp(s, "parity") == 0 || strcmp(s, "1-parity") == 0) {
      81         438 :                 *level = 0;
      82         438 :                 return 0;
      83             :         }
      84             : 
      85        5734 :         if (strcmp(s, "q-parity") == 0 || strcmp(s, "2-parity") == 0) {
      86         223 :                 *level = 1;
      87         223 :                 return 0;
      88             :         }
      89             : 
      90        5511 :         if (strcmp(s, "r-parity") == 0 || strcmp(s, "3-parity") == 0) {
      91         198 :                 *level = 2;
      92         198 :                 return 0;
      93             :         }
      94             : 
      95        5313 :         if (strcmp(s, "4-parity") == 0) {
      96         195 :                 *level = 3;
      97         195 :                 return 0;
      98             :         }
      99             : 
     100        5118 :         if (strcmp(s, "5-parity") == 0) {
     101         192 :                 *level = 4;
     102         192 :                 return 0;
     103             :         }
     104             : 
     105        4926 :         if (strcmp(s, "6-parity") == 0) {
     106         188 :                 *level = 5;
     107         188 :                 return 0;
     108             :         }
     109             : 
     110        4738 :         if (strcmp(s, "z-parity") == 0) {
     111           0 :                 *level = 2;
     112           0 :                 if (mode)
     113           0 :                         *mode = RAID_MODE_VANDERMONDE;
     114           0 :                 return 0;
     115             :         }
     116             : 
     117        4738 :         return -1;
     118             : }
     119             : 
     120         256 : const char* lev_raid_name(unsigned mode, unsigned n)
     121             : {
     122         256 :         switch (n) {
     123          34 :         case 1 : return "par1";
     124          25 :         case 2 : return "par2";
     125           3 :         case 3 : if (mode == RAID_MODE_CAUCHY)
     126           3 :                         return "par3";
     127             :                 else
     128           0 :                         return "parz";
     129           3 :         case 4 : return "par4";
     130           4 :         case 5 : return "par5";
     131         187 :         case 6 : return "par6";
     132             :         }
     133             : 
     134           0 :         return 0;
     135             : }
     136             : 
     137         258 : void state_init(struct snapraid_state* state)
     138             : {
     139             :         unsigned l;
     140             : 
     141         258 :         memset(&state->opt, 0, sizeof(state->opt));
     142         258 :         state->filter_hidden = 0;
     143         258 :         state->autosave = 0;
     144         258 :         state->need_write = 0;
     145         258 :         state->checked_read = 0;
     146         258 :         state->block_size = 256 * 1024; /* default 256 KiB */
     147         258 :         state->raid_mode = RAID_MODE_CAUCHY;
     148         258 :         state->file_mode = MODE_SEQUENTIAL;
     149        1806 :         for (l = 0; l < LEV_MAX; ++l) {
     150        1548 :                 state->parity[l].path[0] = 0;
     151        1548 :                 state->parity[l].smartctl[0] = 0;
     152        1548 :                 state->parity[l].uuid[0] = 0;
     153        1548 :                 state->parity[l].device = 0;
     154        1548 :                 state->parity[l].total_blocks = 0;
     155        1548 :                 state->parity[l].free_blocks = 0;
     156        1548 :                 state->parity[l].tick = 0;
     157        1548 :                 state->parity[l].skip_access = 0;
     158        1548 :                 state->parity[l].is_excluded = 0;
     159             :         }
     160         258 :         state->tick_io = 0;
     161         258 :         state->tick_cpu = 0;
     162         258 :         state->tick_last = tick();
     163         258 :         state->share[0] = 0;
     164         258 :         state->pool[0] = 0;
     165         258 :         state->pool_device = 0;
     166         258 :         state->lockfile[0] = 0;
     167         258 :         state->level = 1; /* default is the lowest protection */
     168         258 :         state->clear_past_hash = 0;
     169         258 :         state->no_conf = 0;
     170             : 
     171         258 :         tommy_list_init(&state->disklist);
     172         258 :         tommy_list_init(&state->maplist);
     173         258 :         tommy_list_init(&state->contentlist);
     174         258 :         tommy_list_init(&state->filterlist);
     175         258 :         tommy_list_init(&state->importlist);
     176         258 :         tommy_hashdyn_init(&state->importset);
     177         258 :         tommy_hashdyn_init(&state->previmportset);
     178         258 :         tommy_hashdyn_init(&state->searchset);
     179         258 :         tommy_arrayblkof_init(&state->infoarr, sizeof(snapraid_info));
     180         258 : }
     181             : 
     182         236 : void state_done(struct snapraid_state* state)
     183             : {
     184         236 :         tommy_list_foreach(&state->disklist, (tommy_foreach_func*)disk_free);
     185         236 :         tommy_list_foreach(&state->maplist, (tommy_foreach_func*)map_free);
     186         236 :         tommy_list_foreach(&state->contentlist, (tommy_foreach_func*)content_free);
     187         236 :         tommy_list_foreach(&state->filterlist, (tommy_foreach_func*)filter_free);
     188         236 :         tommy_list_foreach(&state->importlist, (tommy_foreach_func*)import_file_free);
     189         236 :         tommy_hashdyn_foreach(&state->searchset, (tommy_foreach_func*)search_file_free);
     190         236 :         tommy_hashdyn_done(&state->importset);
     191         236 :         tommy_hashdyn_done(&state->previmportset);
     192         236 :         tommy_hashdyn_done(&state->searchset);
     193         236 :         tommy_arrayblkof_done(&state->infoarr);
     194         236 : }
     195             : 
     196             : /**
     197             :  * Check the configuration.
     198             :  */
     199         256 : static void state_config_check(struct snapraid_state* state, const char* path, tommy_list* filterlist_disk)
     200             : {
     201             :         tommy_node* i;
     202             :         unsigned l;
     203             : 
     204             :         /* check for parity level */
     205         256 :         if (state->raid_mode == RAID_MODE_VANDERMONDE) {
     206           0 :                 if (state->level > 3) {
     207             :                         /* LCOV_EXCL_START */
     208             :                         log_fatal("If you use the z-parity you cannot have more than 3 parities.\n");
     209             :                         exit(EXIT_FAILURE);
     210             :                         /* LCOV_EXCL_STIO */
     211             :                 }
     212             :         }
     213             : 
     214             :         for (l = 0; l < state->level; ++l) {
     215             :                 if (state->parity[l].path[0] == 0) {
     216             :                         /* LCOV_EXCL_START */
     217             :                         log_fatal("No '%s' specification in '%s'\n", lev_config_name(l), path);
     218             :                         exit(EXIT_FAILURE);
     219             :                         /* LCOV_EXCL_STOP */
     220             :                 }
     221             :         }
     222             : 
     223         256 :         if (tommy_list_empty(&state->contentlist)) {
     224             :                 /* LCOV_EXCL_START */
     225             :                 log_fatal("No 'content' specification in '%s'\n", path);
     226             :                 exit(EXIT_FAILURE);
     227             :                 /* LCOV_EXCL_STOP */
     228             :         }
     229             : 
     230             :         /* check for equal paths */
     231        1759 :         for (i = state->contentlist; i != 0; i = i->next) {
     232        1503 :                 struct snapraid_content* content = i->data;
     233             : 
     234        9791 :                 for (l = 0; l < state->level; ++l) {
     235        8288 :                         if (pathcmp(state->parity[l].path, content->content) == 0) {
     236             :                                 /* LCOV_EXCL_START */
     237             :                                 log_fatal("Same path used for '%s' and 'content' as '%s'\n", lev_config_name(l), content->content);
     238             :                                 exit(EXIT_FAILURE);
     239             :                                 /* LCOV_EXCL_STOP */
     240             :                         }
     241             :                 }
     242             :         }
     243             : 
     244             :         /* check device of data disks */
     245         256 :         if (!state->opt.skip_device && !state->opt.skip_disk_access) {
     246           0 :                 for (i = state->disklist; i != 0; i = i->next) {
     247             :                         tommy_node* j;
     248           0 :                         struct snapraid_disk* disk = i->data;
     249             : 
     250             :                         /* skip data disks that are not accessible */
     251           0 :                         if (disk->skip_access)
     252           0 :                                 continue;
     253             : 
     254             : #ifdef _WIN32
     255             :                         if (disk->device == 0) {
     256             :                                 /* LCOV_EXCL_START */
     257             :                                 log_fatal("Disk '%s' has a zero serial number.\n", disk->dir);
     258             :                                 log_fatal("This is not necessarily wrong, but for using SnapRAID\n");
     259             :                                 log_fatal("it's better to change the serial number of the disk.\n");
     260             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     261             :                                 exit(EXIT_FAILURE);
     262             :                                 /* LCOV_EXCL_STOP */
     263             :                         }
     264             : #endif
     265             : 
     266           0 :                         for (j = i->next; j != 0; j = j->next) {
     267           0 :                                 struct snapraid_disk* other = j->data;
     268           0 :                                 if (disk->device == other->device) {
     269           0 :                                         if (state->opt.force_device) {
     270             :                                                 /* mark disks to be skipped */
     271           0 :                                                 disk->skip_access = 1;
     272           0 :                                                 other->skip_access = 1;
     273           0 :                                                 if (!state->opt.no_warnings)
     274           0 :                                                         log_fatal("DANGER! Ignoring that disks '%s' and '%s' are on the same device\n", disk->name, other->name);
     275             :                                         } else {
     276             :                                                 /* LCOV_EXCL_START */
     277             :                                                 log_fatal("Disks '%s' and '%s' are on the same device.\n", disk->dir, other->dir);
     278             : #ifdef _WIN32
     279             :                                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     280             :                                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     281             :                                                 log_fatal("to change one of the disk serial.\n");
     282             : #endif
     283             :                                                 /* in "fix" we allow to continue anyway */
     284             :                                                 if (strcmp(state->command, "fix") == 0) {
     285             :                                                         log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
     286             :                                                 }
     287             :                                                 exit(EXIT_FAILURE);
     288             :                                                 /* LCOV_EXCL_STOP */
     289             :                                         }
     290             :                                 }
     291             :                         }
     292             : 
     293             :                         /* skip data disks that are not accessible */
     294           0 :                         if (disk->skip_access)
     295           0 :                                 continue;
     296             : 
     297           0 :                         if (!state->opt.skip_parity_access) {
     298           0 :                                 for (l = 0; l < state->level; ++l) {
     299           0 :                                         if (disk->device == state->parity[l].device) {
     300             :                                                 /* LCOV_EXCL_START */
     301             :                                                 log_fatal("Disk '%s' and %s '%s' are on the same device.\n", disk->dir, lev_name(l), state->parity[l].path);
     302             : #ifdef _WIN32
     303             :                                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     304             :                                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     305             :                                                 log_fatal("to change one of the disk serial.\n");
     306             : #endif
     307             :                                                 exit(EXIT_FAILURE);
     308             :                                                 /* LCOV_EXCL_STOP */
     309             :                                         }
     310             :                                 }
     311             :                         }
     312             : 
     313           0 :                         if (state->pool[0] != 0 && disk->device == state->pool_device) {
     314             :                                 /* LCOV_EXCL_START */
     315             :                                 log_fatal("Disk '%s' and pool '%s' are on the same device.\n", disk->dir, state->pool);
     316             : #ifdef _WIN32
     317             :                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", disk->device);
     318             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     319             :                                 log_fatal("to change one of the disk serial.\n");
     320             : #endif
     321             :                                 exit(EXIT_FAILURE);
     322             :                                 /* LCOV_EXCL_STOP */
     323             :                         }
     324             :                 }
     325             :         }
     326             : 
     327             :         /* check device of parity disks */
     328         256 :         if (!state->opt.skip_device && !state->opt.skip_parity_access) {
     329           0 :                 for (l = 0; l < state->level; ++l) {
     330             :                         unsigned j;
     331             : 
     332             :                         /* skip parity disks that are not accessible */
     333           0 :                         if (state->parity[l].skip_access)
     334           0 :                                 continue;
     335             : 
     336             : #ifdef _WIN32
     337             :                         if (state->parity[l].device == 0) {
     338             :                                 /* LCOV_EXCL_START */
     339             :                                 log_fatal("Disk '%s' has a zero serial number.\n", state->parity[l].path);
     340             :                                 log_fatal("This is not necessarily wrong, but for using SnapRAID\n");
     341             :                                 log_fatal("it's better to change the serial number of the disk.\n");
     342             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     343             :                                 exit(EXIT_FAILURE);
     344             :                                 /* LCOV_EXCL_STOP */
     345             :                         }
     346             : #endif
     347             : 
     348           0 :                         for (j = l + 1; j < state->level; ++j) {
     349           0 :                                 if (state->parity[l].device == state->parity[j].device) {
     350           0 :                                         if (state->opt.force_device) {
     351             :                                                 /* mark disks to be skipped */
     352           0 :                                                 state->parity[l].skip_access = 1;
     353           0 :                                                 state->parity[j].skip_access = 1;
     354           0 :                                                 if (!state->opt.no_warnings)
     355           0 :                                                         log_fatal("DANGER! Skipping parities '%s' and '%s' on the same device\n", lev_config_name(l), lev_config_name(j));
     356             :                                         } else {
     357             :                                                 /* LCOV_EXCL_START */
     358             :                                                 log_fatal("Parity '%s' and '%s' are on the same device.\n", state->parity[l].path, state->parity[j].path);
     359             : #ifdef _WIN32
     360             :                                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->parity[l].device);
     361             :                                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     362             :                                                 log_fatal("to change one of the disk serial.\n");
     363             : #endif
     364             :                                                 /* in "fix" we allow to continue anyway */
     365             :                                                 if (strcmp(state->command, "fix") == 0) {
     366             :                                                         log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
     367             :                                                 }
     368             :                                                 exit(EXIT_FAILURE);
     369             :                                                 /* LCOV_EXCL_STOP */
     370             :                                         }
     371             :                                 }
     372             :                         }
     373             : 
     374             :                         /* skip parity disks that are not accessible */
     375           0 :                         if (state->parity[l].skip_access)
     376           0 :                                 continue;
     377             : 
     378           0 :                         if (state->pool[0] != 0 && state->pool_device == state->parity[l].device) {
     379             :                                 /* LCOV_EXCL_START */
     380             :                                 log_fatal("Pool '%s' and parity '%s' are on the same device.\n", state->pool, state->parity[l].path);
     381             : #ifdef _WIN32
     382             :                                 log_fatal("Both have the serial number '%" PRIx64 "'.\n", state->pool_device);
     383             :                                 log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'\n");
     384             :                                 log_fatal("to change one of the disk serial.\n");
     385             : #endif
     386             :                                 exit(EXIT_FAILURE);
     387             :                                 /* LCOV_EXCL_STOP */
     388             :                         }
     389             :                 }
     390             :         }
     391             : 
     392             :         /* check device of pool disk */
     393             : #ifdef _WIN32
     394             :         if (!state->opt.skip_device) {
     395             :                 if (state->pool[0] != 0 && state->pool_device == 0) {
     396             :                         /* LCOV_EXCL_START */
     397             :                         log_fatal("Disk '%s' has a zero serial number.\n", state->pool);
     398             :                         log_fatal("This is not necessarily wrong, but for using SnapRAID\n");
     399             :                         log_fatal("it's better to change the serial number of the disk.\n");
     400             :                         log_fatal("Try using the 'VolumeID' tool by 'Mark Russinovich'.\n");
     401             :                         exit(EXIT_FAILURE);
     402             :                         /* LCOV_EXCL_STOP */
     403             :                 }
     404             :         }
     405             : #endif
     406             : 
     407             :         /* count the content files */
     408         256 :         if (!state->opt.skip_device && !state->opt.skip_content_access) {
     409             :                 unsigned content_count;
     410             : 
     411           0 :                 content_count = 0;
     412           0 :                 for (i = state->contentlist; i != 0; i = i->next) {
     413             :                         tommy_node* j;
     414           0 :                         struct snapraid_content* content = i->data;
     415             : 
     416             :                         /* check if there are others in the same disk */
     417           0 :                         for (j = i->next; j != 0; j = j->next) {
     418           0 :                                 struct snapraid_content* other = j->data;
     419           0 :                                 if (content->device == other->device) {
     420           0 :                                         break;
     421             :                                 }
     422             :                         }
     423           0 :                         if (j != 0) {
     424             :                                 /* skip it */
     425           0 :                                 continue;
     426             :                         }
     427             : 
     428           0 :                         ++content_count;
     429             :                 }
     430             : 
     431           0 :                 if (content_count < state->level + 1) {
     432             :                         /* LCOV_EXCL_START */
     433             :                         log_fatal("You must have at least %d 'content' files in different disks.\n", state->level + 1);
     434             :                         exit(EXIT_FAILURE);
     435             :                         /* LCOV_EXCL_STOP */
     436             :                 }
     437             :         }
     438             : 
     439             :         /* check for speed */
     440             : #ifdef CONFIG_X86
     441         256 :         if (!raid_cpu_has_ssse3())
     442             : #endif
     443           0 :         if (state->raid_mode == RAID_MODE_CAUCHY && !state->opt.no_warnings) {
     444           0 :                 if (state->level == 3) {
     445           0 :                         log_fatal("WARNING! Your CPU doesn't have a fast implementation for triple parity.\n");
     446           0 :                         log_fatal("WARNING! It's recommended to switch to 'z-parity' instead than '3-parity'.\n");
     447           0 :                 } else if (state->level > 3) {
     448           0 :                         log_fatal("WARNING! Your CPU doesn't have a fast implementation beyond triple parity.\n");
     449           0 :                         log_fatal("WARNING! It's recommended to reduce the parity levels to triple parity.\n");
     450             :                 }
     451             :         }
     452             : 
     453             :         /* ensure that specified filter disks are valid ones */
     454         264 :         for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
     455             :                 tommy_node* j;
     456           8 :                 struct snapraid_filter* filter = i->data;
     457          43 :                 for (j = state->disklist; j != 0; j = j->next) {
     458          38 :                         struct snapraid_disk* disk = j->data;
     459          38 :                         if (fnmatch(filter->pattern, disk->name, FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     460           3 :                                 break;
     461             :                 }
     462           8 :                 if (j == 0) {
     463             :                         /* check matching with parity disks */
     464          20 :                         for (l = 0; l < state->level; ++l)
     465          20 :                                 if (fnmatch(filter->pattern, lev_config_name(l), FNM_CASEINSENSITIVE_FOR_WIN) == 0)
     466           5 :                                         break;
     467           5 :                         if (l == state->level) {
     468             :                                 /* LCOV_EXCL_START */
     469             :                                 log_fatal("Option -d, --filter-disk %s doesn't match any data or parity disk.\n", filter->pattern);
     470             :                                 exit(EXIT_FAILURE);
     471             :                                 /* LCOV_EXCL_STOP */
     472             :                         }
     473             :                 }
     474             :         }
     475         256 : }
     476             : 
     477             : /**
     478             :  * Validate the smartctl command.
     479             :  *
     480             :  * It must contains only one %s string, and not other % chars.
     481             :  */
     482         362 : static int validate_smartctl(const char* custom)
     483             : {
     484         362 :         const char* s = custom;
     485         362 :         int arg = 0;
     486             : 
     487        2534 :         while (*s) {
     488        1810 :                 if (s[0] == '%' && s[1] == 's') {
     489         181 :                         if (arg) {
     490             :                                 /* LCOV_EXCL_START */
     491             :                                 return -1;
     492             :                                 /* LCOV_EXCL_STOP */
     493             :                         }
     494         181 :                         arg = 1;
     495        1629 :                 } else if (s[0] == '%') {
     496             :                         /* LCOV_EXCL_START */
     497             :                         return -1;
     498             :                         /* LCOV_EXCL_STOP */
     499             :                 }
     500             : 
     501        1810 :                 ++s;
     502             :         }
     503             : 
     504         362 :         return 0;
     505             : }
     506             : 
     507         257 : void state_config(struct snapraid_state* state, const char* path, const char* command, struct snapraid_option* opt, tommy_list* filterlist_disk)
     508             : {
     509             :         STREAM* f;
     510             :         unsigned line;
     511             :         tommy_node* i;
     512             :         unsigned l;
     513             : 
     514             :         /* copy the options */
     515         257 :         state->opt = *opt;
     516             : 
     517             :         /* if unset, sort by physical order */
     518         257 :         if (!state->opt.force_order)
     519           1 :                 state->opt.force_order = SORT_PHYSICAL;
     520             : 
     521             :         /* adjust file mode */
     522         257 :         if (state->opt.skip_sequential)
     523           1 :                 state->file_mode &= ~MODE_SEQUENTIAL;
     524             : 
     525             :         /* store current command */
     526         257 :         state->command = command;
     527             : 
     528         257 :         log_tag("conf:%s\n", path);
     529             : 
     530         257 :         f = sopen_read(path);
     531         257 :         if (!f) {
     532             :                 /* LCOV_EXCL_START */
     533             :                 if (errno == ENOENT) {
     534             :                         log_fatal("No configuration file found at '%s'\n", path);
     535             :                 } else if (errno == EACCES) {
     536             :                         log_fatal("You do not have rights to access the configuration file '%s'\n", path);
     537             :                 } else {
     538             :                         log_fatal("Error opening the configuration file '%s'. %s.\n", path, strerror(errno));
     539             :                 }
     540             :                 exit(EXIT_FAILURE);
     541             :                 /* LCOV_EXCL_STOP */
     542             :         }
     543             : 
     544         257 :         line = 1;
     545             :         while (1) {
     546             :                 char tag[PATH_MAX];
     547             :                 char buffer[PATH_MAX];
     548             :                 int ret;
     549             :                 int c;
     550             :                 unsigned level;
     551             : 
     552             :                 /* skip initial spaces */
     553        6067 :                 sgetspace(f);
     554             : 
     555             :                 /* read the command */
     556        6067 :                 ret = sgettok(f, tag, sizeof(tag));
     557        6067 :                 if (ret < 0) {
     558             :                         /* LCOV_EXCL_START */
     559             :                         log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
     560             :                         exit(EXIT_FAILURE);
     561             :                         /* LCOV_EXCL_STOP */
     562             :                 }
     563             : 
     564             :                 /* skip spaces after the command */
     565        6067 :                 sgetspace(f);
     566             : 
     567        6067 :                 if (strcmp(tag, "blocksize") == 0
     568             :                         /* block_size is the old format of the option */
     569        5810 :                         || strcmp(tag, "block_size") == 0) {
     570             : 
     571         257 :                         ret = sgetu32(f, &state->block_size);
     572         257 :                         if (ret < 0) {
     573             :                                 /* LCOV_EXCL_START */
     574             :                                 log_fatal("Invalid 'blocksize' specification in '%s' at line %u\n", path, line);
     575             :                                 exit(EXIT_FAILURE);
     576             :                                 /* LCOV_EXCL_STOP */
     577             :                         }
     578         257 :                         if (state->block_size < 1) {
     579             :                                 /* LCOV_EXCL_START */
     580             :                                 log_fatal("Too small 'blocksize' specification in '%s' at line %u\n", path, line);
     581             :                                 exit(EXIT_FAILURE);
     582             :                                 /* LCOV_EXCL_STOP */
     583             :                         }
     584         257 :                         if (state->block_size > 16 * 1024) {
     585             :                                 /* LCOV_EXCL_START */
     586             :                                 log_fatal("Too big 'blocksize' specification in '%s' at line %u\n", path, line);
     587             :                                 exit(EXIT_FAILURE);
     588             :                                 /* LCOV_EXCL_STOP */
     589             :                         }
     590             :                         /* check if it's a power of 2 */
     591         257 :                         if ((state->block_size & (state->block_size - 1)) != 0) {
     592             :                                 /* LCOV_EXCL_START */
     593             :                                 log_fatal("Not power of 2 'blocksize' specification in '%s' at line %u\n", path, line);
     594             :                                 exit(EXIT_FAILURE);
     595             :                                 /* LCOV_EXCL_STOP */
     596             :                         }
     597         257 :                         state->block_size *= 1024;
     598        5810 :                 } else if (lev_config_scan(tag, &level, &state->raid_mode) == 0) {
     599             :                         char device[PATH_MAX];
     600             :                         char* slash;
     601             :                         uint64_t dev;
     602             :                         int skip_access;
     603             : 
     604        1253 :                         if (*state->parity[level].path) {
     605             :                                 /* LCOV_EXCL_START */
     606             :                                 log_fatal("Multiple '%s' specification in '%s' at line %u\n", tag, path, line);
     607             :                                 exit(EXIT_FAILURE);
     608             :                                 /* LCOV_EXCL_STOP */
     609             :                         }
     610             : 
     611        1253 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     612        1253 :                         if (ret < 0) {
     613             :                                 /* LCOV_EXCL_START */
     614             :                                 log_fatal("Invalid '%s' specification in '%s' at line %u\n", tag, path, line);
     615             :                                 exit(EXIT_FAILURE);
     616             :                                 /* LCOV_EXCL_STOP */
     617             :                         }
     618             : 
     619        1253 :                         if (!*buffer) {
     620             :                                 /* LCOV_EXCL_START */
     621             :                                 log_fatal("Empty '%s' specification in '%s' at line %u\n", tag, path, line);
     622             :                                 exit(EXIT_FAILURE);
     623             :                                 /* LCOV_EXCL_STOP */
     624             :                         }
     625             : 
     626        1253 :                         pathimport(state->parity[level].path, sizeof(state->parity[level].path), buffer);
     627             : 
     628        1253 :                         skip_access = 0;
     629        1253 :                         if (!state->opt.skip_parity_access) {
     630             :                                 struct stat st;
     631             : 
     632             :                                 /* get the device of the directory containing the parity file */
     633        1078 :                                 pathimport(device, sizeof(device), buffer);
     634        1078 :                                 slash = strrchr(device, '/');
     635        1078 :                                 if (slash)
     636        1078 :                                         *slash = 0;
     637             :                                 else
     638           0 :                                         pathcpy(device, sizeof(device), ".");
     639             : 
     640        1078 :                                 if (stat(device, &st) == 0) {
     641        1076 :                                         dev = st.st_dev;
     642             :                                 } else {
     643             :                                         /* if the disk can be skipped */
     644           2 :                                         if (state->opt.force_device) {
     645             :                                                 /* use a fake device, and mark the disk to be skipped */
     646           1 :                                                 dev = 0;
     647           1 :                                                 skip_access = 1;
     648           1 :                                                 log_fatal("DANGER! Skipping inaccessible parity disk '%s'...\n", tag);
     649             :                                         } else {
     650             :                                                 /* LCOV_EXCL_START */
     651             :                                                 log_fatal("Error accessing 'parity' dir '%s' specification in '%s' at line %u\n", device, path, line);
     652             : 
     653             :                                                 /* in "fix" we allow to continue anyway */
     654             :                                                 if (strcmp(state->command, "fix") == 0) {
     655             :                                                         log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
     656             :                                                 }
     657             :                                                 exit(EXIT_FAILURE);
     658             :                                                 /* LCOV_EXCL_STOP */
     659             :                                         }
     660             :                                 }
     661             :                         } else {
     662             :                                 /* use a fake device */
     663         175 :                                 dev = 0;
     664             :                         }
     665             : 
     666        1252 :                         state->parity[level].device = dev;
     667        1252 :                         state->parity[level].skip_access = skip_access;
     668             : 
     669             :                         /* adjust the level */
     670        1252 :                         if (state->level < level + 1)
     671         995 :                                 state->level = level + 1;
     672        4557 :                 } else if (strcmp(tag, "share") == 0) {
     673          34 :                         if (*state->share) {
     674             :                                 /* LCOV_EXCL_START */
     675             :                                 log_fatal("Multiple 'share' specification in '%s' at line %u\n", path, line);
     676             :                                 exit(EXIT_FAILURE);
     677             :                                 /* LCOV_EXCL_STOP */
     678             :                         }
     679             : 
     680          34 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     681          34 :                         if (ret < 0) {
     682             :                                 /* LCOV_EXCL_START */
     683             :                                 log_fatal("Invalid 'share' specification in '%s' at line %u\n", path, line);
     684             :                                 exit(EXIT_FAILURE);
     685             :                                 /* LCOV_EXCL_STOP */
     686             :                         }
     687             : 
     688          34 :                         if (!*buffer) {
     689             :                                 /* LCOV_EXCL_START */
     690             :                                 log_fatal("Empty 'share' specification in '%s' at line %u\n", path, line);
     691             :                                 exit(EXIT_FAILURE);
     692             :                                 /* LCOV_EXCL_STOP */
     693             :                         }
     694             : 
     695          34 :                         pathimport(state->share, sizeof(state->share), buffer);
     696        4523 :                 } else if (strcmp(tag, "pool") == 0) {
     697             :                         struct stat st;
     698             : 
     699          34 :                         if (*state->pool) {
     700             :                                 /* LCOV_EXCL_START */
     701             :                                 log_fatal("Multiple 'pool' specification in '%s' at line %u\n", path, line);
     702             :                                 exit(EXIT_FAILURE);
     703             :                                 /* LCOV_EXCL_STOP */
     704             :                         }
     705             : 
     706          34 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     707          34 :                         if (ret < 0) {
     708             :                                 /* LCOV_EXCL_START */
     709             :                                 log_fatal("Invalid 'pool' specification in '%s' at line %u\n", path, line);
     710             :                                 exit(EXIT_FAILURE);
     711             :                                 /* LCOV_EXCL_STOP */
     712             :                         }
     713             : 
     714          34 :                         if (!*buffer) {
     715             :                                 /* LCOV_EXCL_START */
     716             :                                 log_fatal("Empty 'pool' specification in '%s' at line %u\n", path, line);
     717             :                                 exit(EXIT_FAILURE);
     718             :                                 /* LCOV_EXCL_STOP */
     719             :                         }
     720             : 
     721          34 :                         pathimport(state->pool, sizeof(state->pool), buffer);
     722             : 
     723             :                         /* get the device of the directory containing the pool tree */
     724          34 :                         if (stat(buffer, &st) != 0) {
     725             :                                 /* LCOV_EXCL_START */
     726             :                                 log_fatal("Error accessing 'pool' dir '%s' specification in '%s' at line %u\n", buffer, path, line);
     727             :                                 exit(EXIT_FAILURE);
     728             :                                 /* LCOV_EXCL_STOP */
     729             :                         }
     730             : 
     731          34 :                         state->pool_device = st.st_dev;
     732        4489 :                 } else if (strcmp(tag, "content") == 0) {
     733             :                         struct snapraid_content* content;
     734             :                         char device[PATH_MAX];
     735             :                         char* slash;
     736             :                         struct stat st;
     737             :                         uint64_t dev;
     738             : 
     739        1503 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     740        1503 :                         if (ret < 0) {
     741             :                                 /* LCOV_EXCL_START */
     742             :                                 log_fatal("Invalid 'content' specification in '%s' at line %u\n", path, line);
     743             :                                 exit(EXIT_FAILURE);
     744             :                                 /* LCOV_EXCL_STOP */
     745             :                         }
     746             : 
     747        1503 :                         if (pathcmp(buffer, "/dev/null") == 0 || pathcmp(buffer, "NUL") == 0) {
     748             :                                 /* LCOV_EXCL_START */
     749             :                                 log_fatal("You cannot use the null device as 'content' specification in '%s' at line %u\n", path, line);
     750             :                                 exit(EXIT_FAILURE);
     751             :                                 /* LCOV_EXCL_STOP */
     752             :                         }
     753             : 
     754        1503 :                         if (!*buffer) {
     755             :                                 /* LCOV_EXCL_START */
     756             :                                 log_fatal("Empty 'content' specification in '%s' at line %u\n", path, line);
     757             :                                 exit(EXIT_FAILURE);
     758             :                                 /* LCOV_EXCL_STOP */
     759             :                         }
     760             : 
     761             :                         /* check if the content file is already specified */
     762        5647 :                         for (i = state->contentlist; i != 0; i = i->next) {
     763        4144 :                                 content = i->data;
     764        4144 :                                 if (pathcmp(content->content, buffer) == 0)
     765           0 :                                         break;
     766             :                         }
     767        1503 :                         if (i) {
     768             :                                 /* LCOV_EXCL_START */
     769             :                                 log_fatal("Duplicate 'content' specification in '%s' at line %u\n", path, line);
     770             :                                 exit(EXIT_FAILURE);
     771             :                                 /* LCOV_EXCL_STOP */
     772             :                         }
     773             : 
     774             :                         /* get the device of the directory containing the content file */
     775        1503 :                         pathimport(device, sizeof(device), buffer);
     776        1503 :                         slash = strrchr(device, '/');
     777        1503 :                         if (slash)
     778        1503 :                                 *slash = 0;
     779             :                         else
     780           0 :                                 pathcpy(device, sizeof(device), ".");
     781        1503 :                         if (stat(device, &st) == 0) {
     782        1502 :                                 dev = st.st_dev;
     783             :                         } else {
     784           1 :                                 if (state->opt.skip_content_access) {
     785             :                                         /* use a fake device */
     786           1 :                                         dev = 0;
     787           1 :                                         log_fatal("WARNING! Skipping inaccessible content file '%s'...\n", buffer);
     788             :                                 } else {
     789             :                                         /* LCOV_EXCL_START */
     790             :                                         log_fatal("Error accessing 'content' dir '%s' specification in '%s' at line %u\n", device, path, line);
     791             :                                         exit(EXIT_FAILURE);
     792             :                                         /* LCOV_EXCL_STOP */
     793             :                                 }
     794             :                         }
     795             : 
     796             :                         /* set the lock file at the first accessible content file */
     797        1503 :                         if (state->lockfile[0] == 0 && dev != 0) {
     798         256 :                                 pathcpy(state->lockfile, sizeof(state->lockfile), buffer);
     799         256 :                                 pathcat(state->lockfile, sizeof(state->lockfile), ".lock");
     800             :                         }
     801             : 
     802        1503 :                         content = content_alloc(buffer, dev);
     803             : 
     804        1503 :                         tommy_list_insert_tail(&state->contentlist, &content->node, content);
     805        2986 :                 } else if (strcmp(tag, "disk") == 0) {
     806             :                         char dir[PATH_MAX];
     807             :                         char device[PATH_MAX];
     808             :                         char uuid[UUID_MAX];
     809             :                         struct snapraid_disk* disk;
     810             :                         uint64_t dev;
     811             :                         int skip_access;
     812             : 
     813        1531 :                         ret = sgettok(f, buffer, sizeof(buffer));
     814        1531 :                         if (ret < 0) {
     815             :                                 /* LCOV_EXCL_START */
     816             :                                 log_fatal("Invalid 'disk' name specification in '%s' at line %u\n", path, line);
     817             :                                 exit(EXIT_FAILURE);
     818             :                                 /* LCOV_EXCL_STOP */
     819             :                         }
     820             : 
     821        1531 :                         if (!*buffer) {
     822             :                                 /* LCOV_EXCL_START */
     823             :                                 log_fatal("Empty 'disk' name specification in '%s' at line %u\n", path, line);
     824             :                                 exit(EXIT_FAILURE);
     825             :                                 /* LCOV_EXCL_STOP */
     826             :                         }
     827             : 
     828        1531 :                         sgetspace(f);
     829             : 
     830        1531 :                         ret = sgetlasttok(f, dir, sizeof(dir));
     831        1531 :                         if (ret < 0) {
     832             :                                 /* LCOV_EXCL_START */
     833             :                                 log_fatal("Invalid 'disk' dir specification in '%s' at line %u\n", path, line);
     834             :                                 exit(EXIT_FAILURE);
     835             :                                 /* LCOV_EXCL_STOP */
     836             :                         }
     837             : 
     838        1531 :                         if (!*dir) {
     839             :                                 /* LCOV_EXCL_START */
     840             :                                 log_fatal("Empty 'disk' dir specification in '%s' at line %u\n", path, line);
     841             :                                 exit(EXIT_FAILURE);
     842             :                                 /* LCOV_EXCL_STOP */
     843             :                         }
     844             : 
     845             :                         /* get the device of the dir */
     846        1531 :                         pathimport(device, sizeof(device), dir);
     847             : 
     848             :                         /* check if the disk name already exists */
     849        5346 :                         for (i = state->disklist; i != 0; i = i->next) {
     850        3815 :                                 disk = i->data;
     851        3815 :                                 if (strcmp(disk->name, buffer) == 0)
     852           0 :                                         break;
     853             :                         }
     854        1531 :                         if (i) {
     855             :                                 /* LCOV_EXCL_START */
     856             :                                 log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
     857             :                                 exit(EXIT_FAILURE);
     858             :                                 /* LCOV_EXCL_STOP */
     859             :                         }
     860             : 
     861             :                         /* if the disk has to be present */
     862        1531 :                         skip_access = 0;
     863        1531 :                         if (!state->opt.skip_disk_access) {
     864             :                                 struct stat st;
     865             : 
     866        1387 :                                 if (stat(device, &st) == 0) {
     867        1386 :                                         dev = st.st_dev;
     868             : 
     869             :                                         /* read the uuid, if unsupported use an empty one */
     870        1386 :                                         if (devuuid(dev, uuid, sizeof(uuid)) != 0) {
     871           0 :                                                 *uuid = 0;
     872             :                                         }
     873             : 
     874             :                                         /* fake a different UUID when testing */
     875        1386 :                                         if (state->opt.fake_uuid) {
     876           2 :                                                 snprintf(uuid, sizeof(uuid), "fake-uuid-%d", state->opt.fake_uuid);
     877           2 :                                                 --state->opt.fake_uuid;
     878             :                                         }
     879             :                                 } else {
     880             :                                         /* if the disk can be skipped */
     881           1 :                                         if (state->opt.force_device) {
     882             :                                                 /* use a fake device, and mark the disk to be skipped */
     883           1 :                                                 dev = 0;
     884           1 :                                                 *uuid = 0;
     885           1 :                                                 skip_access = 1;
     886           1 :                                                 log_fatal("DANGER! Skipping inaccessible data disk '%s'...\n", buffer);
     887             :                                         } else {
     888             :                                                 /* LCOV_EXCL_START */
     889             :                                                 log_fatal("Error accessing 'disk' '%s' specification in '%s' at line %u\n", dir, device, line);
     890             : 
     891             :                                                 /* in "fix" we allow to continue anyway */
     892             :                                                 if (strcmp(state->command, "fix") == 0) {
     893             :                                                         log_fatal("You can '%s' anyway, using 'snapraid --force-device %s'.\n", state->command, state->command);
     894             :                                                 }
     895             :                                                 exit(EXIT_FAILURE);
     896             :                                                 /* LCOV_EXCL_STOP */
     897             :                                         }
     898             :                                 }
     899             :                         } else {
     900             :                                 /* use a fake device */
     901         144 :                                 dev = 0;
     902         144 :                                 *uuid = 0;
     903             :                         }
     904             : 
     905        1531 :                         disk = disk_alloc(buffer, dir, dev, uuid, skip_access);
     906             : 
     907        1531 :                         tommy_list_insert_tail(&state->disklist, &disk->node, disk);
     908        1455 :                 } else if (strcmp(tag, "smartctl") == 0) {
     909             :                         char custom[PATH_MAX];
     910             : 
     911         362 :                         ret = sgettok(f, buffer, sizeof(buffer));
     912         362 :                         if (ret < 0) {
     913             :                                 /* LCOV_EXCL_START */
     914             :                                 log_fatal("Invalid 'smartctl' name specification in '%s' at line %u\n", path, line);
     915             :                                 exit(EXIT_FAILURE);
     916             :                                 /* LCOV_EXCL_STOP */
     917             :                         }
     918             : 
     919         362 :                         if (!*buffer) {
     920             :                                 /* LCOV_EXCL_START */
     921             :                                 log_fatal("Empty 'smartctl' name specification in '%s' at line %u\n", path, line);
     922             :                                 exit(EXIT_FAILURE);
     923             :                                 /* LCOV_EXCL_STOP */
     924             :                         }
     925             : 
     926         362 :                         sgetspace(f);
     927             : 
     928         362 :                         ret = sgetlasttok(f, custom, sizeof(custom));
     929         362 :                         if (ret < 0) {
     930             :                                 /* LCOV_EXCL_START */
     931             :                                 log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
     932             :                                 exit(EXIT_FAILURE);
     933             :                                 /* LCOV_EXCL_STOP */
     934             :                         }
     935             : 
     936         362 :                         if (!*custom) {
     937             :                                 /* LCOV_EXCL_START */
     938             :                                 log_fatal("Empty 'smartctl' option specification in '%s' at line %u\n", path, line);
     939             :                                 exit(EXIT_FAILURE);
     940             :                                 /* LCOV_EXCL_STOP */
     941             :                         }
     942             : 
     943         362 :                         if (validate_smartctl(custom) != 0) {
     944             :                                 /* LCOV_EXCL_START */
     945             :                                 log_fatal("Invalid 'smartctl' option specification in '%s' at line %u\n", path, line);
     946             :                                 exit(EXIT_FAILURE);
     947             :                                 /* LCOV_EXCL_STOP */
     948             :                         }
     949             : 
     950             :                         /* search for parity */
     951         362 :                         if (lev_config_scan(buffer, &level, 0) == 0) {
     952         181 :                                 if (state->parity[level].smartctl[0] != 0) {
     953             :                                         /* LCOV_EXCL_START */
     954             :                                         log_fatal("Duplicate parity smartctl '%s' at line %u\n", buffer, line);
     955             :                                         exit(EXIT_FAILURE);
     956             :                                         /* LCOV_EXCL_STOP */
     957             :                                 }
     958             : 
     959         181 :                                 pathcpy(state->parity[level].smartctl, sizeof(state->parity[level].smartctl), custom);
     960             :                         } else {
     961             :                                 struct snapraid_disk* disk;
     962             : 
     963             :                                 /* search the disk */
     964         181 :                                 disk = 0;
     965         181 :                                 for (i = state->disklist; i != 0; i = i->next) {
     966         181 :                                         disk = i->data;
     967         181 :                                         if (strcmp(disk->name, buffer) == 0)
     968         181 :                                                 break;
     969             :                                 }
     970         181 :                                 if (!disk) {
     971             :                                         /* LCOV_EXCL_START */
     972             :                                         log_fatal("Missing disk smartctl '%s' at line %u\n", buffer, line);
     973             :                                         exit(EXIT_FAILURE);
     974             :                                         /* LCOV_EXCL_STOP */
     975             :                                 }
     976         181 :                                 if (disk->smartctl[0] != 0) {
     977             :                                         /* LCOV_EXCL_START */
     978             :                                         log_fatal("Duplicate disk name '%s' at line %u\n", buffer, line);
     979             :                                         exit(EXIT_FAILURE);
     980             :                                         /* LCOV_EXCL_STOP */
     981             :                                 }
     982             : 
     983         181 :                                 pathcpy(disk->smartctl, sizeof(disk->smartctl), custom);
     984             :                         }
     985        1093 :                 } else if (strcmp(tag, "nohidden") == 0) {
     986           0 :                         state->filter_hidden = 1;
     987        1093 :                 } else if (strcmp(tag, "exclude") == 0) {
     988             :                         struct snapraid_filter* filter;
     989             : 
     990         256 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
     991         256 :                         if (ret < 0) {
     992             :                                 /* LCOV_EXCL_START */
     993             :                                 log_fatal("Invalid 'exclude' specification in '%s' at line %u\n", path, line);
     994             :                                 exit(EXIT_FAILURE);
     995             :                                 /* LCOV_EXCL_STOP */
     996             :                         }
     997             : 
     998         256 :                         if (!*buffer) {
     999             :                                 /* LCOV_EXCL_START */
    1000             :                                 log_fatal("Empty 'exclude' specification in '%s' at line %u\n", path, line);
    1001             :                                 exit(EXIT_FAILURE);
    1002             :                                 /* LCOV_EXCL_STOP */
    1003             :                         }
    1004             : 
    1005         256 :                         filter = filter_alloc_file(-1, buffer);
    1006         256 :                         if (!filter) {
    1007             :                                 /* LCOV_EXCL_START */
    1008             :                                 log_fatal("Invalid 'exclude' specification '%s' in '%s' at line %u\n", buffer, path, line);
    1009             :                                 log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
    1010             :                                 exit(EXIT_FAILURE);
    1011             :                                 /* LCOV_EXCL_STOP */
    1012             :                         }
    1013         256 :                         tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
    1014         837 :                 } else if (strcmp(tag, "include") == 0) {
    1015             :                         struct snapraid_filter* filter;
    1016             : 
    1017         256 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
    1018         256 :                         if (ret < 0) {
    1019             :                                 /* LCOV_EXCL_START */
    1020             :                                 log_fatal("Invalid 'include' specification in '%s' at line %u\n", path, line);
    1021             :                                 exit(EXIT_FAILURE);
    1022             :                                 /* LCOV_EXCL_STOP */
    1023             :                         }
    1024             : 
    1025         256 :                         if (!*buffer) {
    1026             :                                 /* LCOV_EXCL_START */
    1027             :                                 log_fatal("Empty 'include' specification in '%s' at line %u\n", path, line);
    1028             :                                 exit(EXIT_FAILURE);
    1029             :                                 /* LCOV_EXCL_STOP */
    1030             :                         }
    1031             : 
    1032         256 :                         filter = filter_alloc_file(1, buffer);
    1033         256 :                         if (!filter) {
    1034             :                                 /* LCOV_EXCL_START */
    1035             :                                 log_fatal("Invalid 'include' specification '%s' in '%s' at line %u\n", buffer, path, line);
    1036             :                                 log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
    1037             :                                 exit(EXIT_FAILURE);
    1038             :                                 /* LCOV_EXCL_STOP */
    1039             :                         }
    1040         256 :                         tommy_list_insert_tail(&state->filterlist, &filter->node, filter);
    1041         581 :                 } else if (strcmp(tag, "autosave") == 0) {
    1042             :                         char* e;
    1043             : 
    1044          34 :                         ret = sgetlasttok(f, buffer, sizeof(buffer));
    1045          34 :                         if (ret < 0) {
    1046             :                                 /* LCOV_EXCL_START */
    1047             :                                 log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
    1048             :                                 exit(EXIT_FAILURE);
    1049             :                                 /* LCOV_EXCL_STOP */
    1050             :                         }
    1051             : 
    1052          34 :                         if (!*buffer) {
    1053             :                                 /* LCOV_EXCL_START */
    1054             :                                 log_fatal("Empty 'autosave' specification in '%s' at line %u\n", path, line);
    1055             :                                 exit(EXIT_FAILURE);
    1056             :                                 /* LCOV_EXCL_STOP */
    1057             :                         }
    1058             : 
    1059          34 :                         state->autosave = strtoul(buffer, &e, 0);
    1060             : 
    1061          34 :                         if (!e || *e) {
    1062             :                                 /* LCOV_EXCL_START */
    1063             :                                 log_fatal("Invalid 'autosave' specification in '%s' at line %u\n", path, line);
    1064             :                                 exit(EXIT_FAILURE);
    1065             :                                 /* LCOV_EXCL_STOP */
    1066             :                         }
    1067             : 
    1068             :                         /* convert to GB */
    1069          34 :                         state->autosave *= GIGA;
    1070         547 :                 } else if (tag[0] == 0) {
    1071             :                         /* allow empty lines */
    1072          34 :                 } else if (tag[0] == '#') {
    1073          34 :                         ret = sgetline(f, buffer, sizeof(buffer));
    1074          34 :                         if (ret < 0) {
    1075             :                                 /* LCOV_EXCL_START */
    1076             :                                 log_fatal("Invalid comment in '%s' at line %u\n", path, line);
    1077             :                                 exit(EXIT_FAILURE);
    1078             :                                 /* LCOV_EXCL_STOP */
    1079             :                         }
    1080             :                 } else {
    1081             :                         /* LCOV_EXCL_START */
    1082             :                         log_fatal("Invalid command '%s' in '%s' at line %u\n", tag, path, line);
    1083             :                         exit(EXIT_FAILURE);
    1084             :                         /* LCOV_EXCL_STOP */
    1085             :                 }
    1086             : 
    1087             :                 /* skip final spaces */
    1088        6066 :                 sgetspace(f);
    1089             : 
    1090             :                 /* next line */
    1091        6066 :                 c = sgeteol(f);
    1092        6066 :                 if (c == EOF) {
    1093         256 :                         break;
    1094             :                 }
    1095        5810 :                 if (c != '\n') {
    1096             :                         /* LCOV_EXCL_START */
    1097             :                         log_fatal("Extra data in '%s' at line %u\n", path, line);
    1098             :                         exit(EXIT_FAILURE);
    1099             :                         /* LCOV_EXCL_STOP */
    1100             :                 }
    1101        5810 :                 ++line;
    1102        5810 :         }
    1103             : 
    1104         256 :         if (serror(f)) {
    1105             :                 /* LCOV_EXCL_START */
    1106             :                 log_fatal("Error reading the configuration file '%s' at line %u\n", path, line);
    1107             :                 exit(EXIT_FAILURE);
    1108             :                 /* LCOV_EXCL_STOP */
    1109             :         }
    1110             : 
    1111         256 :         sclose(f);
    1112             : 
    1113         256 :         state_config_check(state, path, filterlist_disk);
    1114             : 
    1115             :         /* select the default hash */
    1116         256 :         if (state->opt.force_murmur3) {
    1117           1 :                 state->besthash = HASH_MURMUR3;
    1118         255 :         } else if (state->opt.force_spooky2) {
    1119           1 :                 state->besthash = HASH_SPOOKY2;
    1120             :         } else {
    1121             : #ifdef CONFIG_X86
    1122         254 :                 if (sizeof(void*) == 4 && !raid_cpu_has_slowmult())
    1123         254 :                         state->besthash = HASH_MURMUR3;
    1124             :                 else
    1125           0 :                         state->besthash = HASH_SPOOKY2;
    1126             : #else
    1127             :                 if (sizeof(void*) == 4)
    1128             :                         state->besthash = HASH_MURMUR3;
    1129             :                 else
    1130             :                         state->besthash = HASH_SPOOKY2;
    1131             : #endif
    1132             :         }
    1133             : 
    1134             :         /* by default use the best hash */
    1135         256 :         state->hash = state->besthash;
    1136             : 
    1137             :         /* by default use a random hash seed */
    1138         256 :         if (randomize(state->hashseed, HASH_SIZE) != 0) {
    1139             :                 /* LCOV_EXCL_START */
    1140             :                 log_fatal("Failed to get random values.\n");
    1141             :                 exit(EXIT_FAILURE);
    1142             :                 /* LCOV_EXCL_STOP */
    1143             :         }
    1144             : 
    1145             :         /* no previous hash by default */
    1146         256 :         state->prevhash = HASH_UNDEFINED;
    1147             : 
    1148             :         /* intentionally not set the prevhashseed, if used valgrind will warn about it */
    1149             : 
    1150         256 :         log_tag("blocksize:%u\n", state->block_size);
    1151        1787 :         for (i = state->disklist; i != 0; i = i->next) {
    1152        1531 :                 struct snapraid_disk* disk = i->data;
    1153        1531 :                 log_tag("disk:%s:%s\n", disk->name, disk->dir);
    1154             :         }
    1155             : 
    1156         256 :         log_tag("mode:%s\n", lev_raid_name(state->raid_mode, state->level));
    1157        1503 :         for (l = 0; l < state->level; ++l)
    1158        1247 :                 log_tag("%s:%s\n", lev_config_name(l), state->parity[l].path);
    1159         256 :         if (state->pool[0] != 0)
    1160          34 :                 log_tag("pool:%s\n", state->pool);
    1161         256 :         if (state->share[0] != 0)
    1162          34 :                 log_tag("share:%s\n", state->share);
    1163         256 :         if (state->autosave != 0)
    1164          34 :                 log_tag("autosave:%" PRIu64 "\n", state->autosave);
    1165         768 :         for (i = tommy_list_head(&state->filterlist); i != 0; i = i->next) {
    1166             :                 char out[PATH_MAX];
    1167         512 :                 struct snapraid_filter* filter = i->data;
    1168         512 :                 log_tag("filter:%s\n", filter_type(filter, out, sizeof(out)));
    1169             :         }
    1170         256 :         if (state->filter_hidden)
    1171           0 :                 log_tag("filter:nohidden:\n");
    1172         256 :         log_flush();
    1173         256 : }
    1174             : 
    1175             : /**
    1176             :  * Find a disk by name.
    1177             :  */
    1178        6817 : static struct snapraid_disk* find_disk_by_name(struct snapraid_state* state, const char* name)
    1179             : {
    1180             :         tommy_node* i;
    1181             : 
    1182       23789 :         for (i = state->disklist; i != 0; i = i->next) {
    1183       23781 :                 struct snapraid_disk* disk = i->data;
    1184       23781 :                 if (strcmp(disk->name, name) == 0)
    1185        6809 :                         return disk;
    1186             :         }
    1187             : 
    1188           8 :         if (state->no_conf) {
    1189             :                 /* without a configuration file, add disks automatically */
    1190             :                 struct snapraid_disk* disk;
    1191             : 
    1192           6 :                 disk = disk_alloc(name, "DUMMY/", -1, "", 0);
    1193             : 
    1194           6 :                 tommy_list_insert_tail(&state->disklist, &disk->node, disk);
    1195             : 
    1196           6 :                 return disk;
    1197             :         }
    1198             : 
    1199           2 :         return 0;
    1200             : }
    1201             : 
    1202             : /**
    1203             :  * Find a disk by UUID.
    1204             :  */
    1205           2 : static struct snapraid_disk* find_disk_by_uuid(struct snapraid_state* state, const char* uuid)
    1206             : {
    1207             :         tommy_node* i;
    1208           2 :         struct snapraid_disk* found = 0;
    1209             : 
    1210             :         /* special test case to find the first matching UUID */
    1211             :         /* when testing UUID are all equal or not supported */
    1212             :         /* and we should handle this case specifically */
    1213           2 :         if (state->opt.match_first_uuid)
    1214           2 :                 return state->disklist->data;
    1215             : 
    1216             :         /* LCOV_EXCL_START */
    1217             :         /* never find an empty uuid */
    1218             :         if (!*uuid)
    1219             :                 return 0;
    1220             : 
    1221             :         for (i = state->disklist; i != 0; i = i->next) {
    1222             :                 struct snapraid_disk* disk = i->data;
    1223             :                 if (strcmp(disk->uuid, uuid) == 0) {
    1224             :                         /* never match duplicate UUID */
    1225             :                         if (found)
    1226             :                                 return 0;
    1227             :                         found = disk;
    1228             :                 }
    1229             :         }
    1230             : 
    1231             :         return found;
    1232             :         /* LCOV_EXCL_STOP */
    1233             : }
    1234             : 
    1235             : /**
    1236             :  * Update the disk mapping if required.
    1237             :  */
    1238         251 : static void state_map(struct snapraid_state* state)
    1239             : {
    1240             :         unsigned hole;
    1241             :         tommy_node* i;
    1242             :         unsigned uuid_mismatch;
    1243             :         unsigned diskcount;
    1244             :         unsigned l;
    1245             : 
    1246             :         /* remove all the mapping without a disk */
    1247             :         /* this happens when a disk is removed from the configuration file */
    1248             :         /* From SnapRAID 4.0 mappings are automatically removed if a disk is not used */
    1249             :         /* when saving the content file, but we keep this code to import older content files. */
    1250        1847 :         for (i = state->maplist; i != 0; ) {
    1251        1345 :                 struct snapraid_map* map = i->data;
    1252             :                 struct snapraid_disk* disk;
    1253             : 
    1254        1345 :                 disk = find_disk_by_name(state, map->name);
    1255             : 
    1256             :                 /* go to the next mapping before removing */
    1257        1345 :                 i = i->next;
    1258             : 
    1259        1345 :                 if (disk == 0) {
    1260             :                         /* disk not found, remove the mapping */
    1261           0 :                         tommy_list_remove_existing(&state->maplist, &map->node);
    1262           0 :                         map_free(map);
    1263             :                 }
    1264             :         }
    1265             : 
    1266             :         /* maps each unmapped disk present in the configuration file in the first available hole */
    1267             :         /* this happens when you add disks for the first time in the configuration file */
    1268         251 :         hole = 0; /* first position to try */
    1269        1752 :         for (i = state->disklist; i != 0; i = i->next) {
    1270        1501 :                 struct snapraid_disk* disk = i->data;
    1271             :                 struct snapraid_map* map;
    1272             :                 tommy_node* j;
    1273             : 
    1274             :                 /* check if the disk is already mapped */
    1275        5241 :                 for (j = state->maplist; j != 0; j = j->next) {
    1276        5085 :                         map = j->data;
    1277        5085 :                         if (strcmp(disk->name, map->name) == 0) {
    1278             :                                 /* mapping found */
    1279        1345 :                                 break;
    1280             :                         }
    1281             :                 }
    1282        1501 :                 if (j != 0) {
    1283             :                         /* mapping is present, then copy the free blocks into to disk */
    1284        1345 :                         disk->total_blocks = map->total_blocks;
    1285        1345 :                         disk->free_blocks = map->free_blocks;
    1286        1345 :                         continue;
    1287             :                 }
    1288             : 
    1289             :                 /* mapping not found, search for an hole */
    1290             :                 while (1) {
    1291        1060 :                         for (j = state->maplist; j != 0; j = j->next) {
    1292         904 :                                 map = j->data;
    1293         904 :                                 if (map->position == hole) {
    1294             :                                         /* position already used */
    1295         150 :                                         break;
    1296             :                                 }
    1297             :                         }
    1298         306 :                         if (j == 0) {
    1299             :                                 /* hole found */
    1300         156 :                                 break;
    1301             :                         }
    1302             : 
    1303             :                         /* try with the next one */
    1304         150 :                         ++hole;
    1305         150 :                 }
    1306             : 
    1307             :                 /* insert the new mapping */
    1308         156 :                 map = map_alloc(disk->name, hole, 0, 0, "");
    1309             : 
    1310         156 :                 tommy_list_insert_tail(&state->maplist, &map->node, map);
    1311             :         }
    1312             : 
    1313             :         /* without configuration don't check for number of data disks or uuid changes */
    1314         251 :         if (state->no_conf)
    1315         252 :                 return;
    1316             : 
    1317             :         /* counter for the number of UUID mismatches */
    1318         250 :         uuid_mismatch = 0;
    1319             : 
    1320             :         /* check if mapping match the disk uuid */
    1321         250 :         if (!state->opt.skip_disk_access) {
    1322        1584 :                 for (i = state->maplist; i != 0; i = i->next) {
    1323        1357 :                         struct snapraid_map* map = i->data;
    1324             :                         struct snapraid_disk* disk;
    1325             : 
    1326        1357 :                         disk = find_disk_by_name(state, map->name);
    1327        1357 :                         if (disk == 0) {
    1328             :                                 /* LCOV_EXCL_START */
    1329             :                                 log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
    1330             :                                 os_abort();
    1331             :                                 /* LCOV_EXCL_STOP */
    1332             :                         }
    1333             : 
    1334        1357 :                         if (disk->has_unsupported_uuid) {
    1335             :                                 /* if uuid is not available, skip this one */
    1336           1 :                                 continue;
    1337             :                         }
    1338             : 
    1339             :                         /* if the uuid is changed */
    1340        1356 :                         if (strcmp(disk->uuid, map->uuid) != 0) {
    1341             :                                 /* mark the disk as with an UUID change */
    1342         112 :                                 disk->has_different_uuid = 1;
    1343             : 
    1344             :                                 /* if the previous uuid is available */
    1345         112 :                                 if (map->uuid[0] != 0) {
    1346             :                                         /* count the number of uuid change */
    1347           4 :                                         ++uuid_mismatch;
    1348           4 :                                         log_fatal("UUID change for disk '%s' from '%s' to '%s'\n", disk->name, map->uuid, disk->uuid);
    1349             :                                 } else {
    1350             :                                         /* no message here, because having a disk without */
    1351             :                                         /* UUID is the normal state of an empty disk */
    1352         108 :                                         disk->had_empty_uuid = 1;
    1353             :                                 }
    1354             : 
    1355             :                                 /* update the uuid in the mapping, */
    1356         112 :                                 pathcpy(map->uuid, sizeof(map->uuid), disk->uuid);
    1357             : 
    1358             :                                 /* write the new state with the new uuid */
    1359         112 :                                 state->need_write = 1;
    1360             :                         }
    1361             :                 }
    1362             :         }
    1363             : 
    1364             :         /* check the parity uuid */
    1365         250 :         if (!state->opt.skip_parity_access) {
    1366        1258 :                 for (l = 0; l < state->level; ++l) {
    1367             :                         char uuid[UUID_MAX];
    1368             :                         int ret;
    1369             : 
    1370        1042 :                         ret = devuuid(state->parity[l].device, uuid, sizeof(uuid));
    1371        1042 :                         if (ret != 0) {
    1372             :                                 /* uuid not available, just ignore */
    1373           1 :                                 continue;
    1374             :                         }
    1375             : 
    1376             :                         /* if the uuid is changed */
    1377        1041 :                         if (strcmp(uuid, state->parity[l].uuid) != 0) {
    1378             :                                 /* if the previous uuid is available */
    1379          46 :                                 if (state->parity[l].uuid[0] != 0) {
    1380             :                                         /* count the number of uuid change */
    1381           0 :                                         ++uuid_mismatch;
    1382           0 :                                         log_fatal("UUID change for parity '%s' from '%s' to '%s'\n", lev_config_name(l), state->parity[l].uuid, uuid);
    1383             :                                 }
    1384             : 
    1385             :                                 /* update the uuid */
    1386          46 :                                 pathcpy(state->parity[l].uuid, sizeof(state->parity[l].uuid), uuid);
    1387             : 
    1388             :                                 /* write the new state with the new uuid */
    1389          46 :                                 state->need_write = 1;
    1390             :                         }
    1391             :                 }
    1392             :         }
    1393             : 
    1394         250 :         if (!state->opt.force_uuid && uuid_mismatch > state->level) {
    1395             :                 /* LCOV_EXCL_START */
    1396             :                 log_fatal("Too many disks have UUID changed from the latest 'sync'.\n");
    1397             :                 log_fatal("If this happens because you really replaced them,\n");
    1398             :                 log_fatal("you can '%s' anyway, using 'snapraid --force-uuid %s'.\n", state->command, state->command);
    1399             :                 log_fatal("Instead, it's possible that you messed up the disk mount points,\n");
    1400             :                 log_fatal("and you have to restore the mount points at the state of the latest sync.\n");
    1401             :                 exit(EXIT_FAILURE);
    1402             :                 /* LCOV_EXCL_STOP */
    1403             :         }
    1404             : 
    1405             :         /* count the number of data disks, including holes left after removing some */
    1406         250 :         diskcount = 0;
    1407        1745 :         for (i = state->maplist; i != 0; i = i->next) {
    1408        1495 :                 struct snapraid_map* map = i->data;
    1409             : 
    1410        1495 :                 if (map->position + 1 > diskcount)
    1411        1323 :                         diskcount = map->position + 1;
    1412             :         }
    1413             : 
    1414             :         /* ensure to don't go over the limit of the RAID engine */
    1415         250 :         if (diskcount > RAID_DATA_MAX) {
    1416             :                 /* LCOV_EXCL_START */
    1417             :                 log_fatal("Too many data disks. No more than %u.\n", RAID_DATA_MAX);
    1418             :                 exit(EXIT_FAILURE);
    1419             :                 /* LCOV_EXCL_STOP */
    1420             :         }
    1421             : 
    1422             :         /* now count the real number of data disks, excluding holes left after removing some */
    1423         250 :         diskcount = tommy_list_count(&state->maplist);
    1424             : 
    1425             :         /* recommend number of parities */
    1426         250 :         if (!state->opt.no_warnings) {
    1427         250 :                 if (diskcount >= 36 && state->level < 6) {
    1428           0 :                         log_fatal("WARNING! With %u disks it's recommended to use six parity levels.\n", diskcount);
    1429         250 :                 } else if (diskcount >= 29 && state->level < 5) {
    1430           0 :                         log_fatal("WARNING! With %u disks it's recommended to use five parity levels.\n", diskcount);
    1431         250 :                 } else if (diskcount >= 22 && state->level < 4) {
    1432           0 :                         log_fatal("WARNING! With %u disks it's recommended to use four parity levels.\n", diskcount);
    1433         250 :                 } else if (diskcount >= 15 && state->level < 3) {
    1434           0 :                         log_fatal("WARNING! With %u disks it's recommended to use three parity levels.\n", diskcount);
    1435         250 :                 } else if (diskcount >= 5 && state->level < 2) {
    1436          34 :                         log_fatal("WARNING! With %u disks it's recommended to use two parity levels.\n", diskcount);
    1437             :                 }
    1438             :         }
    1439             : }
    1440             : 
    1441         164 : void state_refresh(struct snapraid_state* state)
    1442             : {
    1443             :         tommy_node* i;
    1444             :         unsigned l;
    1445             : 
    1446             :         /* for all disks */
    1447        1146 :         for (i = state->maplist; i != 0; i = i->next) {
    1448         982 :                 struct snapraid_map* map = i->data;
    1449             :                 struct snapraid_disk* disk;
    1450             :                 uint64_t total_space;
    1451             :                 uint64_t free_space;
    1452             :                 int ret;
    1453             : 
    1454         982 :                 disk = find_disk_by_name(state, map->name);
    1455         982 :                 if (disk == 0) {
    1456             :                         /* LCOV_EXCL_START */
    1457             :                         log_fatal("Internal inconsistency for mapping '%s'\n", map->name);
    1458             :                         os_abort();
    1459             :                         /* LCOV_EXCL_STOP */
    1460             :                 }
    1461             : 
    1462         982 :                 ret = fsinfo(disk->dir, 0, &total_space, &free_space);
    1463         982 :                 if (ret != 0) {
    1464             :                         /* LCOV_EXCL_START */
    1465             :                         log_fatal("Error accessing disk '%s' to get file-system info. %s.\n", disk->dir, strerror(errno));
    1466             :                         exit(EXIT_FAILURE);
    1467             :                         /* LCOV_EXCL_STOP */
    1468             :                 }
    1469             : 
    1470             :                 /* set the new free blocks */
    1471         982 :                 map->total_blocks = total_space / state->block_size;
    1472         982 :                 map->free_blocks = free_space / state->block_size;
    1473             : 
    1474             :                 /* also update the disk info */
    1475         982 :                 disk->total_blocks = map->total_blocks;
    1476         982 :                 disk->free_blocks = map->free_blocks;
    1477             :         }
    1478             : 
    1479             :         /* for all parities */
    1480        1118 :         for (l = 0; l < state->level; ++l) {
    1481             :                 uint64_t total_space;
    1482             :                 uint64_t free_space;
    1483             :                 int ret;
    1484             : 
    1485         954 :                 ret = fsinfo(state->parity[l].path, 0, &total_space, &free_space);
    1486         954 :                 if (ret != 0) {
    1487             :                         /* LCOV_EXCL_START */
    1488             :                         log_fatal("Error accessing file '%s' to get file-system info. %s.\n", state->parity[l].path, strerror(errno));
    1489             :                         exit(EXIT_FAILURE);
    1490             :                         /* LCOV_EXCL_STOP */
    1491             :                 }
    1492             : 
    1493             :                 /* set the new free blocks */
    1494         954 :                 state->parity[l].total_blocks = total_space / state->block_size;
    1495         954 :                 state->parity[l].free_blocks = free_space / state->block_size;
    1496             :         }
    1497             : 
    1498             :         /* note what we don't set need_write = 1, because we don't want */
    1499             :         /* to update the content file only for the free space info. */
    1500         164 : }
    1501             : 
    1502             : /**
    1503             :  * Check the content.
    1504             :  */
    1505         244 : static void state_content_check(struct snapraid_state* state, const char* path)
    1506             : {
    1507             :         tommy_node* i;
    1508             : 
    1509             :         /* check that any map has different name and position */
    1510        1703 :         for (i = state->maplist; i != 0; i = i->next) {
    1511        1459 :                 struct snapraid_map* map = i->data;
    1512             :                 tommy_node* j;
    1513        5094 :                 for (j = i->next; j != 0; j = j->next) {
    1514        3635 :                         struct snapraid_map* other = j->data;
    1515        3635 :                         if (strcmp(map->name, other->name) == 0) {
    1516             :                                 /* LCOV_EXCL_START */
    1517             :                                 log_fatal("Colliding 'map' disk specification in '%s'\n", path);
    1518             :                                 exit(EXIT_FAILURE);
    1519             :                                 /* LCOV_EXCL_STOP */
    1520             :                         }
    1521        3635 :                         if (map->position == other->position) {
    1522             :                                 /* LCOV_EXCL_START */
    1523             :                                 log_fatal("Colliding 'map' index specification in '%s'\n", path);
    1524             :                                 exit(EXIT_FAILURE);
    1525             :                                 /* LCOV_EXCL_STOP */
    1526             :                         }
    1527             :                 }
    1528             :         }
    1529         244 : }
    1530             : 
    1531             : /**
    1532             :  * Check if the position is REQUIRED, or we can completely clear it from the state.
    1533             :  *
    1534             :  * Note that position with only DELETED blocks are discarged.
    1535             :  */
    1536      746566 : static int fs_position_is_required(struct snapraid_state* state, block_off_t pos)
    1537             : {
    1538             :         tommy_node* i;
    1539             : 
    1540             :         /* check for each disk */
    1541      792984 :         for (i = state->disklist; i != 0; i = i->next) {
    1542      792984 :                 struct snapraid_disk* disk = i->data;
    1543      792984 :                 struct snapraid_block* block = fs_par2block_get(disk, pos);
    1544             : 
    1545             :                 /* if we have at least one file, the position is needed */
    1546      792984 :                 if (block_has_file(block))
    1547      746566 :                         return 1;
    1548             :         }
    1549             : 
    1550           0 :         return 0;
    1551             : }
    1552             : 
    1553             : /**
    1554             :  * Check if the info block is REQUIREQ.
    1555             :  *
    1556             :  * This is used to ensure that we keep the last check used for scrubbing.
    1557             :  * and that we add it when importing old context files.
    1558             :  *
    1559             :  * Note that you can have position without info blocks, for example
    1560             :  * if all the blocks are not synced.
    1561             :  *
    1562             :  * Note also that not requiring an info block, doesn't mean that if present it
    1563             :  * can be discarded.
    1564             :  */
    1565     1515002 : static int fs_info_is_required(struct snapraid_state* state, block_off_t pos)
    1566             : {
    1567             :         tommy_node* i;
    1568             : 
    1569             :         /* check for each disk */
    1570     2068422 :         for (i = state->disklist; i != 0; i = i->next) {
    1571     2000506 :                 struct snapraid_disk* disk = i->data;
    1572     2000506 :                 struct snapraid_block* block = fs_par2block_get(disk, pos);
    1573             : 
    1574             :                 /* if we have at least one synced file, the info is required */
    1575     2000506 :                 if (block_state_get(block) == BLOCK_STATE_BLK)
    1576     1447086 :                         return 1;
    1577             :         }
    1578             : 
    1579       67916 :         return 0;
    1580             : }
    1581             : 
    1582           0 : static void fs_position_clear_deleted(struct snapraid_state* state, block_off_t pos)
    1583             : {
    1584             :         tommy_node* i;
    1585             : 
    1586             :         /* check for each disk if block is really used */
    1587           0 :         for (i = state->disklist; i != 0; i = i->next) {
    1588           0 :                 struct snapraid_disk* disk = i->data;
    1589           0 :                 struct snapraid_block* block = fs_par2block_get(disk, pos);
    1590             : 
    1591             :                 /* if the block is deleted */
    1592           0 :                 if (block_state_get(block) == BLOCK_STATE_DELETED) {
    1593             :                         /* set it to empty */
    1594           0 :                         fs_deallocate(disk, pos);
    1595             :                 }
    1596             :         }
    1597           0 : }
    1598             : 
    1599             : /**
    1600             :  * Check if a block position in a disk is deleted.
    1601             :  */
    1602     4472427 : static int fs_is_block_deleted_ts(struct snapraid_disk* disk, struct snapraid_chunk** fs_last, block_off_t pos)
    1603             : {
    1604     4472427 :         struct snapraid_block* block = fs_par2block_get_ts(disk, fs_last, pos);
    1605             : 
    1606     4472427 :         return block_state_get(block) == BLOCK_STATE_DELETED;
    1607             : }
    1608             : 
    1609             : /**
    1610             :  * Flush the file checking the final CRC.
    1611             :  * We exploit the fact that the CRC is always stored in the last 4 bytes.
    1612             :  */
    1613           1 : static void decoding_error(const char* path, STREAM* f)
    1614             : {
    1615             :         unsigned char buf[4];
    1616             :         uint32_t crc_stored;
    1617             :         uint32_t crc_computed;
    1618             : 
    1619           1 :         if (seof(f)) {
    1620             :                 /* LCOV_EXCL_START */
    1621             :                 log_fatal("Unexpected end of content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    1622             :                 log_fatal("This content file is truncated. Use an alternate copy.\n");
    1623             :                 exit(EXIT_FAILURE);
    1624             :                 /* LCOV_EXCL_STOP */
    1625             :         }
    1626             : 
    1627           1 :         if (serror(f)) {
    1628             :                 /* LCOV_EXCL_START */
    1629             :                 log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    1630             :                 exit(EXIT_FAILURE);
    1631             :                 /* LCOV_EXCL_STOP */
    1632             :         }
    1633             : 
    1634           1 :         log_fatal("Decoding error in '%s' at offset %" PRIi64 "\n", path, stell(f));
    1635             : 
    1636           1 :         if (sdeplete(f, buf) != 0) {
    1637             :                 /* LCOV_EXCL_START */
    1638             :                 log_fatal("Error flushing the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    1639             :                 exit(EXIT_FAILURE);
    1640             :                 /* LCOV_EXCL_STOP */
    1641             :         }
    1642             : 
    1643             :         /* get the stored crc from the last four bytes */
    1644           1 :         crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
    1645             : 
    1646             :         /* get the computed crc */
    1647           1 :         crc_computed = scrc(f);
    1648             : 
    1649             :         /* adjust the stored crc to include itself */
    1650           1 :         crc_stored = crc32c(crc_stored, buf, 4);
    1651             : 
    1652           1 :         if (crc_computed != crc_stored) {
    1653           1 :                 log_fatal("Mismatching CRC in '%s'\n", path);
    1654           1 :                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    1655           1 :                 exit(EXIT_FAILURE);
    1656             :         } else {
    1657           0 :                 log_fatal("The file CRC is correct!\n");
    1658             :         }
    1659           0 : }
    1660             : 
    1661         245 : static void state_read_content(struct snapraid_state* state, const char* path, STREAM* f)
    1662             : {
    1663             :         block_off_t blockmax;
    1664             :         unsigned count_file;
    1665             :         unsigned count_hardlink;
    1666             :         unsigned count_symlink;
    1667             :         unsigned count_dir;
    1668             :         int crc_checked;
    1669             :         char buffer[PATH_MAX];
    1670             :         int ret;
    1671             :         tommy_array disk_mapping;
    1672             :         uint32_t mapping_max;
    1673             : 
    1674         245 :         blockmax = 0;
    1675         245 :         count_file = 0;
    1676         245 :         count_hardlink = 0;
    1677         245 :         count_symlink = 0;
    1678         245 :         count_dir = 0;
    1679         245 :         crc_checked = 0;
    1680         245 :         mapping_max = 0;
    1681         245 :         tommy_array_init(&disk_mapping);
    1682             : 
    1683         245 :         ret = sread(f, buffer, 12);
    1684         245 :         if (ret < 0) {
    1685             :                 /* LCOV_EXCL_START */
    1686             :                 decoding_error(path, f);
    1687             :                 log_fatal("Invalid header!\n");
    1688             :                 os_abort();
    1689             :                 /* LCOV_EXCL_STOP */
    1690             :         }
    1691             : 
    1692             :         /*
    1693             :          * File format versions:
    1694             :          *  - SNAPCNT1/SnapRAID 4.0 First version.
    1695             :          *  - SNAPCNT2/SnapRAID 7.0 Adds entries 'M' and 'P', to add free_blocks support.
    1696             :          *    The previous 'm' entry is now deprecated, but supported for importing.
    1697             :          *    Similarly for text file, we add 'mapping' and 'parity' deprecating 'map'.
    1698             :          */
    1699         245 :         if (memcmp(buffer, "SNAPCNT1\n\3\0\0", 12) != 0
    1700         245 :                 && memcmp(buffer, "SNAPCNT2\n\3\0\0", 12) != 0) {
    1701             :                 /* LCOV_EXCL_START */
    1702             :                 if (memcmp(buffer, "SNAPCNT", 7) != 0) {
    1703             :                         decoding_error(path, f);
    1704             :                         log_fatal("Invalid header!\n");
    1705             :                         os_abort();
    1706             :                 } else {
    1707             :                         log_fatal("The content file '%s' was generated with a newer version of SnapRAID!\n", path);
    1708             :                         exit(EXIT_FAILURE);
    1709             :                 }
    1710             :                 /* LCOV_EXCL_STOP */
    1711             :         }
    1712             : 
    1713             :         while (1) {
    1714             :                 int c;
    1715             : 
    1716             :                 /* read the command */
    1717     3653695 :                 c = sgetc(f);
    1718     3653695 :                 if (c == EOF) {
    1719         244 :                         break;
    1720             :                 }
    1721             : 
    1722     3653451 :                 if (c == 'f') {
    1723             :                         /* file */
    1724             :                         char sub[PATH_MAX];
    1725             :                         uint64_t v_size;
    1726             :                         uint64_t v_mtime_sec;
    1727             :                         uint32_t v_mtime_nsec;
    1728             :                         uint64_t v_inode;
    1729             :                         uint32_t v_idx;
    1730             :                         struct snapraid_file* file;
    1731             :                         struct snapraid_disk* disk;
    1732             :                         uint32_t mapping;
    1733             : 
    1734     3532951 :                         ret = sgetb32(f, &mapping);
    1735     3532951 :                         if (ret < 0 || mapping >= mapping_max) {
    1736             :                                 /* LCOV_EXCL_START */
    1737             :                                 decoding_error(path, f);
    1738             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    1739             :                                 os_abort();
    1740             :                                 /* LCOV_EXCL_STOP */
    1741             :                         }
    1742     3532951 :                         disk = tommy_array_get(&disk_mapping, mapping);
    1743             : 
    1744     3532951 :                         ret = sgetb64(f, &v_size);
    1745     3532951 :                         if (ret < 0) {
    1746             :                                 /* LCOV_EXCL_START */
    1747             :                                 decoding_error(path, f);
    1748             :                                 os_abort();
    1749             :                                 /* LCOV_EXCL_STOP */
    1750             :                         }
    1751             : 
    1752             :                         /* check for impossible file size to avoid to crash for a too big allocation */
    1753     3532951 :                         if (v_size / state->block_size > blockmax) {
    1754             :                                 /* LCOV_EXCL_START */
    1755             :                                 decoding_error(path, f);
    1756             :                                 log_fatal("Internal inconsistency in file size too big!\n");
    1757             :                                 os_abort();
    1758             :                                 /* LCOV_EXCL_STOP */
    1759             :                         }
    1760             : 
    1761     3532951 :                         ret = sgetb64(f, &v_mtime_sec);
    1762     3532951 :                         if (ret < 0) {
    1763             :                                 /* LCOV_EXCL_START */
    1764             :                                 decoding_error(path, f);
    1765             :                                 os_abort();
    1766             :                                 /* LCOV_EXCL_STOP */
    1767             :                         }
    1768             : 
    1769     3532951 :                         ret = sgetb32(f, &v_mtime_nsec);
    1770     3532951 :                         if (ret < 0) {
    1771             :                                 /* LCOV_EXCL_START */
    1772             :                                 decoding_error(path, f);
    1773             :                                 os_abort();
    1774             :                                 /* LCOV_EXCL_STOP */
    1775             :                         }
    1776             : 
    1777             :                         /* STAT_NSEC_INVALID is encoded as 0 */
    1778     3532951 :                         if (v_mtime_nsec == 0)
    1779           0 :                                 v_mtime_nsec = STAT_NSEC_INVALID;
    1780             :                         else
    1781     3532951 :                                 --v_mtime_nsec;
    1782             : 
    1783     3532951 :                         ret = sgetb64(f, &v_inode);
    1784     3532951 :                         if (ret < 0) {
    1785             :                                 /* LCOV_EXCL_START */
    1786             :                                 decoding_error(path, f);
    1787             :                                 os_abort();
    1788             :                                 /* LCOV_EXCL_STOP */
    1789             :                         }
    1790             : 
    1791     3532951 :                         ret = sgetbs(f, sub, sizeof(sub));
    1792     3532951 :                         if (ret < 0) {
    1793             :                                 /* LCOV_EXCL_START */
    1794             :                                 decoding_error(path, f);
    1795             :                                 os_abort();
    1796             :                                 /* LCOV_EXCL_STOP */
    1797             :                         }
    1798     3532951 :                         if (!*sub) {
    1799             :                                 /* LCOV_EXCL_START */
    1800             :                                 decoding_error(path, f);
    1801             :                                 log_fatal("Internal inconsistency for null file!\n");
    1802             :                                 os_abort();
    1803             :                                 /* LCOV_EXCL_STOP */
    1804             :                         }
    1805             : 
    1806             :                         /* allocate the file */
    1807     3532951 :                         file = file_alloc(state->block_size, sub, v_size, v_mtime_sec, v_mtime_nsec, v_inode, 0);
    1808             : 
    1809             :                         /* insert the file in the file containers */
    1810     3532951 :                         tommy_hashdyn_insert(&disk->inodeset, &file->nodeset, file, file_inode_hash(file->inode));
    1811     3532951 :                         tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub));
    1812     3532951 :                         tommy_hashdyn_insert(&disk->stampset, &file->stampset, file, file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec));
    1813     3532951 :                         tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
    1814             : 
    1815             :                         /* read all the blocks */
    1816     3532951 :                         v_idx = 0;
    1817    10619094 :                         while (v_idx < file->blockmax) {
    1818             :                                 block_off_t v_pos;
    1819             :                                 uint32_t v_count;
    1820             : 
    1821             :                                 /* get the "subcommand */
    1822     3553192 :                                 c = sgetc(f);
    1823             : 
    1824     3553192 :                                 ret = sgetb32(f, &v_pos);
    1825     3553192 :                                 if (ret < 0) {
    1826             :                                         /* LCOV_EXCL_START */
    1827             :                                         decoding_error(path, f);
    1828             :                                         os_abort();
    1829             :                                         /* LCOV_EXCL_STOP */
    1830             :                                 }
    1831             : 
    1832     3553192 :                                 ret = sgetb32(f, &v_count);
    1833     3553192 :                                 if (ret < 0) {
    1834             :                                         /* LCOV_EXCL_START */
    1835             :                                         decoding_error(path, f);
    1836             :                                         os_abort();
    1837             :                                         /* LCOV_EXCL_STOP */
    1838             :                                 }
    1839             : 
    1840     3553192 :                                 if (v_idx + v_count > file->blockmax) {
    1841             :                                         /* LCOV_EXCL_START */
    1842             :                                         decoding_error(path, f);
    1843             :                                         log_fatal("Internal inconsistency in block number!\n");
    1844             :                                         os_abort();
    1845             :                                         /* LCOV_EXCL_STOP */
    1846             :                                 }
    1847             : 
    1848     3553192 :                                 if (v_pos + v_count > blockmax) {
    1849             :                                         /* LCOV_EXCL_START */
    1850             :                                         decoding_error(path, f);
    1851             :                                         log_fatal("Internal inconsistency in block size %u/%u!\n", blockmax, v_pos + v_count);
    1852             :                                         os_abort();
    1853             :                                         /* LCOV_EXCL_START */
    1854             :                                 }
    1855             : 
    1856             :                                 /* fill the blocks in the run */
    1857             :                                 while (v_count) {
    1858             :                                         struct snapraid_block* block = fs_file2block_get(file, v_idx);
    1859             : 
    1860             :                                         switch (c) {
    1861             :                                         case 'b' :
    1862             :                                                 block_state_set(block, BLOCK_STATE_BLK);
    1863             :                                                 break;
    1864             :                                         case 'n' :
    1865             :                                                 /* deprecated NEW blocks are converted to CHG ones */
    1866             :                                                 block_state_set(block, BLOCK_STATE_CHG);
    1867             :                                                 break;
    1868             :                                         case 'g' :
    1869             :                                                 block_state_set(block, BLOCK_STATE_CHG);
    1870             :                                                 break;
    1871             :                                         case 'p' :
    1872             :                                                 block_state_set(block, BLOCK_STATE_REP);
    1873             :                                                 break;
    1874             :                                         default :
    1875             :                                                 /* LCOV_EXCL_START */
    1876             :                                                 decoding_error(path, f);
    1877             :                                                 log_fatal("Invalid block type!\n");
    1878             :                                                 os_abort();
    1879             :                                                 /* LCOV_EXCL_STOP */
    1880             :                                         }
    1881             : 
    1882             :                                         /* read the hash only for 'blk/chg/rep', and not for 'new' */
    1883     8701477 :                                         if (c != 'n') {
    1884     8701477 :                                                 ret = sread(f, block->hash, HASH_SIZE);
    1885     8701477 :                                                 if (ret < 0) {
    1886             :                                                         /* LCOV_EXCL_START */
    1887             :                                                         decoding_error(path, f);
    1888             :                                                         os_abort();
    1889             :                                                         /* LCOV_EXCL_STOP */
    1890             :                                                 }
    1891             :                                         } else {
    1892             :                                                 /* set the ZERO hash for deprecated NEW blocks */
    1893           0 :                                                 hash_zero_set(block->hash);
    1894             :                                         }
    1895             : 
    1896             :                                         /* if the block contains a hash of past data */
    1897             :                                         /* and we are clearing such undeterminated hashes */
    1898     8701477 :                                         if (state->clear_past_hash
    1899     2403226 :                                                 && block_has_past_hash(block)
    1900             :                                         ) {
    1901             :                                                 /* set the hash value to INVALID */
    1902       28006 :                                                 hash_invalid_set(block->hash);
    1903             :                                         }
    1904             : 
    1905             :                                         /* if we are disabling the copy optimization */
    1906             :                                         /* we want also to clear any already previously stored information */
    1907             :                                         /* in other sync commands */
    1908             :                                         /* note that this is required only in sync, and we detect */
    1909             :                                         /* this using the clear_past_hash flag */
    1910     8701477 :                                         if (state->clear_past_hash
    1911     2403226 :                                                 && state->opt.force_nocopy
    1912       14195 :                                                 && block_state_get(block) == BLOCK_STATE_REP
    1913             :                                         ) {
    1914             :                                                 /* set the hash value to INVALID */
    1915           1 :                                                 hash_invalid_set(block->hash);
    1916             :                                                 /* convert from REP to CHG block */
    1917           1 :                                                 block_state_set(block, BLOCK_STATE_CHG);
    1918             :                                         }
    1919             : 
    1920             :                                         /* if we want a full sync, marks block as invalid parity */
    1921             :                                         /* note that we do this after the force_nocopy option */
    1922             :                                         /* to avoid to mixup the two things */
    1923     8701477 :                                         if (state->opt.force_full
    1924       82316 :                                                 && block_state_get(block) == BLOCK_STATE_BLK) {
    1925             :                                                 /* convert from BLK to REP */
    1926       82316 :                                                 block_state_set(block, BLOCK_STATE_REP);
    1927             :                                         }
    1928             : 
    1929             :                                         /* set the parity association */
    1930     8701477 :                                         fs_allocate(disk, v_pos, file, v_idx);
    1931             : 
    1932             :                                         /* go to the next block */
    1933     8701477 :                                         ++v_idx;
    1934     8701477 :                                         ++v_pos;
    1935     8701477 :                                         --v_count;
    1936             :                                 }
    1937             :                         }
    1938             : 
    1939             :                         /* stat */
    1940     3532951 :                         ++count_file;
    1941      120500 :                 } else if (c == 'i') {
    1942             :                         /* "inf" command */
    1943             :                         snapraid_info info;
    1944             :                         uint32_t v_pos;
    1945             :                         uint32_t v_oldest;
    1946             : 
    1947         244 :                         ret = sgetb32(f, &v_oldest);
    1948         244 :                         if (ret < 0) {
    1949             :                                 /* LCOV_EXCL_START */
    1950             :                                 decoding_error(path, f);
    1951             :                                 os_abort();
    1952             :                                 /* LCOV_EXCL_STOP */
    1953             :                         }
    1954             : 
    1955         244 :                         v_pos = 0;
    1956       59310 :                         while (v_pos < blockmax) {
    1957             :                                 int bad;
    1958             :                                 int rehash;
    1959             :                                 int justsynced;
    1960             :                                 uint32_t t;
    1961             :                                 uint32_t flag;
    1962             :                                 uint32_t v_count;
    1963             : 
    1964       58822 :                                 ret = sgetb32(f, &v_count);
    1965       58822 :                                 if (ret < 0) {
    1966             :                                         /* LCOV_EXCL_START */
    1967             :                                         decoding_error(path, f);
    1968             :                                         os_abort();
    1969             :                                         /* LCOV_EXCL_STOP */
    1970             :                                 }
    1971             : 
    1972       58822 :                                 if (v_pos + v_count > blockmax) {
    1973             :                                         /* LCOV_EXCL_START */
    1974             :                                         decoding_error(path, f);
    1975             :                                         log_fatal("Internal inconsistency in info size %u/%u!\n", blockmax, v_pos + v_count);
    1976             :                                         os_abort();
    1977             :                                         /* LCOV_EXCL_STOP */
    1978             :                                 }
    1979             : 
    1980       58822 :                                 ret = sgetb32(f, &flag);
    1981       58822 :                                 if (ret < 0) {
    1982             :                                         /* LCOV_EXCL_START */
    1983             :                                         decoding_error(path, f);
    1984             :                                         os_abort();
    1985             :                                         /* LCOV_EXCL_STOP */
    1986             :                                 }
    1987             : 
    1988             :                                 /* if there is an info */
    1989       58822 :                                 if ((flag & 1) != 0) {
    1990             :                                         /* read the time */
    1991       58807 :                                         ret = sgetb32(f, &t);
    1992       58807 :                                         if (ret < 0) {
    1993             :                                                 /* LCOV_EXCL_START */
    1994             :                                                 decoding_error(path, f);
    1995             :                                                 os_abort();
    1996             :                                                 /* LCOV_EXCL_STOP */
    1997             :                                         }
    1998             : 
    1999             :                                         /* analyze the flags */
    2000       58807 :                                         bad = (flag & 2) != 0;
    2001       58807 :                                         rehash = (flag & 4) != 0;
    2002       58807 :                                         justsynced = (flag & 8) != 0;
    2003             : 
    2004       58807 :                                         if (rehash && state->prevhash == HASH_UNDEFINED) {
    2005             :                                                 /* LCOV_EXCL_START */
    2006             :                                                 decoding_error(path, f);
    2007             :                                                 log_fatal("Internal inconsistency for missing previous checksum!\n");
    2008             :                                                 os_abort();
    2009             :                                                 /* LCOV_EXCL_STOP */
    2010             :                                         }
    2011             : 
    2012       58807 :                                         info = info_make(t + v_oldest, bad, rehash, justsynced);
    2013             :                                 } else {
    2014          15 :                                         info = 0;
    2015             :                                 }
    2016             : 
    2017     1632646 :                                 while (v_count) {
    2018             :                                         /* insert the info in the array */
    2019     1515002 :                                         info_set(&state->infoarr, v_pos, info);
    2020             : 
    2021             :                                         /* ensure that an info is present only for used positions */
    2022     1515002 :                                         if (fs_info_is_required(state, v_pos)) {
    2023     1447086 :                                                 if (!info) {
    2024             :                                                         /* LCOV_EXCL_START */
    2025             :                                                         decoding_error(path, f);
    2026             :                                                         log_fatal("Internal inconsistency for missing info!\n");
    2027             :                                                         os_abort();
    2028             :                                                         /* LCOV_EXCL_STOP */
    2029             :                                                 }
    2030             :                                         } else {
    2031             :                                                 /* extra info are accepted for backward compatibility */
    2032             :                                                 /* they are discarded at the first write */
    2033             :                                         }
    2034             : 
    2035             :                                         /* go to next block */
    2036     1515002 :                                         ++v_pos;
    2037     1515002 :                                         --v_count;
    2038             :                                 }
    2039             :                         }
    2040      120256 :                 } else if (c == 'h') {
    2041             :                         /* hole */
    2042             :                         uint32_t v_pos;
    2043             :                         struct snapraid_disk* disk;
    2044             :                         uint32_t mapping;
    2045             : 
    2046        1345 :                         ret = sgetb32(f, &mapping);
    2047        1345 :                         if (ret < 0 || mapping >= mapping_max) {
    2048             :                                 /* LCOV_EXCL_START */
    2049             :                                 decoding_error(path, f);
    2050             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    2051             :                                 os_abort();
    2052             :                                 /* LCOV_EXCL_STOP */
    2053             :                         }
    2054        1345 :                         disk = tommy_array_get(&disk_mapping, mapping);
    2055             : 
    2056        1345 :                         v_pos = 0;
    2057        4113 :                         while (v_pos < blockmax) {
    2058             :                                 uint32_t v_idx;
    2059             :                                 uint32_t v_count;
    2060             :                                 struct snapraid_file* deleted;
    2061             : 
    2062        1423 :                                 ret = sgetb32(f, &v_count);
    2063        1423 :                                 if (ret < 0) {
    2064             :                                         /* LCOV_EXCL_START */
    2065             :                                         decoding_error(path, f);
    2066             :                                         os_abort();
    2067             :                                         /* LCOV_EXCL_STOP */
    2068             :                                 }
    2069             : 
    2070        1423 :                                 if (v_pos + v_count > blockmax) {
    2071             :                                         /* LCOV_EXCL_START */
    2072             :                                         decoding_error(path, f);
    2073             :                                         log_fatal("Internal inconsistency in hole size %u/%u!\n", blockmax, v_pos + v_count);
    2074             :                                         os_abort();
    2075             :                                         /* LCOV_EXCL_STOP */
    2076             :                                 }
    2077             : 
    2078             :                                 /* get the sub-command */
    2079        1423 :                                 c = sgetc(f);
    2080             : 
    2081        1423 :                                 switch (c) {
    2082             :                                 case 'o' :
    2083             :                                         /* if it's a run of deleted blocks */
    2084             : 
    2085             :                                         /* allocate a fake deleted file */
    2086          41 :                                         deleted = file_alloc(state->block_size, "<deleted>", v_count * (data_off_t)state->block_size, 0, 0, 0, 0);
    2087             : 
    2088             :                                         /* mark the file as deleted */
    2089          41 :                                         file_flag_set(deleted, FILE_IS_DELETED);
    2090             : 
    2091             :                                         /* insert it in the list of deleted files */
    2092          41 :                                         tommy_list_insert_tail(&disk->deletedlist, &deleted->nodelist, deleted);
    2093             : 
    2094             :                                         /* process all blocks */
    2095          41 :                                         v_idx = 0;
    2096       27968 :                                         while (v_count) {
    2097       27886 :                                                 struct snapraid_block* block = fs_file2block_get(deleted, v_idx);
    2098             : 
    2099             :                                                 /* set the block as deleted */
    2100       27886 :                                                 block_state_set(block, BLOCK_STATE_DELETED);
    2101             : 
    2102             :                                                 /* read the hash */
    2103       27886 :                                                 ret = sread(f, block->hash, HASH_SIZE);
    2104       27886 :                                                 if (ret < 0) {
    2105             :                                                         /* LCOV_EXCL_START */
    2106             :                                                         decoding_error(path, f);
    2107             :                                                         os_abort();
    2108             :                                                         /* LCOV_EXCL_STOP */
    2109             :                                                 }
    2110             : 
    2111             :                                                 /* if we are clearing indeterminate hashes */
    2112       27886 :                                                 if (state->clear_past_hash) {
    2113             :                                                         /* set the hash value to INVALID */
    2114        9432 :                                                         hash_invalid_set(block->hash);
    2115             :                                                 }
    2116             : 
    2117             :                                                 /* insert the block in the block array */
    2118       27886 :                                                 fs_allocate(disk, v_pos, deleted, v_idx);
    2119             : 
    2120             :                                                 /* go to next block */
    2121       27886 :                                                 ++v_pos;
    2122       27886 :                                                 ++v_idx;
    2123       27886 :                                                 --v_count;
    2124             :                                         }
    2125          41 :                                         break;
    2126             :                                 case 'O' :
    2127             :                                         /* go to the next run */
    2128        1382 :                                         v_pos += v_count;
    2129        1382 :                                         break;
    2130             :                                 default :
    2131             :                                         /* LCOV_EXCL_START */
    2132             :                                         decoding_error(path, f);
    2133             :                                         log_fatal("Invalid hole type!\n");
    2134             :                                         os_abort();
    2135             :                                         /* LCOV_EXCL_STOP */
    2136             :                                 }
    2137             :                         }
    2138      118911 :                 } else if (c == 's') {
    2139             :                         /* symlink */
    2140             :                         char sub[PATH_MAX];
    2141             :                         char linkto[PATH_MAX];
    2142             :                         struct snapraid_link* slink;
    2143             :                         struct snapraid_disk* disk;
    2144             :                         uint32_t mapping;
    2145             : 
    2146      113843 :                         ret = sgetb32(f, &mapping);
    2147      113843 :                         if (ret < 0 || mapping >= mapping_max) {
    2148             :                                 /* LCOV_EXCL_START */
    2149             :                                 decoding_error(path, f);
    2150             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    2151             :                                 os_abort();
    2152             :                                 /* LCOV_EXCL_STOP */
    2153             :                         }
    2154      113843 :                         disk = tommy_array_get(&disk_mapping, mapping);
    2155             : 
    2156      113843 :                         ret = sgetbs(f, sub, sizeof(sub));
    2157      113843 :                         if (ret < 0) {
    2158             :                                 /* LCOV_EXCL_START */
    2159             :                                 decoding_error(path, f);
    2160             :                                 os_abort();
    2161             :                                 /* LCOV_EXCL_STOP */
    2162             :                         }
    2163             : 
    2164      113843 :                         if (!*sub) {
    2165             :                                 /* LCOV_EXCL_START */
    2166             :                                 decoding_error(path, f);
    2167             :                                 log_fatal("Internal inconsistency for null symlink!\n");
    2168             :                                 os_abort();
    2169             :                                 /* LCOV_EXCL_STOP */
    2170             :                         }
    2171             : 
    2172      113843 :                         ret = sgetbs(f, linkto, sizeof(linkto));
    2173      113843 :                         if (ret < 0) {
    2174             :                                 /* LCOV_EXCL_START */
    2175             :                                 decoding_error(path, f);
    2176             :                                 os_abort();
    2177             :                                 /* LCOV_EXCL_STOP */
    2178             :                         }
    2179             : 
    2180             :                         /* allocate the link as symbolic link */
    2181      113843 :                         slink = link_alloc(sub, linkto, FILE_IS_SYMLINK);
    2182             : 
    2183             :                         /* insert the link in the link containers */
    2184      113843 :                         tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
    2185      113843 :                         tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
    2186             : 
    2187             :                         /* stat */
    2188      113843 :                         ++count_symlink;
    2189        5068 :                 } else if (c == 'a') {
    2190             :                         /* hardlink */
    2191             :                         char sub[PATH_MAX];
    2192             :                         char linkto[PATH_MAX];
    2193             :                         struct snapraid_link* slink;
    2194             :                         struct snapraid_disk* disk;
    2195             :                         uint32_t mapping;
    2196             : 
    2197         576 :                         ret = sgetb32(f, &mapping);
    2198         576 :                         if (ret < 0 || mapping >= mapping_max) {
    2199             :                                 /* LCOV_EXCL_START */
    2200             :                                 decoding_error(path, f);
    2201             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    2202             :                                 os_abort();
    2203             :                                 /* LCOV_EXCL_STOP */
    2204             :                         }
    2205         576 :                         disk = tommy_array_get(&disk_mapping, mapping);
    2206             : 
    2207         576 :                         ret = sgetbs(f, sub, sizeof(sub));
    2208         576 :                         if (ret < 0) {
    2209             :                                 /* LCOV_EXCL_START */
    2210             :                                 decoding_error(path, f);
    2211             :                                 os_abort();
    2212             :                                 /* LCOV_EXCL_STOP */
    2213             :                         }
    2214             : 
    2215         576 :                         if (!*sub) {
    2216             :                                 /* LCOV_EXCL_START */
    2217             :                                 decoding_error(path, f);
    2218             :                                 log_fatal("Internal inconsistency for null hardlink!\n");
    2219             :                                 os_abort();
    2220             :                                 /* LCOV_EXCL_STOP */
    2221             :                         }
    2222             : 
    2223         576 :                         ret = sgetbs(f, linkto, sizeof(linkto));
    2224         576 :                         if (ret < 0) {
    2225             :                                 /* LCOV_EXCL_START */
    2226             :                                 decoding_error(path, f);
    2227             :                                 os_abort();
    2228             :                                 /* LCOV_EXCL_STOP */
    2229             :                         }
    2230             : 
    2231         576 :                         if (!*linkto) {
    2232             :                                 /* LCOV_EXCL_START */
    2233             :                                 decoding_error(path, f);
    2234             :                                 log_fatal("Internal inconsistency for empty hardlink '%s'!\n", sub);
    2235             :                                 os_abort();
    2236             :                                 /* LCOV_EXCL_STOP */
    2237             :                         }
    2238             : 
    2239             :                         /* allocate the link as hard link */
    2240         576 :                         slink = link_alloc(sub, linkto, FILE_IS_HARDLINK);
    2241             : 
    2242             :                         /* insert the link in the link containers */
    2243         576 :                         tommy_hashdyn_insert(&disk->linkset, &slink->nodeset, slink, link_name_hash(slink->sub));
    2244         576 :                         tommy_list_insert_tail(&disk->linklist, &slink->nodelist, slink);
    2245             : 
    2246             :                         /* stat */
    2247         576 :                         ++count_hardlink;
    2248        4492 :                 } else if (c == 'r') {
    2249             :                         /* dir */
    2250             :                         char sub[PATH_MAX];
    2251             :                         struct snapraid_dir* dir;
    2252             :                         struct snapraid_disk* disk;
    2253             :                         uint32_t mapping;
    2254             : 
    2255         762 :                         ret = sgetb32(f, &mapping);
    2256         762 :                         if (ret < 0 || mapping >= mapping_max) {
    2257             :                                 /* LCOV_EXCL_START */
    2258             :                                 decoding_error(path, f);
    2259             :                                 log_fatal("Internal inconsistency in mapping index!\n");
    2260             :                                 os_abort();
    2261             :                                 /* LCOV_EXCL_STOP */
    2262             :                         }
    2263         762 :                         disk = tommy_array_get(&disk_mapping, mapping);
    2264             : 
    2265         762 :                         ret = sgetbs(f, sub, sizeof(sub));
    2266         762 :                         if (ret < 0) {
    2267             :                                 /* LCOV_EXCL_START */
    2268             :                                 decoding_error(path, f);
    2269             :                                 os_abort();
    2270             :                                 /* LCOV_EXCL_STOP */
    2271             :                         }
    2272             : 
    2273         762 :                         if (!*sub) {
    2274             :                                 /* LCOV_EXCL_START */
    2275             :                                 decoding_error(path, f);
    2276             :                                 log_fatal("Internal inconsistency for null dir!\n");
    2277             :                                 os_abort();
    2278             :                                 /* LCOV_EXCL_STOP */
    2279             :                         }
    2280             : 
    2281             :                         /* allocate the dir */
    2282         762 :                         dir = dir_alloc(sub);
    2283             : 
    2284             :                         /* insert the dir in the dir containers */
    2285         762 :                         tommy_hashdyn_insert(&disk->dirset, &dir->nodeset, dir, dir_name_hash(dir->sub));
    2286         762 :                         tommy_list_insert_tail(&disk->dirlist, &dir->nodelist, dir);
    2287             : 
    2288             :                         /* stat */
    2289         762 :                         ++count_dir;
    2290        3730 :                 } else if (c == 'c') {
    2291             :                         /* get the subcommand */
    2292         245 :                         c = sgetc(f);
    2293             : 
    2294         245 :                         switch (c) {
    2295             :                         case 'u' :
    2296         157 :                                 state->hash = HASH_MURMUR3;
    2297         157 :                                 break;
    2298             :                         case 'k' :
    2299          88 :                                 state->hash = HASH_SPOOKY2;
    2300          88 :                                 break;
    2301             :                         default :
    2302             :                                 /* LCOV_EXCL_START */
    2303             :                                 decoding_error(path, f);
    2304             :                                 log_fatal("Invalid checksum!\n");
    2305             :                                 os_abort();
    2306             :                                 /* LCOV_EXCL_STOP */
    2307             :                         }
    2308             : 
    2309             :                         /* read the seed */
    2310         245 :                         ret = sread(f, state->hashseed, HASH_SIZE);
    2311         245 :                         if (ret < 0) {
    2312             :                                 /* LCOV_EXCL_START */
    2313             :                                 decoding_error(path, f);
    2314             :                                 os_abort();
    2315             :                                 /* LCOV_EXCL_STOP */
    2316             :                         }
    2317        3485 :                 } else if (c == 'C') {
    2318             :                         /* get the sub-command */
    2319           4 :                         c = sgetc(f);
    2320             : 
    2321           4 :                         switch (c) {
    2322             :                         case 'u' :
    2323           4 :                                 state->prevhash = HASH_MURMUR3;
    2324           4 :                                 break;
    2325             :                         case 'k' :
    2326           0 :                                 state->prevhash = HASH_SPOOKY2;
    2327           0 :                                 break;
    2328             :                         default :
    2329             :                                 /* LCOV_EXCL_START */
    2330             :                                 decoding_error(path, f);
    2331             :                                 log_fatal("Invalid checksum!\n");
    2332             :                                 os_abort();
    2333             :                                 /* LCOV_EXCL_STOP */
    2334             :                         }
    2335             : 
    2336             :                         /* read the seed */
    2337           4 :                         ret = sread(f, state->prevhashseed, HASH_SIZE);
    2338           4 :                         if (ret < 0) {
    2339             :                                 /* LCOV_EXCL_START */
    2340             :                                 decoding_error(path, f);
    2341             :                                 os_abort();
    2342             :                                 /* LCOV_EXCL_STOP */
    2343             :                         }
    2344        3481 :                 } else if (c == 'z') {
    2345             :                         block_off_t blksize;
    2346             : 
    2347         245 :                         ret = sgetb32(f, &blksize);
    2348         245 :                         if (ret < 0) {
    2349             :                                 /* LCOV_EXCL_START */
    2350             :                                 decoding_error(path, f);
    2351             :                                 os_abort();
    2352             :                                 /* LCOV_EXCL_STOP */
    2353             :                         }
    2354             : 
    2355             :                         /* without configuration, auto assign the block size */
    2356         245 :                         if (state->no_conf) {
    2357           1 :                                 state->block_size = blksize;
    2358             :                         }
    2359             : 
    2360         245 :                         if (blksize != state->block_size) {
    2361             :                                 /* LCOV_EXCL_START */
    2362             :                                 decoding_error(path, f);
    2363             :                                 log_fatal("Mismatching 'blksize' and 'blocksize' specification!\n");
    2364             :                                 log_fatal("Please restore the 'blocksize' value in the configuration file to '%u'\n", blksize / 1024);
    2365             :                                 exit(EXIT_FAILURE);
    2366             :                                 /* LCOV_EXCL_STOP */
    2367             :                         }
    2368        3236 :                 } else if (c == 'x') {
    2369         245 :                         ret = sgetb32(f, &blockmax);
    2370         245 :                         if (ret < 0) {
    2371             :                                 /* LCOV_EXCL_START */
    2372             :                                 decoding_error(path, f);
    2373             :                                 os_abort();
    2374             :                                 /* LCOV_EXCL_STOP */
    2375             :                         }
    2376        4342 :                 } else if (c == 'm' || c == 'M') {
    2377             :                         struct snapraid_map* map;
    2378             :                         char uuid[UUID_MAX];
    2379             :                         uint32_t v_pos;
    2380             :                         uint32_t v_total_blocks;
    2381             :                         uint32_t v_free_blocks;
    2382             :                         struct snapraid_disk* disk;
    2383             : 
    2384        1351 :                         ret = sgetbs(f, buffer, sizeof(buffer));
    2385        1351 :                         if (ret < 0) {
    2386             :                                 /* LCOV_EXCL_START */
    2387             :                                 decoding_error(path, f);
    2388             :                                 os_abort();
    2389             :                                 /* LCOV_EXCL_STOP */
    2390             :                         }
    2391             : 
    2392        1351 :                         ret = sgetb32(f, &v_pos);
    2393        1351 :                         if (ret < 0) {
    2394             :                                 /* LCOV_EXCL_START */
    2395             :                                 decoding_error(path, f);
    2396             :                                 os_abort();
    2397             :                                 /* LCOV_EXCL_STOP */
    2398             :                         }
    2399             : 
    2400             :                         /* from SnapRAID 7.0 the 'M' command includes the free space */
    2401        1351 :                         if (c == 'M') {
    2402        1351 :                                 ret = sgetb32(f, &v_total_blocks);
    2403        1351 :                                 if (ret < 0) {
    2404             :                                         /* LCOV_EXCL_START */
    2405             :                                         decoding_error(path, f);
    2406             :                                         os_abort();
    2407             :                                         /* LCOV_EXCL_STOP */
    2408             :                                 }
    2409             : 
    2410        1351 :                                 ret = sgetb32(f, &v_free_blocks);
    2411        1351 :                                 if (ret < 0) {
    2412             :                                         /* LCOV_EXCL_START */
    2413             :                                         decoding_error(path, f);
    2414             :                                         os_abort();
    2415             :                                         /* LCOV_EXCL_STOP */
    2416             :                                 }
    2417             :                         } else {
    2418           0 :                                 v_total_blocks = 0;
    2419           0 :                                 v_free_blocks = 0;
    2420             :                         }
    2421             : 
    2422             :                         /* read the uuid */
    2423        1351 :                         ret = sgetbs(f, uuid, sizeof(uuid));
    2424        1351 :                         if (ret < 0) {
    2425             :                                 /* LCOV_EXCL_START */
    2426             :                                 decoding_error(path, f);
    2427             :                                 os_abort();
    2428             :                                 /* LCOV_EXCL_STOP */
    2429             :                         }
    2430             : 
    2431             :                         /* find the disk */
    2432        1351 :                         disk = find_disk_by_name(state, buffer);
    2433        1351 :                         if (!disk) {
    2434             :                                 /* search by UUID if renamed */
    2435           2 :                                 disk = find_disk_by_uuid(state, uuid);
    2436           2 :                                 if (disk) {
    2437           2 :                                         log_fatal("WARNING! Renaming disk '%s' to '%s'\n", buffer, disk->name);
    2438             : 
    2439             :                                         /* write the new state with the new name */
    2440           2 :                                         state->need_write = 1;
    2441             :                                 }
    2442             :                         }
    2443        1351 :                         if (!disk) {
    2444             :                                 /* LCOV_EXCL_START */
    2445             :                                 decoding_error(path, f);
    2446             :                                 log_fatal("Disk '%s' with uuid '%s' not present in the configuration file!\n", buffer, uuid);
    2447             :                                 log_fatal("If you have removed it from the configuration file, please restore it\n");
    2448             :                                 /* if it's a command without UUID, it cannot autorename using UUID */
    2449             :                                 if (state->opt.skip_disk_access)
    2450             :                                         log_fatal("If you have renamed it, run 'sync' to update the new name\n");
    2451             :                                 exit(EXIT_FAILURE);
    2452             :                                 /* LCOV_EXCL_STOP */
    2453             :                         }
    2454             : 
    2455        1351 :                         map = map_alloc(disk->name, v_pos, v_total_blocks, v_free_blocks, uuid);
    2456             : 
    2457        1351 :                         tommy_list_insert_tail(&state->maplist, &map->node, map);
    2458             : 
    2459             :                         /* insert in the mapping vector */
    2460        1351 :                         tommy_array_grow(&disk_mapping, mapping_max + 1);
    2461        1351 :                         tommy_array_set(&disk_mapping, mapping_max, disk);
    2462        1351 :                         ++mapping_max;
    2463        1640 :                 } else if (c == 'P') {
    2464             :                         /* from SnapRAID 7.0 the 'P' command includes the free space */
    2465             :                         char uuid[UUID_MAX];
    2466             :                         uint32_t v_level;
    2467             :                         uint32_t v_total_blocks;
    2468             :                         uint32_t v_free_blocks;
    2469             : 
    2470        1395 :                         ret = sgetb32(f, &v_level);
    2471        1395 :                         if (ret < 0) {
    2472             :                                 /* LCOV_EXCL_START */
    2473             :                                 decoding_error(path, f);
    2474             :                                 os_abort();
    2475             :                                 /* LCOV_EXCL_STOP */
    2476             :                         }
    2477             : 
    2478        1395 :                         ret = sgetb32(f, &v_total_blocks);
    2479        1395 :                         if (ret < 0) {
    2480             :                                 /* LCOV_EXCL_START */
    2481             :                                 decoding_error(path, f);
    2482             :                                 os_abort();
    2483             :                                 /* LCOV_EXCL_STOP */
    2484             :                         }
    2485             : 
    2486        1395 :                         ret = sgetb32(f, &v_free_blocks);
    2487        1395 :                         if (ret < 0) {
    2488             :                                 /* LCOV_EXCL_START */
    2489             :                                 decoding_error(path, f);
    2490             :                                 os_abort();
    2491             :                                 /* LCOV_EXCL_STOP */
    2492             :                         }
    2493             : 
    2494        1395 :                         ret = sgetbs(f, uuid, sizeof(uuid));
    2495        1395 :                         if (ret < 0) {
    2496             :                                 /* LCOV_EXCL_START */
    2497             :                                 decoding_error(path, f);
    2498             :                                 os_abort();
    2499             :                                 /* LCOV_EXCL_STOP */
    2500             :                         }
    2501             : 
    2502             :                         /* auto configure if configuration is missing */
    2503        1395 :                         if (state->no_conf && v_level < LEV_MAX && v_level >= state->level)
    2504           5 :                                 state->level = v_level + 1;
    2505             : 
    2506             :                         /* if we use this parity entry */
    2507        1395 :                         if (v_level < state->level) {
    2508             :                                 /* set the parity info */
    2509        1141 :                                 pathcpy(state->parity[v_level].uuid, sizeof(state->parity[v_level].uuid), uuid);
    2510        1141 :                                 state->parity[v_level].total_blocks = v_total_blocks;
    2511        1141 :                                 state->parity[v_level].free_blocks = v_free_blocks;
    2512             :                         }
    2513         245 :                 } else if (c == 'N') {
    2514             :                         uint32_t crc_stored;
    2515             :                         uint32_t crc_computed;
    2516             : 
    2517             :                         /* get the crc before reading it from the file */
    2518         244 :                         crc_computed = scrc(f);
    2519             : 
    2520         244 :                         ret = sgetble32(f, &crc_stored);
    2521         244 :                         if (ret < 0) {
    2522             :                                 /* LCOV_EXCL_START */
    2523             :                                 /* here don't call decoding_error() because it's too late to get the crc */
    2524             :                                 log_fatal("Error reading the CRC in '%s' at offset %" PRIi64 "\n", path, stell(f));
    2525             :                                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    2526             :                                 exit(EXIT_FAILURE);
    2527             :                                 /* LCOV_EXCL_STOP */
    2528             :                         }
    2529             : 
    2530         244 :                         if (crc_stored != crc_computed) {
    2531             :                                 /* LCOV_EXCL_START */
    2532             :                                 /* here don't call decoding_error() because it's too late to get the crc */
    2533             :                                 log_fatal("Mismatching CRC in '%s'\n", path);
    2534             :                                 log_fatal("This content file is damaged! Use an alternate copy.\n");
    2535             :                                 exit(EXIT_FAILURE);
    2536             :                                 /* LCOV_EXCL_STOP */
    2537             :                         }
    2538             : 
    2539         244 :                         crc_checked = 1;
    2540             :                 } else {
    2541             :                         /* LCOV_EXCL_START */
    2542             :                         decoding_error(path, f);
    2543             :                         log_fatal("Invalid command '%c'!\n", (char)c);
    2544             :                         os_abort();
    2545             :                         /* LCOV_EXCL_STOP */
    2546             :                 }
    2547     3653450 :         }
    2548             : 
    2549         244 :         tommy_array_done(&disk_mapping);
    2550             : 
    2551         244 :         if (serror(f)) {
    2552             :                 /* LCOV_EXCL_START */
    2553             :                 log_fatal("Error reading the content file '%s' at offset %" PRIi64 "\n", path, stell(f));
    2554             :                 exit(EXIT_FAILURE);
    2555             :                 /* LCOV_EXCL_STOP */
    2556             :         }
    2557             : 
    2558         244 :         if (!crc_checked) {
    2559             :                 /* LCOV_EXCL_START */
    2560             :                 log_fatal("Finished reading '%s' without finding the CRC\n", path);
    2561             :                 log_fatal("This content file is truncated or damaged! Use an alternate copy.\n");
    2562             :                 exit(EXIT_FAILURE);
    2563             :                 /* LCOV_EXCL_STOP */
    2564             :         }
    2565             : 
    2566             :         /* check the file-system on all disks */
    2567         244 :         state_fscheck(state, "after read");
    2568             : 
    2569             :         /* check that the stored parity size matches the loaded state */
    2570         244 :         if (blockmax != parity_allocated_size(state)) {
    2571             :                 /* LCOV_EXCL_START */
    2572             :                 log_fatal("Internal inconsistency in parity size %u/%u in '%s' at offset %" PRIi64 "\n", blockmax, parity_allocated_size(state), path, stell(f));
    2573             :                 if (state->opt.skip_content_check) {
    2574             :                         log_fatal("Overriding.\n");
    2575             :                         blockmax = parity_allocated_size(state);
    2576             :                 } else {
    2577             :                         exit(EXIT_FAILURE);
    2578             :                 }
    2579             :                 /* LCOV_EXCL_STOP */
    2580             :         }
    2581             : 
    2582         244 :         msg_verbose("%8u files\n", count_file);
    2583         244 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    2584         244 :         msg_verbose("%8u symlinks\n", count_symlink);
    2585         244 :         msg_verbose("%8u empty dirs\n", count_dir);
    2586         244 : }
    2587             : 
    2588             : struct state_write_thread_context {
    2589             :         struct snapraid_state* state;
    2590             : #if HAVE_MT_WRITE
    2591             :         pthread_t thread;
    2592             : #endif
    2593             :         /* input */
    2594             :         block_off_t blockmax;
    2595             :         time_t info_oldest;
    2596             :         int info_has_rehash;
    2597             :         STREAM* f;
    2598             :         /* output */
    2599             :         uint32_t crc;
    2600             :         unsigned count_file;
    2601             :         unsigned count_hardlink;
    2602             :         unsigned count_symlink;
    2603             :         unsigned count_dir;
    2604             : };
    2605             : 
    2606         148 : static void* state_write_thread(void* arg)
    2607             : {
    2608         148 :         struct state_write_thread_context* context = arg;
    2609         148 :         struct snapraid_state* state = context->state;
    2610         148 :         block_off_t blockmax = context->blockmax;
    2611         148 :         time_t info_oldest = context->info_oldest;
    2612         148 :         int info_has_rehash = context->info_has_rehash;
    2613         148 :         STREAM* f = context->f;
    2614             :         uint32_t crc;
    2615             :         unsigned count_file;
    2616             :         unsigned count_hardlink;
    2617             :         unsigned count_symlink;
    2618             :         unsigned count_dir;
    2619             :         tommy_node* i;
    2620             :         block_off_t idx;
    2621             :         block_off_t begin;
    2622             :         unsigned l;
    2623             : 
    2624         148 :         count_file = 0;
    2625         148 :         count_hardlink = 0;
    2626         148 :         count_symlink = 0;
    2627         148 :         count_dir = 0;
    2628             : 
    2629             :         /* write header */
    2630         148 :         swrite("SNAPCNT2\n\3\0\0", 12, f);
    2631             : 
    2632             :         /* write block size and block max */
    2633         148 :         sputc('z', f);
    2634         148 :         sputb32(state->block_size, f);
    2635         148 :         sputc('x', f);
    2636         148 :         sputb32(blockmax, f);
    2637         148 :         if (serror(f)) {
    2638             :                 /* LCOV_EXCL_START */
    2639             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2640             :                 return context;
    2641             :                 /* LCOV_EXCL_STOP */
    2642             :         }
    2643             : 
    2644         148 :         sputc('c', f);
    2645         148 :         if (state->hash == HASH_MURMUR3) {
    2646         118 :                 sputc('u', f);
    2647          30 :         } else if (state->hash == HASH_SPOOKY2) {
    2648          30 :                 sputc('k', f);
    2649             :         } else {
    2650             :                 /* LCOV_EXCL_START */
    2651             :                 log_fatal("Unexpected hash when writing the content file '%s'.\n", serrorfile(f));
    2652             :                 return context;
    2653             :                 /* LCOV_EXCL_STOP */
    2654             :         }
    2655         148 :         swrite(state->hashseed, HASH_SIZE, f);
    2656         148 :         if (serror(f)) {
    2657             :                 /* LCOV_EXCL_START */
    2658             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2659             :                 return context;
    2660             :                 /* LCOV_EXCL_STOP */
    2661             :         }
    2662             : 
    2663             :         /* previous hash only present */
    2664         148 :         if (state->prevhash != HASH_UNDEFINED) {
    2665             :                 /* if at least one rehash tag found, we have to save the previous hash */
    2666           4 :                 if (info_has_rehash) {
    2667           3 :                         sputc('C', f);
    2668           3 :                         if (state->prevhash == HASH_MURMUR3) {
    2669           3 :                                 sputc('u', f);
    2670           0 :                         } else if (state->prevhash == HASH_SPOOKY2) {
    2671           0 :                                 sputc('k', f);
    2672             :                         } else {
    2673             :                                 /* LCOV_EXCL_START */
    2674             :                                 log_fatal("Unexpected prevhash when writing the content file '%s'.\n", serrorfile(f));
    2675             :                                 return context;
    2676             :                                 /* LCOV_EXCL_STOP */
    2677             :                         }
    2678           3 :                         swrite(state->prevhashseed, HASH_SIZE, f);
    2679           3 :                         if (serror(f)) {
    2680             :                                 /* LCOV_EXCL_START */
    2681             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2682             :                                 return context;
    2683             :                                 /* LCOV_EXCL_STOP */
    2684             :                         }
    2685             :                 }
    2686             :         }
    2687             : 
    2688             :         /* for each map */
    2689        1036 :         for (i = state->maplist; i != 0; i = i->next) {
    2690         888 :                 struct snapraid_map* map = i->data;
    2691             :                 struct snapraid_disk* disk;
    2692             : 
    2693             :                 /* find the disk for this mapping */
    2694         888 :                 disk = find_disk_by_name(state, map->name);
    2695         888 :                 if (!disk) {
    2696             :                         /* LCOV_EXCL_START */
    2697             :                         log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
    2698             :                         return context;
    2699             :                         /* LCOV_EXCL_STOP */
    2700             :                 }
    2701             : 
    2702             :                 /* save the mapping only if disk is mapped */
    2703         888 :                 if (disk->mapping_idx != -1) {
    2704         796 :                         sputc('M', f);
    2705         796 :                         sputbs(map->name, f);
    2706         796 :                         sputb32(map->position, f);
    2707         796 :                         sputb32(map->total_blocks, f);
    2708         796 :                         sputb32(map->free_blocks, f);
    2709         796 :                         sputbs(map->uuid, f);
    2710         796 :                         if (serror(f)) {
    2711             :                                 /* LCOV_EXCL_START */
    2712             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2713             :                                 return context;
    2714             :                                 /* LCOV_EXCL_STOP */
    2715             :                         }
    2716             :                 }
    2717             :         }
    2718             : 
    2719             :         /* for each parity */
    2720        1011 :         for (l = 0; l < state->level; ++l) {
    2721         863 :                 sputc('P', f);
    2722         863 :                 sputb32(l, f);
    2723         863 :                 sputb32(state->parity[l].total_blocks, f);
    2724         863 :                 sputb32(state->parity[l].free_blocks, f);
    2725         863 :                 sputbs(state->parity[l].uuid, f);
    2726         863 :                 if (serror(f)) {
    2727             :                         /* LCOV_EXCL_START */
    2728             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2729             :                         return context;
    2730             :                         /* LCOV_EXCL_STOP */
    2731             :                 }
    2732             :         }
    2733             : 
    2734             :         /* for each disk */
    2735        1036 :         for (i = state->disklist; i != 0; i = i->next) {
    2736             :                 tommy_node* j;
    2737         888 :                 struct snapraid_disk* disk = i->data;
    2738             :                 struct snapraid_chunk* fs_last;
    2739             : 
    2740             :                 /**
    2741             :                  * This is the last accessed chunk in the tree operations.
    2742             :                  * Here we are inside a thread, and we cannot use the
    2743             :                  * ::disk member to store this information.
    2744             :                  */
    2745         888 :                 fs_last = 0;
    2746             : 
    2747             :                 /* if the disk is not mapped, skip it */
    2748         888 :                 if (disk->mapping_idx < 0)
    2749          92 :                         continue;
    2750             : 
    2751             :                 /* for each file */
    2752     1748749 :                 for (j = disk->filelist; j != 0; j = j->next) {
    2753     1747953 :                         struct snapraid_file* file = j->data;
    2754             :                         uint64_t size;
    2755             :                         uint64_t mtime_sec;
    2756             :                         int32_t mtime_nsec;
    2757             :                         uint64_t inode;
    2758             : 
    2759     1747953 :                         size = file->size;
    2760     1747953 :                         mtime_sec = file->mtime_sec;
    2761     1747953 :                         mtime_nsec = file->mtime_nsec;
    2762     1747953 :                         inode = file->inode;
    2763             : 
    2764     1747953 :                         sputc('f', f);
    2765     1747953 :                         sputb32(disk->mapping_idx, f);
    2766     1747953 :                         sputb64(size, f);
    2767     1747953 :                         sputb64(mtime_sec, f);
    2768             :                         /* encode STAT_NSEC_INVALID as 0 */
    2769     1747953 :                         if (mtime_nsec == STAT_NSEC_INVALID)
    2770           0 :                                 sputb32(0, f);
    2771             :                         else
    2772     1747953 :                                 sputb32(mtime_nsec + 1, f);
    2773     1747953 :                         sputb64(inode, f);
    2774     1747953 :                         sputbs(file->sub, f);
    2775     1747953 :                         if (serror(f)) {
    2776             :                                 /* LCOV_EXCL_START */
    2777             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2778             :                                 return context;
    2779             :                                 /* LCOV_EXCL_STOP */
    2780             :                         }
    2781             : 
    2782             :                         /* for all the blocks of the file */
    2783     1747953 :                         begin = 0;
    2784     5254144 :                         while (begin < file->blockmax) {
    2785     1758238 :                                 unsigned v_state = block_state_get(fs_file2block_get(file, begin));
    2786     1758238 :                                 block_off_t v_pos = fs_file2par_get_ts(disk, &fs_last, file, begin);
    2787             :                                 uint32_t v_count;
    2788             : 
    2789             :                                 block_off_t end;
    2790             : 
    2791             :                                 /* find the end of run of blocks */
    2792     1758238 :                                 end = begin + 1;
    2793     6066576 :                                 while (end < file->blockmax) {
    2794     2562818 :                                         if (v_state != block_state_get(fs_file2block_get(file, end)))
    2795          12 :                                                 break;
    2796     2562806 :                                         if (v_pos + (end - begin) != fs_file2par_get_ts(disk, &fs_last, file, end))
    2797       12706 :                                                 break;
    2798     2550100 :                                         ++end;
    2799             :                                 }
    2800             : 
    2801     1758238 :                                 switch (v_state) {
    2802             :                                 case BLOCK_STATE_BLK :
    2803     1634130 :                                         sputc('b', f);
    2804     1634130 :                                         break;
    2805             :                                 case BLOCK_STATE_CHG :
    2806       67707 :                                         sputc('g', f);
    2807       67707 :                                         break;
    2808             :                                 case BLOCK_STATE_REP :
    2809       56401 :                                         sputc('p', f);
    2810       56401 :                                         break;
    2811             :                                 default :
    2812             :                                         /* LCOV_EXCL_START */
    2813             :                                         log_fatal("Internal inconsistency in state for block %u state %u\n", v_pos, v_state);
    2814             :                                         return context;
    2815             :                                         /* LCOV_EXCL_STOP */
    2816             :                                 }
    2817             : 
    2818     1758238 :                                 sputb32(v_pos, f);
    2819             : 
    2820     1758238 :                                 v_count = end - begin;
    2821     1758238 :                                 sputb32(v_count, f);
    2822             : 
    2823             :                                 /* write hashes */
    2824     6066576 :                                 for (idx = begin; idx < end; ++idx) {
    2825     4308338 :                                         struct snapraid_block* block = fs_file2block_get(file, idx);
    2826             : 
    2827     4308338 :                                         swrite(block->hash, HASH_SIZE, f);
    2828             :                                 }
    2829             : 
    2830     1758238 :                                 if (serror(f)) {
    2831             :                                         /* LCOV_EXCL_START */
    2832             :                                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2833             :                                         return context;
    2834             :                                         /* LCOV_EXCL_STOP */
    2835             :                                 }
    2836             : 
    2837             :                                 /* next begin position */
    2838     1758238 :                                 begin = end;
    2839             :                         }
    2840             : 
    2841     1747953 :                         ++count_file;
    2842             :                 }
    2843             : 
    2844             :                 /* for each link */
    2845       58058 :                 for (j = disk->linklist; j != 0; j = j->next) {
    2846       57262 :                         struct snapraid_link* slink = j->data;
    2847             : 
    2848       57262 :                         switch (link_flag_get(slink, FILE_IS_LINK_MASK)) {
    2849             :                         case FILE_IS_HARDLINK :
    2850         312 :                                 sputc('a', f);
    2851         312 :                                 ++count_hardlink;
    2852         312 :                                 break;
    2853             :                         case FILE_IS_SYMLINK :
    2854       56950 :                                 sputc('s', f);
    2855       56950 :                                 ++count_symlink;
    2856       56950 :                                 break;
    2857             :                         }
    2858             : 
    2859       57262 :                         sputb32(disk->mapping_idx, f);
    2860       57262 :                         sputbs(slink->sub, f);
    2861       57262 :                         sputbs(slink->linkto, f);
    2862       57262 :                         if (serror(f)) {
    2863             :                                 /* LCOV_EXCL_START */
    2864             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2865             :                                 return context;
    2866             :                                 /* LCOV_EXCL_STOP */
    2867             :                         }
    2868             :                 }
    2869             : 
    2870             :                 /* for each dir */
    2871        1211 :                 for (j = disk->dirlist; j != 0; j = j->next) {
    2872         415 :                         struct snapraid_dir* dir = j->data;
    2873             : 
    2874         415 :                         sputc('r', f);
    2875         415 :                         sputb32(disk->mapping_idx, f);
    2876         415 :                         sputbs(dir->sub, f);
    2877         415 :                         if (serror(f)) {
    2878             :                                 /* LCOV_EXCL_START */
    2879             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2880             :                                 return context;
    2881             :                                 /* LCOV_EXCL_STOP */
    2882             :                         }
    2883             : 
    2884         415 :                         ++count_dir;
    2885             :                 }
    2886             : 
    2887             :                 /* deleted blocks of the disk */
    2888         796 :                 sputc('h', f);
    2889         796 :                 sputb32(disk->mapping_idx, f);
    2890         796 :                 if (serror(f)) {
    2891             :                         /* LCOV_EXCL_START */
    2892             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2893             :                         return context;
    2894             :                         /* LCOV_EXCL_STOP */
    2895             :                 }
    2896         796 :                 begin = 0;
    2897        2466 :                 while (begin < blockmax) {
    2898             :                         int is_deleted;
    2899             :                         block_off_t end;
    2900             : 
    2901         874 :                         is_deleted = fs_is_block_deleted_ts(disk, &fs_last, begin);
    2902             : 
    2903             :                         /* find the end of run of blocks */
    2904         874 :                         end = begin + 1;
    2905     4473223 :                         while (end < blockmax
    2906     4471553 :                                 && is_deleted == fs_is_block_deleted_ts(disk, &fs_last, end)
    2907             :                         ) {
    2908     4471475 :                                 ++end;
    2909             :                         }
    2910             : 
    2911         874 :                         sputb32(end - begin, f);
    2912             : 
    2913         874 :                         if (is_deleted) {
    2914             :                                 /* write the run of deleted blocks with hash */
    2915          49 :                                 sputc('o', f);
    2916             : 
    2917             :                                 /* write all the hash */
    2918       15965 :                                 while (begin < end) {
    2919       15867 :                                         struct snapraid_block* block = fs_par2block_get_ts(disk, &fs_last, begin);
    2920             : 
    2921       15867 :                                         swrite(block->hash, HASH_SIZE, f);
    2922             : 
    2923       15867 :                                         ++begin;
    2924             :                                 }
    2925             :                         } else {
    2926             :                                 /* write the run of blocks without hash */
    2927             :                                 /* they can be either used or empty blocks */
    2928         825 :                                 sputc('O', f);
    2929             : 
    2930             :                                 /* next begin position */
    2931         825 :                                 begin = end;
    2932             :                         }
    2933             : 
    2934         874 :                         if (serror(f)) {
    2935             :                                 /* LCOV_EXCL_START */
    2936             :                                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2937             :                                 return context;
    2938             :                                 /* LCOV_EXCL_STOP */
    2939             :                         }
    2940             :                 }
    2941             :         }
    2942             : 
    2943             :         /* write the info for each block */
    2944         148 :         sputc('i', f);
    2945         148 :         sputb32(info_oldest, f);
    2946         148 :         begin = 0;
    2947       30307 :         while (begin < blockmax) {
    2948             :                 snapraid_info info;
    2949             :                 block_off_t end;
    2950             :                 time_t t;
    2951             :                 unsigned flag;
    2952             : 
    2953       30011 :                 info = info_get(&state->infoarr, begin);
    2954             : 
    2955             :                 /* find the end of run of blocks */
    2956       30011 :                 end = begin + 1;
    2957      776577 :                 while (end < blockmax
    2958      746421 :                         && info == info_get(&state->infoarr, end)
    2959             :                 ) {
    2960      716555 :                         ++end;
    2961             :                 }
    2962             : 
    2963       30011 :                 sputb32(end - begin, f);
    2964             : 
    2965             :                 /* if there is info */
    2966       30011 :                 if (info) {
    2967             :                         /* other flags */
    2968       29988 :                         flag = 1; /* info is present */
    2969       29988 :                         if (info_get_bad(info))
    2970        1341 :                                 flag |= 2;
    2971       29988 :                         if (info_get_rehash(info))
    2972        9819 :                                 flag |= 4;
    2973       29988 :                         if (info_get_justsynced(info))
    2974       17932 :                                 flag |= 8;
    2975       29988 :                         sputb32(flag, f);
    2976             : 
    2977       29988 :                         t = info_get_time(info) - info_oldest;
    2978       29988 :                         sputb32(t, f);
    2979             :                 } else {
    2980             :                         /* write a special 0 flag to mark missing info */
    2981          23 :                         sputb32(0, f);
    2982             :                 }
    2983             : 
    2984       30011 :                 if (serror(f)) {
    2985             :                         /* LCOV_EXCL_START */
    2986             :                         log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    2987             :                         return context;
    2988             :                         /* LCOV_EXCL_STOP */
    2989             :                 }
    2990             : 
    2991             :                 /* next begin position */
    2992       30011 :                 begin = end;
    2993             :         }
    2994             : 
    2995         148 :         sputc('N', f);
    2996             : 
    2997             :         /* flush data written to the disk */
    2998         148 :         if (sflush(f)) {
    2999             :                 /* LCOV_EXCL_START */
    3000             :                 log_fatal("Error writing the content file '%s' (in flush before crc). %s.\n", serrorfile(f), strerror(errno));
    3001             :                 return context;
    3002             :                 /* LCOV_EXCL_STOP */
    3003             :         }
    3004             : 
    3005             :         /* get the file crc */
    3006         148 :         crc = scrc(f);
    3007             : 
    3008             :         /* compare the crc of the data written to file */
    3009             :         /* with the one of the data written to the stream */
    3010         148 :         if (crc != scrc_stream(f)) {
    3011             :                 /* LCOV_EXCL_START */
    3012             :                 log_fatal("CRC mismatch writing the content stream.\n");
    3013             :                 log_fatal("DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
    3014             :                 log_fatal("Try running a memory test like http://www.memtest86.com/\n");
    3015             :                 return context;
    3016             :                 /* LCOV_EXCL_STOP */
    3017             :         }
    3018             : 
    3019         148 :         sputble32(crc, f);
    3020         148 :         if (serror(f)) {
    3021             :                 /* LCOV_EXCL_START */
    3022             :                 log_fatal("Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    3023             :                 return context;
    3024             :                 /* LCOV_EXCL_STOP */
    3025             :         }
    3026             : 
    3027             :         /* set output variables */
    3028         148 :         context->crc = crc;
    3029         148 :         context->count_file = count_file;
    3030         148 :         context->count_hardlink = count_hardlink;
    3031         148 :         context->count_symlink = count_symlink;
    3032         148 :         context->count_dir = count_dir;
    3033             : 
    3034         148 :         return 0;
    3035             : }
    3036             : 
    3037         148 : static void state_write_content(struct snapraid_state* state, uint32_t* out_crc)
    3038             : {
    3039             : #if HAVE_MT_WRITE
    3040             :         int fail;
    3041             :         int first;
    3042             : #else
    3043             :         STREAM* f;
    3044             :         unsigned count_content;
    3045             :         unsigned k;
    3046             :         struct state_write_thread_context* context;
    3047             :         void* retval;
    3048             : #endif
    3049             :         tommy_node* i;
    3050             :         block_off_t blockmax;
    3051             :         time_t info_oldest;
    3052             :         int info_has_rehash;
    3053             :         int mapping_idx;
    3054             :         block_off_t idx;
    3055             :         uint32_t crc;
    3056             :         unsigned count_file;
    3057             :         unsigned count_hardlink;
    3058             :         unsigned count_symlink;
    3059             :         unsigned count_dir;
    3060             : 
    3061             :         /* blocks of all array */
    3062         148 :         blockmax = parity_allocated_size(state);
    3063             : 
    3064             :         /* check the file-system on all disks */
    3065         148 :         state_fscheck(state, "before write");
    3066             : 
    3067             :         /* clear the info for unused blocks */
    3068             :         /* and get some other info */
    3069         148 :         info_oldest = 0; /* oldest time in info */
    3070         148 :         info_has_rehash = 0; /* if there is a rehash info */
    3071      746714 :         for (idx = 0; idx < blockmax; ++idx) {
    3072             :                 /* if the position is used */
    3073      746566 :                 if (fs_position_is_required(state, idx)) {
    3074      746566 :                         snapraid_info info = info_get(&state->infoarr, idx);
    3075             : 
    3076             :                         /* only if there is some info to store */
    3077      746566 :                         if (info) {
    3078      702081 :                                 time_t info_time = info_get_time(info);
    3079             : 
    3080      702081 :                                 if (!info_oldest || info_time < info_oldest)
    3081         156 :                                         info_oldest = info_time;
    3082             : 
    3083      702081 :                                 if (info_get_rehash(info))
    3084       18429 :                                         info_has_rehash = 1;
    3085             :                         }
    3086             :                 } else {
    3087             :                         /* clear any previous info */
    3088           0 :                         info_set(&state->infoarr, idx, 0);
    3089             : 
    3090             :                         /* and clear any deleted blocks */
    3091           0 :                         fs_position_clear_deleted(state, idx);
    3092             :                 }
    3093             :         }
    3094             : 
    3095             :         /* map disks */
    3096         148 :         mapping_idx = 0;
    3097        1036 :         for (i = state->maplist; i != 0; i = i->next) {
    3098         888 :                 struct snapraid_map* map = i->data;
    3099             :                 struct snapraid_disk* disk;
    3100             : 
    3101             :                 /* find the disk for this mapping */
    3102         888 :                 disk = find_disk_by_name(state, map->name);
    3103         888 :                 if (!disk) {
    3104             :                         /* LCOV_EXCL_START */
    3105             :                         log_fatal("Internal inconsistency for unmapped disk '%s'\n", map->name);
    3106             :                         os_abort();
    3107             :                         /* LCOV_EXCL_STOP */
    3108             :                 }
    3109             : 
    3110             :                 /* save the mapping only for not empty disks */
    3111         888 :                 if (!disk_is_empty(disk, blockmax)) {
    3112             :                         /* assign the mapping index used to identify disks */
    3113         796 :                         disk->mapping_idx = mapping_idx;
    3114         796 :                         ++mapping_idx;
    3115             :                 } else {
    3116             :                         /* mark the disk as without mapping */
    3117          92 :                         disk->mapping_idx = -1;
    3118             :                 }
    3119             :         }
    3120             : 
    3121             : #if HAVE_MT_WRITE
    3122             :         /* start all writing threads */
    3123             :         i = tommy_list_head(&state->contentlist);
    3124             :         while (i) {
    3125             :                 struct snapraid_content* content = i->data;
    3126             :                 struct state_write_thread_context* context;
    3127             :                 char tmp[PATH_MAX];
    3128             :                 STREAM* f;
    3129             : 
    3130             :                 msg_progress("Saving state to %s...\n", content->content);
    3131             : 
    3132             :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    3133             :                 f = sopen_write(tmp);
    3134             :                 if (f == 0) {
    3135             :                         /* LCOV_EXCL_START */
    3136             :                         log_fatal("Error opening the content file '%s'. %s.\n", tmp, strerror(errno));
    3137             :                         exit(EXIT_FAILURE);
    3138             :                         /* LCOV_EXCL_STOP */
    3139             :                 }
    3140             : 
    3141             :                 /* allocate the thread context */
    3142             :                 context = malloc_nofail(sizeof(struct state_write_thread_context));
    3143             :                 content->context = context;
    3144             : 
    3145             :                 /* initialize */
    3146             :                 context->state = state;
    3147             :                 context->blockmax = blockmax;
    3148             :                 context->info_oldest = info_oldest;
    3149             :                 context->info_has_rehash = info_has_rehash;
    3150             :                 context->f = f;
    3151             : 
    3152             :                 if (pthread_create(&context->thread, 0, state_write_thread, context) != 0) {
    3153             :                         /* LCOV_EXCL_START */
    3154             :                         log_fatal("Failed to create thread.\n");
    3155             :                         exit(EXIT_FAILURE);
    3156             :                         /* LCOV_EXCL_STOP */
    3157             :                 }
    3158             : 
    3159             :                 i = i->next;
    3160             :         }
    3161             : 
    3162             :         /* join all thread */
    3163             :         fail = 0;
    3164             :         first = 1;
    3165             :         crc = 0;
    3166             :         count_file = 0;
    3167             :         count_hardlink = 0;
    3168             :         count_symlink = 0;
    3169             :         count_dir = 0;
    3170             :         i = tommy_list_head(&state->contentlist);
    3171             :         while (i) {
    3172             :                 struct snapraid_content* content = i->data;
    3173             :                 struct state_write_thread_context* context = content->context;
    3174             :                 void* retval;
    3175             : 
    3176             :                 if (pthread_join(context->thread, &retval) != 0) {
    3177             :                         /* LCOV_EXCL_START */
    3178             :                         log_fatal("Failed to join thread.\n");
    3179             :                         exit(EXIT_FAILURE);
    3180             :                         /* LCOV_EXCL_STOP */
    3181             :                 }
    3182             : 
    3183             :                 if (retval) {
    3184             :                         /* LCOV_EXCL_START */
    3185             :                         fail = 1;
    3186             :                         /* LCOV_EXCL_STOP */
    3187             :                 } else {
    3188             :                         STREAM* f = context->f;
    3189             : 
    3190             :                         /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
    3191             :                         /* than even in a system crash event we have one valid copy of the file. */
    3192             :                         if (sflush(f) != 0) {
    3193             :                                 /* LCOV_EXCL_START */
    3194             :                                 log_fatal("Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
    3195             :                                 exit(EXIT_FAILURE);
    3196             :                                 /* LCOV_EXCL_STOP */
    3197             :                         }
    3198             : 
    3199             : #if HAVE_FSYNC
    3200             :                         if (ssync(f) != 0) {
    3201             :                                 /* LCOV_EXCL_START */
    3202             :                                 log_fatal("Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
    3203             :                                 exit(EXIT_FAILURE);
    3204             :                                 /* LCOV_EXCL_STOP */
    3205             :                         }
    3206             : #endif
    3207             : 
    3208             :                         if (sclose(f) != 0) {
    3209             :                                 /* LCOV_EXCL_START */
    3210             :                                 log_fatal("Error closing the content file. %s.\n", strerror(errno));
    3211             :                                 exit(EXIT_FAILURE);
    3212             :                                 /* LCOV_EXCL_STOP */
    3213             :                         }
    3214             : 
    3215             :                         if (first) {
    3216             :                                 first = 0;
    3217             :                                 crc = context->crc;
    3218             :                                 count_file = context->count_file;
    3219             :                                 count_hardlink = context->count_hardlink;
    3220             :                                 count_symlink = context->count_symlink;
    3221             :                                 count_dir = context->count_dir;
    3222             :                         } else {
    3223             :                                 if (crc != context->crc) {
    3224             :                                         /* LCOV_EXCL_START */
    3225             :                                         log_fatal("Different CRCs writing content streams.\n");
    3226             :                                         log_fatal("DANGER! Your RAM memory is broken! DO NOT PROCEED UNTIL FIXED!\n");
    3227             :                                         log_fatal("Try running a memory test like http://www.memtest86.com/\n");
    3228             :                                         exit(EXIT_FAILURE);
    3229             :                                         /* LCOV_EXCL_STOP */
    3230             :                                 }
    3231             :                         }
    3232             :                 }
    3233             : 
    3234             :                 free(context);
    3235             : 
    3236             :                 i = i->next;
    3237             :         }
    3238             : 
    3239             :         /* abort on failure */
    3240             :         if (fail) {
    3241             :                 /* LCOV_EXCL_START */
    3242             :                 exit(EXIT_FAILURE);
    3243             :                 /* LCOV_EXCL_STOP */
    3244             :         }
    3245             : #else
    3246             :         /* count the content files */
    3247         148 :         count_content = 0;
    3248         148 :         i = tommy_list_head(&state->contentlist);
    3249        1307 :         while (i) {
    3250        1011 :                 struct snapraid_content* content = i->data;
    3251        1011 :                 msg_progress("Saving state to %s...\n", content->content);
    3252        1011 :                 ++count_content;
    3253        1011 :                 i = i->next;
    3254             :         }
    3255             : 
    3256             :         /* open all the content files */
    3257         148 :         f = sopen_multi_write(count_content);
    3258         148 :         if (!f) {
    3259             :                 /* LCOV_EXCL_START */
    3260             :                 log_fatal("Error opening the content files.\n");
    3261             :                 exit(EXIT_FAILURE);
    3262             :                 /* LCOV_EXCL_STOP */
    3263             :         }
    3264             : 
    3265         148 :         k = 0;
    3266         148 :         i = tommy_list_head(&state->contentlist);
    3267        1307 :         while (i) {
    3268        1011 :                 struct snapraid_content* content = i->data;
    3269             :                 char tmp[PATH_MAX];
    3270        1011 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    3271        1011 :                 if (sopen_multi_file(f, k, tmp) != 0) {
    3272             :                         /* LCOV_EXCL_START */
    3273             :                         log_fatal("Error opening the content file '%s'. %s.\n", tmp, strerror(errno));
    3274             :                         exit(EXIT_FAILURE);
    3275             :                         /* LCOV_EXCL_STOP */
    3276             :                 }
    3277        1011 :                 ++k;
    3278        1011 :                 i = i->next;
    3279             :         }
    3280             : 
    3281             :         /* allocate the thread context */
    3282         148 :         context = malloc_nofail(sizeof(struct state_write_thread_context));
    3283             : 
    3284             :         /* initialize */
    3285         148 :         context->state = state;
    3286         148 :         context->blockmax = blockmax;
    3287         148 :         context->info_oldest = info_oldest;
    3288         148 :         context->info_has_rehash = info_has_rehash;
    3289         148 :         context->f = f;
    3290             : 
    3291         148 :         retval = state_write_thread(context);
    3292             : 
    3293             :         /* abort on failure */
    3294         148 :         if (retval) {
    3295             :                 /* LCOV_EXCL_START */
    3296             :                 exit(EXIT_FAILURE);
    3297             :                 /* LCOV_EXCL_STOP */
    3298             :         }
    3299             : 
    3300             :         /* Use the sequence fflush() -> fsync() -> fclose() -> rename() to ensure */
    3301             :         /* than even in a system crash event we have one valid copy of the file. */
    3302         148 :         if (sflush(f) != 0) {
    3303             :                 /* LCOV_EXCL_START */
    3304             :                 log_fatal("Error writing the content file '%s', in flush(). %s.\n", serrorfile(f), strerror(errno));
    3305             :                 exit(EXIT_FAILURE);
    3306             :                 /* LCOV_EXCL_STOP */
    3307             :         }
    3308             : 
    3309             : #if HAVE_FSYNC
    3310         148 :         if (ssync(f) != 0) {
    3311             :                 /* LCOV_EXCL_START */
    3312             :                 log_fatal("Error writing the content file '%s' in sync(). %s.\n", serrorfile(f), strerror(errno));
    3313             :                 exit(EXIT_FAILURE);
    3314             :                 /* LCOV_EXCL_STOP */
    3315             :         }
    3316             : #endif
    3317             : 
    3318         148 :         if (sclose(f) != 0) {
    3319             :                 /* LCOV_EXCL_START */
    3320             :                 log_fatal("Error closing the content file. %s.\n", strerror(errno));
    3321             :                 exit(EXIT_FAILURE);
    3322             :                 /* LCOV_EXCL_STOP */
    3323             :         }
    3324             : 
    3325         148 :         crc = context->crc;
    3326         148 :         count_file = context->count_file;
    3327         148 :         count_hardlink = context->count_hardlink;
    3328         148 :         count_symlink = context->count_symlink;
    3329         148 :         count_dir = context->count_dir;
    3330             : 
    3331         148 :         free(context);
    3332             : #endif
    3333             : 
    3334         148 :         msg_verbose("%8u files\n", count_file);
    3335         148 :         msg_verbose("%8u hardlinks\n", count_hardlink);
    3336         148 :         msg_verbose("%8u symlinks\n", count_symlink);
    3337         148 :         msg_verbose("%8u empty dirs\n", count_dir);
    3338             : 
    3339         148 :         *out_crc = crc;
    3340         148 : }
    3341             : 
    3342         252 : void state_read(struct snapraid_state* state)
    3343             : {
    3344             :         STREAM* f;
    3345             :         char path[PATH_MAX];
    3346             :         struct stat st;
    3347             :         tommy_node* node;
    3348             :         int ret;
    3349             :         int c;
    3350             : 
    3351             :         /* iterate over all the available content files and load the first one present */
    3352         252 :         f = 0;
    3353         252 :         node = tommy_list_head(&state->contentlist);
    3354         554 :         while (node) {
    3355         295 :                 struct snapraid_content* content = node->data;
    3356         295 :                 pathcpy(path, sizeof(path), content->content);
    3357             : 
    3358         295 :                 if (!state->no_conf) {
    3359         294 :                         log_tag("content:%s\n", path);
    3360         294 :                         log_flush();
    3361             :                 }
    3362         295 :                 msg_progress("Loading state from %s...\n", path);
    3363             : 
    3364         295 :                 f = sopen_read(path);
    3365         295 :                 if (f != 0) {
    3366             :                         /* if opened stop the search */
    3367         245 :                         break;
    3368             :                 } else {
    3369             :                         /* if it's real error of an existing file, abort */
    3370          50 :                         if (errno != ENOENT) {
    3371             :                                 /* LCOV_EXCL_START */
    3372             :                                 log_fatal("Error opening the content file '%s'. %s.\n", path, strerror(errno));
    3373             :                                 exit(EXIT_FAILURE);
    3374             :                                 /* LCOV_EXCL_STOP */
    3375             :                         }
    3376             : 
    3377             :                         /* otherwise continue */
    3378          50 :                         if (node->next) {
    3379          43 :                                 log_fatal("WARNING! Content file '%s' not found, trying with another copy...\n", path);
    3380             : 
    3381             :                                 /* ensure to rewrite all the content files */
    3382          43 :                                 state->need_write = 1;
    3383             :                         }
    3384             :                 }
    3385             : 
    3386             :                 /* next content file */
    3387          50 :                 node = node->next;
    3388             :         }
    3389             : 
    3390             :         /* if not found, assume empty */
    3391         252 :         if (!f) {
    3392           7 :                 log_fatal("No content file found. Assuming empty.\n");
    3393             : 
    3394             :                 /* create the initial mapping */
    3395           7 :                 state_map(state);
    3396         258 :                 return;
    3397             :         }
    3398             : 
    3399             :         /* get the stat of the content file */
    3400         245 :         ret = fstat(shandle(f), &st);
    3401         245 :         if (ret != 0) {
    3402             :                 /* LCOV_EXCL_START */
    3403             :                 log_fatal("Error stating the content file '%s'. %s.\n", path, strerror(errno));
    3404             :                 exit(EXIT_FAILURE);
    3405             :                 /* LCOV_EXCL_STOP */
    3406             :         }
    3407             : 
    3408             :         /* go further to check other content files */
    3409        1909 :         while (node) {
    3410             :                 char other_path[PATH_MAX];
    3411             :                 struct stat other_st;
    3412        1419 :                 struct snapraid_content* content = node->data;
    3413        1419 :                 pathcpy(other_path, sizeof(other_path), content->content);
    3414             : 
    3415        1419 :                 ret = stat(other_path, &other_st);
    3416        1419 :                 if (ret != 0) {
    3417             :                         /* allow missing content files, but not any other kind of error */
    3418           1 :                         if (errno != ENOENT) {
    3419             :                                 /* LCOV_EXCL_START */
    3420             :                                 log_fatal("Error stating the content file '%s'. %s.\n", other_path, strerror(errno));
    3421             :                                 exit(EXIT_FAILURE);
    3422             :                                 /* LCOV_EXCL_STOP */
    3423             :                         }
    3424             : 
    3425             :                         /* ensure to rewrite all the content files */
    3426           1 :                         state->need_write = 1;
    3427             :                 } else {
    3428             :                         /* if the size is different */
    3429        1418 :                         if (other_st.st_size != st.st_size) {
    3430          39 :                                 log_fatal("WARNING! Content files '%s' and '%s' have a different size!\n", path, other_path);
    3431          39 :                                 log_fatal("Likely one of the two is broken!\n");
    3432             : 
    3433             :                                 /* ensure to rewrite all the content files */
    3434          39 :                                 state->need_write = 1;
    3435             :                         }
    3436             :                 }
    3437             : 
    3438             :                 /* next content file */
    3439        1419 :                 node = node->next;
    3440             :         }
    3441             : 
    3442             :         /* start with a undefined default. */
    3443             :         /* it's for compatibility with version 1.0 where MD5 was implicit. */
    3444         245 :         state->hash = HASH_UNDEFINED;
    3445             : 
    3446             :         /* start with a zero seed, it was the default in old versions */
    3447         245 :         memset(state->hashseed, 0, HASH_SIZE);
    3448             : 
    3449             :         /* previous hash, start with an undefined value */
    3450         245 :         state->prevhash = HASH_UNDEFINED;
    3451             : 
    3452             :         /* intentionally not set the prevhashseed, if used valgrind will warn about it */
    3453             : 
    3454             :         /* get the first char to detect the file type */
    3455         245 :         c = sgetc(f);
    3456         245 :         sungetc(c, f);
    3457             : 
    3458             :         /* guess the file type from the first char */
    3459         245 :         if (c == 'S') {
    3460         245 :                 state_read_content(state, path, f);
    3461             :         } else {
    3462             :                 /* LCOV_EXCL_START */
    3463             :                 log_fatal("From SnapRAID v9.0 the text content file is not supported anymore.\n");
    3464             :                 log_fatal("You have first to upgrade to SnapRAID v8.1 to convert it to binary format.\n");
    3465             :                 exit(EXIT_FAILURE);
    3466             :                 /* LCOV_EXCL_STOP */
    3467             :         }
    3468             : 
    3469         244 :         sclose(f);
    3470             : 
    3471         244 :         if (state->hash == HASH_UNDEFINED) {
    3472             :                 /* LCOV_EXCL_START */
    3473             :                 log_fatal("The checksum to use is not specified.\n");
    3474             :                 log_fatal("This happens because you are likely upgrading from SnapRAID 1.0.\n");
    3475             :                 log_fatal("To use a new SnapRAID you must restart from scratch,\n");
    3476             :                 log_fatal("deleting all the content and parity files.\n");
    3477             :                 exit(EXIT_FAILURE);
    3478             :                 /* LCOV_EXCL_STOP */
    3479             :         }
    3480             : 
    3481             :         /* update the mapping */
    3482         244 :         state_map(state);
    3483             : 
    3484         244 :         state_content_check(state, path);
    3485             : 
    3486             :         /* mark that we read the content file, and it passed all the checks */
    3487         244 :         state->checked_read = 1;
    3488             : }
    3489             : 
    3490             : struct state_verify_thread_context {
    3491             :         struct snapraid_state* state;
    3492             :         struct snapraid_content* content;
    3493             : #if HAVE_MT_VERIFY
    3494             :         pthread_t thread;
    3495             : #else
    3496             :         void* retval;
    3497             : #endif
    3498             :         /* input */
    3499             :         uint32_t crc;
    3500             :         STREAM* f;
    3501             : };
    3502             : 
    3503        1011 : static void* state_verify_thread(void* arg)
    3504             : {
    3505        1011 :         struct state_verify_thread_context* context = arg;
    3506        1011 :         STREAM* f = context->f;
    3507             :         unsigned char buf[4];
    3508             :         uint32_t crc_stored;
    3509             :         uint32_t crc_computed;
    3510             : 
    3511        1011 :         if (sdeplete(f, buf) != 0) {
    3512             :                 /* LCOV_EXCL_START */
    3513             :                 log_fatal("Error flushing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
    3514             :                 return context;
    3515             :                 /* LCOV_EXCL_STOP */
    3516             :         }
    3517             : 
    3518             :         /* get the stored crc from the last four bytes */
    3519        1011 :         crc_stored = buf[0] | (uint32_t)buf[1] << 8 | (uint32_t)buf[2] << 16 | (uint32_t)buf[3] << 24;
    3520             : 
    3521        1011 :         if (crc_stored != context->crc) {
    3522             :                 /* LCOV_EXCL_START */
    3523             :                 log_fatal("DANGER! Wrong stored CRC in '%s'\n", serrorfile(f));
    3524             :                 return context;
    3525             :                 /* LCOV_EXCL_STOP */
    3526             :         }
    3527             : 
    3528             :         /* get the computed crc */
    3529        1011 :         crc_computed = scrc(f);
    3530             : 
    3531             :         /* adjust the stored crc to include itself */
    3532        1011 :         crc_stored = crc32c(crc_stored, buf, 4);
    3533             : 
    3534        1011 :         if (crc_computed != crc_stored) {
    3535             :                 /* LCOV_EXCL_START */
    3536             :                 log_fatal("DANGER! Wrong file CRC in '%s'\n", serrorfile(f));
    3537             :                 return context;
    3538             :                 /* LCOV_EXCL_STOP */
    3539             :         }
    3540             : 
    3541        1011 :         return 0;
    3542             : }
    3543             : 
    3544         148 : static void state_verify_content(struct snapraid_state* state, uint32_t crc)
    3545             : {
    3546             :         tommy_node* i;
    3547             :         int fail;
    3548             : 
    3549             :         /* start all reading threads */
    3550         148 :         i = tommy_list_head(&state->contentlist);
    3551        1307 :         while (i) {
    3552        1011 :                 struct snapraid_content* content = i->data;
    3553             :                 struct state_verify_thread_context* context;
    3554             :                 char tmp[PATH_MAX];
    3555             :                 STREAM* f;
    3556             : 
    3557        1011 :                 msg_progress("Verifying %s...\n", content->content);
    3558             : 
    3559        1011 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    3560             : 
    3561        1011 :                 f = sopen_read(tmp);
    3562        1011 :                 if (f == 0) {
    3563             :                         /* LCOV_EXCL_START */
    3564             :                         log_fatal("Error reopening the content file '%s'. %s.\n", tmp, strerror(errno));
    3565             :                         exit(EXIT_FAILURE);
    3566             :                         /* LCOV_EXCL_STOP */
    3567             :                 }
    3568             : 
    3569             :                 /* allocate the thread context */
    3570        1011 :                 context = malloc_nofail(sizeof(struct state_verify_thread_context));
    3571        1011 :                 content->context = context;
    3572             : 
    3573             :                 /* initialize */
    3574        1011 :                 context->state = state;
    3575        1011 :                 context->content = content;
    3576        1011 :                 context->crc = crc;
    3577        1011 :                 context->f = f;
    3578             : 
    3579             : #if HAVE_MT_VERIFY
    3580        1011 :                 if (pthread_create(&context->thread, 0, state_verify_thread, context) != 0) {
    3581             :                         /* LCOV_EXCL_START */
    3582             :                         log_fatal("Failed to create thread.\n");
    3583             :                         exit(EXIT_FAILURE);
    3584             :                         /* LCOV_EXCL_STOP */
    3585             :                 }
    3586             : #else
    3587             :                 context->retval = state_verify_thread(context);
    3588             : #endif
    3589             : 
    3590        1011 :                 i = i->next;
    3591             :         }
    3592             : 
    3593             :         /* join all thread */
    3594         148 :         fail = 0;
    3595         148 :         i = tommy_list_head(&state->contentlist);
    3596        1307 :         while (i) {
    3597        1011 :                 struct snapraid_content* content = i->data;
    3598        1011 :                 struct state_verify_thread_context* context = content->context;
    3599             :                 void* retval;
    3600             : 
    3601             : #if HAVE_MT_VERIFY
    3602        1011 :                 if (pthread_join(context->thread, &retval) != 0) {
    3603             :                         /* LCOV_EXCL_START */
    3604             :                         log_fatal("Failed to join thread.\n");
    3605             :                         exit(EXIT_FAILURE);
    3606             :                         /* LCOV_EXCL_STOP */
    3607             :                 }
    3608             : #else
    3609             :                 retval = context->retval;
    3610             : #endif
    3611        1011 :                 if (retval) {
    3612             :                         /* LCOV_EXCL_START */
    3613             :                         fail = 1;
    3614             :                         /* LCOV_EXCL_STOP */
    3615             :                 } else {
    3616        1011 :                         STREAM* f = context->f;
    3617             : 
    3618        1011 :                         if (sclose(f) != 0) {
    3619             :                                 /* LCOV_EXCL_START */
    3620             :                                 log_fatal("Error closing the content file. %s.\n", strerror(errno));
    3621             :                                 exit(EXIT_FAILURE);
    3622             :                                 /* LCOV_EXCL_STOP */
    3623             :                         }
    3624             :                 }
    3625             : 
    3626        1011 :                 free(context);
    3627             : 
    3628        1011 :                 i = i->next;
    3629             :         }
    3630             : 
    3631             :         /* abort on failure */
    3632         148 :         if (fail) {
    3633             :                 /* LCOV_EXCL_START */
    3634             :                 exit(EXIT_FAILURE);
    3635             :                 /* LCOV_EXCL_STOP */
    3636             :         }
    3637         148 : }
    3638             : 
    3639         148 : static void state_rename_content(struct snapraid_state* state)
    3640             : {
    3641             :         tommy_node* i;
    3642             : 
    3643         148 :         i = tommy_list_head(&state->contentlist);
    3644        1307 :         while (i) {
    3645        1011 :                 struct snapraid_content* content = i->data;
    3646             :                 char tmp[PATH_MAX];
    3647             : 
    3648             :                 /* now renames the just written copy with the correct name */
    3649        1011 :                 pathprint(tmp, sizeof(tmp), "%s.tmp", content->content);
    3650        1011 :                 if (rename(tmp, content->content) != 0) {
    3651             :                         /* LCOV_EXCL_START */
    3652             :                         log_fatal("Error renaming the content file '%s' to '%s' in rename(). %s.\n", tmp, content->content, strerror(errno));
    3653             :                         exit(EXIT_FAILURE);
    3654             :                         /* LCOV_EXCL_STOP */
    3655             :                 }
    3656             : 
    3657        1011 :                 i = i->next;
    3658             :         }
    3659         148 : }
    3660             : 
    3661         148 : void state_write(struct snapraid_state* state)
    3662             : {
    3663             :         uint32_t crc;
    3664             : 
    3665             :         /* write all the content files */
    3666         148 :         state_write_content(state, &crc);
    3667             : 
    3668             :         /* verify the just written files */
    3669         148 :         state_verify_content(state, crc);
    3670             : 
    3671             :         /* rename the new files, over the old ones */
    3672         148 :         state_rename_content(state);
    3673             : 
    3674         148 :         state->need_write = 0; /* no write needed anymore */
    3675         148 :         state->checked_read = 0; /* what we wrote is not checked in read */
    3676         148 : }
    3677             : 
    3678         122 : void state_skip(struct snapraid_state* state)
    3679             : {
    3680             :         tommy_node* i;
    3681             : 
    3682             :         /* for each disk */
    3683         850 :         for (i = state->disklist; i != 0; i = i->next) {
    3684             :                 tommy_node* j;
    3685         728 :                 struct snapraid_disk* disk = i->data;
    3686             : 
    3687         728 :                 if (!disk->skip_access)
    3688         727 :                         continue;
    3689             : 
    3690             :                 /* for each file */
    3691        1885 :                 for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
    3692        1884 :                         struct snapraid_file* file = j->data;
    3693        1884 :                         file_flag_set(file, FILE_IS_EXCLUDED);
    3694             :                 }
    3695             : 
    3696             :                 /* for each link */
    3697          57 :                 for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
    3698          56 :                         struct snapraid_link* slink = j->data;
    3699          56 :                         link_flag_set(slink, FILE_IS_EXCLUDED);
    3700             :                 }
    3701             : 
    3702             :                 /* for each dir */
    3703           1 :                 for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
    3704           0 :                         struct snapraid_dir* dir = j->data;
    3705           0 :                         dir_flag_set(dir, FILE_IS_EXCLUDED);
    3706             :                 }
    3707             :         }
    3708         122 : }
    3709             : 
    3710         122 : void state_filter(struct snapraid_state* state, tommy_list* filterlist_file, tommy_list* filterlist_disk, int filter_missing, int filter_error)
    3711             : {
    3712             :         tommy_node* i;
    3713             :         unsigned l;
    3714             : 
    3715             :         /* if no filter, include all */
    3716         122 :         if (!filter_missing && !filter_error && tommy_list_empty(filterlist_file) && tommy_list_empty(filterlist_disk))
    3717         235 :                 return;
    3718             : 
    3719           9 :         msg_progress("Filtering...\n");
    3720             : 
    3721          17 :         for (i = tommy_list_head(filterlist_disk); i != 0; i = i->next) {
    3722           8 :                 struct snapraid_filter* filter = i->data;
    3723           8 :                 msg_verbose("\t%s%s\n", filter->pattern, filter->is_disk ? "//" : "");
    3724             :         }
    3725          12 :         for (i = tommy_list_head(filterlist_file); i != 0; i = i->next) {
    3726           3 :                 struct snapraid_filter* filter = i->data;
    3727           3 :                 msg_verbose("\t%s%s\n", filter->pattern, filter->is_dir ? "/" : "");
    3728             :         }
    3729           9 :         if (filter_missing)
    3730           2 :                 msg_verbose("\t<missing>\n");
    3731           9 :         if (filter_error)
    3732           2 :                 msg_verbose("\t<error>\n");
    3733             : 
    3734             :         /* for each disk */
    3735          63 :         for (i = state->disklist; i != 0; i = i->next) {
    3736             :                 tommy_node* j;
    3737          54 :                 struct snapraid_disk* disk = i->data;
    3738             : 
    3739             :                 /* if we filter for presence, we have to access the disk, so better to print something */
    3740          54 :                 if (filter_missing)
    3741          12 :                         msg_progress("Scanning disk %s...\n", disk->name);
    3742             : 
    3743             :                 /* for each file */
    3744      133944 :                 for (j = tommy_list_head(&disk->filelist); j != 0; j = j->next) {
    3745      133890 :                         struct snapraid_file* file = j->data;
    3746             : 
    3747      133890 :                         if (filter_path(filterlist_disk, 0, disk->name, file->sub) != 0
    3748       80982 :                                 || filter_path(filterlist_file, 0, disk->name, file->sub) != 0
    3749       78138 :                                 || filter_existence(filter_missing, disk->dir, file->sub) != 0
    3750       38698 :                                 || filter_correctness(filter_error, &state->infoarr, disk, file) != 0
    3751             :                         ) {
    3752      109404 :                                 file_flag_set(file, FILE_IS_EXCLUDED);
    3753             :                         }
    3754             :                 }
    3755             : 
    3756             :                 /* for each link */
    3757        4414 :                 for (j = tommy_list_head(&disk->linklist); j != 0; j = j->next) {
    3758        4360 :                         struct snapraid_link* slink = j->data;
    3759             : 
    3760        4360 :                         if (filter_path(filterlist_disk, 0, disk->name, slink->sub) != 0
    3761        2641 :                                 || filter_path(filterlist_file, 0, disk->name, slink->sub) != 0
    3762        2536 :                                 || filter_existence(filter_missing, disk->dir, slink->sub) != 0
    3763             :                         ) {
    3764        3221 :                                 link_flag_set(slink, FILE_IS_EXCLUDED);
    3765             :                         }
    3766             :                 }
    3767             : 
    3768             :                 /* for each dir */
    3769          82 :                 for (j = tommy_list_head(&disk->dirlist); j != 0; j = j->next) {
    3770          28 :                         struct snapraid_dir* dir = j->data;
    3771             : 
    3772          28 :                         if (filter_dir(filterlist_disk, 0, disk->name, dir->sub) != 0
    3773          28 :                                 || filter_dir(filterlist_file, 0, disk->name, dir->sub) != 0
    3774          28 :                                 || filter_existence(filter_missing, disk->dir, dir->sub) != 0
    3775             :                         ) {
    3776           8 :                                 dir_flag_set(dir, FILE_IS_EXCLUDED);
    3777             :                         }
    3778             :                 }
    3779             :         }
    3780             : 
    3781             :         /* if we are filtering by disk */
    3782           9 :         if (!tommy_list_empty(filterlist_disk)) {
    3783             :                 /* for each parity disk */
    3784          20 :                 for (l = 0; l < state->level; ++l) {
    3785             :                         /* check if the parity is excluded by name */
    3786          16 :                         if (filter_path(filterlist_disk, 0, lev_config_name(l), 0) != 0) {
    3787             :                                 /* excluded the parity from further operation */
    3788          11 :                                 state->parity[l].is_excluded = 1;
    3789             :                         }
    3790             :                 }
    3791             :         } else {
    3792             :                 /* if we are filtering by file, exclude all parity */
    3793           5 :                 if (filter_missing || !tommy_list_empty(filterlist_file)) {
    3794             :                         /* for each parity disk */
    3795          16 :                         for (l = 0; l < state->level; ++l) {
    3796          13 :                                 state->parity[l].is_excluded = 1;
    3797             :                         }
    3798             :                 }
    3799             :         }
    3800             : }
    3801             : 
    3802         216 : int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, block_off_t blockmax, block_off_t countmax)
    3803             : {
    3804             :         time_t now;
    3805             : 
    3806         216 :         if (state->opt.gui) {
    3807           5 :                 log_tag("run:begin:%u:%u:%u\n", blockstart, blockmax, countmax);
    3808           5 :                 log_flush();
    3809             :         }
    3810             : 
    3811         216 :         now = time(0);
    3812             : 
    3813         216 :         state->progress_whole_start = now;
    3814             : 
    3815         216 :         state->progress_tick = 0;
    3816         216 :         state->progress_ptr = 0;
    3817         216 :         state->progress_wasted = 0;
    3818             : 
    3819             :         /* stop if requested */
    3820         216 :         if (global_interrupt) {
    3821             :                 /* LCOV_EXCL_START */
    3822             :                 if (!state->opt.gui) {
    3823             :                         msg_status("Not starting for interruption\n");
    3824             :                 }
    3825             :                 log_tag("sigint:0: SIGINT received\n");
    3826             :                 log_flush();
    3827             :                 return 0;
    3828             :                 /* LCOV_EXCL_STOP */
    3829             :         }
    3830             : 
    3831         216 :         return 1;
    3832             : }
    3833             : 
    3834         216 : void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize)
    3835             : {
    3836         216 :         if (state->opt.gui) {
    3837           5 :                 log_tag("run:end\n");
    3838           5 :                 log_flush();
    3839         211 :         } else if (countmax == 0) {
    3840          19 :                 msg_status("Nothing to do\n");
    3841             :         } else {
    3842             :                 time_t now;
    3843             :                 time_t elapsed;
    3844             : 
    3845         192 :                 unsigned countsize_MB = (countsize + MEGA - 1) / MEGA;
    3846             : 
    3847         192 :                 now = time(0);
    3848             : 
    3849         192 :                 elapsed = now - state->progress_whole_start - state->progress_wasted;
    3850             : 
    3851         192 :                 msg_bar("%u%% completed, %u MB processed", countpos * 100 / countmax, countsize_MB);
    3852         192 :                 if (elapsed >= 60)
    3853           0 :                         msg_bar(" in %u:%02u", (unsigned)(elapsed / 3600), (unsigned)((elapsed % 3600) / 60));
    3854         192 :                 msg_bar("\n");
    3855         192 :                 msg_flush();
    3856             :         }
    3857         216 : }
    3858             : 
    3859           1 : void state_progress_stop(struct snapraid_state* state)
    3860             : {
    3861             :         time_t now;
    3862             : 
    3863           1 :         now = time(0);
    3864             : 
    3865           1 :         if (!state->opt.gui) {
    3866           1 :                 msg_bar("\n");
    3867           1 :                 msg_flush();
    3868             :         }
    3869             : 
    3870           1 :         state->progress_interruption = now;
    3871           1 : }
    3872             : 
    3873           1 : void state_progress_restart(struct snapraid_state* state)
    3874             : {
    3875             :         time_t now;
    3876             : 
    3877           1 :         now = time(0);
    3878             : 
    3879             :         /* reset the progress counter */
    3880           1 :         state->progress_tick = 0;
    3881           1 :         state->progress_ptr = 0;
    3882             : 
    3883           1 :         if (now >= state->progress_interruption) /* avoid degenerated cases when the clock is manually adjusted */
    3884           1 :                 state->progress_wasted += now - state->progress_interruption;
    3885           1 : }
    3886             : 
    3887             : #define PROGRESS_CLEAR "          "
    3888             : 
    3889     1049984 : int state_progress(struct snapraid_state* state, block_off_t blockpos, block_off_t countpos, block_off_t countmax, data_off_t countsize)
    3890             : {
    3891             :         time_t now;
    3892             :         int pred;
    3893             : 
    3894     1049984 :         now = time(0);
    3895             : 
    3896             :         /* previous position */
    3897     1049984 :         pred = state->progress_ptr + PROGRESS_MAX - 1;
    3898     1049984 :         if (pred >= PROGRESS_MAX)
    3899     1049790 :                 pred -= PROGRESS_MAX;
    3900             : 
    3901             :         /* if the previous measure is different */
    3902     1049984 :         if (state->progress_tick == 0
    3903     1049790 :                 || state->progress_time[pred] != now
    3904             :         ) {
    3905             :                 uint64_t tick_total;
    3906             :                 uint64_t tick_cpu;
    3907             :                 time_t elapsed;
    3908         274 :                 unsigned out_perc = 0;
    3909         274 :                 unsigned out_speed = 0;
    3910         274 :                 unsigned out_cpu = 0;
    3911         274 :                 unsigned out_eta = 0;
    3912             : 
    3913         274 :                 tick_total = state->tick_cpu + state->tick_io;
    3914         274 :                 tick_cpu = state->tick_cpu;
    3915         274 :                 elapsed = now - state->progress_whole_start - state->progress_wasted;
    3916             : 
    3917             :                 /* completion percentage */
    3918         274 :                 if (countmax)
    3919         274 :                         out_perc = countpos * 100 / countmax;
    3920             : 
    3921             :                 /* if we have at least 5 measures */
    3922         274 :                 if (state->progress_tick >= 5
    3923             :                         /* or if we are running in test mode, with at least one measure */
    3924         273 :                         || (state->opt.force_progress && state->progress_tick >= 1)
    3925             :                 ) {
    3926             :                         int oldest;
    3927             :                         int past;
    3928             :                         time_t delta_time;
    3929             :                         block_off_t delta_pos;
    3930             :                         data_off_t delta_size;
    3931             :                         uint64_t delta_tick_total;
    3932             :                         uint64_t delta_tick_cpu;
    3933             : 
    3934             :                         /* number of past measures */
    3935          79 :                         past = state->progress_tick;
    3936             : 
    3937             :                         /* drop the oldest ones, to promptly */
    3938             :                         /* skip the startup phase */
    3939          79 :                         past -= past / 5;
    3940             : 
    3941             :                         /* check how much we can go in the past */
    3942          79 :                         if (past >= PROGRESS_MAX) {
    3943             :                                 /* the vector is filled, so we are already in position */
    3944             :                                 /* to get the possible oldest one */
    3945           0 :                                 oldest = state->progress_ptr;
    3946             :                         } else {
    3947             :                                 /* go backward the number of positions selected */
    3948          79 :                                 oldest = state->progress_ptr + PROGRESS_MAX - past;
    3949          79 :                                 if (oldest >= PROGRESS_MAX)
    3950          79 :                                         oldest -= PROGRESS_MAX;
    3951             :                         }
    3952             : 
    3953          79 :                         delta_time = now - state->progress_time[oldest];
    3954          79 :                         delta_pos = countpos - state->progress_pos[oldest];
    3955          79 :                         delta_size = countsize - state->progress_size[oldest];
    3956          79 :                         delta_tick_total = tick_total - state->progress_tick_total[oldest];
    3957          79 :                         delta_tick_cpu = tick_cpu - state->progress_tick_cpu[oldest];
    3958             : 
    3959             :                         /* estimate the speed in MB/s */
    3960          79 :                         if (delta_time != 0)
    3961          79 :                                 out_speed = (unsigned)(delta_size / MEGA / delta_time);
    3962             : 
    3963             :                         /* estimate the cpu usage percentage */
    3964          79 :                         if (delta_tick_total != 0)
    3965           9 :                                 out_cpu = (unsigned)(delta_tick_cpu * 100U / delta_tick_total);
    3966             : 
    3967             :                         /* estimate the remaining time in minutes */
    3968          79 :                         if (delta_pos != 0)
    3969          79 :                                 out_eta = (countmax - countpos) * delta_time / (60 * delta_pos);
    3970             :                 }
    3971             : 
    3972         274 :                 if (state->opt.gui) {
    3973           7 :                         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);
    3974           7 :                         log_flush();
    3975             :                 } else {
    3976         267 :                         msg_bar("%u%%, %u MB", out_perc, (unsigned)(countsize / MEGA));
    3977         267 :                         if (out_speed)
    3978          64 :                                 msg_bar(", %u MB/s", out_speed);
    3979         267 :                         if (out_cpu)
    3980           9 :                                 msg_bar(", CPU %u%%", out_cpu);
    3981         267 :                         if (out_eta)
    3982           2 :                                 msg_bar(", %u:%02u ETA", out_eta / 60, out_eta % 60);
    3983         267 :                         msg_bar("%s\r", PROGRESS_CLEAR);
    3984         267 :                         msg_flush();
    3985             :                 }
    3986             : 
    3987             :                 /* store the new measure */
    3988         274 :                 state->progress_time[state->progress_ptr] = now;
    3989         274 :                 state->progress_pos[state->progress_ptr] = countpos;
    3990         274 :                 state->progress_size[state->progress_ptr] = countsize;
    3991         274 :                 state->progress_tick_cpu[state->progress_ptr] = tick_cpu;
    3992         274 :                 state->progress_tick_total[state->progress_ptr] = tick_total;
    3993             : 
    3994             :                 /* next position */
    3995         274 :                 ++state->progress_ptr;
    3996         274 :                 if (state->progress_ptr >= PROGRESS_MAX)
    3997           0 :                         state->progress_ptr -= PROGRESS_MAX;
    3998             : 
    3999             :                 /* one more measure */
    4000         274 :                 ++state->progress_tick;
    4001             :         }
    4002             : 
    4003             :         /* stop if requested */
    4004     1049984 :         if (global_interrupt) {
    4005             :                 /* LCOV_EXCL_START */
    4006             :                 if (!state->opt.gui) {
    4007             :                         log_fatal("\n");
    4008             :                         log_fatal("Stopping for interruption at block %u\n", blockpos);
    4009             :                 }
    4010             :                 log_tag("sigint:%u: SIGINT received\n", blockpos);
    4011             :                 log_flush();
    4012             :                 return 1;
    4013             :                 /* LCOV_EXCL_STOP */
    4014             :         }
    4015             : 
    4016     1049984 :         return 0;
    4017             : }
    4018             : 
    4019          91 : void state_usage_print(struct snapraid_state* state)
    4020             : {
    4021             :         uint64_t tick_total;
    4022             :         uint64_t tick_max;
    4023             :         tommy_node* i;
    4024             :         unsigned l;
    4025             : 
    4026          91 :         tick_max = 0;
    4027          91 :         tick_total = 0;
    4028         635 :         for (i = state->disklist; i != 0; i = i->next) {
    4029         544 :                 struct snapraid_disk* disk = i->data;
    4030         544 :                 tick_total += disk->tick;
    4031         544 :                 if (disk->tick > tick_max)
    4032          96 :                         tick_max = disk->tick;
    4033             :         }
    4034         622 :         for (l = 0; l < state->level; ++l) {
    4035         531 :                 tick_total += state->parity[l].tick;
    4036         531 :                 if (state->parity[l].tick > tick_max)
    4037          20 :                         tick_max = state->parity[l].tick;
    4038             :         }
    4039             : 
    4040          91 :         if (!tick_total)
    4041         110 :                 return;
    4042             : 
    4043          72 :         if (msg_level >= MSG_PROGRESS) {
    4044             :                 /* here we don't use msg_progress() because it doesn't allow partial output */
    4045           1 :                 printf("Time for disk:");
    4046           7 :                 for (i = state->disklist; i != 0; i = i->next) {
    4047           6 :                         struct snapraid_disk* disk = i->data;
    4048           6 :                         if (disk->tick == tick_max)
    4049           1 :                                 printf(" %s:", disk->name);
    4050             :                         else
    4051           5 :                                 printf(" ");
    4052           6 :                         printf("%" PRIu64 "%%", disk->tick * 100U / tick_total);
    4053             :                 }
    4054           1 :                 printf("\n");
    4055             : 
    4056           1 :                 printf("Time for parity:");
    4057           7 :                 for (l = 0; l < state->level; ++l) {
    4058           6 :                         if (state->parity[l].tick == tick_max)
    4059           0 :                                 printf(" %s:", lev_config_name(l));
    4060             :                         else
    4061           6 :                                 printf(" ");
    4062           6 :                         printf("%" PRIu64 "%%", state->parity[l].tick * 100U / tick_total);
    4063             :                 }
    4064           1 :                 printf("\n");
    4065             :         }
    4066             : }
    4067             : 
    4068         481 : void state_fscheck(struct snapraid_state* state, const char* ope)
    4069             : {
    4070             :         tommy_node* i;
    4071             : 
    4072             :         /* check the file-system on all disks */
    4073        3361 :         for (i = state->disklist; i != 0; i = i->next) {
    4074        2880 :                 struct snapraid_disk* disk = i->data;
    4075             : 
    4076        2880 :                 if (fs_check(disk) != 0) {
    4077             :                         /* LCOV_EXCL_START */
    4078             :                         log_fatal("Internal inconsistency in file-system for disk '%s' %s\n", disk->name, ope);
    4079             :                         os_abort();
    4080             :                         /* LCOV_EXCL_STOP */
    4081             :                 }
    4082             :         }
    4083         481 : }
    4084             : 
    4085           1 : void generate_configuration(const char* path)
    4086             : {
    4087             :         struct snapraid_state state;
    4088             :         struct snapraid_content* content;
    4089             :         unsigned i;
    4090             :         tommy_node* j;
    4091             : 
    4092           1 :         state_init(&state);
    4093             : 
    4094             :         /* mark that we are without a configuration file */
    4095           1 :         state.no_conf = 1;
    4096             : 
    4097             :         /* create the dummy content entry */
    4098           1 :         content = content_alloc(path, -1);
    4099             : 
    4100             :         /* adds the content entry */
    4101           1 :         tommy_list_insert_tail(&state.contentlist, &content->node, content);
    4102             : 
    4103             :         /* read the content file */
    4104           1 :         state_read(&state);
    4105             : 
    4106             :         /* output a dummy configuration file */
    4107           1 :         printf("# Configuration file generated from %s\n", path);
    4108           1 :         printf("\n");
    4109           1 :         printf("# Use this blocksize\n");
    4110           1 :         printf("blocksize %u\n", state.block_size / 1024);
    4111           1 :         printf("\n");
    4112           7 :         for (i = 0; i < state.level; ++i) {
    4113           6 :                 printf("# Set the correct path for the %s file\n", lev_name(i));
    4114           6 :                 if (state.parity[i].uuid[0])
    4115           6 :                         printf("# The file was in the disk with id '%s'\n", state.parity[i].uuid);
    4116           6 :                 printf("%s ENTER_HERE_THE_PARITY_FILE\n", lev_config_name(i));
    4117           6 :                 printf("\n");
    4118             :         }
    4119           1 :         printf("# Add any other content file\n");
    4120           1 :         printf("content %s\n", path);
    4121           1 :         printf("\n");
    4122           7 :         for (j = state.maplist; j; j = j->next) {
    4123           6 :                 struct snapraid_map* map = j->data;
    4124             :                 struct snapraid_disk* disk;
    4125           6 :                 printf("# Set the correct dir for disk '%s'\n", map->name);
    4126           6 :                 if (map->uuid[0])
    4127           6 :                         printf("# Disk '%s' is the one with id '%s'\n", map->name, map->uuid);
    4128           6 :                 disk = find_disk_by_name(&state, map->name);
    4129           6 :                 if (disk && disk->filelist) {
    4130           6 :                         struct snapraid_file* file = disk->filelist->data;
    4131           6 :                         if (file) {
    4132           6 :                                 printf("# and containing: %s\n", file->sub);
    4133             :                         }
    4134             :                 }
    4135           6 :                 printf("disk %s ENTER_HERE_THE_DIR\n", map->name);
    4136           6 :                 printf("\n");
    4137             :         }
    4138             : 
    4139           1 :         state_done(&state);
    4140           1 : }
    4141             : 

Generated by: LCOV version 1.10