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