1 | /* libmondo-files.c file manipulation |
---|
2 | $Id: libmondo-files.c 1098 2007-02-03 22:48:53Z bruno $ |
---|
3 | */ |
---|
4 | |
---|
5 | /** |
---|
6 | * @file |
---|
7 | * Functions to manipulate files. |
---|
8 | */ |
---|
9 | |
---|
10 | |
---|
11 | #include "my-stuff.h" |
---|
12 | #include "mondostructures.h" |
---|
13 | #include "libmondo-files.h" |
---|
14 | |
---|
15 | #include "lib-common-externs.h" |
---|
16 | |
---|
17 | #include "libmondo-tools-EXT.h" |
---|
18 | #include "libmondo-gui-EXT.h" |
---|
19 | #include "libmondo-devices-EXT.h" |
---|
20 | #include "libmondo-fork-EXT.h" |
---|
21 | #include "libmondo-string-EXT.h" |
---|
22 | |
---|
23 | #include "mr_file.h" |
---|
24 | |
---|
25 | /*@unused@*/ |
---|
26 | //static char cvsid[] = "$Id: libmondo-files.c 1098 2007-02-03 22:48:53Z bruno $"; |
---|
27 | |
---|
28 | extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN]; |
---|
29 | |
---|
30 | extern int g_currentY; |
---|
31 | extern char *g_mondo_home; |
---|
32 | |
---|
33 | /** |
---|
34 | * @addtogroup fileGroup |
---|
35 | * @{ |
---|
36 | */ |
---|
37 | /** |
---|
38 | * Get an md5 checksum of the specified file. |
---|
39 | * @param filename The file to checksum. |
---|
40 | * @return The 32-character ASCII representation of the 128-bit checksum. |
---|
41 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
42 | */ |
---|
43 | char *calc_checksum_of_file(char *filename) |
---|
44 | { |
---|
45 | /*@ buffers ***************************************************** */ |
---|
46 | static char output[MAX_STR_LEN]; |
---|
47 | char command[MAX_STR_LEN * 2]; |
---|
48 | char tmp[MAX_STR_LEN]; |
---|
49 | |
---|
50 | /*@ pointers **************************************************** */ |
---|
51 | char *p; |
---|
52 | FILE *fin; |
---|
53 | |
---|
54 | /*@ initialize pointers ***************************************** */ |
---|
55 | |
---|
56 | p = output; |
---|
57 | |
---|
58 | /*@************************************************************** */ |
---|
59 | |
---|
60 | assert_string_is_neither_NULL_nor_zerolength(filename); |
---|
61 | if (does_file_exist(filename)) { |
---|
62 | sprintf(command, "md5sum \"%s\"", filename); |
---|
63 | fin = popen(command, "r"); |
---|
64 | if (fin) { |
---|
65 | (void) fgets(output, MAX_STR_LEN, fin); |
---|
66 | p = strchr(output, ' '); |
---|
67 | paranoid_pclose(fin); |
---|
68 | } |
---|
69 | } else { |
---|
70 | sprintf(tmp, "File '%s' not found; cannot calc checksum", |
---|
71 | filename); |
---|
72 | log_it(tmp); |
---|
73 | } |
---|
74 | if (p) { |
---|
75 | *p = '\0'; |
---|
76 | } |
---|
77 | return (output); |
---|
78 | } |
---|
79 | |
---|
80 | |
---|
81 | /** |
---|
82 | * Get a not-quite-unique representation of some of the file's @c stat properties. |
---|
83 | * The returned string has the form <tt>size-mtime-ctime</tt>. |
---|
84 | * @param curr_fname The file to generate the "checksum" for. |
---|
85 | * @return The "checksum". |
---|
86 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
87 | */ |
---|
88 | char *calc_file_ugly_minichecksum(char *curr_fname) |
---|
89 | { |
---|
90 | |
---|
91 | /*@ buffers ***************************************************** */ |
---|
92 | static char curr_cksum[1000]; |
---|
93 | |
---|
94 | /*@ pointers **************************************************** */ |
---|
95 | |
---|
96 | /*@ structures ************************************************** */ |
---|
97 | struct stat buf; |
---|
98 | |
---|
99 | /*@ initialize data *************************************************** */ |
---|
100 | curr_cksum[0] = '\0'; |
---|
101 | |
---|
102 | /*@************************************************************** */ |
---|
103 | |
---|
104 | assert_string_is_neither_NULL_nor_zerolength(curr_fname); |
---|
105 | if (lstat(curr_fname, &buf)) { |
---|
106 | return (curr_cksum); // empty |
---|
107 | } |
---|
108 | |
---|
109 | sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size), |
---|
110 | (long) (buf.st_mtime), (long) (buf.st_ctime)); |
---|
111 | return (curr_cksum); |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | /** |
---|
117 | * Get the number of lines in @p filename. |
---|
118 | * @param filename The file to count lines in. |
---|
119 | * @return The number of lines in @p filename. |
---|
120 | * @bug This function uses the shell and "wc -l"; it should probably be rewritten in C. |
---|
121 | */ |
---|
122 | long count_lines_in_file(char *filename) |
---|
123 | { |
---|
124 | |
---|
125 | /*@ buffers ***************************************************** */ |
---|
126 | char command[MAX_STR_LEN * 2]; |
---|
127 | char incoming[MAX_STR_LEN]; |
---|
128 | char tmp[MAX_STR_LEN]; |
---|
129 | |
---|
130 | /*@ long ******************************************************** */ |
---|
131 | long noof_lines = -1L; |
---|
132 | |
---|
133 | /*@ pointers **************************************************** */ |
---|
134 | FILE *fin; |
---|
135 | |
---|
136 | /*@ initialize [0] to null ******************************************** */ |
---|
137 | incoming[0] = '\0'; |
---|
138 | |
---|
139 | assert_string_is_neither_NULL_nor_zerolength(filename); |
---|
140 | if (!does_file_exist(filename)) { |
---|
141 | sprintf(tmp, |
---|
142 | "%s does not exist, so I cannot found the number of lines in it", |
---|
143 | filename); |
---|
144 | log_it(tmp); |
---|
145 | return (0); |
---|
146 | } |
---|
147 | sprintf(command, "cat %s | wc -l", filename); |
---|
148 | if (!does_file_exist(filename)) { |
---|
149 | return (-1); |
---|
150 | } |
---|
151 | fin = popen(command, "r"); |
---|
152 | if (fin) { |
---|
153 | if (feof(fin)) { |
---|
154 | noof_lines = 0; |
---|
155 | } else { |
---|
156 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
157 | while (strlen(incoming) > 0 |
---|
158 | && incoming[strlen(incoming) - 1] < 32) { |
---|
159 | incoming[strlen(incoming) - 1] = '\0'; |
---|
160 | } |
---|
161 | noof_lines = atol(incoming); |
---|
162 | } |
---|
163 | paranoid_pclose(fin); |
---|
164 | } |
---|
165 | return (noof_lines); |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | /** |
---|
170 | * Check for existence of given @p filename. |
---|
171 | * @param filename The file to check for. |
---|
172 | * @return TRUE if it exists, FALSE otherwise. |
---|
173 | */ |
---|
174 | bool does_file_exist(char *filename) |
---|
175 | { |
---|
176 | |
---|
177 | /*@ structures ************************************************** */ |
---|
178 | struct stat buf; |
---|
179 | |
---|
180 | /*@************************************************************** */ |
---|
181 | |
---|
182 | assert(filename != NULL); |
---|
183 | // assert_string_is_neither_NULL_nor_zerolength(filename); |
---|
184 | if (lstat(filename, &buf)) { |
---|
185 | log_msg(20, "%s does not exist", filename); |
---|
186 | return (FALSE); |
---|
187 | } else { |
---|
188 | log_msg(20, "%s exists", filename); |
---|
189 | return (TRUE); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | /** |
---|
199 | * Modify @p inout (a file containing a list of files) to only contain files |
---|
200 | * that exist. |
---|
201 | * @param inout The filelist to operate on. |
---|
202 | * @note The original file is renamed beforehand, so it will not be accessible |
---|
203 | * while the modification is in progress. |
---|
204 | */ |
---|
205 | void exclude_nonexistent_files(char *inout) |
---|
206 | { |
---|
207 | char infname[MAX_STR_LEN]; |
---|
208 | char outfname[MAX_STR_LEN]; |
---|
209 | char tmp[MAX_STR_LEN]; |
---|
210 | char incoming[MAX_STR_LEN]; |
---|
211 | |
---|
212 | /*@ int ********************************************************* */ |
---|
213 | int i; |
---|
214 | |
---|
215 | /*@ pointers **************************************************** */ |
---|
216 | FILE *fin, *fout; |
---|
217 | |
---|
218 | |
---|
219 | /*@ end vars *********************************************************** */ |
---|
220 | |
---|
221 | assert_string_is_neither_NULL_nor_zerolength(inout); |
---|
222 | sprintf(infname, "%s.in", inout); |
---|
223 | sprintf(outfname, "%s", inout); |
---|
224 | sprintf(tmp, "cp -f %s %s", inout, infname); |
---|
225 | run_program_and_log_output(tmp, FALSE); |
---|
226 | if (!(fin = fopen(infname, "r"))) { |
---|
227 | log_OS_error("Unable to openin infname"); |
---|
228 | return; |
---|
229 | } |
---|
230 | if (!(fout = fopen(outfname, "w"))) { |
---|
231 | log_OS_error("Unable to openout outfname"); |
---|
232 | return; |
---|
233 | } |
---|
234 | for (fgets(incoming, MAX_STR_LEN, fin); !feof(fin); |
---|
235 | fgets(incoming, MAX_STR_LEN, fin)) { |
---|
236 | i = strlen(incoming) - 1; |
---|
237 | if (i >= 0 && incoming[i] < 32) { |
---|
238 | incoming[i] = '\0'; |
---|
239 | } |
---|
240 | if (does_file_exist(incoming)) { |
---|
241 | fprintf(fout, "%s\n", incoming); |
---|
242 | } else { |
---|
243 | sprintf(tmp, "Excluding '%s'-nonexistent\n", incoming); |
---|
244 | log_it(tmp); |
---|
245 | } |
---|
246 | } |
---|
247 | paranoid_fclose(fout); |
---|
248 | paranoid_fclose(fin); |
---|
249 | unlink(infname); |
---|
250 | } |
---|
251 | |
---|
252 | |
---|
253 | |
---|
254 | |
---|
255 | |
---|
256 | |
---|
257 | |
---|
258 | |
---|
259 | |
---|
260 | /** |
---|
261 | * Attempt to find the user's kernel by calling Mindi. |
---|
262 | * If Mindi can't find the kernel, ask user. If @p kernel is not empty, |
---|
263 | * don't do anything. |
---|
264 | * @param kernel Where to put the found kernel. |
---|
265 | * @return 0 for success, 1 for failure. |
---|
266 | */ |
---|
267 | int figure_out_kernel_path_interactively_if_necessary(char *kernel) |
---|
268 | { |
---|
269 | char tmp[MAX_STR_LEN]; |
---|
270 | char *command; |
---|
271 | |
---|
272 | if (!kernel[0]) { |
---|
273 | strcpy(kernel, |
---|
274 | call_program_and_get_last_line_of_output |
---|
275 | ("mindi --findkernel 2> /dev/null")); |
---|
276 | } |
---|
277 | // If we didn't get anything back, check whether mindi raised a fatal error |
---|
278 | if (!kernel[0]) { |
---|
279 | malloc_string(command); |
---|
280 | strcpy(command, "grep 'Fatal error' /var/log/mindi.log"); |
---|
281 | strcpy(tmp, call_program_and_get_last_line_of_output(command)); |
---|
282 | if (strlen(tmp) > 1) { |
---|
283 | popup_and_OK(tmp); |
---|
284 | fatal_error("Mindi gave a fatal error. Please check '/var/log/mindi.log'."); |
---|
285 | } |
---|
286 | mr_free(command); |
---|
287 | } |
---|
288 | log_it("Calling Mindi with kernel path of '%s'", kernel); |
---|
289 | while (!kernel[0]) { |
---|
290 | if (!ask_me_yes_or_no |
---|
291 | ("Kernel not found or invalid. Choose another?")) { |
---|
292 | return (1); |
---|
293 | } |
---|
294 | if (!popup_and_get_string |
---|
295 | ("Kernel path", |
---|
296 | "What is the full path and filename of your kernel, please?", |
---|
297 | kernel, MAX_STR_LEN / 4)) { |
---|
298 | fatal_error |
---|
299 | ("Kernel not found. Please specify with the '-k' flag."); |
---|
300 | } |
---|
301 | sprintf(tmp, "User says kernel is at %s", kernel); |
---|
302 | log_it(tmp); |
---|
303 | } |
---|
304 | return (0); |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | |
---|
309 | |
---|
310 | |
---|
311 | |
---|
312 | /** |
---|
313 | * Find location of specified executable in user's PATH. |
---|
314 | * @param fname The basename of the executable to search for (e.g. @c afio). |
---|
315 | * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found. |
---|
316 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
317 | * @bug The checks with @c file and @c dirname seem pointless. If @c incoming is "", then you're calling |
---|
318 | * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts |
---|
319 | * to nothing. |
---|
320 | */ |
---|
321 | char *find_home_of_exe(char *fname) |
---|
322 | { |
---|
323 | /*@ buffers ********************* */ |
---|
324 | static char output[MAX_STR_LEN]; |
---|
325 | char *incoming; |
---|
326 | char *command; |
---|
327 | |
---|
328 | malloc_string(incoming); |
---|
329 | malloc_string(command); |
---|
330 | incoming[0] = '\0'; |
---|
331 | /*@******************************* */ |
---|
332 | |
---|
333 | assert_string_is_neither_NULL_nor_zerolength(fname); |
---|
334 | sprintf(command, "which %s 2> /dev/null", fname); |
---|
335 | strcpy(incoming, call_program_and_get_last_line_of_output(command)); |
---|
336 | if (incoming[0] == '\0') { |
---|
337 | if (system("which file > /dev/null 2> /dev/null")) { |
---|
338 | mr_free(incoming); |
---|
339 | mr_free(command); |
---|
340 | output[0] = '\0'; |
---|
341 | return (NULL); // forget it :) |
---|
342 | } |
---|
343 | sprintf(command, |
---|
344 | "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", |
---|
345 | incoming); |
---|
346 | strcpy(incoming, |
---|
347 | call_program_and_get_last_line_of_output(command)); |
---|
348 | } |
---|
349 | if (incoming[0] == '\0') // yes, it is == '\0' twice, not once :) |
---|
350 | { |
---|
351 | sprintf(command, "dirname %s 2> /dev/null", incoming); |
---|
352 | strcpy(incoming, |
---|
353 | call_program_and_get_last_line_of_output(command)); |
---|
354 | } |
---|
355 | strcpy(output, incoming); |
---|
356 | if (output[0] != '\0' && does_file_exist(output)) { |
---|
357 | log_msg(4, "find_home_of_exe () --- Found %s at %s", fname, |
---|
358 | incoming); |
---|
359 | } else { |
---|
360 | output[0] = '\0'; |
---|
361 | log_msg(4, "find_home_of_exe() --- Could not find %s", fname); |
---|
362 | } |
---|
363 | mr_free(incoming); |
---|
364 | mr_free(command); |
---|
365 | if (!output[0]) { |
---|
366 | return (NULL); |
---|
367 | } else { |
---|
368 | return (output); |
---|
369 | } |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | |
---|
374 | |
---|
375 | |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | /** |
---|
380 | * Get the last sequence of digits surrounded by non-digits in the first 32k of |
---|
381 | * a file. |
---|
382 | * @param logfile The file to look in. |
---|
383 | * @return The number found, or 0 if none. |
---|
384 | */ |
---|
385 | int get_trackno_from_logfile(char *logfile) |
---|
386 | { |
---|
387 | |
---|
388 | /*@ pointers ********************************************************* */ |
---|
389 | FILE *fin; |
---|
390 | |
---|
391 | /*@ int ************************************************************** */ |
---|
392 | int trackno = 0; |
---|
393 | size_t len = 0; |
---|
394 | |
---|
395 | /*@ buffer ************************************************************ */ |
---|
396 | char datablock[32701]; |
---|
397 | |
---|
398 | assert_string_is_neither_NULL_nor_zerolength(logfile); |
---|
399 | if (!(fin = fopen(logfile, "r"))) { |
---|
400 | log_OS_error("Unable to open logfile"); |
---|
401 | fatal_error("Unable to open logfile to read trackno"); |
---|
402 | } |
---|
403 | len = fread(datablock, 1, 32700, fin); |
---|
404 | paranoid_fclose(fin); |
---|
405 | if (len <= 0) { |
---|
406 | return (0); |
---|
407 | } |
---|
408 | for (; len > 0 && !isdigit(datablock[len - 1]); len--); |
---|
409 | datablock[len--] = '\0'; |
---|
410 | for (; len > 0 && isdigit(datablock[len - 1]); len--); |
---|
411 | trackno = atoi(datablock + len); |
---|
412 | /* |
---|
413 | sprintf(tmp,"datablock=%s; trackno=%d",datablock+len, trackno); |
---|
414 | log_it(tmp); |
---|
415 | */ |
---|
416 | return (trackno); |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | |
---|
425 | /** |
---|
426 | * Get a percentage from the last line of @p filename. We look for the string |
---|
427 | * "% done" on the last line and, if we find it, grab the number before the last % sign. |
---|
428 | * @param filename The file to get the percentage from. |
---|
429 | * @return The percentage found, or 0 for error. |
---|
430 | */ |
---|
431 | int grab_percentage_from_last_line_of_file(char *filename) |
---|
432 | { |
---|
433 | |
---|
434 | /*@ buffers ***************************************************** */ |
---|
435 | char tmp[MAX_STR_LEN]; |
---|
436 | char lastline[MAX_STR_LEN]; |
---|
437 | char command[MAX_STR_LEN]; |
---|
438 | /*@ pointers **************************************************** */ |
---|
439 | char *p; |
---|
440 | |
---|
441 | /*@ int's ******************************************************* */ |
---|
442 | int i; |
---|
443 | |
---|
444 | for (i = NOOF_ERR_LINES - 1; |
---|
445 | i >= 0 && !strstr(err_log_lines[i], "% Done") |
---|
446 | && !strstr(err_log_lines[i], "% done"); i--); |
---|
447 | if (i < 0) { |
---|
448 | sprintf(command, |
---|
449 | "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'", |
---|
450 | filename, '%'); |
---|
451 | strcpy(lastline, |
---|
452 | call_program_and_get_last_line_of_output(command)); |
---|
453 | if (!lastline[0]) { |
---|
454 | return (0); |
---|
455 | } |
---|
456 | } else { |
---|
457 | strcpy(lastline, err_log_lines[i]); |
---|
458 | } |
---|
459 | |
---|
460 | p = strrchr(lastline, '%'); |
---|
461 | if (p) { |
---|
462 | *p = '\0'; |
---|
463 | } |
---|
464 | // log_msg(2, "lastline='%s', ", p, lastline); |
---|
465 | if (!p) { |
---|
466 | return (0); |
---|
467 | } |
---|
468 | *p = '\0'; |
---|
469 | for (p--; *p != ' ' && p != lastline; p--); |
---|
470 | if (p != lastline) { |
---|
471 | p++; |
---|
472 | } |
---|
473 | i = atoi(p); |
---|
474 | |
---|
475 | sprintf(tmp, "'%s' --> %d", p, i); |
---|
476 | // log_to_screen(tmp); |
---|
477 | |
---|
478 | return (i); |
---|
479 | } |
---|
480 | |
---|
481 | |
---|
482 | |
---|
483 | |
---|
484 | |
---|
485 | /** |
---|
486 | * Return the last line of @p filename. |
---|
487 | * @param filename The file to get the last line of. |
---|
488 | * @return The last line of the file. |
---|
489 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
490 | */ |
---|
491 | char *last_line_of_file(char *filename) |
---|
492 | { |
---|
493 | /*@ buffers ***************************************************** */ |
---|
494 | static char output[MAX_STR_LEN]; |
---|
495 | static char command[MAX_STR_LEN * 2]; |
---|
496 | static char tmp[MAX_STR_LEN]; |
---|
497 | |
---|
498 | /*@ pointers **************************************************** */ |
---|
499 | FILE *fin; |
---|
500 | |
---|
501 | /*@ end vars **************************************************** */ |
---|
502 | |
---|
503 | if (!does_file_exist(filename)) { |
---|
504 | sprintf(tmp, "Tring to get last line of nonexistent file (%s)", |
---|
505 | filename); |
---|
506 | log_it(tmp); |
---|
507 | output[0] = '\0'; |
---|
508 | return (output); |
---|
509 | } |
---|
510 | sprintf(command, "tail -n1 %s", filename); |
---|
511 | fin = popen(command, "r"); |
---|
512 | (void) fgets(output, MAX_STR_LEN, fin); |
---|
513 | paranoid_pclose(fin); |
---|
514 | while (strlen(output) > 0 && output[strlen(output) - 1] < 32) { |
---|
515 | output[strlen(output) - 1] = '\0'; |
---|
516 | } |
---|
517 | return (output); |
---|
518 | } |
---|
519 | |
---|
520 | /** |
---|
521 | * Get the length of @p filename in bytes. |
---|
522 | * @param filename The file to get the length of. |
---|
523 | * @return The length of the file, or -1 for error. |
---|
524 | */ |
---|
525 | off_t length_of_file(char *filename) |
---|
526 | { |
---|
527 | /*@ pointers *************************************************** */ |
---|
528 | FILE *fin; |
---|
529 | |
---|
530 | /*@ long long ************************************************* */ |
---|
531 | off_t length; |
---|
532 | |
---|
533 | fin = fopen(filename, "r"); |
---|
534 | if (!fin) { |
---|
535 | log_it("filename=%s", filename); |
---|
536 | log_OS_error("Unable to openin filename"); |
---|
537 | return (-1); |
---|
538 | } |
---|
539 | fseeko(fin, 0, SEEK_END); |
---|
540 | length = ftello(fin); |
---|
541 | paranoid_fclose(fin); |
---|
542 | return (length); |
---|
543 | } |
---|
544 | |
---|
545 | |
---|
546 | |
---|
547 | /** |
---|
548 | * ????? |
---|
549 | * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed. |
---|
550 | */ |
---|
551 | int |
---|
552 | make_checksum_list_file(char *filelist, char *cksumlist, char *comppath) |
---|
553 | { |
---|
554 | /*@ pointers **************************************************** */ |
---|
555 | FILE *fin; |
---|
556 | FILE *fout; |
---|
557 | |
---|
558 | /*@ int ******************************************************* */ |
---|
559 | int percentage; |
---|
560 | int i; |
---|
561 | int counter = 0; |
---|
562 | |
---|
563 | /*@ buffer ****************************************************** */ |
---|
564 | char stub_fname[1000]; |
---|
565 | char curr_fname[1000]; |
---|
566 | char curr_cksum[1000]; |
---|
567 | char tmp[1000]; |
---|
568 | |
---|
569 | /*@ long [long] ************************************************* */ |
---|
570 | off_t filelist_length; |
---|
571 | off_t curr_pos; |
---|
572 | long start_time; |
---|
573 | long current_time; |
---|
574 | long time_taken; |
---|
575 | long time_remaining; |
---|
576 | |
---|
577 | /*@ end vars *************************************************** */ |
---|
578 | |
---|
579 | start_time = get_time(); |
---|
580 | filelist_length = length_of_file(filelist); |
---|
581 | sprintf(tmp, "filelist = %s; cksumlist = %s", filelist, cksumlist); |
---|
582 | log_it(tmp); |
---|
583 | fin = fopen(filelist, "r"); |
---|
584 | if (fin == NULL) { |
---|
585 | log_OS_error("Unable to fopen-in filelist"); |
---|
586 | log_to_screen("Can't open filelist"); |
---|
587 | return (1); |
---|
588 | } |
---|
589 | fout = fopen(cksumlist, "w"); |
---|
590 | if (fout == NULL) { |
---|
591 | log_OS_error("Unable to openout cksumlist"); |
---|
592 | paranoid_fclose(fin); |
---|
593 | log_to_screen("Can't open checksum list"); |
---|
594 | return (1); |
---|
595 | } |
---|
596 | for (fgets(stub_fname, 999, fin); !feof(fin); |
---|
597 | fgets(stub_fname, 999, fin)) { |
---|
598 | if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) { |
---|
599 | stub_fname[i] = '\0'; |
---|
600 | } |
---|
601 | sprintf(tmp, "%s%s", comppath, stub_fname); |
---|
602 | strcpy(curr_fname, tmp + 1); |
---|
603 | strcpy(curr_cksum, calc_file_ugly_minichecksum(curr_fname)); |
---|
604 | fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum); |
---|
605 | if (counter++ > 12) { |
---|
606 | current_time = get_time(); |
---|
607 | counter = 0; |
---|
608 | curr_fname[37] = '\0'; |
---|
609 | curr_pos = ftello(fin) / 1024; |
---|
610 | percentage = (int) (curr_pos * 100 / filelist_length); |
---|
611 | time_taken = current_time - start_time; |
---|
612 | if (percentage == 0) { |
---|
613 | /* printf("%0d%% done \r",percentage); */ |
---|
614 | } else { |
---|
615 | time_remaining = |
---|
616 | time_taken * 100 / (long) (percentage) - time_taken; |
---|
617 | sprintf(tmp, |
---|
618 | "%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r", |
---|
619 | percentage, (int) (time_taken / 60), |
---|
620 | (int) (time_taken % 60), |
---|
621 | (int) (time_remaining / 60), |
---|
622 | (int) (time_remaining % 60), curr_fname); |
---|
623 | log_to_screen(tmp); |
---|
624 | } |
---|
625 | sync(); |
---|
626 | } |
---|
627 | } |
---|
628 | paranoid_fclose(fout); |
---|
629 | paranoid_fclose(fin); |
---|
630 | log_it("Done."); |
---|
631 | return (0); |
---|
632 | } |
---|
633 | |
---|
634 | |
---|
635 | /** |
---|
636 | * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>. |
---|
637 | * @param outdir_fname The directory to create. |
---|
638 | * @return The return value of @c mkdir. |
---|
639 | */ |
---|
640 | int make_hole_for_dir(char *outdir_fname) |
---|
641 | { |
---|
642 | char tmp[MAX_STR_LEN * 2]; |
---|
643 | int res = 0; |
---|
644 | |
---|
645 | assert_string_is_neither_NULL_nor_zerolength(outdir_fname); |
---|
646 | sprintf(tmp, "mkdir -p %s", outdir_fname); |
---|
647 | res = system(tmp); |
---|
648 | return (res); |
---|
649 | } |
---|
650 | |
---|
651 | |
---|
652 | /** |
---|
653 | * Create the parent directories of @p outfile_fname. |
---|
654 | * @param outfile_fname The file to make a "hole" for. |
---|
655 | * @return 0, always. |
---|
656 | * @bug Return value unnecessary. |
---|
657 | */ |
---|
658 | int make_hole_for_file(char *outfile_fname) |
---|
659 | { |
---|
660 | /*@ buffer ****************************************************** */ |
---|
661 | char command[MAX_STR_LEN * 2]; |
---|
662 | |
---|
663 | /*@ int ******************************************************** */ |
---|
664 | int res = 0; |
---|
665 | |
---|
666 | /*@ end vars *************************************************** */ |
---|
667 | |
---|
668 | assert_string_is_neither_NULL_nor_zerolength(outfile_fname); |
---|
669 | assert(!strstr(outfile_fname, MNT_CDROM)); |
---|
670 | assert(!strstr(outfile_fname, "/dev/cdrom")); |
---|
671 | sprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname); |
---|
672 | res += system(command); |
---|
673 | sprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname); |
---|
674 | res += system(command); |
---|
675 | sprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname); |
---|
676 | res += system(command); |
---|
677 | unlink(outfile_fname); |
---|
678 | return (0); |
---|
679 | } |
---|
680 | |
---|
681 | |
---|
682 | |
---|
683 | |
---|
684 | /** |
---|
685 | * Get the number of lines in @p filelist_fname that contain the string @p wildcard. |
---|
686 | * @param filelist_fname The file to search through. |
---|
687 | * @param wildcard The string to search for. This is @e not a shell glob or a regular expression. |
---|
688 | * @return The number of lines matched. |
---|
689 | */ |
---|
690 | long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard) |
---|
691 | { |
---|
692 | /*@ long ******************************************************* */ |
---|
693 | long matches = 0; |
---|
694 | |
---|
695 | /*@ pointers *************************************************** */ |
---|
696 | FILE *fin; |
---|
697 | |
---|
698 | /*@ buffers **************************************************** */ |
---|
699 | char incoming[MAX_STR_LEN]; |
---|
700 | |
---|
701 | /*@ end vars *************************************************** */ |
---|
702 | |
---|
703 | |
---|
704 | fin = fopen(filelist_fname, "r"); |
---|
705 | |
---|
706 | if (!fin) { |
---|
707 | log_OS_error("Unable to openin filelist_fname"); |
---|
708 | return (0); |
---|
709 | } |
---|
710 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
711 | while (!feof(fin)) { |
---|
712 | if (strstr(incoming, wildcard)) { |
---|
713 | matches++; |
---|
714 | } |
---|
715 | (void) fgets(incoming, MAX_STR_LEN - 1, fin); |
---|
716 | } |
---|
717 | paranoid_fclose(fin); |
---|
718 | return (matches); |
---|
719 | } |
---|
720 | |
---|
721 | |
---|
722 | |
---|
723 | |
---|
724 | /** |
---|
725 | * Register our PID in a file in /var/run. |
---|
726 | * The PID will be put in /var/run/monitas-<tt>name_str</tt>.pid. |
---|
727 | * @param pid 0 to remove file, anything else to create it. |
---|
728 | * @param name_str The basename of the PID file (e.g. "mondo" or "server") |
---|
729 | * @note This function does not provide support against multiple instances, unless you check for that yourself. |
---|
730 | */ |
---|
731 | void register_pid(pid_t pid, char *name_str) |
---|
732 | { |
---|
733 | char tmp[MAX_STR_LEN + 1], lockfile_fname[MAX_STR_LEN + 1]; |
---|
734 | int res; |
---|
735 | FILE *fin; |
---|
736 | |
---|
737 | sprintf(lockfile_fname, "/var/run/monitas-%s.pid", name_str); |
---|
738 | if (!pid) { |
---|
739 | log_it("Unregistering PID"); |
---|
740 | if (unlink(lockfile_fname)) { |
---|
741 | log_it("Error unregistering PID"); |
---|
742 | } |
---|
743 | return; |
---|
744 | } |
---|
745 | if (does_file_exist(lockfile_fname)) { |
---|
746 | tmp[0] = '\0'; |
---|
747 | if ((fin = fopen(lockfile_fname, "r"))) { |
---|
748 | (void) fgets(tmp, MAX_STR_LEN, fin); |
---|
749 | paranoid_fclose(fin); |
---|
750 | } else { |
---|
751 | log_OS_error("Unable to openin lockfile_fname"); |
---|
752 | } |
---|
753 | pid = (pid_t) atol(tmp); |
---|
754 | sprintf(tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid); |
---|
755 | res = system(tmp); |
---|
756 | if (!res) { |
---|
757 | log_it |
---|
758 | ("I believe the daemon is already running. If it isn't, please delete %s and try again.", |
---|
759 | lockfile_fname); |
---|
760 | } |
---|
761 | } |
---|
762 | sprintf(tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(), |
---|
763 | lockfile_fname); |
---|
764 | if (system(tmp)) { |
---|
765 | fatal_error("Cannot register PID"); |
---|
766 | } |
---|
767 | } |
---|
768 | |
---|
769 | |
---|
770 | |
---|
771 | /** |
---|
772 | * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/mountlist.txt. |
---|
773 | * @param tmpdir The tempdir where the mountlist is stored. |
---|
774 | * @param dev The device to search for. |
---|
775 | * @return The size of the partition in KB. |
---|
776 | */ |
---|
777 | long size_of_partition_in_mountlist_K(char *tmpdir, char *dev) |
---|
778 | { |
---|
779 | char command[MAX_STR_LEN]; |
---|
780 | char mountlist[MAX_STR_LEN]; |
---|
781 | char sz_res[MAX_STR_LEN]; |
---|
782 | long file_len_K; |
---|
783 | |
---|
784 | sprintf(mountlist, "%s/mountlist.txt", tmpdir); |
---|
785 | sprintf(command, |
---|
786 | "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'", |
---|
787 | dev, tmpdir); |
---|
788 | log_it(command); |
---|
789 | strcpy(sz_res, call_program_and_get_last_line_of_output(command)); |
---|
790 | file_len_K = atol(sz_res); |
---|
791 | log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K); |
---|
792 | return (file_len_K); |
---|
793 | } |
---|
794 | |
---|
795 | /** |
---|
796 | * Calculate the total size (in KB) of all the biggiefiles in this backup. |
---|
797 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used. |
---|
798 | * @return The total size of all biggiefiles in KB. |
---|
799 | */ |
---|
800 | long size_of_all_biggiefiles_K(struct s_bkpinfo *bkpinfo) |
---|
801 | { |
---|
802 | /*@ buffers ***************************************************** */ |
---|
803 | char *fname; |
---|
804 | char *biggielist; |
---|
805 | char *comment; |
---|
806 | char *tmp; |
---|
807 | char *command; |
---|
808 | |
---|
809 | /*@ long ******************************************************** */ |
---|
810 | long scratchL = 0; |
---|
811 | long file_len_K; |
---|
812 | |
---|
813 | /*@ pointers *************************************************** */ |
---|
814 | FILE *fin = NULL; |
---|
815 | |
---|
816 | /*@ end vars *************************************************** */ |
---|
817 | |
---|
818 | malloc_string(fname); |
---|
819 | malloc_string(biggielist); |
---|
820 | malloc_string(comment); |
---|
821 | malloc_string(tmp); |
---|
822 | malloc_string(command); |
---|
823 | log_it("Calculating size of all biggiefiles (in total)"); |
---|
824 | sprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir); |
---|
825 | log_it("biggielist = %s", biggielist); |
---|
826 | if (!(fin = fopen(biggielist, "r"))) { |
---|
827 | log_OS_error |
---|
828 | ("Cannot open biggielist. OK, so estimate is based on filesets only."); |
---|
829 | } else { |
---|
830 | log_msg(4, "Reading it..."); |
---|
831 | for (fgets(fname, MAX_STR_LEN, fin); !feof(fin); |
---|
832 | fgets(fname, MAX_STR_LEN, fin)) { |
---|
833 | if (fname[strlen(fname) - 1] <= 32) { |
---|
834 | fname[strlen(fname) - 1] = '\0'; |
---|
835 | } |
---|
836 | if (0 == strncmp(fname, "/dev/", 5)) { |
---|
837 | if (is_dev_an_NTFS_dev(fname)) { |
---|
838 | if ( !find_home_of_exe("ntfsresize")) { |
---|
839 | fatal_error("ntfsresize not found"); |
---|
840 | } |
---|
841 | sprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname); |
---|
842 | log_it("command = %s", command); |
---|
843 | strcpy (tmp, call_program_and_get_last_line_of_output(command)); |
---|
844 | log_it("res of it = %s", tmp); |
---|
845 | file_len_K = atoll(tmp) / 1024L; |
---|
846 | } else { |
---|
847 | file_len_K = get_phys_size_of_drive(fname) * 1024L; |
---|
848 | } |
---|
849 | } else { |
---|
850 | /* BERLIOS: more than long here ??? */ |
---|
851 | file_len_K = (long) (length_of_file(fname) / 1024); |
---|
852 | } |
---|
853 | if (file_len_K > 0) { |
---|
854 | scratchL += file_len_K; |
---|
855 | log_msg(4, "%s --> %ld K", fname, file_len_K); |
---|
856 | } |
---|
857 | sprintf(comment, |
---|
858 | "After adding %s, scratchL+%ld now equals %ld", fname, |
---|
859 | file_len_K, scratchL); |
---|
860 | log_msg(4, comment); |
---|
861 | if (feof(fin)) { |
---|
862 | break; |
---|
863 | } |
---|
864 | } |
---|
865 | } |
---|
866 | log_it("Closing..."); |
---|
867 | paranoid_fclose(fin); |
---|
868 | log_it("Finished calculating total size of all biggiefiles"); |
---|
869 | mr_free(fname); |
---|
870 | mr_free(biggielist); |
---|
871 | mr_free(comment); |
---|
872 | mr_free(tmp); |
---|
873 | mr_free(command); |
---|
874 | return (scratchL); |
---|
875 | } |
---|
876 | |
---|
877 | /** |
---|
878 | * Determine the amount of space (in KB) occupied by a mounted CD. |
---|
879 | * This can also be used to find the space used for other directories. |
---|
880 | * @param mountpt The mountpoint/directory to check. |
---|
881 | * @return The amount of space occupied in KB. |
---|
882 | */ |
---|
883 | long long space_occupied_by_cd(char *mountpt) |
---|
884 | { |
---|
885 | /*@ buffer ****************************************************** */ |
---|
886 | char tmp[MAX_STR_LEN]; |
---|
887 | char command[MAX_STR_LEN * 2]; |
---|
888 | long long llres; |
---|
889 | /*@ pointers **************************************************** */ |
---|
890 | char *p; |
---|
891 | FILE *fin; |
---|
892 | |
---|
893 | /*@ end vars *************************************************** */ |
---|
894 | |
---|
895 | sprintf(command, "du -sk %s", mountpt); |
---|
896 | errno = 0; |
---|
897 | fin = popen(command, "r"); |
---|
898 | if (errno) { |
---|
899 | log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno)); |
---|
900 | llres = 0; |
---|
901 | } else { |
---|
902 | (void) fgets(tmp, MAX_STR_LEN, fin); |
---|
903 | paranoid_pclose(fin); |
---|
904 | p = strchr(tmp, '\t'); |
---|
905 | if (p) { |
---|
906 | *p = '\0'; |
---|
907 | } |
---|
908 | for (p = tmp, llres = 0; *p != '\0'; p++) { |
---|
909 | llres *= 10; |
---|
910 | llres += (int) (*p - '0'); |
---|
911 | } |
---|
912 | } |
---|
913 | |
---|
914 | return (llres); |
---|
915 | } |
---|
916 | |
---|
917 | |
---|
918 | /** |
---|
919 | * Update a CRC checksum to include another character. |
---|
920 | * @param crc The original CRC checksum. |
---|
921 | * @param c The character to add. |
---|
922 | * @return The new CRC checksum. |
---|
923 | * @ingroup utilityGroup |
---|
924 | */ |
---|
925 | unsigned int updcrc(unsigned int crc, unsigned int c) |
---|
926 | { |
---|
927 | unsigned int tmp; |
---|
928 | tmp = (crc >> 8) ^ c; |
---|
929 | crc = (crc << 8) ^ crctttab[tmp & 255]; |
---|
930 | return crc; |
---|
931 | } |
---|
932 | |
---|
933 | /** |
---|
934 | * Update a reverse CRC checksum to include another character. |
---|
935 | * @param crc The original CRC checksum. |
---|
936 | * @param c The character to add. |
---|
937 | * @return The new CRC checksum. |
---|
938 | * @ingroup utilityGroup |
---|
939 | */ |
---|
940 | unsigned int updcrcr(unsigned int crc, unsigned int c) |
---|
941 | { |
---|
942 | unsigned int tmp; |
---|
943 | tmp = crc ^ c; |
---|
944 | crc = (crc >> 8) ^ crc16tab[tmp & 0xff]; |
---|
945 | return crc; |
---|
946 | } |
---|
947 | |
---|
948 | |
---|
949 | |
---|
950 | |
---|
951 | /** |
---|
952 | * Check for an executable on the user's system; write a message to the |
---|
953 | * screen and the log if we can't find it. |
---|
954 | * @param fname The executable basename to look for. |
---|
955 | * @return 0 if it's found, nonzero if not. |
---|
956 | */ |
---|
957 | int whine_if_not_found(char *fname) |
---|
958 | { |
---|
959 | /*@ buffers *** */ |
---|
960 | char command[MAX_STR_LEN * 2]; |
---|
961 | char errorstr[MAX_STR_LEN]; |
---|
962 | |
---|
963 | |
---|
964 | sprintf(command, "which %s > /dev/null 2> /dev/null", fname); |
---|
965 | sprintf(errorstr, |
---|
966 | "Please install '%s'. I cannot find it on your system.", |
---|
967 | fname); |
---|
968 | if (system(command)) { |
---|
969 | log_to_screen(errorstr); |
---|
970 | log_to_screen |
---|
971 | ("There may be hyperlink at http://www.mondorescue.com which"); |
---|
972 | log_to_screen("will take you to the relevant (missing) package."); |
---|
973 | return (1); |
---|
974 | } else { |
---|
975 | return (0); |
---|
976 | } |
---|
977 | } |
---|
978 | |
---|
979 | |
---|
980 | |
---|
981 | |
---|
982 | |
---|
983 | |
---|
984 | /** |
---|
985 | * Create a data file at @p fname containing @p contents. |
---|
986 | * The data actually can be multiple lines, despite the name. |
---|
987 | * @param fname The file to create. |
---|
988 | * @param contents The data to put in it. |
---|
989 | * @return 0 for success, 1 for failure. |
---|
990 | */ |
---|
991 | int write_one_liner_data_file(char *fname, char *contents) |
---|
992 | { |
---|
993 | /*@ pointers *************************************************** */ |
---|
994 | FILE *fout; |
---|
995 | int res = 0; |
---|
996 | |
---|
997 | /*@ end vars *************************************************** */ |
---|
998 | |
---|
999 | assert_string_is_neither_NULL_nor_zerolength(fname); |
---|
1000 | if (!contents) { |
---|
1001 | log_it("%d: Warning - writing NULL to %s", __LINE__, fname); |
---|
1002 | } |
---|
1003 | if (!(fout = fopen(fname, "w"))) { |
---|
1004 | log_it("fname=%s"); |
---|
1005 | log_OS_error("Unable to openout fname"); |
---|
1006 | return (1); |
---|
1007 | } |
---|
1008 | fprintf(fout, "%s\n", contents); |
---|
1009 | paranoid_fclose(fout); |
---|
1010 | return (res); |
---|
1011 | } |
---|
1012 | |
---|
1013 | |
---|
1014 | |
---|
1015 | /** |
---|
1016 | * Read @p fname into @p contents. |
---|
1017 | * @param fname The file to read. |
---|
1018 | * @param contents Where to put its contents. |
---|
1019 | * @return 0 for success, nonzero for failure. |
---|
1020 | */ |
---|
1021 | int read_one_liner_data_file(char *fname, char *contents) |
---|
1022 | { |
---|
1023 | /*@ pointers *************************************************** */ |
---|
1024 | FILE *fin; |
---|
1025 | int res = 0; |
---|
1026 | int i; |
---|
1027 | |
---|
1028 | /*@ end vars *************************************************** */ |
---|
1029 | |
---|
1030 | assert_string_is_neither_NULL_nor_zerolength(fname); |
---|
1031 | if (!contents) { |
---|
1032 | log_it("%d: Warning - reading NULL from %s", __LINE__, fname); |
---|
1033 | } |
---|
1034 | if (!(fin = fopen(fname, "r"))) { |
---|
1035 | log_it("fname=%s", fname); |
---|
1036 | log_OS_error("Unable to openin fname"); |
---|
1037 | return (1); |
---|
1038 | } |
---|
1039 | fscanf(fin, "%s\n", contents); |
---|
1040 | i = strlen(contents); |
---|
1041 | if (i > 0 && contents[i - 1] < 32) { |
---|
1042 | contents[i - 1] = '\0'; |
---|
1043 | } |
---|
1044 | paranoid_fclose(fin); |
---|
1045 | return (res); |
---|
1046 | } |
---|
1047 | |
---|
1048 | |
---|
1049 | |
---|
1050 | |
---|
1051 | |
---|
1052 | |
---|
1053 | |
---|
1054 | |
---|
1055 | |
---|
1056 | /** |
---|
1057 | * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir. |
---|
1058 | * Currently this includes: copy Mondo's home directory to scratchdir, untar "mondo_home/payload.tgz" |
---|
1059 | * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore |
---|
1060 | * and post-nuke.tgz (if it exists) to tmpdir, and run "hostname > scratchdir/HOSTNAME". |
---|
1061 | * @param bkpinfo The backup information structure. Fields used: |
---|
1062 | * - @c bkpinfo->postnuke_tarball |
---|
1063 | * - @c bkpinfo->scratchdir |
---|
1064 | * - @c bkpinfo->tmpdir |
---|
1065 | */ |
---|
1066 | void copy_mondo_and_mindi_stuff_to_scratchdir(struct s_bkpinfo *bkpinfo) |
---|
1067 | { |
---|
1068 | /*@ Char buffers ** */ |
---|
1069 | char command[MAX_STR_LEN * 2]; |
---|
1070 | char tmp[MAX_STR_LEN]; |
---|
1071 | char old_pwd[MAX_STR_LEN]; |
---|
1072 | |
---|
1073 | mvaddstr_and_log_it(g_currentY, 0, |
---|
1074 | "Copying Mondo's core files to the scratch directory"); |
---|
1075 | |
---|
1076 | log_msg(4, "g_mondo_home='%s'", g_mondo_home); |
---|
1077 | if (strlen(g_mondo_home) < 2) { |
---|
1078 | find_and_store_mondoarchives_home(g_mondo_home); |
---|
1079 | } |
---|
1080 | sprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, |
---|
1081 | bkpinfo->scratchdir); |
---|
1082 | |
---|
1083 | log_msg(4, "command = %s", command); |
---|
1084 | if (run_program_and_log_output(command, 1)) { |
---|
1085 | fatal_error("Failed to copy Mondo's stuff to scratchdir"); |
---|
1086 | } |
---|
1087 | |
---|
1088 | sprintf(tmp, "%s/payload.tgz", g_mondo_home); |
---|
1089 | if (does_file_exist(tmp)) { |
---|
1090 | log_it("Untarring payload %s to scratchdir %s", tmp, |
---|
1091 | bkpinfo->scratchdir); |
---|
1092 | (void) getcwd(old_pwd, MAX_STR_LEN - 1); |
---|
1093 | chdir(bkpinfo->scratchdir); |
---|
1094 | sprintf(command, "tar -zxvf %s", tmp); |
---|
1095 | if (run_program_and_log_output(command, FALSE)) { |
---|
1096 | fatal_error("Failed to untar payload"); |
---|
1097 | } |
---|
1098 | chdir(old_pwd); |
---|
1099 | } |
---|
1100 | |
---|
1101 | sprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, |
---|
1102 | bkpinfo->scratchdir); |
---|
1103 | |
---|
1104 | if (run_program_and_log_output(command, FALSE)) { |
---|
1105 | fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir"); |
---|
1106 | } |
---|
1107 | |
---|
1108 | strcpy(tmp, |
---|
1109 | call_program_and_get_last_line_of_output("which mondorestore")); |
---|
1110 | if (!tmp[0]) { |
---|
1111 | fatal_error |
---|
1112 | ("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?"); |
---|
1113 | } |
---|
1114 | sprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir); |
---|
1115 | if (run_program_and_log_output(command, FALSE)) { |
---|
1116 | fatal_error("Failed to copy mondorestore to tmpdir"); |
---|
1117 | } |
---|
1118 | |
---|
1119 | sprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir); |
---|
1120 | paranoid_system(command); |
---|
1121 | |
---|
1122 | if (bkpinfo->postnuke_tarball[0]) { |
---|
1123 | sprintf(command, "cp -f %s %s/post-nuke.tgz", |
---|
1124 | bkpinfo->postnuke_tarball, bkpinfo->tmpdir); |
---|
1125 | if (run_program_and_log_output(command, FALSE)) { |
---|
1126 | fatal_error("Unable to copy post-nuke tarball to tmpdir"); |
---|
1127 | } |
---|
1128 | } |
---|
1129 | |
---|
1130 | |
---|
1131 | mvaddstr_and_log_it(g_currentY++, 74, "Done."); |
---|
1132 | } |
---|
1133 | |
---|
1134 | |
---|
1135 | |
---|
1136 | |
---|
1137 | |
---|
1138 | /** |
---|
1139 | * Store the client's NFS configuration in files to be restored at restore-time. |
---|
1140 | * Assumes that @c bkpinfo->media_type = nfs, but does not check for this. |
---|
1141 | * @param bkpinfo The backup information structure. Fields used: |
---|
1142 | * - @c nfs_mount |
---|
1143 | * - @c nfs_remote_dir |
---|
1144 | * - @c tmpdir |
---|
1145 | */ |
---|
1146 | void store_nfs_config(struct s_bkpinfo *bkpinfo) |
---|
1147 | { |
---|
1148 | |
---|
1149 | /*@ buffers ******** */ |
---|
1150 | char nfs_dev[MAX_STR_LEN]; |
---|
1151 | char mac_addr[MAX_STR_LEN]; |
---|
1152 | char nfs_mount[MAX_STR_LEN]; |
---|
1153 | char nfs_client_ipaddr[MAX_STR_LEN]; |
---|
1154 | char nfs_client_netmask[MAX_STR_LEN]; |
---|
1155 | char nfs_client_broadcast[MAX_STR_LEN]; |
---|
1156 | char nfs_client_defgw[MAX_STR_LEN]; |
---|
1157 | char nfs_server_ipaddr[MAX_STR_LEN]; |
---|
1158 | char tmp[MAX_STR_LEN]; |
---|
1159 | char command[MAX_STR_LEN * 2]; |
---|
1160 | |
---|
1161 | FILE *fd1 = NULL; |
---|
1162 | |
---|
1163 | /*@ pointers ***** */ |
---|
1164 | char *p; |
---|
1165 | |
---|
1166 | log_it("Storing NFS configuration"); |
---|
1167 | strcpy(tmp, bkpinfo->nfs_mount); |
---|
1168 | p = strchr(tmp, ':'); |
---|
1169 | if (!p) { |
---|
1170 | fatal_error |
---|
1171 | ("NFS mount doesn't have a colon in it, e.g. 192.168.1.4:/home/nfs"); |
---|
1172 | } |
---|
1173 | *(p++) = '\0'; |
---|
1174 | strcpy(nfs_server_ipaddr, tmp); |
---|
1175 | strcpy(nfs_mount, p); |
---|
1176 | /* BERLIOS : there is a bug #67 here as it only considers the first NIC */ |
---|
1177 | sprintf(command, |
---|
1178 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1"); |
---|
1179 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command)); |
---|
1180 | sprintf(command, |
---|
1181 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2"); |
---|
1182 | strcpy(nfs_client_ipaddr, |
---|
1183 | call_program_and_get_last_line_of_output(command)); |
---|
1184 | sprintf(command, |
---|
1185 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2"); |
---|
1186 | strcpy(nfs_client_netmask, |
---|
1187 | call_program_and_get_last_line_of_output(command)); |
---|
1188 | sprintf(command, |
---|
1189 | "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2"); |
---|
1190 | strcpy(nfs_client_broadcast, |
---|
1191 | call_program_and_get_last_line_of_output(command)); |
---|
1192 | sprintf(command, |
---|
1193 | "route -n | grep '^0.0.0.0' | awk '{print $2}'"); |
---|
1194 | strcpy(nfs_client_defgw, |
---|
1195 | call_program_and_get_last_line_of_output(command)); |
---|
1196 | sprintf(tmp, |
---|
1197 | "nfs_client_ipaddr=%s; nfs_server_ipaddr=%s; nfs_mount=%s", |
---|
1198 | nfs_client_ipaddr, nfs_server_ipaddr, nfs_mount); |
---|
1199 | if (strlen(nfs_dev) < 2) { |
---|
1200 | fatal_error |
---|
1201 | ("Unable to find ethN (eth0, eth1, ...) adapter via NFS mount you specified."); |
---|
1202 | } |
---|
1203 | /******** |
---|
1204 | * If the NFS device that found above is a bonded device, |
---|
1205 | * we need to replace it with an ethN device or the |
---|
1206 | * networking will not start during an NFS restore. |
---|
1207 | * |
---|
1208 | * If the NFS device in nfs_dev begins with the word "bond", |
---|
1209 | * look for the corresponding slave ethN device and copy it to nfs_dev. |
---|
1210 | * Using the common MAC address |
---|
1211 | ********/ |
---|
1212 | if (!strncmp(nfs_dev, "bond", 4)) { |
---|
1213 | log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", nfs_dev); |
---|
1214 | sprintf(command, |
---|
1215 | "ifconfig %s | awk '{print $5}' | head -n1", nfs_dev); |
---|
1216 | strcpy(mac_addr, call_program_and_get_last_line_of_output(command)); |
---|
1217 | sprintf(command, |
---|
1218 | "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr,nfs_dev); |
---|
1219 | strcpy(nfs_dev, call_program_and_get_last_line_of_output(command)); |
---|
1220 | log_to_screen("Replacing it with %s\n", nfs_dev); |
---|
1221 | } |
---|
1222 | |
---|
1223 | mkdir(MONDO_CACHE,0x755); |
---|
1224 | fd1 = mr_fopen(MONDORESTORECFG, "a"); |
---|
1225 | |
---|
1226 | mr_fprintf(fd1, "nfs-dev %s\n", nfs_dev); |
---|
1227 | mr_fprintf(fd1, "nfs-client-ipaddr %s\n", nfs_client_ipaddr); |
---|
1228 | mr_fprintf(fd1, "nfs-client-netmask %s\n", nfs_client_netmask); |
---|
1229 | mr_fprintf(fd1, "nfs-client-broadcast %s\n", nfs_client_broadcast); |
---|
1230 | mr_fprintf(fd1, "nfs-client-defgw %s\n", nfs_client_defgw); |
---|
1231 | mr_fprintf(fd1, "nfs-server-ipaddr %s\n", nfs_server_ipaddr); |
---|
1232 | mr_fprintf(fd1, "nfs-server-mount %s\n", bkpinfo->nfs_mount); |
---|
1233 | mr_fprintf(fd1, "nfs-server-path %s\n", bkpinfo->nfs_remote_dir); |
---|
1234 | mr_fprintf(fd1, "iso-prefix %s\n", bkpinfo->prefix); |
---|
1235 | mr_fclose(fd1); |
---|
1236 | |
---|
1237 | log_it("Finished storing NFS configuration"); |
---|
1238 | } |
---|
1239 | |
---|
1240 | |
---|
1241 | |
---|
1242 | |
---|
1243 | |
---|
1244 | |
---|
1245 | /** |
---|
1246 | * Determine the approximate number of media that the backup will take up, |
---|
1247 | * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K() |
---|
1248 | * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as |
---|
1249 | * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any |
---|
1250 | * "imagedevs", the estimate is not shown as it will be wildly inaccurate. |
---|
1251 | * If there are more than 50 media estimated, the estimate will not be shown. |
---|
1252 | * @param bkpinfo The backup information structure. Fields used: |
---|
1253 | * - @c bkpinfo->backup_media_type |
---|
1254 | * - @c bkpinfo->image_devs |
---|
1255 | * - @c bkpinfo->media_size |
---|
1256 | * - @c bkpinfo->optimal_set_size |
---|
1257 | * - @c bkpinfo->use_lzo |
---|
1258 | * @param noof_sets The number of filesets created. |
---|
1259 | * @ingroup archiveGroup |
---|
1260 | */ |
---|
1261 | void |
---|
1262 | estimate_noof_media_required(struct s_bkpinfo *bkpinfo, long noof_sets) |
---|
1263 | { |
---|
1264 | /*@ buffers *************** */ |
---|
1265 | char tmp[MAX_STR_LEN]; |
---|
1266 | |
---|
1267 | /*@ long long ************* */ |
---|
1268 | long long scratchLL; |
---|
1269 | |
---|
1270 | if (bkpinfo->media_size[1] <= 0 || bkpinfo->backup_media_type == nfs) { |
---|
1271 | log_to_screen("Number of media required: UNKNOWN"); |
---|
1272 | return; |
---|
1273 | } |
---|
1274 | |
---|
1275 | log_it("Estimating number of media required..."); |
---|
1276 | scratchLL = |
---|
1277 | (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size) |
---|
1278 | + (long long) (size_of_all_biggiefiles_K(bkpinfo)); |
---|
1279 | scratchLL = (scratchLL / 1024) / bkpinfo->media_size[1]; |
---|
1280 | scratchLL++; |
---|
1281 | if (bkpinfo->use_lzo) { |
---|
1282 | scratchLL = (scratchLL * 2) / 3; |
---|
1283 | } else if (bkpinfo->use_gzip) { |
---|
1284 | scratchLL = (scratchLL * 2) / 3; |
---|
1285 | } else { |
---|
1286 | scratchLL = scratchLL / 2; |
---|
1287 | } |
---|
1288 | if (!scratchLL) { |
---|
1289 | scratchLL++; |
---|
1290 | } |
---|
1291 | if (scratchLL <= 1) { |
---|
1292 | sprintf(tmp, |
---|
1293 | "Your backup will probably occupy a single %s. Maybe two.", |
---|
1294 | media_descriptor_string(bkpinfo->backup_media_type)); |
---|
1295 | } else if (scratchLL > 4) { |
---|
1296 | sprintf(tmp, |
---|
1297 | "Your backup will occupy one meeeeellion media! (maybe %s)", |
---|
1298 | number_to_text((int) (scratchLL + 1))); |
---|
1299 | } else { |
---|
1300 | sprintf(tmp, "Your backup will occupy approximately %s media.", |
---|
1301 | number_to_text((int) (scratchLL + 1))); |
---|
1302 | } |
---|
1303 | if (!bkpinfo->image_devs[0] && (scratchLL < 50)) { |
---|
1304 | log_to_screen(tmp); |
---|
1305 | } |
---|
1306 | } |
---|
1307 | |
---|
1308 | |
---|
1309 | /** |
---|
1310 | * Get the last suffix of @p instr. |
---|
1311 | * If @p instr was "httpd.log.gz", we would return "gz". |
---|
1312 | * @param instr The filename to get the suffix of. |
---|
1313 | * @return The suffix (without a dot), or "" if none. |
---|
1314 | * @note The returned string points to static storage that will be overwritten with each call. |
---|
1315 | */ |
---|
1316 | char *sz_last_suffix(char *instr) |
---|
1317 | { |
---|
1318 | static char outstr[MAX_STR_LEN]; |
---|
1319 | char *p; |
---|
1320 | |
---|
1321 | p = strrchr(instr, '.'); |
---|
1322 | if (!p) { |
---|
1323 | outstr[0] = '\0'; |
---|
1324 | } else { |
---|
1325 | strcpy(outstr, p); |
---|
1326 | } |
---|
1327 | return (outstr); |
---|
1328 | } |
---|
1329 | |
---|
1330 | |
---|
1331 | /** |
---|
1332 | * Determine whether a file is compressed. This is done |
---|
1333 | * by reading through the "do-not-compress-these" file distributed with Mondo. |
---|
1334 | * @param filename The file to check. |
---|
1335 | * @return TRUE if it's compressed, FALSE if not. |
---|
1336 | */ |
---|
1337 | bool is_this_file_compressed(char *filename) |
---|
1338 | { |
---|
1339 | char do_not_compress_these[MAX_STR_LEN]; |
---|
1340 | char tmp[MAX_STR_LEN]; |
---|
1341 | char *p; |
---|
1342 | |
---|
1343 | sprintf(tmp, "%s/do-not-compress-these", g_mondo_home); |
---|
1344 | if (!does_file_exist(tmp)) { |
---|
1345 | return (FALSE); |
---|
1346 | } |
---|
1347 | strcpy(do_not_compress_these, last_line_of_file(tmp)); |
---|
1348 | for (p = do_not_compress_these; p != NULL; p++) { |
---|
1349 | strcpy(tmp, p); |
---|
1350 | if (strchr(tmp, ' ')) { |
---|
1351 | *(strchr(tmp, ' ')) = '\0'; |
---|
1352 | } |
---|
1353 | if (!strcmp(sz_last_suffix(filename), tmp)) { /*printf("MATCH\n"); */ |
---|
1354 | return (TRUE); |
---|
1355 | } |
---|
1356 | if (!(p = strchr(p, ' '))) { |
---|
1357 | break; |
---|
1358 | } |
---|
1359 | } |
---|
1360 | return (FALSE); |
---|
1361 | } |
---|
1362 | |
---|
1363 | |
---|
1364 | |
---|
1365 | int mode_of_file(char *fname) |
---|
1366 | { |
---|
1367 | struct stat buf; |
---|
1368 | |
---|
1369 | if (lstat(fname, &buf)) { |
---|
1370 | return (-1); |
---|
1371 | } // error |
---|
1372 | else { |
---|
1373 | return (buf.st_mode); |
---|
1374 | } |
---|
1375 | } |
---|
1376 | |
---|
1377 | |
---|
1378 | |
---|
1379 | |
---|
1380 | /** |
---|
1381 | * Create a small script that mounts /boot, calls @c grub-install, and syncs the disks. |
---|
1382 | * @param outfile Where to put the script. |
---|
1383 | * @return 0 for success, 1 for failure. |
---|
1384 | */ |
---|
1385 | int make_grub_install_scriptlet(char *outfile) |
---|
1386 | { |
---|
1387 | FILE *fout; |
---|
1388 | char *tmp; |
---|
1389 | int retval = 0; |
---|
1390 | |
---|
1391 | malloc_string(tmp); |
---|
1392 | if ((fout = fopen(outfile, "w"))) { |
---|
1393 | fprintf(fout, |
---|
1394 | "#!/bin/sh\n\nmount /boot > /dev/null 2> /dev/null\ngrub-install $@\nres=$?\nsync;sync;sync\nexit $res\n"); |
---|
1395 | paranoid_fclose(fout); |
---|
1396 | log_msg(2, "Created %s", outfile); |
---|
1397 | sprintf(tmp, "chmod +x %s", outfile); |
---|
1398 | paranoid_system(tmp); |
---|
1399 | retval = 0; |
---|
1400 | } else { |
---|
1401 | retval = 1; |
---|
1402 | } |
---|
1403 | mr_free(tmp); |
---|
1404 | return (retval); |
---|
1405 | } |
---|
1406 | |
---|
1407 | /* @} - end fileGroup */ |
---|