source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-files.c@ 2291

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