source: MondoRescue/branches/2.2.9/mondo/src/common/libmondo-filelist.c@ 2230

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