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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

  • Property svn:keywords set to Id
File size: 49.0 KB
Line 
1/* $Id: libmondo-filelist.c 688 2006-07-17 13:44:46Z 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 688 2006-07-17 13:44:46Z 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 struct s_node *filelist = NULL;
850 FILE *pin = NULL;
851 char *command_to_open_fname = NULL;
852 char *fname = NULL;
853 char *tmp = NULL;
854 char *tmp1 = NULL;
855 int pos_in_fname = 0;
856 size_t n = 0;
857 int percentage = 0;
858
859 long lines_in_filelist = 0L;
860 long lino = 0L;
861
862 assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
863
864 if (!does_file_exist(filelist_fname)) {
865 fatal_error("filelist does not exist -- cannot load it");
866 }
867 log_to_screen(_("Loading filelist"));
868 asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
869 asprintf(&tmp, "zcat %s | wc -l", filelist_fname);
870 log_msg(6, "tmp = %s", tmp);
871 tmp1 = call_program_and_get_last_line_of_output(tmp);
872 paranoid_free(tmp);
873
874 lines_in_filelist = atol(tmp1);
875 paranoid_free(tmp1);
876
877 if (lines_in_filelist < 3) {
878 log_to_screen(_("Warning - surprisingly short filelist."));
879 }
880 g_original_noof_lines_in_filelist = lines_in_filelist;
881 if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
882 return (NULL);
883 }
884 filelist->ch = '/';
885 filelist->right = NULL;
886 filelist->down = malloc(sizeof(struct s_node));
887 filelist->expanded = filelist->selected = FALSE;
888 (filelist->down)->ch = '\0';
889 (filelist->down)->right = (filelist->down)->down = FALSE;
890 (filelist->down)->expanded = (filelist->down)->selected = FALSE;
891 if (!(pin = popen(command_to_open_fname, "r"))) {
892 log_OS_error("Unable to openin filelist_fname");
893 return (NULL);
894 }
895 paranoid_free(command_to_open_fname);
896
897 open_evalcall_form(_("Loading filelist from disk"));
898 for (getline(&fname, &n, pin); !feof(pin); getline(&fname, &n, pin)) {
899 if ((fname[strlen(fname) - 1] == 13
900 || fname[strlen(fname) - 1] == 10) && strlen(fname) > 0) {
901 fname[strlen(fname) - 1] = '\0';
902 }
903 if (!strlen(fname)) {
904 continue;
905 }
906 for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
907 if (fname[pos_in_fname] != '/') {
908 continue;
909 }
910 asprintf(&tmp, fname);
911 tmp[pos_in_fname] = '\0';
912 if (strlen(tmp)) {
913 add_string_at_node(filelist, tmp);
914 }
915 paranoid_free(tmp);
916 }
917 add_string_at_node(filelist, fname);
918 if (!(++lino % 1111)) {
919 percentage = (int) (lino * 100 / lines_in_filelist);
920 update_evalcall_form(percentage);
921 }
922 }
923 paranoid_free(fname);
924 paranoid_pclose(pin);
925 close_evalcall_form();
926 log_it("Finished loading filelist");
927 return (filelist);
928}
929
930
931/**
932 * Log a list of files in @p node.
933 * @param node The toplevel node to use.
934 */
935void show_filelist(struct s_node *node)
936{
937 static int depth = 0;
938 static char current_string[200];
939
940 if (depth == 0) {
941 log_msg(0, "----------------show filelist--------------");
942 }
943 current_string[depth] = node->ch;
944
945 log_msg(3, "depth=%d", depth);
946 if (node->down) {
947 log_msg(3, "moving down");
948 depth++;
949 show_filelist(node->down);
950 depth--;
951 }
952
953 if (!node->ch) {
954 log_msg(0, "%s\n", current_string);
955 }
956
957 if (node->right) {
958 log_msg(3, "moving right");
959 show_filelist(node->right);
960 }
961 if (depth == 0) {
962 log_msg(0, "----------------show filelist--------------");
963 }
964 return;
965}
966
967
968/**
969 * Reset the filelist to the state it was when it was loaded. This does not
970 * touch the file on disk.
971 * @param filelist The filelist tree structure.
972 */
973void reload_filelist(struct s_node *filelist)
974{
975 assert(filelist != NULL);
976 toggle_node_selection(filelist, FALSE);
977 toggle_path_expandability(filelist, "/", FALSE);
978 toggle_all_root_dirs_on(filelist);
979}
980
981
982/**
983 * Save a filelist tree structure to disk.
984 * @param filelist The filelist tree structure to save.
985 * @param outfname Where to save it.
986 */
987void save_filelist(struct s_node *filelist, char *outfname)
988{
989 /*@ int ********************************************************* */
990 static int percentage;
991 static int depth = 0;
992
993 /*@ buffers ***************************************************** */
994 static char str[MAX_STR_LEN];
995
996 /*@ structures ************************************************** */
997 struct s_node *node;
998
999 /*@ pointers **************************************************** */
1000 static FILE *fout = NULL;
1001
1002 /*@ long ******************************************************** */
1003 static long lines_in_filelist = 0;
1004 static long lino = 0;
1005
1006 /*@ end vars *************************************************** */
1007
1008 assert(filelist != NULL);
1009 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1010 if (depth == 0) {
1011 log_to_screen(_("Saving filelist"));
1012 if (!(fout = fopen(outfname, "w"))) {
1013 fatal_error("Cannot openout/save filelist");
1014 }
1015 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
1016 open_evalcall_form(_("Saving selection to disk"));
1017 }
1018 for (node = filelist; node != NULL; node = node->right) {
1019 str[depth] = node->ch;
1020 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1021 if (!node->ch) {
1022// if (node->selected)
1023// {
1024 fprintf(fout, "%s\n", str);
1025// }
1026 if (!(++lino % 1111)) {
1027 percentage = (int) (lino * 100 / lines_in_filelist);
1028 update_evalcall_form(percentage);
1029 }
1030 }
1031 if (node->down) {
1032 depth++;
1033 save_filelist(node->down, "");
1034 depth--;
1035 }
1036 }
1037 if (depth == 0) {
1038 paranoid_fclose(fout);
1039 close_evalcall_form();
1040 log_it("Finished saving filelist");
1041 }
1042}
1043
1044
1045/**
1046 * Toggle all root dirs on.
1047 * @param filelist The filelist tree structure to operate on.
1048 * @bug I don't understand this function. Would someone care to explain it?
1049 */
1050void toggle_all_root_dirs_on(struct s_node *filelist)
1051{
1052 /*@ structures ************************************************** */
1053 struct s_node *node;
1054
1055 /*@ int ********************************************************* */
1056 static int depth = 0;
1057 static int root_dirs_expanded;
1058
1059 /*@ buffers ***************************************************** */
1060 static char filename[MAX_STR_LEN];
1061
1062 /*@ end vars *************************************************** */
1063
1064 assert(filelist != NULL);
1065 if (depth == 0) {
1066 log_it("Toggling all root dirs ON");
1067 root_dirs_expanded = 0;
1068 }
1069 for (node = filelist; node != NULL; node = node->right) {
1070 filename[depth] = node->ch;
1071 if (node->ch == '\0' && strlen(filename) > 1
1072 && (!strchr(filename + 1, '/'))) {
1073 node->selected = FALSE;
1074 node->expanded = TRUE;
1075// log_it (filename);
1076 root_dirs_expanded++;
1077 }
1078 if (node->down) {
1079 depth++;
1080 toggle_all_root_dirs_on(node->down);
1081 depth--;
1082 }
1083 }
1084 if (depth == 0) {
1085 log_it("Finished toggling all root dirs ON");
1086 }
1087}
1088
1089
1090/**
1091 * Toggle the expandability of a path.
1092 * @param filelist The filelist tree to operate on.
1093 * @param pathname The path to toggle expandability of.
1094 * @param on_or_off Whether to toggle it on or off.
1095 * @bug I don't understand this function. Would someone care to explain it?
1096 */
1097void
1098toggle_path_expandability(struct s_node *filelist, char *pathname,
1099 bool on_or_off)
1100{
1101
1102 /*@ int ******************************************************** */
1103 static int depth = 0;
1104 static int total_expanded;
1105 static int root_depth;
1106 int j;
1107 /*@ structures ************************************************* */
1108 struct s_node *node;
1109
1110 /*@ buffers **************************************************** */
1111 static char current_filename[MAX_STR_LEN];
1112
1113 /*@ end vars *************************************************** */
1114
1115 assert(filelist != NULL);
1116 assert_string_is_neither_NULL_nor_zerolength(pathname);
1117 if (depth == 0) {
1118 total_expanded = 0;
1119// log_it ("Toggling path's expandability");
1120 for (root_depth = (int) strlen(pathname);
1121 root_depth > 0 && pathname[root_depth - 1] != '/';
1122 root_depth--);
1123 if (root_depth < 2) {
1124 root_depth = (int) strlen(pathname);
1125 }
1126 }
1127 for (node = filelist; node != NULL; node = node->right) {
1128 current_filename[depth] = node->ch;
1129 if (node->down) {
1130 depth++;
1131 toggle_path_expandability(node->down, pathname, on_or_off);
1132 depth--;
1133 }
1134 if (node->ch == '\0') {
1135 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1136 for (j = root_depth;
1137 current_filename[j] != '/'
1138 && current_filename[j] != '\0'; j++);
1139 if (current_filename[j] != '\0') {
1140 for (j++;
1141 current_filename[j] != '/'
1142 && current_filename[j] != '\0'; j++);
1143 }
1144 if (current_filename[j] == '\0') {
1145 node->expanded =
1146 (!strcmp(pathname, current_filename) ? TRUE :
1147 on_or_off);
1148 }
1149 }
1150 }
1151 if (node->expanded) {
1152 if (total_expanded < ARBITRARY_MAXIMUM - 32
1153 || !strrchr(current_filename + strlen(pathname), '/')) {
1154 total_expanded++;
1155 } else {
1156 node->expanded = FALSE;
1157 }
1158 }
1159 }
1160 if (depth == 0) {
1161// log_it ("Finished toggling expandability");
1162 }
1163}
1164
1165
1166/**
1167 * Toggle whether a path is selected.
1168 * @param filelist The filelist tree to operate on.
1169 * @param pathname The path to toggle selection of.
1170 * @param on_or_off Whether to toggle it on or off.
1171 * @bug I don't understand this function. Would someone care to explain it?
1172 */
1173void
1174toggle_path_selection(struct s_node *filelist, char *pathname,
1175 bool on_or_off)
1176{
1177 /*@ int ********************************************************* */
1178 static int depth = 0;
1179 int j;
1180
1181 /*@ structures ************************************************** */
1182 struct s_node *node;
1183
1184 /*@ buffers ***************************************************** */
1185 static char current_filename[MAX_STR_LEN];
1186
1187 /*@ end vars *************************************************** */
1188 assert(filelist != NULL);
1189 assert_string_is_neither_NULL_nor_zerolength(pathname);
1190 if (depth == 0) {
1191 log_it("Toggling path's selection");
1192 }
1193 for (node = filelist; node != NULL; node = node->right) {
1194 current_filename[depth] = node->ch;
1195 if (node->down) {
1196 depth++;
1197 toggle_path_selection(node->down, pathname, on_or_off);
1198 depth--;
1199 }
1200 if (node->ch == '\0') {
1201 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1202 for (j = 0;
1203 pathname[j] != '\0'
1204 && pathname[j] == current_filename[j]; j++);
1205 if (current_filename[j] == '/'
1206 || current_filename[j] == '\0') {
1207 node->selected = on_or_off;
1208 }
1209 }
1210 }
1211 }
1212 if (depth == 0) {
1213 log_it("Finished toggling selection");
1214 }
1215}
1216
1217
1218/**
1219 * Toggle node selection of a filelist tree.
1220 * @param filelist The filelist tree to operate on.
1221 * @param on_or_off Whether to toggle selection on or off.
1222 * @bug I don't understand this function. Would someone care to explain it?
1223 */
1224void toggle_node_selection(struct s_node *filelist, bool on_or_off)
1225{
1226 /*@ structure ************************************************** */
1227 struct s_node *node;
1228
1229 /*@ end vars *************************************************** */
1230 assert(filelist != NULL);
1231 for (node = filelist; node != NULL; node = node->right) {
1232 if (node->ch == '/') {
1233 continue;
1234 } /* don't go deep */
1235 if (node->ch == '\0') {
1236 node->selected = on_or_off;
1237 }
1238 if (node->down) {
1239 toggle_node_selection(node->down, on_or_off);
1240 }
1241 }
1242}
1243
1244
1245/**
1246 * Number of entries in the skeleton filelist.
1247 */
1248long g_skeleton_entries = 0;
1249
1250
1251/**
1252 * Wrapper around mondo_makefilelist().
1253 * @param bkpinfo The backup information structure. Fields used:
1254 * - @c bkpinfo->differential
1255 * - @c bkpinfo->exclude_paths
1256 * - @c bkpinfo->include_paths
1257 * - @c bkpinfo->make_filelist
1258 * - @c bkpinfo->scratchdir
1259 * - @c bkpinfo->tmpdir
1260 * @return 0 for success, nonzero for failure.
1261 * @see mondo_makefilelist
1262 */
1263int prepare_filelist(struct s_bkpinfo *bkpinfo)
1264{
1265
1266 /*@ int **************************************************** */
1267 int res = 0;
1268 /*@ buffers ************************************************ */
1269
1270 assert(bkpinfo != NULL);
1271 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir,
1272 bkpinfo->scratchdir);
1273 if (bkpinfo->make_filelist) {
1274 mvaddstr_and_log_it(g_currentY, 0,
1275 _("Making catalog of files to be backed up"));
1276 } else {
1277 mvaddstr_and_log_it(g_currentY, 0,
1278 _("Using supplied catalog of files to be backed up"));
1279 }
1280
1281 if (bkpinfo->make_filelist) {
1282 res =
1283 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1284 bkpinfo->scratchdir, bkpinfo->include_paths,
1285 bkpinfo->exclude_paths,
1286 bkpinfo->differential, NULL);
1287 } else {
1288 res =
1289 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1290 bkpinfo->scratchdir, NULL,
1291 bkpinfo->exclude_paths,
1292 bkpinfo->differential,
1293 bkpinfo->include_paths);
1294 }
1295
1296 if (res) {
1297 log_OS_error("Call to mondo_makefilelist failed");
1298 mvaddstr_and_log_it(g_currentY++, 74, _("Failed."));
1299 } else {
1300 mvaddstr_and_log_it(g_currentY++, 74, _("Done."));
1301 }
1302 return (res);
1303}
1304
1305
1306/**
1307 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1308 * @param dir The directory to list to @p fout.
1309 * @param sth The directories to skip (exclude).
1310 * @param fout The file to write everything to.
1311 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1312 * @return 0, always.
1313 * @bug Return value should be @c void.
1314 */
1315int open_and_list_dir(char *dir, char *sth, FILE * fout,
1316 time_t time_of_last_full_backup,
1317 char *skeleton_filelist)
1318{
1319 DIR *dip = NULL;
1320 struct dirent *dit = NULL;
1321 struct stat statbuf;
1322 char *new = NULL;
1323 char *tmp = NULL;
1324 char *tmp1 = NULL;
1325 static int percentage = 0;
1326 char *skip_these = NULL;
1327 char *new_with_spaces = NULL;
1328 static char *name_of_evalcall_form = NULL;
1329 int i = 0;
1330 static int depth = 0;
1331 char *p = NULL;
1332 static int counter = 0;
1333 static int uberctr = 0;
1334 static char *find_skeleton_marker = NULL;
1335 static long skeleton_lino = 0;
1336 static time_t last_time = 0;
1337 time_t this_time;
1338
1339 p = strrchr(dir, '/');
1340 if (p) {
1341 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
1342 return (0);
1343 }
1344 }
1345
1346 if (!depth) {
1347 malloc_string(find_skeleton_marker);
1348#if linux
1349 // 2.6 has /sys as a proc-type thing -- must be excluded
1350 asprintf(&tmp,
1351 "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",
1352 dir, MAX_SKEL_DEPTH, skeleton_filelist);
1353#else
1354 // On BSD, for example, /sys is the kernel sources -- don't exclude
1355 asprintf(&tmp,
1356 "find %s -maxdepth %d -path /proc -prune -o -type d -a -print > %s 2> /dev/null",
1357 dir, MAX_SKEL_DEPTH, skeleton_filelist);
1358#endif
1359 system(tmp);
1360 paranoid_free(tmp);
1361
1362 asprintf(&tmp, "wc -l %s | awk '{print $1;}'", skeleton_filelist);
1363 tmp1 = call_program_and_get_last_line_of_output(tmp);
1364 paranoid_free(tmp);
1365
1366 g_skeleton_entries = 1 + atol(tmp1);
1367 paranoid_free(tmp1);
1368
1369 asprintf(&name_of_evalcall_form, "Making catalog of %s", dir);
1370 open_evalcall_form(name_of_evalcall_form);
1371 find_skeleton_marker[0] = '\0';
1372 skeleton_lino = 1;
1373 log_msg(5, "entries = %ld", g_skeleton_entries);
1374 percentage = 0;
1375 } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
1376 {
1377 sprintf(find_skeleton_marker,
1378 "grep -Fv \"%s\" %s > %s.new 2> /dev/null", dir,
1379 skeleton_filelist, skeleton_filelist);
1380// log_msg(0, "fsm = %s", find_skeleton_marker);
1381 if (!system(find_skeleton_marker)) {
1382 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1383 skeleton_lino++;
1384// log_msg(5, "Found %s", dir);
1385// log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
1386 sprintf(find_skeleton_marker, "mv -f %s.new %s",
1387 skeleton_filelist, skeleton_filelist);
1388// log_msg(6, "fsm = %s", find_skeleton_marker);
1389 run_program_and_log_output(find_skeleton_marker, 8);
1390 time(&this_time);
1391 if (this_time != last_time) {
1392 last_time = this_time;
1393#ifndef _XWIN
1394 if (!g_text_mode) {
1395 asprintf(&tmp, _("Reading %-68s"), dir);
1396 newtDrawRootText(0, g_noof_rows - 3, tmp);
1397 paranoid_free(tmp);
1398 }
1399#endif
1400 update_evalcall_form(percentage);
1401 }
1402 }
1403 }
1404
1405 depth++;
1406
1407// log_msg(0, "Cataloguing %s", dir);
1408 if (sth[0] == ' ') {
1409 asprintf(&skip_these, "%s", sth);
1410 } else {
1411 asprintf(&skip_these, " %s ", sth);
1412 }
1413 asprintf(&new_with_spaces, " %s ", dir);
1414 if ((dip = opendir(dir)) == NULL) {
1415 log_OS_error("opendir");
1416 } else if (strstr(skip_these, new_with_spaces)) {
1417 fprintf(fout, "%s\n", dir); // if excluded dir then print dir ONLY
1418 } else {
1419 fprintf(fout, "%s\n", dir);
1420 while ((dit = readdir(dip)) != NULL) {
1421 i++;
1422 if (strcmp(dir, "/")) {
1423 asprintf(&new, "%s/%s", dir, dit->d_name);
1424 } else {
1425 asprintf(&new, "%s%s", dir, dit->d_name);
1426 }
1427 paranoid_free(new_with_spaces);
1428 asprintf(&new_with_spaces, " %s ", new);
1429 /* BERLIOS: Old code
1430 new_with_spaces[0] = ' ';
1431 strcpy(new_with_spaces + 1, new);
1432 strcat(new_with_spaces, " ");
1433 */
1434 if (strstr(skip_these, new_with_spaces)) {
1435 fprintf(fout, "%s\n", new);
1436 } else {
1437 if (!lstat(new, &statbuf)) {
1438 if (!S_ISLNK(statbuf.st_mode)
1439 && S_ISDIR(statbuf.st_mode)) {
1440 open_and_list_dir(new, skip_these, fout,
1441 time_of_last_full_backup,
1442 skeleton_filelist);
1443 } else {
1444 if (time_of_last_full_backup == 0
1445 || time_of_last_full_backup <
1446 statbuf.st_ctime) {
1447 fprintf(fout, "%s\n", new);
1448 if ((counter++) > 128) {
1449 counter = 0;
1450 uberctr++;
1451 asprintf(&tmp, " %c ",
1452 special_dot_char(uberctr));
1453#ifndef _XWIN
1454 if (!g_text_mode) {
1455 newtDrawRootText(77, g_noof_rows - 3,
1456 tmp);
1457 newtRefresh();
1458 }
1459 paranoid_free(tmp);
1460#endif
1461 }
1462 }
1463 }
1464 }
1465 }
1466 paranoid_free(new);
1467 }
1468 }
1469 paranoid_free(new_with_spaces);
1470 paranoid_free(skip_these);
1471
1472 if (dip) {
1473 if (closedir(dip) == -1) {
1474 log_OS_error("closedir");
1475 }
1476 }
1477 depth--;
1478 if (!depth) {
1479 close_evalcall_form();
1480 paranoid_free(name_of_evalcall_form);
1481 paranoid_free(find_skeleton_marker);
1482 unlink(skeleton_filelist);
1483 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1484 }
1485 return (0);
1486}
1487
1488
1489/**
1490 * Get the next entry in the space-separated list in @p incoming.
1491 * So if @p incoming was '"one and two" three four', we would
1492 * return "one and two".
1493 * @param incoming The list to get the next entry from.
1494 * @return The first item in the list (respecting double quotes).
1495 * @note The returned string points to static data that will be overwritten with each call.
1496 */
1497char *next_entry(char *incoming)
1498{
1499 static char sz_res[MAX_STR_LEN];
1500 char *p;
1501 bool in_quotes = FALSE;
1502
1503 strcpy(sz_res, incoming);
1504 p = sz_res;
1505 while ((*p != ' ' || in_quotes) && *p != '\0') {
1506 if (*p == '\"') {
1507 in_quotes = !in_quotes;
1508 }
1509 p++;
1510 }
1511 *p = '\0';
1512 return (sz_res);
1513}
1514
1515
1516/**
1517 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1518 * @param logfile Unused.
1519 * @param tmpdir The tmpdir of the backup.
1520 * @param scratchdir The scratchdir of the backup.
1521 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1522 * @param excp The paths to NOT back up.
1523 * @param differential The differential level (currently only 0 and 1 are supported).
1524 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1525 * @return 0, always.
1526 * @bug @p logfile is unused.
1527 * @bug Return value is meaningless.
1528 */
1529int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1530 char *include_paths, char *excp, int differential,
1531 char *userdef_filelist)
1532{
1533 char sz_datefile_wildcard[] = "/var/cache/mondo/difflevel.%d";
1534 char *p = NULL;
1535 char *q = NULL;
1536 char *tmp = NULL;
1537 char *tmp1 = NULL;
1538 char *tmp2 = NULL;
1539 char *sz_datefile;
1540 char *sz_filelist = NULL;
1541 char *exclude_paths = NULL;
1542 int i = 0;
1543 FILE *fout = NULL;
1544 char *command = NULL;
1545 time_t time_of_last_full_backup = 0;
1546 struct stat statbuf;
1547 char *skeleton_filelist = NULL;
1548
1549 // The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1550 asprintf(&sz_datefile, sz_datefile_wildcard, 0);
1551 if (!include_paths && !userdef_filelist) {
1552 fatal_error
1553 ("Please supply either include_paths or userdef_filelist");
1554 }
1555// make hole for filelist
1556 asprintf(&command, "mkdir -p %s/archives", scratchdir);
1557 paranoid_system(command);
1558 paranoid_free(command);
1559
1560 asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
1561 make_hole_for_file(sz_filelist);
1562
1563 if (differential == 0) {
1564 // restore last good datefile if it exists
1565 asprintf(&command, "cp -f %s.aborted %s", sz_datefile,
1566 sz_datefile);
1567 run_program_and_log_output(command, 3);
1568 paranoid_free(command);
1569
1570 // backup last known good datefile just in case :)
1571 if (does_file_exist(sz_datefile)) {
1572 asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
1573 sz_datefile);
1574 paranoid_system(command);
1575 paranoid_free(command);
1576 }
1577 make_hole_for_file(sz_datefile);
1578 tmp = call_program_and_get_last_line_of_output("date +%s");
1579 write_one_liner_data_file(sz_datefile, tmp);
1580 paranoid_free(tmp);
1581 } else if (lstat(sz_datefile, &statbuf)) {
1582 log_msg(2,
1583 "Warning - unable to find date of previous backup. Full backup instead.");
1584 differential = 0;
1585 time_of_last_full_backup = 0;
1586 } else {
1587 time_of_last_full_backup = statbuf.st_mtime;
1588 log_msg(2, "Differential backup. Yay.");
1589 }
1590 paranoid_free(sz_datefile);
1591
1592// use user-specified filelist (if specified)
1593 if (userdef_filelist) {
1594 log_msg(1,
1595 "Using the user-specified filelist - %s - instead of calculating one",
1596 userdef_filelist);
1597 asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
1598 if (run_program_and_log_output(command, 3)) {
1599 fatal_error("Failed to copy user-specified filelist");
1600 }
1601 paranoid_free(command);
1602 } else {
1603 log_msg(2, "include_paths = '%s'", include_paths);
1604 log_msg(1, "Calculating filelist");
1605 tmp = call_program_and_get_last_line_of_output("locate /win386.swp 2> /dev/null");
1606 tmp1 = call_program_and_get_last_line_of_output("locate /hiberfil.sys 2> /dev/null");
1607 tmp2 = call_program_and_get_last_line_of_output("locate /pagefile.sys 2> /dev/null");
1608 asprintf(&exclude_paths, " %s %s %s %s %s %s . .. \
1609" MNT_CDROM " " MNT_FLOPPY " /media/cdrom /media/cdrecorder \
1610/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);
1611 paranoid_free(tmp);
1612 paranoid_free(tmp1);
1613 paranoid_free(tmp2);
1614
1615 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1616 log_msg(2,
1617 "Generating skeleton filelist so that we can track our progress");
1618 asprintf(&skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
1619 make_hole_for_file(skeleton_filelist);
1620
1621 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1622 log_msg(2, "Opening out filelist to %s", sz_filelist);
1623 if (!(fout = fopen(sz_filelist, "w"))) {
1624 fatal_error("Cannot openout to sz_filelist");
1625 }
1626 i = 0;
1627 if (strlen(include_paths) == 0) {
1628 log_msg(1, "Including only '/' in %s", sz_filelist);
1629 open_and_list_dir("/", exclude_paths, fout,
1630 time_of_last_full_backup, skeleton_filelist);
1631 } else {
1632 p = include_paths;
1633 while (*p) {
1634 q = next_entry(p);
1635 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1636 open_and_list_dir(q, exclude_paths, fout,
1637 time_of_last_full_backup,
1638 skeleton_filelist);
1639 p += strlen(q);
1640 while (*p == ' ') {
1641 p++;
1642 }
1643 }
1644 }
1645 paranoid_fclose(fout);
1646 paranoid_free(skeleton_filelist);
1647 paranoid_free(exclude_paths);
1648 }
1649 log_msg(2, "Copying new filelist to scratchdir");
1650 asprintf(&command, "mkdir -p %s/archives", scratchdir);
1651 paranoid_system(command);
1652 paranoid_free(command);
1653
1654 asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
1655 paranoid_system(command);
1656 paranoid_free(command);
1657
1658 asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
1659 paranoid_system(command);
1660 paranoid_free(command);
1661 paranoid_free(sz_filelist);
1662
1663 log_msg(2, "Exiting");
1664 return (0);
1665}
1666
1667
1668/**
1669 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1670 * @param startnode The node containing the root of the directory tree.
1671 * @param string_to_find The string to look for at @p startnode.
1672 * @return The node containing the last element of @p string_to_find, or NULL if
1673 * it was not found.
1674 */
1675struct s_node *find_string_at_node(struct s_node *startnode,
1676 char *string_to_find)
1677{
1678 /*@ int ******************************************************** */
1679 int noof_chars;
1680 static int depth = 0;
1681 static char original_string[MAX_STR_LEN];
1682
1683 /*@ sturctures ************************************************* */
1684 struct s_node *node;
1685
1686 /*@ char ****************************************************** */
1687 char char_to_find;
1688
1689 /*@ bools ****************************************************** */
1690
1691 if (!depth) {
1692 strcpy(original_string, string_to_find);
1693 }
1694
1695 assert(startnode != NULL);
1696 assert(string_to_find != NULL);
1697
1698 noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
1699
1700 log_msg(7, "starting --- str=%s", string_to_find);
1701
1702/* walk across tree if necessary */
1703 node = startnode;
1704 char_to_find = string_to_find[0];
1705 if (node->right != NULL && node->ch < char_to_find) {
1706 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1707 char_to_find, node->ch, (node->right)->ch);
1708 return (find_string_at_node(node->right, string_to_find));
1709 }
1710
1711/* walk down tree if appropriate */
1712 if (node->down != NULL && node->ch == char_to_find) {
1713 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1714 depth++;
1715 node = find_string_at_node(node->down, string_to_find + 1);
1716 depth--;
1717 return (node);
1718 }
1719
1720 if (char_to_find == '\0' && node->ch == '\0') {
1721 log_msg(7, "%s is in tree", original_string);
1722 return (node);
1723 } else {
1724 log_msg(7, "%s is NOT in tree", original_string);
1725 return (NULL);
1726 }
1727}
1728
1729
1730/**
1731 * Write all entries in @p needles_list_fname which are also in
1732 * @p filelist to @p matches_list_fname.
1733 * @param needles_list_fname A file containing strings to look for, 1 per line.
1734 * @param filelist The node for the root of the directory structure to search in.
1735 * @param matches_list_fname The filename where we should put the matches.
1736 * @return The number of matches found.
1737 */
1738long save_filelist_entries_in_common(char *needles_list_fname,
1739 struct s_node *filelist,
1740 char *matches_list_fname,
1741 bool use_star)
1742{
1743 int retval = 0;
1744 struct s_node *found_node;
1745 FILE *fin;
1746 FILE *fout;
1747 char *fname = NULL;
1748 char *tmp;
1749 size_t len = 0; // Scrub's patch doesn't work without that
1750
1751// log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
1752 log_msg(5, "starting");
1753 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1754 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1755 if (!(fin = fopen(needles_list_fname, "r"))) {
1756 fatal_error("Cannot openin needles_list_fname");
1757 }
1758 if (!(fout = fopen(matches_list_fname, "w"))) {
1759 fatal_error("Cannot openout matches_list_fname");
1760 }
1761 while (!feof(fin)) {
1762 getline(&fname, &len, fin);
1763 if (!use_star) {
1764 if (fname[0] == '/') {
1765 asprintf(&tmp, fname);
1766 } else {
1767 asprintf(&tmp, "/%s", fname);
1768 }
1769 paranoid_free(fname);
1770 fname = tmp;
1771 }
1772 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1773 fname[strlen(fname) - 1] = '\0';
1774 }
1775
1776 log_msg(5, "Looking for '%s'", fname);
1777 found_node = find_string_at_node(filelist, fname);
1778 if (found_node) {
1779 if (found_node->selected) {
1780 if (fname[0] == '/') {
1781 asprintf(&tmp, fname + 1);
1782 paranoid_free(fname);
1783 fname = tmp;
1784 }
1785 log_msg(5, "Found '%s'", fname);
1786 turn_wildcard_chars_into_literal_chars(tmp, fname);
1787 fprintf(fout, "%s\n", tmp);
1788 paranoid_free(tmp);
1789 retval++;
1790 }
1791 }
1792 paranoid_free(fname);
1793 }
1794 paranoid_fclose(fout);
1795 paranoid_fclose(fin);
1796 return (retval);
1797}
1798
1799
1800/**
1801 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1802 * @p filelist.
1803 * @param filelist The top node of the directory structure to add the files to.
1804 * @param list_of_files_fname The file containing the files to add, 1 per line.
1805 * @param flag_em If TRUE, then flag the added files for restoration.
1806 * @return 0 for success, nonzero for failure.
1807 */
1808int add_list_of_files_to_filelist(struct s_node *filelist,
1809 char *list_of_files_fname, bool flag_em)
1810{
1811 FILE *fin;
1812 char *tmp = NULL;
1813 size_t n = 0;
1814 struct s_node *nod;
1815
1816 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1817 if (!(fin = fopen(list_of_files_fname, "r"))) {
1818 iamhere(list_of_files_fname);
1819 return (1);
1820 }
1821 for (getline(&tmp, &n, fin); !feof(fin); getline(&tmp, &n, fin)) {
1822 if (!tmp[0]) {
1823 continue;
1824 }
1825 if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
1826 && strlen(tmp) > 0) {
1827 tmp[strlen(tmp) - 1] = '\0';
1828 }
1829 log_msg(2, "tmp = '%s'", tmp);
1830 if (!tmp[0]) {
1831 continue;
1832 }
1833 if ((nod = find_string_at_node(filelist, tmp))) {
1834 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1835 } else {
1836 add_string_at_node(filelist, tmp);
1837 nod = find_string_at_node(filelist, tmp);
1838 }
1839
1840 if (nod && flag_em) {
1841 toggle_path_selection(filelist, tmp, TRUE);
1842 log_msg(5, "Flagged '%s'", tmp);
1843 }
1844 }
1845 paranoid_fclose(fin);
1846 paranoid_free(tmp);
1847 return (0);
1848}
1849
1850/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.