source: MondoRescue/branches/stable/mondo/src/common/libmondo-files.c@ 1173

Last change on this file since 1173 was 1173, checked in by Bruno Cornec, 17 years ago

merges from trunk for memory management for libmondo-mountlist.c mainly

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