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

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

merge -r978:1042 $SVN_M/branches/stable

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