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

Last change on this file since 688 was 688, checked in by bcornec, 13 years ago

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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