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

Last change on this file since 783 was 783, checked in by bruno, 13 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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