source: MondoRescue/trunk/mondo/mondo/common/libmondo-files.c@ 197

Last change on this file since 197 was 197, checked in by bcornec, 18 years ago

merge -r193:196 $SVN_M/branches/2.05

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