source: MondoRescue/branches/3.3/mondo/src/common/libmondo-files.c@ 3867

Last change on this file since 3867 was 3867, checked in by Bruno Cornec, 5 months ago

Fix find_home_of_exe calls + errors

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