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

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