source: MondoRescue/branches/3.3/mondo/src/common/libmondo-filelist.c@ 3878

Last change on this file since 3878 was 3878, checked in by Bruno Cornec, 2 years ago

Fix compiler errors

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