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