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