source: MondoRescue/trunk/mondo/src/common/libmondo-filelist.c@ 991

Last change on this file since 991 was 991, checked in by Bruno Cornec, 17 years ago

Fix a compiler warning on libmondo-filelist.c

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