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

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

Still other memory management improvements ( I hope :-)

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