source: branches/3.1/mondo/src/common/libmondo-files.c @ 2937

Last change on this file since 2937 was 2937, checked in by bruno, 6 years ago

svn merge -r 2849:2935 ../3.0

  • Adds 3 binaries called potentially by udev o support USB key mount at restore time (Victor Gattegno)
  • mindi only sets the boot flag of a USB device if none is present already (should avoid flip/flop situation)
  • Fix a compatibility bug in analyze-my-lvm whete exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Precise that -J is not supported by mondorestore yet.
  • Fix a compatibility bug in mindi where exclude lists with '|' was incorrectly analyzed (' ' still expected)
  • Install an MBR on USB devices when possible
  • Improve error message in case mondorestore isn't found
  • Adds support for more virtio devices for KVM restore
  • Improve mbr.bin handling
  • Fix an issue in test of FindKernel? for Xen and non Xen cases
    • Fix #492 by removing an extra useless fi
    • Move fvat nd fat modules to CDROM_MODS in order to have them in the initrd to support saving logs to a USB device e.g.
    • Fix an error when dmidecode reports comments before system name
    • Fix previous broken patch on comment management for bkphw
    • Improve msgs printed during kbd analysis
    • Fix a small remaining pb with new comment support
    • Try to again improve the mount/umount stuff in mondo -Adds mention to OEL, CentOS, ScientifLinux? as RHEL clones
  • Fix a panadoid_free done on the wrong variable (solve a memory leak)
  • Solve #495 by skipping the potential (auto-read-only) when analyzin /proc/mdstat result
  • Fix some markup in man pages
  • adds the post-nuke script sample for Debian (Guillaume Pernot/Andrée? Leidenfrost)
  • Avoid a useless umount when changing of afio set, let insist_on_cd check and perform if needed
  • Copy everything under .ssh of root account to allow for sshfs mount without passwd at restore time.
  • Adds support for bnx2i driver
  • Try to fix #496 by supporting the protocol in the output of the mount command (sshfs#...)
  • Uses the -d option of the mount command to avoid leaving loop devices in use. Probably provoked by the usage of a more recent busybox, but anyway that option doesn't hurt, and does nothing for non-loop devices. Should fix the problems reported on the ML with NFS and more than 8 ISO images (8 being the default number of loop devices available)
  • Attempt to fix #436 by adding some newtDrawForm where appropriate.
  • Fix bugs in the kernel detection routine of mindi (some echo were done without capturing their output, confusing the return). This should also help the Xen detection issue
  • Change the logging function to be nearer from 2.2.10, and start using some
  • Initialize some global fields with an empty string, now that newt Draws more regularly progress forms
  • Mindi progression still not good, but showing now 3% when calling mindi. Would need a specific handling for the progress bar to evolve more afterwards
  • Add compatibility with the latest SSSTK + PSP content for HP ProLiant? + branding homogeneous now. We only take conrep out of the SSSTK pending a package availability. Using hpacuscripting now. Review of parameters called following test made.
  • Fix #501 by pushing BOOT_SIZE to 36864 for certain Ubuntu cases
  • Fix some remaining inconsistencis in the ProLiant? HW backup. ready for usage.
  • Fix an issue on udevadm call (needs an intermediate variable)
  • Fix a remaining ProLiant? tool call
  • Remove again some wrong variable initialization for Net in order to not have strange messages now we call Draw more often
  • Adds support for hwaddr (MAC addresses) at restore time, and map to the correct NIC if it's found at restore time (based on code from Stefan Heijmans)
  • Try to Fix #499 by changing init in order to never exit, and rebooting using sysrq in /proc (not portable). Maybe should move to using a real init, and a separated rcS script.
  • Fixes the shell msg error by using setsid as per Busybox FAQ
  • Fix #491 by correctly checking Xen kernel used for a dom0 env (apollo13)
  • Improves logging in Die()
  • Improve again ProLiant? HW support, by adding references to the yet to come SSSTK rpm so that conrep is called at its future right place.
  • Improve mindi support for kernel 3.x
  • Fix #499 for good, by using a real init from busybox and moving our init script to etc/init.d/rcS. We also now call the reboot from busybox to exit, which indeed reset the system correctly (in a VM at least which wasn't the case before). Reauires a new mindi-busybox as well.
  • Attempt to solve #503 by handling /lib64 separately for udev and only if not a link (to /lib) for Ubuntu 64
  • Improve again previous tests for bug #503
  • Improve -d option description for NFS
  • Allow http download for ftp content
  • New italian mirror
  • Fix #505 by documenting environment variables
    • Fix a bug where on OBDR mode compression was turned off to create the OBDR content but not back on (report from the mailing list)
  • solve http://trac.mondorescue.org/ticket/520 by Victor Gattegno
  • Fix #524 as option -U needs a size also
  • Removes useless gendf option
  • Fix #511 now stabgrub is called also after conf file modifications
  • Adds support for SUID binaries
  • remove a useless xenkernelname variable from mindi
  • Should fix #512 by clearing old scratchdir before changing to a new
  • Simplify the function engaging SCSI for Smart Array controllers, and support as many controller as we have
  • Add some binaries used on SLES 10 by udev
  • Fix a syntax issue in newt-specific.c
  • Fix a memory free bug where a used variable was freed to early
  • Fix a memory free bug where a used variable was misused in asprintf/free
  • Fix #498 by using an external program (hpsa_obdr_mode) which can set the mode of the tape to CD-ROM or Sequential at will.

That program will have to be downloaded from http://cciss.sf.net

  • Adds support for the route command in mindi-busybox
  • Force block size back to 32768 in OBDR mode after skipping the 2 tape marks, to read the additional rootfs data corerctly - Fix #498 for good it seems
  • Adds support for a new pata_atiixp driver
  • Fix #525 and improve printing when no /etc/raidtab, but still md devices
  • Exclude /run similarly to /sys and /proc
  • Fix a bug in mindi when having multiple swap partiions with same starting string (/dev/dm-1 and /dev/dm-16). We now add a space in the grep to avoid mischoice.
  • Fix grub installation if we had to go in the hack part which wasn't using the correct variables to make decisions !
  • Remove all mention of mondorescue.com from code
  • Fix some website typo on the download page and precise that we now develop on Mageia
  • Exclude sysfs devpts and tmpfs partitions found in RHEL 6.2 from analyze by mindi (made error messages)
  • Force activation of first partition which should not be optional as everything is destroyed before
  • Fix a bug in analyze-my-lvm where LVs were created in random order (the one of lvscan) instead of using the dm-xxx order, which creates issues at restore time between the mapping created then and the one we had at backup time which could be inconssistent

-mondoarchive now exists if the mt command is missing when needed

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