source: MondoRescue/branches/3.0/mondo/src/common/libmondo-files.c@ 3188

Last change on this file since 3188 was 3188, checked in by Bruno Cornec, 11 years ago
  • Backport tons of dynamic memory management rewrite from 3.1 branch. Test is needed
  • Property svn:keywords set to Id
File size: 36.8 KB
RevLine 
[2053]1/* file manipulation
[116]2 $Id: libmondo-files.c 3188 2013-09-25 06:55:39Z 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 3188 2013-09-25 06:55:39Z 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];
[3188]49 char *command = NULL;
[1]50
[116]51 /*@ pointers **************************************************** */
[3188]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)) {
[3188]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 }
[3188]74 mr_free(command);
[116]75 } else {
[3188]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 ***************************************************** */
[3188]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)) {
[3188]141 log_it("%s does not exist, so I cannot found the number of lines in it", filename);
[116]142 return (0);
[1]143 }
[3188]144 mr_asprintf(command, "cat %s | wc -l", filename);
[116]145 if (!does_file_exist(filename)) {
[3188]146 mr_free(command);
[116]147 return (-1);
[1]148 }
[116]149 fin = popen(command, "r");
[3188]150 mr_free(command);
151
[116]152 if (fin) {
153 if (feof(fin)) {
154 noof_lines = 0;
155 } else {
[3188]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);
[3188]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{
[3188]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);
[3188]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);
[3188]225 mr_free(tmp);
226
[116]227 if (!(fin = fopen(infname, "r"))) {
228 log_OS_error("Unable to openin infname");
[3188]229 mr_free(infname);
[116]230 return;
[1]231 }
[3188]232
233 mr_asprintf(outfname, "%s", inout);
[116]234 if (!(fout = fopen(outfname, "w"))) {
235 log_OS_error("Unable to openout outfname");
[3188]236 mr_free(infname);
237 mr_free(outfname);
[116]238 return;
[1]239 }
[3188]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 {
[3188]250 log_it("Excluding '%s'-nonexistent\n", incoming);
[116]251 }
[3188]252 mr_free(incoming);
[1]253 }
[3188]254 mr_free(incoming);
255
[116]256 paranoid_fclose(fout);
257 paranoid_fclose(fin);
258 unlink(infname);
[3188]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{
[3188]272 char *tmp = NULL;
273 char *command = NULL;
[1]274
[116]275 if (!kernel[0]) {
[3188]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]) {
[3188]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);
[3188]284 mr_free(tmp);
285 mr_free(command);
[392]286 fatal_error("Mindi gave a fatal error. Please check '/var/log/mindi.log'.");
287 }
[3188]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]) {
293 if (!ask_me_yes_or_no
[541]294 ("Kernel not found or invalid. Choose another?")) {
[116]295 return (1);
296 }
297 if (!popup_and_get_string
[541]298 ("Kernel path",
299 "What is the full path and filename of your kernel, please?",
[116]300 kernel, MAX_STR_LEN / 4)) {
301 fatal_error
302 ("Kernel not found. Please specify with the '-k' flag.");
303 }
[3188]304 log_it("User says kernel is at %s", kernel);
[116]305 }
306 return (0);
[1]307}
308
309
310/**
311 * Find location of specified executable in user's PATH.
312 * @param fname The basename of the executable to search for (e.g. @c afio).
313 * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found.
314 * @note The returned string points to static storage that will be overwritten with each call.
315 * @bug The checks with @c file and @c dirname seem pointless. If @c incoming is "", then you're calling
316 * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts
317 * to nothing.
318 */
[116]319char *find_home_of_exe(char *fname)
[1]320{
[116]321 /*@ buffers ********************* */
322 static char output[MAX_STR_LEN];
[3188]323 char *incoming = NULL;
324 char *command = NULL;
[1]325
[116]326 /*@******************************* */
[1]327
[116]328 assert_string_is_neither_NULL_nor_zerolength(fname);
[3188]329 mr_asprintf(command, "which %s 2> /dev/null", fname);
330 mr_asprintf(incoming, call_program_and_get_last_line_of_output(command));
331 mr_free(command);
332
[116]333 if (incoming[0] == '\0') {
334 if (system("which file > /dev/null 2> /dev/null")) {
[3188]335 mr_free(incoming);
[116]336 output[0] = '\0';
337 return (NULL); // forget it :)
338 }
[3188]339 mr_asprintf(command, "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", incoming);
340 mr_free(incomig);
341
342 mr_asprintf(incoming, "%s", call_program_and_get_last_line_of_output(command));
343 mr_free(command);
[116]344 }
[3188]345 if (incoming[0] == '\0') {
346 mr_asprintf(command, "dirname %s 2> /dev/null", incoming);
347 mr_free(incoming);
348
349 mr_asprintf(incoming, "%s", call_program_and_get_last_line_of_output(command));
350 mr_free(command);
[1]351 }
[116]352 strcpy(output, incoming);
353 if (output[0] != '\0' && does_file_exist(output)) {
[3188]354 log_msg(4, "find_home_of_exe () --- Found %s at %s", fname, incoming);
[116]355 } else {
356 output[0] = '\0';
357 log_msg(4, "find_home_of_exe() --- Could not find %s", fname);
358 }
[3188]359 mr_free(incoming);
[116]360 if (!output[0]) {
361 return (NULL);
362 } else {
363 return (output);
364 }
[1]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 */
[116]374int get_trackno_from_logfile(char *logfile)
[1]375{
376
[116]377 /*@ pointers ********************************************************* */
378 FILE *fin;
[1]379
[116]380 /*@ int ************************************************************** */
381 int trackno = 0;
382 size_t len = 0;
[1]383
[116]384 /*@ buffer ************************************************************ */
385 char datablock[32701];
[1]386
[116]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);
[1]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 */
[116]416int grab_percentage_from_last_line_of_file(char *filename)
[1]417{
418
[116]419 /*@ buffers ***************************************************** */
[3188]420 char *lastline = NULL;
421 char *command = NULL;
[116]422 /*@ pointers **************************************************** */
[3188]423 char *p = NULL;
[1]424
[116]425 /*@ int's ******************************************************* */
[3188]426 int i = 0;
[1]427
[3188]428 for (i = NOOF_ERR_LINES - 1; i >= 0 && !strstr(err_log_lines[i], "% Done") && !strstr(err_log_lines[i], "% done"); i--);
[116]429 if (i < 0) {
[3188]430 mr_asprintf(command, "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'", filename, '%');
431 mr_asprintf(lastline, "%s", call_program_and_get_last_line_of_output(command));
432 mr_free(command);
[116]433 if (!lastline[0]) {
[3188]434 mr_free(lastline);
[116]435 return (0);
436 }
437 } else {
[3188]438 mr_asprintf(lastline, "%s", err_log_lines[i]);
[1]439 }
440
[116]441 p = strrchr(lastline, '%');
442 if (p) {
443 *p = '\0';
[3188]444 } else {
445 mr_free(lastline);
[116]446 return (0);
447 }
[3188]448
449 for (p--; isdigit(*p) && p != lastline; p--);
[116]450 if (p != lastline) {
451 p++;
452 }
453 i = atoi(p);
[3188]454 mr_free(lastline);
[116]455 return (i);
[1]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 */
[116]468char *last_line_of_file(char *filename)
[1]469{
[116]470 /*@ buffers ***************************************************** */
471 static char output[MAX_STR_LEN];
[3188]472 char *command = NULL;
[1]473
[116]474 /*@ pointers **************************************************** */
475 FILE *fin;
[1]476
[116]477 /*@ end vars **************************************************** */
[1]478
[116]479 if (!does_file_exist(filename)) {
[3188]480 log_it("Tring to get last line of nonexistent file (%s)", filename);
[116]481 output[0] = '\0';
482 return (output);
483 }
[3188]484 mr_asprintf(command, "tail -n1 %s", filename);
[116]485 fin = popen(command, "r");
[3188]486 mr_free(command);
[3060]487 p = fgets(output, MAX_STR_LEN, fin);
488 if (p == NULL) {
489 // FIXME
490 }
[116]491 paranoid_pclose(fin);
492 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
493 output[strlen(output) - 1] = '\0';
494 }
495 return (output);
[1]496}
497
498/**
499 * Get the length of @p filename in bytes.
500 * @param filename The file to get the length of.
501 * @return The length of the file, or -1 for error.
502 */
[684]503off_t length_of_file(char *filename)
[1]504{
[116]505 /*@ pointers *************************************************** */
506 FILE *fin;
[1]507
[116]508 /*@ long long ************************************************* */
[684]509 off_t length;
[1]510
[116]511 fin = fopen(filename, "r");
512 if (!fin) {
513 log_it("filename=%s", filename);
514 log_OS_error("Unable to openin filename");
515 return (-1);
516 }
[415]517 fseeko(fin, 0, SEEK_END);
[684]518 length = ftello(fin);
[116]519 paranoid_fclose(fin);
520 return (length);
[1]521}
522
523
524
525/**
526 * ?????
527 * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
528 */
[3188]529int make_checksum_list_file(char *filelist, char *cksumlist, char *comppath) {
[116]530 /*@ pointers **************************************************** */
531 FILE *fin;
532 FILE *fout;
[1]533
[116]534 /*@ int ******************************************************* */
535 int percentage;
536 int i;
537 int counter = 0;
[1]538
[116]539 /*@ buffer ****************************************************** */
[3188]540 char *stub_fname = NULL;
541 char *curr_fname = NULL;
542 char *curr_cksum = NULL;
543 char *tmp = NULL;
[1]544
[116]545 /*@ long [long] ************************************************* */
[684]546 off_t filelist_length;
547 off_t curr_pos;
[116]548 long start_time;
549 long current_time;
550 long time_taken;
551 long time_remaining;
[1]552
[116]553 /*@ end vars *************************************************** */
[1]554
[116]555 start_time = get_time();
556 filelist_length = length_of_file(filelist);
[3188]557 log_it("filelist = %s; cksumlist = %s", filelist, cksumlist);
558
[116]559 fin = fopen(filelist, "r");
560 if (fin == NULL) {
561 log_OS_error("Unable to fopen-in filelist");
562 log_to_screen("Can't open filelist");
563 return (1);
[1]564 }
[116]565 fout = fopen(cksumlist, "w");
566 if (fout == NULL) {
567 log_OS_error("Unable to openout cksumlist");
568 paranoid_fclose(fin);
[541]569 log_to_screen("Can't open checksum list");
[116]570 return (1);
[1]571 }
[3188]572 for (mr_getline(stub_fname, fin); !feof(fin); mr_getline(stub_fname, fin)) {
[116]573 if (stub_fname[(i = strlen(stub_fname) - 1)] < 32) {
574 stub_fname[i] = '\0';
575 }
[3188]576 mr_asprintf(tmp, "%s%s", comppath, stub_fname);
577 mr_free(stub_fname);
578
579 mr_asprintf(curr_fname, "%s", tmp + 1);
580 mr_free(tmp);
581
582 mr_asprintf(curr_cksum, "%s", calc_file_ugly_minichecksum(curr_fname));
[116]583 fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
[3188]584 mr_free(curr_cksum);
585
[116]586 if (counter++ > 12) {
587 current_time = get_time();
588 counter = 0;
[3188]589 /* BERLIOS: 37 really ? */
[116]590 curr_fname[37] = '\0';
[684]591 curr_pos = ftello(fin) / 1024;
[116]592 percentage = (int) (curr_pos * 100 / filelist_length);
593 time_taken = current_time - start_time;
[3188]594 if (percentage != 0) {
595 time_remaining = time_taken * 100 / (long) (percentage) - time_taken;
596 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]597 }
598 sync();
599 }
[3188]600 mr_free(curr_fname);
[116]601 }
[3188]602 mr_free(stub_fname);
603
[116]604 paranoid_fclose(fout);
605 paranoid_fclose(fin);
606 log_it("Done.");
607 return (0);
[1]608}
609
610
611/**
612 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
613 * @param outdir_fname The directory to create.
614 * @return The return value of @c mkdir.
615 */
[3188]616int make_hole_for_dir(const char *outdir_fname)
[1]617{
[3188]618 char *tmp = NULL;
[116]619 int res = 0;
[1]620
[116]621 assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
[3188]622 mr_asprintf(tmp, "mkdir -p %s", outdir_fname);
[116]623 res = system(tmp);
[3188]624 mr_free(tmp);
[116]625 return (res);
[1]626}
627
628
629/**
630 * Create the parent directories of @p outfile_fname.
631 * @param outfile_fname The file to make a "hole" for.
632 * @return 0, always.
633 * @bug Return value unnecessary.
634 */
[116]635int make_hole_for_file(char *outfile_fname)
[1]636{
[116]637 /*@ buffer ****************************************************** */
[3188]638 char *command = NULL;
[1]639
[116]640 /*@ int ******************************************************** */
641 int res = 0;
[1]642
[116]643 /*@ end vars *************************************************** */
[1]644
[116]645 assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
646 assert(!strstr(outfile_fname, MNT_CDROM));
647 assert(!strstr(outfile_fname, "/dev/cdrom"));
[3188]648 mr_asprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
[116]649 res += system(command);
[3188]650 mr_free(command);
651
652 mr_asprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
[116]653 res += system(command);
[3188]654 mr_free(command);
655
656 mr_asprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
[116]657 res += system(command);
[3188]658 mr_free(command);
659
[116]660 unlink(outfile_fname);
661 return (0);
[1]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 */
[116]671long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
[1]672{
[116]673 /*@ long ******************************************************* */
674 long matches = 0;
[1]675
[116]676 /*@ pointers *************************************************** */
677 FILE *fin;
[1]678
[116]679 /*@ buffers **************************************************** */
[3188]680 char *incoming = NULL;
[1]681
[116]682 /*@ end vars *************************************************** */
[1]683
684
[116]685 fin = fopen(filelist_fname, "r");
[1]686
[116]687 if (!fin) {
688 log_OS_error("Unable to openin filelist_fname");
689 return (0);
[1]690 }
[3188]691 mr_getline(incoming, fin);
692 while (!feof(fin)) {
[116]693 if (strstr(incoming, wildcard)) {
694 matches++;
695 }
[3188]696 mr_free(incoming);
697 mr_getline(incoming, fin);
[116]698 }
[3188]699 mr_free(incoming);
[116]700 paranoid_fclose(fin);
701 return (matches);
[1]702}
703
704
705
706/**
707 * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/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 */
[116]712long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
[1]713{
[3188]714 char *command = NULL;
715 char *mountlist = NULL;
716 char *sz_res = NULL;
[116]717 long file_len_K;
718
[3188]719 mr_asprintf(mountlist, "%s/mountlist.txt", tmpdir);
720 mr_asprintf(command, "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'", dev, tmpdir);
721 mr_free(mountlist);
722
[116]723 log_it(command);
[3188]724 mr_asprintf(sz_res, "%s", call_program_and_get_last_line_of_output(command));
[116]725 file_len_K = atol(sz_res);
726 log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
[3188]727 mr_free(sz_res);
728 mr_free(command);
729
[116]730 return (file_len_K);
[1]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 */
[1645]738long size_of_all_biggiefiles_K()
[1]739{
[116]740 /*@ buffers ***************************************************** */
[3188]741 char *fname = NULL;
742 char *biggielist = NULL;
743 char *tmp = NULL;
744 char *command = NULL;
[1]745
[116]746 /*@ long ******************************************************** */
747 long scratchL = 0;
748 long file_len_K;
[1]749
[116]750 /*@ pointers *************************************************** */
751 FILE *fin = NULL;
[1]752
[116]753 /*@ end vars *************************************************** */
[1]754
[116]755 log_it("Calculating size of all biggiefiles (in total)");
[3188]756 mr_asprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
[116]757 log_it("biggielist = %s", biggielist);
[3188]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.");
[116]763 } else {
764 log_msg(4, "Reading it...");
[3188]765 for (mr_getline(fname, fin); !feof(fin); mr_getline(fname, fin)) {
[116]766 if (fname[strlen(fname) - 1] <= 32) {
767 fname[strlen(fname) - 1] = '\0';
768 }
769 if (0 == strncmp(fname, "/dev/", 5)) {
[296]770 if (is_dev_an_NTFS_dev(fname)) {
771 if ( !find_home_of_exe("ntfsresize")) {
[3188]772 mr_free(tmp);
773 mr_free(fname);
[296]774 fatal_error("ntfsresize not found");
775 }
[3188]776 mr_free(tmp);
777
778 mr_asprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
[296]779 log_it("command = %s", command);
[3188]780 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output(command));
781 mr_free(command);
782
[296]783 log_it("res of it = %s", tmp);
784 file_len_K = atoll(tmp) / 1024L;
[3188]785 mr_free(tmp);
[296]786 } else {
787 file_len_K = get_phys_size_of_drive(fname) * 1024L;
788 }
[116]789 } else {
[684]790 /* BERLIOS: more than long here ??? */
[116]791 file_len_K = (long) (length_of_file(fname) / 1024);
792 }
793 if (file_len_K > 0) {
794 scratchL += file_len_K;
795 log_msg(4, "%s --> %ld K", fname, file_len_K);
796 }
[3188]797 log_msg(4, "After adding %s, scratchL+%ld now equals %ld", fname, file_len_K, scratchL);
[116]798 if (feof(fin)) {
799 break;
800 }
[3188]801 mr_free(fname);
[116]802 }
[3188]803 mr_free(fname);
[1]804 }
[116]805 log_it("Closing...");
806 paranoid_fclose(fin);
807 log_it("Finished calculating total size of all biggiefiles");
808 return (scratchL);
[1]809}
810
811/**
812 * Determine the amount of space (in KB) occupied by a mounted CD.
813 * This can also be used to find the space used for other directories.
814 * @param mountpt The mountpoint/directory to check.
815 * @return The amount of space occupied in KB.
816 */
[116]817long long space_occupied_by_cd(char *mountpt)
[1]818{
[116]819 /*@ buffer ****************************************************** */
[3188]820 char *tmp = NULL;
821 char *command = NULL;
[116]822 long long llres;
823 /*@ pointers **************************************************** */
824 char *p;
825 FILE *fin;
[1]826
[116]827 /*@ end vars *************************************************** */
[1]828
[3188]829 mr_asprintf(command, "du -sk %s", mountpt);
[816]830 errno = 0;
[116]831 fin = popen(command, "r");
[816]832 if (errno) {
833 log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
834 llres = 0;
835 } else {
[3188]836 mr_getline(tmp, fin);
[816]837 paranoid_pclose(fin);
838 p = strchr(tmp, '\t');
839 if (p) {
840 *p = '\0';
841 }
842 for (p = tmp, llres = 0; *p != '\0'; p++) {
843 llres *= 10;
844 llres += (int) (*p - '0');
845 }
[3188]846 mr_free(tmp);
[116]847 }
[3188]848 mr_free(command);
[816]849
[116]850 return (llres);
[1]851}
852
853
854/**
855 * Update a CRC checksum to include another character.
856 * @param crc The original CRC checksum.
857 * @param c The character to add.
858 * @return The new CRC checksum.
859 * @ingroup utilityGroup
860 */
[116]861unsigned int updcrc(unsigned int crc, unsigned int c)
[1]862{
[116]863 unsigned int tmp;
864 tmp = (crc >> 8) ^ c;
865 crc = (crc << 8) ^ crctttab[tmp & 255];
866 return crc;
[1]867}
868
869/**
870 * Update a reverse CRC checksum to include another character.
871 * @param crc The original CRC checksum.
872 * @param c The character to add.
873 * @return The new CRC checksum.
874 * @ingroup utilityGroup
875 */
[116]876unsigned int updcrcr(unsigned int crc, unsigned int c)
[1]877{
[116]878 unsigned int tmp;
879 tmp = crc ^ c;
880 crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
881 return crc;
[1]882}
883
884
885
886
887/**
888 * Check for an executable on the user's system; write a message to the
889 * screen and the log if we can't find it.
890 * @param fname The executable basename to look for.
891 * @return 0 if it's found, nonzero if not.
892 */
[116]893int whine_if_not_found(char *fname)
[1]894{
[116]895 /*@ buffers *** */
[3188]896 char *command = NULL;
897 int res = 0;
[1]898
[3188]899 mr_asprintf(command, "which %s > /dev/null 2> /dev/null", fname);
900 res = system(command);
901 mr_free(command);
[1]902
[3188]903 if (res) {
904 log_to_screen("Please install '%s'. I cannot find it on your system.", fname);
905 log_to_screen("There may be hyperlink at http://www.mondorescue.org which");
[541]906 log_to_screen("will take you to the relevant (missing) package.");
[116]907 return (1);
908 } else {
909 return (0);
910 }
[1]911}
912
913
914
915
916
917
918/**
919 * Create a data file at @p fname containing @p contents.
920 * The data actually can be multiple lines, despite the name.
921 * @param fname The file to create.
922 * @param contents The data to put in it.
923 * @return 0 for success, 1 for failure.
924 */
[116]925int write_one_liner_data_file(char *fname, char *contents)
[1]926{
[116]927 /*@ pointers *************************************************** */
928 FILE *fout;
929 int res = 0;
[1]930
[116]931 /*@ end vars *************************************************** */
[1]932
[116]933 assert_string_is_neither_NULL_nor_zerolength(fname);
934 if (!contents) {
935 log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
936 }
937 if (!(fout = fopen(fname, "w"))) {
938 log_it("fname=%s");
939 log_OS_error("Unable to openout fname");
940 return (1);
941 }
942 fprintf(fout, "%s\n", contents);
943 paranoid_fclose(fout);
944 return (res);
[1]945}
946
947
948
949/**
950 * Read @p fname into @p contents.
951 * @param fname The file to read.
952 * @param contents Where to put its contents.
953 * @return 0 for success, nonzero for failure.
954 */
[116]955int read_one_liner_data_file(char *fname, char *contents)
[1]956{
[116]957 /*@ pointers *************************************************** */
958 FILE *fin;
959 int res = 0;
960 int i;
[1]961
[116]962 /*@ end vars *************************************************** */
[1]963
[116]964 assert_string_is_neither_NULL_nor_zerolength(fname);
965 if (!contents) {
966 log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
967 }
968 if (!(fin = fopen(fname, "r"))) {
969 log_it("fname=%s", fname);
970 log_OS_error("Unable to openin fname");
971 return (1);
972 }
[3060]973 res = fscanf(fin, "%s\n", contents);
[116]974 i = strlen(contents);
975 if (i > 0 && contents[i - 1] < 32) {
976 contents[i - 1] = '\0';
977 }
978 paranoid_fclose(fin);
[3060]979 res = 0;
[116]980 return (res);
[1]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, untar "mondo_home/payload.tgz"
994 * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore
995 * and post-nuke.tgz (if it exists) to tmpdir, 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 */
[1645]1001void copy_mondo_and_mindi_stuff_to_scratchdir()
[1]1002{
[116]1003 /*@ Char buffers ** */
[3188]1004 char *command = NULL;
1005 char *tmp = NULL;
[116]1006 char old_pwd[MAX_STR_LEN];
[3188]1007 int res = 0;
[1]1008
[3188]1009 mvaddstr_and_log_it(g_currentY, 0, "Copying Mondo's core files to the scratch directory");
[1]1010
[116]1011 log_msg(4, "g_mondo_home='%s'", g_mondo_home);
1012 if (strlen(g_mondo_home) < 2) {
1013 find_and_store_mondoarchives_home(g_mondo_home);
1014 }
[3188]1015 mr_asprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, bkpinfo->scratchdir);
[1]1016
[116]1017 log_msg(4, "command = %s", command);
[3188]1018 res = run_program_and_log_output(command, 1);
1019 mr_free(command);
1020
1021 if (res) {
[116]1022 fatal_error("Failed to copy Mondo's stuff to scratchdir");
1023 }
[1]1024
[3188]1025 mr_asprintf(tmp, "%s/payload.tgz", g_mondo_home);
[116]1026 if (does_file_exist(tmp)) {
[3188]1027 log_it("Untarring payload %s to scratchdir %s", tmp, bkpinfo->scratchdir);
[3060]1028 if (getcwd(old_pwd, MAX_STR_LEN - 1)) {
1029 // FIXME
1030 }
1031 if (chdir(bkpinfo->scratchdir)) {
1032 // FIXME
1033 }
[3188]1034 mr_asprintf(command, "tar -zxvf %s", tmp);
1035 res = run_program_and_log_output(command, FALSE);
1036 if (res) {
1037 mr_free(command);
1038 mr_free(tmp);
[116]1039 fatal_error("Failed to untar payload");
1040 }
[3188]1041 mr_free(command);
[3060]1042 if (chdir(old_pwd)) {
1043 // FIXME
1044 }
[116]1045 }
[3188]1046 mr_free(tmp);
[1]1047
[3188]1048 mr_asprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
[1]1049
[116]1050 if (run_program_and_log_output(command, FALSE)) {
1051 fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
1052 }
[3188]1053 mr_free(command);
[1]1054
[3188]1055 mr_asprintf(tmp, "%s", call_program_and_get_last_line_of_output("which mondorestore"));
[116]1056 if (!tmp[0]) {
[3188]1057 mr_free(tmp);
1058 fatal_error("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
[116]1059 }
[3188]1060
1061 mr_asprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
1062 mr_free(tmp);
1063 res = run_program_and_log_output(command, FALSE);
1064 if (res) {
1065 mr_free(command);
[116]1066 fatal_error("Failed to copy mondorestore to tmpdir");
1067 }
[3188]1068 mr_free(command);
[1]1069
[3188]1070 mr_asprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
[116]1071 paranoid_system(command);
[3188]1072 mr_free(command);
[1]1073
[116]1074 if (bkpinfo->postnuke_tarball[0]) {
[3188]1075 mr_asprintf(command, "cp -f %s %s/post-nuke.tgz", bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
1076 res = run_program_and_log_output(command, FALSE);
1077 mr_free(command);
1078
1079 if (res) {
[116]1080 fatal_error("Unable to copy post-nuke tarball to tmpdir");
1081 }
1082 }
[1]1083
[116]1084 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[1]1085}
1086
1087
1088
1089
1090
1091/**
[2380]1092 * Store the client's NETFS configuration in files to be restored at restore-time.
1093 * Assumes that @c bkpinfo->media_type = netfs, but does not check for this.
[1]1094 * @param bkpinfo The backup information structure. Fields used:
[2380]1095 * - @c netfs_mount
1096 * - @c netfs_remote_dir
[1]1097 * - @c tmpdir
1098 */
[2380]1099void store_netfs_config()
[1]1100{
1101
[116]1102 /*@ buffers ******** */
[3188]1103 char *netfs_dev = NULL;
1104 char *netfs_client_hwaddr = NULL;
1105 char *netfs_mount = NULL;
1106 char *netfs_client_ipaddr = NULL;
1107 char *netfs_client_netmask = NULL;
1108 char *netfs_client_broadcast = NULL;
1109 char *netfs_client_defgw = NULL;
1110 char *netfs_server_ipaddr = NULL;
1111 char *tmp = NULL;
1112 char *command = NULL;
[1]1113
[116]1114 /*@ pointers ***** */
1115 char *p;
[1]1116
[3188]1117 if (! bkpinfo->netfs_mount) {
1118 fatal_error("No netfs_mount found !");
1119 }
1120
[2380]1121 log_it("Storing Network configuration");
[3188]1122 mr_asprintf(tmp, "%s", bkpinfo->netfs_mount);
[116]1123 p = strchr(tmp, ':');
1124 if (!p) {
[3188]1125 fatal_error("Network mount doesn't have a colon in it, e.g. 192.168.1.4:/home/nfs");
[116]1126 }
1127 *(p++) = '\0';
[3188]1128 mr_asprintf(netfs_server_ipaddr, "%s", tmp);
1129 mr_asprintf(netfs_mount, "%s", p);
1130 mr_free(tmp);
[1236]1131
[802]1132 /* BERLIOS : there is a bug #67 here as it only considers the first NIC */
[3188]1133 mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\n' | head -n1 | cut -d' ' -f1");
1134 mr_asprintf(netfs_dev, "%s", call_program_and_get_last_line_of_output(command));
1135 mr_free(command);
1136
1137 mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f7 | cut -d':' -f2");
1138 mr_asprintf(netfs_client_ipaddr, "%s", call_program_and_get_last_line_of_output(command));
1139 mr_free(command);
1140
1141 mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f9 | cut -d':' -f2");
1142 mr_asprintf(netfs_client_netmask, "%s", call_program_and_get_last_line_of_output(command));
1143 mr_free(command);
1144
1145 mr_asprintf(command, "%s", "ifconfig | tr '\n' '#' | sed s/##// | tr '#' ' ' | tr '' '\\n' | head -n1 | tr -s '\t' ' ' | cut -d' ' -f8 | cut -d':' -f2");
1146 mr_asprintf(netfs_client_broadcast, "%s", call_program_and_get_last_line_of_output(command));
1147 mr_free(command);
1148
1149 mr_asprintf(command, "%s", "route -n | grep '^0.0.0.0' | awk '{print $2}'");
1150 mr_asprintf(netfs_client_defgw, "%s", call_program_and_get_last_line_of_output(command));
1151 mr_free(command);
1152
1153 mr_asprintf(command, "ifconfig %s | head -1 | awk '{print $5}'", netfs_dev);
1154 mr_asprintf(netfs_client_hwaddr, "%s" call_program_and_get_last_line_of_output(command));
1155 mr_free(command);
1156
1157 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]1158 if (strlen(netfs_dev) < 2) {
[3188]1159 fatal_error("Unable to find ethN (eth0, eth1, ...) adapter via Network mount you specified.");
[116]1160 }
[3188]1161
[802]1162 /********
[3188]1163 * If the Network device found above is a bonded device,
[802]1164 * we need to replace it with an ethN device or the
[2380]1165 * networking will not start during an Network restore.
[802]1166 *
[2380]1167 * If the Network device in netfs_dev begins with the word "bond", or alb or aft
1168 * look for the corresponding slave ethN device and copy it to netfs_dev.
[802]1169 * Using the common MAC address
1170 ********/
[2380]1171 if (!strncmp(netfs_dev, "bond", 4) || !strncmp(netfs_dev, "alb", 3) || !strncmp(netfs_dev, "aft", 3)) {
1172 log_to_screen("Found bonding device %s; looking for corresponding ethN slave device\n", netfs_dev);
[3188]1173 mr_asprintf(command, "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1",netfs_client_hwaddr,netfs_dev);
1174 mr_asprintf(netfs_dev, "%s", call_program_and_get_last_line_of_output(command));
1175 mr_free(command);
[2380]1176 log_to_screen("Replacing it with %s\n", netfs_dev);
[802]1177 }
[1]1178
[3188]1179 mr_asprintf(tmp, "%s/NETFS-DEV", bkpinfo->tmpdir);
[2380]1180 write_one_liner_data_file(tmp, netfs_dev);
[3188]1181 mr_free(netfs_dev);
1182 mr_free(tmp);
[1]1183
[3188]1184 mr_asprintf(tmp, "%s/NETFS-CLIENT-HWADDR", bkpinfo->tmpdir);
1185 write_one_liner_data_file(tmp, netfs_client_hwaddr);
1186 mr_free(netfs_client_hwaddr);
1187 mr_free(tmp);
[2380]1188
[3188]1189 mr_asprintf(tmp, "%s/NETFS-CLIENT-IPADDR", bkpinfo->tmpdir);
[2380]1190 write_one_liner_data_file(tmp, netfs_client_ipaddr);
[3188]1191 mr_free(netfs_client_ipaddr);
1192 mr_free(tmp);
1193
1194 mr_asprintf(tmp, "%s/NETFS-CLIENT-NETMASK", bkpinfo->tmpdir);
[2380]1195 write_one_liner_data_file(tmp, netfs_client_netmask);
[3188]1196 mr_free(netfs_client_netmask);
1197 mr_free(tmp);
1198
1199 mr_asprintf(tmp, "%s/NETFS-CLIENT-BROADCAST", bkpinfo->tmpdir);
[2380]1200 write_one_liner_data_file(tmp, netfs_client_broadcast);
[3188]1201 mr_free(netfs_client_broadcast);
1202 mr_free(tmp);
1203
1204 mr_asprintf(tmp, "%s/NETFS-CLIENT-DEFGW", bkpinfo->tmpdir);
[2380]1205 write_one_liner_data_file(tmp, netfs_client_defgw);
[3188]1206 mr_free(netfs_client_defgw);
1207 mr_free(tmp);
1208
1209 mr_asprintf(tmp, "%s/NETFS-SERVER-IPADDR", bkpinfo->tmpdir);
[2380]1210 write_one_liner_data_file(tmp, netfs_server_ipaddr);
[3188]1211 mr_free(netfs_server_ipaddr);
1212 mr_free(tmp);
1213
1214 mr_asprintf(tmp, "%s/NETFS-SERVER-MOUNT", bkpinfo->tmpdir);
[2380]1215 write_one_liner_data_file(tmp, bkpinfo->netfs_mount);
[3188]1216 mr_free(tmp);
1217
[2847]1218 if (bkpinfo->netfs_user) {
[3188]1219 mr_asprintf(tmp, "%s/NETFS-SERVER-USER", bkpinfo->tmpdir);
[2847]1220 write_one_liner_data_file(tmp, bkpinfo->netfs_user);
[3188]1221 mr_free(tmp);
[2847]1222 }
[3188]1223 mr_asprintf(tmp, "%s/NETFS-SERVER-PATH", bkpinfo->tmpdir);
[2380]1224 write_one_liner_data_file(tmp, bkpinfo->netfs_remote_dir);
[3188]1225 mr_free(tmp);
1226
1227 mr_asprintf(tmp, "%s/ISO-PREFIX", bkpinfo->tmpdir);
[148]1228 write_one_liner_data_file(tmp, bkpinfo->prefix);
[3188]1229 mr_free(tmp);
[1]1230
[3188]1231 sprintf(tmp, "%s/NETFS-PROTO", bkpinfo->tmpdir);
1232 write_one_liner_data_file(tmp, bkpinfo->netfs_proto);
1233 mr_free(tmp);
[1]1234
1235
[3188]1236 log_it("Finished storing Network configuration");
1237}
[1]1238
1239
1240/**
1241 * Determine the approximate number of media that the backup will take up,
1242 * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K()
1243 * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as
1244 * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any
1245 * "imagedevs", the estimate is not shown as it will be wildly inaccurate.
1246 * If there are more than 50 media estimated, the estimate will not be shown.
1247 * @param bkpinfo The backup information structure. Fields used:
1248 * - @c bkpinfo->backup_media_type
1249 * - @c bkpinfo->image_devs
1250 * - @c bkpinfo->media_size
1251 * - @c bkpinfo->optimal_set_size
1252 * - @c bkpinfo->use_lzo
1253 * @param noof_sets The number of filesets created.
1254 * @ingroup archiveGroup
1255 */
1256void
[1645]1257estimate_noof_media_required(long noof_sets)
[1]1258{
[116]1259 /*@ buffers *************** */
[3188]1260 char *tmp = NULL;
[2242]1261 char *mds = NULL;
[1]1262
[116]1263 /*@ long long ************* */
1264 long long scratchLL;
[1]1265
[3150]1266 if (bkpinfo->media_size <= 0) {
[116]1267 log_to_screen("Number of media required: UNKNOWN");
1268 return;
1269 }
[1]1270
[116]1271 log_it("Estimating number of media required...");
[3188]1272 scratchLL = (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size) + (long long) (size_of_all_biggiefiles_K());
[3150]1273 scratchLL = (scratchLL / 1024) / bkpinfo->media_size;
[116]1274 scratchLL++;
1275 if (bkpinfo->use_lzo) {
1276 scratchLL = (scratchLL * 2) / 3;
[1002]1277 } else if (bkpinfo->use_gzip) {
[998]1278 scratchLL = (scratchLL * 2) / 3;
[3188]1279 } else if (bkpinfo->use_lzma) {
1280 scratchLL = (scratchLL * 2) / 3;
[116]1281 } else {
1282 scratchLL = scratchLL / 2;
1283 }
1284 if (!scratchLL) {
1285 scratchLL++;
1286 }
1287 if (scratchLL <= 1) {
[2242]1288 mds = media_descriptor_string(bkpinfo->backup_media_type);
[3188]1289 mr_asprintf(tmp, "Your backup will probably occupy a single %s. Maybe two.", mds);
[2242]1290 mr_free(mds);
[116]1291 } else if (scratchLL > 4) {
[3188]1292 mr_asprintf(tmp, "Your backup will occupy one meeeeellion media! (maybe %s)", number_to_text((int) (scratchLL + 1)));
[116]1293 } else {
[3188]1294 mr_asprintf(tmp, "Your backup will occupy approximately %s media.", number_to_text((int) (scratchLL + 1)));
[116]1295 }
[3188]1296 if (scratchLL < 50) {
[116]1297 log_to_screen(tmp);
1298 }
[3188]1299 mr_free(tmp);
[1]1300}
1301
1302
1303/**
1304 * Determine whether a file is compressed. This is done
1305 * by reading through the "do-not-compress-these" file distributed with Mondo.
1306 * @param filename The file to check.
1307 * @return TRUE if it's compressed, FALSE if not.
1308 */
[116]1309bool is_this_file_compressed(char *filename)
[1]1310{
[3188]1311 char *do_not_compress_these = NULL;
1312 char *tmp = NULL;
[116]1313 char *p;
[1236]1314 char *q = NULL;
[1]1315
[1236]1316 q = strrchr(filename, '.');
1317 if (q == NULL) {
1318 return (FALSE);
1319 }
1320
[3188]1321 mr_asprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
[116]1322 if (!does_file_exist(tmp)) {
[3188]1323 mr_free(tmp);
[116]1324 return (FALSE);
1325 }
[1236]1326 /* BERLIOS: This is just plain WRONG !! */
[3188]1327 mr_asprintf(do_not_compress_these,"%s", last_line_of_file(tmp));
1328 mr_free(tmp);
[1236]1329
[116]1330 for (p = do_not_compress_these; p != NULL; p++) {
[3188]1331 mr_asprintf(tmp, "%s", p);
[116]1332 if (strchr(tmp, ' ')) {
1333 *(strchr(tmp, ' ')) = '\0';
1334 }
[1236]1335 if (!strcmp(q, tmp)) {
[3188]1336 mr_free(tmp);
1337 mr_free(do_not_compress_these);
[116]1338 return (TRUE);
1339 }
1340 if (!(p = strchr(p, ' '))) {
1341 break;
1342 }
[3188]1343 mr_free(tmp);
[116]1344 }
[3188]1345 mr_free(do_not_compress_these);
[116]1346 return (FALSE);
[1]1347}
1348
1349/* @} - end fileGroup */
Note: See TracBrowser for help on using the repository browser.