source: MondoRescue/trunk/mondo/mondo/common/libmondo-files.c@ 764

Last change on this file since 764 was 764, checked in by Bruno Cornec, 18 years ago

merge -r728:763 $SVN_M/branches/stable

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