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

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

remove differentiated support for cdrw

  • Property svn:keywords set to Id
File size: 30.8 KB
RevLine 
[2053]1/* file manipulation
[116]2 $Id: libmondo-files.c 3874 2024-03-07 18:23:09Z bruno $
[1]3*/
4
5/**
6 * @file
7 * Functions to manipulate files.
8 */
9
10
11#include "my-stuff.h"
[2242]12#include "mr_mem.h"
[3613]13#include "mr_file.h"
[1]14#include "mondostructures.h"
[3871]15
[1]16#include "libmondo-tools-EXT.h"
[541]17#include "libmondo-gui-EXT.h"
[1]18#include "libmondo-devices-EXT.h"
19#include "libmondo-fork-EXT.h"
20#include "libmondo-string-EXT.h"
[3874]21#include "newt-specific-EXT.h"
[1]22
23/*@unused@*/
[116]24//static char cvsid[] = "$Id: libmondo-files.c 3874 2024-03-07 18:23:09Z bruno $";
[1]25
26extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
27
28extern int g_currentY;
29extern char *g_mondo_home;
30
[3350]31extern bool g_text_mode;
32
[1645]33/* Reference to global bkpinfo */
34extern struct s_bkpinfo *bkpinfo;
35
[1]36/**
[3867]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/**
[1]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 */
[116]71char *calc_checksum_of_file(char *filename)
[1]72{
[116]73 /*@ buffers ***************************************************** */
74 static char output[MAX_STR_LEN];
[3191]75 char *command = NULL;
[1]76
[116]77 /*@ pointers **************************************************** */
[3191]78 char *p = NULL;
[3060]79 char *q;
[116]80 FILE *fin;
[1]81
[116]82 /*@ initialize pointers ***************************************** */
[1]83
[116]84 p = output;
[1]85
[116]86 /*@************************************************************** */
[1]87
[116]88 assert_string_is_neither_NULL_nor_zerolength(filename);
89 if (does_file_exist(filename)) {
[3191]90 mr_asprintf(command, "md5sum \"%s\"", filename);
[116]91 fin = popen(command, "r");
92 if (fin) {
[3060]93 q = fgets(output, MAX_STR_LEN, fin);
94 if (!q) {
95 // FIXME
96 }
97 q = strchr(output, ' ');
[116]98 paranoid_pclose(fin);
99 }
[3191]100 mr_free(command);
[116]101 } else {
[3191]102 log_it("File '%s' not found; cannot calc checksum", filename);
[1]103 }
[116]104 if (p) {
105 *p = '\0';
106 }
107 return (output);
[1]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 */
[116]118char *calc_file_ugly_minichecksum(char *curr_fname)
[1]119{
120
[116]121 /*@ buffers ***************************************************** */
122 static char curr_cksum[1000];
[1]123
[116]124 /*@ pointers **************************************************** */
[1]125
[116]126 /*@ structures ************************************************** */
127 struct stat buf;
[1]128
[116]129 /*@ initialize data *************************************************** */
130 curr_cksum[0] = '\0';
[1]131
[116]132 /*@************************************************************** */
[1]133
[116]134 assert_string_is_neither_NULL_nor_zerolength(curr_fname);
135 if (lstat(curr_fname, &buf)) {
136 return (curr_cksum); // empty
137 }
[1]138
[116]139 sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
140 (long) (buf.st_mtime), (long) (buf.st_ctime));
141 return (curr_cksum);
[1]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 */
[116]152long count_lines_in_file(char *filename)
[1]153{
154
[116]155 /*@ buffers ***************************************************** */
[3191]156 char *command = NULL;
157 char *incoming = NULL;
[1]158
[116]159 /*@ long ******************************************************** */
160 long noof_lines = -1L;
[1]161
[116]162 /*@ pointers **************************************************** */
163 FILE *fin;
[1]164
[116]165 assert_string_is_neither_NULL_nor_zerolength(filename);
166 if (!does_file_exist(filename)) {
[3191]167 log_it("%s does not exist, so I cannot found the number of lines in it", filename);
[116]168 return (0);
[1]169 }
[3191]170 mr_asprintf(command, "cat %s | wc -l", filename);
[116]171 if (!does_file_exist(filename)) {
[3191]172 mr_free(command);
[116]173 return (-1);
[1]174 }
[116]175 fin = popen(command, "r");
[3191]176 mr_free(command);
177
[116]178 if (fin) {
179 if (feof(fin)) {
180 noof_lines = 0;
181 } else {
[3191]182 mr_getline(incoming, fin);
183 while (strlen(incoming) > 0 && incoming[strlen(incoming) - 1] < 32) {
[116]184 incoming[strlen(incoming) - 1] = '\0';
185 }
186 noof_lines = atol(incoming);
[3191]187 mr_free(incoming);
[116]188 }
189 paranoid_pclose(fin);
190 }
191 return (noof_lines);
[1]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 */
[116]206void exclude_nonexistent_files(char *inout)
[1]207{
[3191]208 char *infname = NULL;
209 char *outfname = NULL;
210 char *tmp = NULL;
211 char *incoming = NULL;
[1]212
[116]213 /*@ int ********************************************************* */
214 int i;
[1]215
[116]216 /*@ pointers **************************************************** */
217 FILE *fin, *fout;
[1]218
219
[116]220 /*@ end vars *********************************************************** */
[1]221
[116]222 assert_string_is_neither_NULL_nor_zerolength(inout);
[3191]223 mr_asprintf(infname, "%s.in", inout);
224 mr_asprintf(tmp, "cp -f %s %s", inout, infname);
[116]225 run_program_and_log_output(tmp, FALSE);
[3191]226 mr_free(tmp);
227
[116]228 if (!(fin = fopen(infname, "r"))) {
229 log_OS_error("Unable to openin infname");
[3191]230 mr_free(infname);
[116]231 return;
[1]232 }
[3191]233
234 mr_asprintf(outfname, "%s", inout);
[116]235 if (!(fout = fopen(outfname, "w"))) {
236 log_OS_error("Unable to openout outfname");
[3191]237 mr_free(infname);
238 mr_free(outfname);
[116]239 return;
[1]240 }
[3191]241 mr_free(outfname);
242
243 for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
[116]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 {
[3191]251 log_it("Excluding '%s'-nonexistent\n", incoming);
[116]252 }
[3191]253 mr_free(incoming);
[1]254 }
[3191]255 mr_free(incoming);
256
[116]257 paranoid_fclose(fout);
258 paranoid_fclose(fin);
259 unlink(infname);
[3191]260 mr_free(infname);
[1]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.
[3829]269 * @return 0 for success aborts otherwise
[1]270 */
[116]271int figure_out_kernel_path_interactively_if_necessary(char *kernel)
[1]272{
[3191]273 char *tmp = NULL;
274 char *command = NULL;
[1]275
[3829]276 if (kernel == NULL) {
277 kernel = call_program_and_get_last_line_of_output("mindi --findkernel 2> /dev/null");
[116]278 }
[392]279 // If we didn't get anything back, check whether mindi raised a fatal error
[3829]280 if (strcmp(kernel,"") == 0) {
[3292]281 mr_asprintf(command, "grep 'Fatal error' %s", MINDI_LOGFILE);
[3610]282 tmp = call_program_and_get_last_line_of_output(command);
[392]283 if (strlen(tmp) > 1) {
284 popup_and_OK(tmp);
[3191]285 mr_free(tmp);
286 mr_free(command);
[3292]287 fatal_error("Mindi gave a fatal error. Please check "MINDI_LOGFILE);
[392]288 }
[3191]289 mr_free(tmp);
290 mr_free(command);
[3350]291 } else {
292 // If we've found the kernel, allow sysadmin to modify it if he wants
[3829]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 }
[3350]302 }
303 }
[392]304 }
[3350]305 log_it("User says kernel is at %s", kernel);
306
[116]307 log_it("Calling Mindi with kernel path of '%s'", kernel);
[3829]308 return(0);
[1]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).
[3866]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
[1]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 */
[3866]321char *find_home_of_exe(const char *fname)
[1]322{
[116]323 /*@ buffers ********************* */
[3866]324 char *output = NULL;
[3191]325 char *command = NULL;
[1]326
[116]327 /*@******************************* */
[1]328
[116]329 assert_string_is_neither_NULL_nor_zerolength(fname);
[3866]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);
[3191]334 mr_free(command);
335
[3866]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);
[3191]341 mr_free(command);
[116]342 }
[3191]343
[116]344 if (output[0] != '\0' && does_file_exist(output)) {
[3866]345 log_msg(4, "find_home_of_exe --- Found %s at %s", fname, output);
346 return(output);
[116]347 } else {
[3194]348 log_msg(4, "find_home_of_exe --- Could not find %s", fname);
349 return(NULL);
[116]350 }
[1]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 */
[116]360int get_trackno_from_logfile(char *logfile)
[1]361{
362
[116]363 /*@ pointers ********************************************************* */
364 FILE *fin;
[1]365
[116]366 /*@ int ************************************************************** */
367 int trackno = 0;
368 size_t len = 0;
[1]369
[116]370 /*@ buffer ************************************************************ */
371 char datablock[32701];
[1]372
[116]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);
[1]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 */
[116]402int grab_percentage_from_last_line_of_file(char *filename)
[1]403{
404
[116]405 /*@ buffers ***************************************************** */
[3191]406 char *lastline = NULL;
407 char *command = NULL;
[116]408 /*@ pointers **************************************************** */
[3191]409 char *p = NULL;
[1]410
[116]411 /*@ int's ******************************************************* */
[3191]412 int i = 0;
[1]413
[3191]414 for (i = NOOF_ERR_LINES - 1; i >= 0 && !strstr(err_log_lines[i], "% Done") && !strstr(err_log_lines[i], "% done"); i--);
[116]415 if (i < 0) {
[3191]416 mr_asprintf(command, "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'", filename, '%');
[3610]417 lastline = call_program_and_get_last_line_of_output(command);
[3191]418 mr_free(command);
[116]419 if (!lastline[0]) {
[3191]420 mr_free(lastline);
[3194]421 return(0);
[116]422 }
423 } else {
[3191]424 mr_asprintf(lastline, "%s", err_log_lines[i]);
[1]425 }
426
[116]427 p = strrchr(lastline, '%');
428 if (p) {
429 *p = '\0';
[3191]430 } else {
431 mr_free(lastline);
[3194]432 return(0);
[116]433 }
[3191]434
435 for (p--; isdigit(*p) && p != lastline; p--);
[116]436 if (p != lastline) {
437 p++;
438 }
439 i = atoi(p);
[3191]440 mr_free(lastline);
[3194]441 return(i);
[1]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 */
[116]454char *last_line_of_file(char *filename)
[1]455{
[116]456 /*@ buffers ***************************************************** */
457 static char output[MAX_STR_LEN];
[3191]458 char *command = NULL;
[3194]459 char *p = NULL;
[1]460
[116]461 /*@ pointers **************************************************** */
462 FILE *fin;
[1]463
[116]464 /*@ end vars **************************************************** */
[1]465
[116]466 if (!does_file_exist(filename)) {
[3194]467 log_it("Trying to get last line of nonexistent file (%s)", filename);
[116]468 output[0] = '\0';
469 return (output);
470 }
[3191]471 mr_asprintf(command, "tail -n1 %s", filename);
[116]472 fin = popen(command, "r");
[3191]473 mr_free(command);
[3060]474 p = fgets(output, MAX_STR_LEN, fin);
475 if (p == NULL) {
476 // FIXME
477 }
[116]478 paranoid_pclose(fin);
479 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
480 output[strlen(output) - 1] = '\0';
481 }
[3194]482 return(output);
[1]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 */
[684]490off_t length_of_file(char *filename)
[1]491{
[116]492 /*@ pointers *************************************************** */
493 FILE *fin;
[1]494
[116]495 /*@ long long ************************************************* */
[684]496 off_t length;
[1]497
[116]498 fin = fopen(filename, "r");
499 if (!fin) {
500 log_it("filename=%s", filename);
501 log_OS_error("Unable to openin filename");
[3194]502 return(-1);
[116]503 }
[415]504 fseeko(fin, 0, SEEK_END);
[684]505 length = ftello(fin);
[116]506 paranoid_fclose(fin);
[3194]507 return(length);
[1]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 */
[3191]516int make_checksum_list_file(char *filelist, char *cksumlist, char *comppath) {
[116]517 /*@ pointers **************************************************** */
518 FILE *fin;
519 FILE *fout;
[1]520
[116]521 /*@ int ******************************************************* */
522 int percentage;
523 int i;
524 int counter = 0;
[1]525
[116]526 /*@ buffer ****************************************************** */
[3191]527 char *stub_fname = NULL;
528 char *curr_fname = NULL;
529 char *curr_cksum = NULL;
530 char *tmp = NULL;
[1]531
[116]532 /*@ long [long] ************************************************* */
[684]533 off_t filelist_length;
534 off_t curr_pos;
[116]535 long start_time;
536 long current_time;
537 long time_taken;
538 long time_remaining;
[1]539
[116]540 /*@ end vars *************************************************** */
[1]541
[116]542 start_time = get_time();
543 filelist_length = length_of_file(filelist);
[3191]544 log_it("filelist = %s; cksumlist = %s", filelist, cksumlist);
545
[116]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");
[3194]550 return(1);
[1]551 }
[116]552 fout = fopen(cksumlist, "w");
553 if (fout == NULL) {
554 log_OS_error("Unable to openout cksumlist");
555 paranoid_fclose(fin);
[541]556 log_to_screen("Can't open checksum list");
[3194]557 return(1);
[1]558 }
[3191]559 for (mr_getline(stub_fname, fin); !feof(fin); mr_getline(stub_fname, fin)) {
[3194]560 i = strlen(stub_fname) - 1;
561 if (stub_fname[i] < 32) {
[116]562 stub_fname[i] = '\0';
563 }
[3191]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));
[116]571 fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
[3191]572 mr_free(curr_cksum);
573
[116]574 if (counter++ > 12) {
575 current_time = get_time();
576 counter = 0;
[3430]577 /* TODO: 37 really ? */
[116]578 curr_fname[37] = '\0';
[684]579 curr_pos = ftello(fin) / 1024;
[116]580 percentage = (int) (curr_pos * 100 / filelist_length);
581 time_taken = current_time - start_time;
[3191]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);
[116]585 }
586 sync();
587 }
[3191]588 mr_free(curr_fname);
[116]589 }
[3191]590 mr_free(stub_fname);
591
[116]592 paranoid_fclose(fout);
593 paranoid_fclose(fin);
594 log_it("Done.");
[3194]595 return(0);
[1]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 */
[3191]604int make_hole_for_dir(const char *outdir_fname)
[1]605{
[3191]606 char *tmp = NULL;
[116]607 int res = 0;
[1]608
[116]609 assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
[3191]610 mr_asprintf(tmp, "mkdir -p %s", outdir_fname);
[116]611 res = system(tmp);
[3191]612 mr_free(tmp);
[3194]613 return(res);
[1]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 */
[116]623int make_hole_for_file(char *outfile_fname)
[1]624{
[116]625 /*@ buffer ****************************************************** */
[3191]626 char *command = NULL;
[1]627
[116]628 /*@ int ******************************************************** */
629 int res = 0;
[1]630
[116]631 /*@ end vars *************************************************** */
[1]632
[116]633 assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
634 assert(!strstr(outfile_fname, MNT_CDROM));
635 assert(!strstr(outfile_fname, "/dev/cdrom"));
[3191]636 mr_asprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
[116]637 res += system(command);
[3191]638 mr_free(command);
639
640 mr_asprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
[116]641 res += system(command);
[3191]642 mr_free(command);
643
644 mr_asprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
[116]645 res += system(command);
[3191]646 mr_free(command);
647
[116]648 unlink(outfile_fname);
[3194]649 return(0);
[1]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 */
[116]659long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
[1]660{
[116]661 /*@ long ******************************************************* */
662 long matches = 0;
[1]663
[116]664 /*@ pointers *************************************************** */
665 FILE *fin;
[1]666
[116]667 /*@ buffers **************************************************** */
[3191]668 char *incoming = NULL;
[1]669
[116]670 /*@ end vars *************************************************** */
[1]671
672
[116]673 fin = fopen(filelist_fname, "r");
[1]674
[116]675 if (!fin) {
676 log_OS_error("Unable to openin filelist_fname");
[3194]677 return(0);
[1]678 }
[3191]679 mr_getline(incoming, fin);
680 while (!feof(fin)) {
[116]681 if (strstr(incoming, wildcard)) {
682 matches++;
683 }
[3191]684 mr_free(incoming);
685 mr_getline(incoming, fin);
[116]686 }
[3191]687 mr_free(incoming);
[116]688 paranoid_fclose(fin);
[3194]689 return(matches);
[1]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 */
[116]700long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
[1]701{
[3191]702 char *command = NULL;
703 char *mountlist = NULL;
704 char *sz_res = NULL;
[116]705 long file_len_K;
706
[3191]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
[116]711 log_it(command);
[3610]712 sz_res = call_program_and_get_last_line_of_output(command);
[116]713 file_len_K = atol(sz_res);
714 log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
[3191]715 mr_free(sz_res);
716 mr_free(command);
717
[3194]718 return(file_len_K);
[1]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 */
[1645]726long size_of_all_biggiefiles_K()
[1]727{
[116]728 /*@ buffers ***************************************************** */
[3191]729 char *fname = NULL;
730 char *biggielist = NULL;
731 char *tmp = NULL;
732 char *command = NULL;
[1]733
[116]734 /*@ long ******************************************************** */
735 long scratchL = 0;
736 long file_len_K;
[1]737
[116]738 /*@ pointers *************************************************** */
739 FILE *fin = NULL;
[1]740
[116]741 /*@ end vars *************************************************** */
[1]742
[116]743 log_it("Calculating size of all biggiefiles (in total)");
[3191]744 mr_asprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
[116]745 log_it("biggielist = %s", biggielist);
[3191]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.");
[116]751 } else {
752 log_msg(4, "Reading it...");
[3191]753 for (mr_getline(fname, fin); !feof(fin); mr_getline(fname, fin)) {
[116]754 if (fname[strlen(fname) - 1] <= 32) {
755 fname[strlen(fname) - 1] = '\0';
756 }
757 if (0 == strncmp(fname, "/dev/", 5)) {
[296]758 if (is_dev_an_NTFS_dev(fname)) {
[3866]759 if ((tmp = find_home_of_exe("ntfsresize")) == NULL) {
[3191]760 mr_free(tmp);
761 mr_free(fname);
[296]762 fatal_error("ntfsresize not found");
763 }
[3866]764 mr_asprintf(command, "%s --force --info %s|grep '^You might resize at '|cut -d' ' -f5", tmp, fname);
[3191]765 mr_free(tmp);
[296]766 log_it("command = %s", command);
[3610]767 tmp = call_program_and_get_last_line_of_output(command);
[3191]768 mr_free(command);
769
[296]770 log_it("res of it = %s", tmp);
771 file_len_K = atoll(tmp) / 1024L;
[3191]772 mr_free(tmp);
[296]773 } else {
774 file_len_K = get_phys_size_of_drive(fname) * 1024L;
775 }
[116]776 } else {
[3430]777 /* TODO: more than long here ??? */
[116]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 }
[3191]784 log_msg(4, "After adding %s, scratchL+%ld now equals %ld", fname, file_len_K, scratchL);
[116]785 if (feof(fin)) {
786 break;
787 }
[3191]788 mr_free(fname);
[116]789 }
[3191]790 mr_free(fname);
[1]791 }
[116]792 log_it("Closing...");
793 paranoid_fclose(fin);
794 log_it("Finished calculating total size of all biggiefiles");
[3206]795 log_it("Total size of all biggiefiles = %ld Kb", scratchL);
[3194]796 return(scratchL);
[1]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 */
[116]805long long space_occupied_by_cd(char *mountpt)
[1]806{
[116]807 /*@ buffer ****************************************************** */
[3191]808 char *tmp = NULL;
809 char *command = NULL;
[116]810 long long llres;
811 /*@ pointers **************************************************** */
812 char *p;
813 FILE *fin;
[1]814
[116]815 /*@ end vars *************************************************** */
[1]816
[3191]817 mr_asprintf(command, "du -sk %s", mountpt);
[816]818 errno = 0;
[116]819 fin = popen(command, "r");
[816]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 {
[3191]824 mr_getline(tmp, fin);
[816]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 }
[3191]834 mr_free(tmp);
[116]835 }
[3191]836 mr_free(command);
[816]837
[3194]838 return(llres);
[1]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 */
[116]849unsigned int updcrc(unsigned int crc, unsigned int c)
[1]850{
[116]851 unsigned int tmp;
852 tmp = (crc >> 8) ^ c;
853 crc = (crc << 8) ^ crctttab[tmp & 255];
[3194]854 return(crc);
[1]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 */
[116]864unsigned int updcrcr(unsigned int crc, unsigned int c)
[1]865{
[116]866 unsigned int tmp;
867 tmp = crc ^ c;
868 crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
[3194]869 return(crc);
[1]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 */
[3867]881int whine_if_not_found(const char *fname)
[1]882{
[116]883 /*@ buffers *** */
[3866]884 char *tmp = NULL;
[1]885
[3866]886 tmp = find_home_of_exe(fname);
887 if (tmp == NULL) {
[3191]888 log_to_screen("Please install '%s'. I cannot find it on your system.", fname);
[3194]889 return(1);
[116]890 } else {
[3866]891 mr_free(tmp);
[3194]892 return(0);
[116]893 }
[1]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 */
[116]905int write_one_liner_data_file(char *fname, char *contents)
[1]906{
[116]907 /*@ pointers *************************************************** */
908 FILE *fout;
909 int res = 0;
[1]910
[116]911 /*@ end vars *************************************************** */
[1]912
[116]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);
[1]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 */
[116]935int read_one_liner_data_file(char *fname, char *contents)
[1]936{
[116]937 /*@ pointers *************************************************** */
938 FILE *fin;
939 int res = 0;
940 int i;
[1]941
[116]942 /*@ end vars *************************************************** */
[1]943
[116]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");
[3194]951 return(1);
[116]952 }
[3060]953 res = fscanf(fin, "%s\n", contents);
[116]954 i = strlen(contents);
955 if (i > 0 && contents[i - 1] < 32) {
956 contents[i - 1] = '\0';
957 }
958 paranoid_fclose(fin);
[3060]959 res = 0;
[3194]960 return(res);
[1]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 */
[1645]978void copy_mondo_and_mindi_stuff_to_scratchdir()
[1]979{
[116]980 /*@ Char buffers ** */
[3191]981 char *command = NULL;
982 char *tmp = NULL;
[3613]983 char *old_pwd = NULL;
[3191]984 int res = 0;
[1]985
[3191]986 mvaddstr_and_log_it(g_currentY, 0, "Copying Mondo's core files to the scratch directory");
[1]987
[116]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 }
[3191]992 mr_asprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, bkpinfo->scratchdir);
[1]993
[116]994 log_msg(4, "command = %s", command);
[3191]995 res = run_program_and_log_output(command, 1);
996 mr_free(command);
997
998 if (res) {
[116]999 fatal_error("Failed to copy Mondo's stuff to scratchdir");
1000 }
[1]1001
[3191]1002 mr_asprintf(tmp, "%s/payload.tgz", g_mondo_home);
[116]1003 if (does_file_exist(tmp)) {
[3191]1004 log_it("Untarring payload %s to scratchdir %s", tmp, bkpinfo->scratchdir);
[3613]1005 old_pwd = mr_getcwd();
[3060]1006 if (chdir(bkpinfo->scratchdir)) {
1007 // FIXME
1008 }
[3191]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);
[116]1014 fatal_error("Failed to untar payload");
1015 }
[3191]1016 mr_free(command);
[3060]1017 if (chdir(old_pwd)) {
1018 // FIXME
1019 }
[3613]1020 mr_free(old_pwd);
[116]1021 }
[3191]1022 mr_free(tmp);
[1]1023
[3191]1024 mr_asprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
[1]1025
[116]1026 if (run_program_and_log_output(command, FALSE)) {
1027 fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
1028 }
[3191]1029 mr_free(command);
[1]1030
[3610]1031 tmp = call_program_and_get_last_line_of_output("which mondorestore");
[116]1032 if (!tmp[0]) {
[3191]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?");
[116]1035 }
[3191]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);
[116]1042 fatal_error("Failed to copy mondorestore to tmpdir");
1043 }
[3191]1044 mr_free(command);
[1]1045
[3191]1046 mr_asprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
[116]1047 paranoid_system(command);
[3191]1048 mr_free(command);
[1]1049
[3826]1050 if (bkpinfo->postnuke_tarball != NULL) {
[3191]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) {
[116]1056 fatal_error("Unable to copy post-nuke tarball to tmpdir");
1057 }
1058 }
[1]1059
[116]1060 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[1]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
[1645]1080estimate_noof_media_required(long noof_sets)
[1]1081{
[116]1082 /*@ buffers *************** */
[3191]1083 char *tmp = NULL;
[3611]1084 char *tmp1 = NULL;
[2242]1085 char *mds = NULL;
[1]1086
[116]1087 /*@ long long ************* */
1088 long long scratchLL;
[1]1089
[3150]1090 if (bkpinfo->media_size <= 0) {
[116]1091 log_to_screen("Number of media required: UNKNOWN");
1092 return;
1093 }
[1]1094
[116]1095 log_it("Estimating number of media required...");
[3191]1096 scratchLL = (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size) + (long long) (size_of_all_biggiefiles_K());
[3150]1097 scratchLL = (scratchLL / 1024) / bkpinfo->media_size;
[116]1098 scratchLL++;
1099 if (bkpinfo->use_lzo) {
1100 scratchLL = (scratchLL * 2) / 3;
[1002]1101 } else if (bkpinfo->use_gzip) {
[998]1102 scratchLL = (scratchLL * 2) / 3;
[3191]1103 } else if (bkpinfo->use_lzma) {
1104 scratchLL = (scratchLL * 2) / 3;
[116]1105 } else {
1106 scratchLL = scratchLL / 2;
1107 }
1108 if (!scratchLL) {
1109 scratchLL++;
1110 }
[3611]1111 tmp1 = number_to_text((int) (scratchLL + 1));
[116]1112 if (scratchLL <= 1) {
[2242]1113 mds = media_descriptor_string(bkpinfo->backup_media_type);
[3191]1114 mr_asprintf(tmp, "Your backup will probably occupy a single %s. Maybe two.", mds);
[2242]1115 mr_free(mds);
[3611]1116 } else if (scratchLL > 5) {
1117 mr_asprintf(tmp, "Your backup will occupy one meeeeellion media! (maybe %s)", tmp1);
[116]1118 } else {
[3611]1119 mr_asprintf(tmp, "Your backup will occupy approximately %s media.", tmp1);
[116]1120 }
[3191]1121 if (scratchLL < 50) {
[116]1122 log_to_screen(tmp);
1123 }
[3611]1124 mr_free(tmp1);
[3191]1125 mr_free(tmp);
[1]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 */
[116]1135bool is_this_file_compressed(char *filename)
[1]1136{
[3191]1137 char *do_not_compress_these = NULL;
1138 char *tmp = NULL;
[116]1139 char *p;
[1236]1140 char *q = NULL;
[1]1141
[1236]1142 q = strrchr(filename, '.');
1143 if (q == NULL) {
1144 return (FALSE);
1145 }
1146
[3191]1147 mr_asprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
[116]1148 if (!does_file_exist(tmp)) {
[3191]1149 mr_free(tmp);
[116]1150 return (FALSE);
1151 }
[3430]1152 /* TODO: This is just plain WRONG !! */
[3191]1153 mr_asprintf(do_not_compress_these,"%s", last_line_of_file(tmp));
1154 mr_free(tmp);
[1236]1155
[116]1156 for (p = do_not_compress_these; p != NULL; p++) {
[3191]1157 mr_asprintf(tmp, "%s", p);
[116]1158 if (strchr(tmp, ' ')) {
1159 *(strchr(tmp, ' ')) = '\0';
1160 }
[1236]1161 if (!strcmp(q, tmp)) {
[3191]1162 mr_free(tmp);
1163 mr_free(do_not_compress_these);
[116]1164 return (TRUE);
1165 }
1166 if (!(p = strchr(p, ' '))) {
1167 break;
1168 }
[3191]1169 mr_free(tmp);
[116]1170 }
[3191]1171 mr_free(do_not_compress_these);
[116]1172 return (FALSE);
[1]1173}
1174
1175/* @} - end fileGroup */
Note: See TracBrowser for help on using the repository browser.