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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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