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