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

Last change on this file since 3875 was 3871, checked in by Bruno Cornec, 3 months ago

Fix syntax issues

  • Property svn:keywords set to Id
File size: 49.5 KB
Line 
1/*
2 $Id: libmondo-filelist.c 3871 2024-03-07 17:06:33Z 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 3871 2024-03-07 17:06:33Z 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->tmpdir, 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->tmpdir);
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
146
147int sort_file(char *orig_fname)
148{
149 char *tmp_fname = NULL;
150 char *command = NULL;
151 int retval = 0;
152
153 log_msg(5, "Sorting file %s", orig_fname);
154
155 if (!does_file_exist(orig_fname)) {
156 log_msg(2, "file %s empty", orig_fname);
157 return (0);
158 } // no sense in trying to sort an empty file
159
160 mr_asprintf(tmp_fname, "%s/sortfile", bkpinfo->tmpdir);
161 mr_asprintf(command, "sort %s > %s 2>> %s", orig_fname, tmp_fname, MONDO_LOGFILE);
162 retval = system(command);
163 mr_free(command);
164
165 if (retval) {
166 log_msg(2, "Failed to sort %s - oh dear", orig_fname);
167 } else {
168 log_msg(5, "Sorted %s --> %s OK. Copying it back to %s now", orig_fname, tmp_fname, orig_fname);
169 mr_asprintf(command, "mv -f %s %s", tmp_fname, orig_fname);
170 retval += run_program_and_log_output(command, 5);
171 mr_free(command);
172
173 if (retval) {
174 log_msg(2, "Failed to copy %s back to %s - oh dear", tmp_fname, orig_fname);
175 } else {
176 log_msg(5, "%s was sorted OK.", orig_fname);
177 }
178 }
179 mr_free(tmp_fname);
180 log_msg(5, "Finished sorting file %s", orig_fname);
181 return (retval);
182}
183
184
185
186/**
187 * Chop the filelist into sets.
188 * Each fileset is a list of files whose total (uncompressed) size is usually
189 * about X KB. Files bigger than 8X KB are placed in a "biggielist"; they will
190 * be sliced and compressed separately from the regular files.
191 *
192 * @param filelist The big filelist (filelist.full) to chop up.
193 * @param outdir The directory to place the files (filelist.N where N is
194 * an integer, biggielist.txt, and LAST-FILELIST-NUMBER) created
195 * @param maxsetsizeK Optimal size of a fileset (X above).
196 * @return number of errors encountered (0 for success).
197 */
198int chop_filelist(char *filelist, char *outdir, long maxsetsizeK)
199{
200/*@ long ****************************************/
201 long lino = 0;
202 // A big file has more than 64 MB of real content
203 long curr_set_size;
204 long noof_lines;
205 long siz;
206
207 /*@ int **************************************** */
208 int i;
209 long curr_set_no;
210
211 /*@ buffers ************************************* */
212 char *outfname = NULL;
213 char *biggie_fname = NULL;
214 char *incoming = NULL;
215 char *tmp = NULL;
216
217 /*@ pointers *********************************** */
218 FILE *fin;
219 FILE *fout;
220 FILE *fbig;
221
222 /*@ structures ********************************* */
223 struct stat buf;
224 int err = 0;
225
226 assert_string_is_neither_NULL_nor_zerolength(filelist);
227 assert_string_is_neither_NULL_nor_zerolength(outdir);
228 assert(maxsetsizeK > 0);
229
230 log_it("filelist=%s;", filelist);
231 open_evalcall_form("Dividing filelist into sets");
232 noof_lines = count_lines_in_file(filelist);
233 if (!(fin = fopen(filelist, "r"))) {
234 log_OS_error("Cannot openin filelist");
235 return (0);
236 }
237 curr_set_no = 0;
238 curr_set_size = 0;
239 mr_asprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
240 mr_asprintf(biggie_fname, "%s/biggielist.txt", outdir);
241 log_it("outfname=%s; biggie_fname=%s", outfname, biggie_fname);
242 if (!(fbig = fopen(biggie_fname, "w"))) {
243 log_OS_error("Cannot openout biggie_fname");
244 err++;
245 mr_free(outfname);
246 mr_free(biggie_fname);
247 return (curr_set_no + 1);
248 }
249 if (!(fout = fopen(outfname, "w"))) {
250 log_OS_error("Cannot openout outfname");
251 err++;
252 mr_free(outfname);
253 mr_free(biggie_fname);
254 return (curr_set_no + 1);
255 }
256
257 mr_getline(incoming, fin);
258 while (!feof(fin)) {
259 lino++;
260 i = strlen(incoming) - 1;
261 if (i < 0) {
262 i = 0;
263 }
264 if (incoming[i] < 32) {
265 incoming[i] = '\0';
266 }
267 if (!strncmp(incoming, "/dev/", 5)) {
268 siz = 1;
269 } else if (lstat(incoming, &buf) != 0) {
270 siz = 0;
271 } else {
272 // blocks are 512 bytes long - cf man 2 stat - Pass to the previous unit (MB => kB e.g.)
273 // use blocks instead of size to allow sparse file correct handling as much as possible
274 siz = (long) ((buf.st_blocks*512) >> 10);
275 }
276 if (siz > g_max_biggie_size) {
277 log_msg(10, "Adding %s to big files (size = %ld)", incoming, siz);
278 fprintf(fbig, "%s\n", incoming);
279 } else {
280 curr_set_size += siz;
281 log_msg(10, "Adding %s to filelist %d (size = %ld)", incoming, curr_set_no, siz);
282 fprintf(fout, "%s\n", incoming);
283 if (curr_set_size > maxsetsizeK) {
284 paranoid_fclose(fout);
285 sort_file(outfname);
286 mr_free(outfname);
287 curr_set_no++;
288 curr_set_size = 0;
289
290 mr_asprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
291 if (!(fout = fopen(outfname, "w"))) {
292 log_OS_error("Unable to openout outfname");
293 err++;
294 mr_free(outfname);
295 mr_free(biggie_fname);
296 mr_free(incoming);
297 return (curr_set_no + 1);
298 }
299 update_evalcall_form((int) (lino * 100 / noof_lines));
300 }
301 }
302 mr_free(incoming);
303 mr_getline(incoming, fin);
304 }
305 mr_free(incoming);
306
307 paranoid_fclose(fin);
308 paranoid_fclose(fout);
309 paranoid_fclose(fbig);
310
311 if (length_of_file(outfname) <= 2) {
312 unlink(outfname);
313 g_noof_sets--;
314 }
315 g_noof_sets = curr_set_no;
316 sort_file(outfname);
317 mr_free(outfname);
318
319 sort_file(biggie_fname);
320 mr_free(biggie_fname);
321
322 mr_asprintf(outfname, "%s/LAST-FILELIST-NUMBER", outdir);
323 mr_asprintf(tmp, "%ld", curr_set_no);
324 if (write_one_liner_data_file(outfname, tmp)) {
325 log_OS_error
326 ("Unable to echo write one-liner to LAST-FILELIST-NUMBER");
327 err = 1;
328 }
329 mr_free(tmp);
330 mr_free(outfname);
331
332 if (curr_set_no == 0) {
333 log_msg(1, "Only one fileset. Fine.");
334 } else {
335 log_msg(1, "Filelist divided into %ld sets", curr_set_no + 1);
336 }
337 close_evalcall_form();
338 /* This is to work around an obscure bug in Newt; open a form, close it,
339 carry on... I don't know why it works but it works. If you don't do this
340 then update_progress_form() won't show the "time taken / time remaining"
341 line. The bug only crops up AFTER the call to chop_filelist(). Weird. */
342 if (!g_text_mode) {
343 open_progress_form("", "", "", "", 100);
344 newtPopHelpLine();
345 newtFormDestroy(g_progressForm);
346 newtPopWindow();
347 }
348 return (err ? 0 : curr_set_no + 1);
349}
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/**
975 * Reset the filelist to the state it was when it was loaded. This does not
976 * touch the file on disk.
977 * @param filelist The filelist tree structure.
978 */
979void reload_filelist(struct s_node *filelist)
980{
981 assert(filelist != NULL);
982 toggle_node_selection(filelist, FALSE);
983 toggle_path_expandability(filelist, "/", FALSE);
984 toggle_all_root_dirs_on(filelist);
985}
986
987
988
989/**
990 * Save a filelist tree structure to disk.
991 * @param filelist The filelist tree structure to save.
992 * @param outfname Where to save it.
993 */
994void save_filelist(struct s_node *filelist, char *outfname)
995{
996 /*@ int ********************************************************* */
997 static int percentage;
998 static int depth = 0;
999
1000 /*@ buffers ***************************************************** */
1001 static char str[MAX_STR_LEN];
1002
1003 /*@ structures ************************************************** */
1004 struct s_node *node;
1005
1006 /*@ pointers **************************************************** */
1007 static FILE *fout = NULL;
1008
1009 /*@ long ******************************************************** */
1010 static long lines_in_filelist = 0;
1011 static long lino = 0;
1012
1013 /*@ end vars *************************************************** */
1014
1015 assert(filelist != NULL);
1016 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1017 if (depth == 0) {
1018 log_to_screen("Saving filelist");
1019 if (!(fout = fopen(outfname, "w"))) {
1020 fatal_error("Cannot openout/save filelist");
1021 }
1022 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
1023 open_evalcall_form("Saving selection to disk");
1024 }
1025 for (node = filelist; node != NULL; node = node->right) {
1026 str[depth] = node->ch;
1027 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1028 if (!node->ch) {
1029// if (node->selected)
1030// {
1031 fprintf(fout, "%s\n", str);
1032// }
1033 if (!(++lino % 1111)) {
1034 percentage = (int) (lino * 100 / lines_in_filelist);
1035 update_evalcall_form(percentage);
1036 }
1037 }
1038 if (node->down) {
1039 depth++;
1040 save_filelist(node->down, "");
1041 depth--;
1042 }
1043 }
1044 if (depth == 0) {
1045 paranoid_fclose(fout);
1046 close_evalcall_form();
1047 log_it("Finished saving filelist");
1048 }
1049}
1050
1051
1052
1053/**
1054 * Toggle all root dirs on.
1055 * @param filelist The filelist tree structure to operate on.
1056 * @bug I don't understand this function. Would someone care to explain it?
1057 */
1058void toggle_all_root_dirs_on(struct s_node *filelist)
1059{
1060 /*@ structures ************************************************** */
1061 struct s_node *node;
1062
1063 /*@ int ********************************************************* */
1064 static int depth = 0;
1065 static int root_dirs_expanded;
1066
1067 /*@ buffers ***************************************************** */
1068 static char filename[MAX_STR_LEN];
1069
1070 /*@ end vars *************************************************** */
1071
1072 assert(filelist != NULL);
1073 if (depth == 0) {
1074 log_it("Toggling all root dirs ON");
1075 root_dirs_expanded = 0;
1076 }
1077 for (node = filelist; node != NULL; node = node->right) {
1078 filename[depth] = node->ch;
1079 if (node->ch == '\0' && strlen(filename) > 1
1080 && (!strchr(filename + 1, '/'))) {
1081 node->selected = FALSE;
1082 node->expanded = TRUE;
1083// log_it (filename);
1084 root_dirs_expanded++;
1085 }
1086 if (node->down) {
1087 depth++;
1088 toggle_all_root_dirs_on(node->down);
1089 depth--;
1090 }
1091 }
1092 if (depth == 0) {
1093 log_it("Finished toggling all root dirs ON");
1094 }
1095}
1096
1097
1098/**
1099 * Toggle the expandability of a path.
1100 * @param filelist The filelist tree to operate on.
1101 * @param pathname The path to toggle expandability of.
1102 * @param on_or_off Whether to toggle it on or off.
1103 * @bug I don't understand this function. Would someone care to explain it?
1104 */
1105void
1106toggle_path_expandability(struct s_node *filelist, char *pathname,
1107 bool on_or_off)
1108{
1109
1110 /*@ int ******************************************************** */
1111 static int depth = 0;
1112 static int total_expanded;
1113 static int root_depth;
1114 int j;
1115 /*@ structures ************************************************* */
1116 struct s_node *node;
1117
1118 /*@ buffers **************************************************** */
1119 static char current_filename[MAX_STR_LEN];
1120
1121/* char tmp[MAX_STR_LEN+2]; */
1122
1123 /*@ end vars *************************************************** */
1124
1125 assert(filelist != NULL);
1126 assert_string_is_neither_NULL_nor_zerolength(pathname);
1127 if (depth == 0) {
1128 total_expanded = 0;
1129// log_it ("Toggling path's expandability");
1130 for (root_depth = (int) strlen(pathname);
1131 root_depth > 0 && pathname[root_depth - 1] != '/';
1132 root_depth--);
1133 if (root_depth < 2) {
1134 root_depth = (int) strlen(pathname);
1135 }
1136 }
1137 for (node = filelist; node != NULL; node = node->right) {
1138 current_filename[depth] = node->ch;
1139 if (node->down) {
1140 depth++;
1141 toggle_path_expandability(node->down, pathname, on_or_off);
1142 depth--;
1143 }
1144 if (node->ch == '\0') {
1145 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1146 for (j = root_depth;
1147 current_filename[j] != '/'
1148 && current_filename[j] != '\0'; j++);
1149 if (current_filename[j] != '\0') {
1150 for (j++;
1151 current_filename[j] != '/'
1152 && current_filename[j] != '\0'; j++);
1153 }
1154 if (current_filename[j] == '\0') {
1155 node->expanded =
1156 (!strcmp(pathname, current_filename) ? TRUE :
1157 on_or_off);
1158 }
1159 }
1160 }
1161 if (node->expanded) {
1162 if (total_expanded < ARBITRARY_MAXIMUM - 32
1163 || !strrchr(current_filename + strlen(pathname), '/')) {
1164 total_expanded++;
1165 } else {
1166 node->expanded = FALSE;
1167 }
1168 }
1169 }
1170 if (depth == 0) {
1171// log_it ("Finished toggling expandability");
1172 }
1173}
1174
1175/**
1176 * Toggle whether a path is selected.
1177 * @param filelist The filelist tree to operate on.
1178 * @param pathname The path to toggle selection of.
1179 * @param on_or_off Whether to toggle it on or off.
1180 * @bug I don't understand this function. Would someone care to explain it?
1181 */
1182void
1183toggle_path_selection(struct s_node *filelist, char *pathname,
1184 bool on_or_off)
1185{
1186 /*@ int ********************************************************* */
1187 static int depth = 0;
1188 int j;
1189
1190 /*@ structures ************************************************** */
1191 struct s_node *node;
1192
1193 /*@ buffers ***************************************************** */
1194 static char current_filename[MAX_STR_LEN];
1195
1196 /*@ end vars *************************************************** */
1197 assert(filelist != NULL);
1198 assert_string_is_neither_NULL_nor_zerolength(pathname);
1199 if (depth == 0) {
1200 log_it("Toggling path's selection");
1201 }
1202 for (node = filelist; node != NULL; node = node->right) {
1203 current_filename[depth] = node->ch;
1204 if (node->down) {
1205 depth++;
1206 toggle_path_selection(node->down, pathname, on_or_off);
1207 depth--;
1208 }
1209 if (node->ch == '\0') {
1210 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1211 for (j = 0;
1212 pathname[j] != '\0'
1213 && pathname[j] == current_filename[j]; j++);
1214 if (current_filename[j] == '/'
1215 || current_filename[j] == '\0') {
1216 node->selected = on_or_off;
1217 }
1218 }
1219 }
1220 }
1221 if (depth == 0) {
1222 log_it("Finished toggling selection");
1223 }
1224}
1225
1226
1227/**
1228 * Toggle node selection of a filelist tree.
1229 * @param filelist The filelist tree to operate on.
1230 * @param on_or_off Whether to toggle selection on or off.
1231 * @bug I don't understand this function. Would someone care to explain it?
1232 */
1233void toggle_node_selection(struct s_node *filelist, bool on_or_off)
1234{
1235 /*@ structure ************************************************** */
1236 struct s_node *node;
1237
1238 /*@ end vars *************************************************** */
1239 assert(filelist != NULL);
1240 for (node = filelist; node != NULL; node = node->right) {
1241 if (node->ch == '/') {
1242 continue;
1243 } /* don't go deep */
1244 if (node->ch == '\0') {
1245 node->selected = on_or_off;
1246 }
1247 if (node->down) {
1248 toggle_node_selection(node->down, on_or_off);
1249 }
1250 }
1251}
1252
1253
1254
1255/**
1256 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1257 */
1258char *g_skeleton_filelist = NULL;
1259
1260
1261
1262/**
1263 * Get the next entry in the space-separated list in @p incoming.
1264 * So if @p incoming was '"one and two" three four', we would
1265 * return "one and two".
1266 * @param incoming The list to get the next entry from.
1267 * @return The first item in the list (respecting double quotes).
1268 * @note The returned string points to static data that will be overwritten with each call.
1269 */
1270char *next_entry(char *incoming)
1271{
1272 char *sz_res;
1273 char *p;
1274 bool in_quotes = FALSE;
1275
1276 mr_asprintf(sz_res, "%s", incoming);
1277 p = sz_res;
1278 while ((*p != '|' || in_quotes) && *p != '\0') {
1279 if (*p == '\"') {
1280 in_quotes = !in_quotes;
1281 }
1282 p++;
1283 }
1284 *p = '\0';
1285 return (sz_res);
1286}
1287
1288
1289/**
1290 * Number of entries in the skeleton filelist.
1291 */
1292long g_skeleton_entries = 0;
1293
1294
1295/**
1296 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1297 * @param dir The directory to list to @p fout.
1298 * @param sth The directories to skip (exclude).
1299 * @param fout The file to write everything to.
1300 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1301 * @return 0, always.
1302 * @bug Return value should be @c void.
1303 */
1304int open_and_list_dir(char *dir1, char *sth, FILE * fout, time_t time_of_last_full_backup) {
1305
1306 const char delims[] = "|";
1307
1308 DIR *dip;
1309 struct dirent *dit;
1310 struct stat statbuf;
1311 char *new = NULL;
1312 char *tmp = NULL;
1313 char *tmp2 = NULL;
1314 char *dir = NULL;
1315 static int percentage = 0;
1316 char *skip_these = NULL;
1317 char *new_with_pipe = NULL;
1318 char *strtmp = NULL;
1319 char *token = NULL;
1320 char *find_excludes = NULL;
1321 char *name_of_evalcall_form = NULL;
1322 char *find_skeleton_marker = NULL;
1323 int i;
1324 int lastpos = 0;
1325 static int depth = 0;
1326 char *p;
1327 static int counter = 0;
1328 static int uberctr = 0;
1329 static long skeleton_lino = 0L;
1330 static time_t last_time = (time_t)0;
1331 time_t this_time;
1332
1333 /* dir is needed when we pass it to the shell */
1334 dir = mr_stresc(dir1, "'", '\\', '\'');
1335 p = strrchr(dir1, '/');
1336 if (p) {
1337 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
1338 mr_free(dir);
1339 return (0);
1340 }
1341 }
1342
1343 if (!depth) {
1344 mr_asprintf(find_excludes, " ");
1345 while((token = mr_strtok(sth, delims, &lastpos)) != NULL) {
1346 mr_strcat(find_excludes," -path %s -prune -o", token);
1347 mr_free(token);
1348 }
1349
1350/**
1351 * The maximum depth of directories to put in the skeleton filelist.
1352 * This is a balance between performance and a good progress indicator.
1353 */
1354#define MAX_SKEL_DEPTH 3
1355
1356#if linux
1357 // 2.6 has /sys as a proc-type thing -- must be excluded
1358 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);
1359#else
1360 // On BSD, for example, /sys is the kernel sources -- don't exclude
1361 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);
1362#endif
1363 mr_free(find_excludes);
1364
1365 log_msg(5, "find command = %s", strtmp);
1366 paranoid_system(strtmp);
1367 mr_free(strtmp);
1368
1369 mr_asprintf(tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
1370 tmp2 = call_program_and_get_last_line_of_output(tmp);
1371 g_skeleton_entries = 1 + atol(tmp2);
1372 mr_free(tmp2);
1373 mr_free(tmp);
1374
1375 mr_asprintf(name_of_evalcall_form, "Making catalog of %s", dir1);
1376 open_evalcall_form(name_of_evalcall_form);
1377 mr_free(name_of_evalcall_form);
1378
1379 skeleton_lino = 1;
1380 log_msg(5, "entries = %ld", g_skeleton_entries);
1381 percentage = 0;
1382 } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
1383 {
1384 mr_asprintf(find_skeleton_marker, "grep -Fv '%s' %s > %s.new 2> /dev/null", dir, g_skeleton_filelist, g_skeleton_filelist);
1385 if (!system(find_skeleton_marker)) {
1386 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1387 skeleton_lino++;
1388 mr_free(find_skeleton_marker);
1389
1390 mr_asprintf(find_skeleton_marker, "mv -f %s.new %s", g_skeleton_filelist, g_skeleton_filelist);
1391 paranoid_system(find_skeleton_marker);
1392 time(&this_time);
1393 if (this_time != last_time) {
1394 last_time = this_time;
1395 if (!g_text_mode) {
1396 int cols, rows;
1397 newtGetScreenSize(&cols, &rows);
1398 mr_asprintf(tmp, "Reading %-*s", cols, dir1);
1399 newtDrawRootText(0, g_noof_rows - 3, tmp);
1400 mr_free(tmp);
1401 }
1402 update_evalcall_form(percentage);
1403 }
1404 }
1405 mr_free(find_skeleton_marker);
1406 }
1407 mr_free(dir);
1408
1409 depth++;
1410
1411 mr_asprintf(skip_these, "|%s|", sth);
1412
1413 mr_asprintf(new_with_pipe, "|%s|", dir1);
1414 if ((dip = opendir(dir1)) == NULL) {
1415 mr_asprintf(tmp,"opendir %s", dir1);
1416 log_OS_error(tmp);
1417 mr_free(tmp);
1418 } else if (strstr(skip_these, new_with_pipe)) {
1419 log_msg(10, "Found dir ***%s**** excluded", dir1);
1420 fprintf(fout, "%s\n", dir1); // if excluded dir then print dir ONLY
1421 } else {
1422 log_msg(10, "Found dir ***%s**** parsed", dir1);
1423 fprintf(fout, "%s\n", dir1);
1424 while ((dit = readdir(dip)) != NULL) {
1425 i++;
1426 if (strcmp(dir1, "/")) {
1427 mr_asprintf(new,"%s/%s",dir1,dit->d_name);
1428 } else {
1429 mr_asprintf(new,"%s%s",dir1,dit->d_name);
1430 }
1431 mr_free(new_with_pipe);
1432 mr_asprintf(new_with_pipe, "|%s|", new);
1433 if (strstr(skip_these, new_with_pipe)) {
1434 fprintf(fout, "%s\n", new);
1435 log_msg(10, "Found child dir ***%s**** excluded", new);
1436 mr_free(new_with_pipe);
1437 } else {
1438 mr_free(new_with_pipe);
1439 if (!lstat(new, &statbuf)) {
1440 if (!S_ISLNK(statbuf.st_mode)
1441 && S_ISDIR(statbuf.st_mode)) {
1442 log_msg(10, "Found child dir ***%s**** parsed", new);
1443 open_and_list_dir(new, skip_these, fout, time_of_last_full_backup);
1444 } else {
1445 if (time_of_last_full_backup == 0 || time_of_last_full_backup < statbuf.st_ctime) {
1446 log_msg(10, "Found child file ***%s**** parsed", new);
1447 fprintf(fout, "%s\n", new);
1448 if ((counter++) > 128) {
1449 counter = 0;
1450 uberctr++;
1451 mr_asprintf(tmp, " %c ", special_dot_char(uberctr));
1452 if (!g_text_mode) {
1453 newtDrawRootText(77, g_noof_rows - 3, tmp);
1454 newtRefresh();
1455 }
1456 mr_free(tmp);
1457 }
1458 }
1459 }
1460 }
1461 }
1462 mr_free(new);
1463 }
1464 }
1465 mr_free(new_with_pipe);
1466 mr_free(skip_these);
1467
1468 if (dip) {
1469 if (closedir(dip) == -1) {
1470 log_OS_error("closedir");
1471 }
1472 }
1473 depth--;
1474 if (!depth) {
1475 close_evalcall_form();
1476 unlink(g_skeleton_filelist);
1477 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1478 }
1479 return (0);
1480}
1481
1482
1483
1484/**
1485 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1486 * @param logfile Unused.
1487 * @param tmpdir The tmpdir of the backup.
1488 * @param scratchdir The scratchdir of the backup.
1489 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1490 * @param excp The paths to NOT back up.
1491 * @param differential The differential level (currently only 0 and 1 are supported).
1492 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1493 * @return 0, always.
1494 * @bug @p logfile is unused.
1495 * @bug Return value is meaningless.
1496 */
1497int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1498 char *include_paths, char *excp, int differential,
1499 char *userdef_filelist)
1500{
1501 char *p, *q;
1502 char *sz_datefile;
1503 char *sz_filelist;
1504 char *exclude_paths = NULL;
1505 FILE *fout;
1506 char *command = NULL;
1507 time_t time_of_last_full_backup = 0;
1508 struct stat statbuf;
1509 char *tmp1 = NULL;
1510 char *tmp2 = NULL;
1511
1512 mr_asprintf(sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
1513 if (!include_paths && !userdef_filelist) {
1514 fatal_error("Please supply either include_paths or userdef_filelist");
1515 }
1516 // make hole for filelist
1517 mr_asprintf(command, "mkdir -p %s/archives", scratchdir);
1518 paranoid_system(command);
1519 mr_free(command);
1520
1521 mr_asprintf(sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
1522 make_hole_for_file(sz_filelist);
1523
1524 if (differential == 0) {
1525 // restore last good datefile if it exists
1526 mr_asprintf(command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
1527 run_program_and_log_output(command, 3);
1528 mr_free(command);
1529
1530 // backup last known good datefile just in case :)
1531 if (does_file_exist(sz_datefile)) {
1532 mr_asprintf(command, "mv -f %s %s.aborted", sz_datefile, sz_datefile);
1533 paranoid_system(command);
1534 mr_free(command);
1535 }
1536 make_hole_for_file(sz_datefile);
1537 tmp2 = call_program_and_get_last_line_of_output("date +%s");
1538 write_one_liner_data_file(sz_datefile, tmp2);
1539 mr_free(tmp2);
1540 } else if (lstat(sz_datefile, &statbuf)) {
1541 log_msg(2, "Warning - unable to find date of previous backup. Full backup instead.");
1542 differential = 0;
1543 time_of_last_full_backup = 0;
1544 } else {
1545 time_of_last_full_backup = statbuf.st_mtime;
1546 log_msg(2, "Differential backup. Yay.");
1547 }
1548 paranoid_free(sz_datefile);
1549
1550// use user-specified filelist (if specified)
1551 if (userdef_filelist) {
1552 log_msg(1, "Using the user-specified filelist - %s - instead of calculating one", userdef_filelist);
1553 mr_asprintf(command, "cp -f %s %s", userdef_filelist, sz_filelist);
1554 if (run_program_and_log_output(command, 3)) {
1555 mr_free(command);
1556 fatal_error("Failed to copy user-specified filelist");
1557 }
1558 mr_free(command);
1559 } else {
1560 if (include_paths) {
1561 log_msg(2, "include_paths = '%s'", include_paths);
1562 }
1563 log_msg(1, "Calculating filelist");
1564 tmp2 = call_program_and_get_last_line_of_output("mount | grep -Ew 'ntfs|ntfs-3g|fat|vfat|dos' | awk '{print $3}'");
1565 if (strlen(tmp2) < 1) {
1566 mr_asprintf(tmp1," ");
1567 } else {
1568 log_msg(2, "Found windows FS: %s",tmp2);
1569 mr_asprintf(tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
1570 mr_free(tmp2);
1571 tmp2 = call_program_and_get_last_line_of_output(tmp1);
1572 log_msg(2, "Found windows files: %s",tmp2);
1573 }
1574 mr_free(tmp1);
1575
1576 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);
1577 if (excp != NULL) {
1578 mr_strcat(exclude_paths,"|%s",excp);
1579 }
1580 mr_free(tmp2);
1581
1582 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1583 log_msg(2, "Generating skeleton filelist so that we can track our progress");
1584 mr_asprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
1585 make_hole_for_file(g_skeleton_filelist);
1586
1587 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1588 log_msg(2, "Opening out filelist to %s", sz_filelist);
1589 if (!(fout = fopen(sz_filelist, "w"))) {
1590 fatal_error("Cannot openout to sz_filelist");
1591 }
1592 if ((include_paths != NULL) && (strlen(include_paths) == 0)) {
1593 log_msg(1, "Including only '/' in %s", sz_filelist);
1594 open_and_list_dir("/", exclude_paths, fout, time_of_last_full_backup);
1595 } else {
1596 p = include_paths;
1597 while (*p) {
1598 q = next_entry(p);
1599 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1600 open_and_list_dir(q, exclude_paths, fout, time_of_last_full_backup);
1601 p += strlen(q);
1602 paranoid_free(q);
1603 while (*p == '|') {
1604 p++;
1605 }
1606 }
1607 }
1608 mr_free(exclude_paths);
1609 paranoid_fclose(fout);
1610 }
1611 log_msg(2, "Copying new filelist to scratchdir");
1612 mr_asprintf(command, "mkdir -p %s/archives", scratchdir);
1613 paranoid_system(command);
1614 mr_free(command);
1615
1616 mr_asprintf(command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
1617 paranoid_system(command);
1618 mr_free(command);
1619
1620 mr_asprintf(command, "mv -f %s %s", sz_filelist, tmpdir);
1621 paranoid_system(command);
1622 mr_free(command);
1623
1624 paranoid_free(sz_filelist);
1625 log_msg(2, "Freeing variables");
1626 mr_free(g_skeleton_filelist);
1627 log_msg(2, "Exiting");
1628 return (0);
1629}
1630
1631
1632/**
1633 * Wrapper around mondo_makefilelist().
1634 * @param bkpinfo The backup information structure. Fields used:
1635 * - @c bkpinfo->differential
1636 * - @c bkpinfo->exclude_paths
1637 * - @c bkpinfo->include_paths
1638 * - @c bkpinfo->make_filelist
1639 * - @c bkpinfo->scratchdir
1640 * - @c bkpinfo->tmpdir
1641 * @return 0 for success, nonzero for failure.
1642 * @see mondo_makefilelist
1643 */
1644int prepare_filelist()
1645{
1646
1647 /*@ int **************************************************** */
1648 int res = 0;
1649
1650 assert(bkpinfo != NULL);
1651 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir, bkpinfo->scratchdir);
1652 if (bkpinfo->make_filelist) {
1653 mvaddstr_and_log_it(g_currentY, 0,
1654 "Making catalog of files to be backed up");
1655 } else {
1656 mvaddstr_and_log_it(g_currentY, 0,
1657 "Using supplied catalog of files to be backed up");
1658 }
1659
1660 if (bkpinfo->make_filelist) {
1661 res = mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir, bkpinfo->scratchdir, bkpinfo->include_paths, bkpinfo->exclude_paths, bkpinfo->differential, NULL);
1662 } else {
1663 res = mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir, bkpinfo->scratchdir, NULL, bkpinfo->exclude_paths, bkpinfo->differential, bkpinfo->include_paths);
1664 }
1665
1666 if (res) {
1667 log_OS_error("Call to mondo_makefilelist failed");
1668 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1669 } else {
1670 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1671 }
1672 return (res);
1673}
1674
1675
1676/**
1677 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1678 * @param startnode The node containing the root of the directory tree.
1679 * @param string_to_find The string to look for at @p startnode.
1680 * @return The node containing the last element of @p string_to_find, or NULL if
1681 * it was not found.
1682 */
1683struct s_node *find_string_at_node(struct s_node *startnode,
1684 char *string_to_find)
1685{
1686 /*@ int ******************************************************** */
1687 static int depth = 0;
1688 static char original_string[MAX_STR_LEN];
1689
1690 /*@ sturctures ************************************************* */
1691 struct s_node *node;
1692
1693 /*@ char ****************************************************** */
1694 char char_to_find;
1695
1696 /*@ bools ****************************************************** */
1697
1698 if (!depth) {
1699 strcpy(original_string, string_to_find);
1700 }
1701
1702 assert(startnode != NULL);
1703 assert(string_to_find != NULL);
1704
1705 log_msg(7, "starting --- str=%s", string_to_find);
1706
1707/* walk across tree if necessary */
1708 node = startnode;
1709 char_to_find = string_to_find[0];
1710 if (node->right != NULL && node->ch < char_to_find) {
1711 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1712 char_to_find, node->ch, (node->right)->ch);
1713 return (find_string_at_node(node->right, string_to_find));
1714 }
1715
1716/* walk down tree if appropriate */
1717 if (node->down != NULL && node->ch == char_to_find) {
1718 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1719 depth++;
1720 node = find_string_at_node(node->down, string_to_find + 1);
1721 depth--;
1722 return (node);
1723 }
1724
1725 if (char_to_find == '\0' && node->ch == '\0') {
1726 log_msg(7, "%s is in tree", original_string);
1727 return (node);
1728 } else {
1729 log_msg(7, "%s is NOT in tree", original_string);
1730 return (NULL);
1731 }
1732}
1733
1734
1735
1736/**
1737 * Write all entries in @p needles_list_fname which are also in
1738 * @p filelist to @p matches_list_fname.
1739 * @param needles_list_fname A file containing strings to look for, 1 per line.
1740 * @param filelist The node for the root of the directory structure to search in.
1741 * @param matches_list_fname The filename where we should put the matches.
1742 * @return The number of matches found.
1743 */
1744long save_filelist_entries_in_common(char *needles_list_fname,
1745 struct s_node *filelist,
1746 char *matches_list_fname,
1747 bool use_star)
1748{
1749 int retval = 0;
1750 struct s_node *found_node;
1751 FILE *fin;
1752 FILE *fout;
1753 char *fname = NULL;
1754 char *tmp = NULL;
1755
1756 log_msg(5, "starting");
1757 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1758 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1759 if (!(fin = fopen(needles_list_fname, "r"))) {
1760 fatal_error("Cannot openin needles_list_fname");
1761 }
1762 if (!(fout = fopen(matches_list_fname, "w"))) {
1763 fatal_error("Cannot openout matches_list_fname");
1764 }
1765 while (!feof(fin)) {
1766 mr_getline(fname, fin);
1767 if (!use_star) {
1768 if (fname[0] == '/') {
1769 mr_asprintf(tmp, "%s", fname);
1770 } else {
1771 mr_asprintf(tmp, "/%s", fname);
1772 }
1773 mr_free(fname);
1774 fname = tmp;
1775 }
1776 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1777 fname[strlen(fname) - 1] = '\0';
1778 }
1779
1780 log_msg(5, "Looking for '%s'", fname);
1781 found_node = find_string_at_node(filelist, fname);
1782 if (found_node) {
1783 if (found_node->selected) {
1784 if (fname[0] == '/') {
1785 mr_asprintf(tmp, "%s", fname + 1);
1786 mr_free(fname);
1787 fname = tmp;
1788 }
1789 log_msg(5, "Found '%s'", fname);
1790 tmp = mr_stresc(fname, "[]*?", '\\', '\'');
1791 mr_free(fname);
1792 fname = tmp;
1793 fprintf(fout, "%s\n", fname);
1794 retval++;
1795 }
1796 }
1797 mr_free(fname);
1798 }
1799 paranoid_fclose(fout);
1800 paranoid_fclose(fin);
1801 return (retval);
1802}
1803
1804
1805
1806/**
1807 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1808 * @p filelist.
1809 * @param filelist The top node of the directory structure to add the files to.
1810 * @param list_of_files_fname The file containing the files to add, 1 per line.
1811 * @param flag_em If TRUE, then flag the added files for restoration.
1812 * @return 0 for success, nonzero for failure.
1813 */
1814int add_list_of_files_to_filelist(struct s_node *filelist,
1815 char *list_of_files_fname, bool flag_em)
1816{
1817 FILE *fin = NULL;
1818 char *tmp = NULL;
1819 struct s_node *nod = NULL;
1820
1821 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1822 if (!(fin = fopen(list_of_files_fname, "r"))) {
1823 log_it("%s",list_of_files_fname);
1824 return (1);
1825 }
1826 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
1827 if (!tmp[0]) {
1828 mr_free(tmp);
1829 continue;
1830 }
1831 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)) {
1832 tmp[strlen(tmp) - 1] = '\0';
1833 }
1834 log_msg(2, "tmp = '%s'", tmp);
1835 if (!tmp[0]) {
1836 mr_free(tmp);
1837 continue;
1838 }
1839 if ((nod = find_string_at_node(filelist, tmp))) {
1840 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1841 } else {
1842 add_string_at_node(filelist, tmp);
1843 nod = find_string_at_node(filelist, tmp);
1844 }
1845
1846 if (nod && flag_em) {
1847 toggle_path_selection(filelist, tmp, TRUE);
1848 log_msg(5, "Flagged '%s'", tmp);
1849 }
1850 mr_free(tmp);
1851 }
1852 mr_free(tmp);
1853 paranoid_fclose(fin);
1854 return (0);
1855}
1856
1857/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.