File: snapraid.c

package info (click to toggle)
snapraid 13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,656 kB
  • sloc: ansic: 46,018; makefile: 1,004; sh: 154
file content (1944 lines) | stat: -rw-r--r-- 62,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
/*
 * Copyright (C) 2011 Andrea Mazzoleni
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "portable.h"

#include "snapraid.h"
#include "support.h"
#include "elem.h"
#include "import.h"
#include "search.h"
#include "state.h"
#include "io.h"
#include "raid/raid.h"

/****************************************************************************/
/* main */

void version(void)
{
	msg_status(PACKAGE " v" VERSION " by Andrea Mazzoleni, " PACKAGE_URL "\n");
}

void usage(void)
{
	version();

	printf("Usage: " PACKAGE " status|diff|sync|scrub|list|dup|up|down|touch|smart|pool|check|fix [options]\n");
	printf("\n");
	printf("Commands:\n");
	printf("  status Print the status of the array\n");
	printf("  diff   Show the changes that needs to be synchronized\n");
	printf("  sync   Synchronize the state of the array\n");
	printf("  scrub  Scrub the array\n");
	printf("  list   List the array content\n");
	printf("  dup    Find duplicate files\n");
	printf("  up     Spin-up the array\n");
	printf("  down   Spin-down the array\n");
	printf("  touch  Add non-zero ns timestamps to files\n");
	printf("  smart  SMART attributes of the array\n");
	printf("  pool   Create or update the virtual view of the array\n");
	printf("  check  Check the array\n");
	printf("  fix    Fix the array\n");
	printf("\n");
	printf("Options:\n");
	printf("  " SWITCH_GETOPT_LONG("-c, --conf FILE       ", "-c") "  Configuration file\n");
	printf("  " SWITCH_GETOPT_LONG("-f, --filter PATTERN  ", "-f") "  Process only files matching the pattern\n");
	printf("  " SWITCH_GETOPT_LONG("-d, --filter-disk NAME", "-f") "  Process only files in the specified disk\n");
	printf("  " SWITCH_GETOPT_LONG("-m, --filter-missing  ", "-m") "  Process only missing/deleted files\n");
	printf("  " SWITCH_GETOPT_LONG("-e, --filter-error    ", "-e") "  Process only files with errors\n");
	printf("  " SWITCH_GETOPT_LONG("-p, --plan PLAN       ", "-p") "  Define a scrub plan or percentage\n");
	printf("  " SWITCH_GETOPT_LONG("-o, --older-than DAYS ", "-o") "  Process only the older part of the array\n");
	printf("  " SWITCH_GETOPT_LONG("-i, --import DIR      ", "-i") "  Import deleted files\n");
	printf("  " SWITCH_GETOPT_LONG("-l, --log FILE        ", "-l") "  Log file. Default none\n");
	printf("  " SWITCH_GETOPT_LONG("-a, --audit-only      ", "-a") "  Check only file data and not parity\n");
	printf("  " SWITCH_GETOPT_LONG("-h, --pre-hash        ", "-h") "  Pre-hash all the new data\n");
	printf("  " SWITCH_GETOPT_LONG("-Z, --force-zero      ", "-Z") "  Force syncing of files that get zero size\n");
	printf("  " SWITCH_GETOPT_LONG("-E, --force-empty     ", "-E") "  Force syncing of disks that get empty\n");
	printf("  " SWITCH_GETOPT_LONG("-U, --force-uuid      ", "-U") "  Force commands on disks with uuid changed\n");
	printf("  " SWITCH_GETOPT_LONG("-D, --force-device    ", "-D") "  Force commands with inaccessible/shared disks\n");
	printf("  " SWITCH_GETOPT_LONG("-N, --force-nocopy    ", "-N") "  Force commands disabling the copy detection\n");
	printf("  " SWITCH_GETOPT_LONG("-F, --force-full      ", "-F") "  Force a full parity computation in sync\n");
	printf("  " SWITCH_GETOPT_LONG("-R, --force-realloc   ", "-R") "  Force a full parity reallocation in sync\n");
	printf("  " SWITCH_GETOPT_LONG("-w, --bw-limit RATE   ", "-w") "  Limit IO bandwidth (M|G)\n");
	printf("  " SWITCH_GETOPT_LONG("-v, --verbose         ", "-v") "  Verbose\n");
}

