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

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