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