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

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

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

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