source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-files.c @ 1645

Last change on this file since 1645 was 1645, checked in by Bruno Cornec, 12 years ago

Render bkpinfo global (potential issue on thread, but should not be a problem as that structure is indeed static during archive)
Should solve the tmpdir issue from previous rev.
May still not compile

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