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

Last change on this file since 1161 was 1161, checked in by Bruno Cornec, 17 years ago

Update trunk with the latest feedback from stable (obtained with meld) - First pass

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