source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-files.c@ 2289

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