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

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

libmondo-filelist.c memory review (asprintf and the like) - compiles but no test made yet

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