LCOV - code coverage report
Current view: top level - cmdline - snapraid.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 409 442 92.5 %
Date: 2015-05-12 Functions: 7 7 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, --percentage PERC ", "-p") "  Process only a part of the array\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 on disks with same device id\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         213 : void memory(void)
      78             : {
      79         213 :         log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter());
      80             : 
      81             :         /* size of the block + the size of the blockarr pointer to it */
      82         213 :         log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block) + sizeof(void*)));
      83             : 
      84             :         /* size of the file + the size of the 3 hashtables pointers to it, * 2 for the hashtable grow factor */
      85         213 :         log_tag("memory:file:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_file) + 3*2*sizeof(void*)));
      86             : 
      87         213 :         msg_progress("Using %u MiB of memory.\n", (unsigned)(malloc_counter() / (1024 * 1024)));
      88             : 
      89         213 : }
      90             : 
      91             : /****************************************************************************/
      92             : /* log */
      93             : 
      94         235 : void log_open(const char* log)
      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         235 :         if (log == 0)
     105         322 :                 return;
     106             : 
     107          75 :         t = time(0);
     108          75 :         tm = localtime(&t);
     109          75 :         if (tm) {
     110          75 :                 strftime(text_T, sizeof(text_T), "%H%M%S", tm);
     111          75 :                 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          75 :         mode = "wt";
     121          75 :         if (*log == '>') {
     122           3 :                 ++log;
     123             : 
     124           3 :                 if (*log == '>') {
     125           1 :                         mode = "at";
     126           1 :                         ++log;
     127             :                 }
     128             : 
     129           3 :                 if (log[0] == '&' && log[1] == '1') {
     130           1 :                         stdlog = stdout;
     131           1 :                         return;
     132             :                 }
     133             : 
     134           2 :                 if (log[0] == '&' && log[1] == '2') {
     135           1 :                         stdlog = stderr;
     136           1 :                         return;
     137             :                 }
     138             :         }
     139             : 
     140             :         /* process the path */
     141         928 :         for (*path = 0; *log != 0; ) {
     142         782 :                 switch (*log) {
     143             :                 case '%' :
     144           2 :                         ++log;
     145           2 :                         switch (*log) {
     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", *log);
     158             :                                 exit(EXIT_FAILURE);
     159             :                                 /* LCOV_EXCL_STOP */
     160             :                         }
     161           2 :                         break;
     162             :                 default :
     163         780 :                         pathcatc(path, sizeof(path), *log);
     164         780 :                         break;
     165             :                 }
     166         782 :                 ++log;
     167             :         }
     168             : 
     169          73 :         stdlog = fopen(path, mode);
     170          73 :         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         217 : void log_close(const char* log)
     179             : {
     180         217 :         if (stdlog != stdout && stdlog != stderr && stdlog != 0) {
     181          73 :                 if (fclose(stdlog) != 0) {
     182             :                         /* LCOV_EXCL_START */
     183             :                         log_fatal("Error closing the log file '%s'. %s.\n", log, strerror(errno));
     184             :                         exit(EXIT_FAILURE);
     185             :                         /* LCOV_EXCL_STOP */
     186             :                 }
     187             :         }
     188             : 
     189         217 :         stdlog = 0;
     190         217 : }
     191             : 
     192             : /****************************************************************************/
     193             : /* config */
     194             : 
     195         242 : 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         242 :         pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf");
     213             : #endif
     214         242 : }
     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_FORCE_CONTENT_TEXT 274
     238             : #define OPT_TEST_SKIP_CONTENT_CHECK 275
     239             : #define OPT_TEST_SKIP_PARITY_ACCESS 276
     240             : #define OPT_TEST_EXPECT_FAILURE 277
     241             : #define OPT_TEST_RUN 278
     242             : #define OPT_TEST_FORCE_SCAN_WINFIND 279
     243             : #define OPT_TEST_IMPORT_CONTENT 280
     244             : #define OPT_TEST_FORCE_PROGRESS 281
     245             : #define OPT_TEST_SKIP_DISK_ACCESS 282
     246             : #define OPT_TEST_FORCE_AUTOSAVE_AT 283
     247             : #define OPT_TEST_FAKE_DEVICE 284
     248             : #define OPT_TEST_EXPECT_NEED_SYNC 285
     249             : 
     250             : #if HAVE_GETOPT_LONG
     251             : struct option long_options[] = {
     252             :         { "conf", 1, 0, 'c' },
     253             :         { "filter", 1, 0, 'f' },
     254             :         { "filter-disk", 1, 0, 'd' },
     255             :         { "filter-missing", 0, 0, 'm' },
     256             :         { "filter-error", 0, 0, 'e' },
     257             :         { "percentage", 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             :         /* Force the use of text content file */
     331             :         { "test-force-content-text", 0, 0, OPT_TEST_FORCE_CONTENT_TEXT },
     332             : 
     333             :         /* Relax the checks done at the content file */
     334             :         { "test-skip-content-check", 0, 0, OPT_TEST_SKIP_CONTENT_CHECK },
     335             : 
     336             :         /* Skip the parity access */
     337             :         { "test-skip-parity-access", 0, 0, OPT_TEST_SKIP_PARITY_ACCESS },
     338             : 
     339             :         /* Exit generic failure */
     340             :         { "test-expect-failure", 0, 0, OPT_TEST_EXPECT_FAILURE },
     341             : 
     342             :         /* Exit generic need sync */
     343             :         { "test-expect-need-sync", 0, 0, OPT_TEST_EXPECT_NEED_SYNC },
     344             : 
     345             :         /* Run some command after loading the state and before the command */
     346             :         { "test-run", 1, 0, OPT_TEST_RUN },
     347             : 
     348             :         /* Use the FindFirst/Next approach in Windows to list files */
     349             :         { "test-force-scan-winfind", 0, 0, OPT_TEST_FORCE_SCAN_WINFIND },
     350             : 
     351             :         /* Alternative import working by data */
     352             :         { "test-import-content", 1, 0, OPT_TEST_IMPORT_CONTENT },
     353             : 
     354             :         /* Force immediate progress state update */
     355             :         { "test-force-progress", 0, 0, OPT_TEST_FORCE_PROGRESS },
     356             : 
     357             :         /* Skip the disk access */
     358             :         { "test-skip-disk-access", 0, 0, OPT_TEST_SKIP_DISK_ACCESS },
     359             : 
     360             :         /* Force autosave at the specified block */
     361             :         { "test-force-autosave-at", 1, 0, OPT_TEST_FORCE_AUTOSAVE_AT },
     362             : 
     363             :         /* Fake device data */
     364             :         { "test-fake-device", 0, 0, OPT_TEST_FAKE_DEVICE },
     365             : 
     366             :         { 0, 0, 0, 0 }
     367             : };
     368             : #endif
     369             : 
     370             : #define OPTIONS "c:f:d:mep:o:S:B:L:i:l:ZEUDNFahTC:vqHVG"
     371             : 
     372             : volatile int global_interrupt = 0;
     373             : 
     374             : /* LCOV_EXCL_START */
     375             : void signal_handler(int signal)
     376             : {
     377             :         switch (signal) {
     378             :         case SIGINT :
     379             :                 global_interrupt = 1;
     380             :                 break;
     381             :         }
     382             : }
     383             : /* LCOV_EXCL_STOP */
     384             : 
     385             : #define OPERATION_DIFF 0
     386             : #define OPERATION_SYNC 1
     387             : #define OPERATION_CHECK 2
     388             : #define OPERATION_FIX 3
     389             : #define OPERATION_DRY 4
     390             : #define OPERATION_DUP 5
     391             : #define OPERATION_LIST 6
     392             : #define OPERATION_POOL 7
     393             : #define OPERATION_REHASH 8
     394             : #define OPERATION_SCRUB 9
     395             : #define OPERATION_STATUS 10
     396             : #define OPERATION_REWRITE 11
     397             : #define OPERATION_NANO 12
     398             : #define OPERATION_SPINUP 13
     399             : #define OPERATION_SPINDOWN 14
     400             : #define OPERATION_DEVICES 15
     401             : #define OPERATION_SMART 16
     402             : 
     403         242 : int main(int argc, char* argv[])
     404             : {
     405             :         int c;
     406             :         struct snapraid_option opt;
     407             :         char conf[PATH_MAX];
     408             :         struct snapraid_state state;
     409             :         int operation;
     410             :         block_off_t blockstart;
     411             :         block_off_t blockcount;
     412             :         int ret;
     413             :         tommy_list filterlist_file;
     414             :         tommy_list filterlist_disk;
     415             :         int filter_missing;
     416             :         int filter_error;
     417             :         int percentage;
     418             :         int olderthan;
     419             :         char* e;
     420             :         const char* command;
     421             :         const char* import_timestamp;
     422             :         const char* import_content;
     423             :         const char* log;
     424             :         int lock;
     425             :         const char* gen_conf;
     426             :         const char* run;
     427             :         int speedtest;
     428             :         int period;
     429             :         time_t t;
     430             :         struct tm* tm;
     431             :         int i;
     432             : 
     433             :         /* defaults */
     434         242 :         config(conf, sizeof(conf), argv[0]);
     435         242 :         memset(&opt, 0, sizeof(opt));
     436         242 :         opt.io_error_limit = 100;
     437         242 :         blockstart = 0;
     438         242 :         blockcount = 0;
     439         242 :         tommy_list_init(&filterlist_file);
     440         242 :         tommy_list_init(&filterlist_disk);
     441         242 :         period = 1000;
     442         242 :         filter_missing = 0;
     443         242 :         filter_error = 0;
     444         242 :         percentage = -1;
     445         242 :         olderthan = -1;
     446         242 :         import_timestamp = 0;
     447         242 :         import_content = 0;
     448         242 :         log = 0;
     449         242 :         lock = 0;
     450         242 :         gen_conf = 0;
     451         242 :         speedtest = 0;
     452         242 :         run = 0;
     453             : 
     454         242 :         opterr = 0;
     455        2551 :         while ((c =
     456             : #if HAVE_GETOPT_LONG
     457             :                 getopt_long(argc, argv, OPTIONS, long_options, 0))
     458             : #else
     459             :                 getopt(argc, argv, OPTIONS))
     460             : #endif
     461             :                 != EOF) {
     462        2069 :                 switch (c) {
     463             :                 case 'c' :
     464         235 :                         pathimport(conf, sizeof(conf), optarg);
     465         235 :                         break;
     466             :                 case 'f' : {
     467           3 :                         struct snapraid_filter* filter = filter_alloc_file(1, optarg);
     468           3 :                         if (!filter) {
     469             :                                 /* LCOV_EXCL_START */
     470             :                                 log_fatal("Invalid filter specification '%s'\n", optarg);
     471             :                                 exit(EXIT_FAILURE);
     472             :                                 /* LCOV_EXCL_STOP */
     473             :                         }
     474           3 :                         tommy_list_insert_tail(&filterlist_file, &filter->node, filter);
     475           3 :                 } break;
     476             :                 case 'd' : {
     477           3 :                         struct snapraid_filter* filter = filter_alloc_disk(1, optarg);
     478           3 :                         if (!filter) {
     479             :                                 /* LCOV_EXCL_START */
     480             :                                 log_fatal("Invalid filter specification '%s'\n", optarg);
     481             :                                 exit(EXIT_FAILURE);
     482             :                                 /* LCOV_EXCL_STOP */
     483             :                         }
     484           3 :                         tommy_list_insert_tail(&filterlist_disk, &filter->node, filter);
     485           3 :                 } break;
     486             :                 case 'm' :
     487           2 :                         filter_missing = 1;
     488           2 :                         opt.expected_missing = 1;
     489           2 :                         break;
     490             :                 case 'e' :
     491             :                         /* when processing only error, we filter both files and blocks */
     492             :                         /* and we apply fixes only to synced ones */
     493           2 :                         filter_error = 1;
     494           2 :                         opt.badonly = 1;
     495           2 :                         opt.syncedonly = 1;
     496           2 :                         break;
     497             :                 case 'p' :
     498           2 :                         percentage = strtoul(optarg, &e, 10);
     499           2 :                         if (!e || *e || percentage > 100) {
     500             :                                 /* LCOV_EXCL_START */
     501             :                                 log_fatal("Invalid percentage '%s'\n", optarg);
     502             :                                 exit(EXIT_FAILURE);
     503             :                                 /* LCOV_EXCL_STOP */
     504             :                         }
     505           2 :                         break;
     506             :                 case 'o' :
     507           1 :                         olderthan = strtoul(optarg, &e, 10);
     508           1 :                         if (!e || *e || olderthan > 1000) {
     509             :                                 /* LCOV_EXCL_START */
     510             :                                 log_fatal("Invalid number of days '%s'\n", optarg);
     511             :                                 exit(EXIT_FAILURE);
     512             :                                 /* LCOV_EXCL_STOP */
     513             :                         }
     514           1 :                         break;
     515             :                 case 'S' :
     516           0 :                         blockstart = strtoul(optarg, &e, 0);
     517           0 :                         if (!e || *e) {
     518             :                                 /* LCOV_EXCL_START */
     519             :                                 log_fatal("Invalid start position '%s'\n", optarg);
     520             :                                 exit(EXIT_FAILURE);
     521             :                                 /* LCOV_EXCL_STOP */
     522             :                         }
     523           0 :                         break;
     524             :                 case 'B' :
     525           5 :                         blockcount = strtoul(optarg, &e, 0);
     526           5 :                         if (!e || *e) {
     527             :                                 /* LCOV_EXCL_START */
     528             :                                 log_fatal("Invalid count number '%s'\n", optarg);
     529             :                                 exit(EXIT_FAILURE);
     530             :                                 /* LCOV_EXCL_STOP */
     531             :                         }
     532           5 :                         break;
     533             :                 case 'L' :
     534           0 :                         opt.io_error_limit = strtoul(optarg, &e, 0);
     535           0 :                         if (!e || *e) {
     536             :                                 /* LCOV_EXCL_START */
     537             :                                 log_fatal("Invalid error limit number '%s'\n", optarg);
     538             :                                 exit(EXIT_FAILURE);
     539             :                                 /* LCOV_EXCL_STOP */
     540             :                         }
     541           0 :                         break;
     542             :                 case 'i' :
     543           1 :                         if (import_timestamp) {
     544             :                                 /* LCOV_EXCL_START */
     545             :                                 log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_timestamp);
     546             :                                 exit(EXIT_FAILURE);
     547             :                                 /* LCOV_EXCL_STOP */
     548             :                         }
     549           1 :                         import_timestamp = optarg;
     550           1 :                         break;
     551             :                 case OPT_TEST_IMPORT_CONTENT :
     552           1 :                         if (import_content) {
     553             :                                 /* LCOV_EXCL_START */
     554             :                                 log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_content);
     555             :                                 exit(EXIT_FAILURE);
     556             :                                 /* LCOV_EXCL_STOP */
     557             :                         }
     558           1 :                         import_content = optarg;
     559           1 :                         break;
     560             :                 case 'l' :
     561          75 :                         if (log) {
     562             :                                 /* LCOV_EXCL_START */
     563             :                                 log_fatal("Log file '%s' already specified as '%s'\n", optarg, log);
     564             :                                 exit(EXIT_FAILURE);
     565             :                                 /* LCOV_EXCL_STOP */
     566             :                         }
     567          75 :                         log = optarg;
     568          75 :                         break;
     569             :                 case 'Z' :
     570           0 :                         opt.force_zero = 1;
     571           0 :                         break;
     572             :                 case 'E' :
     573           1 :                         opt.force_empty = 1;
     574           1 :                         break;
     575             :                 case 'U' :
     576           0 :                         opt.force_uuid = 1;
     577           0 :                         break;
     578             :                 case 'D' :
     579           0 :                         opt.force_device = 1;
     580           0 :                         break;
     581             :                 case 'N' :
     582           1 :                         opt.force_nocopy = 1;
     583           1 :                         break;
     584             :                 case 'F' :
     585           1 :                         opt.force_full = 1;
     586           1 :                         break;
     587             :                 case 'a' :
     588           6 :                         opt.auditonly = 1;
     589           6 :                         break;
     590             :                 case 'h' :
     591           6 :                         opt.prehash = 1;
     592           6 :                         break;
     593             :                 case 'v' :
     594          20 :                         ++msg_level;
     595          20 :                         break;
     596             :                 case 'q' :
     597         645 :                         --msg_level;
     598         645 :                         break;
     599             :                 case 'G' :
     600          20 :                         opt.gui = 1;
     601          20 :                         break;
     602             :                 case 'H' :
     603           1 :                         usage();
     604           1 :                         exit(EXIT_SUCCESS);
     605             :                 case 'V' :
     606           1 :                         version();
     607           1 :                         exit(EXIT_SUCCESS);
     608             :                 case 'T' :
     609           4 :                         speedtest = 1;
     610           4 :                         break;
     611             :                 case 'C' :
     612           1 :                         gen_conf = optarg;
     613           1 :                         break;
     614             :                 case OPT_TEST_KILL_AFTER_SYNC :
     615           9 :                         opt.kill_after_sync = 1;
     616           9 :                         break;
     617             :                 case OPT_TEST_EXPECT_UNRECOVERABLE :
     618          13 :                         opt.expect_unrecoverable = 1;
     619          13 :                         break;
     620             :                 case OPT_TEST_EXPECT_RECOVERABLE :
     621          22 :                         opt.expect_recoverable = 1;
     622          22 :                         break;
     623             :                 case OPT_TEST_SKIP_SELF :
     624         235 :                         opt.skip_self = 1;
     625         235 :                         break;
     626             :                 case OPT_TEST_SKIP_SIGN :
     627           0 :                         opt.skip_sign = 1;
     628           0 :                         break;
     629             :                 case OPT_TEST_SKIP_FALLOCATE :
     630           1 :                         opt.skip_fallocate = 1;
     631           1 :                         break;
     632             :                 case OPT_TEST_SKIP_SEQUENTIAL :
     633           1 :                         opt.skip_sequential = 1;
     634           1 :                         break;
     635             :                 case OPT_TEST_SKIP_DEVICE :
     636         242 :                         opt.skip_device = 1;
     637         242 :                         period = 50; /* reduce period of the speed test */
     638         242 :                         break;
     639             :                 case OPT_TEST_SKIP_CONTENT_CHECK :
     640           0 :                         opt.skip_content_check = 1;
     641           0 :                         break;
     642             :                 case OPT_TEST_SKIP_PARITY_ACCESS :
     643           0 :                         opt.skip_parity_access = 1;
     644           0 :                         break;
     645             :                 case OPT_TEST_SKIP_DISK_ACCESS :
     646           0 :                         opt.skip_disk_access = 1;
     647           0 :                         break;
     648             :                 case OPT_TEST_FORCE_MURMUR3 :
     649           1 :                         opt.force_murmur3 = 1;
     650           1 :                         break;
     651             :                 case OPT_TEST_FORCE_SPOOKY2 :
     652           1 :                         opt.force_spooky2 = 1;
     653           1 :                         break;
     654             :                 case OPT_TEST_SKIP_LOCK :
     655           0 :                         opt.skip_lock = 1;
     656           0 :                         break;
     657             :                 case OPT_TEST_FORCE_ORDER_PHYSICAL :
     658           2 :                         opt.force_order = SORT_PHYSICAL;
     659           2 :                         break;
     660             :                 case OPT_TEST_FORCE_ORDER_INODE :
     661           0 :                         opt.force_order = SORT_INODE;
     662           0 :                         break;
     663             :                 case OPT_TEST_FORCE_ORDER_ALPHA :
     664         233 :                         opt.force_order = SORT_ALPHA;
     665         233 :                         break;
     666             :                 case OPT_TEST_FORCE_ORDER_DIR :
     667           0 :                         opt.force_order = SORT_DIR;
     668           0 :                         break;
     669             :                 case OPT_TEST_FORCE_SCRUB_AT :
     670           4 :                         opt.force_scrub_at = atoi(optarg);
     671           4 :                         break;
     672             :                 case OPT_TEST_FORCE_SCRUB_EVEN :
     673           1 :                         opt.force_scrub_even = 1;
     674           1 :                         break;
     675             :                 case OPT_TEST_FORCE_CONTENT_WRITE :
     676           0 :                         opt.force_content_write = 1;
     677           0 :                         break;
     678             :                 case OPT_TEST_FORCE_CONTENT_TEXT :
     679           4 :                         opt.force_content_text = 1;
     680           4 :                         break;
     681             :                 case OPT_TEST_EXPECT_FAILURE :
     682             :                         /* invert the exit codes */
     683          13 :                         exit_success = 1;
     684          13 :                         exit_failure = 0;
     685          13 :                         break;
     686             :                 case OPT_TEST_EXPECT_NEED_SYNC :
     687             :                         /* invert the exit codes */
     688           5 :                         exit_success = 1;
     689           5 :                         exit_sync_needed = 0;
     690           5 :                         break;
     691             :                 case OPT_TEST_RUN :
     692          10 :                         run = optarg;
     693          10 :                         break;
     694             :                 case OPT_TEST_FORCE_SCAN_WINFIND :
     695           0 :                         opt.force_scan_winfind = 1;
     696           0 :                         break;
     697             :                 case OPT_TEST_FORCE_PROGRESS :
     698         233 :                         opt.force_progress = 1;
     699         233 :                         break;
     700             :                 case OPT_TEST_FORCE_AUTOSAVE_AT :
     701           1 :                         opt.force_autosave_at = atoi(optarg);
     702           1 :                         break;
     703             :                 case OPT_TEST_FAKE_DEVICE :
     704           1 :                         opt.fake_device = 1;
     705           1 :                         break;
     706             :                 default :
     707             :                         /* LCOV_EXCL_START */
     708             :                         log_fatal("Unknown option '%c'\n", (char)c);
     709             :                         exit(EXIT_FAILURE);
     710             :                         /* LCOV_EXCL_STOP */
     711             :                 }
     712             :         }
     713             : 
     714         240 :         os_init(opt.force_scan_winfind);
     715         240 :         raid_init();
     716         240 :         crc32c_init();
     717             : 
     718         240 :         if (speedtest != 0) {
     719           4 :                 speed(period);
     720           4 :                 os_done();
     721           4 :                 exit(EXIT_SUCCESS);
     722             :         }
     723             : 
     724         236 :         if (gen_conf != 0) {
     725           1 :                 generate_configuration(gen_conf);
     726           1 :                 os_done();
     727           1 :                 exit(EXIT_SUCCESS);
     728             :         }
     729             : 
     730         235 :         if (optind + 1 != argc) {
     731             :                 /* LCOV_EXCL_START */
     732             :                 usage();
     733             :                 exit(EXIT_FAILURE);
     734             :                 /* LCOV_EXCL_STOP */
     735             :         }
     736             : 
     737         235 :         command = argv[optind];
     738         235 :         if (strcmp(command, "diff") == 0) {
     739           6 :                 operation = OPERATION_DIFF;
     740         229 :         } else if (strcmp(argv[optind], "sync") == 0) {
     741          64 :                 operation = OPERATION_SYNC;
     742         165 :         } else if (strcmp(argv[optind], "check") == 0) {
     743          76 :                 operation = OPERATION_CHECK;
     744          89 :         } else if (strcmp(argv[optind], "fix") == 0) {
     745          44 :                 operation = OPERATION_FIX;
     746          45 :         } else if (strcmp(argv[optind], "test-dry") == 0) {
     747           2 :                 operation = OPERATION_DRY;
     748          43 :         } else if (strcmp(argv[optind], "dup") == 0) {
     749           3 :                 operation = OPERATION_DUP;
     750          40 :         } else if (strcmp(argv[optind], "list") == 0) {
     751           3 :                 operation = OPERATION_LIST;
     752          37 :         } else if (strcmp(argv[optind], "pool") == 0) {
     753           3 :                 operation = OPERATION_POOL;
     754          34 :         } else if (strcmp(argv[optind], "rehash") == 0) {
     755           1 :                 operation = OPERATION_REHASH;
     756          33 :         } else if (strcmp(argv[optind], "scrub") == 0) {
     757           9 :                 operation = OPERATION_SCRUB;
     758          24 :         } else if (strcmp(argv[optind], "status") == 0) {
     759          14 :                 operation = OPERATION_STATUS;
     760          10 :         } else if (strcmp(argv[optind], "test-rewrite") == 0) {
     761           4 :                 operation = OPERATION_REWRITE;
     762           6 :         } else if (strcmp(argv[optind], "test-nano") == 0) {
     763           1 :                 operation = OPERATION_NANO;
     764           5 :         } else if (strcmp(argv[optind], "up") == 0) {
     765           1 :                 operation = OPERATION_SPINUP;
     766           4 :         } else if (strcmp(argv[optind], "down") == 0) {
     767           1 :                 operation = OPERATION_SPINDOWN;
     768           3 :         } else if (strcmp(argv[optind], "devices") == 0) {
     769           1 :                 operation = OPERATION_DEVICES;
     770           2 :         } else if (strcmp(argv[optind], "smart") == 0) {
     771           2 :                 operation = OPERATION_SMART;
     772             :         } else {
     773             :                 /* LCOV_EXCL_START */
     774             :                 log_fatal("Unknown command '%s'\n", argv[optind]);
     775             :                 exit(EXIT_FAILURE);
     776             :                 /* LCOV_EXCL_STOP */
     777             :         }
     778             : 
     779             :         /* check options compatibility */
     780         235 :         switch (operation) {
     781             :         case OPERATION_CHECK :
     782          76 :                 break;
     783             :         default :
     784         159 :                 if (opt.auditonly) {
     785             :                         /* LCOV_EXCL_START */
     786             :                         log_fatal("You cannot use -a, --audit-only with the '%s' command\n", command);
     787             :                         exit(EXIT_FAILURE);
     788             :                         /* LCOV_EXCL_STOP */
     789             :                 }
     790             :         }
     791             : 
     792         235 :         switch (operation) {
     793             :         case OPERATION_SYNC :
     794             :         case OPERATION_CHECK :
     795             :         case OPERATION_FIX :
     796         184 :                 break;
     797             :         default :
     798          51 :                 if (opt.force_nocopy) {
     799             :                         /* LCOV_EXCL_START */
     800             :                         log_fatal("You cannot use -N, --force-nocopy with the '%s' command\n", command);
     801             :                         exit(EXIT_FAILURE);
     802             :                         /* LCOV_EXCL_STOP */
     803             :                 }
     804             :         }
     805             : 
     806         235 :         switch (operation) {
     807             :         case OPERATION_SYNC :
     808          64 :                 break;
     809             :         default :
     810         171 :                 if (opt.prehash) {
     811             :                         /* LCOV_EXCL_START */
     812             :                         log_fatal("You cannot use -h, --pre-hash with the '%s' command\n", command);
     813             :                         exit(EXIT_FAILURE);
     814             :                         /* LCOV_EXCL_STOP */
     815             :                 }
     816             : 
     817         171 :                 if (opt.force_full) {
     818             :                         /* LCOV_EXCL_START */
     819             :                         log_fatal("You cannot use -F, --force-full with the '%s' command\n", command);
     820             :                         exit(EXIT_FAILURE);
     821             :                         /* LCOV_EXCL_STOP */
     822             :                 }
     823             :         }
     824             : 
     825         235 :         if (opt.force_full && opt.force_nocopy) {
     826             :                 /* LCOV_EXCL_START */
     827             :                 log_fatal("You cannot use the -F, --force-full and -N, --force-nocopy options at the same time\n");
     828             :                 exit(EXIT_FAILURE);
     829             :                 /* LCOV_EXCL_STOP */
     830             :         }
     831             : 
     832         235 :         if (opt.prehash && opt.force_nocopy) {
     833             :                 /* LCOV_EXCL_START */
     834             :                 log_fatal("You cannot use the -h, --pre-hash and -N, --force-nocopy options at the same time\n");
     835             :                 exit(EXIT_FAILURE);
     836             :                 /* LCOV_EXCL_STOP */
     837             :         }
     838             : 
     839         235 :         switch (operation) {
     840             :         case OPERATION_CHECK :
     841             :         case OPERATION_FIX :
     842             :         case OPERATION_DRY :
     843         122 :                 break;
     844             :         default :
     845         113 :                 if (!tommy_list_empty(&filterlist_file)) {
     846             :                         /* LCOV_EXCL_START */
     847             :                         log_fatal("You cannot use -f, --filter with the '%s' command\n", command);
     848             :                         exit(EXIT_FAILURE);
     849             :                         /* LCOV_EXCL_STOP */
     850             :                 }
     851         113 :                 if (!tommy_list_empty(&filterlist_disk)) {
     852             :                         /* LCOV_EXCL_START */
     853             :                         log_fatal("You cannot use -d, --filter-disk with the '%s' command\n", command);
     854             :                         exit(EXIT_FAILURE);
     855             :                         /* LCOV_EXCL_STOP */
     856             :                 }
     857         113 :                 if (filter_missing != 0) {
     858             :                         /* LCOV_EXCL_START */
     859             :                         log_fatal("You cannot use -m, --filter-missing with the '%s' command\n", command);
     860             :                         exit(EXIT_FAILURE);
     861             :                         /* LCOV_EXCL_STOP */
     862             :                 }
     863         113 :                 if (filter_error != 0) {
     864             :                         /* LCOV_EXCL_START */
     865             :                         log_fatal("You cannot use -e, --filter-error with the '%s' command\n", command);
     866             :                         exit(EXIT_FAILURE);
     867             :                         /* LCOV_EXCL_STOP */
     868             :                 }
     869             :         }
     870             : 
     871         235 :         switch (operation) {
     872             :         case OPERATION_CHECK :
     873             :         case OPERATION_FIX :
     874         120 :                 break;
     875             :         default :
     876         115 :                 if (import_timestamp != 0 || import_content != 0) {
     877             :                         /* LCOV_EXCL_START */
     878             :                         log_fatal("You cannot import with the '%s' command\n", command);
     879             :                         exit(EXIT_FAILURE);
     880             :                         /* LCOV_EXCL_STOP */
     881             :                 }
     882             :         }
     883             : 
     884         235 :         switch (operation) {
     885             :         case OPERATION_LIST :
     886             :         case OPERATION_DUP :
     887             :         case OPERATION_STATUS :
     888             :         case OPERATION_REWRITE :
     889             :         case OPERATION_REHASH :
     890             :         case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
     891             :                 /* avoid to check and access data disks if not needed */
     892          26 :                 opt.skip_disk_access = 1;
     893          26 :                 break;
     894             :         }
     895             : 
     896         235 :         switch (operation) {
     897             :         case OPERATION_DIFF :
     898             :         case OPERATION_LIST :
     899             :         case OPERATION_DUP :
     900             :         case OPERATION_POOL :
     901             :         case OPERATION_STATUS :
     902             :         case OPERATION_REWRITE :
     903             :         case OPERATION_REHASH :
     904             :         case OPERATION_NANO :
     905             :         case OPERATION_SPINUP : /* we want to do it in different threads to avoid blocking */
     906             :                 /* avoid to check and access parity disks if not needed */
     907          36 :                 opt.skip_parity_access = 1;
     908          36 :                 break;
     909             :         }
     910             : 
     911         235 :         switch (operation) {
     912             :         case OPERATION_DIFF :
     913             :         case OPERATION_LIST :
     914             :         case OPERATION_DUP :
     915             :         case OPERATION_POOL :
     916             :         case OPERATION_NANO :
     917             :         case OPERATION_SPINUP :
     918             :         case OPERATION_SPINDOWN :
     919             :         case OPERATION_DEVICES :
     920             :         case OPERATION_SMART :
     921          21 :                 opt.skip_self = 1;
     922          21 :                 break;
     923             :         }
     924             : 
     925         235 :         switch (operation) {
     926             :         case OPERATION_DEVICES :
     927             :         case OPERATION_SMART :
     928             :                 /* we may need to use these commands during operations */
     929           3 :                 opt.skip_lock = 1;
     930           3 :                 break;
     931             :         }
     932             : 
     933             :         /* open the log file */
     934         235 :         log_open(log);
     935             : 
     936             :         /* print generic info into the log */
     937         235 :         t = time(0);
     938         235 :         tm = localtime(&t);
     939         235 :         log_tag("version:%s\n", PACKAGE_VERSION);
     940         235 :         log_tag("unixtime:%" PRIi64 "\n", (int64_t)t);
     941         235 :         if (tm) {
     942             :                 char datetime[64];
     943         235 :                 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", tm);
     944         235 :                 log_tag("time:%s\n", datetime);
     945             :         }
     946         235 :         log_tag("command:%s\n", command);
     947        3095 :         for (i = 0; i < argc; ++i)
     948        2860 :                 log_tag("argv:%u:%s\n", i, argv[i]);
     949         235 :         log_flush();
     950             : 
     951         235 :         if (!opt.skip_self)
     952           1 :                 selftest();
     953             : 
     954         235 :         state_init(&state);
     955             : 
     956             :         /* read the configuration file */
     957         235 :         state_config(&state, conf, command, &opt, &filterlist_disk);
     958             : 
     959             :         /* set the raid mode */
     960         235 :         raid_mode(state.raid_mode);
     961             : 
     962             : #if HAVE_LOCKFILE
     963             :         /* create the lock file */
     964         235 :         if (!opt.skip_lock) {
     965         232 :                 lock = lock_lock(state.lockfile);
     966         232 :                 if (lock == -1) {
     967             :                         /* LCOV_EXCL_START */
     968             :                         if (errno != EWOULDBLOCK) {
     969             :                                 log_fatal("Error creating the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
     970             :                         } else {
     971             :                                 log_fatal("The lock file '%s' is already locked!\n", state.lockfile);
     972             :                                 log_fatal("SnapRAID is already in use!\n");
     973             :                         }
     974             :                         exit(EXIT_FAILURE);
     975             :                         /* LCOV_EXCL_STOP */
     976             :                 }
     977             :         }
     978             : #else
     979             :         (void)lock;
     980             : #endif
     981             : 
     982         235 :         if (operation == OPERATION_DIFF) {
     983           6 :                 state_read(&state);
     984             : 
     985           6 :                 ret = state_diff(&state);
     986             : 
     987             :                 /* abort if sync needed */
     988           6 :                 if (ret > 0)
     989           5 :                         exit(EXIT_SYNC_NEEDED);
     990         229 :         } else if (operation == OPERATION_SYNC) {
     991             : 
     992             :                 /* in the next state read ensures to clear all the past hashes in case */
     993             :                 /* we are reading from an incomplete sync */
     994             :                 /* The undeterminated hash are only for CHG/DELETED blocks for which we don't */
     995             :                 /* know if the previous interrupted sync was able to update or not the parity */
     996             :                 /* The sync process instead needs to trust this information because it's used */
     997             :                 /* to avoid to recompute the parity if all the input are equals as before. */
     998          64 :                 state.clear_past_hash = 1;
     999             : 
    1000          64 :                 state_read(&state);
    1001             : 
    1002          63 :                 state_scan(&state);
    1003             : 
    1004             :                 /* refresh the size info before the content write */
    1005          63 :                 state_refresh(&state);
    1006             : 
    1007          63 :                 memory();
    1008             : 
    1009             :                 /* intercept Ctrl+C */
    1010          63 :                 signal(SIGINT, &signal_handler);
    1011             : 
    1012             :                 /* save the new state before the sync */
    1013             :                 /* this allow to recover the case of the changes in the array after an aborted sync. */
    1014             : 
    1015             :                 /* for example, think at this case: */
    1016             :                 /* - add some files at the array */
    1017             :                 /* - run a sync command, it will recompute the parity adding the new files */
    1018             :                 /* - abort the sync command before it stores the new content file */
    1019             :                 /* - delete the not yet synced files from the array */
    1020             :                 /* - run a new sync command */
    1021             : 
    1022             :                 /* the new sync command has now way to know that the parity file was modified */
    1023             :                 /* because the files triggering these changes are now deleted */
    1024             :                 /* and they aren't listed in the content file */
    1025             : 
    1026          63 :                 if (state.need_write)
    1027          61 :                         state_write(&state);
    1028             : 
    1029             :                 /* run a test command if required */
    1030          63 :                 if (run != 0) {
    1031          10 :                         ret = system(run); /* ignore error */
    1032          10 :                         if (ret != 0) {
    1033             :                                 /* LCOV_EXCL_START */
    1034             :                                 log_fatal("Error in running command '%s'.\n", run);
    1035             :                                 exit(EXIT_FAILURE);
    1036             :                                 /* LCOV_EXCL_STOP */
    1037             :                         }
    1038             :                 }
    1039             : 
    1040             :                 /* waits some time to ensure that any concurrent modification done at the files, */
    1041             :                 /* using the same mtime read by the scan process, will be read by sync. */
    1042             :                 /* Note that any later modification done, potentially not read by sync, will have */
    1043             :                 /* a different mtime, and it will be syncronized at the next sync. */
    1044             :                 /* The worst case is the FAT filesystem with a two seconds resolution for mtime. */
    1045             :                 /* If you don't use FAT, the wait is not needed, because most filesystems have now */
    1046             :                 /* at least microseconds resolution, but better to be safe. */
    1047          63 :                 if (!opt.skip_self)
    1048           0 :                         sleep(2);
    1049             : 
    1050          63 :                 ret = state_sync(&state, blockstart, blockcount);
    1051             : 
    1052             :                 /* save the new state if required */
    1053          63 :                 if (!opt.kill_after_sync && (state.need_write || state.opt.force_content_write))
    1054          41 :                         state_write(&state);
    1055             : 
    1056             :                 /* abort if required */
    1057          63 :                 if (ret != 0) {
    1058             :                         /* LCOV_EXCL_START */
    1059             :                         exit(EXIT_FAILURE);
    1060             :                         /* LCOV_EXCL_STOP */
    1061             :                 }
    1062         165 :         } else if (operation == OPERATION_DRY) {
    1063           2 :                 state_read(&state);
    1064             : 
    1065             :                 /* apply the command line filter */
    1066           2 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    1067             : 
    1068           2 :                 memory();
    1069             : 
    1070             :                 /* intercept Ctrl+C */
    1071           2 :                 signal(SIGINT, &signal_handler);
    1072             : 
    1073           2 :                 state_dry(&state, blockstart, blockcount);
    1074         163 :         } else if (operation == OPERATION_REHASH) {
    1075           1 :                 state_read(&state);
    1076             : 
    1077             :                 /* intercept Ctrl+C */
    1078           1 :                 signal(SIGINT, &signal_handler);
    1079             : 
    1080           1 :                 state_rehash(&state);
    1081             : 
    1082             :                 /* save the new state if required */
    1083           1 :                 if (state.need_write)
    1084           1 :                         state_write(&state);
    1085         162 :         } else if (operation == OPERATION_SCRUB) {
    1086           9 :                 state_read(&state);
    1087             : 
    1088           9 :                 memory();
    1089             : 
    1090             :                 /* intercept Ctrl+C */
    1091           9 :                 signal(SIGINT, &signal_handler);
    1092             : 
    1093           9 :                 ret = state_scrub(&state, percentage, olderthan);
    1094             : 
    1095             :                 /* save the new state if required */
    1096           9 :                 if (state.need_write || state.opt.force_content_write)
    1097           8 :                         state_write(&state);
    1098             : 
    1099             :                 /* abort if required */
    1100           9 :                 if (ret != 0) {
    1101             :                         /* LCOV_EXCL_START */
    1102             :                         exit(EXIT_FAILURE);
    1103             :                         /* LCOV_EXCL_STOP */
    1104             :                 }
    1105         153 :         } else if (operation == OPERATION_REWRITE) {
    1106           4 :                 state_read(&state);
    1107             : 
    1108           4 :                 state_write(&state);
    1109             : 
    1110           4 :                 memory();
    1111         149 :         } else if (operation == OPERATION_NANO) {
    1112           1 :                 state_read(&state);
    1113             : 
    1114           1 :                 state_nano(&state);
    1115             : 
    1116           1 :                 state_write(&state);
    1117             : 
    1118           1 :                 memory();
    1119         148 :         } else if (operation == OPERATION_SPINUP) {
    1120           1 :                 state_device(&state, DEVICE_UP);
    1121         147 :         } else if (operation == OPERATION_SPINDOWN) {
    1122           1 :                 state_device(&state, DEVICE_DOWN);
    1123         146 :         } else if (operation == OPERATION_DEVICES) {
    1124           1 :                 state_device(&state, DEVICE_LIST);
    1125         145 :         } else if (operation == OPERATION_SMART) {
    1126           2 :                 state_device(&state, DEVICE_SMART);
    1127         143 :         } else if (operation == OPERATION_STATUS) {
    1128          14 :                 state_read(&state);
    1129             : 
    1130          14 :                 memory();
    1131             : 
    1132          14 :                 state_status(&state);
    1133         129 :         } else if (operation == OPERATION_DUP) {
    1134           3 :                 state_read(&state);
    1135             : 
    1136           3 :                 state_dup(&state);
    1137         126 :         } else if (operation == OPERATION_LIST) {
    1138           3 :                 state_read(&state);
    1139             : 
    1140           3 :                 state_list(&state);
    1141         123 :         } else if (operation == OPERATION_POOL) {
    1142           3 :                 state_read(&state);
    1143             : 
    1144           3 :                 state_pool(&state);
    1145             :         } else {
    1146         120 :                 state_read(&state);
    1147             : 
    1148             :                 /* if we are also trying to recover */
    1149         120 :                 if (!state.opt.auditonly) {
    1150             :                         /* import the user specified dirs */
    1151         114 :                         if (import_timestamp != 0)
    1152           1 :                                 state_search(&state, import_timestamp);
    1153         114 :                         if (import_content != 0)
    1154           1 :                                 state_import(&state, import_content);
    1155             : 
    1156             :                         /* import from all the array */
    1157         114 :                         if (!state.opt.force_nocopy)
    1158         114 :                                 state_search_array(&state);
    1159             :                 }
    1160             : 
    1161             :                 /* apply the command line filter */
    1162         120 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    1163             : 
    1164         120 :                 memory();
    1165             : 
    1166             :                 /* intercept Ctrl+C */
    1167         120 :                 signal(SIGINT, &signal_handler);
    1168             : 
    1169         120 :                 if (operation == OPERATION_CHECK) {
    1170          76 :                         ret = state_check(&state, 0, blockstart, blockcount);
    1171             :                 } else { /* it's fix */
    1172          44 :                         ret = state_check(&state, 1, blockstart, blockcount);
    1173             :                 }
    1174             : 
    1175             :                 /* abort if required */
    1176         120 :                 if (ret != 0) {
    1177             :                         /* LCOV_EXCL_START */
    1178             :                         exit(EXIT_FAILURE);
    1179             :                         /* LCOV_EXCL_STOP */
    1180             :                 }
    1181             :         }
    1182             : 
    1183             :         /* close log file */
    1184         217 :         log_close(log);
    1185             : 
    1186             : #if HAVE_LOCKFILE
    1187         217 :         if (!opt.skip_lock) {
    1188         215 :                 if (lock_unlock(lock) == -1) {
    1189             :                         /* LCOV_EXCL_START */
    1190             :                         log_fatal("Error closing the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
    1191             :                         exit(EXIT_FAILURE);
    1192             :                         /* LCOV_EXCL_STOP */
    1193             :                 }
    1194             :         }
    1195             : #endif
    1196             : 
    1197         217 :         state_done(&state);
    1198         217 :         tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
    1199         217 :         tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
    1200             : 
    1201         217 :         os_done();
    1202             : 
    1203         217 :         return EXIT_SUCCESS;
    1204             : }
    1205             : 

Generated by: LCOV version 1.10