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

Last change on this file since 2206 was 2206, checked in by Bruno Cornec, 15 years ago

-Improvements in tests for mountlist

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