source: MondoRescue/branches/3.1/mondo/src/common/libmondo-filelist.c@ 3148

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

2nd phase for svn merge -r 2935:3146 ../3.0

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