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

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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