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

Last change on this file since 3148 was 3148, checked in by Bruno Cornec, 11 years ago

2nd phase for svn merge -r 2935:3146 ../3.0

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