source: branches/3.1/mondo/src/common/libmondo-files.c @ 3147

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