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

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 7 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

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