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