source: MondoRescue/branches/stable/mondo/src/common/libmondo-files.c@ 1113

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

Memory management improvements again for libmondo-fork.c & libmondo-files.c

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