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

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