LCOV - code coverage report
Current view: top level - cmdline - snapraid.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 446 474 94.1 %
Date: 2015-11-13 Functions: 8 8 100.0 %

          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 "snapraid.h"
      21             : #include "support.h"
      22             : #include "elem.h"
      23             : #include "import.h"
      24             : #include "search.h"
      25             : #include "state.h"
      26             : #include "raid/raid.h"
      27             : 
      28             : /****************************************************************************/
      29             : /* main */
      30             : 
      31           2 : void version(void)
      32             : {
      33           2 :         msg_status(PACKAGE " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
      34           2 : }
      35             : 
      36           1 : void usage(void)
      37             : {
      38           1 :         version();
      39             : 
      40           1 :         printf("Usage: " PACKAGE " status|diff|sync|scrub|list|dup|up|down|smart|pool|check|fix [options]\n");
      41           1 :         printf("\n");
      42           1 :         printf("Commands:\n");
      43           1 :         printf("  status Print the status of the array\n");
      44           1 :         printf("  diff   Show the changes that needs to be syncronized\n");
      45           1 :         printf("  sync   Syncronize the state of the array\n");
      46           1 :         printf("  scrub  Scrub the array\n");
      47           1 :         printf("  list   List the array content\n");
      48           1 :         printf("  dup    Find duplicate files\n");
      49           1 :         printf("  up     Spinup the array\n");
      50           1 :         printf("  down   Spindown the array\n");
      51           1 :         printf("  smart  SMART attributes of the array\n");
      52           1 :         printf("  pool   Create or update the virtual view of the array\n");
      53           1 :         printf("  check  Check the array\n");
      54           1 :         printf("  fix    Fix the array\n");
      55           1 :         printf("\n");
      56           1 :         printf("Options:\n");
      57           1 :         printf("  " SWITCH_GETOPT_LONG("-c, --conf FILE       ", "-c") "  Configuration file\n");
      58           1 :         printf("  " SWITCH_GETOPT_LONG("-f, --filter PATTERN  ", "-f") "  Process only files matching the pattern\n");
      59           1 :         printf("  " SWITCH_GETOPT_LONG("-d, --filter-disk NAME", "-f") "  Process only files in the specified disk\n");
      60           1 :         printf("  " SWITCH_GETOPT_LONG("-m, --filter-missing  ", "-m") "  Process only missing/deleted files\n");
      61           1 :         printf("  " SWITCH_GETOPT_LONG("-e, --filter-error    ", "-e") "  Process only files with errors\n");
      62           1 :         printf("  " SWITCH_GETOPT_LONG("-p, --plan PLAN       ", "-p") "  Define a scrub plan or percentage\n");
      63           1 :         printf("  " SWITCH_GETOPT_LONG("-o, --older-than DAYS ", "-o") "  Process only the older part of the array\n");
      64           1 :         printf("  " SWITCH_GETOPT_LONG("-i, --import DIR      ", "-i") "  Import deleted files\n");
      65           1 :         printf("  " SWITCH_GETOPT_LONG("-l, --log FILE        ", "-l") "  Log file. Default none\n");
      66           1 :         printf("  " SWITCH_GETOPT_LONG("-a, --audit-only      ", "-a") "  Check only file data and not parity\n");
      67           1 :         printf("  " SWITCH_GETOPT_LONG("-h, --pre-hash        ", "-h") "  Pre hash all the new data\n");
      68           1 :         printf("  " SWITCH_GETOPT_LONG("-Z, --force-zero      ", "-Z") "  Force synching of files that get zero size\n");
      69           1 :         printf("  " SWITCH_GETOPT_LONG("-E, --force-empty     ", "-E") "  Force synching of disks that get empty\n");
      70           1 :         printf("  " SWITCH_GETOPT_LONG("-U, --force-uuid      ", "-U") "  Force commands on disks with uuid changed\n");
      71           1 :         printf("  " SWITCH_GETOPT_LONG("-D, --force-device    ", "-D") "  Force commands with unaccessible/shared disks\n");
      72           1 :         printf("  " SWITCH_GETOPT_LONG("-N, --force-nocopy    ", "-N") "  Force commands disabling the copy detection\n");
      73           1 :         printf("  " SWITCH_GETOPT_LONG("-F, --force-full      ", "-F") "  Force commands requiring a full sync\n");
      74           1 :         printf("  " SWITCH_GETOPT_LONG("-v, --verbose         ", "-v") "  Verbose\n");
      75           1 : }
      76             : 
      77         222 : void memory(void)
      78             : {
      79         222 :         log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter());
      80             : 
      81             :         /* size of the block */
      82         222 :         log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block)));
      83         222 :         log_tag("memory:chunk:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_chunk)));
      84         222 :         log_tag("memory:file:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_file)));
      85         222 :         log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link)));
      86         222 :         log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir)));
      87             : 
      88         222 :         msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter() / (1024 * 1024)));
      89         222 : }
      90             : 
      91             : /****************************************************************************/
      92             : /* log */
      93             : 
      94         247 : void log_open(const char* file)
      95             : {
      96             :         char path[PATH_MAX];
      97             :         const char* mode;
      98             :         char text_T[32];
      99             :         char text_D[32];
     100             :         time_t t;
     101             :         struct tm* tm;
     102             : 
     103             :         /* leave stdlog at 0 if not specified */
     104         247 :         if (file == 0)
     105         343 :                 return;
     106             : 
     107          77 :         t = time(0);
     108          77 :         tm = localtime(&t);
     109          77 :         if (tm) {
     110          77 :                 strftime(text_T, sizeof(text_T), "%H%M%S", tm);
     111          77 :                 strftime(text_D, sizeof(text_T), "%Y%m%d", tm);
     112             :         } else {
     113             :                 /* LCOV_EXCL_START */
     114             :                 strcpy(text_T, "invalid");
     115             :                 strcpy(text_D, "invalid");
     116             :                 /* LCOV_EXCL_STOP */
     117             :         }
     118             : 
     119             :         /* file mode */
     120          77 :         mode = "wt";
     121          77 :         if (*file == '>') {
     122           4 :                 ++file;
     123             : 
     124           4 :                 if (*file == '>') {
     125           1 :                         mode = "at";
     126           1 :                         ++file;
     127             :                 }
     128             : 
     129           4 :                 if (file[0] == '&' && file[1] == '1') {
     130           2 :                         stdlog = stdout;
     131           2 :                         return;
     132             :                 }
     133             : 
     134           2 :                 if (file[0] == '&' && file[1] == '2') {
     135           1 :                         stdlog = stderr;
     136           1 :                         return;
     137             :                 }
     138             :         }
     139             : 
     140             :         /* process the path */
     141         938 :         for (*path = 0; *file != 0; ) {
     142         790 :                 switch (*file) {
     143             :                 case '%' :
     144           2 :                         ++file;
     145           2 :                         switch (*file) {
     146             :                         case '%' :
     147           0 :                                 pathcatc(path, sizeof(path), '%');
     148           0 :                                 break;
     149             :                         case 'T' :
     150           1 :                                 pathcat(path, sizeof(path), text_T);
     151           1 :                                 break;
     152             :                         case 'D' :
     153           1 :                                 pathcat(path, sizeof(path), text_D);
     154           1 :                                 break;
     155             :                         default :
     156             :                                 /* LCOV_EXCL_START */
     157             :                                 log_fatal("Invalid type specifier '%c' in the log file.\n", *file);
     158             :                                 exit(EXIT_FAILURE);
     159             :                                 /* LCOV_EXCL_STOP */
     160             :                         }
     161           2 :                         break;
     162             :                 default :
     163         788 :                         pathcatc(path, sizeof(path), *file);
     164         788 :                         break;
     165             :                 }
     166         790 :                 ++file;
     167             :         }
     168             : 
     169          74 :         stdlog = fopen(path, mode);
     170          74 :         if (!stdlog) {
     171             :                 /* LCOV_EXCL_START */
     172             :                 log_fatal("Error opening the log file '%s'. %s.\n", path, strerror(errno));
     173             :                 exit(EXIT_FAILURE);
     174             :                 /* LCOV_EXCL_STOP */
     175             :         }
     176             : }
     177             : 
     178         225 : void log_close(const char* file)
     179             : {
     180         225 :         if (stdlog != stdout && stdlog != stderr && stdlog != 0) {
     181          74 :                 if (fclose(stdlog) != 0) {
     182             :                         /* LCOV_EXCL_START */
     183             :                         log_fatal("Error closing the log file '%s'. %s.\n", file, strerror(errno));
     184             :                         exit(EXIT_FAILURE);
     185             :                         /* LCOV_EXCL_STOP */
     186             :                 }
     187             :         }
     188             : 
     189         225 :         stdlog = 0;
     190         225 : }
     191             : 
     192             : /****************************************************************************/
     193             : /* config */
     194             : 
     195         257 : void config(char* conf, size_t conf_size, const char* argv0)
     196             : {
     197             : #ifdef _WIN32
     198             :         char* slash;
     199             : 
     200             :         pathimport(conf, conf_size, argv0);
     201             : 
     202             :         slash = strrchr(conf, '/');
     203             :         if (slash) {
     204             :                 slash[1] = 0;
     205             :                 pathcat(conf, conf_size, PACKAGE ".conf");
     206             :         } else {
     207             :                 pathcpy(conf, conf_size, PACKAGE ".conf");
     208             :         }
     209             : #else
     210             :         (void)argv0;
     211             : 
     212         257 :         pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf");
     213             : #endif
     214         257 : }
     215             : 
     216             : /****************************************************************************/
     217             : /* main */
     218             : 
     219             : #define OPT_TEST_SKIP_SELF 256
     220             : #define OPT_TEST_KILL_AFTER_SYNC 257
     221             : #define OPT_TEST_EXPECT_UNRECOVERABLE 258
     222             : #define OPT_TEST_EXPECT_RECOVERABLE 259
     223             : #define OPT_TEST_SKIP_SIGN 260
     224             : #define OPT_TEST_SKIP_FALLOCATE 261
     225             : #define OPT_TEST_SKIP_DEVICE 262
     226             : #define OPT_TEST_SKIP_SEQUENTIAL 263
     227             : #define OPT_TEST_FORCE_MURMUR3 264
     228             : #define OPT_TEST_FORCE_SPOOKY2 265
     229             : #define OPT_TEST_SKIP_LOCK 266
     230             : #define OPT_TEST_FORCE_ORDER_PHYSICAL 267
     231             : #define OPT_TEST_FORCE_ORDER_INODE 268
     232             : #define OPT_TEST_FORCE_ORDER_ALPHA 269
     233             : #define OPT_TEST_FORCE_ORDER_DIR 270
     234             : #define OPT_TEST_FORCE_SCRUB_AT 271
     235             : #define OPT_TEST_FORCE_SCRUB_EVEN 272
     236             : #define OPT_TEST_FORCE_CONTENT_WRITE 273
     237             : #define OPT_TEST_SKIP_CONTENT_CHECK 275
     238             : #define OPT_TEST_SKIP_PARITY_ACCESS 276
     239             : #define OPT_TEST_EXPECT_FAILURE 277
     240             : #define OPT_TEST_RUN 278
     241             : #define OPT_TEST_FORCE_SCAN_WINFIND 279
     242             : #define OPT_TEST_IMPORT_CONTENT 280
     243             : #define OPT_TEST_FORCE_PROGRESS 281
     244             : #define OPT_TEST_SKIP_DISK_ACCESS 282
     245             : #define OPT_TEST_FORCE_AUTOSAVE_AT 283
     246             : #define OPT_TEST_FAKE_DEVICE 284
     247             : #define OPT_TEST_EXPECT_NEED_SYNC 285
     248             : 
     249             : #if HAVE_GETOPT_LONG
     250             : struct option long_options[] = {
     251             :         { "conf", 1, 0, 'c' },
     252             :         { "filter", 1, 0, 'f' },
     253             :         { "filter-disk", 1, 0, 'd' },
     254             :         { "filter-missing", 0, 0, 'm' },
     255             :         { "filter-error", 0, 0, 'e' },
     256             :         { "percentage", 1, 0, 'p' }, /* legacy name for --plan */
     257             :         { "plan", 1, 0, 'p' },
     258             :         { "older-than", 1, 0, 'o' },
     259             :         { "start", 1, 0, 'S' },
     260             :         { "count", 1, 0, 'B' },
     261             :         { "error-limit", 1, 0, 'L' },
     262             :         { "import", 1, 0, 'i' },
     263             :         { "log", 1, 0, 'l' },
     264             :         { "force-zero", 0, 0, 'Z' },
     265             :         { "force-empty", 0, 0, 'E' },
     266             :         { "force-uuid", 0, 0, 'U' },
     267             :         { "force-device", 0, 0, 'D' },
     268             :         { "force-nocopy", 0, 0, 'N' },
     269             :         { "force-full", 0, 0, 'F' },
     270             :         { "audit-only", 0, 0, 'a' },
     271             :         { "pre-hash", 0, 0, 'h' },
     272             :         { "speed-test", 0, 0, 'T' }, /* undocumented speed test command */
     273             :         { "gen-conf", 1, 0, 'C' },
     274             :         { "verbose", 0, 0, 'v' },
     275             :         { "quiet", 0, 0, 'q' }, /* undocumented quiet option */
     276             :         { "gui", 0, 0, 'G' }, /* undocumented GUI interface option */
     277             :         { "help", 0, 0, 'H' },
     278             :         { "version", 0, 0, 'V' },
     279             : 
     280             :         /* The following are test specific options, DO NOT USE! */
     281             : 
     282             :         /* After syncing, do not write the new content file */
     283             :         { "test-kill-after-sync", 0, 0, OPT_TEST_KILL_AFTER_SYNC },
     284             : 
     285             :         /* Exit with failure if after check/fix there ARE NOT unrecoverable errors. */
     286             :         { "test-expect-unrecoverable", 0, 0, OPT_TEST_EXPECT_UNRECOVERABLE },
     287             : 
     288             :         /* Exit with failure if after check/fix there ARE NOT recoverable errors. */
     289             :         { "test-expect-recoverable", 0, 0, OPT_TEST_EXPECT_RECOVERABLE },
     290             : 
     291             :         /* Skip the initial self test */
     292             :         { "test-skip-self", 0, 0, OPT_TEST_SKIP_SELF },
     293             : 
     294             :         /* Skip the initial sign check when reading the content file */
     295             :         { "test-skip-sign", 0, 0, OPT_TEST_SKIP_SIGN },
     296             : 
     297             :         /* Skip the fallocate() when growing the parity files */
     298             :         { "test-skip-fallocate", 0, 0, OPT_TEST_SKIP_FALLOCATE },
     299             : 
     300             :         /* Skip the sequential hint when reading files */
     301             :         { "test-skip-sequential", 0, 0, OPT_TEST_SKIP_SEQUENTIAL },
     302             : 
     303             :         /* Skip the device check */
     304             :         { "test-skip-device", 0, 0, OPT_TEST_SKIP_DEVICE },
     305             : 
     306             :         /* Force Murmur3 hash */
     307             :         { "test-force-murmur3", 0, 0, OPT_TEST_FORCE_MURMUR3 },
     308             : 
     309             :         /* Force Spooky2 hash */
     310             :         { "test-force-spooky2", 0, 0, OPT_TEST_FORCE_SPOOKY2 },
     311             : 
     312             :         /* Skip the use of lock file */
     313             :         { "test-skip-lock", 0, 0, OPT_TEST_SKIP_LOCK },
     314             : 
     315             :         /* Force a sort order for files */
     316             :         { "test-force-order-physical", 0, 0, OPT_TEST_FORCE_ORDER_PHYSICAL },
     317             :         { "test-force-order-inode", 0, 0, OPT_TEST_FORCE_ORDER_INODE },
     318             :         { "test-force-order-alpha", 0, 0, OPT_TEST_FORCE_ORDER_ALPHA },
     319             :         { "test-force-order-dir", 0, 0, OPT_TEST_FORCE_ORDER_DIR },
     320             : 
     321             :         /* Force scrub of the specified number of blocks */
     322             :         { "test-force-scrub-at", 1, 0, OPT_TEST_FORCE_SCRUB_AT },
     323             : 
     324             :         /* Force scrub of all the even blocks. This is really for testing, don't try it */
     325             :         { "test-force-scrub-even", 0, 0, OPT_TEST_FORCE_SCRUB_EVEN },
     326             : 
     327             :         /* Force write of the content file even if no modification is done */
     328             :         { "test-force-content-write", 0, 0, OPT_TEST_FORCE_CONTENT_WRITE },
     329             : 
     330             :         /* Relax the checks done at the content file */
     331             :         { "test-skip-content-check", 0, 0, OPT_TEST_SKIP_CONTENT_CHECK },
     332             : 
     333             :         /* Skip the parity access */
     334             :         { "test-skip-parity-access", 0, 0, OPT_TEST_SKIP_PARITY_ACCESS },
     335             : 
     336             :         /* Exit generic failure */
     337             :         { "test-expect-failure", 0, 0, OPT_TEST_EXPECT_FAILURE },
     338             : 
     339             :         /* Exit generic need sync */
     340             :         { "test-expect-need-sync", 0, 0, OPT_TEST_EXPECT_NEED_SYNC },
     341             : 
     342             :         /* Run some command after loading the state and before the command */
     343             :         { "test-run", 1, 0, OPT_TEST_RUN },
     344             : 
     345             :         /* Use the FindFirst/Next approach in Windows to list files */
     346             :         { "test-force-scan-winfind", 0, 0, OPT_TEST_FORCE_SCAN_WINFIND },
     347             : 
     348             :         /* Alternative import working by data */
     349             :         { "test-import-content", 1, 0, OPT_TEST_IMPORT_CONTENT },
     350             : 
     351             :         /* Force immediate progress state update */
     352             :         { "test-force-progress", 0, 0, OPT_TEST_FORCE_PROGRESS },
     353             : 
     354             :         /* Skip the disk access */
     355             :         { "test-skip-disk-access", 0, 0, OPT_TEST_SKIP_DISK_ACCESS },
     356             : 
     357             :         /* Force autosave at the specified block */
     358             :         { "test-force-autosave-at", 1, 0, OPT_TEST_FORCE_AUTOSAVE_AT },
     359             : 
     360             :         /* Fake device data */
     361             :         { "test-fake-device", 0, 0, OPT_TEST_FAKE_DEVICE },
     362             : 
     363             :         { 0, 0, 0, 0 }
     364             : };
     365             : #endif
     366             : 
     367             : #define OPTIONS "c:f:d:mep:o:S:B:L:i:l:ZEUDNFahTC:vqHVG"
     368             : 
     369             : volatile int global_interrupt = 0;
     370             : 
     371             : /* LCOV_EXCL_START */
     372             : void signal_handler(int signum)
     373             : {
     374             :         (void)signum;
     375             : 
     376             :         /* report the request of interruption */
     377             :         global_interrupt = 1;
     378             : }
     379             : /* LCOV_EXCL_STOP */
     380             : 
     381         208 : void signal_init(void)
     382             : {
     383             : #if HAVE_SIGACTION
     384             :         struct sigaction sa;
     385             : 
     386         208 :         sa.sa_handler = signal_handler;
     387         208 :         sigemptyset(&sa.sa_mask);
     388             : 
     389             :         /* use the SA_RESTART to automatically restart interrupted system calls */
     390         208 :         sa.sa_flags = SA_RESTART;
     391             : 
     392         208 :         sigaction(SIGHUP, &sa, 0);
     393         208 :         sigaction(SIGTERM, &sa, 0);
     394         208 :         sigaction(SIGINT, &sa, 0);
     395         208 :         sigaction(SIGQUIT, &sa, 0);
     396             : #else
     397             :         signal(SIGINT, signal_handler);
     398             : #endif
     399         208 : }
     400             : 
     401             : #define OPERATION_DIFF 0
     402             : #define OPERATION_SYNC 1
     403             : #define OPERATION_CHECK 2
     404             : #define OPERATION_FIX 3
     405             : #define OPERATION_DRY 4
     406             : #define OPERATION_DUP 5
     407             : #define OPERATION_LIST 6
     408             : #define OPERATION_POOL 7
     409             : #define OPERATION_REHASH 8
     410             : #define OPERATION_SCRUB 9
     411             : #define OPERATION_STATUS 10
     412             : #define OPERATION_REWRITE 11
     413             : #define OPERATION_READ 12
     414             : #define OPERATION_NANO 13
     415             : #define OPERATION_SPINUP 14
     416             : #define OPERATION_SPINDOWN 15
     417             : #define OPERATION_DEVICES 16
     418             : #define OPERATION_SMART 17
     419             : 
     420         257 : int main(int argc, char* argv[])
     421             : {
     422             :         int c;
     423             :         struct snapraid_option opt;
     424             :         char conf[PATH_MAX];
     425             :         struct snapraid_state state;
     426             :         int operation;
     427             :         block_off_t blockstart;
     428             :         block_off_t blockcount;
     429             :         int ret;
     430             :         tommy_list filterlist_file;
     431             :         tommy_list filterlist_disk;
     432             :         int filter_missing;
     433             :         int filter_error;
     434             :         int plan;
     435             :         int olderthan;
     436             :         char* e;
     437             :         const char* command;
     438             :         const char* import_timestamp;
     439             :         const char* import_content;
     440             :         const char* log_file;
     441             :         int lock;
     442             :         const char* gen_conf;
     443             :         const char* run;
     444             :         int speedtest;
     445             :         int period;
     446             :         time_t t;
     447             :         struct tm* tm;
     448             :         int i;
     449             : 
     450             :         /* defaults */
     451         257 :         config(conf, sizeof(conf), argv[0]);
     452         257 :         memset(&opt, 0, sizeof(opt));
     453         257 :         opt.io_error_limit = 100;
     454         257 :         blockstart = 0;
     455         257 :         blockcount = 0;
     456         257 :         tommy_list_init(&filterlist_file);
     457         257 :         tommy_list_init(&filterlist_disk);
     458         257 :         period = 1000;
     459         257 :         filter_missing = 0;
     460         257 :         filter_error = 0;
     461         257 :         plan = SCRUB_AUTO;
     462         257 :         olderthan = SCRUB_AUTO;
     463         257 :         import_timestamp = 0;
     464         257 :         import_content = 0;
     465         257 :         log_file = 0;
     466         257 :         lock = 0;
     467         257 :         gen_conf = 0;
     468         257 :         speedtest = 0;
     469         257 :         run = 0;
     470             : 
     471         257 :         opterr = 0;
     472        2722 :         while ((c =
     473             : #if HAVE_GETOPT_LONG
     474             :                 getopt_long(argc, argv, OPTIONS, long_options, 0))
     475             : #else
     476             :                 getopt(argc, argv, OPTIONS))
     477             : #endif
     478             :                 != EOF) {
     479        2213 :                 switch (c) {
     480             :                 case 'c' :
     481         250 :                         pathimport(conf, sizeof(conf), optarg);
     482         250 :                         break;
     483             :                 case 'f' : {
     484           6 :                         struct snapraid_filter* filter = filter_alloc_file(1, optarg);
     485           6 :                         if (!filter) {
     486             :                                 /* LCOV_EXCL_START */
     487             :                                 log_fatal("Invalid filter specification '%s'\n", optarg);
     488             :                                 log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
     489             :                                 exit(EXIT_FAILURE);
     490             :                                 /* LCOV_EXCL_STOP */
     491             :                         }
     492           3 :                         tommy_list_insert_tail(&filterlist_file, &filter->node, filter);
     493           3 :                 } break;
     494             :                 case 'd' : {
     495           8 :                         struct snapraid_filter* filter = filter_alloc_disk(1, optarg);
     496           8 :                         if (!filter) {
     497             :                                 /* LCOV_EXCL_START */
     498             :                                 log_fatal("Invalid filter specification '%s'\n", optarg);
     499             :                                 exit(EXIT_FAILURE);
     500             :                                 /* LCOV_EXCL_STOP */
     501             :                         }
     502           8 :                         tommy_list_insert_tail(&filterlist_disk, &filter->node, filter);
     503           8 :                 } break;
     504             :                 case 'm' :
     505           2 :                         filter_missing = 1;
     506           2 :                         opt.expected_missing = 1;
     507           2 :                         break;
     508             :                 case 'e' :
     509             :                         /* when processing only error, we filter both files and blocks */
     510             :                         /* and we apply fixes only to synced ones */
     511           2 :                         filter_error = 1;
     512           2 :                         opt.badonly = 1;
     513           2 :                         opt.syncedonly = 1;
     514           2 :                         break;
     515             :                 case 'p' :
     516           5 :                         if (strcmp(optarg, "bad") == 0) {
     517           1 :                                 plan = SCRUB_BAD;
     518           4 :                         } else if (strcmp(optarg, "new") == 0) {
     519           1 :                                 plan = SCRUB_NEW;
     520           3 :                         } else if (strcmp(optarg, "full") == 0) {
     521           2 :                                 plan = SCRUB_FULL;
     522             :                         } else {
     523           1 :                                 plan = strtoul(optarg, &e, 10);
     524           1 :                                 if (!e || *e || plan > 100) {
     525             :                                         /* LCOV_EXCL_START */
     526             :                                         log_fatal("Invalid plan/percentage '%s'\n", optarg);
     527             :                                         exit(EXIT_FAILURE);
     528             :                                         /* LCOV_EXCL_STOP */
     529             :                                 }
     530             :                         }
     531           5 :                         break;
     532             :                 case 'o' :
     533           1 :                         olderthan = strtoul(optarg, &e, 10);
     534           1 :                         if (!e || *e || olderthan > 1000) {
     535             :                                 /* LCOV_EXCL_START */
     536             :                                 log_fatal("Invalid number of days '%s'\n", optarg);
     537             :                                 exit(EXIT_FAILURE);
     538             :                                 /* LCOV_EXCL_STOP */
     539             :                         }
     540           1 :                         break;
     541             :                 case 'S' :
     542           2 :                         blockstart = strtoul(optarg, &e, 0);
     543           2 :                         if (!e || *e) {
     544             :                                 /* LCOV_EXCL_START */
     545             :                                 log_fatal("Invalid start position '%s'\n", optarg);
     546             :                                 exit(EXIT_FAILURE);
     547             :                                 /* LCOV_EXCL_STOP */
     548             :                         }
     549           2 :                         break;
     550             :                 case 'B' :
     551           7 :                         blockcount = strtoul(optarg, &e, 0);
     552           7 :                         if (!e || *e) {
     553             :                                 /* LCOV_EXCL_START */
     554             :                                 log_fatal("Invalid count number '%s'\n", optarg);
     555             :                                 exit(EXIT_FAILURE);
     556             :                                 /* LCOV_EXCL_STOP */
     557             :                         }
     558           7 :                         break;
     559             :                 case 'L' :
     560           0 :                         opt.io_error_limit = strtoul(optarg, &e, 0);
     561           0 :                         if (!e || *e) {
     562             :                                 /* LCOV_EXCL_START */
     563             :                                 log_fatal("Invalid error limit number '%s'\n", optarg);
     564             :                                 exit(EXIT_FAILURE);
     565             :                                 /* LCOV_EXCL_STOP */
     566             :                         }
     567           0 :                         break;
     568             :                 case 'i' :
     569           1 :                         if (import_timestamp) {
     570             :                                 /* LCOV_EXCL_START */
     571             :                                 log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_timestamp);
     572             :                                 exit(EXIT_FAILURE);
     573             :                                 /* LCOV_EXCL_STOP */
     574             :                         }
     575           1 :                         import_timestamp = optarg;
     576           1 :                         break;
     577             :                 case OPT_TEST_IMPORT_CONTENT :
     578           1 :                         if (import_content) {
     579             :                                 /* LCOV_EXCL_START */
     580             :                                 log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_content);
     581             :                                 exit(EXIT_FAILURE);
     582             :                                 /* LCOV_EXCL_STOP */
     583             :                         }
     584           1 :                         import_content = optarg;
     585           1 :                         break;
     586             :                 case 'l' :
     587          77 :                         if (log_file) {
     588             :                                 /* LCOV_EXCL_START */
     589             :                                 log_fatal("Log file '%s' already specified as '%s'\n", optarg, log_file);
     590             :                                 exit(EXIT_FAILURE);
     591             :                                 /* LCOV_EXCL_STOP */
     592             :                         }
     593          77 :                         log_file = optarg;
     594          77 :                         break;
     595             :                 case 'Z' :
     596           0 :                         opt.force_zero = 1;
     597           0 :                         break;
     598             :                 case 'E' :
     599           5 :                         opt.force_empty = 1;
     600           5 :                         break;
     601             :                 case 'U' :
     602           0 :                         opt.force_uuid = 1;
     603           0 :                         break;
     604             :                 case 'D' :
     605           1 :                         opt.force_device = 1;
     606           1 :                         break;
     607             :                 case 'N' :
     608           1 :                         opt.force_nocopy = 1;
     609           1 :                         break;
     610             :                 case 'F' :
     611           1 :                         opt.force_full = 1;
     612           1 :                         break;
     613             :                 case 'a' :
     614           6 :                         opt.auditonly = 1;
     615           6 :                         break;
     616             :                 case 'h' :
     617           7 :                         opt.prehash = 1;
     618           7 :                         break;
     619             :                 case 'v' :
     620          23 :                         ++msg_level;
     621          23 :                         break;
     622             :                 case 'q' :
     623         681 :                         --msg_level;
     624         681 :                         break;
     625             :                 case 'G' :
     626          23 :                         opt.gui = 1;
     627          23 :                         break;
     628             :                 case 'H' :
     629           1 :                         usage();
     630           1 :                         exit(EXIT_SUCCESS);
     631             :                 case 'V' :
     632           1 :                         version();
     633           1 :                         exit(EXIT_SUCCESS);
     634             :                 case 'T' :
     635           4 :                         speedtest = 1;
     636           4 :                         break;
     637             :                 case 'C' :
     638           1 :                         gen_conf = optarg;
     639           1 :                         break;
     640             :                 case OPT_TEST_KILL_AFTER_SYNC :
     641           9 :                         opt.kill_after_sync = 1;
     642           9 :                         break;
     643             :                 case OPT_TEST_EXPECT_UNRECOVERABLE :
     644          13 :                         opt.expect_unrecoverable = 1;
     645          13 :                         break;
     646             :                 case OPT_TEST_EXPECT_RECOVERABLE :
     647          23 :                         opt.expect_recoverable = 1;
     648          23 :                         break;
     649             :                 case OPT_TEST_SKIP_SELF :
     650         250 :                         opt.skip_self = 1;
     651         250 :                         break;
     652             :                 case OPT_TEST_SKIP_SIGN :
     653           0 :                         opt.skip_sign = 1;
     654           0 :                         break;
     655             :                 case OPT_TEST_SKIP_FALLOCATE :
     656           1 :                         opt.skip_fallocate = 1;
     657           1 :                         break;
     658             :                 case OPT_TEST_SKIP_SEQUENTIAL :
     659           1 :                         opt.skip_sequential = 1;
     660           1 :                         break;
     661             :                 case OPT_TEST_SKIP_DEVICE :
     662         257 :                         opt.skip_device = 1;
     663         257 :                         period = 50; /* reduce period of the speed test */
     664         257 :                         break;
     665             :                 case OPT_TEST_SKIP_CONTENT_CHECK :
     666           0 :                         opt.skip_content_check = 1;
     667           0 :                         break;
     668             :                 case OPT_TEST_SKIP_PARITY_ACCESS :
     669           0 :                         opt.skip_parity_access = 1;
     670           0 :                         break;
     671             :                 case OPT_TEST_SKIP_DISK_ACCESS :
     672           0 :                         opt.skip_disk_access = 1;
     673           0 :                         break;
     674             :                 case OPT_TEST_FORCE_MURMUR3 :
     675           1 :                         opt.force_murmur3 = 1;
     676           1 :                         break;
     677             :                 case OPT_TEST_FORCE_SPOOKY2 :
     678           1 :                         opt.force_spooky2 = 1;
     679           1 :                         break;
     680             :                 case OPT_TEST_SKIP_LOCK :
     681           0 :                         opt.skip_lock = 1;
     682           0 :                         break;
     683             :                 case OPT_TEST_FORCE_ORDER_PHYSICAL :
     684           2 :                         opt.force_order = SORT_PHYSICAL;
     685           2 :                         break;
     686             :                 case OPT_TEST_FORCE_ORDER_INODE :
     687           0 :                         opt.force_order = SORT_INODE;
     688           0 :                         break;
     689             :                 case OPT_TEST_FORCE_ORDER_ALPHA :
     690         248 :                         opt.force_order = SORT_ALPHA;
     691         248 :                         break;
     692             :                 case OPT_TEST_FORCE_ORDER_DIR :
     693           0 :                         opt.force_order = SORT_DIR;
     694           0 :                         break;
     695             :                 case OPT_TEST_FORCE_SCRUB_AT :
     696           4 :                         opt.force_scrub_at = atoi(optarg);
     697           4 :                         break;
     698             :                 case OPT_TEST_FORCE_SCRUB_EVEN :
     699           1 :                         opt.force_scrub_even = 1;
     700           1 :                         break;
     701             :                 case OPT_TEST_FORCE_CONTENT_WRITE :
     702           0 :                         opt.force_content_write = 1;
     703           0 :                         break;
     704             :                 case OPT_TEST_EXPECT_FAILURE :
     705             :                         /* invert the exit codes */
     706          19 :                         exit_success = 1;
     707          19 :                         exit_failure = 0;
     708          19 :                         break;
     709             :                 case OPT_TEST_EXPECT_NEED_SYNC :
     710             :                         /* invert the exit codes */
     711           6 :                         exit_success = 1;
     712           6 :                         exit_sync_needed = 0;
     713           6 :                         break;
     714             :                 case OPT_TEST_RUN :
     715          10 :                         run = optarg;
     716          10 :                         break;
     717             :                 case OPT_TEST_FORCE_SCAN_WINFIND :
     718           0 :                         opt.force_scan_winfind = 1;
     719           0 :                         break;
     720             :                 case OPT_TEST_FORCE_PROGRESS :
     721         248 :                         opt.force_progress = 1;
     722         248 :                         break;
     723             :                 case OPT_TEST_FORCE_AUTOSAVE_AT :
     724           1 :                         opt.force_autosave_at = atoi(optarg);
     725           1 :                         break;
     726             :                 case OPT_TEST_FAKE_DEVICE :
     727           1 :                         opt.fake_device = 1;
     728           1 :                         break;
     729             :                 default :
     730             :                         /* LCOV_EXCL_START */
     731             :                         log_fatal("Unknown option '%c'\n", (char)c);
     732             :                         exit(EXIT_FAILURE);
     733             :                         /* LCOV_EXCL_STOP */
     734             :                 }
     735             :         }
     736             : 
     737         252 :         os_init(opt.force_scan_winfind);
     738         252 :         raid_init();
     739         252 :         crc32c_init();
     740             : 
     741         252 :         if (speedtest != 0) {
     742           4 :                 speed(period);
     743           4 :                 os_done();
     744           4 :                 exit(EXIT_SUCCESS);
     745             :         }
     746             : 
     747         248 :         if (gen_conf != 0) {
     748           1 :                 generate_configuration(gen_conf);
     749           1 :                 os_done();
     750           1 :                 exit(EXIT_SUCCESS);
     751             :         }
     752             : 
     753         247 :         if (optind + 1 != argc) {
     754             :                 /* LCOV_EXCL_START */
     755             :                 usage();
     756             :                 exit(EXIT_FAILURE);
     757             :                 /* LCOV_EXCL_STOP */
     758             :         }
     759             : 
     760         247 :         command = argv[optind];
     761         247 :         if (strcmp(command, "diff") == 0) {
     762           7 :                 operation = OPERATION_DIFF;
     763         240 :         } else if (strcmp(argv[optind], "sync") == 0) {
     764          74 :                 operation = OPERATION_SYNC;
     765         166 :         } else if (strcmp(argv[optind], "check") == 0) {
     766          75 :                 operation = OPERATION_CHECK;
     767          91 :         } else if (strcmp(argv[optind], "fix") == 0) {
     768          46 :                 operation = OPERATION_FIX;
     769          45 :         } else if (strcmp(argv[optind], "test-dry") == 0) {
     770           2 :                 operation = OPERATION_DRY;
     771          43 :         } else if (strcmp(argv[optind], "dup") == 0) {
     772           3 :                 operation = OPERATION_DUP;
     773          40 :         } else if (strcmp(argv[optind], "list") == 0) {
     774           3 :                 operation = OPERATION_LIST;
     775          37 :         } else if (strcmp(argv[optind], "pool") == 0) {
     776           3 :                 operation = OPERATION_POOL;
     777          34 :         } else if (strcmp(argv[optind], "rehash") == 0) {
     778           1 :                 operation = OPERATION_REHASH;
     779          33 :         } else if (strcmp(argv[optind], "scrub") == 0) {
     780          11 :                 operation = OPERATION_SCRUB;
     781          22 :         } else if (strcmp(argv[optind], "status") == 0) {
     782          14 :                 operation = OPERATION_STATUS;
     783           8 :         } else if (strcmp(argv[optind], "test-rewrite") == 0) {
     784           1 :                 operation = OPERATION_REWRITE;
     785           7 :         } else if (strcmp(argv[optind], "test-read") == 0) {
     786           1 :                 operation = OPERATION_READ;
     787           6 :         } else if (strcmp(argv[optind], "test-nano") == 0) {
     788           1 :                 operation = OPERATION_NANO;
     789           5 :         } else if (strcmp(argv[optind], "up") == 0) {
     790           1 :                 operation = OPERATION_SPINUP;
     791           4 :         } else if (strcmp(argv[optind], "down") == 0) {
     792           1 :                 operation = OPERATION_SPINDOWN;
     793           3 :         } else if (strcmp(argv[optind], "devices") == 0) {
     794           1 :                 operation = OPERATION_DEVICES;
     795           2 :         } else if (strcmp(argv[optind], "smart") == 0) {
     796           2 :                 operation = OPERATION_SMART;
     797             :         } else {
     798             :                 /* LCOV_EXCL_START */
     799             :                 log_fatal("Unknown command '%s'\n", argv[optind]);
     800             :                 exit(EXIT_FAILURE);
     801             :                 /* LCOV_EXCL_STOP */
     802             :         }
     803             : 
     804             :         /* check options compatibility */
     805         247 :         switch (operation) {
     806             :         case OPERATION_CHECK :
     807          75 :                 break;
     808             :         default :
     809         172 :                 if (opt.auditonly) {
     810             :                         /* LCOV_EXCL_START */
     811             :                         log_fatal("You cannot use -a, --audit-only with the '%s' command\n", command);
     812             :                         exit(EXIT_FAILURE);
     813             :                         /* LCOV_EXCL_STOP */
     814             :                 }
     815             :         }
     816             : 
     817         247 :         switch (operation) {
     818             :         case OPERATION_FIX :
     819          46 :                 break;
     820             :         default :
     821         201 :                 if (opt.force_device) {
     822             :                         /* LCOV_EXCL_START */
     823             :                         log_fatal("You cannot use -D, --force-device with the '%s' command\n", command);
     824             :                         exit(EXIT_FAILURE);
     825             :                         /* LCOV_EXCL_STOP */
     826             :                 }
     827             :         }
     828             : 
     829         247 :         switch (operation) {
     830             :         case OPERATION_SYNC :
     831             :         case OPERATION_CHECK :
     832             :         case OPERATION_FIX :
     833         195 :                 break;
     834             :         default :
     835          52 :                 if (opt.force_nocopy) {
     836             :                         /* LCOV_EXCL_START */
     837             :                         log_fatal("You cannot use -N, --force-nocopy with the '%s' command\n", command);
     838             :                         exit(EXIT_FAILURE);
     839             :                         /* LCOV_EXCL_STOP */
     840             :                 }
     841             :         }
     842             : 
     843         247 :         switch (operation) {
     844             :         case OPERATION_SYNC :
     845          74 :                 break;
     846             :         default :
     847         173 :                 if (opt.prehash) {
     848             :                         /* LCOV_EXCL_START */
     849             :                         log_fatal("You cannot use -h, --pre-hash with the '%s' command\n", command);
     850             :                         exit(EXIT_FAILURE);
     851             :                         /* LCOV_EXCL_STOP */
     852             :                 }
     853             : 
     854         173 :                 if (opt.force_full) {
     855             :                         /* LCOV_EXCL_START */
     856             :                         log_fatal("You cannot use -F, --force-full with the '%s' command\n", command);
     857             :                         exit(EXIT_FAILURE);
     858             :                         /* LCOV_EXCL_STOP */
     859             :                 }
     860             :         }
     861             : 
     862         247 :         if (opt.force_full && opt.force_nocopy) {
     863             :                 /* LCOV_EXCL_START */
     864             :                 log_fatal("You cannot use the -F, --force-full and -N, --force-nocopy options at the same time\n");
     865             :                 exit(EXIT_FAILURE);
     866             :                 /* LCOV_EXCL_STOP */
     867             :         }
     868             : 
     869         247 :         if (opt.prehash && opt.force_nocopy) {
     870             :                 /* LCOV_EXCL_START */
     871             :                 log_fatal("You cannot use the -h, --pre-hash and -N, --force-nocopy options at the same time\n");
     872             :                 exit(EXIT_FAILURE);
     873             :                 /* LCOV_EXCL_STOP */
     874             :         }
     875             : 
     876         247 :         switch (operation) {
     877             :         case OPERATION_CHECK :
     878             :         case OPERATION_FIX :
     879             :         case OPERATION_DRY :
     880         123 :                 break;
     881             :         default :
     882         124 :                 if (!tommy_list_empty(&filterlist_file)) {
     883             :                         /* LCOV_EXCL_START */
     884             :                         log_fatal("You cannot use -f, --filter with the '%s' command\n", command);
     885             :                         exit(EXIT_FAILURE);
     886             :                         /* LCOV_EXCL_STOP */
     887             :                 }
     888         124 :                 if (!tommy_list_empty(&filterlist_disk)) {
     889             :                         /* LCOV_EXCL_START */
     890             :                         log_fatal("You cannot use -d, --filter-disk with the '%s' command\n", command);
     891             :                         exit(EXIT_FAILURE);
     892             :                         /* LCOV_EXCL_STOP */
     893             :                 }
     894         124 :                 if (filter_missing != 0) {
     895             :                         /* LCOV_EXCL_START */
     896             :                         log_fatal("You cannot use -m, --filter-missing with the '%s' command\n", command);
     897             :                         exit(EXIT_FAILURE);
     898             :                         /* LCOV_EXCL_STOP */
     899             :                 }
     900         124 :                 if (filter_error != 0) {
     901             :                         /* LCOV_EXCL_START */
     902             :                         log_fatal("You cannot use -e, --filter-error with the '%s' command\n", command);
     903             :                         exit(EXIT_FAILURE);
     904             :                         /* LCOV_EXCL_STOP */
     905             :                 }
     906             :         }
     907             : 
     908             :         /* errors must be always fixed on all disks */
     909             :         /* becasue we don't keep the information on what disk is the error */
     910         247 :         if (filter_error != 0 && !tommy_list_empty(&filterlist_disk)) {
     911             :                 /* LCOV_EXCL_START */
     912             :                 log_fatal("You cannot use -e, --filter-error and -d, --filter-disk at the same time\n");
     913             :                 exit(EXIT_FAILURE);
     914             :                 /* LCOV_EXCL_STOP */
     915             :         }
     916             : 
     917         247 :         switch (operation) {
     918             :         case OPERATION_CHECK :
     919             :         case OPERATION_FIX :
     920         121 :                 break;
     921             :         default :
     922         126 :                 if (import_timestamp != 0 || import_content != 0) {
     923             :                         /* LCOV_EXCL_START */
     924             :                         log_fatal("You cannot import with the '%s' command\n", command);
     925             :                         exit(EXIT_FAILURE);
     926             :                         /* LCOV_EXCL_STOP */
     927             :                 }
     928             :         }
     929             : 
     930         247 :         switch (operation) {
     931             :         case OPERATION_LIST :
     932             :         case OPERATION_DUP :
     933             :         case OPERATION_STATUS :
     934             :         case OPERATION_REWRITE :
     935             :         case OPERATION_READ :
     936             :         case OPERATION_REHASH :
     937             :         case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
     938             :                 /* avoid to check and access data disks if not needed */
     939          24 :                 opt.skip_disk_access = 1;
     940          24 :                 break;
     941             :         }
     942             : 
     943         247 :         switch (operation) {
     944             :         case OPERATION_DIFF :
     945             :         case OPERATION_LIST :
     946             :         case OPERATION_DUP :
     947             :         case OPERATION_POOL :
     948             :         case OPERATION_STATUS :
     949             :         case OPERATION_REWRITE :
     950             :         case OPERATION_READ :
     951             :         case OPERATION_REHASH :
     952             :         case OPERATION_NANO :
     953             :         case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
     954             :                 /* avoid to check and access parity disks if not needed */
     955          35 :                 opt.skip_parity_access = 1;
     956          35 :                 break;
     957             :         }
     958             : 
     959         247 :         switch (operation) {
     960             :         case OPERATION_FIX :
     961             :         case OPERATION_CHECK :
     962             :                 /* avoid to stop processing if a content file is not accessible */
     963         121 :                 opt.skip_content_access = 1;
     964         121 :                 break;
     965             :         }
     966             : 
     967         247 :         switch (operation) {
     968             :         case OPERATION_DIFF :
     969             :         case OPERATION_LIST :
     970             :         case OPERATION_DUP :
     971             :         case OPERATION_POOL :
     972             :         case OPERATION_NANO :
     973             :         case OPERATION_SPINUP :
     974             :         case OPERATION_SPINDOWN :
     975             :         case OPERATION_DEVICES :
     976             :         case OPERATION_SMART :
     977          22 :                 opt.skip_self = 1;
     978          22 :                 break;
     979             :         }
     980             : 
     981         247 :         switch (operation) {
     982             :         case OPERATION_DEVICES :
     983             :         case OPERATION_SMART :
     984             :                 /* we may need to use these commands during operations */
     985           3 :                 opt.skip_lock = 1;
     986           3 :                 break;
     987             :         }
     988             : 
     989             :         /* open the log file */
     990         247 :         log_open(log_file);
     991             : 
     992             :         /* print generic info into the log */
     993         247 :         t = time(0);
     994         247 :         tm = localtime(&t);
     995         247 :         log_tag("version:%s\n", PACKAGE_VERSION);
     996         247 :         log_tag("unixtime:%" PRIi64 "\n", (int64_t)t);
     997         247 :         if (tm) {
     998             :                 char datetime[64];
     999         247 :                 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", tm);
    1000         247 :                 log_tag("time:%s\n", datetime);
    1001             :         }
    1002         247 :         log_tag("command:%s\n", command);
    1003        3274 :         for (i = 0; i < argc; ++i)
    1004        3027 :                 log_tag("argv:%u:%s\n", i, argv[i]);
    1005         247 :         log_flush();
    1006             : 
    1007         247 :         if (!opt.skip_self)
    1008           1 :                 selftest();
    1009             : 
    1010         247 :         state_init(&state);
    1011             : 
    1012             :         /* read the configuration file */
    1013         247 :         state_config(&state, conf, command, &opt, &filterlist_disk);
    1014             : 
    1015             :         /* set the raid mode */
    1016         246 :         raid_mode(state.raid_mode);
    1017             : 
    1018             : #if HAVE_LOCKFILE
    1019             :         /* create the lock file */
    1020         246 :         if (!opt.skip_lock && state.lockfile[0]) {
    1021         243 :                 lock = lock_lock(state.lockfile);
    1022         243 :                 if (lock == -1) {
    1023             :                         /* LCOV_EXCL_START */
    1024             :                         if (errno != EWOULDBLOCK) {
    1025             :                                 log_fatal("Error creating the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
    1026             :                         } else {
    1027             :                                 log_fatal("The lock file '%s' is already locked!\n", state.lockfile);
    1028             :                                 log_fatal("SnapRAID is already in use!\n");
    1029             :                         }
    1030             :                         exit(EXIT_FAILURE);
    1031             :                         /* LCOV_EXCL_STOP */
    1032             :                 }
    1033             :         }
    1034             : #else
    1035             :         (void)lock;
    1036             : #endif
    1037             : 
    1038         246 :         if (operation == OPERATION_DIFF) {
    1039           7 :                 state_read(&state);
    1040             : 
    1041           7 :                 ret = state_diff(&state);
    1042             : 
    1043             :                 /* abort if sync needed */
    1044           7 :                 if (ret > 0)
    1045           6 :                         exit(EXIT_SYNC_NEEDED);
    1046         239 :         } else if (operation == OPERATION_SYNC) {
    1047             : 
    1048             :                 /* in the next state read ensures to clear all the past hashes in case */
    1049             :                 /* we are reading from an incomplete sync */
    1050             :                 /* The undeterminated hash are only for CHG/DELETED blocks for which we don't */
    1051             :                 /* know if the previous interrupted sync was able to update or not the parity. */
    1052             :                 /* The sync process instead needs to trust this information because it's used */
    1053             :                 /* to avoid to recompute the parity if all the input are equals as before. */
    1054             : 
    1055             :                 /* In these cases we don't know if the old state is still the one */
    1056             :                 /* stored inside the parity, because after an aborted sync, the parity */
    1057             :                 /* may be or may be not have been updated with the data that may be now */
    1058             :                 /* deleted. Then we reset the hash to a bogus value. */
    1059             : 
    1060             :                 /* An example for CHG blocks is: */
    1061             :                 /* - One file is added creating a CHG block with ZERO state */
    1062             :                 /* - Sync aborted after updating the parity to the new state, */
    1063             :                 /*   but without saving the content file representing this new BLK state. */
    1064             :                 /* - File is now deleted after the aborted sync */
    1065             :                 /* - Sync again, deleting the blocks overt the CHG ones */
    1066             :                 /*   with the hash of CHG blocks not represeting the real parity state */
    1067             : 
    1068             :                 /* An example for DELETED blocks is: */
    1069             :                 /* - One file is deleted creating DELETED blocks */
    1070             :                 /* - Sync aborted after, updating the parity to the new state, */
    1071             :                 /*   but without saving the content file representing this new EMPTY state. */
    1072             :                 /* - Another file is added again over the DELETE ones */
    1073             :                 /*   with the hash of DELETED blocks not represeting the real parity state */
    1074          74 :                 state.clear_past_hash = 1;
    1075             : 
    1076          74 :                 state_read(&state);
    1077             : 
    1078          73 :                 state_scan(&state);
    1079             : 
    1080             :                 /* refresh the size info before the content write */
    1081          72 :                 state_refresh(&state);
    1082             : 
    1083          72 :                 memory();
    1084             : 
    1085             :                 /* intercept signals while operating */
    1086          72 :                 signal_init();
    1087             : 
    1088             :                 /* save the new state before the sync */
    1089             :                 /* this allow to recover the case of the changes in the array after an aborted sync. */
    1090             : 
    1091             :                 /* for example, think at this case: */
    1092             :                 /* - add some files at the array */
    1093             :                 /* - run a sync command, it will recompute the parity adding the new files */
    1094             :                 /* - abort the sync command before it stores the new content file */
    1095             :                 /* - delete the not yet synced files from the array */
    1096             :                 /* - run a new sync command */
    1097             : 
    1098             :                 /* the new sync command has now way to know that the parity file was modified */
    1099             :                 /* because the files triggering these changes are now deleted */
    1100             :                 /* and they aren't listed in the content file */
    1101             : 
    1102          72 :                 if (state.need_write)
    1103          70 :                         state_write(&state);
    1104             : 
    1105             :                 /* run a test command if required */
    1106          72 :                 if (run != 0) {
    1107          10 :                         ret = system(run); /* ignore error */
    1108          10 :                         if (ret != 0) {
    1109             :                                 /* LCOV_EXCL_START */
    1110             :                                 log_fatal("Error in running command '%s'.\n", run);
    1111             :                                 exit(EXIT_FAILURE);
    1112             :                                 /* LCOV_EXCL_STOP */
    1113             :                         }
    1114             :                 }
    1115             : 
    1116             :                 /* waits some time to ensure that any concurrent modification done at the files, */
    1117             :                 /* using the same mtime read by the scan process, will be read by sync. */
    1118             :                 /* Note that any later modification done, potentially not read by sync, will have */
    1119             :                 /* a different mtime, and it will be syncronized at the next sync. */
    1120             :                 /* The worst case is the FAT filesystem with a two seconds resolution for mtime. */
    1121             :                 /* If you don't use FAT, the wait is not needed, because most filesystems have now */
    1122             :                 /* at least microseconds resolution, but better to be safe. */
    1123          72 :                 if (!opt.skip_self)
    1124           0 :                         sleep(2);
    1125             : 
    1126          72 :                 ret = state_sync(&state, blockstart, blockcount);
    1127             : 
    1128             :                 /* save the new state if required */
    1129          72 :                 if (!opt.kill_after_sync && (state.need_write || state.opt.force_content_write))
    1130          47 :                         state_write(&state);
    1131             : 
    1132             :                 /* abort if required */
    1133          72 :                 if (ret != 0) {
    1134             :                         /* LCOV_EXCL_START */
    1135             :                         exit(EXIT_FAILURE);
    1136             :                         /* LCOV_EXCL_STOP */
    1137             :                 }
    1138         165 :         } else if (operation == OPERATION_DRY) {
    1139           2 :                 state_read(&state);
    1140             : 
    1141             :                 /* filter */
    1142           2 :                 state_skip(&state);
    1143           2 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    1144             : 
    1145           2 :                 memory();
    1146             : 
    1147             :                 /* intercept signals while operating */
    1148           2 :                 signal_init();
    1149             : 
    1150           2 :                 state_dry(&state, blockstart, blockcount);
    1151         163 :         } else if (operation == OPERATION_REHASH) {
    1152           1 :                 state_read(&state);
    1153             : 
    1154             :                 /* intercept signals while operating */
    1155           1 :                 signal_init();
    1156             : 
    1157           1 :                 state_rehash(&state);
    1158             : 
    1159             :                 /* save the new state if required */
    1160           1 :                 if (state.need_write)
    1161           1 :                         state_write(&state);
    1162         162 :         } else if (operation == OPERATION_SCRUB) {
    1163          11 :                 state_read(&state);
    1164             : 
    1165          11 :                 memory();
    1166             : 
    1167             :                 /* intercept signals while operating */
    1168          11 :                 signal_init();
    1169             : 
    1170          11 :                 ret = state_scrub(&state, plan, olderthan);
    1171             : 
    1172             :                 /* save the new state if required */
    1173          11 :                 if (state.need_write || state.opt.force_content_write)
    1174          10 :                         state_write(&state);
    1175             : 
    1176             :                 /* abort if required */
    1177          11 :                 if (ret != 0) {
    1178             :                         /* LCOV_EXCL_START */
    1179             :                         exit(EXIT_FAILURE);
    1180             :                         /* LCOV_EXCL_STOP */
    1181             :                 }
    1182         151 :         } else if (operation == OPERATION_REWRITE) {
    1183           1 :                 state_read(&state);
    1184             : 
    1185             :                 /* intercept signals while operating */
    1186           1 :                 signal_init();
    1187             : 
    1188           1 :                 state_write(&state);
    1189             : 
    1190           1 :                 memory();
    1191         150 :         } else if (operation == OPERATION_READ) {
    1192           1 :                 state_read(&state);
    1193             : 
    1194           1 :                 memory();
    1195         149 :         } else if (operation == OPERATION_NANO) {
    1196           1 :                 state_read(&state);
    1197             : 
    1198           1 :                 state_nano(&state);
    1199             : 
    1200             :                 /* intercept signals while operating */
    1201           1 :                 signal_init();
    1202             : 
    1203           1 :                 state_write(&state);
    1204             : 
    1205           1 :                 memory();
    1206         148 :         } else if (operation == OPERATION_SPINUP) {
    1207           1 :                 state_device(&state, DEVICE_UP);
    1208         147 :         } else if (operation == OPERATION_SPINDOWN) {
    1209           1 :                 state_device(&state, DEVICE_DOWN);
    1210         146 :         } else if (operation == OPERATION_DEVICES) {
    1211           1 :                 state_device(&state, DEVICE_LIST);
    1212         145 :         } else if (operation == OPERATION_SMART) {
    1213           2 :                 state_device(&state, DEVICE_SMART);
    1214         143 :         } else if (operation == OPERATION_STATUS) {
    1215          14 :                 state_read(&state);
    1216             : 
    1217          14 :                 memory();
    1218             : 
    1219          14 :                 state_status(&state);
    1220         129 :         } else if (operation == OPERATION_DUP) {
    1221           3 :                 state_read(&state);
    1222             : 
    1223           3 :                 state_dup(&state);
    1224         126 :         } else if (operation == OPERATION_LIST) {
    1225           3 :                 state_read(&state);
    1226             : 
    1227           3 :                 state_list(&state);
    1228         123 :         } else if (operation == OPERATION_POOL) {
    1229           3 :                 state_read(&state);
    1230             : 
    1231           3 :                 state_pool(&state);
    1232             :         } else {
    1233         120 :                 state_read(&state);
    1234             : 
    1235             :                 /* if we are also trying to recover */
    1236         120 :                 if (!state.opt.auditonly) {
    1237             :                         /* import the user specified dirs */
    1238         114 :                         if (import_timestamp != 0)
    1239           1 :                                 state_search(&state, import_timestamp);
    1240         114 :                         if (import_content != 0)
    1241           1 :                                 state_import(&state, import_content);
    1242             : 
    1243             :                         /* import from all the array */
    1244         114 :                         if (!state.opt.force_nocopy)
    1245         114 :                                 state_search_array(&state);
    1246             :                 }
    1247             : 
    1248             :                 /* filter */
    1249         120 :                 state_skip(&state);
    1250         120 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    1251             : 
    1252         120 :                 memory();
    1253             : 
    1254             :                 /* intercept signals while operating */
    1255         120 :                 signal_init();
    1256             : 
    1257         120 :                 if (operation == OPERATION_CHECK) {
    1258          75 :                         ret = state_check(&state, 0, blockstart, blockcount);
    1259             :                 } else { /* it's fix */
    1260          45 :                         ret = state_check(&state, 1, blockstart, blockcount);
    1261             :                 }
    1262             : 
    1263             :                 /* abort if required */
    1264         120 :                 if (ret != 0) {
    1265             :                         /* LCOV_EXCL_START */
    1266             :                         exit(EXIT_FAILURE);
    1267             :                         /* LCOV_EXCL_STOP */
    1268             :                 }
    1269             :         }
    1270             : 
    1271             :         /* close log file */
    1272         225 :         log_close(log_file);
    1273             : 
    1274             : #if HAVE_LOCKFILE
    1275         225 :         if (!opt.skip_lock && state.lockfile[0]) {
    1276         223 :                 if (lock_unlock(lock) == -1) {
    1277             :                         /* LCOV_EXCL_START */
    1278             :                         log_fatal("Error closing the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
    1279             :                         exit(EXIT_FAILURE);
    1280             :                         /* LCOV_EXCL_STOP */
    1281             :                 }
    1282             :         }
    1283             : #endif
    1284             : 
    1285         225 :         state_done(&state);
    1286         225 :         tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
    1287         225 :         tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
    1288             : 
    1289         225 :         os_done();
    1290             : 
    1291         225 :         return EXIT_SUCCESS;
    1292             : }
    1293             : 

Generated by: LCOV version 1.10