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

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

merge -r295:299 $SVN_M/branches/2.06

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