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

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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