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