source: MondoRescue/branches/3.0/mondo/src/common/libmondo-filelist.c@ 3141

Last change on this file since 3141 was 3141, checked in by Bruno Cornec, 11 years ago

r5345@localhost: bruno | 2013-06-13 12:46:54 +0200

  • Solving #702 by adding a -F option which is meant to be used especially when mondoarchive is launched from cron, to avoid going into a loop where mondoarchive asks questions to the admin, where they cannot be answerd, which fills the logs and the underlying file system. With -F mondoarchive will exits at the first interaction request instead.
  • Property svn:keywords set to Id
File size: 51.0 KB
Line 
1/*
2 $Id: libmondo-filelist.c 3141 2013-06-13 16:05:57Z bruno $
3*/
4
5/**
6 * @file
7 * Functions which create, chop, and edit the filelist.
8 */
9
10#include "my-stuff.h"
11#include "mondostructures.h"
12#include "lib-common-externs.h"
13#include "libmondo-filelist.h"
14#include "libmondo-string-EXT.h"
15#include "libmondo-files-EXT.h"
16#include "libmondo-fork-EXT.h"
17#include "libmondo-gui-EXT.h"
18#include "libmondo-tools-EXT.h"
19#include "mr_mem.h"
20#include "mr_str.h"
21
22#include <time.h>
23#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <dirent.h>
27#include <errno.h>
28#include <stdio.h>
29
30
31extern ssize_t getline(char **lineptr, size_t * n, FILE * stream);
32extern char *MONDO_LOGFILE;
33
34extern long g_max_biggie_size;
35
36/* Reference to global bkpinfo */
37extern struct s_bkpinfo *bkpinfo;
38
39
40/*@unused@*/
41//static char cvsid[] = "$Id: libmondo-filelist.c 3141 2013-06-13 16:05:57Z bruno $";
42
43/**
44 * Number of lines in the filelist last loaded.
45 * @warning This implies that two filesets cannot be loaded at once.
46 * @ingroup globalGroup
47 */
48long g_original_noof_lines_in_filelist = 0;
49
50/**
51 * Number of filesets in the current backup.
52 * @ingroup globalGroup
53 */
54long g_noof_sets = 0;
55
56extern bool g_text_mode;
57extern newtComponent g_progressForm;
58extern int g_currentY;
59extern int g_noof_rows;
60
61extern char *g_getfacl;
62extern char *g_getfattr;
63
64
65
66/**
67 * @addtogroup filelistGroup
68 * @{
69 */
70/**
71 * Call chop_filelist() to chop the filelist into sets.
72 * @param bkpinfo The backup information structure. Fields used:
73 * - @c bkpinfo->image_devs
74 * - @c bkpinfo->optimal_set_size
75 * - @c bkpinfo->scratchdir
76 * - @c bkpinfo->tmpdir
77 * @see chop_filelist
78 */
79int call_filelist_chopper()
80{
81 /*@ buffers *********************** */
82 char *dev = NULL;
83 char *filelist = NULL;
84 char *tempfile = NULL;
85 long noof_sets;
86
87 /*@ pointers ********************** */
88 char *ptr;
89 FILE *fout;
90
91 /*@ int *************************** */
92 int i, retval = 0;
93
94 mvaddstr_and_log_it(g_currentY, 0, "Dividing filelist into sets");
95
96 log_to_screen("Dividing filelist into sets. Please wait.");
97 i = 0;
98 mr_asprintf(&filelist, "%s/archives/filelist.full", bkpinfo->scratchdir);
99 if (!does_file_exist(filelist)) {
100 log_it("filelist %s not found", filelist);
101 mr_free(filelist);
102 fatal_error("call_filelist_chopper() -- filelist not found!");
103 }
104
105 noof_sets = chop_filelist(filelist, bkpinfo->tmpdir, bkpinfo->optimal_set_size);
106 mr_free(filelist);
107 estimate_noof_media_required(noof_sets); // for cosmetic purposes
108
109 mr_asprintf(&tempfile, "%s/biggielist.txt", bkpinfo->tmpdir);
110 if (!(fout = fopen(tempfile, "a"))) {
111 log_OS_error("Cannot append to biggielist");
112 retval++;
113 mr_free(tempfile);
114 return (retval);
115 }
116 mr_free(tempfile);
117
118 log_it(bkpinfo->image_devs);
119
120 ptr = bkpinfo->image_devs;
121
122 malloc_string(dev);
123 while (ptr && *ptr) {
124 strcpy(dev, ptr);
125 log_it("Examining imagedev %s", dev);
126 for (i = 0; i < (int) strlen(dev) && dev[i] != ' '; i++);
127 dev[i] = '\0';
128 if (!strlen(dev)) {
129 continue;
130 }
131 fprintf(fout, "%s\n", dev);
132 log_it("Adding '%s' to biggielist", dev);
133 if ((ptr = strchr(ptr, ' '))) {
134 ptr++;
135 }
136 }
137 paranoid_fclose(fout);
138 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
139
140 paranoid_free(dev);
141 return (retval);
142}
143
144
145
146int sort_file(char *orig_fname)
147{
148 char *tmp_fname = NULL;
149 char *command = NULL;
150 int retval = 0;
151
152 log_msg(5, "Sorting file %s", orig_fname);
153
154 if (!does_file_exist(orig_fname)) {
155 log_msg(2, "file %s empty", orig_fname);
156 return (0);
157 } // no sense in trying to sort an empty file
158
159 mr_asprintf(&tmp_fname, "%s/sortfile", bkpinfo->tmpdir);
160
161 mr_asprintf(&command, "sort %s > %s 2>> %s", orig_fname, tmp_fname, MONDO_LOGFILE);
162 retval = system(command);
163 mr_free(command);
164
165 if (retval) {
166 log_msg(2, "Failed to sort %s - oh dear", orig_fname);
167 } else {
168 log_msg(5, "Sorted %s --> %s OK. Copying it back to %s now", orig_fname, tmp_fname, orig_fname);
169 mr_asprintf(&command, "mv -f %s %s", tmp_fname, orig_fname);
170 retval += run_program_and_log_output(command, 5);
171 mr_free(command);
172
173 if (retval) {
174 log_msg(2, "Failed to copy %s back to %s - oh dear", tmp_fname, orig_fname);
175 } else {
176 log_msg(5, "%s was sorted OK.", orig_fname);
177 }
178 }
179 mr_free(tmp_fname);
180 log_msg(5, "Finished sorting file %s", orig_fname);
181 return (retval);
182}
183
184
185
186/**
187 * Chop the filelist into sets.
188 * Each fileset is a list of files whose total (uncompressed) size is usually
189 * about X KB. Files bigger than 8X KB are placed in a "biggielist"; they will
190 * be sliced and compressed separately from the regular files.
191 *
192 * @param filelist The big filelist (filelist.full) to chop up.
193 * @param outdir The directory to place the files (filelist.N where N is
194 * an integer, biggielist.txt, and LAST-FILELIST-NUMBER) created
195 * @param maxsetsizeK Optimal size of a fileset (X above).
196 * @return number of errors encountered (0 for success).
197 */
198int chop_filelist(char *filelist, char *outdir, long maxsetsizeK)
199{
200/*@ long ****************************************/
201 long lino = 0;
202 // A big file has more than 64 MB of real content
203 long curr_set_size;
204 long noof_lines;
205 long siz;
206
207 /*@ int **************************************** */
208 int i;
209 long curr_set_no;
210
211 /*@ buffers ************************************* */
212 char *outfname = NULL;
213 char *biggie_fname = NULL;
214 char *incoming = NULL;
215 char *tmp = NULL;
216
217 /*@ pointers *********************************** */
218 FILE *fin;
219 FILE *fout;
220 FILE *fbig;
221
222 /*@ structures ********************************* */
223 struct stat buf;
224 int err = 0;
225
226 assert_string_is_neither_NULL_nor_zerolength(filelist);
227 assert_string_is_neither_NULL_nor_zerolength(outdir);
228 assert(maxsetsizeK > 0);
229
230 log_it("filelist=%s;", filelist);
231 open_evalcall_form("Dividing filelist into sets");
232 noof_lines = count_lines_in_file(filelist);
233 if (!(fin = fopen(filelist, "r"))) {
234 log_OS_error("Cannot openin filelist");
235 return (0);
236 }
237 curr_set_no = 0;
238 curr_set_size = 0;
239 mr_asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
240 mr_asprintf(&biggie_fname, "%s/biggielist.txt", outdir);
241 log_it("outfname=%s; biggie_fname=%s", outfname, biggie_fname);
242 if (!(fbig = fopen(biggie_fname, "w"))) {
243 log_OS_error("Cannot openout biggie_fname");
244 err++;
245 mr_free(outfname);
246 mr_free(biggie_fname);
247 return (curr_set_no + 1);
248 }
249 if (!(fout = fopen(outfname, "w"))) {
250 log_OS_error("Cannot openout outfname");
251 err++;
252 mr_free(outfname);
253 mr_free(biggie_fname);
254 return (curr_set_no + 1);
255 }
256
257 mr_getline(&incoming, fin);
258 while (!feof(fin)) {
259 lino++;
260 i = strlen(incoming) - 1;
261 if (i < 0) {
262 i = 0;
263 }
264 if (incoming[i] < 32) {
265 incoming[i] = '\0';
266 }
267 if (!strncmp(incoming, "/dev/", 5)) {
268 siz = 1;
269 } else if (lstat(incoming, &buf) != 0) {
270 siz = 0;
271 } else {
272 // blocks are 512 bytes long - cf man 2 stat - Pass to the previous unit (MB => kB e.g.)
273 // use blocks instead of size to allow sparse file correct handling as much as possible
274 siz = (long) ((buf.st_blocks*512) >> 10);
275 }
276 if (siz > g_max_biggie_size) {
277 log_msg(10, "Adding %s to big files (size = %ld)\n", incoming, siz);
278 fprintf(fbig, "%s\n", incoming);
279 } else {
280 curr_set_size += siz;
281 log_msg(10, "Adding %s to filelist %d (size = %ld)\n", incoming, curr_set_no, siz);
282 fprintf(fout, "%s\n", incoming);
283 if (curr_set_size > maxsetsizeK) {
284 paranoid_fclose(fout);
285 sort_file(outfname);
286 mr_free(outfname);
287 curr_set_no++;
288 curr_set_size = 0;
289
290 mr_asprintf(&outfname, "%s/filelist.%ld", outdir, curr_set_no);
291 if (!(fout = fopen(outfname, "w"))) {
292 log_OS_error("Unable to openout outfname");
293 err++;
294 mr_free(outfname);
295 mr_free(biggie_fname);
296 mr_free(incoming);
297 return (curr_set_no + 1);
298 }
299 update_evalcall_form((int) (lino * 100 / noof_lines));
300 }
301 }
302 mr_free(incoming);
303 mr_getline(&incoming, fin);
304 }
305 mr_free(incoming);
306
307 paranoid_fclose(fin);
308 paranoid_fclose(fout);
309 paranoid_fclose(fbig);
310
311 if (length_of_file(outfname) <= 2) {
312 unlink(outfname);
313 g_noof_sets--;
314 }
315 g_noof_sets = curr_set_no;
316 sort_file(outfname);
317 mr_free(outfname);
318
319 sort_file(biggie_fname);
320 mr_free(biggie_fname);
321
322 mr_asprintf(&outfname, "%s/LAST-FILELIST-NUMBER", outdir);
323 mr_asprintf(&tmp, "%ld", curr_set_no);
324 if (write_one_liner_data_file(outfname, tmp)) {
325 log_OS_error
326 ("Unable to echo write one-liner to LAST-FILELIST-NUMBER");
327 err = 1;
328 }
329 mr_free(tmp);
330 mr_free(outfname);
331
332 if (curr_set_no == 0) {
333 mr_asprintf(&tmp, "Only one fileset. Fine.");
334 } else {
335 mr_asprintf(&tmp, "Filelist divided into %ld sets", curr_set_no + 1);
336 }
337 log_msg(1, tmp);
338 mr_free(tmp);
339 close_evalcall_form();
340 /* This is to work around an obscure bug in Newt; open a form, close it,
341 carry on... I don't know why it works but it works. If you don't do this
342 then update_progress_form() won't show the "time taken / time remaining"
343 line. The bug only crops up AFTER the call to chop_filelist(). Weird. */
344#ifndef _XWIN
345 if (!g_text_mode) {
346 open_progress_form("", "", "", "", 100);
347 newtPopHelpLine();
348 newtFormDestroy(g_progressForm);
349 newtPopWindow();
350 }
351#endif
352 return (err ? 0 : curr_set_no + 1);
353}
354
355
356
357
358
359/**
360 * Free all the memory used by a filelist structure.
361 * Since this may take a long time for large filelists, a progress bar will be displayed.
362 * @param filelist The filelist to free.
363 */
364void free_filelist(struct s_node *filelist)
365{
366 /*@ int's ******************************************************* */
367 static int depth = 0;
368 int percentage;
369
370 /*@ long's ****************************************************** */
371 static long i = 0;
372
373 /*@ end vars **************************************************** */
374
375 assert(filelist != NULL);
376 if (depth == 0) {
377 open_evalcall_form("Freeing memory");
378 log_to_screen("Freeing memory formerly occupied by filelist");
379 }
380 depth++;
381
382 if (filelist->ch == '\0') {
383 if (!(i++ % 1111)) {
384 percentage =
385 (int) (i * 100 / g_original_noof_lines_in_filelist);
386 update_evalcall_form(percentage);
387
388 }
389 }
390
391 if (filelist->right) {
392 free_filelist(filelist->right);
393 filelist->right = NULL;
394 }
395 if (filelist->down) {
396/* if (!(i++ %39999)) { update_evalcall_form(0); } */
397 free_filelist(filelist->down);
398 filelist->down = NULL;
399 }
400 filelist->ch = '\0';
401 paranoid_free(filelist);
402 depth--;
403 if (depth == 0) {
404 close_evalcall_form();
405 log_it("Finished freeing memory");
406 }
407}
408
409
410int call_exe_and_pipe_output_to_fd(char *syscall, FILE * pout)
411{
412 FILE *pattr;
413 char *tmp;
414 char *p;
415
416 pattr = popen(syscall, "r");
417 if (!pattr) {
418 log_msg(1, "Failed to open fattr() %s", syscall);
419 return (1);
420 }
421 if (feof(pattr)) {
422 log_msg(1, "Failed to call fattr() %s", syscall);
423 paranoid_pclose(pattr);
424 return (2);
425 }
426 malloc_string(tmp);
427 for (p = fgets(tmp, MAX_STR_LEN, pattr); !feof(pattr) && (p != NULL);
428 p = fgets(tmp, MAX_STR_LEN, pattr)) {
429 fputs(tmp, pout);
430 }
431 paranoid_pclose(pattr);
432 paranoid_free(tmp);
433 return (0);
434}
435
436
437
438int gen_aux_list(char *filelist, char *syscall_sprintf,
439 char *auxlist_fname)
440{
441 FILE *fin;
442 FILE *pout;
443 char *pout_command = NULL;
444 char *syscall;
445 char *file_to_analyze;
446 char *strtmp = NULL;
447 char *tmp = NULL;
448 char *p = NULL;
449 int i;
450
451 if (!(fin = fopen(filelist, "r"))) {
452 log_msg(1, "Cannot openin filelist %s", filelist);
453 return (1);
454 }
455 mr_asprintf(&pout_command, "gzip -c1 > %s", auxlist_fname);
456 if (!(pout = popen(pout_command, "w"))) {
457 log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
458 fclose(fin);
459 mr_free(pout_command);
460 return (4);
461 }
462 mr_free(pout_command);
463
464 malloc_string(file_to_analyze);
465 for (p = fgets(file_to_analyze, MAX_STR_LEN, fin); !feof(fin) && (p != NULL);
466 p = fgets(file_to_analyze, MAX_STR_LEN, fin)) {
467 i = strlen(file_to_analyze);
468 if (i > 0 && file_to_analyze[i - 1] < 32) {
469 file_to_analyze[i - 1] = '\0';
470 }
471 log_msg(8, "Analyzing %s", file_to_analyze);
472 tmp = mr_stresc(file_to_analyze, " `$\\\"(){}[]'*?&|!#~", '\\');
473 mr_asprintf(&strtmp, syscall_sprintf, tmp);
474 paranoid_free(tmp);
475 mr_asprintf(&syscall, "%s 2>> /dev/null", strtmp); // " MONDO_LOGFILE);
476 paranoid_free(strtmp);
477 log_msg(20,"calling %s\n",syscall);
478 call_exe_and_pipe_output_to_fd(syscall, pout);
479 paranoid_free(syscall);
480 }
481 paranoid_fclose(fin);
482 paranoid_pclose(pout);
483 paranoid_free(file_to_analyze);
484 return (0);
485}
486
487
488int get_acl_list(char *filelist, char *facl_fname)
489{
490 char *command = NULL;
491 int retval = 0;
492
493 if (g_getfacl != NULL) {
494 mr_asprintf(&command, "touch \"%s\"", facl_fname);
495 run_program_and_log_output(command, 8);
496 mr_free(command);
497
498 retval =
499 gen_aux_list(filelist, "getfacl --all-effective -P \"%s\"",
500 facl_fname);
501 }
502 return (retval);
503}
504
505
506int get_fattr_list(char *filelist, char *fattr_fname)
507{
508 char *command;
509 int retval = 0;
510
511 if (g_getfattr != NULL) {
512 mr_asprintf(&command, "touch \"%s\"", fattr_fname);
513 run_program_and_log_output(command, 8);
514 mr_free(command);
515 retval =
516 gen_aux_list(filelist, "getfattr --en=hex -m - -h -d \"%s\"",
517 fattr_fname);
518 }
519 return (retval);
520}
521
522
523int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
524 char *executable)
525{
526 const int my_depth = 8;
527 char *command = NULL;
528 char *syscall_pin = NULL;
529 char *syscall_pout = NULL;
530 char *incoming;
531 char *current_subset_file, *current_master_file;
532 char *masklist = NULL;
533 int retval = 0;
534 int i;
535 char *p, *q, *r;
536 char *tmp = NULL;
537 FILE *pin, *pout, *faclin;
538
539 log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist,
540 original_exat_fname, executable);
541 if (!orig_msklist || !orig_msklist[0]
542 || !does_file_exist(orig_msklist)) {
543 log_msg(1, "No masklist provided. I shall therefore set ALL attributes.");
544 if (strstr(executable,"acl")) {
545 /* setfacl needs no additional option for physical walk */
546 mr_asprintf(&tmp,"");
547 } else {
548 /* setfattr needs option -h for physical walk */
549 mr_asprintf(&tmp,"-h");
550 }
551 mr_asprintf(&command, "gzip -dc %s | %s %s --restore - 2>> %s", original_exat_fname, executable, tmp, MONDO_LOGFILE);
552 mr_free(tmp);
553 log_msg(1, "command = %s", command);
554 retval = system(command);
555 mr_free(command);
556 log_msg(1, "Returning w/ retval=%d", retval);
557 return (retval);
558 }
559 if (length_of_file(original_exat_fname) <= 0) {
560 log_msg(1,
561 "original_exat_fname %s is empty or missing, so no need to set EXAT list",
562 original_exat_fname);
563 return (0);
564 }
565 malloc_string(incoming);
566 malloc_string(current_subset_file);
567 malloc_string(current_master_file);
568 mr_asprintf(&masklist, "%s/masklist", bkpinfo->tmpdir);
569 mr_asprintf(&command, "cp -f %s %s", orig_msklist, masklist);
570 run_program_and_log_output(command, 1);
571 mr_free(command);
572
573 sort_file(masklist);
574 current_subset_file[0] = current_master_file[0] = '\0';
575
576 mr_asprintf(&syscall_pout, "%s --restore - 2>> %s", executable, MONDO_LOGFILE);
577 log_msg(1, "syscall_pout = %s", syscall_pout);
578 pout = popen(syscall_pout, "w");
579 mr_free(syscall_pout);
580
581 if (!pout) {
582 log_it("Unable to openout to syscall_pout");
583 mr_free(masklist);
584 return (1);
585 }
586
587 mr_asprintf(&syscall_pin, "gzip -dc %s", original_exat_fname);
588 log_msg(1, "syscall_pin = %s", syscall_pin);
589 pin = popen(syscall_pin, "r");
590 mr_free(syscall_pin);
591
592 if (!pin) {
593 pclose(pout);
594 log_it("Unable to openin from syscall");
595 return (1);
596 }
597 faclin = fopen(masklist, "r");
598 if (!faclin) {
599 pclose(pin);
600 pclose(pout);
601 log_it("Unable to openin masklist");
602 mr_free(masklist);
603 return (1);
604 }
605// printf("Hi there. Starting the loop\n");
606
607 r = fgets(current_subset_file, MAX_STR_LEN, faclin);
608 r = fgets(incoming, MAX_STR_LEN, pin);
609 while (!feof(pin) && !feof(faclin) && (r != NULL)) {
610// printf("incoming = %s", incoming);
611
612 strcpy(current_master_file, incoming + 8);
613
614 p = current_subset_file;
615 if (*p == '/') {
616 p++;
617 }
618 i = strlen(p);
619 if (i > 0 && p[i - 1] < 32) {
620 p[i - 1] = '\0';
621 }
622
623
624 q = current_master_file;
625 if (*q == '/') {
626 q++;
627 }
628 i = strlen(q);
629 if (i > 0 && q[i - 1] < 32) {
630 q[i - 1] = '\0';
631 }
632
633 i = strcmp(p, q);
634 log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
635
636// printf("%s v %s --> %d\n", p, q, i);
637
638 if (i < 0) { // read another subset file in.
639 log_msg(my_depth, "Reading next subset line in\n\n");
640 r = fgets(current_subset_file, MAX_STR_LEN, faclin);
641 continue;
642 }
643
644 if (!i) {
645 fputs(incoming, pout);
646 }
647 r = fgets(incoming, MAX_STR_LEN, pin);
648 if (!i) {
649 log_msg(my_depth, "Copying master %s", q);
650 }
651// if (!i) { printf("Match --- %s\n", q); }
652
653 while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
654 if (!i) {
655
656// printf("%s", incoming);
657
658 fputs(incoming, pout);
659 }
660 r = fgets(incoming, MAX_STR_LEN, pin);
661 }
662 if (!i) {
663 r = fgets(current_subset_file, MAX_STR_LEN, faclin);
664 }
665 }
666 while (!feof(pin)) {
667 r = fgets(incoming, MAX_STR_LEN, pin);
668 }
669 fclose(faclin);
670 pclose(pin);
671 pclose(pout);
672
673// printf("OK, loop is done\n");
674
675 unlink(masklist);
676 mr_free(masklist);
677
678 paranoid_free(current_subset_file);
679 paranoid_free(current_master_file);
680 paranoid_free(incoming);
681 return (retval);
682}
683
684
685int set_fattr_list(char *masklist, char *fattr_fname)
686{
687 if (find_home_of_exe("setfattr")) {
688 return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
689 } else {
690 log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
691 return(0);
692 }
693}
694
695
696
697int set_acl_list(char *masklist, char *acl_fname)
698{
699 if (find_home_of_exe("setfacl")) {
700 return (set_EXAT_list(masklist, acl_fname, "setfacl"));
701 } else {
702 log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
703 return(0);
704 }
705}
706
707
708/**
709 * Get the number of the last fileset in the backup.
710 * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
711 * @return The last filelist number.
712 * @note This function should only be called at restore-time.
713 */
714int get_last_filelist_number()
715{
716 /*@ buffers ***************************************************** */
717 char val_sz[MAX_STR_LEN];
718 char *cfg_fname = NULL;
719
720 /*@ long ******************************************************** */
721 int val_i;
722
723 /*@ end vars **************************************************** */
724
725 assert(bkpinfo != NULL);
726
727 strcpy(val_sz,"");
728 mr_asprintf(&cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
729 read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
730 mr_free(cfg_fname);
731
732 val_i = atoi(val_sz);
733 if (val_i <= 0) {
734 val_i = 500;
735 }
736 return (val_i);
737}
738
739
740/**
741 * Add a string at @p startnode.
742 * @param startnode The node to start at when searching for where to add the string.
743 * @param string_to_add The string to add.
744 * @return 0 for success, 1 for failure.
745 * @bug I don't understand this function. Would someone care to explain it?
746 */
747int add_string_at_node(struct s_node *startnode, char *string_to_add)
748{
749
750
751 /*@ int ******************************************************** */
752 int noof_chars;
753 int i;
754 int res;
755
756 /*@ sturctures ************************************************* */
757 struct s_node *node, *newnode;
758
759 /*@ char ****************************************************** */
760 char char_to_add;
761
762 /*@ bools ****************************************************** */
763
764 const bool sosodef = FALSE;
765
766 static int depth = 0;
767 static char original_string[MAX_STR_LEN];
768
769 assert(startnode != NULL);
770 assert(string_to_add != NULL);
771
772 if (!depth) {
773 strcpy(original_string, string_to_add);
774 }
775
776 noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
777
778/* walk across tree if necessary */
779 node = startnode;
780 char_to_add = string_to_add[0];
781 if (node->right != NULL && node->ch < char_to_add) {
782 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
783 char_to_add, node->ch, (node->right)->ch);
784 return (add_string_at_node(node->right, string_to_add));
785 }
786
787/* walk down tree if appropriate */
788 if (node->down != NULL && node->ch == char_to_add) {
789 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
790 depth++;
791 res = add_string_at_node(node->down, string_to_add + 1);
792 depth--;
793 return (res);
794 }
795
796 if (char_to_add == '\0' && node->ch == '\0') {
797 log_msg(6, "%s already in tree", original_string);
798 return (1);
799 }
800
801/* add here */
802 if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
803 log_to_screen("failed to malloc");
804 depth--;
805 return (1);
806 }
807 if (char_to_add < node->ch) // add to the left of node
808 {
809 log_msg(7, "depth=%d char=%c --- adding (left)", depth,
810 char_to_add);
811 memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
812 node->right = newnode;
813 } else if (char_to_add > node->ch) // add to the right of node
814 {
815 log_msg(7, "depth=%d char=%c --- adding (right)", depth,
816 char_to_add);
817 newnode->right = node->right; // newnode is to the RIGHT of node
818 node->right = newnode;
819 node = newnode;
820 }
821 // from now on, we're working on 'node'
822 node->down = NULL;
823 node->ch = char_to_add;
824 node->expanded = node->selected = FALSE;
825 if (char_to_add == '\0') {
826 log_msg(6, "Added %s OK", original_string);
827 return (0);
828 }
829// add the rest
830 log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
831 for (i = 1; i < noof_chars; i++) {
832 if (!
833 (node->down =
834 (struct s_node *) malloc(sizeof(struct s_node)))) {
835 log_to_screen("%s - failed to malloc", string_to_add);
836 return (1);
837 }
838 node = node->down;
839 char_to_add = string_to_add[i];
840 log_msg(6, "Adding '%c'", char_to_add);
841 node->ch = char_to_add;
842 node->right = node->down = NULL;
843 node->expanded = node->selected = FALSE;
844 if (!node->ch) {
845 node->selected = sosodef;
846 }
847 }
848 log_msg(6, "Finally - added %s OK", original_string);
849 return (0);
850}
851
852
853
854
855/**
856 * Load a filelist into a <tt>struct s_node</tt>.
857 * When you are done with the filelist, call free_filelist().
858 * @param filelist_fname The file to load the filelist from.
859 * @return A filelist tree structure.
860 */
861struct s_node *load_filelist(char *filelist_fname)
862{
863
864 /*@ structures ************************************************* */
865 struct s_node *filelist;
866
867 /*@ pointers *************************************************** */
868 FILE *pin;
869
870 /*@ buffers **************************************************** */
871 char *command_to_open_fname = NULL;
872 char fname[MAX_STR_LEN];
873 char tmp[MAX_STR_LEN];
874 char *tmp1 = NULL;
875 int pos_in_fname;
876 /*@ int ******************************************************** */
877 int percentage;
878
879 /*@ long ******************************************************* */
880 long lines_in_filelist;
881 long lino = 0;
882 /*@ end vars *************************************************** */
883
884 assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
885
886 if (!does_file_exist(filelist_fname)) {
887 fatal_error("filelist does not exist -- cannot load it");
888 }
889 log_to_screen("Loading filelist");
890 mr_asprintf(&tmp1, "zcat %s | wc -l", filelist_fname);
891 log_msg(6, "tmp1 = %s", tmp1);
892 lines_in_filelist = atol(call_program_and_get_last_line_of_output(tmp1));
893 mr_free(tmp1);
894
895 if (lines_in_filelist < 3) {
896 log_to_screen("Warning - surprisingly short filelist.");
897 }
898 g_original_noof_lines_in_filelist = lines_in_filelist;
899 if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
900 return (NULL);
901 }
902 filelist->ch = '/';
903 filelist->right = NULL;
904 filelist->down = malloc(sizeof(struct s_node));
905 filelist->expanded = filelist->selected = FALSE;
906 (filelist->down)->ch = '\0';
907 (filelist->down)->right = (filelist->down)->down = FALSE;
908 (filelist->down)->expanded = (filelist->down)->selected = FALSE;
909
910 mr_asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
911 if (!(pin = popen(command_to_open_fname, "r"))) {
912 log_OS_error("Unable to openin filelist_fname");
913 mr_free(command_to_open_fname);
914 return (NULL);
915 }
916 mr_free(command_to_open_fname);
917
918 open_evalcall_form("Loading filelist from disk");
919 for (tmp1 = fgets(fname, MAX_STR_LEN, pin); !feof(pin);
920 tmp1 = fgets(fname, MAX_STR_LEN, pin)) {
921 if (((fname[strlen(fname) - 1] == 13) || (fname[strlen(fname) - 1] == 10)) && (strlen(fname) > 0)) {
922 fname[strlen(fname) - 1] = '\0';
923 }
924// strip_spaces (fname);
925 if (!strlen(fname)) {
926 continue;
927 }
928 for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
929 if (fname[pos_in_fname] != '/') {
930 continue;
931 }
932 strcpy(tmp, fname);
933 tmp[pos_in_fname] = '\0';
934 if (strlen(tmp)) {
935 add_string_at_node(filelist, tmp);
936 }
937 }
938 add_string_at_node(filelist, fname);
939 if (!(++lino % 1111)) {
940 percentage = (int) (lino * 100 / lines_in_filelist);
941 update_evalcall_form(percentage);
942 }
943 }
944 paranoid_pclose(pin);
945 close_evalcall_form();
946 log_it("Finished loading filelist");
947 return (filelist);
948}
949
950
951/**
952 * Log a list of files in @p node.
953 * @param node The toplevel node to use.
954 */
955void show_filelist(struct s_node *node)
956{
957 static int depth = 0;
958 static char current_string[200];
959
960 if (depth == 0) {
961 log_msg(0, "----------------show filelist--------------");
962 }
963 current_string[depth] = node->ch;
964
965 log_msg(3, "depth=%d", depth);
966 if (node->down) {
967 log_msg(3, "moving down");
968 depth++;
969 show_filelist(node->down);
970 depth--;
971 }
972
973 if (!node->ch) {
974 log_msg(0, "%s\n", current_string);
975 }
976
977 if (node->right) {
978 log_msg(3, "moving right");
979 show_filelist(node->right);
980 }
981 if (depth == 0) {
982 log_msg(0, "----------------show filelist--------------");
983 }
984 return;
985}
986
987
988
989
990/**
991 * Reset the filelist to the state it was when it was loaded. This does not
992 * touch the file on disk.
993 * @param filelist The filelist tree structure.
994 */
995void reload_filelist(struct s_node *filelist)
996{
997 assert(filelist != NULL);
998 toggle_node_selection(filelist, FALSE);
999 toggle_path_expandability(filelist, "/", FALSE);
1000 toggle_all_root_dirs_on(filelist);
1001}
1002
1003
1004
1005/**
1006 * Save a filelist tree structure to disk.
1007 * @param filelist The filelist tree structure to save.
1008 * @param outfname Where to save it.
1009 */
1010void save_filelist(struct s_node *filelist, char *outfname)
1011{
1012 /*@ int ********************************************************* */
1013 static int percentage;
1014 static int depth = 0;
1015
1016 /*@ buffers ***************************************************** */
1017 static char str[MAX_STR_LEN];
1018
1019 /*@ structures ************************************************** */
1020 struct s_node *node;
1021
1022 /*@ pointers **************************************************** */
1023 static FILE *fout = NULL;
1024
1025 /*@ long ******************************************************** */
1026 static long lines_in_filelist = 0;
1027 static long lino = 0;
1028
1029 /*@ end vars *************************************************** */
1030
1031 assert(filelist != NULL);
1032 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1033 if (depth == 0) {
1034 log_to_screen("Saving filelist");
1035 if (!(fout = fopen(outfname, "w"))) {
1036 fatal_error("Cannot openout/save filelist");
1037 }
1038 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
1039 open_evalcall_form("Saving selection to disk");
1040 }
1041 for (node = filelist; node != NULL; node = node->right) {
1042 str[depth] = node->ch;
1043 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1044 if (!node->ch) {
1045// if (node->selected)
1046// {
1047 fprintf(fout, "%s\n", str);
1048// }
1049 if (!(++lino % 1111)) {
1050 percentage = (int) (lino * 100 / lines_in_filelist);
1051 update_evalcall_form(percentage);
1052 }
1053 }
1054 if (node->down) {
1055 depth++;
1056 save_filelist(node->down, "");
1057 depth--;
1058 }
1059 }
1060 if (depth == 0) {
1061 paranoid_fclose(fout);
1062 close_evalcall_form();
1063 log_it("Finished saving filelist");
1064 }
1065}
1066
1067
1068
1069/**
1070 * Toggle all root dirs on.
1071 * @param filelist The filelist tree structure to operate on.
1072 * @bug I don't understand this function. Would someone care to explain it?
1073 */
1074void toggle_all_root_dirs_on(struct s_node *filelist)
1075{
1076 /*@ structures ************************************************** */
1077 struct s_node *node;
1078
1079 /*@ int ********************************************************* */
1080 static int depth = 0;
1081 static int root_dirs_expanded;
1082
1083 /*@ buffers ***************************************************** */
1084 static char filename[MAX_STR_LEN];
1085
1086 /*@ end vars *************************************************** */
1087
1088 assert(filelist != NULL);
1089 if (depth == 0) {
1090 log_it("Toggling all root dirs ON");
1091 root_dirs_expanded = 0;
1092 }
1093 for (node = filelist; node != NULL; node = node->right) {
1094 filename[depth] = node->ch;
1095 if (node->ch == '\0' && strlen(filename) > 1
1096 && (!strchr(filename + 1, '/'))) {
1097 node->selected = FALSE;
1098 node->expanded = TRUE;
1099// log_it (filename);
1100 root_dirs_expanded++;
1101 }
1102 if (node->down) {
1103 depth++;
1104 toggle_all_root_dirs_on(node->down);
1105 depth--;
1106 }
1107 }
1108 if (depth == 0) {
1109 log_it("Finished toggling all root dirs ON");
1110 }
1111}
1112
1113
1114/**
1115 * Toggle the expandability of a path.
1116 * @param filelist The filelist tree to operate on.
1117 * @param pathname The path to toggle expandability of.
1118 * @param on_or_off Whether to toggle it on or off.
1119 * @bug I don't understand this function. Would someone care to explain it?
1120 */
1121void
1122toggle_path_expandability(struct s_node *filelist, char *pathname,
1123 bool on_or_off)
1124{
1125
1126 /*@ int ******************************************************** */
1127 static int depth = 0;
1128 static int total_expanded;
1129 static int root_depth;
1130 int j;
1131 /*@ structures ************************************************* */
1132 struct s_node *node;
1133
1134 /*@ buffers **************************************************** */
1135 static char current_filename[MAX_STR_LEN];
1136
1137/* char tmp[MAX_STR_LEN+2]; */
1138
1139 /*@ end vars *************************************************** */
1140
1141 assert(filelist != NULL);
1142 assert_string_is_neither_NULL_nor_zerolength(pathname);
1143 if (depth == 0) {
1144 total_expanded = 0;
1145// log_it ("Toggling path's expandability");
1146 for (root_depth = (int) strlen(pathname);
1147 root_depth > 0 && pathname[root_depth - 1] != '/';
1148 root_depth--);
1149 if (root_depth < 2) {
1150 root_depth = (int) strlen(pathname);
1151 }
1152 }
1153 for (node = filelist; node != NULL; node = node->right) {
1154 current_filename[depth] = node->ch;
1155 if (node->down) {
1156 depth++;
1157 toggle_path_expandability(node->down, pathname, on_or_off);
1158 depth--;
1159 }
1160 if (node->ch == '\0') {
1161 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1162 for (j = root_depth;
1163 current_filename[j] != '/'
1164 && current_filename[j] != '\0'; j++);
1165 if (current_filename[j] != '\0') {
1166 for (j++;
1167 current_filename[j] != '/'
1168 && current_filename[j] != '\0'; j++);
1169 }
1170 if (current_filename[j] == '\0') {
1171 node->expanded =
1172 (!strcmp(pathname, current_filename) ? TRUE :
1173 on_or_off);
1174 }
1175 }
1176 }
1177 if (node->expanded) {
1178 if (total_expanded < ARBITRARY_MAXIMUM - 32
1179 || !strrchr(current_filename + strlen(pathname), '/')) {
1180 total_expanded++;
1181 } else {
1182 node->expanded = FALSE;
1183 }
1184 }
1185 }
1186 if (depth == 0) {
1187// log_it ("Finished toggling expandability");
1188 }
1189}
1190
1191/**
1192 * Toggle whether a path is selected.
1193 * @param filelist The filelist tree to operate on.
1194 * @param pathname The path to toggle selection of.
1195 * @param on_or_off Whether to toggle it on or off.
1196 * @bug I don't understand this function. Would someone care to explain it?
1197 */
1198void
1199toggle_path_selection(struct s_node *filelist, char *pathname,
1200 bool on_or_off)
1201{
1202 /*@ int ********************************************************* */
1203 static int depth = 0;
1204 int j;
1205
1206 /*@ structures ************************************************** */
1207 struct s_node *node;
1208
1209 /*@ buffers ***************************************************** */
1210 static char current_filename[MAX_STR_LEN];
1211
1212 /*@ end vars *************************************************** */
1213 assert(filelist != NULL);
1214 assert_string_is_neither_NULL_nor_zerolength(pathname);
1215 if (depth == 0) {
1216 log_it("Toggling path's selection");
1217 }
1218 for (node = filelist; node != NULL; node = node->right) {
1219 current_filename[depth] = node->ch;
1220 if (node->down) {
1221 depth++;
1222 toggle_path_selection(node->down, pathname, on_or_off);
1223 depth--;
1224 }
1225 if (node->ch == '\0') {
1226 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1227 for (j = 0;
1228 pathname[j] != '\0'
1229 && pathname[j] == current_filename[j]; j++);
1230 if (current_filename[j] == '/'
1231 || current_filename[j] == '\0') {
1232 node->selected = on_or_off;
1233 }
1234 }
1235 }
1236 }
1237 if (depth == 0) {
1238 log_it("Finished toggling selection");
1239 }
1240}
1241
1242
1243/**
1244 * Toggle node selection of a filelist tree.
1245 * @param filelist The filelist tree to operate on.
1246 * @param on_or_off Whether to toggle selection on or off.
1247 * @bug I don't understand this function. Would someone care to explain it?
1248 */
1249void toggle_node_selection(struct s_node *filelist, bool on_or_off)
1250{
1251 /*@ structure ************************************************** */
1252 struct s_node *node;
1253
1254 /*@ end vars *************************************************** */
1255 assert(filelist != NULL);
1256 for (node = filelist; node != NULL; node = node->right) {
1257 if (node->ch == '/') {
1258 continue;
1259 } /* don't go deep */
1260 if (node->ch == '\0') {
1261 node->selected = on_or_off;
1262 }
1263 if (node->down) {
1264 toggle_node_selection(node->down, on_or_off);
1265 }
1266 }
1267}
1268
1269
1270
1271/**
1272 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1273 */
1274char *g_skeleton_filelist = NULL;
1275
1276
1277
1278/**
1279 * Get the next entry in the space-separated list in @p incoming.
1280 * So if @p incoming was '"one and two" three four', we would
1281 * return "one and two".
1282 * @param incoming The list to get the next entry from.
1283 * @return The first item in the list (respecting double quotes).
1284 * @note The returned string points to static data that will be overwritten with each call.
1285 */
1286char *next_entry(char *incoming)
1287{
1288 char *sz_res;
1289 char *p;
1290 bool in_quotes = FALSE;
1291
1292 mr_asprintf(&sz_res, "%s", incoming);
1293 p = sz_res;
1294 while ((*p != '|' || in_quotes) && *p != '\0') {
1295 if (*p == '\"') {
1296 in_quotes = !in_quotes;
1297 }
1298 p++;
1299 }
1300 *p = '\0';
1301 return (sz_res);
1302}
1303
1304
1305
1306
1307/**
1308 * Number of entries in the skeleton filelist.
1309 */
1310long g_skeleton_entries = 0;
1311
1312
1313/**
1314 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1315 * @param dir The directory to list to @p fout.
1316 * @param sth The directories to skip (exclude).
1317 * @param fout The file to write everything to.
1318 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1319 * @return 0, always.
1320 * @bug Return value should be @c void.
1321 */
1322int open_and_list_dir(char *dir1, char *sth, FILE * fout,
1323 time_t time_of_last_full_backup)
1324{
1325 const char delims[] = "|";
1326
1327 DIR *dip;
1328 struct dirent *dit;
1329 struct stat statbuf;
1330 char *new;
1331 char *tmp;
1332 char *dir = NULL;
1333 static int percentage = 0;
1334 char *skip_these;
1335 char *new_with_pipe;
1336 char *strtmp = NULL;
1337 char *token = NULL;
1338 char *find_excludes = NULL;
1339 static char *name_of_evalcall_form;
1340 int i;
1341 int lastpos = 0;
1342 static int depth = 0;
1343 char *p;
1344 static int counter = 0;
1345 static int uberctr = 0;
1346 static char *find_skeleton_marker;
1347 static long skeleton_lino = 0;
1348 static time_t last_time = 0;
1349 time_t this_time;
1350
1351 /* dir is needed when we pass it to the shell */
1352 dir = mr_stresc(dir1, "`$\\\"(){}'[]&*?|!#~", '\\');
1353 p = strrchr(dir1, '/');
1354 if (p) {
1355 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
1356 mr_free(dir);
1357 return (0);
1358 }
1359 }
1360 mr_asprintf(&find_excludes, " ");
1361
1362 if (!depth) {
1363 malloc_string(name_of_evalcall_form);
1364 malloc_string(find_skeleton_marker);
1365 while((token = mr_strtok(sth, delims, &lastpos)) != NULL) {
1366 mr_strcat(find_excludes," -path %s -prune -o", token);
1367 mr_free(token);
1368 }
1369
1370/**
1371 * The maximum depth of directories to put in the skeleton filelist.
1372 * This is a balance between performance and a good progress indicator.
1373 */
1374#define MAX_SKEL_DEPTH 3
1375
1376#if linux
1377 // 2.6 has /sys as a proc-type thing -- must be excluded
1378 mr_asprintf(&strtmp, "find '%s' -maxdepth %d -fstype mvfs -prune -o -fstype devpts -prune -o -fstype tmpfs -prune -o -fstype devtmpfs -prune -o -fstype proc -prune -o -fstype selinuxfs -prune -o -fstype securityfs -prune -o -fstype hugetlbfs -o -fstype cgroup -prune -prune -o -fstype mqueue -prune -o -fstype debugfs -prune -o -fstype sysfs -prune -o -fstype rpc_pipefs -prune -o -fstype none -prune -o %s -type d -print > %s 2> /dev/null", dir, MAX_SKEL_DEPTH, find_excludes, g_skeleton_filelist);
1379#else
1380 // On BSD, for example, /sys is the kernel sources -- don't exclude
1381 mr_asprintf(&strtmp, "find '%s' -maxdepth %d -fstype mvfs -prune -o -path /proc -prune -o %s -type d -print > %s 2> /dev/null", dir, MAX_SKEL_DEPTH, find_excludes, g_skeleton_filelist);
1382#endif
1383 paranoid_free(find_excludes);
1384 log_msg(5, "find command = %s", strtmp);
1385 paranoid_system(strtmp);
1386 paranoid_free(strtmp);
1387 mr_asprintf(&tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
1388 g_skeleton_entries =
1389 1 + atol(call_program_and_get_last_line_of_output(tmp));
1390 paranoid_free(tmp);
1391 sprintf(name_of_evalcall_form, "Making catalog of %s", dir1);
1392 open_evalcall_form(name_of_evalcall_form);
1393 find_skeleton_marker[0] = '\0';
1394 skeleton_lino = 1;
1395 log_msg(5, "entries = %ld", g_skeleton_entries);
1396 percentage = 0;
1397 } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
1398 {
1399 sprintf(find_skeleton_marker,
1400 "grep -Fv '%s' %s > %s.new 2> /dev/null", dir,
1401 g_skeleton_filelist, g_skeleton_filelist);
1402// log_msg(0, "fsm = %s", find_skeleton_marker);
1403 if (!system(find_skeleton_marker)) {
1404 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1405 skeleton_lino++;
1406// log_msg(5, "Found %s", dir);
1407// log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
1408 sprintf(find_skeleton_marker, "mv -f %s.new %s",
1409 g_skeleton_filelist, g_skeleton_filelist);
1410// log_msg(6, "fsm = %s", find_skeleton_marker);
1411 paranoid_system(find_skeleton_marker);
1412 time(&this_time);
1413 if (this_time != last_time) {
1414 last_time = this_time;
1415#ifndef _XWIN
1416 if (!g_text_mode) {
1417 int cols, rows;
1418 newtGetScreenSize(&cols, &rows);
1419 mr_asprintf(&tmp, "Reading %-*s", cols, dir1);
1420 newtDrawRootText(0, g_noof_rows - 3, tmp);
1421 mr_free(tmp);
1422 }
1423#endif
1424 update_evalcall_form(percentage);
1425 }
1426 }
1427 }
1428
1429 depth++;
1430
1431// log_msg(0, "Cataloguing %s", dir);
1432 mr_asprintf(&skip_these, "|%s|", sth);
1433
1434 mr_asprintf(&new_with_pipe, "|%s|", dir1);
1435 if ((dip = opendir(dir1)) == NULL) {
1436 mr_asprintf(&tmp,"opendir %s", dir1);
1437 log_OS_error(tmp);
1438 paranoid_free(tmp);
1439 } else if (strstr(skip_these, new_with_pipe)) {
1440 log_msg(10, "Found dir ***%s**** excluded", dir1);
1441 fprintf(fout, "%s\n", dir1); // if excluded dir then print dir ONLY
1442 } else {
1443 log_msg(10, "Found dir ***%s**** parsed", dir1);
1444 fprintf(fout, "%s\n", dir1);
1445 while ((dit = readdir(dip)) != NULL) {
1446 i++;
1447 if (strcmp(dir1, "/")) {
1448 mr_asprintf(&new,"%s/%s",dir1,dit->d_name);
1449 } else {
1450 mr_asprintf(&new,"%s%s",dir1,dit->d_name);
1451 }
1452 paranoid_free(new_with_pipe);
1453 mr_asprintf(&new_with_pipe, "|%s|", new);
1454 if (strstr(skip_these, new_with_pipe)) {
1455 fprintf(fout, "%s\n", new);
1456 log_msg(10, "Found child dir ***%s**** excluded", new);
1457 paranoid_free(new_with_pipe);
1458 } else {
1459 paranoid_free(new_with_pipe);
1460 if (!lstat(new, &statbuf)) {
1461 if (!S_ISLNK(statbuf.st_mode)
1462 && S_ISDIR(statbuf.st_mode)) {
1463 log_msg(10, "Found child dir ***%s**** parsed", new);
1464 open_and_list_dir(new, skip_these, fout,
1465 time_of_last_full_backup);
1466 } else {
1467 if (time_of_last_full_backup == 0
1468 || time_of_last_full_backup <
1469 statbuf.st_ctime) {
1470 log_msg(10, "Found child file ***%s**** parsed", new);
1471 fprintf(fout, "%s\n", new);
1472 if ((counter++) > 128) {
1473 counter = 0;
1474 uberctr++;
1475 mr_asprintf(&tmp, " %c ", special_dot_char(uberctr));
1476#ifndef _XWIN
1477 if (!g_text_mode) {
1478 newtDrawRootText(77, g_noof_rows - 3,
1479 tmp);
1480 newtRefresh();
1481 }
1482#endif
1483 paranoid_free(tmp);
1484 }
1485 }
1486 }
1487 }
1488 }
1489 paranoid_free(new);
1490 }
1491 }
1492 paranoid_free(new_with_pipe);
1493 paranoid_free(skip_these);
1494 mr_free(dir);
1495
1496 if (dip) {
1497 if (closedir(dip) == -1) {
1498 log_OS_error("closedir");
1499 }
1500 }
1501 depth--;
1502 if (!depth) {
1503 close_evalcall_form();
1504 paranoid_free(name_of_evalcall_form);
1505 paranoid_free(find_skeleton_marker);
1506 unlink(g_skeleton_filelist);
1507 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1508 }
1509 return (0);
1510}
1511
1512
1513
1514/**
1515 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1516 * @param logfile Unused.
1517 * @param tmpdir The tmpdir of the backup.
1518 * @param scratchdir The scratchdir of the backup.
1519 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1520 * @param excp The paths to NOT back up.
1521 * @param differential The differential level (currently only 0 and 1 are supported).
1522 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1523 * @return 0, always.
1524 * @bug @p logfile is unused.
1525 * @bug Return value is meaningless.
1526 */
1527int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1528 char *include_paths, char *excp, int differential,
1529 char *userdef_filelist)
1530{
1531 char *p, *q;
1532 char *sz_datefile;
1533 char *sz_filelist, *tmp;
1534 char *exclude_paths = NULL;
1535 FILE *fout;
1536 char *command = NULL;
1537 time_t time_of_last_full_backup = 0;
1538 struct stat statbuf;
1539 char *tmp1 = NULL;
1540 char *tmp2 = NULL;
1541
1542 malloc_string(tmp);
1543 malloc_string(g_skeleton_filelist);
1544 mr_asprintf(&sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
1545 if (!include_paths && !userdef_filelist) {
1546 fatal_error
1547 ("Please supply either include_paths or userdef_filelist");
1548 }
1549// make hole for filelist
1550 mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
1551 paranoid_system(command);
1552 mr_free(command);
1553
1554 mr_asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
1555 make_hole_for_file(sz_filelist);
1556
1557 if (differential == 0) {
1558 // restore last good datefile if it exists
1559 mr_asprintf(&command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
1560 run_program_and_log_output(command, 3);
1561 mr_free(command);
1562
1563 // backup last known good datefile just in case :)
1564 if (does_file_exist(sz_datefile)) {
1565 mr_asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
1566 sz_datefile);
1567 paranoid_system(command);
1568 mr_free(command);
1569 }
1570 make_hole_for_file(sz_datefile);
1571 write_one_liner_data_file(sz_datefile,
1572 call_program_and_get_last_line_of_output
1573 ("date +%s"));
1574 } else if (lstat(sz_datefile, &statbuf)) {
1575 log_msg(2,
1576 "Warning - unable to find date of previous backup. Full backup instead.");
1577 differential = 0;
1578 time_of_last_full_backup = 0;
1579 } else {
1580 time_of_last_full_backup = statbuf.st_mtime;
1581 log_msg(2, "Differential backup. Yay.");
1582 }
1583 paranoid_free(sz_datefile);
1584
1585// use user-specified filelist (if specified)
1586 if (userdef_filelist) {
1587 log_msg(1,
1588 "Using the user-specified filelist - %s - instead of calculating one",
1589 userdef_filelist);
1590 mr_asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
1591 if (run_program_and_log_output(command, 3)) {
1592 mr_free(command);
1593 fatal_error("Failed to copy user-specified filelist");
1594 }
1595 mr_free(command);
1596 } else {
1597 log_msg(2, "include_paths = '%s'", include_paths);
1598 log_msg(1, "Calculating filelist");
1599 mr_asprintf(&tmp2, "%s", call_program_and_get_last_line_of_output("mount | grep -Ew 'ntfs|ntfs-3g|fat|vfat|dos' | awk '{print $3}'"));
1600 if (strlen(tmp2) < 1) {
1601 mr_asprintf(&tmp1," ");
1602 } else {
1603 log_msg(2, "Found windows FS: %s",tmp2);
1604 mr_asprintf(&tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
1605 paranoid_free(tmp2);
1606 mr_asprintf(&tmp2, "%s", call_program_and_get_last_line_of_output(tmp1));
1607 log_msg(2, "Found windows files: %s",tmp2);
1608 }
1609 paranoid_free(tmp1);
1610
1611 mr_asprintf(&exclude_paths, MONDO_CACHE"|%s|%s|%s|.|..|"MNT_CDROM"|"MNT_FLOPPY"|/media|/tmp|/proc|/sys|/run|/dev/shm|"MINDI_CACHE, tmp2, (tmpdir[0] == '/' && tmpdir[1] == '/') ? (tmpdir + 1) : tmpdir, (scratchdir[0] == '/' && scratchdir[1] == '/') ? (scratchdir + 1) : scratchdir);
1612 if (excp != NULL) {
1613 mr_strcat(exclude_paths,"|%s",excp);
1614 }
1615 paranoid_free(tmp2);
1616
1617 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1618 log_msg(2,
1619 "Generating skeleton filelist so that we can track our progress");
1620 sprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
1621 make_hole_for_file(g_skeleton_filelist);
1622 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1623 log_msg(2, "Opening out filelist to %s", sz_filelist);
1624 if (!(fout = fopen(sz_filelist, "w"))) {
1625 fatal_error("Cannot openout to sz_filelist");
1626 }
1627 if (strlen(include_paths) == 0) {
1628 log_msg(1, "Including only '/' in %s", sz_filelist);
1629 open_and_list_dir("/", exclude_paths, fout,
1630 time_of_last_full_backup);
1631 } else {
1632 p = include_paths;
1633 while (*p) {
1634 q = next_entry(p);
1635 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1636 open_and_list_dir(q, exclude_paths, fout,
1637 time_of_last_full_backup);
1638 p += strlen(q);
1639 paranoid_free(q);
1640 while (*p == '|') {
1641 p++;
1642 }
1643 }
1644 }
1645 paranoid_fclose(fout);
1646 }
1647 log_msg(2, "Copying new filelist to scratchdir");
1648 mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
1649 paranoid_system(command);
1650 mr_free(command);
1651
1652 mr_asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
1653 paranoid_system(command);
1654 mr_free(command);
1655
1656 mr_asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
1657 paranoid_system(command);
1658 mr_free(command);
1659
1660 paranoid_free(sz_filelist);
1661 log_msg(2, "Freeing variables");
1662 paranoid_free(exclude_paths);
1663 paranoid_free(tmp);
1664 paranoid_free(g_skeleton_filelist);
1665 log_msg(2, "Exiting");
1666 return (0);
1667}
1668
1669/**
1670 * Wrapper around mondo_makefilelist().
1671 * @param bkpinfo The backup information structure. Fields used:
1672 * - @c bkpinfo->differential
1673 * - @c bkpinfo->exclude_paths
1674 * - @c bkpinfo->include_paths
1675 * - @c bkpinfo->make_filelist
1676 * - @c bkpinfo->scratchdir
1677 * - @c bkpinfo->tmpdir
1678 * @return 0 for success, nonzero for failure.
1679 * @see mondo_makefilelist
1680 */
1681int prepare_filelist()
1682{
1683
1684 /*@ int **************************************************** */
1685 int res = 0;
1686
1687 assert(bkpinfo != NULL);
1688 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir,
1689 bkpinfo->scratchdir);
1690 if (bkpinfo->make_filelist) {
1691 mvaddstr_and_log_it(g_currentY, 0,
1692 "Making catalog of files to be backed up");
1693 } else {
1694 mvaddstr_and_log_it(g_currentY, 0,
1695 "Using supplied catalog of files to be backed up");
1696 }
1697
1698 if (bkpinfo->make_filelist) {
1699 res =
1700 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1701 bkpinfo->scratchdir, bkpinfo->include_paths,
1702 bkpinfo->exclude_paths,
1703 bkpinfo->differential, NULL);
1704 } else {
1705 res =
1706 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1707 bkpinfo->scratchdir, NULL,
1708 bkpinfo->exclude_paths,
1709 bkpinfo->differential,
1710 bkpinfo->include_paths);
1711 }
1712
1713 if (res) {
1714 log_OS_error("Call to mondo_makefilelist failed");
1715 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1716 } else {
1717 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1718 }
1719 return (res);
1720}
1721
1722
1723/**
1724 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1725 * @param startnode The node containing the root of the directory tree.
1726 * @param string_to_find The string to look for at @p startnode.
1727 * @return The node containing the last element of @p string_to_find, or NULL if
1728 * it was not found.
1729 */
1730struct s_node *find_string_at_node(struct s_node *startnode,
1731 char *string_to_find)
1732{
1733 /*@ int ******************************************************** */
1734 static int depth = 0;
1735 static char original_string[MAX_STR_LEN];
1736
1737 /*@ sturctures ************************************************* */
1738 struct s_node *node;
1739
1740 /*@ char ****************************************************** */
1741 char char_to_find;
1742
1743 /*@ bools ****************************************************** */
1744
1745 if (!depth) {
1746 strcpy(original_string, string_to_find);
1747 }
1748
1749 assert(startnode != NULL);
1750 assert(string_to_find != NULL);
1751
1752 log_msg(7, "starting --- str=%s", string_to_find);
1753
1754/* walk across tree if necessary */
1755 node = startnode;
1756 char_to_find = string_to_find[0];
1757 if (node->right != NULL && node->ch < char_to_find) {
1758 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1759 char_to_find, node->ch, (node->right)->ch);
1760 return (find_string_at_node(node->right, string_to_find));
1761 }
1762
1763/* walk down tree if appropriate */
1764 if (node->down != NULL && node->ch == char_to_find) {
1765 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1766 depth++;
1767 node = find_string_at_node(node->down, string_to_find + 1);
1768 depth--;
1769 return (node);
1770 }
1771
1772 if (char_to_find == '\0' && node->ch == '\0') {
1773 log_msg(7, "%s is in tree", original_string);
1774 return (node);
1775 } else {
1776 log_msg(7, "%s is NOT in tree", original_string);
1777 return (NULL);
1778 }
1779}
1780
1781
1782
1783/**
1784 * Write all entries in @p needles_list_fname which are also in
1785 * @p filelist to @p matches_list_fname.
1786 * @param needles_list_fname A file containing strings to look for, 1 per line.
1787 * @param filelist The node for the root of the directory structure to search in.
1788 * @param matches_list_fname The filename where we should put the matches.
1789 * @return The number of matches found.
1790 */
1791long save_filelist_entries_in_common(char *needles_list_fname,
1792 struct s_node *filelist,
1793 char *matches_list_fname,
1794 bool use_star)
1795{
1796 int retval = 0;
1797 struct s_node *found_node;
1798 FILE *fin;
1799 FILE *fout;
1800 char *fname;
1801 char *tmp;
1802 size_t len = 0; // Scrub's patch doesn't work without that
1803
1804// log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
1805 malloc_string(fname);
1806 malloc_string(tmp);
1807 log_msg(5, "starting");
1808 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1809 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1810 if (!(fin = fopen(needles_list_fname, "r"))) {
1811 fatal_error("Cannot openin needles_list_fname");
1812 }
1813 if (!(fout = fopen(matches_list_fname, "w"))) {
1814 fatal_error("Cannot openout matches_list_fname");
1815 }
1816 while (!feof(fin)) {
1817// fscanf(fin, "%s\n", fname);
1818 len = MAX_STR_LEN - 1;
1819 if (getline(&fname, &len, fin)) {
1820 // FIXME
1821 }
1822 if (!use_star) {
1823 if (fname[0] == '/') {
1824 strcpy(tmp, fname);
1825 } else {
1826 tmp[0] = '/';
1827 strcpy(tmp + 1, fname);
1828 }
1829 strcpy(fname, tmp);
1830 }
1831 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1832 fname[strlen(fname) - 1] = '\0';
1833 }
1834
1835 log_msg(5, "Looking for '%s'", fname);
1836 found_node = find_string_at_node(filelist, fname);
1837 if (found_node) {
1838 if (found_node->selected) {
1839 if (fname[0] == '/') {
1840 strcpy(tmp, fname + 1);
1841 strcpy(fname, tmp);
1842 }
1843 log_msg(5, "Found '%s'", fname);
1844 turn_wildcard_chars_into_literal_chars(tmp, fname);
1845 fprintf(fout, "%s\n", tmp);
1846 retval++;
1847 }
1848 }
1849 }
1850 paranoid_fclose(fout);
1851 paranoid_fclose(fin);
1852 paranoid_free(fname);
1853 paranoid_free(tmp);
1854 return (retval);
1855}
1856
1857
1858
1859
1860
1861
1862/**
1863 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1864 * @p filelist.
1865 * @param filelist The top node of the directory structure to add the files to.
1866 * @param list_of_files_fname The file containing the files to add, 1 per line.
1867 * @param flag_em If TRUE, then flag the added files for restoration.
1868 * @return 0 for success, nonzero for failure.
1869 */
1870int add_list_of_files_to_filelist(struct s_node *filelist,
1871 char *list_of_files_fname, bool flag_em)
1872{
1873 FILE *fin;
1874 char *tmp;
1875 char *p;
1876 struct s_node *nod;
1877
1878 malloc_string(tmp);
1879 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1880 if (!(fin = fopen(list_of_files_fname, "r"))) {
1881 log_it("%s",list_of_files_fname);
1882 return (1);
1883 }
1884 for (p = fgets(tmp, MAX_STR_LEN, fin); !feof(fin) && (p != NULL);
1885 p = fgets(tmp, MAX_STR_LEN, fin)) {
1886 if (!tmp[0]) {
1887 continue;
1888 }
1889 if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
1890 && strlen(tmp) > 0) {
1891 tmp[strlen(tmp) - 1] = '\0';
1892 }
1893 log_msg(2, "tmp = '%s'", tmp);
1894 if (!tmp[0]) {
1895 continue;
1896 }
1897 if ((nod = find_string_at_node(filelist, tmp))) {
1898 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1899 } else {
1900 add_string_at_node(filelist, tmp);
1901 nod = find_string_at_node(filelist, tmp);
1902 }
1903
1904 if (nod && flag_em) {
1905 toggle_path_selection(filelist, tmp, TRUE);
1906 log_msg(5, "Flagged '%s'", tmp);
1907 }
1908 }
1909 paranoid_fclose(fin);
1910 paranoid_free(tmp);
1911 return (0);
1912}
1913
1914/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.