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

Generated by: LCOV version 1.10