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