source: MondoRescue/branches/3.2/mondo/src/common/libmondo-files.c@ 3194

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