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

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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