LCOV - code coverage report
Current view: top level - cmdline - snapraid.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 780 897 87.0 %
Date: 2026-02-28 09:49:08 Functions: 10 10 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 "io.h"
      27             : #include "raid/raid.h"
      28             : #include "locate.h"
      29             : 
      30             : /****************************************************************************/
      31             : /* misc */
      32             : 
      33           2 : void version(void)
      34             : {
      35           2 :         msg_status(PACKAGE_NAME " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
      36           2 : }
      37             : 
      38           1 : void usage(const char* conf)
      39             : {
      40           1 :         version();
      41             : 
      42           1 :         printf("Usage: " PACKAGE " status|diff|sync|scrub|list|dup|up|down|probe|touch|smart|pool|check|fix [options]\n");
      43           1 :         printf("\n");
      44           1 :         printf("Commands:\n");
      45           1 :         printf("  status Print the status of the array\n");
      46           1 :         printf("  diff   Show the changes that needs to be synchronized\n");
      47           1 :         printf("  sync   Synchronize the state of the array\n");
      48           1 :         printf("  scrub  Scrub the array\n");
      49           1 :         printf("  list   List the array content\n");
      50           1 :         printf("  dup    Find duplicate files\n");
      51           1 :         printf("  up     Spin-up the array\n");
      52           1 :         printf("  down   Spin-down the array\n");
      53           1 :         printf("  probe  Show the spinning status of all disks\n");
      54           1 :         printf("  touch  Add non-zero ns timestamps to files\n");
      55           1 :         printf("  smart  SMART attributes of the array\n");
      56           1 :         printf("  pool   Create or update the virtual view of the array\n");
      57           1 :         printf("  check  Check the array\n");
      58           1 :         printf("  fix    Fix the array\n");
      59           1 :         printf("\n");
      60           1 :         printf("Options:\n");
      61           1 :         printf("  " SWITCH_GETOPT_LONG("-c, --conf FILE       ", "-c") "  Configuration file\n");
      62           1 :         printf("  " SWITCH_GETOPT_LONG("-f, --filter PATTERN  ", "-f") "  Process only files matching the pattern\n");
      63           1 :         printf("  " SWITCH_GETOPT_LONG("-d, --filter-disk NAME", "-d") "  Process only files in the specified disk\n");
      64           1 :         printf("  " SWITCH_GETOPT_LONG("-m, --filter-missing  ", "-m") "  Process only missing/deleted files\n");
      65           1 :         printf("  " SWITCH_GETOPT_LONG("-e, --filter-error    ", "-e") "  Process only files with errors\n");
      66           1 :         printf("  " SWITCH_GETOPT_LONG("-p, --plan PLAN       ", "-p") "  Define a scrub plan or percentage\n");
      67           1 :         printf("  " SWITCH_GETOPT_LONG("-o, --older-than DAYS ", "-o") "  Process only the older part of the array\n");
      68           1 :         printf("  " SWITCH_GETOPT_LONG("-i, --import DIR      ", "-i") "  Import deleted files\n");
      69           1 :         printf("  " SWITCH_GETOPT_LONG("-l, --log FILE        ", "-l") "  Log file. Default none\n");
      70           1 :         printf("  " SWITCH_GETOPT_LONG("-a, --audit-only      ", "-a") "  Check only file data and not parity\n");
      71           1 :         printf("  " SWITCH_GETOPT_LONG("-h, --pre-hash        ", "-h") "  Pre-hash all the new data\n");
      72           1 :         printf("  " SWITCH_GETOPT_LONG("-Z, --force-zero      ", "-Z") "  Force syncing of files that get zero size\n");
      73           1 :         printf("  " SWITCH_GETOPT_LONG("-E, --force-empty     ", "-E") "  Force syncing of disks that get empty\n");
      74           1 :         printf("  " SWITCH_GETOPT_LONG("-U, --force-uuid      ", "-U") "  Force commands on disks with uuid changed\n");
      75           1 :         printf("  " SWITCH_GETOPT_LONG("-D, --force-device    ", "-D") "  Force commands with inaccessible/shared disks\n");
      76           1 :         printf("  " SWITCH_GETOPT_LONG("-N, --force-nocopy    ", "-N") "  Force commands disabling the copy detection\n");
      77             :         /* --force-full, --force-realloc and --force-realloc-tail are not listed as they are dangerous */
      78           1 :         printf("  " SWITCH_GETOPT_LONG("-w, --bw-limit RATE   ", "-w") "  Limit IO bandwidth (M|G)\n");
      79           1 :         printf("  " SWITCH_GETOPT_LONG("-v, --verbose         ", "-v") "  Verbose\n");
      80           1 :         printf("\n");
      81           1 :         printf("Configuration file: %s\n", conf);
      82           1 :         printf("\n");
      83           1 : }
      84             : 
      85         244 : void memory(void)
      86             : {
      87         244 :         log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter_get());
      88             : 
      89             :         /* size of the block */
      90         244 :         log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block)));
      91         244 :         log_tag("memory:extent:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_extent)));
      92         244 :         log_tag("memory:file:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_file)));
      93         244 :         log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link)));
      94         244 :         log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir)));
      95             : 
      96         244 :         msg_progress("Using %u MiB of memory for the file-system.\n", (unsigned)(malloc_counter_get() / MEBI));
      97         244 : }
      98             : 
      99         288 : void test(int argc, char* argv[])
     100             : {
     101             :         int i;
     102             :         char buffer[ESC_MAX];
     103             : 
     104             :         /* special testing code for quoting */
     105         288 :         if (argc < 2 || strcmp(argv[1], "test") != 0)
     106         287 :                 return;
     107             : 
     108           1 :         assert(strcmp(strpolish(strcpy(buffer, "\r \n\xFF")), "    ") == 0);
     109           1 :         assert(strcmp(strtrim(strcpy(buffer, " trim trim \n\r")), "trim trim") == 0);
     110           1 :         assert(strcmp(strlwr(strcpy(buffer, " LoWer\n\r")), " lower\n\r") == 0);
     111             : 
     112           1 :         assert(worddigitstr("longneedlestring", "needle") == 0);
     113           1 :         assert(worddigitstr("longneedlestring", "") == 0);
     114           1 :         assert(worddigitstr("long needle string", "needle") != 0);
     115           1 :         assert(worddigitstr("long1needle2string", "needle") != 0);
     116           1 :         assert(worddigitstr("long\rneedle3string", "needle") != 0);
     117           1 :         assert(worddigitstr("long1needle", "needle") != 0);
     118           1 :         assert(worddigitstr("needle2string", "needle") != 0);
     119           1 :         assert(worddigitstr("needle", "needle") != 0);
     120             : 
     121           1 :         assert(strcmp(esc_tag("simple", buffer), "simple") == 0);
     122           1 :         assert(strcmp(esc_tag("line1\nline2", buffer), "line1\\nline2") == 0);
     123           1 :         assert(strcmp(esc_tag("line1\rline2", buffer), "line1\\rline2") == 0);
     124           1 :         assert(strcmp(esc_tag("key:value", buffer), "key\\dvalue") == 0);
     125           1 :         assert(strcmp(esc_tag("C:\\path\\file", buffer), "C\\d\\\\path\\\\file") == 0);
     126           1 :         assert(strcmp(esc_tag("A\nB\rC:D\\E", buffer), "A\\nB\\rC\\dD\\\\E") == 0);
     127           1 :         assert(strcmp(esc_tag("endwith\\", buffer), "endwith\\\\") == 0);
     128           1 :         assert(strcmp(esc_tag("", buffer), "") == 0);
     129           1 :         assert(strcmp(esc_tag("\n\r:\\\\", buffer), "\\n\\r\\d\\\\\\\\") == 0);
     130             : 
     131           1 :         for (i = 2; i < argc; ++i) {
     132           0 :                 printf("argv[%d]\n", i);
     133           0 :                 printf("\t#%s#\n", argv[i]);
     134           0 :                 printf("\t#%s#\n", esc_shell(argv[i], buffer));
     135             :         }
     136             : 
     137             : #ifdef _WIN32
     138             :         /* basic cases - no special characters, no quotes needed */
     139             :         assert(strcmp(esc_shell("simple", buffer), "simple") == 0);
     140             :         assert(strcmp(esc_shell("file.txt", buffer), "file.txt") == 0);
     141             :         assert(strcmp(esc_shell("file123", buffer), "file123") == 0);
     142             :         assert(strcmp(esc_shell("file_name-test.doc", buffer), "file_name-test.doc") == 0);
     143             :         assert(strcmp(esc_shell(",._+:@/-", buffer), ",._+:@/-") == 0);
     144             :         assert(strcmp(esc_shell("C:\\Users\\test", buffer), "C:\\Users\\test") == 0);
     145             : 
     146             :         /* space - requires quoting */
     147             :         assert(strcmp(esc_shell(" ", buffer), "\" \"") == 0);
     148             :         assert(strcmp(esc_shell("file name.txt", buffer), "\"file name.txt\"") == 0);
     149             :         assert(strcmp(esc_shell("my document.doc", buffer), "\"my document.doc\"") == 0);
     150             :         assert(strcmp(esc_shell("  multiple  spaces  ", buffer), "\"  multiple  spaces  \"") == 0);
     151             : 
     152             :         /* tab - requires quoting */
     153             :         assert(strcmp(esc_shell("\t", buffer), "\"\t\"") == 0);
     154             :         assert(strcmp(esc_shell("file\tname", buffer), "\"file\tname\"") == 0);
     155             : 
     156             :         /* newline - requires quoting */
     157             :         assert(strcmp(esc_shell("\n", buffer), "\"\n\"") == 0);
     158             :         assert(strcmp(esc_shell("line1\nline2", buffer), "\"line1\nline2\"") == 0);
     159             : 
     160             :         /* carriage return - requires quoting */
     161             :         assert(strcmp(esc_shell("\r", buffer), "\"\r\"") == 0);
     162             :         assert(strcmp(esc_shell("text\r\n", buffer), "\"text\r\n\"") == 0);
     163             : 
     164             :         /* double quote - requires quoting and escaping with backslash */
     165             :         assert(strcmp(esc_shell("\"", buffer), "\"\\\"\"") == 0);
     166             :         assert(strcmp(esc_shell("file\"name", buffer), "\"file\\\"name\"") == 0);
     167             :         assert(strcmp(esc_shell("\"quoted\"", buffer), "\"\\\"quoted\\\"\"") == 0);
     168             :         assert(strcmp(esc_shell("say \"hello\"", buffer), "\"say \\\"hello\\\"\"") == 0);
     169             : 
     170             :         /* ampersand - requires quoting */
     171             :         assert(strcmp(esc_shell("&", buffer), "\"&\"") == 0);
     172             :         assert(strcmp(esc_shell("file&name", buffer), "\"file&name\"") == 0);
     173             :         assert(strcmp(esc_shell("a&b&c", buffer), "\"a&b&c\"") == 0);
     174             :         assert(strcmp(esc_shell("file & name", buffer), "\"file & name\"") == 0);
     175             : 
     176             :         /* pipe - requires quoting */
     177             :         assert(strcmp(esc_shell("|", buffer), "\"|\"") == 0);
     178             :         assert(strcmp(esc_shell("file|name", buffer), "\"file|name\"") == 0);
     179             :         assert(strcmp(esc_shell("a | b", buffer), "\"a | b\"") == 0);
     180             : 
     181             :         /* parentheses - requires quoting */
     182             :         assert(strcmp(esc_shell("(", buffer), "\"(\"") == 0);
     183             :         assert(strcmp(esc_shell(")", buffer), "\")\"") == 0);
     184             :         assert(strcmp(esc_shell("(test)", buffer), "\"(test)\"") == 0);
     185             :         assert(strcmp(esc_shell("file (1)", buffer), "\"file (1)\"") == 0);
     186             :         assert(strcmp(esc_shell("file(copy)", buffer), "\"file(copy)\"") == 0);
     187             : 
     188             :         /* angle brackets - requires quoting */
     189             :         assert(strcmp(esc_shell("<", buffer), "\"<\"") == 0);
     190             :         assert(strcmp(esc_shell(">", buffer), "\">\"") == 0);
     191             :         assert(strcmp(esc_shell("a<b>c", buffer), "\"a<b>c\"") == 0);
     192             :         assert(strcmp(esc_shell("file > output", buffer), "\"file > output\"") == 0);
     193             : 
     194             :         /* caret - requires quoting */
     195             :         assert(strcmp(esc_shell("^", buffer), "\"^\"") == 0);
     196             :         assert(strcmp(esc_shell("test^test", buffer), "\"test^test\"") == 0);
     197             :         assert(strcmp(esc_shell("a ^ b", buffer), "\"a ^ b\"") == 0);
     198             : 
     199             :         /* multiple special chars - requires quoting */
     200             :         assert(strcmp(esc_shell("&|()<>^", buffer), "\"&|()<>^\"") == 0);
     201             :         assert(strcmp(esc_shell("test&|test", buffer), "\"test&|test\"") == 0);
     202             :         assert(strcmp(esc_shell("a & b | c", buffer), "\"a & b | c\"") == 0);
     203             : 
     204             :         /* percent sign - requires quoting */
     205             :         assert(strcmp(esc_shell("%", buffer), "\"%\"") == 0);
     206             :         assert(strcmp(esc_shell("%%", buffer), "\"%%\"") == 0);
     207             :         assert(strcmp(esc_shell("%PATH%", buffer), "\"%PATH%\"") == 0);
     208             :         assert(strcmp(esc_shell("test%var%test", buffer), "\"test%var%test\"") == 0);
     209             :         assert(strcmp(esc_shell("%PATH% file", buffer), "\"%PATH% file\"") == 0);
     210             : 
     211             :         /* exclamation mark - requires quoting */
     212             :         assert(strcmp(esc_shell("!", buffer), "\"!\"") == 0);
     213             :         assert(strcmp(esc_shell("!VAR!", buffer), "\"!VAR!\"") == 0);
     214             :         assert(strcmp(esc_shell("test!test", buffer), "\"test!test\"") == 0);
     215             :         assert(strcmp(esc_shell("hello !world!", buffer), "\"hello !world!\"") == 0);
     216             : 
     217             :         /* equals sign - requires quoting */
     218             :         assert(strcmp(esc_shell("=", buffer), "\"=\"") == 0);
     219             :         assert(strcmp(esc_shell("VAR=value", buffer), "\"VAR=value\"") == 0);
     220             :         assert(strcmp(esc_shell("a=b", buffer), "\"a=b\"") == 0);
     221             : 
     222             :         /* semicolon - requires quoting */
     223             :         assert(strcmp(esc_shell(";", buffer), "\";\"") == 0);
     224             :         assert(strcmp(esc_shell("cmd1;cmd2", buffer), "\"cmd1;cmd2\"") == 0);
     225             : 
     226             :         /* backslash - no quotes needed when alone or in path */
     227             :         assert(strcmp(esc_shell("\\", buffer), "\\") == 0);
     228             :         assert(strcmp(esc_shell("C:\\", buffer), "C:\\") == 0);
     229             :         assert(strcmp(esc_shell("C:\\Users", buffer), "C:\\Users") == 0);
     230             :         assert(strcmp(esc_shell("path\\to\\file", buffer), "path\\to\\file") == 0);
     231             :         assert(strcmp(esc_shell("C:\\folder\\", buffer), "C:\\folder\\") == 0);
     232             : 
     233             :         /* backslash with space - requires quoting, normal backslash inside */
     234             :         assert(strcmp(esc_shell("\\ ", buffer), "\"\\ \"") == 0);
     235             :         assert(strcmp(esc_shell("C:\\ ", buffer), "\"C:\\ \"") == 0);
     236             :         assert(strcmp(esc_shell("C:\\Program Files", buffer), "\"C:\\Program Files\"") == 0);
     237             : 
     238             :         /* trailing backslash with quotes - backslashes before closing quote must be doubled */
     239             :         assert(strcmp(esc_shell("C:\\folder\\ ", buffer), "\"C:\\folder\\ \"") == 0);
     240             :         assert(strcmp(esc_shell("path\\ ", buffer), "\"path\\ \"") == 0);
     241             :         assert(strcmp(esc_shell("C:\\My Documents\\", buffer), "\"C:\\My Documents\\\\\"") == 0);
     242             : 
     243             :         /* backslash before embedded quote - backslash before quote must be doubled */
     244             :         assert(strcmp(esc_shell("C:\\\"test\"", buffer), "\"C:\\\\\\\"test\\\"\"") == 0);
     245             :         assert(strcmp(esc_shell("path\\\"file\"", buffer), "\"path\\\\\\\"file\\\"\"") == 0);
     246             : 
     247             :         /* multiple trailing backslashes before end with quotes */
     248             :         assert(strcmp(esc_shell("test\\\\ ", buffer), "\"test\\\\ \"") == 0);
     249             :         assert(strcmp(esc_shell("path\\\\\\\\ ", buffer), "\"path\\\\\\\\ \"") == 0);
     250             : 
     251             :         /* backslash NOT before quote - normal backslash */
     252             :         assert(strcmp(esc_shell("test\\file ", buffer), "\"test\\file \"") == 0);
     253             :         assert(strcmp(esc_shell("a\\b c", buffer), "\"a\\b c\"") == 0);
     254             : 
     255             :         /* control characters - require quoting */
     256             :         assert(strcmp(esc_shell("\x01", buffer), "\"\x01\"") == 0);
     257             :         assert(strcmp(esc_shell("\x1F", buffer), "\"\x1F\"") == 0);
     258             :         assert(strcmp(esc_shell("\x7F", buffer), "\"\x7F\"") == 0); /* DEL character */
     259             :         assert(strcmp(esc_shell("test\x01test", buffer), "\"test\x01test\"") == 0);
     260             : 
     261             :         /* complex real-world examples */
     262             :         assert(strcmp(esc_shell("C:\\Program Files\\App", buffer), "\"C:\\Program Files\\App\"") == 0);
     263             :         assert(strcmp(esc_shell("C:\\Program Files (x86)\\", buffer), "\"C:\\Program Files (x86)\\\\\"") == 0);
     264             :         assert(strcmp(esc_shell("file (copy).txt", buffer), "\"file (copy).txt\"") == 0);
     265             :         assert(strcmp(esc_shell("setup-v1.0.exe", buffer), "setup-v1.0.exe") == 0);
     266             :         assert(strcmp(esc_shell("setup v1.0.exe", buffer), "\"setup v1.0.exe\"") == 0);
     267             : 
     268             :         /* mixed quotes and special chars */
     269             :         assert(strcmp(esc_shell("say \"hi\" & exit", buffer), "\"say \\\"hi\\\" & exit\"") == 0);
     270             :         assert(strcmp(esc_shell("test \"a|b\"", buffer), "\"test \\\"a|b\\\"\"") == 0);
     271             : 
     272             :         /* empty string */
     273             :         assert(strcmp(esc_shell("", buffer), "") == 0);
     274             : 
     275             :         /* all safe characters that don't need escaping */
     276             :         assert(strcmp(esc_shell("abcdefghijklmnopqrstuvwxyz", buffer), "abcdefghijklmnopqrstuvwxyz") == 0);
     277             :         assert(strcmp(esc_shell("ABCDEFGHIJKLMNOPQRSTUVWXYZ", buffer), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 0);
     278             :         assert(strcmp(esc_shell("0123456789", buffer), "0123456789") == 0);
     279             :         assert(strcmp(esc_shell("._-+,@:", buffer), "._-+,@:") == 0);
     280             : #else
     281             :         /* basic cases - no special characters */
     282           1 :         assert(strcmp(esc_shell("simple", buffer), "simple") == 0);
     283           1 :         assert(strcmp(esc_shell("file.txt", buffer), "file.txt") == 0);
     284           1 :         assert(strcmp(esc_shell("file123", buffer), "file123") == 0);
     285           1 :         assert(strcmp(esc_shell("file_name-test.doc", buffer), "file_name-test.doc") == 0);
     286           1 :         assert(strcmp(esc_shell(",._+:@/-", buffer), ",._+:@/-") == 0);
     287           1 :         assert(strcmp(esc_shell("/usr/local/bin", buffer), "/usr/local/bin") == 0);
     288             : 
     289             :         /* empty string */
     290           1 :         assert(strcmp(esc_shell("", buffer), "") == 0);
     291             : 
     292             :         /* space - escape with backslash */
     293           1 :         assert(strcmp(esc_shell(" ", buffer), "\\ ") == 0);
     294           1 :         assert(strcmp(esc_shell("file name.txt", buffer), "file\\ name.txt") == 0);
     295           1 :         assert(strcmp(esc_shell("my document.doc", buffer), "my\\ document.doc") == 0);
     296           1 :         assert(strcmp(esc_shell("  spaces  ", buffer), "\\ \\ spaces\\ \\ ") == 0);
     297           1 :         assert(strcmp(esc_shell("a b c", buffer), "a\\ b\\ c") == 0);
     298             : 
     299             :         /* tab - escape with backslash */
     300           1 :         assert(strcmp(esc_shell("\t", buffer), "\\\t") == 0);
     301           1 :         assert(strcmp(esc_shell("file\tname", buffer), "file\\\tname") == 0);
     302           1 :         assert(strcmp(esc_shell("\t\t", buffer), "\\\t\\\t") == 0);
     303             : 
     304             :         /* newline - escape with backslash */
     305           1 :         assert(strcmp(esc_shell("\n", buffer), "\\\n") == 0);
     306           1 :         assert(strcmp(esc_shell("line1\nline2", buffer), "line1\\\nline2") == 0);
     307           1 :         assert(strcmp(esc_shell("\n\n", buffer), "\\\n\\\n") == 0);
     308             : 
     309             :         /* carriage return - escape with backslash */
     310           1 :         assert(strcmp(esc_shell("\r", buffer), "\\\r") == 0);
     311           1 :         assert(strcmp(esc_shell("text\r\n", buffer), "text\\\r\\\n") == 0);
     312             : 
     313             :         /* tilde (home directory expansion) */
     314           1 :         assert(strcmp(esc_shell("~", buffer), "\\~") == 0);
     315           1 :         assert(strcmp(esc_shell("~/file", buffer), "\\~/file") == 0);
     316           1 :         assert(strcmp(esc_shell("file~name", buffer), "file\\~name") == 0);
     317           1 :         assert(strcmp(esc_shell("~user", buffer), "\\~user") == 0);
     318             : 
     319             :         /* backtick (command substitution) */
     320           1 :         assert(strcmp(esc_shell("`", buffer), "\\`") == 0);
     321           1 :         assert(strcmp(esc_shell("`command`", buffer), "\\`command\\`") == 0);
     322           1 :         assert(strcmp(esc_shell("test`test", buffer), "test\\`test") == 0);
     323           1 :         assert(strcmp(esc_shell("``", buffer), "\\`\\`") == 0);
     324             : 
     325             :         /* hash (comment) */
     326           1 :         assert(strcmp(esc_shell("#", buffer), "\\#") == 0);
     327           1 :         assert(strcmp(esc_shell("#comment", buffer), "\\#comment") == 0);
     328           1 :         assert(strcmp(esc_shell("file#name", buffer), "file\\#name") == 0);
     329           1 :         assert(strcmp(esc_shell("test#123", buffer), "test\\#123") == 0);
     330             : 
     331             :         /* dollar sign (variable expansion) */
     332           1 :         assert(strcmp(esc_shell("$", buffer), "\\$") == 0);
     333           1 :         assert(strcmp(esc_shell("$$", buffer), "\\$\\$") == 0);
     334           1 :         assert(strcmp(esc_shell("$VAR", buffer), "\\$VAR") == 0);
     335           1 :         assert(strcmp(esc_shell("${VAR}", buffer), "\\$\\{VAR\\}") == 0);
     336           1 :         assert(strcmp(esc_shell("test$test", buffer), "test\\$test") == 0);
     337           1 :         assert(strcmp(esc_shell("$1", buffer), "\\$1") == 0);
     338           1 :         assert(strcmp(esc_shell("$PATH", buffer), "\\$PATH") == 0);
     339             : 
     340             :         /* ampersand (background job) */
     341           1 :         assert(strcmp(esc_shell("&", buffer), "\\&") == 0);
     342           1 :         assert(strcmp(esc_shell("&&", buffer), "\\&\\&") == 0);
     343           1 :         assert(strcmp(esc_shell("file&name", buffer), "file\\&name") == 0);
     344           1 :         assert(strcmp(esc_shell("a&b&c", buffer), "a\\&b\\&c") == 0);
     345           1 :         assert(strcmp(esc_shell("cmd1 & cmd2", buffer), "cmd1\\ \\&\\ cmd2") == 0);
     346             : 
     347             :         /* asterisk (wildcard) */
     348           1 :         assert(strcmp(esc_shell("*", buffer), "\\*") == 0);
     349           1 :         assert(strcmp(esc_shell("**", buffer), "\\*\\*") == 0);
     350           1 :         assert(strcmp(esc_shell("*.txt", buffer), "\\*.txt") == 0);
     351           1 :         assert(strcmp(esc_shell("file*name", buffer), "file\\*name") == 0);
     352           1 :         assert(strcmp(esc_shell("test*", buffer), "test\\*") == 0);
     353             : 
     354             :         /* parentheses (subshell) */
     355           1 :         assert(strcmp(esc_shell("(", buffer), "\\(") == 0);
     356           1 :         assert(strcmp(esc_shell(")", buffer), "\\)") == 0);
     357           1 :         assert(strcmp(esc_shell("()", buffer), "\\(\\)") == 0);
     358           1 :         assert(strcmp(esc_shell("(test)", buffer), "\\(test\\)") == 0);
     359           1 :         assert(strcmp(esc_shell("file(1)", buffer), "file\\(1\\)") == 0);
     360           1 :         assert(strcmp(esc_shell("(a)(b)", buffer), "\\(a\\)\\(b\\)") == 0);
     361             : 
     362             :         /* backslash (escape character) */
     363           1 :         assert(strcmp(esc_shell("\\", buffer), "\\\\") == 0);
     364           1 :         assert(strcmp(esc_shell("\\\\", buffer), "\\\\\\\\") == 0);
     365           1 :         assert(strcmp(esc_shell("path\\to\\file", buffer), "path\\\\to\\\\file") == 0);
     366           1 :         assert(strcmp(esc_shell("test\\test", buffer), "test\\\\test") == 0);
     367           1 :         assert(strcmp(esc_shell("a\\b\\c", buffer), "a\\\\b\\\\c") == 0);
     368             : 
     369             :         /* pipe (pipeline) */
     370           1 :         assert(strcmp(esc_shell("|", buffer), "\\|") == 0);
     371           1 :         assert(strcmp(esc_shell("||", buffer), "\\|\\|") == 0);
     372           1 :         assert(strcmp(esc_shell("file|name", buffer), "file\\|name") == 0);
     373           1 :         assert(strcmp(esc_shell("a|b|c", buffer), "a\\|b\\|c") == 0);
     374           1 :         assert(strcmp(esc_shell("cmd1 | cmd2", buffer), "cmd1\\ \\|\\ cmd2") == 0);
     375             : 
     376             :         /* square brackets (wildcard) */
     377           1 :         assert(strcmp(esc_shell("[", buffer), "\\[") == 0);
     378           1 :         assert(strcmp(esc_shell("]", buffer), "\\]") == 0);
     379           1 :         assert(strcmp(esc_shell("[]", buffer), "\\[\\]") == 0);
     380           1 :         assert(strcmp(esc_shell("[abc]", buffer), "\\[abc\\]") == 0);
     381           1 :         assert(strcmp(esc_shell("file[1]", buffer), "file\\[1\\]") == 0);
     382           1 :         assert(strcmp(esc_shell("[0-9]", buffer), "\\[0-9\\]") == 0);
     383             : 
     384             :         /* curly braces (brace expansion) */
     385           1 :         assert(strcmp(esc_shell("{", buffer), "\\{") == 0);
     386           1 :         assert(strcmp(esc_shell("}", buffer), "\\}") == 0);
     387           1 :         assert(strcmp(esc_shell("{}", buffer), "\\{\\}") == 0);
     388           1 :         assert(strcmp(esc_shell("{a,b,c}", buffer), "\\{a,b,c\\}") == 0);
     389           1 :         assert(strcmp(esc_shell("file{1,2}", buffer), "file\\{1,2\\}") == 0);
     390           1 :         assert(strcmp(esc_shell("{1..10}", buffer), "\\{1..10\\}") == 0);
     391             : 
     392             :         /* semicolon (command separator) */
     393           1 :         assert(strcmp(esc_shell(";", buffer), "\\;") == 0);
     394           1 :         assert(strcmp(esc_shell(";;", buffer), "\\;\\;") == 0);
     395           1 :         assert(strcmp(esc_shell("cmd1;cmd2", buffer), "cmd1\\;cmd2") == 0);
     396           1 :         assert(strcmp(esc_shell("test;test", buffer), "test\\;test") == 0);
     397           1 :         assert(strcmp(esc_shell("a; b", buffer), "a\\;\\ b") == 0);
     398             : 
     399             :         /* single quote */
     400           1 :         assert(strcmp(esc_shell("'", buffer), "\\'") == 0);
     401           1 :         assert(strcmp(esc_shell("''", buffer), "\\'\\'") == 0);
     402           1 :         assert(strcmp(esc_shell("'test'", buffer), "\\'test\\'") == 0);
     403           1 :         assert(strcmp(esc_shell("file'name", buffer), "file\\'name") == 0);
     404           1 :         assert(strcmp(esc_shell("it's", buffer), "it\\'s") == 0);
     405             : 
     406             :         /* double quote */
     407           1 :         assert(strcmp(esc_shell("\"", buffer), "\\\"") == 0);
     408           1 :         assert(strcmp(esc_shell("\"\"", buffer), "\\\"\\\"") == 0);
     409           1 :         assert(strcmp(esc_shell("\"test\"", buffer), "\\\"test\\\"") == 0);
     410           1 :         assert(strcmp(esc_shell("file\"name", buffer), "file\\\"name") == 0);
     411           1 :         assert(strcmp(esc_shell("say \"hi\"", buffer), "say\\ \\\"hi\\\"") == 0);
     412             : 
     413             :         /* angle brackets (redirection) */
     414           1 :         assert(strcmp(esc_shell("<", buffer), "\\<") == 0);
     415           1 :         assert(strcmp(esc_shell(">", buffer), "\\>") == 0);
     416           1 :         assert(strcmp(esc_shell("<<", buffer), "\\<\\<") == 0);
     417           1 :         assert(strcmp(esc_shell(">>", buffer), "\\>\\>") == 0);
     418           1 :         assert(strcmp(esc_shell("a<b>c", buffer), "a\\<b\\>c") == 0);
     419           1 :         assert(strcmp(esc_shell("file>output", buffer), "file\\>output") == 0);
     420           1 :         assert(strcmp(esc_shell("cmd < in > out", buffer), "cmd\\ \\<\\ in\\ \\>\\ out") == 0);
     421             : 
     422             :         /* question mark (wildcard) */
     423           1 :         assert(strcmp(esc_shell("?", buffer), "\\?") == 0);
     424           1 :         assert(strcmp(esc_shell("??", buffer), "\\?\\?") == 0);
     425           1 :         assert(strcmp(esc_shell("file?.txt", buffer), "file\\?.txt") == 0);
     426           1 :         assert(strcmp(esc_shell("test?test", buffer), "test\\?test") == 0);
     427           1 :         assert(strcmp(esc_shell("file??", buffer), "file\\?\\?") == 0);
     428             : 
     429             :         /* equals sign (assignment in some contexts) */
     430           1 :         assert(strcmp(esc_shell("=", buffer), "\\=") == 0);
     431           1 :         assert(strcmp(esc_shell("==", buffer), "\\=\\=") == 0);
     432           1 :         assert(strcmp(esc_shell("VAR=value", buffer), "VAR\\=value") == 0);
     433           1 :         assert(strcmp(esc_shell("a=b", buffer), "a\\=b") == 0);
     434           1 :         assert(strcmp(esc_shell("PATH=/usr/bin", buffer), "PATH\\=/usr/bin") == 0);
     435             : 
     436             :         /* exclamation mark (history expansion) */
     437           1 :         assert(strcmp(esc_shell("!", buffer), "\\!") == 0);
     438           1 :         assert(strcmp(esc_shell("!!", buffer), "\\!\\!") == 0);
     439           1 :         assert(strcmp(esc_shell("test!test", buffer), "test\\!test") == 0);
     440           1 :         assert(strcmp(esc_shell("!$", buffer), "\\!\\$") == 0);
     441           1 :         assert(strcmp(esc_shell("!123", buffer), "\\!123") == 0);
     442             : 
     443             :         /* control characters (0x01-0x1F) - escape with backslash */
     444           1 :         assert(strcmp(esc_shell("\x01", buffer), "\\\x01") == 0);
     445           1 :         assert(strcmp(esc_shell("\x02", buffer), "\\\x02") == 0);
     446           1 :         assert(strcmp(esc_shell("\x1F", buffer), "\\\x1F") == 0);
     447           1 :         assert(strcmp(esc_shell("test\x01test", buffer), "test\\\x01test") == 0);
     448             : 
     449             :         /* DEL character (0x7F) */
     450           1 :         assert(strcmp(esc_shell("\x7F", buffer), "\\\x7F") == 0);
     451           1 :         assert(strcmp(esc_shell("test\x7Ftest", buffer), "test\\\x7Ftest") == 0);
     452             : 
     453             :         /* multiple special characters combined */
     454           1 :         assert(strcmp(esc_shell("$VAR & $OTHER", buffer), "\\$VAR\\ \\&\\ \\$OTHER") == 0);
     455           1 :         assert(strcmp(esc_shell("*.txt | grep test", buffer), "\\*.txt\\ \\|\\ grep\\ test") == 0);
     456           1 :         assert(strcmp(esc_shell("file (1) [copy].txt", buffer), "file\\ \\(1\\)\\ \\[copy\\].txt") == 0);
     457           1 :         assert(strcmp(esc_shell("a & b | c", buffer), "a\\ \\&\\ b\\ \\|\\ c") == 0);
     458           1 :         assert(strcmp(esc_shell("cmd1; cmd2 && cmd3", buffer), "cmd1\\;\\ cmd2\\ \\&\\&\\ cmd3") == 0);
     459             : 
     460             :         /* complex real-world examples */
     461           1 :         assert(strcmp(esc_shell("/home/user/My Documents", buffer), "/home/user/My\\ Documents") == 0);
     462           1 :         assert(strcmp(esc_shell("/path/to/file (copy).txt", buffer), "/path/to/file\\ \\(copy\\).txt") == 0);
     463           1 :         assert(strcmp(esc_shell("~/project/file-v1.0.tar.gz", buffer), "\\~/project/file-v1.0.tar.gz") == 0);
     464           1 :         assert(strcmp(esc_shell("$(whoami)@$(hostname)", buffer), "\\$\\(whoami\\)@\\$\\(hostname\\)") == 0);
     465           1 :         assert(strcmp(esc_shell("test && echo 'done'", buffer), "test\\ \\&\\&\\ echo\\ \\'done\\'") == 0);
     466           1 :         assert(strcmp(esc_shell("file #1 [important].txt", buffer), "file\\ \\#1\\ \\[important\\].txt") == 0);
     467           1 :         assert(strcmp(esc_shell("/tmp/test (1).txt", buffer), "/tmp/test\\ \\(1\\).txt") == 0);
     468           1 :         assert(strcmp(esc_shell("var=$HOME/bin:$PATH", buffer), "var\\=\\$HOME/bin:\\$PATH") == 0);
     469             : 
     470             :         /* edge cases with multiple escapes */
     471           1 :         assert(strcmp(esc_shell("a\\ b", buffer), "a\\\\\\ b") == 0);
     472           1 :         assert(strcmp(esc_shell("'\"test\"'", buffer), "\\'\\\"test\\\"\\'") == 0);
     473           1 :         assert(strcmp(esc_shell("$(echo \"test\")", buffer), "\\$\\(echo\\ \\\"test\\\"\\)") == 0);
     474             : 
     475             :         /* all safe characters that don't need escaping */
     476           1 :         assert(strcmp(esc_shell("abcdefghijklmnopqrstuvwxyz", buffer), "abcdefghijklmnopqrstuvwxyz") == 0);
     477           1 :         assert(strcmp(esc_shell("ABCDEFGHIJKLMNOPQRSTUVWXYZ", buffer), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 0);
     478           1 :         assert(strcmp(esc_shell("0123456789", buffer), "0123456789") == 0);
     479           1 :         assert(strcmp(esc_shell("._-+,@:", buffer), "._-+,@:") == 0);
     480           1 :         assert(strcmp(esc_shell("/path/to/file", buffer), "/path/to/file") == 0);
     481           1 :         assert(strcmp(esc_shell("simple_file-name.txt", buffer), "simple_file-name.txt") == 0);
     482             : 
     483             :         /* file extensions and versions */
     484           1 :         assert(strcmp(esc_shell("file.tar.gz", buffer), "file.tar.gz") == 0);
     485           1 :         assert(strcmp(esc_shell("app-v1.2.3.deb", buffer), "app-v1.2.3.deb") == 0);
     486           1 :         assert(strcmp(esc_shell("test_2024-01-01.log", buffer), "test_2024-01-01.log") == 0);
     487             : #endif
     488             : 
     489           1 :         printf("Everything OK\n");
     490             : 
     491           1 :         exit(EXIT_SUCCESS);
     492             : }
     493             : 
     494             : /****************************************************************************/
     495             : /* log */
     496             : 
     497         278 : void log_open(const char* file)
     498             : {
     499             :         char path[PATH_MAX];
     500             :         const char* mode;
     501             :         char text_T[32];
     502             :         char text_D[32];
     503             :         time_t t;
     504             :         struct tm* tm;
     505             : #if HAVE_LOCALTIME_R
     506             :         struct tm tm_res;
     507             : #endif
     508             : 
     509             :         /* leave stdlog at 0 if not specified */
     510         278 :         if (file == 0)
     511         204 :                 return;
     512             : 
     513          80 :         t = time(0);
     514             : #if HAVE_LOCALTIME_R
     515          80 :         tm = localtime_r(&t, &tm_res);
     516             : #else
     517             :         tm = localtime(&t);
     518             : #endif
     519          80 :         if (tm) {
     520          80 :                 strftime(text_T, sizeof(text_T), "%H%M%S", tm);
     521          80 :                 strftime(text_D, sizeof(text_T), "%Y%m%d", tm);
     522             :         } else {
     523             :                 /* LCOV_EXCL_START */
     524             :                 strcpy(text_T, "invalid");
     525             :                 strcpy(text_D, "invalid");
     526             :                 /* LCOV_EXCL_STOP */
     527             :         }
     528             : 
     529             :         /* file mode */
     530          80 :         mode = "wt";
     531          80 :         if (*file == '>') {
     532           7 :                 ++file;
     533             : 
     534           7 :                 if (*file == '>') {
     535           1 :                         mode = "at";
     536           1 :                         ++file;
     537             :                 }
     538             : 
     539           7 :                 if (file[0] == '&' && file[1] == '1') {
     540           5 :                         stdlog = stdout;
     541           5 :                         return;
     542             :                 }
     543             : 
     544           2 :                 if (file[0] == '&' && file[1] == '2') {
     545           1 :                         stdlog = stderr;
     546           1 :                         return;
     547             :                 }
     548             :         }
     549             : 
     550             :         /* process the path */
     551         864 :         for (*path = 0; *file != 0; ) {
     552         790 :                 switch (*file) {
     553           2 :                 case '%' :
     554           2 :                         ++file;
     555           2 :                         switch (*file) {
     556           0 :                         case '%' :
     557           0 :                                 pathcatc(path, sizeof(path), '%');
     558           0 :                                 break;
     559           1 :                         case 'T' :
     560           1 :                                 pathcat(path, sizeof(path), text_T);
     561           1 :                                 break;
     562           1 :                         case 'D' :
     563           1 :                                 pathcat(path, sizeof(path), text_D);
     564           1 :                                 break;
     565           0 :                         default :
     566             :                                 /* LCOV_EXCL_START */
     567             :                                 log_fatal(EUSER, "Invalid type specifier '%c' in the log file.\n", *file);
     568             :                                 exit(EXIT_FAILURE);
     569             :                                 /* LCOV_EXCL_STOP */
     570             :                         }
     571           2 :                         break;
     572         788 :                 default :
     573         788 :                         pathcatc(path, sizeof(path), *file);
     574         788 :                         break;
     575             :                 }
     576         790 :                 ++file;
     577             :         }
     578             : 
     579          74 :         stdlog = fopen(path, mode);
     580          74 :         if (!stdlog) {
     581             :                 /* LCOV_EXCL_START */
     582             :                 log_fatal(errno, "Error opening the log file '%s'. %s.\n", path, strerror(errno));
     583             :                 exit(EXIT_FAILURE);
     584             :                 /* LCOV_EXCL_STOP */
     585             :         }
     586             : }
     587             : 
     588         267 : void log_close(const char* file)
     589             : {
     590         267 :         if (stdlog != stdout && stdlog != stderr && stdlog != 0) {
     591          74 :                 if (fclose(stdlog) != 0) {
     592             :                         /* LCOV_EXCL_START */
     593             :                         log_fatal(errno, "Error closing the log file '%s'. %s.\n", file, strerror(errno));
     594             :                         exit(EXIT_FAILURE);
     595             :                         /* LCOV_EXCL_STOP */
     596             :                 }
     597             :         }
     598             : 
     599         267 :         stdlog = 0;
     600         267 : }
     601             : 
     602             : /****************************************************************************/
     603             : /* config */
     604             : 
     605         287 : void config(char* conf, size_t conf_size, const char* argv0)
     606             : {
     607             : #ifdef _WIN32
     608             :         char* slash;
     609             : 
     610             :         pathimport(conf, conf_size, argv0);
     611             : 
     612             :         slash = strrchr(conf, '/');
     613             :         if (slash) {
     614             :                 slash[1] = 0;
     615             :                 pathcat(conf, conf_size, PACKAGE ".conf");
     616             :         } else {
     617             :                 pathcpy(conf, conf_size, PACKAGE ".conf");
     618             :         }
     619             : #else
     620             :         (void)argv0;
     621             : 
     622             : #ifdef SYSCONFDIR
     623             :         /* if it exists, give precedence at sysconfdir, usually /usr/local/etc */
     624         287 :         if (access(SYSCONFDIR "/" PACKAGE ".conf", F_OK) == 0) {
     625           0 :                 pathcpy(conf, conf_size, SYSCONFDIR "/" PACKAGE ".conf");
     626             :         } else /* otherwise fallback to plain /etc */
     627             : #endif
     628             :         {
     629         287 :                 pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf");
     630             :         }
     631             : #endif
     632         287 : }
     633             : 
     634             : /****************************************************************************/
     635             : /* main */
     636             : 
     637             : /**
     638             :  * Only long options
     639             :  */
     640             : #define OPT_NO_WARNINGS 500
     641             : #define OPT_GUI 501
     642             : #define OPT_GUI_VERBOSE 502
     643             : #define OPT_GUI_RESCAN_AFTER 503
     644             : #define OPT_GUI_THRESHOLD_REMOVES 504
     645             : #define OPT_GUI_THRESHOLD_UPDATES 505
     646             : 
     647             : /**
     648             :  * Test options
     649             :  */
     650             : #define OPT_TEST_SKIP_SELF 256
     651             : #define OPT_TEST_KILL_AFTER_SYNC 257
     652             : #define OPT_TEST_EXPECT_UNRECOVERABLE 258
     653             : #define OPT_TEST_EXPECT_RECOVERABLE 259
     654             : #define OPT_TEST_SKIP_SIGN 260
     655             : #define OPT_TEST_SKIP_FALLOCATE 261
     656             : #define OPT_TEST_SKIP_DEVICE 262
     657             : #define OPT_TEST_FORCE_MURMUR3 264
     658             : #define OPT_TEST_FORCE_SPOOKY2 265
     659             : #define OPT_TEST_SKIP_LOCK 266
     660             : #define OPT_TEST_FORCE_ORDER_PHYSICAL 267
     661             : #define OPT_TEST_FORCE_ORDER_INODE 268
     662             : #define OPT_TEST_FORCE_ORDER_ALPHA 269
     663             : #define OPT_TEST_FORCE_ORDER_DIR 270
     664             : #define OPT_TEST_FORCE_SCRUB_AT 271
     665             : #define OPT_TEST_FORCE_SCRUB_EVEN 272
     666             : #define OPT_TEST_FORCE_CONTENT_WRITE 273
     667             : #define OPT_TEST_SKIP_CONTENT_CHECK 275
     668             : #define OPT_TEST_SKIP_PARITY_ACCESS 276
     669             : #define OPT_TEST_EXPECT_FAILURE 277
     670             : #define OPT_TEST_RUN 278
     671             : #define OPT_TEST_FORCE_SCAN_WINFIND 279
     672             : #define OPT_TEST_IMPORT_CONTENT 280
     673             : #define OPT_TEST_FORCE_PROGRESS 281
     674             : #define OPT_TEST_SKIP_DISK_ACCESS 282
     675             : #define OPT_TEST_FORCE_AUTOSAVE_AT 283
     676             : #define OPT_TEST_FAKE_DEVICE 284
     677             : #define OPT_TEST_EXPECT_NEED_SYNC 285
     678             : #define OPT_TEST_FAKE_UUID 287
     679             : #define OPT_TEST_MATCH_FIRST_UUID 288
     680             : #define OPT_TEST_FORCE_PARITY_UPDATE 289
     681             : #define OPT_TEST_IO_CACHE 290
     682             : #define OPT_TEST_IO_STATS 291
     683             : #define OPT_TEST_COND_SIGNAL_OUTSIDE 292
     684             : #define OPT_TEST_IO_ADVISE_NONE 293
     685             : #define OPT_TEST_IO_ADVISE_SEQUENTIAL 294
     686             : #define OPT_TEST_IO_ADVISE_FLUSH 295
     687             : #define OPT_TEST_IO_ADVISE_FLUSH_WINDOW 296
     688             : #define OPT_TEST_IO_ADVISE_DISCARD 297
     689             : #define OPT_TEST_IO_ADVISE_DISCARD_WINDOW 298
     690             : #define OPT_TEST_IO_ADVISE_DIRECT 299
     691             : #define OPT_TEST_PARITY_LIMIT 301
     692             : #define OPT_TEST_SKIP_CONTENT_WRITE 302
     693             : #define OPT_TEST_SKIP_SPACE_HOLDER 303
     694             : #define OPT_TEST_FORMAT 304
     695             : #define OPT_TEST_SKIP_MULTI_SCAN 305
     696             : #define OPT_TEST_SPEED_PERIOD 307
     697             : #define OPT_TEST_SPEED_DISKS_NUMBER 308
     698             : #define OPT_TEST_SPEED_BLOCKS_SIZE 309
     699             : 
     700             : 
     701             : #if HAVE_GETOPT_LONG
     702             : static struct option long_options[] = {
     703             :         { "conf", 1, 0, 'c' },
     704             :         { "filter", 1, 0, 'f' },
     705             :         { "filter-disk", 1, 0, 'd' },
     706             :         { "filter-missing", 0, 0, 'm' },
     707             :         { "filter-error", 0, 0, 'e' },
     708             :         { "filter-block-error", 0, 0, 'b' },
     709             :         { "percentage", 1, 0, 'p' }, /* legacy name for --plan */
     710             :         { "plan", 1, 0, 'p' },
     711             :         { "older-than", 1, 0, 'o' },
     712             :         { "start", 1, 0, 'S' },
     713             :         { "count", 1, 0, 'B' },
     714             :         { "error-limit", 1, 0, 'L' },
     715             :         { "import", 1, 0, 'i' },
     716             :         { "log", 1, 0, 'l' },
     717             :         { "stats", 0, 0, 'A' },
     718             :         { "force-zero", 0, 0, 'Z' },
     719             :         { "force-empty", 0, 0, 'E' },
     720             :         { "force-uuid", 0, 0, 'U' },
     721             :         { "force-device", 0, 0, 'D' },
     722             :         { "force-nocopy", 0, 0, 'N' },
     723             :         { "force-full", 0, 0, 'F' },
     724             :         { "force-realloc", 0, 0, 'R' },
     725             :         { "force-realloc-tail", 1, 0, 'W' },
     726             :         { "bw-limit", 1, 0, 'w' },
     727             :         { "audit-only", 0, 0, 'a' },
     728             :         { "pre-hash", 0, 0, 'h' },
     729             :         { "tail", 1, 0, 't' },
     730             :         { "speed-test", 0, 0, 'T' }, /* undocumented speed test command */
     731             :         { "speed-test-period", 1, 0, OPT_TEST_SPEED_PERIOD }, /* for how many milliseconds test each feature. Default 1000. */
     732             :         { "speed-test-disks-number", 1, 0, OPT_TEST_SPEED_DISKS_NUMBER }, /* how many disk number uses in the speed test. Default 8. */
     733             :         { "speed-test-blocks-size", 1, 0, OPT_TEST_SPEED_BLOCKS_SIZE }, /* how big in kBytes should be the blocks in the speed test. Default 256. */
     734             :         { "gen-conf", 1, 0, 'C' },
     735             :         { "verbose", 0, 0, 'v' },
     736             :         { "quiet", 0, 0, 'q' },
     737             :         { "help", 0, 0, 'H' },
     738             :         { "version", 0, 0, 'V' },
     739             : 
     740             : 
     741             :         { "no-warnings", 0, 0, OPT_NO_WARNINGS }, /* disable annoying warnings */
     742             :         { "gui", 0, 0, OPT_GUI }, /* undocumented GUI interface option (it was also 'G' in the past) */
     743             :         { "gui-verbose", 0, 0, OPT_GUI_VERBOSE }, /* undocumented GUI interface option */
     744             :         { "gui-rescan-after", 0, 0, OPT_GUI_RESCAN_AFTER }, /* undocumented GUI, force a rescan after the command to log differences */
     745             :         { "gui-threshold-removes", 1, 0, OPT_GUI_THRESHOLD_REMOVES }, /* abort sync if too many removed files */
     746             :         { "gui-threshold-updates", 1, 0, OPT_GUI_THRESHOLD_UPDATES }, /* abort sync if too many updated files */
     747             : 
     748             :         /* The following are test specific options, DO NOT USE! */
     749             : 
     750             :         /* After syncing, do not write the new content file */
     751             :         { "test-kill-after-sync", 0, 0, OPT_TEST_KILL_AFTER_SYNC },
     752             : 
     753             :         /* Exit with failure if after check/fix there ARE NOT unrecoverable errors. */
     754             :         { "test-expect-unrecoverable", 0, 0, OPT_TEST_EXPECT_UNRECOVERABLE },
     755             : 
     756             :         /* Exit with failure if after check/fix there ARE NOT recoverable errors. */
     757             :         { "test-expect-recoverable", 0, 0, OPT_TEST_EXPECT_RECOVERABLE },
     758             : 
     759             :         /* Skip the initial self test */
     760             :         { "test-skip-self", 0, 0, OPT_TEST_SKIP_SELF },
     761             : 
     762             :         /* Skip the initial sign check when reading the content file */
     763             :         { "test-skip-sign", 0, 0, OPT_TEST_SKIP_SIGN },
     764             : 
     765             :         /* Skip the fallocate() when growing the parity files */
     766             :         { "test-skip-fallocate", 0, 0, OPT_TEST_SKIP_FALLOCATE },
     767             : 
     768             :         /* Skip the device check */
     769             :         { "test-skip-device", 0, 0, OPT_TEST_SKIP_DEVICE },
     770             : 
     771             :         /* Force Murmur3 hash */
     772             :         { "test-force-murmur3", 0, 0, OPT_TEST_FORCE_MURMUR3 },
     773             : 
     774             :         /* Force Spooky2 hash */
     775             :         { "test-force-spooky2", 0, 0, OPT_TEST_FORCE_SPOOKY2 },
     776             : 
     777             :         /* Skip the use of lock file */
     778             :         { "test-skip-lock", 0, 0, OPT_TEST_SKIP_LOCK },
     779             : 
     780             :         /* Force a sort order for files */
     781             :         { "test-force-order-physical", 0, 0, OPT_TEST_FORCE_ORDER_PHYSICAL },
     782             :         { "test-force-order-inode", 0, 0, OPT_TEST_FORCE_ORDER_INODE },
     783             :         { "test-force-order-alpha", 0, 0, OPT_TEST_FORCE_ORDER_ALPHA },
     784             :         { "test-force-order-dir", 0, 0, OPT_TEST_FORCE_ORDER_DIR },
     785             : 
     786             :         /* Force scrub of the specified number of blocks */
     787             :         { "test-force-scrub-at", 1, 0, OPT_TEST_FORCE_SCRUB_AT },
     788             : 
     789             :         /* Force scrub of all the even blocks. This is really for testing, don't try it */
     790             :         { "test-force-scrub-even", 0, 0, OPT_TEST_FORCE_SCRUB_EVEN },
     791             : 
     792             :         /* Force write of the content file even if no modification is done */
     793             :         { "test-force-content-write", 0, 0, OPT_TEST_FORCE_CONTENT_WRITE },
     794             : 
     795             :         /* Relax the checks done at the content file */
     796             :         { "test-skip-content-check", 0, 0, OPT_TEST_SKIP_CONTENT_CHECK },
     797             : 
     798             :         /* Skip the parity access */
     799             :         { "test-skip-parity-access", 0, 0, OPT_TEST_SKIP_PARITY_ACCESS },
     800             : 
     801             :         /* Exit generic failure */
     802             :         { "test-expect-failure", 0, 0, OPT_TEST_EXPECT_FAILURE },
     803             : 
     804             :         /* Exit generic need sync */
     805             :         { "test-expect-need-sync", 0, 0, OPT_TEST_EXPECT_NEED_SYNC },
     806             : 
     807             :         /* Run some command after loading the state and before the command */
     808             :         { "test-run", 1, 0, OPT_TEST_RUN },
     809             : 
     810             :         /* Use the FindFirst/Next approach in Windows to list files */
     811             :         { "test-force-scan-winfind", 0, 0, OPT_TEST_FORCE_SCAN_WINFIND },
     812             : 
     813             :         /* Alternative import working by data */
     814             :         { "test-import-content", 1, 0, OPT_TEST_IMPORT_CONTENT },
     815             : 
     816             :         /* Force immediate progress state update */
     817             :         { "test-force-progress", 0, 0, OPT_TEST_FORCE_PROGRESS },
     818             : 
     819             :         /* Skip the disk access */
     820             :         { "test-skip-disk-access", 0, 0, OPT_TEST_SKIP_DISK_ACCESS },
     821             : 
     822             :         /* Force autosave at the specified block */
     823             :         { "test-force-autosave-at", 1, 0, OPT_TEST_FORCE_AUTOSAVE_AT },
     824             : 
     825             :         /* Fake device data */
     826             :         { "test-fake-device", 0, 0, OPT_TEST_FAKE_DEVICE },
     827             : 
     828             :         /* Fake UUID */
     829             :         { "test-fake-uuid", 0, 0, OPT_TEST_FAKE_UUID },
     830             : 
     831             :         /* Match first UUID */
     832             :         { "test-match-first-uuid", 0, 0, OPT_TEST_MATCH_FIRST_UUID },
     833             : 
     834             :         /* Force parity update even if all the data hash is already matching */
     835             :         { "test-force-parity-update", 0, 0, OPT_TEST_FORCE_PARITY_UPDATE },
     836             : 
     837             :         /* Number of IO buffers */
     838             :         { "test-io-cache", 1, 0, OPT_TEST_IO_CACHE },
     839             : 
     840             :         /* Print IO stats */
     841             :         { "test-io-stats", 0, 0, OPT_TEST_IO_STATS }, /* now replaced by -A, --stats */
     842             : 
     843             :         /* Signal condition variable outside the mutex */
     844             :         { "test-cond-signal-outside", 0, 0, OPT_TEST_COND_SIGNAL_OUTSIDE },
     845             : 
     846             :         /* Set the io advise to none */
     847             :         { "test-io-advise-none", 0, 0, OPT_TEST_IO_ADVISE_NONE },
     848             : 
     849             :         /* Set the io advise to sequential */
     850             :         { "test-io-advise-sequential", 0, 0, OPT_TEST_IO_ADVISE_SEQUENTIAL },
     851             : 
     852             :         /* Set the io advise to flush */
     853             :         { "test-io-advise-flush", 0, 0, OPT_TEST_IO_ADVISE_FLUSH },
     854             : 
     855             :         /* Set the io advise to flush window */
     856             :         { "test-io-advise-flush-window", 0, 0, OPT_TEST_IO_ADVISE_FLUSH_WINDOW },
     857             : 
     858             :         /* Set the io advise to discard */
     859             :         { "test-io-advise-discard", 0, 0, OPT_TEST_IO_ADVISE_DISCARD },
     860             : 
     861             :         /* Set the io advise to discard window */
     862             :         { "test-io-advise-discard-window", 0, 0, OPT_TEST_IO_ADVISE_DISCARD_WINDOW },
     863             : 
     864             :         /* Set the io advise to direct */
     865             :         { "test-io-advise-direct", 0, 0, OPT_TEST_IO_ADVISE_DIRECT },
     866             : 
     867             :         /* Set an artificial parity limit */
     868             :         { "test-parity-limit", 1, 0, OPT_TEST_PARITY_LIMIT },
     869             : 
     870             :         /* Skip content write */
     871             :         { "test-skip-content-write", 0, 0, OPT_TEST_SKIP_CONTENT_WRITE },
     872             : 
     873             :         /* Skip space holder file in parity disks */
     874             :         { "test-skip-space-holder", 0, 0, OPT_TEST_SKIP_SPACE_HOLDER },
     875             : 
     876             :         /* Set the output format */
     877             :         { "test-fmt", 1, 0, OPT_TEST_FORMAT },
     878             : 
     879             :         /* Skip thread in disk scan */
     880             :         { "test-skip-multi-scan", 0, 0, OPT_TEST_SKIP_MULTI_SCAN },
     881             : 
     882             :         { 0, 0, 0, 0 }
     883             : };
     884             : #endif
     885             : 
     886             : /*
     887             :  * Free letters: gIjJkKMnPQruxXz
     888             :  *
     889             :  * The 's' letter is used in main.c
     890             :  * The 'G' letter is free but only from 14.0
     891             :  */
     892             : #define OPTIONS "t:c:f:d:mebp:o:S:B:L:i:l:AZEUDNFRW:ahTC:vqHVw:"
     893             : 
     894           6 : int parse_option_size(const char* arg, uint64_t* out_size)
     895             : {
     896             :         char* e;
     897             : 
     898             :         /* parse the number part */
     899           6 :         data_off_t size = strtoul(optarg, &e, 10);
     900           6 :         if (!e || e == arg)
     901           0 :                 return -1;
     902             : 
     903             :         /* Handle suffixes */
     904           6 :         if ((e[0] == 'k' || e[0] == 'K') && e[1] == 0) {
     905           5 :                 size *= KILO;
     906           1 :         } else if ((e[0] == 'm' || e[0] == 'M') && e[1] == 0) {
     907           1 :                 size *= MEGA;
     908           0 :         } else if ((e[0] == 'g' || e[0] == 'G') && e[1] == 0) {
     909           0 :                 size *= GIGA;
     910           0 :         } else if ((e[0] == 't' || e[0] == 'T') && e[1] == 0) {
     911           0 :                 size *= TERA;
     912           0 :         } else if (e[0] != '\0') {
     913           0 :                 return -1;
     914             :         }
     915             : 
     916           6 :         *out_size = size;
     917           6 :         return 0;
     918             : }
     919             : 
     920             : volatile int global_interrupt = 0;
     921             : 
     922             : /* LCOV_EXCL_START */
     923             : void signal_handler(int signum)
     924             : {
     925             :         /* report the request of interruption with the signal received */
     926             :         global_interrupt = signum;
     927             : }
     928             : /* LCOV_EXCL_STOP */
     929             : 
     930         229 : void signal_init(void)
     931             : {
     932             : #if HAVE_SIGACTION
     933             :         struct sigaction sa;
     934             : 
     935         229 :         sa.sa_handler = signal_handler;
     936         229 :         sigemptyset(&sa.sa_mask);
     937             : 
     938             :         /* use the SA_RESTART to automatically restart interrupted system calls */
     939         229 :         sa.sa_flags = SA_RESTART;
     940             : 
     941         229 :         sigaction(SIGHUP, &sa, 0);
     942         229 :         sigaction(SIGTERM, &sa, 0);
     943         229 :         sigaction(SIGINT, &sa, 0);
     944             : #else
     945             :         signal(SIGINT, signal_handler);
     946             : #endif
     947         229 : }
     948             : 
     949             : #define OPERATION_DIFF 0
     950             : #define OPERATION_SYNC 1
     951             : #define OPERATION_CHECK 2
     952             : #define OPERATION_FIX 3
     953             : #define OPERATION_DRY 4
     954             : #define OPERATION_DUP 5
     955             : #define OPERATION_LIST 6
     956             : #define OPERATION_POOL 7
     957             : #define OPERATION_REHASH 8
     958             : #define OPERATION_SCRUB 9
     959             : #define OPERATION_STATUS 10
     960             : #define OPERATION_REWRITE 11
     961             : #define OPERATION_READ 12
     962             : #define OPERATION_TOUCH 13
     963             : #define OPERATION_SPINUP 14
     964             : #define OPERATION_SPINDOWN 15
     965             : #define OPERATION_DEVICES 16
     966             : #define OPERATION_SMART 17
     967             : #define OPERATION_PROBE 18
     968             : #define OPERATION_LOCATE 19
     969             : 
     970         288 : int snapraid_main(int argc, char* argv[])
     971             : {
     972             :         int c;
     973             :         struct snapraid_option opt;
     974             :         char conf[PATH_MAX];
     975             :         struct snapraid_state state;
     976             :         int operation;
     977             :         block_off_t blockstart;
     978             :         block_off_t blockcount;
     979             :         int ret;
     980             :         tommy_list filterlist_file;
     981             :         tommy_list filterlist_disk;
     982             :         int filter_missing;
     983             :         int filter_error;
     984             :         int plan100;
     985             :         int olderthan;
     986             :         char* e;
     987             :         const char* command;
     988             :         const char* import_timestamp;
     989             :         const char* import_content;
     990             :         const char* log_file;
     991             :         int lock;
     992             :         const char* gen_conf;
     993             :         const char* run;
     994             :         int speedtest;
     995             :         int speed_test_period;
     996             :         int speed_test_disks_number;
     997             :         int speed_test_blocks_size;
     998             :         time_t t;
     999             :         struct tm* tm;
    1000             : #if HAVE_LOCALTIME_R
    1001             :         struct tm tm_res;
    1002             : #endif
    1003             :         int i;
    1004             : 
    1005         288 :         test(argc, argv);
    1006             : 
    1007         287 :         lock_init();
    1008             : 
    1009             :         /* defaults */
    1010         287 :         config(conf, sizeof(conf), argv[0]);
    1011         287 :         memset(&opt, 0, sizeof(opt));
    1012         287 :         opt.io_error_limit = 100;
    1013         287 :         blockstart = 0;
    1014         287 :         blockcount = 0;
    1015         287 :         tommy_list_init(&filterlist_file);
    1016         287 :         tommy_list_init(&filterlist_disk);
    1017         287 :         speed_test_period = -1;
    1018         287 :         speed_test_blocks_size = -1;
    1019         287 :         speed_test_disks_number = -1;
    1020         287 :         filter_missing = 0;
    1021         287 :         filter_error = 0;
    1022         287 :         plan100 = SCRUB_AUTO;
    1023         287 :         olderthan = SCRUB_AUTO;
    1024         287 :         import_timestamp = 0;
    1025         287 :         import_content = 0;
    1026         287 :         log_file = 0;
    1027         287 :         lock = 0;
    1028         287 :         gen_conf = 0;
    1029         287 :         speedtest = 0;
    1030         287 :         run = 0;
    1031             : 
    1032         287 :         opterr = 0;
    1033         287 :         while ((c =
    1034             : #if HAVE_GETOPT_LONG
    1035        3607 :                 getopt_long(argc, argv, OPTIONS, long_options, 0))
    1036             : #else
    1037             :                 getopt(argc, argv, OPTIONS))
    1038             : #endif
    1039        3607 :                 != EOF) {
    1040        3324 :                 switch (c) {
    1041         280 :                 case 'c' :
    1042         280 :                         pathimport(conf, sizeof(conf), optarg);
    1043         280 :                         break;
    1044           5 :                 case 'f' : {
    1045           5 :                         struct snapraid_filter* filter = filter_alloc_file(1, "", optarg);
    1046           5 :                         if (!filter) {
    1047             :                                 /* LCOV_EXCL_START */
    1048             :                                 log_fatal(EUSER, "Invalid filter specification '%s'\n", optarg);
    1049             :                                 exit(EXIT_FAILURE);
    1050             :                                 /* LCOV_EXCL_STOP */
    1051             :                         }
    1052           3 :                         tommy_list_insert_tail(&filterlist_file, &filter->node, filter);
    1053           3 :                 } break;
    1054          10 :                 case 'd' : {
    1055          10 :                         struct snapraid_filter* filter = filter_alloc_disk(1, optarg);
    1056          10 :                         if (!filter) {
    1057             :                                 /* LCOV_EXCL_START */
    1058             :                                 log_fatal(EUSER, "Invalid filter specification '%s'\n", optarg);
    1059             :                                 exit(EXIT_FAILURE);
    1060             :                                 /* LCOV_EXCL_STOP */
    1061             :                         }
    1062          10 :                         tommy_list_insert_tail(&filterlist_disk, &filter->node, filter);
    1063          10 :                 } break;
    1064           2 :                 case 'm' :
    1065           2 :                         filter_missing = 1;
    1066           2 :                         opt.expected_missing = 1;
    1067           2 :                         break;
    1068           2 :                 case 'e' :
    1069             :                         /* when processing only error, we filter files */
    1070             :                         /* and we apply fixes only to synced ones */
    1071           2 :                         filter_error = 1;
    1072           2 :                         opt.badfileonly = 1;
    1073           2 :                         opt.syncedonly = 1;
    1074           2 :                         break;
    1075           0 :                 case 'b' :
    1076             :                         /* when processing only block with error, we filter both files and blocks */
    1077             :                         /* and we apply fixes only to synced ones */
    1078           0 :                         filter_error = 1;
    1079           0 :                         opt.badfileonly = 1;
    1080           0 :                         opt.badblockonly = 1;
    1081           0 :                         opt.syncedonly = 1;
    1082           0 :                         break;
    1083           5 :                 case 'p' :
    1084           5 :                         if (strcmp(optarg, "bad") == 0) {
    1085           1 :                                 plan100 = SCRUB_BAD;
    1086           4 :                         } else if (strcmp(optarg, "new") == 0) {
    1087           1 :                                 plan100 = SCRUB_NEW;
    1088           3 :                         } else if (strcmp(optarg, "full") == 0) {
    1089           2 :                                 plan100 = SCRUB_FULL;
    1090             :                         } else {
    1091           1 :                                 double plan_double = strtod(optarg, &e);
    1092           1 :                                 plan100 = plan_double * 100;
    1093           1 :                                 if (!e || *e || plan100 > 10000) {
    1094             :                                         /* LCOV_EXCL_START */
    1095             :                                         log_fatal(EUSER, "Invalid plan/percentage '%s'\n", optarg);
    1096             :                                         exit(EXIT_FAILURE);
    1097             :                                         /* LCOV_EXCL_STOP */
    1098             :                                 }
    1099             :                         }
    1100           5 :                         break;
    1101           1 :                 case 'o' :
    1102           1 :                         olderthan = strtoul(optarg, &e, 10);
    1103           1 :                         if (!e || *e || olderthan > 1000) {
    1104             :                                 /* LCOV_EXCL_START */
    1105             :                                 log_fatal(EUSER, "Invalid number of days '%s'\n", optarg);
    1106             :                                 exit(EXIT_FAILURE);
    1107             :                                 /* LCOV_EXCL_STOP */
    1108             :                         }
    1109           1 :                         break;
    1110           1 :                 case 'w' : /* --bw-limit */
    1111           1 :                         if (optarg == 0) {
    1112             :                                 /* LCOV_EXCL_START */
    1113             :                                 log_fatal(EUSER, "Missing bandwidth limit\n");
    1114             :                                 exit(EXIT_FAILURE);
    1115             :                                 /* LCOV_EXCL_STOP */
    1116             :                         }
    1117             : 
    1118           1 :                         if (parse_option_size(optarg, &opt.bwlimit) != 0) {
    1119             :                                 /* LCOV_EXCL_START */
    1120             :                                 log_fatal(EUSER, "Invalid bandwidth limit '%s'\n", optarg);
    1121             :                                 exit(EXIT_FAILURE);
    1122             :                                 /* LCOV_EXCL_STOP */
    1123             :                         }
    1124           1 :                         break;
    1125           2 :                 case 'S' :
    1126           2 :                         blockstart = strtoul(optarg, &e, 0);
    1127           2 :                         if (!e || *e) {
    1128             :                                 /* LCOV_EXCL_START */
    1129             :                                 log_fatal(EUSER, "Invalid start position '%s'\n", optarg);
    1130             :                                 exit(EXIT_FAILURE);
    1131             :                                 /* LCOV_EXCL_STOP */
    1132             :                         }
    1133           2 :                         break;
    1134           7 :                 case 'B' :
    1135           7 :                         blockcount = strtoul(optarg, &e, 0);
    1136           7 :                         if (!e || *e) {
    1137             :                                 /* LCOV_EXCL_START */
    1138             :                                 log_fatal(EUSER, "Invalid count number '%s'\n", optarg);
    1139             :                                 exit(EXIT_FAILURE);
    1140             :                                 /* LCOV_EXCL_STOP */
    1141             :                         }
    1142           7 :                         break;
    1143           0 :                 case 'L' :
    1144           0 :                         opt.io_error_limit = strtoul(optarg, &e, 0);
    1145           0 :                         if (!e || *e) {
    1146             :                                 /* LCOV_EXCL_START */
    1147             :                                 log_fatal(EUSER, "Invalid error limit number '%s'\n", optarg);
    1148             :                                 exit(EXIT_FAILURE);
    1149             :                                 /* LCOV_EXCL_STOP */
    1150             :                         }
    1151           0 :                         break;
    1152           1 :                 case 'i' :
    1153           1 :                         if (import_timestamp) {
    1154             :                                 /* LCOV_EXCL_START */
    1155             :                                 log_fatal(EUSER, "Import directory '%s' already specified as '%s'\n", optarg, import_timestamp);
    1156             :                                 exit(EXIT_FAILURE);
    1157             :                                 /* LCOV_EXCL_STOP */
    1158             :                         }
    1159           1 :                         import_timestamp = optarg;
    1160           1 :                         break;
    1161           4 :                 case 't' :
    1162           4 :                         if (parse_option_size(optarg, &opt.parity_tail) != 0) {
    1163             :                                 /* LCOV_EXCL_START */
    1164             :                                 log_fatal(EUSER, "Invalid tail size '%s'\n", optarg);
    1165             :                                 exit(EXIT_FAILURE);
    1166             :                                 /* LCOV_EXCL_STOP */
    1167             :                         }
    1168           4 :                         break;
    1169           1 :                 case OPT_TEST_IMPORT_CONTENT :
    1170           1 :                         if (import_content) {
    1171             :                                 /* LCOV_EXCL_START */
    1172             :                                 log_fatal(EUSER, "Import directory '%s' already specified as '%s'\n", optarg, import_content);
    1173             :                                 exit(EXIT_FAILURE);
    1174             :                                 /* LCOV_EXCL_STOP */
    1175             :                         }
    1176           1 :                         import_content = optarg;
    1177           1 :                         break;
    1178          80 :                 case 'l' :
    1179          80 :                         if (log_file) {
    1180             :                                 /* LCOV_EXCL_START */
    1181             :                                 log_fatal(EUSER, "Log file '%s' already specified as '%s'\n", optarg, log_file);
    1182             :                                 exit(EXIT_FAILURE);
    1183             :                                 /* LCOV_EXCL_STOP */
    1184             :                         }
    1185          80 :                         log_file = optarg;
    1186          80 :                         break;
    1187           0 :                 case 'A' :
    1188           0 :                         opt.force_stats = 1;
    1189           0 :                         break;
    1190           0 :                 case 'Z' :
    1191           0 :                         opt.force_zero = 1;
    1192           0 :                         break;
    1193           6 :                 case 'E' :
    1194           6 :                         opt.force_empty = 1;
    1195           6 :                         break;
    1196           0 :                 case 'U' :
    1197           0 :                         opt.force_uuid = 1;
    1198           0 :                         break;
    1199           1 :                 case 'D' :
    1200           1 :                         opt.force_device = 1;
    1201           1 :                         break;
    1202           1 :                 case 'N' :
    1203           1 :                         opt.force_nocopy = 1;
    1204           1 :                         break;
    1205           6 :                 case 'F' :
    1206           6 :                         opt.force_full = 1;
    1207           6 :                         break;
    1208           1 :                 case 'R' :
    1209           1 :                         opt.force_realloc = 1;
    1210           1 :                         break;
    1211           1 :                 case 'W' :
    1212           1 :                         opt.force_realloc = 1;
    1213           1 :                         if (parse_option_size(optarg, &opt.parity_tail) != 0) {
    1214             :                                 /* LCOV_EXCL_START */
    1215             :                                 log_fatal(EUSER, "Invalid tail size '%s'\n", optarg);
    1216             :                                 exit(EXIT_FAILURE);
    1217             :                                 /* LCOV_EXCL_STOP */
    1218             :                         }
    1219           1 :                         break;
    1220           6 :                 case 'a' :
    1221           6 :                         opt.auditonly = 1;
    1222           6 :                         break;
    1223           7 :                 case 'h' :
    1224           7 :                         opt.prehash = 1;
    1225           7 :                         break;
    1226          28 :                 case 'v' :
    1227          28 :                         ++msg_level;
    1228          28 :                         break;
    1229         756 :                 case 'q' :
    1230         756 :                         --msg_level;
    1231         756 :                         break;
    1232         280 :                 case OPT_NO_WARNINGS :
    1233         280 :                         opt.no_warnings = 1;
    1234         280 :                         break;
    1235          28 :                 case OPT_GUI :
    1236          28 :                         opt.gui = 1;
    1237          28 :                         break;
    1238           0 :                 case OPT_GUI_VERBOSE :
    1239           0 :                         opt.gui = 1;
    1240           0 :                         opt.gui_verbose = 1;
    1241           0 :                         break;
    1242           0 :                 case OPT_GUI_RESCAN_AFTER :
    1243           0 :                         opt.gui_rescan_after = 1;
    1244           0 :                         break;
    1245           0 :                 case OPT_GUI_THRESHOLD_REMOVES :
    1246           0 :                         opt.gui_threshold_removes = strtoul(optarg, &e, 0);
    1247           0 :                         if (!e || *e) {
    1248             :                                 /* LCOV_EXCL_START */
    1249             :                                 log_fatal(EUSER, "Invalid threshold '%s'\n", optarg);
    1250             :                                 exit(EXIT_FAILURE);
    1251             :                                 /* LCOV_EXCL_STOP */
    1252             :                         }
    1253           0 :                         break;
    1254           0 :                 case OPT_GUI_THRESHOLD_UPDATES :
    1255           0 :                         opt.gui_threshold_updates = strtoul(optarg, &e, 0);
    1256           0 :                         if (!e || *e) {
    1257             :                                 /* LCOV_EXCL_START */
    1258             :                                 log_fatal(EUSER, "Invalid threshold '%s'\n", optarg);
    1259             :                                 exit(EXIT_FAILURE);
    1260             :                                 /* LCOV_EXCL_STOP */
    1261             :                         }
    1262           0 :                         break;
    1263           1 :                 case 'H' :
    1264           1 :                         usage(conf);
    1265           1 :                         exit(EXIT_SUCCESS);
    1266           1 :                 case 'V' :
    1267           1 :                         version();
    1268           1 :                         exit(EXIT_SUCCESS);
    1269           4 :                 case 'T' :
    1270           4 :                         speedtest = 1;
    1271           4 :                         break;
    1272           0 :                 case OPT_TEST_SPEED_PERIOD :
    1273           0 :                         speed_test_period = atoi(optarg);
    1274           0 :                         break;
    1275           0 :                 case OPT_TEST_SPEED_DISKS_NUMBER :
    1276           0 :                         speed_test_disks_number = atoi(optarg);
    1277           0 :                         break;
    1278           0 :                 case OPT_TEST_SPEED_BLOCKS_SIZE :
    1279           0 :                         speed_test_blocks_size = atoi(optarg);
    1280           0 :                         break;
    1281           1 :                 case 'C' :
    1282           1 :                         gen_conf = optarg;
    1283           1 :                         break;
    1284          10 :                 case OPT_TEST_KILL_AFTER_SYNC :
    1285          10 :                         opt.kill_after_sync = 1;
    1286          10 :                         break;
    1287          13 :                 case OPT_TEST_EXPECT_UNRECOVERABLE :
    1288          13 :                         opt.expect_unrecoverable = 1;
    1289          13 :                         break;
    1290          23 :                 case OPT_TEST_EXPECT_RECOVERABLE :
    1291          23 :                         opt.expect_recoverable = 1;
    1292          23 :                         break;
    1293         280 :                 case OPT_TEST_SKIP_SELF :
    1294         280 :                         opt.skip_self = 1;
    1295         280 :                         break;
    1296           0 :                 case OPT_TEST_SKIP_SIGN :
    1297           0 :                         opt.skip_sign = 1;
    1298           0 :                         break;
    1299           1 :                 case OPT_TEST_SKIP_FALLOCATE :
    1300           1 :                         opt.skip_fallocate = 1;
    1301           1 :                         break;
    1302         287 :                 case OPT_TEST_SKIP_DEVICE :
    1303         287 :                         opt.skip_device = 1;
    1304         287 :                         speed_test_period = 50; /* reduce period of the speed test for running the make check */
    1305         287 :                         break;
    1306           0 :                 case OPT_TEST_SKIP_CONTENT_CHECK :
    1307           0 :                         opt.skip_content_check = 1;
    1308           0 :                         break;
    1309           0 :                 case OPT_TEST_SKIP_PARITY_ACCESS :
    1310           0 :                         opt.skip_parity_access = 1;
    1311           0 :                         break;
    1312           0 :                 case OPT_TEST_SKIP_DISK_ACCESS :
    1313           0 :                         opt.skip_disk_access = 1;
    1314           0 :                         break;
    1315           1 :                 case OPT_TEST_FORCE_MURMUR3 :
    1316           1 :                         opt.force_murmur3 = 1;
    1317           1 :                         break;
    1318           1 :                 case OPT_TEST_FORCE_SPOOKY2 :
    1319           1 :                         opt.force_spooky2 = 1;
    1320           1 :                         break;
    1321           0 :                 case OPT_TEST_SKIP_LOCK :
    1322           0 :                         opt.skip_lock = 1;
    1323           0 :                         break;
    1324           2 :                 case OPT_TEST_FORCE_ORDER_PHYSICAL :
    1325           2 :                         opt.force_order = SORT_PHYSICAL;
    1326           2 :                         break;
    1327           0 :                 case OPT_TEST_FORCE_ORDER_INODE :
    1328           0 :                         opt.force_order = SORT_INODE;
    1329           0 :                         break;
    1330         278 :                 case OPT_TEST_FORCE_ORDER_ALPHA :
    1331         278 :                         opt.force_order = SORT_ALPHA;
    1332         278 :                         break;
    1333           0 :                 case OPT_TEST_FORCE_ORDER_DIR :
    1334           0 :                         opt.force_order = SORT_DIR;
    1335           0 :                         break;
    1336           4 :                 case OPT_TEST_FORCE_SCRUB_AT :
    1337           4 :                         opt.force_scrub_at = atoi(optarg);
    1338           4 :                         break;
    1339           1 :                 case OPT_TEST_FORCE_SCRUB_EVEN :
    1340           1 :                         opt.force_scrub_even = 1;
    1341           1 :                         break;
    1342           0 :                 case OPT_TEST_FORCE_CONTENT_WRITE :
    1343           0 :                         opt.force_content_write = 1;
    1344           0 :                         break;
    1345          18 :                 case OPT_TEST_EXPECT_FAILURE :
    1346             :                         /* invert the exit codes */
    1347          18 :                         exit_success = 1;
    1348          18 :                         exit_failure = 0;
    1349          18 :                         break;
    1350           7 :                 case OPT_TEST_EXPECT_NEED_SYNC :
    1351             :                         /* invert the exit codes */
    1352           7 :                         exit_success = 1;
    1353           7 :                         exit_sync_needed = 0;
    1354           7 :                         break;
    1355          10 :                 case OPT_TEST_RUN :
    1356          10 :                         run = optarg;
    1357          10 :                         break;
    1358           0 :                 case OPT_TEST_FORCE_SCAN_WINFIND :
    1359           0 :                         opt.force_scan_winfind = 1;
    1360           0 :                         break;
    1361         280 :                 case OPT_TEST_FORCE_PROGRESS :
    1362         280 :                         opt.force_progress = 1;
    1363         280 :                         break;
    1364           1 :                 case OPT_TEST_FORCE_AUTOSAVE_AT :
    1365           1 :                         opt.force_autosave_at = atoi(optarg);
    1366           1 :                         break;
    1367           4 :                 case OPT_TEST_FAKE_DEVICE :
    1368           4 :                         opt.fake_device = 1;
    1369           4 :                         break;
    1370           1 :                 case OPT_TEST_FAKE_UUID :
    1371           1 :                         opt.fake_uuid = 2;
    1372           1 :                         break;
    1373           2 :                 case OPT_TEST_MATCH_FIRST_UUID :
    1374           2 :                         opt.match_first_uuid = 1;
    1375           2 :                         break;
    1376           0 :                 case OPT_TEST_FORCE_PARITY_UPDATE :
    1377           0 :                         opt.force_parity_update = 1;
    1378           0 :                         break;
    1379           1 :                 case OPT_TEST_IO_CACHE :
    1380           1 :                         opt.io_cache = atoi(optarg);
    1381           1 :                         if (opt.io_cache != 1 && (opt.io_cache < IO_MIN || opt.io_cache > IO_MAX)) {
    1382             :                                 /* LCOV_EXCL_START */
    1383             :                                 log_fatal(EUSER, "The IO cache should be between %u and %u.\n", IO_MIN, IO_MAX);
    1384             :                                 exit(EXIT_FAILURE);
    1385             :                                 /* LCOV_EXCL_STOP */
    1386             :                         }
    1387           1 :                         break;
    1388           1 :                 case OPT_TEST_IO_STATS :
    1389           1 :                         opt.force_stats = 1;
    1390           1 :                         break;
    1391           0 :                 case OPT_TEST_COND_SIGNAL_OUTSIDE :
    1392             : #if HAVE_THREAD
    1393           0 :                         thread_cond_signal_outside = 1;
    1394             : #endif
    1395           0 :                         break;
    1396         281 :                 case OPT_TEST_IO_ADVISE_NONE :
    1397         281 :                         opt.file_mode = ADVISE_NONE;
    1398         281 :                         break;
    1399           1 :                 case OPT_TEST_IO_ADVISE_SEQUENTIAL :
    1400           1 :                         opt.file_mode = ADVISE_SEQUENTIAL;
    1401           1 :                         break;
    1402           0 :                 case OPT_TEST_IO_ADVISE_FLUSH :
    1403           0 :                         opt.file_mode = ADVISE_FLUSH;
    1404           0 :                         break;
    1405           1 :                 case OPT_TEST_IO_ADVISE_FLUSH_WINDOW :
    1406           1 :                         opt.file_mode = ADVISE_FLUSH_WINDOW;
    1407           1 :                         break;
    1408           0 :                 case OPT_TEST_IO_ADVISE_DISCARD :
    1409           0 :                         opt.file_mode = ADVISE_DISCARD;
    1410           0 :                         break;
    1411           1 :                 case OPT_TEST_IO_ADVISE_DISCARD_WINDOW :
    1412           1 :                         opt.file_mode = ADVISE_DISCARD_WINDOW;
    1413           1 :                         break;
    1414           0 :                 case OPT_TEST_IO_ADVISE_DIRECT :
    1415           0 :                         opt.file_mode = ADVISE_DIRECT;
    1416           0 :                         break;
    1417         280 :                 case OPT_TEST_PARITY_LIMIT :
    1418         280 :                         opt.parity_limit_size = atoll(optarg);
    1419         280 :                         break;
    1420           0 :                 case OPT_TEST_SKIP_CONTENT_WRITE :
    1421           0 :                         opt.skip_content_write = 1;
    1422           0 :                         break;
    1423           0 :                 case OPT_TEST_SKIP_SPACE_HOLDER :
    1424           0 :                         opt.skip_space_holder = 1;
    1425           0 :                         break;
    1426           5 :                 case OPT_TEST_FORMAT :
    1427           5 :                         if (strcmp(optarg, "file") == 0)
    1428           2 :                                 FMT_MODE = FMT_FILE;
    1429           3 :                         else if (strcmp(optarg, "disk") == 0)
    1430           1 :                                 FMT_MODE = FMT_DISK;
    1431           2 :                         else if (strcmp(optarg, "path") == 0)
    1432           2 :                                 FMT_MODE = FMT_PATH;
    1433             :                         else {
    1434             :                                 /* LCOV_EXCL_START */
    1435             :                                 log_fatal(EUSER, "Unknown format '%s'\n", optarg);
    1436             :                                 exit(EXIT_FAILURE);
    1437             :                                 /* LCOV_EXCL_STOP */
    1438             :                         }
    1439           5 :                         break;
    1440           0 :                 case OPT_TEST_SKIP_MULTI_SCAN :
    1441           0 :                         opt.skip_multi_scan = 1;
    1442           0 :                         break;
    1443           0 :                 default :
    1444             :                         /* LCOV_EXCL_START */
    1445             :                         log_fatal(EUSER, "Unknown option '%c'\n", (char)c);
    1446             :                         exit(EXIT_FAILURE);
    1447             :                         /* LCOV_EXCL_STOP */
    1448             :                 }
    1449             :         }
    1450             : 
    1451         283 :         os_init(opt.force_scan_winfind);
    1452         283 :         raid_init();
    1453         283 :         crc32c_init();
    1454         283 :         random_reseed();
    1455             : 
    1456         283 :         if (speedtest != 0) {
    1457           4 :                 speed(speed_test_period, speed_test_disks_number, speed_test_blocks_size);
    1458           4 :                 os_done();
    1459           4 :                 exit(EXIT_SUCCESS);
    1460             :         }
    1461             : 
    1462         279 :         if (gen_conf != 0) {
    1463           1 :                 generate_configuration(gen_conf);
    1464           1 :                 os_done();
    1465           1 :                 exit(EXIT_SUCCESS);
    1466             :         }
    1467             : 
    1468         278 :         if (optind + 1 != argc) {
    1469             :                 /* LCOV_EXCL_START */
    1470             :                 usage(conf);
    1471             :                 exit(EXIT_FAILURE);
    1472             :                 /* LCOV_EXCL_STOP */
    1473             :         }
    1474             : 
    1475         278 :         command = argv[optind];
    1476         278 :         if (strcmp(command, "diff") == 0) {
    1477           8 :                 operation = OPERATION_DIFF;
    1478         270 :         } else if (strcmp(argv[optind], "sync") == 0) {
    1479          94 :                 operation = OPERATION_SYNC;
    1480         176 :         } else if (strcmp(argv[optind], "check") == 0) {
    1481          75 :                 operation = OPERATION_CHECK;
    1482         101 :         } else if (strcmp(argv[optind], "fix") == 0) {
    1483          46 :                 operation = OPERATION_FIX;
    1484          55 :         } else if (strcmp(argv[optind], "test-dry") == 0) {
    1485           3 :                 operation = OPERATION_DRY;
    1486          52 :         } else if (strcmp(argv[optind], "dup") == 0) {
    1487           3 :                 operation = OPERATION_DUP;
    1488          49 :         } else if (strcmp(argv[optind], "list") == 0) {
    1489           5 :                 operation = OPERATION_LIST;
    1490          44 :         } else if (strcmp(argv[optind], "pool") == 0) {
    1491           3 :                 operation = OPERATION_POOL;
    1492          41 :         } else if (strcmp(argv[optind], "rehash") == 0) {
    1493           1 :                 operation = OPERATION_REHASH;
    1494          40 :         } else if (strcmp(argv[optind], "scrub") == 0) {
    1495          11 :                 operation = OPERATION_SCRUB;
    1496          29 :         } else if (strcmp(argv[optind], "status") == 0) {
    1497          15 :                 operation = OPERATION_STATUS;
    1498          14 :         } else if (strcmp(argv[optind], "test-rewrite") == 0) {
    1499           1 :                 operation = OPERATION_REWRITE;
    1500          13 :         } else if (strcmp(argv[optind], "read") == 0) {
    1501           1 :                 operation = OPERATION_READ;
    1502          12 :         } else if (strcmp(argv[optind], "touch") == 0) {
    1503           1 :                 operation = OPERATION_TOUCH;
    1504          11 :         } else if (strcmp(argv[optind], "up") == 0) {
    1505           1 :                 operation = OPERATION_SPINUP;
    1506          10 :         } else if (strcmp(argv[optind], "down") == 0) {
    1507           1 :                 operation = OPERATION_SPINDOWN;
    1508           9 :         } else if (strcmp(argv[optind], "devices") == 0) {
    1509           1 :                 operation = OPERATION_DEVICES;
    1510           8 :         } else if (strcmp(argv[optind], "smart") == 0) {
    1511           1 :                 operation = OPERATION_SMART;
    1512           7 :         } else if (strcmp(argv[optind], "probe") == 0) {
    1513           2 :                 operation = OPERATION_PROBE;
    1514           5 :         } else if (strcmp(argv[optind], "locate") == 0) {
    1515           5 :                 operation = OPERATION_LOCATE;
    1516             :         } else {
    1517             :                 /* LCOV_EXCL_START */
    1518             :                 log_fatal(EUSER, "Unknown command '%s'\n", argv[optind]);
    1519             :                 exit(EXIT_FAILURE);
    1520             :                 /* LCOV_EXCL_STOP */
    1521             :         }
    1522             : 
    1523             :         /* check options compatibility */
    1524         278 :         switch (operation) {
    1525          75 :         case OPERATION_CHECK :
    1526          75 :                 break;
    1527         203 :         default :
    1528         203 :                 if (opt.auditonly) {
    1529             :                         /* LCOV_EXCL_START */
    1530             :                         log_fatal(EUSER, "You cannot use -a, --audit-only with the '%s' command\n", command);
    1531             :                         exit(EXIT_FAILURE);
    1532             :                         /* LCOV_EXCL_STOP */
    1533             :                 }
    1534             :         }
    1535             : 
    1536         278 :         switch (operation) {
    1537         127 :         case OPERATION_FIX :
    1538             :         case OPERATION_CHECK :
    1539             :         case OPERATION_SMART :
    1540             :         case OPERATION_PROBE :
    1541             :         case OPERATION_DEVICES :
    1542             :         case OPERATION_SPINUP :
    1543             :         case OPERATION_SPINDOWN :
    1544         127 :                 break;
    1545         151 :         default :
    1546         151 :                 if (opt.force_device) {
    1547             :                         /* LCOV_EXCL_START */
    1548             :                         log_fatal(EUSER, "You cannot use -D, --force-device with the '%s' command\n", command);
    1549             :                         exit(EXIT_FAILURE);
    1550             :                         /* LCOV_EXCL_STOP */
    1551             :                 }
    1552             :         }
    1553             : 
    1554         278 :         switch (operation) {
    1555         215 :         case OPERATION_SYNC :
    1556             :         case OPERATION_CHECK :
    1557             :         case OPERATION_FIX :
    1558         215 :                 break;
    1559          63 :         default :
    1560          63 :                 if (opt.force_nocopy) {
    1561             :                         /* LCOV_EXCL_START */
    1562             :                         log_fatal(EUSER, "You cannot use -N, --force-nocopy with the '%s' command\n", command);
    1563             :                         exit(EXIT_FAILURE);
    1564             :                         /* LCOV_EXCL_STOP */
    1565             :                 }
    1566             :         }
    1567             : 
    1568         278 :         switch (operation) {
    1569          94 :         case OPERATION_SYNC :
    1570          94 :                 break;
    1571         184 :         default :
    1572         184 :                 if (opt.prehash) {
    1573             :                         /* LCOV_EXCL_START */
    1574             :                         log_fatal(EUSER, "You cannot use -h, --pre-hash with the '%s' command\n", command);
    1575             :                         exit(EXIT_FAILURE);
    1576             :                         /* LCOV_EXCL_STOP */
    1577             :                 }
    1578             : 
    1579         184 :                 if (opt.force_full) {
    1580             :                         /* LCOV_EXCL_START */
    1581             :                         log_fatal(EUSER, "You cannot use -F, --force-full with the '%s' command\n", command);
    1582             :                         exit(EXIT_FAILURE);
    1583             :                         /* LCOV_EXCL_STOP */
    1584             :                 }
    1585             : 
    1586         184 :                 if (opt.force_realloc) {
    1587             :                         /* LCOV_EXCL_START */
    1588             :                         log_fatal(EUSER, "You cannot use -R, --force-realloc with the '%s' command\n", command);
    1589             :                         exit(EXIT_FAILURE);
    1590             :                         /* LCOV_EXCL_STOP */
    1591             :                 }
    1592             :         }
    1593             : 
    1594         278 :         if (opt.force_full && opt.force_nocopy) {
    1595             :                 /* LCOV_EXCL_START */
    1596             :                 log_fatal(EUSER, "You cannot use the -F, --force-full and -N, --force-nocopy options simultaneously\n");
    1597             :                 exit(EXIT_FAILURE);
    1598             :                 /* LCOV_EXCL_STOP */
    1599             :         }
    1600             : 
    1601         278 :         if (opt.force_realloc && opt.force_nocopy) {
    1602             :                 /* LCOV_EXCL_START */
    1603             :                 log_fatal(EUSER, "You cannot use the -R, --force-realloc and -N, --force-nocopy options simultaneously\n");
    1604             :                 exit(EXIT_FAILURE);
    1605             :                 /* LCOV_EXCL_STOP */
    1606             :         }
    1607             : 
    1608         278 :         if (opt.force_realloc && opt.force_full) {
    1609             :                 /* LCOV_EXCL_START */
    1610             :                 log_fatal(EUSER, "You cannot use the -R, --force-realloc and -F, --force-full options simultaneously\n");
    1611             :                 exit(EXIT_FAILURE);
    1612             :                 /* LCOV_EXCL_STOP */
    1613             :         }
    1614             : 
    1615         278 :         if (opt.prehash && opt.force_nocopy) {
    1616             :                 /* LCOV_EXCL_START */
    1617             :                 log_fatal(EUSER, "You cannot use the -h, --pre-hash and -N, --force-nocopy options simultaneously\n");
    1618             :                 exit(EXIT_FAILURE);
    1619             :                 /* LCOV_EXCL_STOP */
    1620             :         }
    1621             : 
    1622         278 :         switch (operation) {
    1623         124 :         case OPERATION_CHECK :
    1624             :         case OPERATION_FIX :
    1625             :         case OPERATION_DRY :
    1626         124 :                 break;
    1627         152 :         default :
    1628         152 :                 if (!tommy_list_empty(&filterlist_disk)) {
    1629             :                         /* LCOV_EXCL_START */
    1630             :                         log_fatal(EUSER, "You cannot use -d, --filter-disk with the '%s' command\n", command);
    1631             :                         exit(EXIT_FAILURE);
    1632             :                         /* LCOV_EXCL_STOP */
    1633             :                 }
    1634             :         /* fallthrough */
    1635             :         case OPERATION_SPINUP :
    1636             :         case OPERATION_SPINDOWN :
    1637         154 :                 if (!tommy_list_empty(&filterlist_file)) {
    1638             :                         /* LCOV_EXCL_START */
    1639             :                         log_fatal(EUSER, "You cannot use -f, --filter with the '%s' command\n", command);
    1640             :                         exit(EXIT_FAILURE);
    1641             :                         /* LCOV_EXCL_STOP */
    1642             :                 }
    1643         154 :                 if (filter_missing != 0) {
    1644             :                         /* LCOV_EXCL_START */
    1645             :                         log_fatal(EUSER, "You cannot use -m, --filter-missing with the '%s' command\n", command);
    1646             :                         exit(EXIT_FAILURE);
    1647             :                         /* LCOV_EXCL_STOP */
    1648             :                 }
    1649         154 :                 if (filter_error != 0) {
    1650             :                         /* LCOV_EXCL_START */
    1651             :                         log_fatal(EUSER, "You cannot use -e, --filter-error with the '%s' command\n", command);
    1652             :                         exit(EXIT_FAILURE);
    1653             :                         /* LCOV_EXCL_STOP */
    1654             :                 }
    1655             :         }
    1656             : 
    1657             :         /* errors must be always fixed on all disks */
    1658             :         /* because we don't keep the information on what disk is the error */
    1659         278 :         if (filter_error != 0 && !tommy_list_empty(&filterlist_disk)) {
    1660             :                 /* LCOV_EXCL_START */
    1661             :                 log_fatal(EUSER, "You cannot use -e, --filter-error and -d, --filter-disk simultaneously\n");
    1662             :                 exit(EXIT_FAILURE);
    1663             :                 /* LCOV_EXCL_STOP */
    1664             :         }
    1665             : 
    1666         278 :         switch (operation) {
    1667         121 :         case OPERATION_CHECK :
    1668             :         case OPERATION_FIX :
    1669         121 :                 break;
    1670         157 :         default :
    1671         157 :                 if (import_timestamp != 0 || import_content != 0) {
    1672             :                         /* LCOV_EXCL_START */
    1673             :                         log_fatal(EUSER, "Import not allowed with the '%s' command\n", command);
    1674             :                         exit(EXIT_FAILURE);
    1675             :                         /* LCOV_EXCL_STOP */
    1676             :                 }
    1677             :         }
    1678             : 
    1679         278 :         switch (operation) {
    1680          26 :         case OPERATION_LIST :
    1681             :         case OPERATION_DUP :
    1682             :         case OPERATION_STATUS :
    1683             :         case OPERATION_REWRITE :
    1684             :         case OPERATION_READ :
    1685             :         case OPERATION_REHASH :
    1686             :                 /* avoid to check and access data disks if not needed */
    1687          26 :                 opt.skip_disk_access = 1;
    1688          26 :                 break;
    1689             :         }
    1690             : 
    1691         278 :         switch (operation) {
    1692          38 :         case OPERATION_DIFF :
    1693             :         case OPERATION_LIST :
    1694             :         case OPERATION_DUP :
    1695             :         case OPERATION_POOL :
    1696             :         case OPERATION_STATUS :
    1697             :         case OPERATION_REWRITE :
    1698             :         case OPERATION_READ :
    1699             :         case OPERATION_REHASH :
    1700             :         case OPERATION_TOUCH :
    1701             :                 /* avoid to check and access parity disks if not needed */
    1702          38 :                 opt.skip_parity_access = 1;
    1703          38 :                 break;
    1704             :         }
    1705             : 
    1706         278 :         switch (operation) {
    1707         121 :         case OPERATION_FIX :
    1708             :         case OPERATION_CHECK :
    1709             :                 /* avoid to stop processing if a content file is not accessible */
    1710         121 :                 opt.skip_content_access = 1;
    1711         121 :                 break;
    1712             :         }
    1713             : 
    1714         278 :         switch (operation) {
    1715          26 :         case OPERATION_DIFF :
    1716             :         case OPERATION_LIST :
    1717             :         case OPERATION_DUP :
    1718             :         case OPERATION_POOL :
    1719             :         case OPERATION_TOUCH :
    1720             :         case OPERATION_SPINUP :
    1721             :         case OPERATION_SPINDOWN :
    1722             :         case OPERATION_DEVICES :
    1723             :         case OPERATION_SMART :
    1724             :         case OPERATION_PROBE :
    1725          26 :                 opt.skip_self = 1;
    1726          26 :                 break;
    1727             :         }
    1728             : 
    1729         278 :         switch (operation) {
    1730             : #if HAVE_DIRECT_IO
    1731         108 :         case OPERATION_SYNC :
    1732             :         case OPERATION_SCRUB :
    1733             :         case OPERATION_DRY :
    1734         108 :                 break;
    1735             : #endif
    1736         170 :         default :
    1737             :                 /* we allow direct IO only on some commands */
    1738         170 :                 if (opt.file_mode == ADVISE_DIRECT)
    1739           0 :                         opt.file_mode = ADVISE_SEQUENTIAL;
    1740         170 :                 break;
    1741             :         }
    1742             : 
    1743         278 :         switch (operation) {
    1744           4 :         case OPERATION_DEVICES :
    1745             :         case OPERATION_SMART :
    1746             :         case OPERATION_PROBE :
    1747             :                 /* we may need to use these commands during operations */
    1748           4 :                 opt.skip_lock = 1;
    1749           4 :                 break;
    1750             :         }
    1751             : 
    1752         278 :         switch (operation) {
    1753           3 :         case OPERATION_SMART :
    1754             :         case OPERATION_PROBE :
    1755             :                 /* allow to run without configuration file */
    1756           3 :                 opt.auto_conf = 1;
    1757           3 :                 break;
    1758             :         }
    1759             : 
    1760             :         /* open the log file */
    1761         278 :         log_open(log_file);
    1762             : 
    1763             :         /* print generic info into the log */
    1764         278 :         t = time(0);
    1765             : #if HAVE_LOCALTIME_R
    1766         278 :         tm = localtime_r(&t, &tm_res);
    1767             : #else
    1768             :         tm = localtime(&t);
    1769             : #endif
    1770         278 :         log_tag("version:%s\n", PACKAGE_VERSION);
    1771         278 :         log_tag("unixtime:%" PRIi64 "\n", (int64_t)t);
    1772         278 :         if (tm) {
    1773             :                 char datetime[64];
    1774         278 :                 strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", tm);
    1775         278 :                 log_tag("time:%s\n", datetime);
    1776             :         }
    1777         278 :         log_tag("command:%s\n", command);
    1778        4526 :         for (i = 0; i < argc; ++i)
    1779        4248 :                 log_tag("argv:%u:%s\n", i, argv[i]);
    1780         278 :         log_flush();
    1781             : 
    1782         278 :         if (!opt.skip_self)
    1783           1 :                 selftest();
    1784             : 
    1785         278 :         state_init(&state);
    1786             : 
    1787             :         /* read the configuration file */
    1788         278 :         state_config(&state, conf, command, &opt, &filterlist_disk);
    1789             : 
    1790             :         /* set the raid mode */
    1791         277 :         raid_mode(state.raid_mode);
    1792             : 
    1793             : #if HAVE_LOCKFILE
    1794             :         /* create the lock file */
    1795         277 :         if (!opt.skip_lock && state.lockfile[0]) {
    1796         273 :                 lock = lock_lock(state.lockfile);
    1797         273 :                 if (lock == -1) {
    1798             :                         /* LCOV_EXCL_START */
    1799             :                         if (errno != EWOULDBLOCK) {
    1800             :                                 log_fatal(errno, "Failed to create the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
    1801             :                         } else {
    1802             :                                 log_fatal(errno, "The lock file '%s' is already in use!\n", state.lockfile);
    1803             :                                 log_fatal(errno, "SnapRAID is already in use!\n");
    1804             :                         }
    1805             :                         exit(EXIT_FAILURE);
    1806             :                         /* LCOV_EXCL_STOP */
    1807             :                 }
    1808             :         }
    1809             : #else
    1810             :         (void)lock;
    1811             : #endif
    1812             : 
    1813         277 :         ret = 0;
    1814         277 :         if (operation == OPERATION_DIFF) {
    1815           8 :                 state_read(&state);
    1816             : 
    1817             :                 /* refresh the size info to log correct info */
    1818           8 :                 state_refresh(&state);
    1819             : 
    1820           8 :                 ret = state_diff(&state);
    1821             : 
    1822             :                 /* abort if sync needed */
    1823           8 :                 if (ret > 0)
    1824           7 :                         exit(EXIT_SYNC_NEEDED);
    1825         269 :         } else if (operation == OPERATION_SYNC) {
    1826             : 
    1827             :                 /* in the next state read ensures to clear all the past hashes in case */
    1828             :                 /* we are reading from an incomplete sync */
    1829             :                 /* The indeterminate hash are only for CHG/DELETED blocks for which we don't */
    1830             :                 /* know if the previous interrupted sync was able to update or not the parity. */
    1831             :                 /* The sync process instead needs to trust this information because it's used */
    1832             :                 /* to avoid to recompute the parity if all the input are equals as before. */
    1833             : 
    1834             :                 /* In these cases we don't know if the old state is still the one */
    1835             :                 /* stored inside the parity, because after an aborted sync, the parity */
    1836             :                 /* may be or may be not have been updated with the data that may be now */
    1837             :                 /* deleted. Then we reset the hash to a bogus value. */
    1838             : 
    1839             :                 /* An example for CHG blocks is: */
    1840             :                 /* - One file is added creating a CHG block with ZERO state */
    1841             :                 /* - Sync aborted after updating the parity to the new state, */
    1842             :                 /*   but without saving the content file representing this new BLK state. */
    1843             :                 /* - File is now deleted after the aborted sync */
    1844             :                 /* - Sync again, deleting the blocks over the CHG ones */
    1845             :                 /*   with the hash of CHG blocks not representing the real parity state */
    1846             : 
    1847             :                 /* An example for DELETED blocks is: */
    1848             :                 /* - One file is deleted creating DELETED blocks */
    1849             :                 /* - Sync aborted after, updating the parity to the new state, */
    1850             :                 /*   but without saving the content file representing this new EMPTY state. */
    1851             :                 /* - Another file is added again over the DELETE ones */
    1852             :                 /*   with the hash of DELETED blocks not representing the real parity state */
    1853          94 :                 state.clear_past_hash = 1;
    1854             : 
    1855          94 :                 state_read(&state);
    1856             : 
    1857             :                 /* mark the files that have to be reallocated */
    1858             :                 /* it will happen in inside scan_file_keep() called in state_scan() */
    1859          93 :                 if (state.opt.force_realloc)
    1860           2 :                         state_locate_mark_tail_blocks_for_resync(&state, opt.parity_tail);
    1861             : 
    1862          93 :                 state_scan(&state);
    1863             : 
    1864          92 :                 if (opt.gui_threshold_removes != 0 && state.removed_files > opt.gui_threshold_removes) {
    1865           0 :                         log_fatal(EUSER, "Too many files were removed (%u, limit is %u). Sync aborted.\n", state.removed_files, opt.gui_threshold_removes);
    1866           0 :                         exit(EXIT_FAILURE);
    1867             :                 }
    1868          92 :                 if (opt.gui_threshold_updates != 0 && state.updated_files > opt.gui_threshold_updates) {
    1869           0 :                         log_fatal(EUSER, "Too many files were updated (%u, limit is %u). Sync aborted.\n", state.updated_files, opt.gui_threshold_updates);
    1870           0 :                         exit(EXIT_FAILURE);
    1871             :                 }
    1872             : 
    1873             :                 /* refresh the size info before the content write */
    1874          92 :                 state_refresh(&state);
    1875             : 
    1876          92 :                 memory();
    1877             : 
    1878             :                 /* intercept signals while operating */
    1879          92 :                 signal_init();
    1880             : 
    1881             :                 /* run a test command if required */
    1882          92 :                 if (run != 0) {
    1883          10 :                         ret = system(run); /* ignore error */
    1884          10 :                         if (ret != 0) {
    1885             :                                 /* LCOV_EXCL_START */
    1886             :                                 log_fatal(errno, "Error executing command '%s'.\n", run);
    1887             :                                 exit(EXIT_FAILURE);
    1888             :                                 /* LCOV_EXCL_STOP */
    1889             :                         }
    1890             :                 }
    1891             : 
    1892             :                 /* waits some time to ensure that any concurrent modification done at the files, */
    1893             :                 /* using the same mtime read by the scan process, will be read by sync. */
    1894             :                 /* Note that any later modification done, potentially not read by sync, will have */
    1895             :                 /* a different mtime, and it will be synchronized at the next sync. */
    1896             :                 /* The worst case is the FAT file-system with a two seconds resolution for mtime. */
    1897             :                 /* If you don't use FAT, the wait is not needed, because most file-systems have now */
    1898             :                 /* at least microseconds resolution, but better to be safe. */
    1899          92 :                 if (!opt.skip_self)
    1900           0 :                         sleep(2);
    1901             : 
    1902          92 :                 ret = state_sync(&state, blockstart, blockcount);
    1903         175 :         } else if (operation == OPERATION_DRY) {
    1904           3 :                 state_read(&state);
    1905             : 
    1906             :                 /* refresh the size info to log correct info */
    1907           3 :                 state_refresh(&state);
    1908             : 
    1909             :                 /* filter */
    1910           3 :                 state_skip(&state);
    1911           3 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    1912             : 
    1913           3 :                 memory();
    1914             : 
    1915             :                 /* intercept signals while operating */
    1916           3 :                 signal_init();
    1917             : 
    1918           3 :                 ret = state_dry(&state, blockstart, blockcount);
    1919         172 :         } else if (operation == OPERATION_REHASH) {
    1920           1 :                 state_read(&state);
    1921             : 
    1922             :                 /* intercept signals while operating */
    1923           1 :                 signal_init();
    1924             : 
    1925           1 :                 state_rehash(&state);
    1926             : 
    1927             :                 /* save the new state if required */
    1928           1 :                 if (state.need_write)
    1929           1 :                         state_write(&state);
    1930         171 :         } else if (operation == OPERATION_SCRUB) {
    1931          11 :                 state_read(&state);
    1932             : 
    1933             :                 /* refresh the size info before the content write */
    1934          11 :                 state_refresh(&state);
    1935             : 
    1936          11 :                 memory();
    1937             : 
    1938             :                 /* intercept signals while operating */
    1939          11 :                 signal_init();
    1940             : 
    1941          11 :                 ret = state_scrub(&state, plan100, olderthan);
    1942         160 :         } else if (operation == OPERATION_REWRITE) {
    1943           1 :                 state_read(&state);
    1944             : 
    1945             :                 /* intercept signals while operating */
    1946           1 :                 signal_init();
    1947             : 
    1948           1 :                 state_write(&state);
    1949             : 
    1950           1 :                 memory();
    1951         159 :         } else if (operation == OPERATION_READ) {
    1952           1 :                 state_read(&state);
    1953             : 
    1954           1 :                 memory();
    1955         158 :         } else if (operation == OPERATION_TOUCH) {
    1956           1 :                 state_read(&state);
    1957             : 
    1958           1 :                 memory();
    1959             : 
    1960           1 :                 state_touch(&state);
    1961             : 
    1962             :                 /* intercept signals while operating */
    1963           1 :                 signal_init();
    1964             : 
    1965           1 :                 state_write(&state);
    1966         157 :         } else if (operation == OPERATION_SPINUP) {
    1967           1 :                 state_device(&state, DEVICE_UP, &filterlist_disk);
    1968         156 :         } else if (operation == OPERATION_SPINDOWN) {
    1969           1 :                 state_device(&state, DEVICE_DOWN, &filterlist_disk);
    1970         155 :         } else if (operation == OPERATION_DEVICES) {
    1971           1 :                 state_device(&state, DEVICE_LIST, 0);
    1972         154 :         } else if (operation == OPERATION_SMART) {
    1973           1 :                 state_device(&state, DEVICE_SMART, 0);
    1974         153 :         } else if (operation == OPERATION_PROBE) {
    1975           2 :                 state_device(&state, DEVICE_PROBE, 0);
    1976         151 :         } else if (operation == OPERATION_STATUS) {
    1977          15 :                 state_read(&state);
    1978             : 
    1979          15 :                 memory();
    1980             : 
    1981          15 :                 state_status(&state);
    1982         136 :         } else if (operation == OPERATION_LOCATE) {
    1983           5 :                 state_read(&state);
    1984             : 
    1985           5 :                 state_locate(&state, opt.parity_tail);
    1986             : 
    1987         131 :         } else if (operation == OPERATION_DUP) {
    1988           3 :                 state_read(&state);
    1989             : 
    1990           3 :                 state_dup(&state);
    1991         128 :         } else if (operation == OPERATION_LIST) {
    1992           5 :                 state_read(&state);
    1993             : 
    1994           5 :                 state_list(&state);
    1995         123 :         } else if (operation == OPERATION_POOL) {
    1996           3 :                 state_read(&state);
    1997             : 
    1998           3 :                 state_pool(&state);
    1999         120 :         } else if (operation == OPERATION_CHECK || operation == OPERATION_FIX) {
    2000         120 :                 state_read(&state);
    2001             : 
    2002             :                 /* if we are also trying to recover */
    2003         120 :                 if (!state.opt.auditonly) {
    2004             :                         /* import the user specified dirs */
    2005         114 :                         if (import_timestamp != 0)
    2006           1 :                                 state_search(&state, import_timestamp);
    2007         114 :                         if (import_content != 0)
    2008           1 :                                 state_import(&state, import_content);
    2009             : 
    2010             :                         /* import from all the array */
    2011         114 :                         if (!state.opt.force_nocopy)
    2012         114 :                                 state_search_array(&state);
    2013             :                 }
    2014             : 
    2015             :                 /* filter */
    2016         120 :                 state_skip(&state);
    2017         120 :                 state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);
    2018             : 
    2019         120 :                 memory();
    2020             : 
    2021             :                 /* intercept signals while operating */
    2022         120 :                 signal_init();
    2023             : 
    2024         120 :                 if (operation == OPERATION_CHECK) {
    2025          75 :                         ret = state_check(&state, 0, blockstart, blockcount);
    2026             :                 } else { /* it's fix */
    2027          45 :                         ret = state_check(&state, 1, blockstart, blockcount);
    2028             : 
    2029             :                         /* rescan if requested by the GUI */
    2030          45 :                         if (opt.gui_rescan_after)
    2031           0 :                                 state_scan(&state);
    2032             :                 }
    2033             :         } else {
    2034             :                 /* LCOV_EXCL_START */
    2035             :                 log_fatal(errno, "Unexpected command '%s'\n", command);
    2036             :                 exit(EXIT_FAILURE);
    2037             :                 /* LCOV_EXCL_STOP */
    2038             :         }
    2039             : 
    2040             :         /* close log file */
    2041         267 :         log_close(log_file);
    2042             : 
    2043             : #if HAVE_LOCKFILE
    2044         267 :         if (!opt.skip_lock && state.lockfile[0]) {
    2045         264 :                 if (lock_unlock(lock) == -1) {
    2046             :                         /* LCOV_EXCL_START */
    2047             :                         log_fatal(errno, "Failed to close the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
    2048             :                         exit(EXIT_FAILURE);
    2049             :                         /* LCOV_EXCL_STOP */
    2050             :                 }
    2051             :         }
    2052             : #endif
    2053             : 
    2054         267 :         state_done(&state);
    2055         267 :         tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
    2056         267 :         tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);
    2057             : 
    2058         267 :         os_done();
    2059         267 :         lock_done();
    2060             : 
    2061             :         /* abort if required */
    2062         267 :         if (ret != 0) {
    2063             :                 /* LCOV_EXCL_START */
    2064             :                 exit(EXIT_FAILURE);
    2065             :                 /* LCOV_EXCL_STOP */
    2066             :         }
    2067         255 :         if (global_interrupt) {
    2068             :                 /* LCOV_EXCL_START */
    2069             : #ifdef _WIN32
    2070             :                 exit(STATUS_CONTROL_C_EXIT);
    2071             : #else
    2072             :                 /* restore default handler */
    2073             :                 signal(global_interrupt, SIG_DFL);
    2074             : 
    2075             :                 /* raise the signal again*/
    2076             :                 raise(global_interrupt);
    2077             : 
    2078             :                 /* if raise() didn't terminate */
    2079             :                 _exit(128 + global_interrupt);
    2080             : #endif
    2081             :                 /* LCOV_EXCL_STOP */
    2082             :         }
    2083             : 
    2084         255 :         return EXIT_SUCCESS;
    2085             : }
    2086             : 

Generated by: LCOV version 1.0