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

Last change on this file since 416 was 416, checked in by bcornec, 14 years ago

merge -r414:415 $SVN_M/branches/stable

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