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

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

merge -r1082:1105 $SVN_M/branches/stable

  • Property svn:keywords set to Id
File size: 33.8 KB
RevLine 
[171]1/* $Id: libmondo-files.c 1106 2007-02-07 22:55:11Z 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 1106 2007-02-07 22:55:11Z 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;
47 char *command;
48 char *tmp;
49 size_t n = 0;
[1]50
[59]51 /*@ pointers **************************************************** */
52 char *p;
53 FILE *fin;
[1]54
[59]55 /*@ initialize pointers ***************************************** */
[1]56
[59]57 p = output;
[1]58
[59]59 /*@************************************************************** */
[1]60
[59]61 assert_string_is_neither_NULL_nor_zerolength(filename);
[171]62
[59]63 if (does_file_exist(filename)) {
[900]64 mr_asprintf(&command, "md5sum \"%s\"", filename);
[59]65 fin = popen(command, "r");
[900]66 mr_free(command);
[171]67
[59]68 if (fin) {
[900]69 mr_getline(&output, &n, fin);
[59]70 p = strchr(output, ' ');
71 paranoid_pclose(fin);
72 }
73 } else {
[900]74 mr_asprintf(&tmp, "File '%s' not found; cannot calc checksum",
[59]75 filename);
76 log_it(tmp);
[900]77 mr_free(tmp);
[1]78 }
[59]79 if (p) {
80 *p = '\0';
81 }
82 return (output);
[1]83}
84
85
86/**
87 * Get the number of lines in @p filename.
88 * @param filename The file to count lines in.
89 * @return The number of lines in @p filename.
90 * @bug This function uses the shell and "wc -l"; it should probably be rewritten in C.
91 */
[59]92long count_lines_in_file(char *filename)
[1]93{
94
[59]95 /*@ buffers ***************************************************** */
[171]96 char *command;
97 char *incoming = NULL;
98 char *tmp;
[1]99
[59]100 /*@ long ******************************************************** */
101 long noof_lines = -1L;
[1]102
[171]103 /*@ int ******************************************************** */
104 size_t n = 0;
105
[59]106 /*@ pointers **************************************************** */
107 FILE *fin;
[1]108
[59]109 assert_string_is_neither_NULL_nor_zerolength(filename);
110 if (!does_file_exist(filename)) {
[900]111 mr_asprintf(&tmp,
[59]112 "%s does not exist, so I cannot found the number of lines in it",
113 filename);
114 log_it(tmp);
[900]115 mr_free(tmp);
[59]116 return (0);
[1]117 }
[900]118 mr_asprintf(&command, "cat %s | wc -l", filename);
[59]119 if (!does_file_exist(filename)) {
120 return (-1);
[1]121 }
[59]122 fin = popen(command, "r");
[900]123 mr_free(command);
[171]124
[59]125 if (fin) {
126 if (feof(fin)) {
127 noof_lines = 0;
128 } else {
[900]129 mr_getline(&incoming, &n, fin);
[59]130 while (strlen(incoming) > 0
131 && incoming[strlen(incoming) - 1] < 32) {
132 incoming[strlen(incoming) - 1] = '\0';
133 }
134 noof_lines = atol(incoming);
[900]135 mr_free(incoming);
[59]136 }
137 paranoid_pclose(fin);
138 }
139 return (noof_lines);
[1]140}
141
142
143/**
144 * Check for existence of given @p filename.
145 * @param filename The file to check for.
146 * @return TRUE if it exists, FALSE otherwise.
147 */
[59]148bool does_file_exist(char *filename)
[1]149{
150
[59]151 /*@ structures ************************************************** */
152 struct stat buf;
[1]153
[59]154 /*@************************************************************** */
[1]155
[59]156 assert(filename != NULL);
[171]157
[59]158 if (lstat(filename, &buf)) {
[1086]159 mr_msg(20, "%s does not exist", filename);
[59]160 return (FALSE);
161 } else {
[1086]162 mr_msg(20, "%s exists", filename);
[59]163 return (TRUE);
164 }
[1]165}
166
167
168/**
169 * Modify @p inout (a file containing a list of files) to only contain files
170 * that exist.
171 * @param inout The filelist to operate on.
172 * @note The original file is renamed beforehand, so it will not be accessible
173 * while the modification is in progress.
174 */
[59]175void exclude_nonexistent_files(char *inout)
[1]176{
[171]177 char *infname;
178 char *outfname;
179 char *tmp;
180 char *incoming = NULL;
[1]181
[59]182 /*@ int ********************************************************* */
183 int i;
[171]184 size_t n = 0;
[1]185
[59]186 /*@ pointers **************************************************** */
187 FILE *fin, *fout;
[1]188
189
[59]190 /*@ end vars *********************************************************** */
[1]191
[59]192 assert_string_is_neither_NULL_nor_zerolength(inout);
[171]193
[900]194 mr_asprintf(&infname, "%s.in", inout);
[171]195
[900]196 mr_asprintf(&tmp, "cp -f %s %s", inout, infname);
[59]197 run_program_and_log_output(tmp, FALSE);
[900]198 mr_free(tmp);
[171]199
[59]200 if (!(fin = fopen(infname, "r"))) {
201 log_OS_error("Unable to openin infname");
[900]202 mr_free(infname);
[59]203 return;
[1]204 }
[171]205
[900]206 mr_asprintf(&outfname, "%s", inout);
[59]207 if (!(fout = fopen(outfname, "w"))) {
208 log_OS_error("Unable to openout outfname");
[900]209 mr_free(outfname);
[59]210 return;
[1]211 }
[900]212 mr_free(outfname);
[171]213
[900]214 for (mr_getline(&incoming, &n, fin); !feof(fin);
215 mr_getline(&incoming, &n, fin)) {
[59]216 i = strlen(incoming) - 1;
217 if (i >= 0 && incoming[i] < 32) {
218 incoming[i] = '\0';
219 }
220 if (does_file_exist(incoming)) {
221 fprintf(fout, "%s\n", incoming);
222 } else {
[900]223 mr_asprintf(&tmp, "Excluding '%s'-nonexistent\n", incoming);
[59]224 log_it(tmp);
[900]225 mr_free(tmp);
[59]226 }
[1]227 }
[900]228 mr_free(incoming);
[59]229 paranoid_fclose(fout);
230 paranoid_fclose(fin);
231 unlink(infname);
[900]232 mr_free(infname);
[1]233}
234
235
236/**
237 * Attempt to find the user's kernel by calling Mindi.
238 * If Mindi can't find the kernel, ask user. If @p kernel is not empty,
239 * don't do anything.
240 * @param kernel Where to put the found kernel.
241 * @return 0 for success, 1 for failure.
242 */
[59]243int figure_out_kernel_path_interactively_if_necessary(char *kernel)
[1]244{
[688]245 char *tmp = NULL;
246 char *command = NULL;
[1]247
[688]248 if (kernel == NULL) {
249 kernel = call_program_and_get_last_line_of_output
[783]250 ("mindi --findkernel 2> /dev/null");
[59]251 }
[393]252 // If we didn't get anything back, check whether mindi raised a fatal error
[688]253 if (kernel == NULL) {
[900]254 mr_asprintf(&command, "grep 'Fatal error' /var/log/mindi.log");
[688]255 tmp = call_program_and_get_last_line_of_output(command);
[393]256 if (strlen(tmp) > 1) {
257 popup_and_OK(tmp);
258 fatal_error("Mindi gave a fatal error. Please check '/var/log/mindi.log'.");
259 }
[900]260 mr_free(command);
261 mr_free(tmp);
[393]262 }
[688]263
264 if (kernel != NULL) {
265 log_it("Calling Mindi with kernel path of '%s'", kernel);
266 } else {
267 log_it("Calling Mindi without kernel, so asking one");
268 }
269
270 while (kernel == NULL) {
[59]271 if (!ask_me_yes_or_no
[507]272 (_("Kernel not found or invalid. Choose another?"))) {
[59]273 return (1);
274 }
275 if (!popup_and_get_string
[507]276 (_("Kernel path"),
277 _("What is the full path and filename of your kernel, please?"),
[688]278 kernel)) {
[59]279 fatal_error
280 ("Kernel not found. Please specify with the '-k' flag.");
281 }
[688]282 log_it("User says kernel is at %s", kernel);
[59]283 }
284 return (0);
[1]285}
286
287
288/**
289 * Find location of specified executable in user's PATH.
290 * @param fname The basename of the executable to search for (e.g. @c afio).
291 * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found.
292 * @note The returned string points to static storage that will be overwritten with each call.
293 * @bug The checks with @c file and @c dirname seem pointless. If @c incoming is "", then you're calling
294 * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts
295 * to nothing.
296 */
[59]297char *find_home_of_exe(char *fname)
[1]298{
[59]299 /*@ buffers ********************* */
[688]300 char *output = NULL;
301 char *incoming = NULL;
302 char *command = NULL;
[1]303
[59]304 /*@******************************* */
[1]305
[59]306 assert_string_is_neither_NULL_nor_zerolength(fname);
[688]307
[900]308 mr_asprintf(&command, "which %s 2> /dev/null", fname);
[688]309 incoming = call_program_and_get_last_line_of_output(command);
[900]310 mr_free(command);
[171]311
[688]312 if (incoming == NULL) {
[59]313 if (system("which file > /dev/null 2> /dev/null")) {
314 return (NULL); // forget it :)
315 }
[900]316 mr_asprintf(&command,
[59]317 "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null",
318 incoming);
[900]319 mr_free(incoming);
[171]320
[688]321 incoming = call_program_and_get_last_line_of_output(command);
[900]322 mr_free(command);
[59]323 }
[688]324 if (incoming == NULL) // yes, it is == '\0' twice, not once :)
[1]325 {
[900]326 mr_asprintf(&command, "dirname %s 2> /dev/null", incoming);
327 mr_free(incoming);
[171]328
[688]329 incoming = call_program_and_get_last_line_of_output(command);
[900]330 mr_free(command);
[1]331 }
[688]332 output = incoming;
[171]333
[688]334 if (output != NULL && does_file_exist(output)) {
[1086]335 mr_msg(4, "find_home_of_exe () --- Found %s at %s", fname, output);
[59]336 } else {
[900]337 mr_free(output);
[1086]338 mr_msg(4, "find_home_of_exe() --- Could not find %s", fname);
[59]339 }
[688]340 return (output);
[1]341}
342
343
344/**
345 * Get the last sequence of digits surrounded by non-digits in the first 32k of
346 * a file.
347 * @param logfile The file to look in.
348 * @return The number found, or 0 if none.
349 */
[59]350int get_trackno_from_logfile(char *logfile)
[1]351{
352
[59]353 /*@ pointers ********************************************************* */
354 FILE *fin;
[1]355
[59]356 /*@ int ************************************************************** */
357 int trackno = 0;
358 size_t len = 0;
[1]359
[59]360 /*@ buffer ************************************************************ */
361 char datablock[32701];
[1]362
[59]363 assert_string_is_neither_NULL_nor_zerolength(logfile);
[171]364
[59]365 if (!(fin = fopen(logfile, "r"))) {
366 log_OS_error("Unable to open logfile");
367 fatal_error("Unable to open logfile to read trackno");
368 }
369 len = fread(datablock, 1, 32700, fin);
370 paranoid_fclose(fin);
371 if (len <= 0) {
372 return (0);
373 }
374 for (; len > 0 && !isdigit(datablock[len - 1]); len--);
375 datablock[len--] = '\0';
376 for (; len > 0 && isdigit(datablock[len - 1]); len--);
377 trackno = atoi(datablock + len);
378 return (trackno);
[1]379}
380
381
382/**
383 * Get a percentage from the last line of @p filename. We look for the string
384 * "% done" on the last line and, if we find it, grab the number before the last % sign.
385 * @param filename The file to get the percentage from.
386 * @return The percentage found, or 0 for error.
387 */
[59]388int grab_percentage_from_last_line_of_file(char *filename)
[1]389{
390
[688]391 char *lastline = NULL;
392 char *command = NULL;
393 char *p = NULL;
[59]394 int i;
[1]395
[688]396 for (i = g_noof_log_lines - 1;
[59]397 i >= 0 && !strstr(err_log_lines[i], "% Done")
398 && !strstr(err_log_lines[i], "% done"); i--);
399 if (i < 0) {
[900]400 mr_asprintf(&command,
[687]401 "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'",
[59]402 filename, '%');
[688]403 lastline = call_program_and_get_last_line_of_output(command);
[900]404 mr_free(command);
[688]405 if (!lastline) {
[59]406 return (0);
407 }
408 } else {
[900]409 mr_asprintf(&lastline, err_log_lines[i]);
[1]410 }
411
[59]412 p = strrchr(lastline, '%');
413 if (p) {
414 *p = '\0';
415 }
416 if (!p) {
[900]417 mr_free(lastline);
[59]418 return (0);
419 }
420 *p = '\0';
421 for (p--; *p != ' ' && p != lastline; p--);
422 if (p != lastline) {
423 p++;
424 }
425 i = atoi(p);
[900]426 mr_free(lastline);
[1]427
[59]428 return (i);
[1]429}
430
431
432/**
433 * Return the last line of @p filename.
434 * @param filename The file to get the last line of.
435 * @return The last line of the file.
436 * @note The returned string points to static storage that will be overwritten with each call.
437 */
[59]438char *last_line_of_file(char *filename)
[1]439{
[59]440 /*@ buffers ***************************************************** */
[688]441 char *output = NULL;
442 char *command = NULL;
443 char *tmp = NULL;
[1]444
[59]445 /*@ pointers **************************************************** */
446 FILE *fin;
[688]447 size_t n = 0;
[1]448
[59]449 /*@ end vars **************************************************** */
[1]450
[59]451 if (!does_file_exist(filename)) {
[900]452 mr_asprintf(&tmp, _("Tring to get last line of nonexistent file (%s)"),
[59]453 filename);
454 log_it(tmp);
[900]455 mr_free(tmp);
[783]456 return (NULL);
[59]457 }
[900]458 mr_asprintf(&command, "tail -n1 %s", filename);
[59]459 fin = popen(command, "r");
[900]460 mr_free(command);
[171]461
[900]462 mr_getline(&output, &n, fin);
[59]463 paranoid_pclose(fin);
464 while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
465 output[strlen(output) - 1] = '\0';
466 }
467 return (output);
[1]468}
469
[171]470
[1]471/**
472 * Get the length of @p filename in bytes.
473 * @param filename The file to get the length of.
474 * @return The length of the file, or -1 for error.
475 */
[687]476off_t length_of_file(char *filename)
[1]477{
[59]478 /*@ pointers *************************************************** */
479 FILE *fin;
[1]480
[59]481 /*@ long long ************************************************* */
[687]482 off_t length;
[1]483
[59]484 fin = fopen(filename, "r");
485 if (!fin) {
486 log_it("filename=%s", filename);
487 log_OS_error("Unable to openin filename");
488 return (-1);
489 }
[416]490 fseeko(fin, 0, SEEK_END);
[687]491 length = ftello(fin);
[59]492 paranoid_fclose(fin);
493 return (length);
[1]494}
495
496
497/**
498 * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
499 * @param outdir_fname The directory to create.
500 * @return The return value of @c mkdir.
501 */
[87]502/* BERLIOS: This function shouldn't call system at all */
[59]503int make_hole_for_dir(char *outdir_fname)
[1]504{
[171]505 char *tmp;
[59]506 int res = 0;
[1]507
[59]508 assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
[900]509 mr_asprintf(&tmp, "mkdir -p %s", outdir_fname);
[59]510 res = system(tmp);
[900]511 mr_free(tmp);
[59]512 return (res);
[1]513}
514
515
516/**
517 * Create the parent directories of @p outfile_fname.
518 * @param outfile_fname The file to make a "hole" for.
519 * @return 0, always.
520 * @bug Return value unnecessary.
521 */
[87]522/* BERLIOS: This function shouldn't call system at all */
[59]523int make_hole_for_file(char *outfile_fname)
[1]524{
[59]525 /*@ buffer ****************************************************** */
[171]526 char *command;
[1]527
[59]528 /*@ int ******************************************************** */
529 int res = 0;
[1]530
[59]531 /*@ end vars *************************************************** */
[1]532
[59]533 assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
534 assert(!strstr(outfile_fname, MNT_CDROM));
535 assert(!strstr(outfile_fname, "/dev/cdrom"));
[171]536
[900]537 mr_asprintf(&command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
[59]538 res += system(command);
[900]539 mr_free(command);
[171]540
[900]541 mr_asprintf(&command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
[59]542 res += system(command);
[900]543 mr_free(command);
[171]544
[900]545 mr_asprintf(&command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
[59]546 res += system(command);
[900]547 mr_free(command);
[59]548 unlink(outfile_fname);
549 return (0);
[1]550}
551
552
553/**
554 * Get the number of lines in @p filelist_fname that contain the string @p wildcard.
555 * @param filelist_fname The file to search through.
556 * @param wildcard The string to search for. This is @e not a shell glob or a regular expression.
557 * @return The number of lines matched.
558 */
[59]559long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
[1]560{
[59]561 /*@ long ******************************************************* */
562 long matches = 0;
[1]563
[59]564 /*@ pointers *************************************************** */
565 FILE *fin;
[1]566
[59]567 /*@ buffers **************************************************** */
[171]568 char *incoming = NULL;
[1]569
[171]570 size_t n = 0;
[59]571 /*@ end vars *************************************************** */
[1]572
573
[59]574 fin = fopen(filelist_fname, "r");
[1]575
[59]576 if (!fin) {
577 log_OS_error("Unable to openin filelist_fname");
578 return (0);
[1]579 }
[900]580 mr_getline(&incoming, &n, fin);
[59]581 while (!feof(fin)) {
582 if (strstr(incoming, wildcard)) {
583 matches++;
584 }
[900]585 mr_getline(&incoming, &n, fin);
[59]586 }
587 paranoid_fclose(fin);
[900]588 mr_free(incoming);
[59]589 return (matches);
[1]590}
591
592
593/**
594 * Register our PID in a file in /var/run.
595 * The PID will be put in /var/run/monitas-<tt>name_str</tt>.pid.
596 * @param pid 0 to remove file, anything else to create it.
597 * @param name_str The basename of the PID file (e.g. "mondo" or "server")
598 * @note This function does not provide support against multiple instances, unless you check for that yourself.
599 */
[59]600void register_pid(pid_t pid, char *name_str)
[1]601{
[171]602 char *tmp;
603 char *lockfile_fname;
[59]604 int res;
[171]605 size_t n = 0;
[59]606 FILE *fin;
[1]607
[900]608 mr_asprintf(&lockfile_fname, "/var/run/monitas-%s.pid", name_str);
[59]609 if (!pid) {
610 log_it("Unregistering PID");
611 if (unlink(lockfile_fname)) {
612 log_it("Error unregistering PID");
613 }
[900]614 mr_free(lockfile_fname);
[59]615 return;
616 }
617 if (does_file_exist(lockfile_fname)) {
618 if ((fin = fopen(lockfile_fname, "r"))) {
[900]619 mr_getline(&tmp, &n, fin);
[59]620 paranoid_fclose(fin);
621 } else {
622 log_OS_error("Unable to openin lockfile_fname");
623 }
624 pid = (pid_t) atol(tmp);
[900]625 mr_free(tmp);
[171]626
[900]627 mr_asprintf(&tmp, "ps %ld > /dev/null 2> /dev/null", (long int) pid);
[59]628 res = system(tmp);
[900]629 mr_free(tmp);
[59]630 if (!res) {
631 log_it
632 ("I believe the daemon is already running. If it isn't, please delete %s and try again.",
633 lockfile_fname);
634 }
635 }
[900]636 mr_asprintf(&tmp, "echo %ld > %s 2> /dev/null", (long int) getpid(),
[59]637 lockfile_fname);
[900]638 mr_free(lockfile_fname);
[171]639
[59]640 if (system(tmp)) {
641 fatal_error("Cannot register PID");
642 }
[900]643 mr_free(tmp);
[171]644 return;
[1]645}
646
647
648/**
649 * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/mountlist.txt.
650 * @param tmpdir The tempdir where the mountlist is stored.
651 * @param dev The device to search for.
652 * @return The size of the partition in KB.
653 */
[59]654long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
[1]655{
[688]656 char *command = NULL;
657 char *sz_res = NULL;
658 long file_len_K = 0L;
[59]659
[900]660 mr_asprintf(&command,
[171]661 "grep '%s ' %s/mountlist.txt | head -n1 | awk '{print $4;}'",
662 dev, tmpdir);
[59]663 log_it(command);
[688]664 sz_res = call_program_and_get_last_line_of_output(command);
[59]665 file_len_K = atol(sz_res);
[1086]666 mr_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
[900]667 mr_free(command);
668 mr_free(sz_res);
[59]669 return (file_len_K);
[1]670}
671
[171]672
[1]673/**
674 * Calculate the total size (in KB) of all the biggiefiles in this backup.
675 * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
676 * @return The total size of all biggiefiles in KB.
677 */
[59]678long size_of_all_biggiefiles_K(struct s_bkpinfo *bkpinfo)
[1]679{
[688]680 char *fname = NULL;
681 char *biggielist = NULL;
682 char *comment = NULL;
683 char *tmp = NULL;
684 char *command = NULL;
[1]685
[59]686 /*@ long ******************************************************** */
[688]687 long scratchL = 0L;
688 long file_len_K = 0L;
[1]689
[59]690 /*@ pointers *************************************************** */
691 FILE *fin = NULL;
[171]692 size_t n = 0;
[1]693
[59]694 /*@ end vars *************************************************** */
[1]695
[59]696 log_it("Calculating size of all biggiefiles (in total)");
[900]697 mr_asprintf(&biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
[59]698 log_it("biggielist = %s", biggielist);
699 if (!(fin = fopen(biggielist, "r"))) {
700 log_OS_error
701 ("Cannot open biggielist. OK, so estimate is based on filesets only.");
702 } else {
[1086]703 mr_msg(4, "Reading it...");
[900]704 for (mr_getline(&fname, &n, fin); !feof(fin);
705 mr_getline(&fname, &n, fin)) {
[59]706 if (fname[strlen(fname) - 1] <= 32) {
707 fname[strlen(fname) - 1] = '\0';
708 }
709 if (0 == strncmp(fname, "/dev/", 5)) {
[300]710 if (is_dev_an_NTFS_dev(fname)) {
[689]711 tmp = find_home_of_exe("ntfsresize");
712 if (!tmp) {
[300]713 fatal_error("ntfsresize not found");
714 }
[900]715 mr_free(tmp);
[689]716
[900]717 mr_asprintf(&command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
[300]718 log_it("command = %s", command);
[688]719 tmp = call_program_and_get_last_line_of_output(command);
[900]720 mr_free(command);
[688]721
[300]722 log_it("res of it = %s", tmp);
723 file_len_K = atoll(tmp) / 1024L;
[900]724 mr_free(tmp);
[300]725 } else {
726 file_len_K = get_phys_size_of_drive(fname) * 1024L;
727 }
[59]728 } else {
[687]729 /* BERLIOS: more than long here ??? */
[59]730 file_len_K = (long) (length_of_file(fname) / 1024);
731 }
732 if (file_len_K > 0) {
733 scratchL += file_len_K;
[1086]734 mr_msg(4, "%s --> %ld K", fname, file_len_K);
[59]735 }
[900]736 mr_asprintf(&comment,
[59]737 "After adding %s, scratchL+%ld now equals %ld", fname,
738 file_len_K, scratchL);
[1086]739 mr_msg(4, comment);
[900]740 mr_free(comment);
[171]741
[59]742 if (feof(fin)) {
743 break;
744 }
745 }
[900]746 mr_free(fname);
[1]747 }
[900]748 mr_free(biggielist);
[171]749
[59]750 log_it("Closing...");
751 paranoid_fclose(fin);
752 log_it("Finished calculating total size of all biggiefiles");
753 return (scratchL);
[1]754}
755
[171]756
[1]757/**
758 * Determine the amount of space (in KB) occupied by a mounted CD.
759 * This can also be used to find the space used for other directories.
760 * @param mountpt The mountpoint/directory to check.
761 * @return The amount of space occupied in KB.
762 */
[59]763long long space_occupied_by_cd(char *mountpt)
[1]764{
[59]765 /*@ buffer ****************************************************** */
[171]766 char *tmp = NULL;
[688]767 char *command = NULL;
[59]768 long long llres;
[171]769 size_t n = 0;
[59]770 /*@ pointers **************************************************** */
[688]771 char *p = NULL;
772 FILE *fin = NULL;
[1]773
[59]774 /*@ end vars *************************************************** */
[1]775
[900]776 mr_asprintf(&command, "du -sk %s", mountpt);
[839]777 errno = 0;
[59]778 fin = popen(command, "r");
[839]779 if (errno) {
780 log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
781 llres = 0;
782 } else {
[900]783 mr_getline(&tmp, &n, fin);
[839]784 paranoid_pclose(fin);
785 p = strchr(tmp, '\t');
786 if (p) {
787 *p = '\0';
788 }
789 for (p = tmp, llres = 0; *p != '\0'; p++) {
790 llres *= 10;
791 llres += (int) (*p - '0');
792 }
793 }
794
[900]795 mr_free(command);
796 mr_free(tmp);
[59]797 return (llres);
[1]798}
799
800
801/**
802 * Update a CRC checksum to include another character.
803 * @param crc The original CRC checksum.
804 * @param c The character to add.
805 * @return The new CRC checksum.
806 * @ingroup utilityGroup
807 */
[59]808unsigned int updcrc(unsigned int crc, unsigned int c)
[1]809{
[59]810 unsigned int tmp;
811 tmp = (crc >> 8) ^ c;
812 crc = (crc << 8) ^ crctttab[tmp & 255];
813 return crc;
[1]814}
815
[171]816
[1]817/**
818 * Update a reverse CRC checksum to include another character.
819 * @param crc The original CRC checksum.
820 * @param c The character to add.
821 * @return The new CRC checksum.
822 * @ingroup utilityGroup
823 */
[59]824unsigned int updcrcr(unsigned int crc, unsigned int c)
[1]825{
[59]826 unsigned int tmp;
827 tmp = crc ^ c;
828 crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
829 return crc;
[1]830}
831
832
833/**
834 * Check for an executable on the user's system; write a message to the
835 * screen and the log if we can't find it.
836 * @param fname The executable basename to look for.
837 * @return 0 if it's found, nonzero if not.
838 */
[59]839int whine_if_not_found(char *fname)
[1]840{
[59]841 /*@ buffers *** */
[171]842 char *command;
843 char *errorstr;
844 int res = 0;
[1]845
846
[900]847 mr_asprintf(&command, "which %s > /dev/null 2> /dev/null", fname);
[171]848 res = system(command);
[900]849 mr_free(command);
[171]850
851 if (res) {
[900]852 mr_asprintf(&errorstr,
[507]853 _("Please install '%s'. I cannot find it on your system."),
[59]854 fname);
855 log_to_screen(errorstr);
[900]856 mr_free(errorstr);
[59]857 log_to_screen
[507]858 (_("There may be an hyperlink at http://www.mondorescue.org which"));
859 log_to_screen(_("will take you to the relevant (missing) package."));
[59]860 return (1);
861 } else {
862 return (0);
863 }
[1]864}
865
866
867/**
868 * Create a data file at @p fname containing @p contents.
869 * The data actually can be multiple lines, despite the name.
870 * @param fname The file to create.
871 * @param contents The data to put in it.
872 * @return 0 for success, 1 for failure.
873 */
[59]874int write_one_liner_data_file(char *fname, char *contents)
[1]875{
[59]876 /*@ pointers *************************************************** */
877 FILE *fout;
878 int res = 0;
[1]879
[59]880 /*@ end vars *************************************************** */
[1]881
[59]882 assert_string_is_neither_NULL_nor_zerolength(fname);
883 if (!contents) {
884 log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
885 }
886 if (!(fout = fopen(fname, "w"))) {
887 log_it("fname=%s");
888 log_OS_error("Unable to openout fname");
889 return (1);
890 }
891 fprintf(fout, "%s\n", contents);
892 paranoid_fclose(fout);
893 return (res);
[1]894}
895
896
897/**
898 * Read @p fname into @p contents.
899 * @param fname The file to read.
900 * @param contents Where to put its contents.
901 * @return 0 for success, nonzero for failure.
902 */
[59]903int read_one_liner_data_file(char *fname, char *contents)
[1]904{
[59]905 /*@ pointers *************************************************** */
906 FILE *fin;
907 int res = 0;
908 int i;
[1]909
[59]910 /*@ end vars *************************************************** */
[1]911
[59]912 assert_string_is_neither_NULL_nor_zerolength(fname);
913 if (!contents) {
914 log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
915 }
916 if (!(fin = fopen(fname, "r"))) {
917 log_it("fname=%s", fname);
918 log_OS_error("Unable to openin fname");
919 return (1);
920 }
921 fscanf(fin, "%s\n", contents);
922 i = strlen(contents);
923 if (i > 0 && contents[i - 1] < 32) {
924 contents[i - 1] = '\0';
925 }
926 paranoid_fclose(fin);
927 return (res);
[1]928}
929
930
931/**
932 * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir.
933 * Currently this includes: copy Mondo's home directory to scratchdir, untar "mondo_home/payload.tgz"
934 * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore
935 * and post-nuke.tgz (if it exists) to tmpdir, and run "hostname > scratchdir/HOSTNAME".
936 * @param bkpinfo The backup information structure. Fields used:
937 * - @c bkpinfo->postnuke_tarball
938 * - @c bkpinfo->scratchdir
939 * - @c bkpinfo->tmpdir
940 */
[59]941void copy_mondo_and_mindi_stuff_to_scratchdir(struct s_bkpinfo *bkpinfo)
[1]942{
[59]943 /*@ Char buffers ** */
[171]944 char *command;
945 char *tmp;
[59]946 char old_pwd[MAX_STR_LEN];
[1]947
[59]948 mvaddstr_and_log_it(g_currentY, 0,
949 "Copying Mondo's core files to the scratch directory");
[1]950
[94]951 /* BERLIOS: Why do we need to do it here as well ? */
[1086]952 mr_msg(4, "g_mondo_home='%s'", g_mondo_home);
[94]953 if ((g_mondo_home == NULL) || strlen(g_mondo_home) < 2) {
[900]954 mr_free(g_mondo_home);
[94]955 g_mondo_home = find_and_store_mondoarchives_home();
[59]956 }
[900]957 mr_asprintf(&command, CP_BIN " --parents -pRdf %s %s", g_mondo_home,
[59]958 bkpinfo->scratchdir);
[1]959
[1086]960 mr_msg(4, "command = %s", command);
[59]961 if (run_program_and_log_output(command, 1)) {
962 fatal_error("Failed to copy Mondo's stuff to scratchdir");
963 }
[900]964 mr_free(command);
[1]965
[900]966 mr_asprintf(&tmp, "%s/payload.tgz", g_mondo_home);
[507]967
968 /* i18n */
[900]969 mr_asprintf(&command, CP_BIN " --parents /usr/share/locale/*/LC_MESSAGES/mondo.mo %s",bkpinfo->scratchdir);
[1086]970 mr_msg(4, "command = %s", command);
[507]971 run_program_and_log_output(command, 1);
[900]972 mr_free(command);
[507]973
[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);
[900]1116 mr_asprintf(&command,
[903]1117 "ifconfig | grep -E '%s' | grep -v '%s' | head -n1 | cut -d' ' -f1", mac_addr);
[900]1118 mr_free(nfs_dev);
[808]1119 nfs_dev = call_program_and_get_last_line_of_output(command);
[900]1120 mr_free(command);
1121 mr_free(mac_addr);
[1]1122
[808]1123 log_to_screen("Replacing it with %s\n", nfs_dev);
1124 }
1125
[900]1126 mr_free(nfs_dev);
1127 mr_free(tmp);
[1]1128
[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{
[59]1268 FILE *fout;
1269 char *tmp;
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.