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

Last change on this file since 2328 was 2328, checked in by Bruno Cornec, 12 years ago

r3339@localhost: bruno | 2009-08-11 23:56:01 +0200
bkpinfo->kernel_path, bkpinfo->call_before_iso, bkpinfo->call_make_iso, bkpinfo->call_burn_iso, bkpinfo->call_after_iso and bkpinfo->postnuke_tarball are now dynamically allocated

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