source: branches/stable/mondo/src/common/libmondo-files.c @ 1113

Last change on this file since 1113 was 1113, checked in by bruno, 13 years ago

Memory management improvements again for libmondo-fork.c & libmondo-files.c

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