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

Last change on this file since 3875 was 3874, checked in by Bruno Cornec, 3 months ago

remove differentiated support for cdrw

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