void memory(void)
{
	log_tag("memory:used:%" PRIu64 "\n", (uint64_t)malloc_counter_get());

	/* size of the block */
	log_tag("memory:block:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_block)));
	log_tag("memory:extent:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_extent)));
	log_tag("memory:file:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_file)));
	log_tag("memory:link:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_link)));
	log_tag("memory:dir:%" PRIu64 "\n", (uint64_t)(sizeof(struct snapraid_dir)));

	msg_progress("Using %u MiB of memory for the file-system.\n", (unsigned)(malloc_counter_get() / MEBI));
}

void test(int argc, char* argv[])
{
	int i;
	char buffer[ESC_MAX];

	/* special testing code for quoting */
	if (argc < 2 || strcmp(argv[1], "test") != 0)
		return;

	assert(strcmp(strpolish(strcpy(buffer, "\r \n\xFF")), "    ") == 0);
	assert(strcmp(strtrim(strcpy(buffer, " trim trim \n\r")), "trim trim") == 0);
	assert(strcmp(strlwr(strcpy(buffer, " LoWer\n\r")), " lower\n\r") == 0);

	assert(worddigitstr("longneedlestring","needle") == 0);
	assert(worddigitstr("longneedlestring","") == 0);
	assert(worddigitstr("long needle string","needle") != 0);
	assert(worddigitstr("long1needle2string","needle") != 0);
	assert(worddigitstr("long\rneedle3string","needle") != 0);
	assert(worddigitstr("long1needle","needle") != 0);
	assert(worddigitstr("needle2string","needle") != 0);
	assert(worddigitstr("needle","needle") != 0);

	assert(strcmp(esc_tag("simple", buffer), "simple") == 0);
	assert(strcmp(esc_tag("line1\nline2", buffer), "line1\\nline2") == 0);
	assert(strcmp(esc_tag("line1\rline2", buffer), "line1\\rline2") == 0);
	assert(strcmp(esc_tag("key:value", buffer), "key\\dvalue") == 0);
	assert(strcmp(esc_tag("C:\\path\\file", buffer), "C\\d\\\\path\\\\file") == 0);
	assert(strcmp(esc_tag("A\nB\rC:D\\E", buffer), "A\\nB\\rC\\dD\\\\E") == 0);
	assert(strcmp(esc_tag("endwith\\", buffer), "endwith\\\\") == 0);
	assert(strcmp(esc_tag("", buffer), "") == 0);
	assert(strcmp(esc_tag("\n\r:\\\\", buffer), "\\n\\r\\d\\\\\\\\") == 0);

	for (i = 2; i < argc; ++i) {
		printf("argv[%d]\n", i);
		printf("\t#%s#\n", argv[i]);
		printf("\t#%s#\n", esc_shell(argv[i], buffer));
	}

#ifdef _WIN32
	/* basic cases - no special characters, no quotes needed */
	assert(strcmp(esc_shell("simple", buffer), "simple") == 0);
	assert(strcmp(esc_shell("file.txt", buffer), "file.txt") == 0);
	assert(strcmp(esc_shell("file123", buffer), "file123") == 0);
	assert(strcmp(esc_shell("file_name-test.doc", buffer), "file_name-test.doc") == 0);
	assert(strcmp(esc_shell(",._+:@/-", buffer), ",._+:@/-") == 0);
	assert(strcmp(esc_shell("C:\\Users\\test", buffer), "C:\\Users\\test") == 0);

	/* space - requires quoting */
	assert(strcmp(esc_shell(" ", buffer), "\" \"") == 0);
	assert(strcmp(esc_shell("file name.txt", buffer), "\"file name.txt\"") == 0);
	assert(strcmp(esc_shell("my document.doc", buffer), "\"my document.doc\"") == 0);
	assert(strcmp(esc_shell("  multiple  spaces  ", buffer), "\"  multiple  spaces  \"") == 0);

	/* tab - requires quoting */
	assert(strcmp(esc_shell("\t", buffer), "\"\t\"") == 0);
	assert(strcmp(esc_shell("file\tname", buffer), "\"file\tname\"") == 0);

	/* newline - requires quoting */
	assert(strcmp(esc_shell("\n", buffer), "\"\n\"") == 0);
	assert(strcmp(esc_shell("line1\nline2", buffer), "\"line1\nline2\"") == 0);

	/* carriage return - requires quoting */
	assert(strcmp(esc_shell("\r", buffer), "\"\r\"") == 0);
	assert(strcmp(esc_shell("text\r\n", buffer), "\"text\r\n\"") == 0);

	/* double quote - requires quoting and escaping with backslash */
	assert(strcmp(esc_shell("\"", buffer), "\"\\\"\"") == 0);
	assert(strcmp(esc_shell("file\"name", buffer), "\"file\\\"name\"") == 0);
	assert(strcmp(esc_shell("\"quoted\"", buffer), "\"\\\"quoted\\\"\"") == 0);
	assert(strcmp(esc_shell("say \"hello\"", buffer), "\"say \\\"hello\\\"\"") == 0);

	/* ampersand - requires quoting */
	assert(strcmp(esc_shell("&", buffer), "\"&\"") == 0);
	assert(strcmp(esc_shell("file&name", buffer), "\"file&name\"") == 0);
	assert(strcmp(esc_shell("a&b&c", buffer), "\"a&b&c\"") == 0);
	assert(strcmp(esc_shell("file & name", buffer), "\"file & name\"") == 0);

	/* pipe - requires quoting */
	assert(strcmp(esc_shell("|", buffer), "\"|\"") == 0);
	assert(strcmp(esc_shell("file|name", buffer), "\"file|name\"") == 0);
	assert(strcmp(esc_shell("a | b", buffer), "\"a | b\"") == 0);

	/* parentheses - requires quoting */
	assert(strcmp(esc_shell("(", buffer), "\"(\"") == 0);
	assert(strcmp(esc_shell(")", buffer), "\")\"") == 0);
	assert(strcmp(esc_shell("(test)", buffer), "\"(test)\"") == 0);
	assert(strcmp(esc_shell("file (1)", buffer), "\"file (1)\"") == 0);
	assert(strcmp(esc_shell("file(copy)", buffer), "\"file(copy)\"") == 0);

	/* angle brackets - requires quoting */
	assert(strcmp(esc_shell("<", buffer), "\"<\"") == 0);
	assert(strcmp(esc_shell(">", buffer), "\">\"") == 0);
	assert(strcmp(esc_shell("a<b>c", buffer), "\"a<b>c\"") == 0);
	assert(strcmp(esc_shell("file > output", buffer), "\"file > output\"") == 0);

	/* caret - requires quoting */
	assert(strcmp(esc_shell("^", buffer), "\"^\"") == 0);
	assert(strcmp(esc_shell("test^test", buffer), "\"test^test\"") == 0);
	assert(strcmp(esc_shell("a ^ b", buffer), "\"a ^ b\"") == 0);

	/* multiple special chars - requires quoting */
	assert(strcmp(esc_shell("&|()<>^", buffer), "\"&|()<>^\"") == 0);
	assert(strcmp(esc_shell("test&|test", buffer), "\"test&|test\"") == 0);
	assert(strcmp(esc_shell("a & b | c", buffer), "\"a & b | c\"") == 0);

	/* percent sign - requires quoting */
	assert(strcmp(esc_shell("%", buffer), "\"%\"") == 0);
	assert(strcmp(esc_shell("%%", buffer), "\"%%\"") == 0);
	assert(strcmp(esc_shell("%PATH%", buffer), "\"%PATH%\"") == 0);
	assert(strcmp(esc_shell("test%var%test", buffer), "\"test%var%test\"") == 0);
	assert(strcmp(esc_shell("%PATH% file", buffer), "\"%PATH% file\"") == 0);

	/* exclamation mark - requires quoting */
	assert(strcmp(esc_shell("!", buffer), "\"!\"") == 0);
	assert(strcmp(esc_shell("!VAR!", buffer), "\"!VAR!\"") == 0);
	assert(strcmp(esc_shell("test!test", buffer), "\"test!test\"") == 0);
	assert(strcmp(esc_shell("hello !world!", buffer), "\"hello !world!\"") == 0);

	/* equals sign - requires quoting */
	assert(strcmp(esc_shell("=", buffer), "\"=\"") == 0);
	assert(strcmp(esc_shell("VAR=value", buffer), "\"VAR=value\"") == 0);
	assert(strcmp(esc_shell("a=b", buffer), "\"a=b\"") == 0);

	/* semicolon - requires quoting */
	assert(strcmp(esc_shell(";", buffer), "\";\"") == 0);
	assert(strcmp(esc_shell("cmd1;cmd2", buffer), "\"cmd1;cmd2\"") == 0);

	/* backslash - no quotes needed when alone or in path */
	assert(strcmp(esc_shell("\\", buffer), "\\") == 0);
	assert(strcmp(esc_shell("C:\\", buffer), "C:\\") == 0);
	assert(strcmp(esc_shell("C:\\Users", buffer), "C:\\Users") == 0);
	assert(strcmp(esc_shell("path\\to\\file", buffer), "path\\to\\file") == 0);
	assert(strcmp(esc_shell("C:\\folder\\", buffer), "C:\\folder\\") == 0);

	/* backslash with space - requires quoting, normal backslash inside */
	assert(strcmp(esc_shell("\\ ", buffer), "\"\\ \"") == 0);
	assert(strcmp(esc_shell("C:\\ ", buffer), "\"C:\\ \"") == 0);
	assert(strcmp(esc_shell("C:\\Program Files", buffer), "\"C:\\Program Files\"") == 0);

	/* trailing backslash with quotes - backslashes before closing quote must be doubled */
	assert(strcmp(esc_shell("C:\\folder\\ ", buffer), "\"C:\\folder\\ \"") == 0);
	assert(strcmp(esc_shell("path\\ ", buffer), "\"path\\ \"") == 0);
	assert(strcmp(esc_shell("C:\\My Documents\\", buffer), "\"C:\\My Documents\\\\\"") == 0);

	/* backslash before embedded quote - backslash before quote must be doubled */
	assert(strcmp(esc_shell("C:\\\"test\"", buffer), "\"C:\\\\\\\"test\\\"\"") == 0);
	assert(strcmp(esc_shell("path\\\"file\"", buffer), "\"path\\\\\\\"file\\\"\"") == 0);

	/* multiple trailing backslashes before end with quotes */
	assert(strcmp(esc_shell("test\\\\ ", buffer), "\"test\\\\ \"") == 0);
	assert(strcmp(esc_shell("path\\\\\\\\ ", buffer), "\"path\\\\\\\\ \"") == 0);

	/* backslash NOT before quote - normal backslash */
	assert(strcmp(esc_shell("test\\file ", buffer), "\"test\\file \"") == 0);
	assert(strcmp(esc_shell("a\\b c", buffer), "\"a\\b c\"") == 0);

	/* control characters - require quoting */
	assert(strcmp(esc_shell("\x01", buffer), "\"\x01\"") == 0);
	assert(strcmp(esc_shell("\x1F", buffer), "\"\x1F\"") == 0);
	assert(strcmp(esc_shell("\x7F", buffer), "\"\x7F\"") == 0); /* DEL character */
	assert(strcmp(esc_shell("test\x01test", buffer), "\"test\x01test\"") == 0);

	/* complex real-world examples */
	assert(strcmp(esc_shell("C:\\Program Files\\App", buffer), "\"C:\\Program Files\\App\"") == 0);
	assert(strcmp(esc_shell("C:\\Program Files (x86)\\", buffer), "\"C:\\Program Files (x86)\\\\\"") == 0);
	assert(strcmp(esc_shell("file (copy).txt", buffer), "\"file (copy).txt\"") == 0);
	assert(strcmp(esc_shell("setup-v1.0.exe", buffer), "setup-v1.0.exe") == 0);
	assert(strcmp(esc_shell("setup v1.0.exe", buffer), "\"setup v1.0.exe\"") == 0);

	/* mixed quotes and special chars */
	assert(strcmp(esc_shell("say \"hi\" & exit", buffer), "\"say \\\"hi\\\" & exit\"") == 0);
	assert(strcmp(esc_shell("test \"a|b\"", buffer), "\"test \\\"a|b\\\"\"") == 0);

	/* empty string */
	assert(strcmp(esc_shell("", buffer), "") == 0);

	/* all safe characters that don't need escaping */
	assert(strcmp(esc_shell("abcdefghijklmnopqrstuvwxyz", buffer), "abcdefghijklmnopqrstuvwxyz") == 0);
	assert(strcmp(esc_shell("ABCDEFGHIJKLMNOPQRSTUVWXYZ", buffer), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 0);
	assert(strcmp(esc_shell("0123456789", buffer), "0123456789") == 0);
	assert(strcmp(esc_shell("._-+,@:", buffer), "._-+,@:") == 0);
#else
	/* basic cases - no special characters */
	assert(strcmp(esc_shell("simple", buffer), "simple") == 0);
	assert(strcmp(esc_shell("file.txt", buffer), "file.txt") == 0);
	assert(strcmp(esc_shell("file123", buffer), "file123") == 0);
	assert(strcmp(esc_shell("file_name-test.doc", buffer), "file_name-test.doc") == 0);
	assert(strcmp(esc_shell(",._+:@/-", buffer), ",._+:@/-") == 0);
	assert(strcmp(esc_shell("/usr/local/bin", buffer), "/usr/local/bin") == 0);

	/* empty string */
	assert(strcmp(esc_shell("", buffer), "") == 0);

	/* space - escape with backslash */
	assert(strcmp(esc_shell(" ", buffer), "\\ ") == 0);
	assert(strcmp(esc_shell("file name.txt", buffer), "file\\ name.txt") == 0);
	assert(strcmp(esc_shell("my document.doc", buffer), "my\\ document.doc") == 0);
	assert(strcmp(esc_shell("  spaces  ", buffer), "\\ \\ spaces\\ \\ ") == 0);
	assert(strcmp(esc_shell("a b c", buffer), "a\\ b\\ c") == 0);

	/* tab - escape with backslash */
	assert(strcmp(esc_shell("\t", buffer), "\\\t") == 0);
	assert(strcmp(esc_shell("file\tname", buffer), "file\\\tname") == 0);
	assert(strcmp(esc_shell("\t\t", buffer), "\\\t\\\t") == 0);

	/* newline - escape with backslash */
	assert(strcmp(esc_shell("\n", buffer), "\\\n") == 0);
	assert(strcmp(esc_shell("line1\nline2", buffer), "line1\\\nline2") == 0);
	assert(strcmp(esc_shell("\n\n", buffer), "\\\n\\\n") == 0);

	/* carriage return - escape with backslash */
	assert(strcmp(esc_shell("\r", buffer), "\\\r") == 0);
	assert(strcmp(esc_shell("text\r\n", buffer), "text\\\r\\\n") == 0);

	/* tilde (home directory expansion) */
	assert(strcmp(esc_shell("~", buffer), "\\~") == 0);
	assert(strcmp(esc_shell("~/file", buffer), "\\~/file") == 0);
	assert(strcmp(esc_shell("file~name", buffer), "file\\~name") == 0);
	assert(strcmp(esc_shell("~user", buffer), "\\~user") == 0);

	/* backtick (command substitution) */
	assert(strcmp(esc_shell("`", buffer), "\\`") == 0);
	assert(strcmp(esc_shell("`command`", buffer), "\\`command\\`") == 0);
	assert(strcmp(esc_shell("test`test", buffer), "test\\`test") == 0);
	assert(strcmp(esc_shell("``", buffer), "\\`\\`") == 0);

	/* hash (comment) */
	assert(strcmp(esc_shell("#", buffer), "\\#") == 0);
	assert(strcmp(esc_shell("#comment", buffer), "\\#comment") == 0);
	assert(strcmp(esc_shell("file#name", buffer), "file\\#name") == 0);
	assert(strcmp(esc_shell("test#123", buffer), "test\\#123") == 0);

	/* dollar sign (variable expansion) */
	assert(strcmp(esc_shell("$", buffer), "\\$") == 0);
	assert(strcmp(esc_shell("$$", buffer), "\\$\\$") == 0);
	assert(strcmp(esc_shell("$VAR", buffer), "\\$VAR") == 0);
	assert(strcmp(esc_shell("${VAR}", buffer), "\\$\\{VAR\\}") == 0);
	assert(strcmp(esc_shell("test$test", buffer), "test\\$test") == 0);
	assert(strcmp(esc_shell("$1", buffer), "\\$1") == 0);
	assert(strcmp(esc_shell("$PATH", buffer), "\\$PATH") == 0);

	/* ampersand (background job) */
	assert(strcmp(esc_shell("&", buffer), "\\&") == 0);
	assert(strcmp(esc_shell("&&", buffer), "\\&\\&") == 0);
	assert(strcmp(esc_shell("file&name", buffer), "file\\&name") == 0);
	assert(strcmp(esc_shell("a&b&c", buffer), "a\\&b\\&c") == 0);
	assert(strcmp(esc_shell("cmd1 & cmd2", buffer), "cmd1\\ \\&\\ cmd2") == 0);

	/* asterisk (wildcard) */
	assert(strcmp(esc_shell("*", buffer), "\\*") == 0);
	assert(strcmp(esc_shell("**", buffer), "\\*\\*") == 0);
	assert(strcmp(esc_shell("*.txt", buffer), "\\*.txt") == 0);
	assert(strcmp(esc_shell("file*name", buffer), "file\\*name") == 0);
	assert(strcmp(esc_shell("test*", buffer), "test\\*") == 0);

	/* parentheses (subshell) */
	assert(strcmp(esc_shell("(", buffer), "\\(") == 0);
	assert(strcmp(esc_shell(")", buffer), "\\)") == 0);
	assert(strcmp(esc_shell("()", buffer), "\\(\\)") == 0);
	assert(strcmp(esc_shell("(test)", buffer), "\\(test\\)") == 0);
	assert(strcmp(esc_shell("file(1)", buffer), "file\\(1\\)") == 0);
	assert(strcmp(esc_shell("(a)(b)", buffer), "\\(a\\)\\(b\\)") == 0);

	/* backslash (escape character) */
	assert(strcmp(esc_shell("\\", buffer), "\\\\") == 0);
	assert(strcmp(esc_shell("\\\\", buffer), "\\\\\\\\") == 0);
	assert(strcmp(esc_shell("path\\to\\file", buffer), "path\\\\to\\\\file") == 0);
	assert(strcmp(esc_shell("test\\test", buffer), "test\\\\test") == 0);
	assert(strcmp(esc_shell("a\\b\\c", buffer), "a\\\\b\\\\c") == 0);

	/* pipe (pipeline) */
	assert(strcmp(esc_shell("|", buffer), "\\|") == 0);
	assert(strcmp(esc_shell("||", buffer), "\\|\\|") == 0);
	assert(strcmp(esc_shell("file|name", buffer), "file\\|name") == 0);
	assert(strcmp(esc_shell("a|b|c", buffer), "a\\|b\\|c") == 0);
	assert(strcmp(esc_shell("cmd1 | cmd2", buffer), "cmd1\\ \\|\\ cmd2") == 0);

	/* square brackets (wildcard) */
	assert(strcmp(esc_shell("[", buffer), "\\[") == 0);
	assert(strcmp(esc_shell("]", buffer), "\\]") == 0);
	assert(strcmp(esc_shell("[]", buffer), "\\[\\]") == 0);
	assert(strcmp(esc_shell("[abc]", buffer), "\\[abc\\]") == 0);
	assert(strcmp(esc_shell("file[1]", buffer), "file\\[1\\]") == 0);
	assert(strcmp(esc_shell("[0-9]", buffer), "\\[0-9\\]") == 0);

	/* curly braces (brace expansion) */
	assert(strcmp(esc_shell("{", buffer), "\\{") == 0);
	assert(strcmp(esc_shell("}", buffer), "\\}") == 0);
	assert(strcmp(esc_shell("{}", buffer), "\\{\\}") == 0);
	assert(strcmp(esc_shell("{a,b,c}", buffer), "\\{a,b,c\\}") == 0);
	assert(strcmp(esc_shell("file{1,2}", buffer), "file\\{1,2\\}") == 0);
	assert(strcmp(esc_shell("{1..10}", buffer), "\\{1..10\\}") == 0);

	/* semicolon (command separator) */
	assert(strcmp(esc_shell(";", buffer), "\\;") == 0);
	assert(strcmp(esc_shell(";;", buffer), "\\;\\;") == 0);
	assert(strcmp(esc_shell("cmd1;cmd2", buffer), "cmd1\\;cmd2") == 0);
	assert(strcmp(esc_shell("test;test", buffer), "test\\;test") == 0);
	assert(strcmp(esc_shell("a; b", buffer), "a\\;\\ b") == 0);

	/* single quote */
	assert(strcmp(esc_shell("'", buffer), "\\'") == 0);
	assert(strcmp(esc_shell("''", buffer), "\\'\\'") == 0);
	assert(strcmp(esc_shell("'test'", buffer), "\\'test\\'") == 0);
	assert(strcmp(esc_shell("file'name", buffer), "file\\'name") == 0);
	assert(strcmp(esc_shell("it's", buffer), "it\\'s") == 0);

	/* double quote */
	assert(strcmp(esc_shell("\"", buffer), "\\\"") == 0);
	assert(strcmp(esc_shell("\"\"", buffer), "\\\"\\\"") == 0);
	assert(strcmp(esc_shell("\"test\"", buffer), "\\\"test\\\"") == 0);
	assert(strcmp(esc_shell("file\"name", buffer), "file\\\"name") == 0);
	assert(strcmp(esc_shell("say \"hi\"", buffer), "say\\ \\\"hi\\\"") == 0);

	/* angle brackets (redirection) */
	assert(strcmp(esc_shell("<", buffer), "\\<") == 0);
	assert(strcmp(esc_shell(">", buffer), "\\>") == 0);
	assert(strcmp(esc_shell("<<", buffer), "\\<\\<") == 0);
	assert(strcmp(esc_shell(">>", buffer), "\\>\\>") == 0);
	assert(strcmp(esc_shell("a<b>c", buffer), "a\\<b\\>c") == 0);
	assert(strcmp(esc_shell("file>output", buffer), "file\\>output") == 0);
	assert(strcmp(esc_shell("cmd < in > out", buffer), "cmd\\ \\<\\ in\\ \\>\\ out") == 0);

	/* question mark (wildcard) */
	assert(strcmp(esc_shell("?", buffer), "\\?") == 0);
	assert(strcmp(esc_shell("??", buffer), "\\?\\?") == 0);
	assert(strcmp(esc_shell("file?.txt", buffer), "file\\?.txt") == 0);
	assert(strcmp(esc_shell("test?test", buffer), "test\\?test") == 0);
	assert(strcmp(esc_shell("file??", buffer), "file\\?\\?") == 0);

	/* equals sign (assignment in some contexts) */
	assert(strcmp(esc_shell("=", buffer), "\\=") == 0);
	assert(strcmp(esc_shell("==", buffer), "\\=\\=") == 0);
	assert(strcmp(esc_shell("VAR=value", buffer), "VAR\\=value") == 0);
	assert(strcmp(esc_shell("a=b", buffer), "a\\=b") == 0);
	assert(strcmp(esc_shell("PATH=/usr/bin", buffer), "PATH\\=/usr/bin") == 0);

	/* exclamation mark (history expansion) */
	assert(strcmp(esc_shell("!", buffer), "\\!") == 0);
	assert(strcmp(esc_shell("!!", buffer), "\\!\\!") == 0);
	assert(strcmp(esc_shell("test!test", buffer), "test\\!test") == 0);
	assert(strcmp(esc_shell("!$", buffer), "\\!\\$") == 0);
	assert(strcmp(esc_shell("!123", buffer), "\\!123") == 0);

	/* control characters (0x01-0x1F) - escape with backslash */
	assert(strcmp(esc_shell("\x01", buffer), "\\\x01") == 0);
	assert(strcmp(esc_shell("\x02", buffer), "\\\x02") == 0);
	assert(strcmp(esc_shell("\x1F", buffer), "\\\x1F") == 0);
	assert(strcmp(esc_shell("test\x01test", buffer), "test\\\x01test") == 0);

	/* DEL character (0x7F) */
	assert(strcmp(esc_shell("\x7F", buffer), "\\\x7F") == 0);
	assert(strcmp(esc_shell("test\x7Ftest", buffer), "test\\\x7Ftest") == 0);

	/* multiple special characters combined */
	assert(strcmp(esc_shell("$VAR & $OTHER", buffer), "\\$VAR\\ \\&\\ \\$OTHER") == 0);
	assert(strcmp(esc_shell("*.txt | grep test", buffer), "\\*.txt\\ \\|\\ grep\\ test") == 0);
	assert(strcmp(esc_shell("file (1) [copy].txt", buffer), "file\\ \\(1\\)\\ \\[copy\\].txt") == 0);
	assert(strcmp(esc_shell("a & b | c", buffer), "a\\ \\&\\ b\\ \\|\\ c") == 0);
	assert(strcmp(esc_shell("cmd1; cmd2 && cmd3", buffer), "cmd1\\;\\ cmd2\\ \\&\\&\\ cmd3") == 0);

	/* complex real-world examples */
	assert(strcmp(esc_shell("/home/user/My Documents", buffer), "/home/user/My\\ Documents") == 0);
	assert(strcmp(esc_shell("/path/to/file (copy).txt", buffer), "/path/to/file\\ \\(copy\\).txt") == 0);
	assert(strcmp(esc_shell("~/project/file-v1.0.tar.gz", buffer), "\\~/project/file-v1.0.tar.gz") == 0);
	assert(strcmp(esc_shell("$(whoami)@$(hostname)", buffer), "\\$\\(whoami\\)@\\$\\(hostname\\)") == 0);
	assert(strcmp(esc_shell("test && echo 'done'", buffer), "test\\ \\&\\&\\ echo\\ \\'done\\'") == 0);
	assert(strcmp(esc_shell("file #1 [important].txt", buffer), "file\\ \\#1\\ \\[important\\].txt") == 0);
	assert(strcmp(esc_shell("/tmp/test (1).txt", buffer), "/tmp/test\\ \\(1\\).txt") == 0);
	assert(strcmp(esc_shell("var=$HOME/bin:$PATH", buffer), "var\\=\\$HOME/bin:\\$PATH") == 0);

	/* edge cases with multiple escapes */
	assert(strcmp(esc_shell("a\\ b", buffer), "a\\\\\\ b") == 0);
	assert(strcmp(esc_shell("'\"test\"'", buffer), "\\'\\\"test\\\"\\'") == 0);
	assert(strcmp(esc_shell("$(echo \"test\")", buffer), "\\$\\(echo\\ \\\"test\\\"\\)") == 0);

	/* all safe characters that don't need escaping */
	assert(strcmp(esc_shell("abcdefghijklmnopqrstuvwxyz", buffer), "abcdefghijklmnopqrstuvwxyz") == 0);
	assert(strcmp(esc_shell("ABCDEFGHIJKLMNOPQRSTUVWXYZ", buffer), "ABCDEFGHIJKLMNOPQRSTUVWXYZ") == 0);
	assert(strcmp(esc_shell("0123456789", buffer), "0123456789") == 0);
	assert(strcmp(esc_shell("._-+,@:", buffer), "._-+,@:") == 0);
	assert(strcmp(esc_shell("/path/to/file", buffer), "/path/to/file") == 0);
	assert(strcmp(esc_shell("simple_file-name.txt", buffer), "simple_file-name.txt") == 0);

	/* file extensions and versions */
	assert(strcmp(esc_shell("file.tar.gz", buffer), "file.tar.gz") == 0);
	assert(strcmp(esc_shell("app-v1.2.3.deb", buffer), "app-v1.2.3.deb") == 0);
	assert(strcmp(esc_shell("test_2024-01-01.log", buffer), "test_2024-01-01.log") == 0);
#endif

	printf("Everything OK\n");

	exit(EXIT_SUCCESS);
}

/****************************************************************************/
/* log */

void log_open(const char* file)
{
	char path[PATH_MAX];
	const char* mode;
	char text_T[32];
	char text_D[32];
	time_t t;
	struct tm* tm;
#if HAVE_LOCALTIME_R
	struct tm tm_res;
#endif

	/* leave stdlog at 0 if not specified */
	if (file == 0)
		return;

	t = time(0);
#if HAVE_LOCALTIME_R
	tm = localtime_r(&t, &tm_res);
#else
	tm = localtime(&t);
#endif
	if (tm) {
		strftime(text_T, sizeof(text_T), "%H%M%S", tm);
		strftime(text_D, sizeof(text_T), "%Y%m%d", tm);
	} else {
		/* LCOV_EXCL_START */
		strcpy(text_T, "invalid");
		strcpy(text_D, "invalid");
		/* LCOV_EXCL_STOP */
	}

	/* file mode */
	mode = "wt";
	if (*file == '>') {
		++file;

		if (*file == '>') {
			mode = "at";
			++file;
		}

		if (file[0] == '&' && file[1] == '1') {
			stdlog = stdout;
			return;
		}

		if (file[0] == '&' && file[1] == '2') {
			stdlog = stderr;
			return;
		}
	}

	/* process the path */
	for (*path = 0; *file != 0; ) {
		switch (*file) {
		case '%' :
			++file;
			switch (*file) {
			case '%' :
				pathcatc(path, sizeof(path), '%');
				break;
			case 'T' :
				pathcat(path, sizeof(path), text_T);
				break;
			case 'D' :
				pathcat(path, sizeof(path), text_D);
				break;
			default :
				/* LCOV_EXCL_START */
				log_fatal("Invalid type specifier '%c' in the log file.\n", *file);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		default :
			pathcatc(path, sizeof(path), *file);
			break;
		}
		++file;
	}

	stdlog = fopen(path, mode);
	if (!stdlog) {
		/* LCOV_EXCL_START */
		log_fatal("Error opening the log file '%s'. %s.\n", path, strerror(errno));
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}
}

void log_close(const char* file)
{
	if (stdlog != stdout && stdlog != stderr && stdlog != 0) {
		if (fclose(stdlog) != 0) {
			/* LCOV_EXCL_START */
			log_fatal("Error closing the log file '%s'. %s.\n", file, strerror(errno));
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	stdlog = 0;
}

/****************************************************************************/
/* config */

void config(char* conf, size_t conf_size, const char* argv0)
{
#ifdef _WIN32
	char* slash;

	pathimport(conf, conf_size, argv0);

	slash = strrchr(conf, '/');
	if (slash) {
		slash[1] = 0;
		pathcat(conf, conf_size, PACKAGE ".conf");
	} else {
		pathcpy(conf, conf_size, PACKAGE ".conf");
	}
#else
	(void)argv0;

#ifdef SYSCONFDIR
	/* if it exists, give precedence at sysconfdir, usually /usr/local/etc */
	if (access(SYSCONFDIR "/" PACKAGE ".conf", F_OK) == 0)
		pathcpy(conf, conf_size, SYSCONFDIR "/" PACKAGE ".conf");
	else /* otherwise fallback to plain /etc */
#endif
		pathcpy(conf, conf_size, "/etc/" PACKAGE ".conf");
#endif
}

/****************************************************************************/
/* main */

#define OPT_TEST_SKIP_SELF 256
#define OPT_TEST_KILL_AFTER_SYNC 257
#define OPT_TEST_EXPECT_UNRECOVERABLE 258
#define OPT_TEST_EXPECT_RECOVERABLE 259
#define OPT_TEST_SKIP_SIGN 260
#define OPT_TEST_SKIP_FALLOCATE 261
#define OPT_TEST_SKIP_DEVICE 262
#define OPT_TEST_FORCE_MURMUR3 264
#define OPT_TEST_FORCE_SPOOKY2 265
#define OPT_TEST_SKIP_LOCK 266
#define OPT_TEST_FORCE_ORDER_PHYSICAL 267
#define OPT_TEST_FORCE_ORDER_INODE 268
#define OPT_TEST_FORCE_ORDER_ALPHA 269
#define OPT_TEST_FORCE_ORDER_DIR 270
#define OPT_TEST_FORCE_SCRUB_AT 271
#define OPT_TEST_FORCE_SCRUB_EVEN 272
#define OPT_TEST_FORCE_CONTENT_WRITE 273
#define OPT_TEST_SKIP_CONTENT_CHECK 275
#define OPT_TEST_SKIP_PARITY_ACCESS 276
#define OPT_TEST_EXPECT_FAILURE 277
#define OPT_TEST_RUN 278
#define OPT_TEST_FORCE_SCAN_WINFIND 279
#define OPT_TEST_IMPORT_CONTENT 280
#define OPT_TEST_FORCE_PROGRESS 281
#define OPT_TEST_SKIP_DISK_ACCESS 282
#define OPT_TEST_FORCE_AUTOSAVE_AT 283
#define OPT_TEST_FAKE_DEVICE 284
#define OPT_TEST_EXPECT_NEED_SYNC 285
#define OPT_NO_WARNINGS 286
#define OPT_TEST_FAKE_UUID 287
#define OPT_TEST_MATCH_FIRST_UUID 288
#define OPT_TEST_FORCE_PARITY_UPDATE 289
#define OPT_TEST_IO_CACHE 290
#define OPT_TEST_IO_STATS 291
#define OPT_TEST_COND_SIGNAL_OUTSIDE 292
#define OPT_TEST_IO_ADVISE_NONE 293
#define OPT_TEST_IO_ADVISE_SEQUENTIAL 294
#define OPT_TEST_IO_ADVISE_FLUSH 295
#define OPT_TEST_IO_ADVISE_FLUSH_WINDOW 296
#define OPT_TEST_IO_ADVISE_DISCARD 297
#define OPT_TEST_IO_ADVISE_DISCARD_WINDOW 298
#define OPT_TEST_IO_ADVISE_DIRECT 299
#define OPT_TEST_PARITY_LIMIT 301
#define OPT_TEST_SKIP_CONTENT_WRITE 302
#define OPT_TEST_SKIP_SPACE_HOLDER 303
#define OPT_TEST_FORMAT 304
#define OPT_TEST_SKIP_MULTI_SCAN 305

#if HAVE_GETOPT_LONG
struct option long_options[] = {
	{ "conf", 1, 0, 'c' },
	{ "filter", 1, 0, 'f' },
	{ "filter-disk", 1, 0, 'd' },
	{ "filter-missing", 0, 0, 'm' },
	{ "filter-error", 0, 0, 'e' },
	{ "filter-block-error", 0, 0, 'b' },
	{ "percentage", 1, 0, 'p' }, /* legacy name for --plan */
	{ "plan", 1, 0, 'p' },
	{ "older-than", 1, 0, 'o' },
	{ "start", 1, 0, 'S' },
	{ "count", 1, 0, 'B' },
	{ "error-limit", 1, 0, 'L' },
	{ "import", 1, 0, 'i' },
	{ "log", 1, 0, 'l' },
	{ "stats", 0, 0, 'A' },
	{ "force-zero", 0, 0, 'Z' },
	{ "force-empty", 0, 0, 'E' },
	{ "force-uuid", 0, 0, 'U' },
	{ "force-device", 0, 0, 'D' },
	{ "force-nocopy", 0, 0, 'N' },
	{ "force-full", 0, 0, 'F' },
	{ "force-realloc", 0, 0, 'R' },
	{ "bw-limit", 1, 0, 'w' },
	{ "audit-only", 0, 0, 'a' },
	{ "pre-hash", 0, 0, 'h' },
	{ "speed-test", 0, 0, 'T' }, /* undocumented speed test command */
	{ "gen-conf", 1, 0, 'C' },
	{ "verbose", 0, 0, 'v' },
	{ "quiet", 0, 0, 'q' },
	{ "gui", 0, 0, 'G' }, /* undocumented GUI interface option */
	{ "help", 0, 0, 'H' },
	{ "version", 0, 0, 'V' },

	/* The following are test specific options, DO NOT USE! */

	/* After syncing, do not write the new content file */
	{ "test-kill-after-sync", 0, 0, OPT_TEST_KILL_AFTER_SYNC },

	/* Exit with failure if after check/fix there ARE NOT unrecoverable errors. */
	{ "test-expect-unrecoverable", 0, 0, OPT_TEST_EXPECT_UNRECOVERABLE },

	/* Exit with failure if after check/fix there ARE NOT recoverable errors. */
	{ "test-expect-recoverable", 0, 0, OPT_TEST_EXPECT_RECOVERABLE },

	/* Skip the initial self test */
	{ "test-skip-self", 0, 0, OPT_TEST_SKIP_SELF },

	/* Skip the initial sign check when reading the content file */
	{ "test-skip-sign", 0, 0, OPT_TEST_SKIP_SIGN },

	/* Skip the fallocate() when growing the parity files */
	{ "test-skip-fallocate", 0, 0, OPT_TEST_SKIP_FALLOCATE },

	/* Skip the device check */
	{ "test-skip-device", 0, 0, OPT_TEST_SKIP_DEVICE },

	/* Force Murmur3 hash */
	{ "test-force-murmur3", 0, 0, OPT_TEST_FORCE_MURMUR3 },

	/* Force Spooky2 hash */
	{ "test-force-spooky2", 0, 0, OPT_TEST_FORCE_SPOOKY2 },

	/* Skip the use of lock file */
	{ "test-skip-lock", 0, 0, OPT_TEST_SKIP_LOCK },

	/* Force a sort order for files */
	{ "test-force-order-physical", 0, 0, OPT_TEST_FORCE_ORDER_PHYSICAL },
	{ "test-force-order-inode", 0, 0, OPT_TEST_FORCE_ORDER_INODE },
	{ "test-force-order-alpha", 0, 0, OPT_TEST_FORCE_ORDER_ALPHA },
	{ "test-force-order-dir", 0, 0, OPT_TEST_FORCE_ORDER_DIR },

	/* Force scrub of the specified number of blocks */
	{ "test-force-scrub-at", 1, 0, OPT_TEST_FORCE_SCRUB_AT },

	/* Force scrub of all the even blocks. This is really for testing, don't try it */
	{ "test-force-scrub-even", 0, 0, OPT_TEST_FORCE_SCRUB_EVEN },

	/* Force write of the content file even if no modification is done */
	{ "test-force-content-write", 0, 0, OPT_TEST_FORCE_CONTENT_WRITE },

	/* Relax the checks done at the content file */
	{ "test-skip-content-check", 0, 0, OPT_TEST_SKIP_CONTENT_CHECK },

	/* Skip the parity access */
	{ "test-skip-parity-access", 0, 0, OPT_TEST_SKIP_PARITY_ACCESS },

	/* Exit generic failure */
	{ "test-expect-failure", 0, 0, OPT_TEST_EXPECT_FAILURE },

	/* Exit generic need sync */
	{ "test-expect-need-sync", 0, 0, OPT_TEST_EXPECT_NEED_SYNC },

	/* Run some command after loading the state and before the command */
	{ "test-run", 1, 0, OPT_TEST_RUN },

	/* Use the FindFirst/Next approach in Windows to list files */
	{ "test-force-scan-winfind", 0, 0, OPT_TEST_FORCE_SCAN_WINFIND },

	/* Alternative import working by data */
	{ "test-import-content", 1, 0, OPT_TEST_IMPORT_CONTENT },

	/* Force immediate progress state update */
	{ "test-force-progress", 0, 0, OPT_TEST_FORCE_PROGRESS },

	/* Skip the disk access */
	{ "test-skip-disk-access", 0, 0, OPT_TEST_SKIP_DISK_ACCESS },

	/* Force autosave at the specified block */
	{ "test-force-autosave-at", 1, 0, OPT_TEST_FORCE_AUTOSAVE_AT },

	/* Fake device data */
	{ "test-fake-device", 0, 0, OPT_TEST_FAKE_DEVICE },

	/* Disable annoying warnings */
	{ "no-warnings", 0, 0, OPT_NO_WARNINGS },

	/* Fake UUID */
	{ "test-fake-uuid", 0, 0, OPT_TEST_FAKE_UUID },

	/* Match first UUID */
	{ "test-match-first-uuid", 0, 0, OPT_TEST_MATCH_FIRST_UUID },

	/* Force parity update even if all the data hash is already matching */
	{ "test-force-parity-update", 0, 0, OPT_TEST_FORCE_PARITY_UPDATE },

	/* Number of IO buffers */
	{ "test-io-cache", 1, 0, OPT_TEST_IO_CACHE },

	/* Print IO stats */
	{ "test-io-stats", 0, 0, OPT_TEST_IO_STATS }, /* now replaced by -A, --stats */

	/* Signal condition variable outside the mutex */
	{ "test-cond-signal-outside", 0, 0, OPT_TEST_COND_SIGNAL_OUTSIDE },

	/* Set the io advise to none */
	{ "test-io-advise-none", 0, 0, OPT_TEST_IO_ADVISE_NONE },

	/* Set the io advise to sequential */
	{ "test-io-advise-sequential", 0, 0, OPT_TEST_IO_ADVISE_SEQUENTIAL },

	/* Set the io advise to flush */
	{ "test-io-advise-flush", 0, 0, OPT_TEST_IO_ADVISE_FLUSH },

	/* Set the io advise to flush window */
	{ "test-io-advise-flush-window", 0, 0, OPT_TEST_IO_ADVISE_FLUSH_WINDOW },

	/* Set the io advise to discard */
	{ "test-io-advise-discard", 0, 0, OPT_TEST_IO_ADVISE_DISCARD },

	/* Set the io advise to discard window */
	{ "test-io-advise-discard-window", 0, 0, OPT_TEST_IO_ADVISE_DISCARD_WINDOW },

	/* Set the io advise to direct */
	{ "test-io-advise-direct", 0, 0, OPT_TEST_IO_ADVISE_DIRECT },

	/* Set an artificial parity limit */
	{ "test-parity-limit", 1, 0, OPT_TEST_PARITY_LIMIT },

	/* Skip content write */
	{ "test-skip-content-write", 0, 0, OPT_TEST_SKIP_CONTENT_WRITE },

	/* Skip space holder file in parity disks */
	{ "test-skip-space-holder", 0, 0, OPT_TEST_SKIP_SPACE_HOLDER },

	/* Set the output format */
	{ "test-fmt", 1, 0, OPT_TEST_FORMAT },

	/* Skip thread in disk scan */
	{ "test-skip-multi-scan", 0, 0, OPT_TEST_SKIP_MULTI_SCAN },

	{ 0, 0, 0, 0 }
};
#endif

/*
 * Free letters: gIjJkKMnPQrtuxXWz
 *
 * The 's' letter is used in main.c
 */
#define OPTIONS "c:f:d:mebp:o:S:B:L:i:l:AZEUDNFRahTC:vqHVGw:"

volatile int global_interrupt = 0;

/* LCOV_EXCL_START */
void signal_handler(int signum)
{
	(void)signum;

	/* report the request of interruption */
	global_interrupt = 1;
}
/* LCOV_EXCL_STOP */

void signal_init(void)
{
#if HAVE_SIGACTION
	struct sigaction sa;

	sa.sa_handler = signal_handler;
	sigemptyset(&sa.sa_mask);

	/* use the SA_RESTART to automatically restart interrupted system calls */
	sa.sa_flags = SA_RESTART;

	sigaction(SIGHUP, &sa, 0);
	sigaction(SIGTERM, &sa, 0);
	sigaction(SIGINT, &sa, 0);
	sigaction(SIGQUIT, &sa, 0);
#else
	signal(SIGINT, signal_handler);
#endif
}

#define OPERATION_DIFF 0
#define OPERATION_SYNC 1
#define OPERATION_CHECK 2
#define OPERATION_FIX 3
#define OPERATION_DRY 4
#define OPERATION_DUP 5
#define OPERATION_LIST 6
#define OPERATION_POOL 7
#define OPERATION_REHASH 8
#define OPERATION_SCRUB 9
#define OPERATION_STATUS 10
#define OPERATION_REWRITE 11
#define OPERATION_READ 12
#define OPERATION_TOUCH 13
#define OPERATION_SPINUP 14
#define OPERATION_SPINDOWN 15
#define OPERATION_DEVICES 16
#define OPERATION_SMART 17
#define OPERATION_PROBE 18

int snapraid_main(int argc, char* argv[])
{
	int c;
	struct snapraid_option opt;
	char conf[PATH_MAX];
	struct snapraid_state state;
	int operation;
	block_off_t blockstart;
	block_off_t blockcount;
	int ret;
	tommy_list filterlist_file;
	tommy_list filterlist_disk;
	int filter_missing;
	int filter_error;
	int plan;
	int olderthan;
	char* e;
	const char* command;
	const char* import_timestamp;
	const char* import_content;
	const char* log_file;
	int lock;
	const char* gen_conf;
	const char* run;
	int speedtest;
	int period;
	time_t t;
	struct tm* tm;
#if HAVE_LOCALTIME_R
	struct tm tm_res;
#endif
	int i;

	test(argc, argv);

	lock_init();

	/* defaults */
	config(conf, sizeof(conf), argv[0]);
	memset(&opt, 0, sizeof(opt));
	opt.io_error_limit = 100;
	blockstart = 0;
	blockcount = 0;
	tommy_list_init(&filterlist_file);
	tommy_list_init(&filterlist_disk);
	period = 1000;
	filter_missing = 0;
	filter_error = 0;
	plan = SCRUB_AUTO;
	olderthan = SCRUB_AUTO;
	import_timestamp = 0;
	import_content = 0;
	log_file = 0;
	lock = 0;
	gen_conf = 0;
	speedtest = 0;
	run = 0;

	opterr = 0;
	while ((c =
#if HAVE_GETOPT_LONG
		getopt_long(argc, argv, OPTIONS, long_options, 0))
#else
		getopt(argc, argv, OPTIONS))
#endif
		!= EOF) {
		switch (c) {
		case 'c' :
			pathimport(conf, sizeof(conf), optarg);
			break;
		case 'f' : {
			struct snapraid_filter* filter = filter_alloc_file(1, optarg);
			if (!filter) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid filter specification '%s'\n", optarg);
				log_fatal("Filters using relative paths are not supported. Ensure to add an initial slash\n");
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			tommy_list_insert_tail(&filterlist_file, &filter->node, filter);
		} break;
		case 'd' : {
			struct snapraid_filter* filter = filter_alloc_disk(1, optarg);
			if (!filter) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid filter specification '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			tommy_list_insert_tail(&filterlist_disk, &filter->node, filter);
		} break;
		case 'm' :
			filter_missing = 1;
			opt.expected_missing = 1;
			break;
		case 'e' :
			/* when processing only error, we filter files */
			/* and we apply fixes only to synced ones */
			filter_error = 1;
			opt.badfileonly = 1;
			opt.syncedonly = 1;
			break;
		case 'b' :
			/* when processing only block with error, we filter both files and blocks */
			/* and we apply fixes only to synced ones */
			filter_error = 1;
			opt.badfileonly = 1;
			opt.badblockonly = 1;
			opt.syncedonly = 1;
			break;
		case 'p' :
			if (strcmp(optarg, "bad") == 0) {
				plan = SCRUB_BAD;
			} else if (strcmp(optarg, "new") == 0) {
				plan = SCRUB_NEW;
			} else if (strcmp(optarg, "full") == 0) {
				plan = SCRUB_FULL;
			} else {
				plan = strtoul(optarg, &e, 10);
				if (!e || *e || plan > 100) {
					/* LCOV_EXCL_START */
					log_fatal("Invalid plan/percentage '%s'\n", optarg);
					exit(EXIT_FAILURE);
					/* LCOV_EXCL_STOP */
				}
			}
			break;
		case 'o' :
			olderthan = strtoul(optarg, &e, 10);
			if (!e || *e || olderthan > 1000) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid number of days '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case 'w' : /* --bw-limit */
			if (optarg == 0) {
				/* LCOV_EXCL_START */
				log_fatal("Missing bandwidth limit\n");
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}

			/* Parse the number part */
			opt.bwlimit = strtoul(optarg, &e, 10);
			if (!e || e == optarg) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid bandwidth limit '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}

			/* Handle suffixes */
			if ((e[0] == 'k' || e[0] == 'K') && e[1] == 0) {
				opt.bwlimit *= 1000;
			} else if ((e[0] == 'm' || e[0] == 'M') && e[1] == 0) {
				opt.bwlimit *= 1000 * 1000;
			} else if ((e[0] == 'g' || e[0] == 'G') && e[1] == 0) {
				opt.bwlimit *= 1000 * 1000 * 1000;
			} else if (e[0] != '\0') {
				/* LCOV_EXCL_START */
				log_fatal("Invalid bandwidth limit suffix '%s'\n", e);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case 'S' :
			blockstart = strtoul(optarg, &e, 0);
			if (!e || *e) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid start position '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case 'B' :
			blockcount = strtoul(optarg, &e, 0);
			if (!e || *e) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid count number '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case 'L' :
			opt.io_error_limit = strtoul(optarg, &e, 0);
			if (!e || *e) {
				/* LCOV_EXCL_START */
				log_fatal("Invalid error limit number '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case 'i' :
			if (import_timestamp) {
				/* LCOV_EXCL_START */
				log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_timestamp);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			import_timestamp = optarg;
			break;
		case OPT_TEST_IMPORT_CONTENT :
			if (import_content) {
				/* LCOV_EXCL_START */
				log_fatal("Import directory '%s' already specified as '%s'\n", optarg, import_content);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			import_content = optarg;
			break;
		case 'l' :
			if (log_file) {
				/* LCOV_EXCL_START */
				log_fatal("Log file '%s' already specified as '%s'\n", optarg, log_file);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			log_file = optarg;
			break;
		case 'A' :
			opt.force_stats = 1;
			break;
		case 'Z' :
			opt.force_zero = 1;
			break;
		case 'E' :
			opt.force_empty = 1;
			break;
		case 'U' :
			opt.force_uuid = 1;
			break;
		case 'D' :
			opt.force_device = 1;
			break;
		case 'N' :
			opt.force_nocopy = 1;
			break;
		case 'F' :
			opt.force_full = 1;
			break;
		case 'R' :
			opt.force_realloc = 1;
			break;
		case 'a' :
			opt.auditonly = 1;
			break;
		case 'h' :
			opt.prehash = 1;
			break;
		case 'v' :
			++msg_level;
			break;
		case 'q' :
			--msg_level;
			break;
		case 'G' :
			opt.gui = 1;
			break;
		case 'H' :
			usage();
			exit(EXIT_SUCCESS);
		case 'V' :
			version();
			exit(EXIT_SUCCESS);
		case 'T' :
			speedtest = 1;
			break;
		case 'C' :
			gen_conf = optarg;
			break;
		case OPT_TEST_KILL_AFTER_SYNC :
			opt.kill_after_sync = 1;
			break;
		case OPT_TEST_EXPECT_UNRECOVERABLE :
			opt.expect_unrecoverable = 1;
			break;
		case OPT_TEST_EXPECT_RECOVERABLE :
			opt.expect_recoverable = 1;
			break;
		case OPT_TEST_SKIP_SELF :
			opt.skip_self = 1;
			break;
		case OPT_TEST_SKIP_SIGN :
			opt.skip_sign = 1;
			break;
		case OPT_TEST_SKIP_FALLOCATE :
			opt.skip_fallocate = 1;
			break;
		case OPT_TEST_SKIP_DEVICE :
			opt.skip_device = 1;
			period = 50; /* reduce period of the speed test */
			break;
		case OPT_TEST_SKIP_CONTENT_CHECK :
			opt.skip_content_check = 1;
			break;
		case OPT_TEST_SKIP_PARITY_ACCESS :
			opt.skip_parity_access = 1;
			break;
		case OPT_TEST_SKIP_DISK_ACCESS :
			opt.skip_disk_access = 1;
			break;
		case OPT_TEST_FORCE_MURMUR3 :
			opt.force_murmur3 = 1;
			break;
		case OPT_TEST_FORCE_SPOOKY2 :
			opt.force_spooky2 = 1;
			break;
		case OPT_TEST_SKIP_LOCK :
			opt.skip_lock = 1;
			break;
		case OPT_TEST_FORCE_ORDER_PHYSICAL :
			opt.force_order = SORT_PHYSICAL;
			break;
		case OPT_TEST_FORCE_ORDER_INODE :
			opt.force_order = SORT_INODE;
			break;
		case OPT_TEST_FORCE_ORDER_ALPHA :
			opt.force_order = SORT_ALPHA;
			break;
		case OPT_TEST_FORCE_ORDER_DIR :
			opt.force_order = SORT_DIR;
			break;
		case OPT_TEST_FORCE_SCRUB_AT :
			opt.force_scrub_at = atoi(optarg);
			break;
		case OPT_TEST_FORCE_SCRUB_EVEN :
			opt.force_scrub_even = 1;
			break;
		case OPT_TEST_FORCE_CONTENT_WRITE :
			opt.force_content_write = 1;
			break;
		case OPT_TEST_EXPECT_FAILURE :
			/* invert the exit codes */
			exit_success = 1;
			exit_failure = 0;
			break;
		case OPT_TEST_EXPECT_NEED_SYNC :
			/* invert the exit codes */
			exit_success = 1;
			exit_sync_needed = 0;
			break;
		case OPT_TEST_RUN :
			run = optarg;
			break;
		case OPT_TEST_FORCE_SCAN_WINFIND :
			opt.force_scan_winfind = 1;
			break;
		case OPT_TEST_FORCE_PROGRESS :
			opt.force_progress = 1;
			break;
		case OPT_TEST_FORCE_AUTOSAVE_AT :
			opt.force_autosave_at = atoi(optarg);
			break;
		case OPT_TEST_FAKE_DEVICE :
			opt.fake_device = 1;
			break;
		case OPT_NO_WARNINGS :
			opt.no_warnings = 1;
			break;
		case OPT_TEST_FAKE_UUID :
			opt.fake_uuid = 2;
			break;
		case OPT_TEST_MATCH_FIRST_UUID :
			opt.match_first_uuid = 1;
			break;
		case OPT_TEST_FORCE_PARITY_UPDATE :
			opt.force_parity_update = 1;
			break;
		case OPT_TEST_IO_CACHE :
			opt.io_cache = atoi(optarg);
			if (opt.io_cache != 1 && (opt.io_cache < IO_MIN || opt.io_cache > IO_MAX)) {
				/* LCOV_EXCL_START */
				log_fatal("The IO cache should be between %u and %u.\n", IO_MIN, IO_MAX);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case OPT_TEST_IO_STATS :
			opt.force_stats = 1;
			break;
		case OPT_TEST_COND_SIGNAL_OUTSIDE :
#if HAVE_THREAD
			thread_cond_signal_outside = 1;
#endif
			break;
		case OPT_TEST_IO_ADVISE_NONE :
			opt.file_mode = ADVISE_NONE;
			break;
		case OPT_TEST_IO_ADVISE_SEQUENTIAL :
			opt.file_mode = ADVISE_SEQUENTIAL;
			break;
		case OPT_TEST_IO_ADVISE_FLUSH :
			opt.file_mode = ADVISE_FLUSH;
			break;
		case OPT_TEST_IO_ADVISE_FLUSH_WINDOW :
			opt.file_mode = ADVISE_FLUSH_WINDOW;
			break;
		case OPT_TEST_IO_ADVISE_DISCARD :
			opt.file_mode = ADVISE_DISCARD;
			break;
		case OPT_TEST_IO_ADVISE_DISCARD_WINDOW :
			opt.file_mode = ADVISE_DISCARD_WINDOW;
			break;
		case OPT_TEST_IO_ADVISE_DIRECT :
			opt.file_mode = ADVISE_DIRECT;
			break;
		case OPT_TEST_PARITY_LIMIT :
			opt.parity_limit_size = atoll(optarg);
			break;
		case OPT_TEST_SKIP_CONTENT_WRITE :
			opt.skip_content_write = 1;
			break;
		case OPT_TEST_SKIP_SPACE_HOLDER :
			opt.skip_space_holder = 1;
			break;
		case OPT_TEST_FORMAT :
			if (strcmp(optarg, "file") == 0)
				FMT_MODE = FMT_FILE;
			else if (strcmp(optarg, "disk") == 0)
				FMT_MODE = FMT_DISK;
			else if (strcmp(optarg, "path") == 0)
				FMT_MODE = FMT_PATH;
			else {
				/* LCOV_EXCL_START */
				log_fatal("Unknown format '%s'\n", optarg);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
			break;
		case OPT_TEST_SKIP_MULTI_SCAN :
			opt.skip_multi_scan = 1;
			break;
		default :
			/* LCOV_EXCL_START */
			log_fatal("Unknown option '%c'\n", (char)c);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	os_init(opt.force_scan_winfind);
	raid_init();
	crc32c_init();

	if (speedtest != 0) {
		speed(period);
		os_done();
		exit(EXIT_SUCCESS);
	}

	if (gen_conf != 0) {
		generate_configuration(gen_conf);
		os_done();
		exit(EXIT_SUCCESS);
	}

	if (optind + 1 != argc) {
		/* LCOV_EXCL_START */
		usage();
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	command = argv[optind];
	if (strcmp(command, "diff") == 0) {
		operation = OPERATION_DIFF;
	} else if (strcmp(argv[optind], "sync") == 0) {
		operation = OPERATION_SYNC;
	} else if (strcmp(argv[optind], "check") == 0) {
		operation = OPERATION_CHECK;
	} else if (strcmp(argv[optind], "fix") == 0) {
		operation = OPERATION_FIX;
	} else if (strcmp(argv[optind], "test-dry") == 0) {
		operation = OPERATION_DRY;
	} else if (strcmp(argv[optind], "dup") == 0) {
		operation = OPERATION_DUP;
	} else if (strcmp(argv[optind], "list") == 0) {
		operation = OPERATION_LIST;
	} else if (strcmp(argv[optind], "pool") == 0) {
		operation = OPERATION_POOL;
	} else if (strcmp(argv[optind], "rehash") == 0) {
		operation = OPERATION_REHASH;
	} else if (strcmp(argv[optind], "scrub") == 0) {
		operation = OPERATION_SCRUB;
	} else if (strcmp(argv[optind], "status") == 0) {
		operation = OPERATION_STATUS;
	} else if (strcmp(argv[optind], "test-rewrite") == 0) {
		operation = OPERATION_REWRITE;
	} else if (strcmp(argv[optind], "test-read") == 0) {
		operation = OPERATION_READ;
	} else if (strcmp(argv[optind], "touch") == 0) {
		operation = OPERATION_TOUCH;
	} else if (strcmp(argv[optind], "up") == 0) {
		operation = OPERATION_SPINUP;
	} else if (strcmp(argv[optind], "down") == 0) {
		operation = OPERATION_SPINDOWN;
	} else if (strcmp(argv[optind], "devices") == 0) {
		operation = OPERATION_DEVICES;
	} else if (strcmp(argv[optind], "smart") == 0) {
		operation = OPERATION_SMART;
	} else if (strcmp(argv[optind], "probe") == 0) {
		operation = OPERATION_PROBE;
	} else {
		/* LCOV_EXCL_START */
		log_fatal("Unknown command '%s'\n", argv[optind]);
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	/* check options compatibility */
	switch (operation) {
	case OPERATION_CHECK :
		break;
	default :
		if (opt.auditonly) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -a, --audit-only with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	switch (operation) {
	case OPERATION_FIX :
	case OPERATION_CHECK :
	case OPERATION_SMART :
	case OPERATION_PROBE :
	case OPERATION_DEVICES :
	case OPERATION_SPINUP :
	case OPERATION_SPINDOWN :
		break;
	default :
		if (opt.force_device) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -D, --force-device with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	switch (operation) {
	case OPERATION_SYNC :
	case OPERATION_CHECK :
	case OPERATION_FIX :
		break;
	default :
		if (opt.force_nocopy) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -N, --force-nocopy with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	switch (operation) {
	case OPERATION_SYNC :
		break;
	default :
		if (opt.prehash) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -h, --pre-hash with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		if (opt.force_full) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -F, --force-full with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}

		if (opt.force_realloc) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -R, --force-realloc with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	if (opt.force_full && opt.force_nocopy) {
		/* LCOV_EXCL_START */
		log_fatal("You cannot use the -F, --force-full and -N, --force-nocopy options simultaneously\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	if (opt.force_realloc && opt.force_nocopy) {
		/* LCOV_EXCL_START */
		log_fatal("You cannot use the -R, --force-realloc and -N, --force-nocopy options simultaneously\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	if (opt.force_realloc && opt.force_full) {
		/* LCOV_EXCL_START */
		log_fatal("You cannot use the -R, --force-realloc and -F, --force-full options simultaneously\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	if (opt.prehash && opt.force_nocopy) {
		/* LCOV_EXCL_START */
		log_fatal("You cannot use the -h, --pre-hash and -N, --force-nocopy options simultaneously\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	switch (operation) {
	case OPERATION_CHECK :
	case OPERATION_FIX :
	case OPERATION_DRY :
		break;
	default :
		if (!tommy_list_empty(&filterlist_disk)) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -d, --filter-disk with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
		/* fallthrough */
	case OPERATION_SPINUP :
	case OPERATION_SPINDOWN :
		if (!tommy_list_empty(&filterlist_file)) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -f, --filter with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
		if (filter_missing != 0) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -m, --filter-missing with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
		if (filter_error != 0) {
			/* LCOV_EXCL_START */
			log_fatal("You cannot use -e, --filter-error with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	/* errors must be always fixed on all disks */
	/* because we don't keep the information on what disk is the error */
	if (filter_error != 0 && !tommy_list_empty(&filterlist_disk)) {
		/* LCOV_EXCL_START */
		log_fatal("You cannot use -e, --filter-error and -d, --filter-disk simultaneously\n");
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}

	switch (operation) {
	case OPERATION_CHECK :
	case OPERATION_FIX :
		break;
	default :
		if (import_timestamp != 0 || import_content != 0) {
			/* LCOV_EXCL_START */
			log_fatal("Import not allowed with the '%s' command\n", command);
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}

	switch (operation) {
	case OPERATION_LIST :
	case OPERATION_DUP :
	case OPERATION_STATUS :
	case OPERATION_REWRITE :
	case OPERATION_READ :
	case OPERATION_REHASH :
		/* avoid to check and access data disks if not needed */
		opt.skip_disk_access = 1;
		break;
	}

	switch (operation) {
	case OPERATION_DIFF :
	case OPERATION_LIST :
	case OPERATION_DUP :
	case OPERATION_POOL :
	case OPERATION_STATUS :
	case OPERATION_REWRITE :
	case OPERATION_READ :
	case OPERATION_REHASH :
	case OPERATION_TOUCH :
		/* avoid to check and access parity disks if not needed */
		opt.skip_parity_access = 1;
		break;
	}

	switch (operation) {
	case OPERATION_FIX :
	case OPERATION_CHECK :
		/* avoid to stop processing if a content file is not accessible */
		opt.skip_content_access = 1;
		break;
	}

	switch (operation) {
	case OPERATION_DIFF :
	case OPERATION_LIST :
	case OPERATION_DUP :
	case OPERATION_POOL :
	case OPERATION_TOUCH :
	case OPERATION_SPINUP :
	case OPERATION_SPINDOWN :
	case OPERATION_DEVICES :
	case OPERATION_SMART :
	case OPERATION_PROBE :
		opt.skip_self = 1;
		break;
	}

	switch (operation) {
#if HAVE_DIRECT_IO
	case OPERATION_SYNC :
	case OPERATION_SCRUB :
	case OPERATION_DRY :
		break;
#endif
	default :
		/* we allow direct IO only on some commands */
		if (opt.file_mode == ADVISE_DIRECT)
			opt.file_mode = ADVISE_SEQUENTIAL;
		break;
	}

	switch (operation) {
	case OPERATION_DEVICES :
	case OPERATION_SMART :
	case OPERATION_PROBE :
		/* we may need to use these commands during operations */
		opt.skip_lock = 1;
		break;
	}

	switch (operation) {
	case OPERATION_SMART :
	case OPERATION_PROBE :
		/* allow to run without configuration file */
		opt.auto_conf = 1;
		break;
	}

	/* open the log file */
	log_open(log_file);

	/* print generic info into the log */
	t = time(0);
#if HAVE_LOCALTIME_R
	tm = localtime_r(&t, &tm_res);
#else
	tm = localtime(&t);
#endif
	log_tag("version:%s\n", PACKAGE_VERSION);
	log_tag("unixtime:%" PRIi64 "\n", (int64_t)t);
	if (tm) {
		char datetime[64];
		strftime(datetime, sizeof(datetime), "%Y-%m-%d %H:%M:%S", tm);
		log_tag("time:%s\n", datetime);
	}
	log_tag("command:%s\n", command);
	for (i = 0; i < argc; ++i)
		log_tag("argv:%u:%s\n", i, argv[i]);
	log_flush();

	if (!opt.skip_self)
		selftest();

	state_init(&state);

	/* read the configuration file */
	state_config(&state, conf, command, &opt, &filterlist_disk);

	/* set the raid mode */
	raid_mode(state.raid_mode);

#if HAVE_LOCKFILE
	/* create the lock file */
	if (!opt.skip_lock && state.lockfile[0]) {
		lock = lock_lock(state.lockfile);
		if (lock == -1) {
			/* LCOV_EXCL_START */
			if (errno != EWOULDBLOCK) {
				log_fatal("Failed to create the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
			} else {
				log_fatal("The lock file '%s' is already in use!\n", state.lockfile);
				log_fatal("SnapRAID is already in use!\n");
			}
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}
#else
	(void)lock;
#endif

	ret = 0;
	if (operation == OPERATION_DIFF) {
		state_read(&state);

		ret = state_diff(&state);

		/* abort if sync needed */
		if (ret > 0)
			exit(EXIT_SYNC_NEEDED);
	} else if (operation == OPERATION_SYNC) {

		/* in the next state read ensures to clear all the past hashes in case */
		/* we are reading from an incomplete sync */
		/* The indeterminate hash are only for CHG/DELETED blocks for which we don't */
		/* know if the previous interrupted sync was able to update or not the parity. */
		/* The sync process instead needs to trust this information because it's used */
		/* to avoid to recompute the parity if all the input are equals as before. */

		/* In these cases we don't know if the old state is still the one */
		/* stored inside the parity, because after an aborted sync, the parity */
		/* may be or may be not have been updated with the data that may be now */
		/* deleted. Then we reset the hash to a bogus value. */

		/* An example for CHG blocks is: */
		/* - One file is added creating a CHG block with ZERO state */
		/* - Sync aborted after updating the parity to the new state, */
		/*   but without saving the content file representing this new BLK state. */
		/* - File is now deleted after the aborted sync */
		/* - Sync again, deleting the blocks over the CHG ones */
		/*   with the hash of CHG blocks not representing the real parity state */

		/* An example for DELETED blocks is: */
		/* - One file is deleted creating DELETED blocks */
		/* - Sync aborted after, updating the parity to the new state, */
		/*   but without saving the content file representing this new EMPTY state. */
		/* - Another file is added again over the DELETE ones */
		/*   with the hash of DELETED blocks not representing the real parity state */
		state.clear_past_hash = 1;

		state_read(&state);

		state_scan(&state);

		/* refresh the size info before the content write */
		state_refresh(&state);

		memory();

		/* intercept signals while operating */
		signal_init();

		/* run a test command if required */
		if (run != 0) {
			ret = system(run); /* ignore error */
			if (ret != 0) {
				/* LCOV_EXCL_START */
				log_fatal("Error executing command '%s'.\n", run);
				exit(EXIT_FAILURE);
				/* LCOV_EXCL_STOP */
			}
		}

		/* waits some time to ensure that any concurrent modification done at the files, */
		/* using the same mtime read by the scan process, will be read by sync. */
		/* Note that any later modification done, potentially not read by sync, will have */
		/* a different mtime, and it will be synchronized at the next sync. */
		/* The worst case is the FAT file-system with a two seconds resolution for mtime. */
		/* If you don't use FAT, the wait is not needed, because most file-systems have now */
		/* at least microseconds resolution, but better to be safe. */
		if (!opt.skip_self)
			sleep(2);

		ret = state_sync(&state, blockstart, blockcount);
	} else if (operation == OPERATION_DRY) {
		state_read(&state);

		/* filter */
		state_skip(&state);
		state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);

		memory();

		/* intercept signals while operating */
		signal_init();

		ret = state_dry(&state, blockstart, blockcount);
	} else if (operation == OPERATION_REHASH) {
		state_read(&state);

		/* intercept signals while operating */
		signal_init();

		state_rehash(&state);

		/* save the new state if required */
		if (state.need_write)
			state_write(&state);
	} else if (operation == OPERATION_SCRUB) {
		state_read(&state);

		memory();

		/* intercept signals while operating */
		signal_init();

		ret = state_scrub(&state, plan, olderthan);
	} else if (operation == OPERATION_REWRITE) {
		state_read(&state);

		/* intercept signals while operating */
		signal_init();

		state_write(&state);

		memory();
	} else if (operation == OPERATION_READ) {
		state_read(&state);

		memory();
	} else if (operation == OPERATION_TOUCH) {
		state_read(&state);

		memory();

		state_touch(&state);

		/* intercept signals while operating */
		signal_init();

		state_write(&state);
	} else if (operation == OPERATION_SPINUP) {
		state_device(&state, DEVICE_UP, &filterlist_disk);
	} else if (operation == OPERATION_SPINDOWN) {
		state_device(&state, DEVICE_DOWN, &filterlist_disk);
	} else if (operation == OPERATION_DEVICES) {
		state_device(&state, DEVICE_LIST, 0);
	} else if (operation == OPERATION_SMART) {
		state_device(&state, DEVICE_SMART, 0);
	} else if (operation == OPERATION_PROBE) {
		state_device(&state, DEVICE_PROBE, 0);
	} else if (operation == OPERATION_STATUS) {
		state_read(&state);

		memory();

		state_status(&state);
	} else if (operation == OPERATION_DUP) {
		state_read(&state);

		state_dup(&state);
	} else if (operation == OPERATION_LIST) {
		state_read(&state);

		state_list(&state);
	} else if (operation == OPERATION_POOL) {
		state_read(&state);

		state_pool(&state);
	} else {
		state_read(&state);

		/* if we are also trying to recover */
		if (!state.opt.auditonly) {
			/* import the user specified dirs */
			if (import_timestamp != 0)
				state_search(&state, import_timestamp);
			if (import_content != 0)
				state_import(&state, import_content);

			/* import from all the array */
			if (!state.opt.force_nocopy)
				state_search_array(&state);
		}

		/* filter */
		state_skip(&state);
		state_filter(&state, &filterlist_file, &filterlist_disk, filter_missing, filter_error);

		memory();

		/* intercept signals while operating */
		signal_init();

		if (operation == OPERATION_CHECK) {
			ret = state_check(&state, 0, blockstart, blockcount);
		} else { /* it's fix */
			ret = state_check(&state, 1, blockstart, blockcount);
		}
	}

	/* close log file */
	log_close(log_file);

#if HAVE_LOCKFILE
	if (!opt.skip_lock && state.lockfile[0]) {
		if (lock_unlock(lock) == -1) {
			/* LCOV_EXCL_START */
			log_fatal("Failed to close the lock file '%s'. %s.\n", state.lockfile, strerror(errno));
			exit(EXIT_FAILURE);
			/* LCOV_EXCL_STOP */
		}
	}
#endif

	state_done(&state);
	tommy_list_foreach(&filterlist_file, (tommy_foreach_func*)filter_free);
	tommy_list_foreach(&filterlist_disk, (tommy_foreach_func*)filter_free);

	os_done();
	lock_done();

	/* abort if required */
	if (ret != 0) {
		/* LCOV_EXCL_START */
		exit(EXIT_FAILURE);
		/* LCOV_EXCL_STOP */
	}
	if (global_interrupt) {
		/* LCOV_EXCL_START */
#ifdef _WIN32
		exit(STATUS_CONTROL_C_EXIT);
#else
		exit(128 + SIGINT);
#endif
		/* LCOV_EXCL_STOP */
	}

	return EXIT_SUCCESS;
}