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

Last change on this file since 687 was 687, checked in by bcornec, 18 years ago

merge -r671:686 $SVN_M/branches/stable

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