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

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

Change find_my_editor and find_home_of_exe to return dynamically assigned stringsi - adapt whine_if_not_found

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