source: MondoRescue/branches/stable/mondo/src/common/libmondo-filelist.c@ 1075

Last change on this file since 1075 was 1075, checked in by Bruno Cornec, 17 years ago

Improvements for USB support in mindi when called from mondo
Changelogs in C files removed from common

  • Property svn:keywords set to Id
File size: 50.9 KB
Line 
1/* libmondo-filelist.c
2 $Id: libmondo-filelist.c 1075 2007-01-25 15:18:59Z 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_str.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
30/**
31 * The maximum depth of directories to put in the skeleton filelist.
32 * This is a balance between performance and a good progress indicator.
33 */
34#define MAX_SKEL_DEPTH 3
35
36
37extern ssize_t getline(char **lineptr, size_t * n, FILE * stream);
38
39
40int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
41 char *include_paths, char *excp, int differential,
42 char *userdef_filelist);
43
44
45/*@unused@*/
46//static char cvsid[] = "$Id: libmondo-filelist.c 1075 2007-01-25 15:18:59Z bruno $";
47
48/**
49 * Number of lines in the filelist last loaded.
50 * @warning This implies that two filesets cannot be loaded at once.
51 * @ingroup globalGroup
52 */
53long g_original_noof_lines_in_filelist = 0;
54
55/**
56 * Number of filesets in the current backup.
57 * @ingroup globalGroup
58 */
59long g_noof_sets = 0;
60
61extern bool g_text_mode;
62extern newtComponent g_progressForm;
63extern int g_currentY;
64extern int g_noof_rows;
65
66extern char *g_getfacl;
67extern char *g_getfattr;
68
69
70
71/**
72 * @addtogroup filelistGroup
73 * @{
74 */
75/**
76 * Call chop_filelist() to chop the filelist into sets.
77 * @param bkpinfo The backup information structure. Fields used:
78 * - @c bkpinfo->image_devs
79 * - @c bkpinfo->optimal_set_size
80 * - @c bkpinfo->scratchdir
81 * - @c bkpinfo->tmpdir
82 * @see chop_filelist
83 */
84int call_filelist_chopper(struct s_bkpinfo *bkpinfo)
85{
86 /*@ buffers *********************** */
87 char *dev;
88 char *filelist;
89 char *tempfile;
90 char *cksumlist;
91 char *tmp;
92 long noof_sets;
93
94 /*@ pointers ********************** */
95 char *ptr;
96 FILE *fout;
97
98 /*@ int *************************** */
99 int i, retval = 0;
100
101 malloc_string(dev);
102 malloc_string(filelist);
103 malloc_string(tempfile);
104 malloc_string(cksumlist);
105 malloc_string(tmp);
106 mvaddstr_and_log_it(g_currentY, 0, "Dividing filelist into sets");
107
108 log_to_screen("Dividing filelist into sets. Please wait.");
109 i = 0;
110 sprintf(filelist, "%s/archives/filelist.full", bkpinfo->scratchdir);
111 sprintf(cksumlist, "%s/cklist.full", bkpinfo->tmpdir);
112 if (!does_file_exist(filelist)) {
113 log_it("filelist %s not found", filelist);
114 fatal_error("call_filelist_chopper() -- filelist not found!");
115 }
116
117 noof_sets =
118 chop_filelist(filelist, bkpinfo->tmpdir,
119 bkpinfo->optimal_set_size);
120 estimate_noof_media_required(bkpinfo, noof_sets); // for cosmetic purposes
121
122 sprintf(tempfile, "%s/biggielist.txt", bkpinfo->tmpdir);
123 if (!(fout = fopen(tempfile, "a"))) {
124 log_OS_error("Cannot append to biggielist");
125 retval++;
126 goto end_of_func;
127 }
128 log_it(bkpinfo->image_devs);
129
130 ptr = bkpinfo->image_devs;
131
132 while (ptr && *ptr) {
133 strcpy(dev, ptr);
134 log_it("Examining imagedev %s", dev);
135 for (i = 0; i < (int) strlen(dev) && dev[i] != ' '; i++);
136 dev[i] = '\0';
137 if (!strlen(dev)) {
138 continue;
139 }
140 fprintf(fout, "%s\n", dev);
141 log_it("Adding '%s' to biggielist", dev);
142 if ((ptr = strchr(ptr, ' '))) {
143 ptr++;
144 }
145 }
146 paranoid_fclose(fout);
147 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
148
149 end_of_func:
150 paranoid_free(filelist);
151 paranoid_free(tempfile);
152 paranoid_free(cksumlist);
153 paranoid_free(dev);
154 paranoid_free(tmp);
155 return (retval);
156}
157
158
159
160int sort_file(char *orig_fname)
161{
162 char *tmp_fname;
163 char *command;
164 int retval = 0;
165
166 log_msg(1, "Sorting file %s", orig_fname);
167 malloc_string(tmp_fname);
168 malloc_string(command);
169 sprintf(tmp_fname, "/tmp/sort.%d.%d.%d", (int) (random() % 32768),
170 (int) (random() % 32768), (int) (random() % 32768));
171
172 if (!does_file_exist(orig_fname)) {
173 return (0);
174 } // no sense in trying to sort an empty file
175
176 sprintf(command, "sort %s > %s 2>> %s", orig_fname, tmp_fname,
177 MONDO_LOGFILE);
178 retval = system(command);
179 if (retval) {
180 log_msg(2, "Failed to sort %s - oh dear", orig_fname);
181 } else {
182 log_msg(2, "Sorted %s --> %s OK. Copying it back to %s now",
183 orig_fname, tmp_fname, orig_fname);
184 sprintf(command, "mv -f %s %s", tmp_fname, orig_fname);
185 retval += run_program_and_log_output(command, 2);
186 if (retval) {
187 log_msg(2, "Failed to copy %s back to %s - oh dear", tmp_fname,
188 orig_fname);
189 } else {
190 log_msg(2, "%s was sorted OK.", orig_fname);
191 }
192 }
193 paranoid_free(tmp_fname);
194 paranoid_free(command);
195 log_msg(1, "Finished sorting file %s", orig_fname);
196 return (retval);
197}
198
199
200
201/**
202 * Chop the filelist into sets.
203 * Each fileset is a list of files whose total (uncompressed) size is usually
204 * about X KB. Files bigger than 8X KB are placed in a "biggielist"; they will
205 * be sliced and compressed separately from the regular files.
206 *
207 * @param filelist The big filelist (filelist.full) to chop up.
208 * @param outdir The directory to place the files (filelist.N where N is
209 * an integer, biggielist.txt, and LAST-FILELIST-NUMBER) created
210 * @param maxsetsizeK Optimal size of a fileset (X above).
211 * @return number of errors encountered (0 for success).
212 */
213int chop_filelist(char *filelist, char *outdir, long maxsetsizeK)
214{
215/*@ long ****************************************/
216 long lino = 0;
217 long max_sane_size_for_a_file;
218 long curr_set_size;
219 long noof_lines;
220 long siz;
221
222 /*@ int **************************************** */
223 int i;
224 long curr_set_no;
225
226 /*@ buffers ************************************* */
227 char *outfname;
228 char *biggie_fname;
229 char *incoming;
230 char *tmp;
231 char *acl_fname;
232 char *xattr_fname;
233
234 /*@ pointers *********************************** */
235 FILE *fin;
236 FILE *fout;
237 FILE *fbig;
238
239 /*@ structures ********************************* */
240 struct stat buf;
241 int err = 0;
242
243 malloc_string(outfname);
244 malloc_string(biggie_fname);
245 incoming = malloc(MAX_STR_LEN * 2);
246 malloc_string(tmp);
247 malloc_string(acl_fname);
248 malloc_string(xattr_fname);
249
250 assert_string_is_neither_NULL_nor_zerolength(filelist);
251 assert_string_is_neither_NULL_nor_zerolength(outdir);
252 assert(maxsetsizeK > 0);
253
254 max_sane_size_for_a_file = 32L * 1024L;
255// max_sane_size_for_a_file = maxsetsizeK*2;
256// if (max_sane_size_for_a_file > 32*1024)
257// { max_sane_size_for_a_file = 32*1024; }
258
259 log_it("filelist=%s;", filelist);
260 open_evalcall_form("Dividing filelist into sets");
261 noof_lines = count_lines_in_file(filelist);
262 if (!(fin = fopen(filelist, "r"))) {
263 log_OS_error("Cannot openin filelist");
264 return (0);
265 }
266 curr_set_no = 0;
267 curr_set_size = 0;
268 sprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
269 sprintf(biggie_fname, "%s/biggielist.txt", outdir);
270 log_it("outfname=%s; biggie_fname=%s", outfname, biggie_fname);
271 if (!(fbig = fopen(biggie_fname, "w"))) {
272 log_OS_error("Cannot openout biggie_fname");
273 err++;
274 goto end_of_func;
275 }
276 if (!(fout = fopen(outfname, "w"))) {
277 log_OS_error("Cannot openout outfname");
278 err++;
279 goto end_of_func;
280 }
281 (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
282 while (!feof(fin)) {
283 lino++;
284 i = strlen(incoming) - 1;
285 if (i < 0) {
286 i = 0;
287 }
288 if (i > MAX_STR_LEN - 1) {
289 incoming[MAX_STR_LEN - 30] = '\0';
290 log_msg(1, "Warning - truncating file %s's name", incoming);
291 err++;
292 }
293 if (incoming[i] < 32) {
294 incoming[i] = '\0';
295 }
296 if (!strncmp(incoming, "/dev/", 5)) {
297 siz = 1;
298 } else if (lstat(incoming, &buf) != 0) {
299 siz = 0;
300 } else {
301 siz = (long) (buf.st_size >> 10);
302 }
303 if (siz > max_sane_size_for_a_file)
304// && strcmp(incoming+strlen(incoming)-4, ".bz2") && strcmp(incoming+strlen(incoming)-4, ".tbz"))
305 {
306 fprintf(fbig, "%s\n", incoming);
307 } else {
308 curr_set_size += siz;
309 fprintf(fout, "%s\n", incoming);
310 if (curr_set_size > maxsetsizeK) {
311 paranoid_fclose(fout);
312 sort_file(outfname);
313 curr_set_no++;
314 curr_set_size = 0;
315 sprintf(outfname, "%s/filelist.%ld", outdir, curr_set_no);
316 if (!(fout = fopen(outfname, "w"))) {
317 log_OS_error("Unable to openout outfname");
318 err++;
319 goto end_of_func;
320 }
321 sprintf(tmp, "Fileset #%ld chopped ", curr_set_no - 1);
322 update_evalcall_form((int) (lino * 100 / noof_lines));
323 /* if (!g_text_mode) {newtDrawRootText(0,22,tmp);newtRefresh();} else {log_it(tmp);} */
324 }
325 }
326 (void) fgets(incoming, MAX_STR_LEN * 2 - 1, fin);
327 }
328 paranoid_fclose(fin);
329 paranoid_fclose(fout);
330 paranoid_fclose(fbig);
331
332 if (length_of_file(outfname) <= 2) {
333 unlink(outfname);
334 g_noof_sets--;
335 }
336 g_noof_sets = curr_set_no;
337 sort_file(outfname);
338 sort_file(biggie_fname);
339 sprintf(outfname, "%s/LAST-FILELIST-NUMBER", outdir);
340 sprintf(tmp, "%ld", curr_set_no);
341 if (write_one_liner_data_file(outfname, tmp)) {
342 log_OS_error
343 ("Unable to echo write one-liner to LAST-FILELIST-NUMBER");
344 err = 1;
345 }
346 if (curr_set_no == 0) {
347 sprintf(tmp, "Only one fileset. Fine.");
348 } else {
349 sprintf(tmp, "Filelist divided into %ld sets", curr_set_no + 1);
350 }
351 log_msg(1, tmp);
352 close_evalcall_form();
353 /* This is to work around an obscure bug in Newt; open a form, close it,
354 carry on... I don't know why it works but it works. If you don't do this
355 then update_progress_form() won't show the "time taken / time remaining"
356 line. The bug only crops up AFTER the call to chop_filelist(). Weird. */
357#ifndef _XWIN
358 if (!g_text_mode) {
359 open_progress_form("", "", "", "", 100);
360 newtPopHelpLine();
361 newtFormDestroy(g_progressForm);
362 newtPopWindow();
363 }
364#endif
365 end_of_func:
366 paranoid_free(outfname);
367 paranoid_free(biggie_fname);
368 paranoid_free(incoming);
369 paranoid_free(tmp);
370 paranoid_free(acl_fname);
371 paranoid_free(xattr_fname);
372 return (err ? 0 : curr_set_no + 1);
373}
374
375
376
377
378
379/**
380 * Free all the memory used by a filelist structure.
381 * Since this may take a long time for large filelists, a progress bar will be displayed.
382 * @param filelist The filelist to free.
383 */
384void free_filelist(struct s_node *filelist)
385{
386 /*@ int's ******************************************************* */
387 static int depth = 0;
388 int percentage;
389
390 /*@ long's ****************************************************** */
391 static long i = 0;
392
393 /*@ end vars **************************************************** */
394
395 assert(filelist != NULL);
396 if (depth == 0) {
397 open_evalcall_form("Freeing memory");
398 log_to_screen("Freeing memory formerly occupied by filelist");
399 }
400 depth++;
401
402 if (filelist->ch == '\0') {
403 if (!(i++ % 1111)) {
404 percentage =
405 (int) (i * 100 / g_original_noof_lines_in_filelist);
406 update_evalcall_form(percentage);
407
408 }
409 }
410
411 if (filelist->right) {
412 free_filelist(filelist->right);
413 filelist->right = NULL;
414 }
415 if (filelist->down) {
416/* if (!(i++ %39999)) { update_evalcall_form(0); } */
417 free_filelist(filelist->down);
418 filelist->down = NULL;
419 }
420 filelist->ch = '\0';
421 paranoid_free(filelist);
422 depth--;
423 if (depth == 0) {
424 close_evalcall_form();
425 log_it("Finished freeing memory");
426 }
427}
428
429
430int call_exe_and_pipe_output_to_fd(char *syscall, FILE * pout)
431{
432 FILE *pattr;
433 char *tmp;
434 pattr = popen(syscall, "r");
435 if (!pattr) {
436 log_msg(1, "Failed to open fattr() %s", syscall);
437 return (1);
438 }
439 if (feof(pattr)) {
440 log_msg(1, "Failed to call fattr() %s", syscall);
441 paranoid_pclose(pattr);
442 return (2);
443 }
444 malloc_string(tmp);
445 for (fgets(tmp, MAX_STR_LEN, pattr); !feof(pattr);
446 fgets(tmp, MAX_STR_LEN, pattr)) {
447 fputs(tmp, pout);
448 }
449 paranoid_pclose(pattr);
450 paranoid_free(tmp);
451 return (0);
452}
453
454
455
456int gen_aux_list(char *filelist, char *syscall_sprintf,
457 char *auxlist_fname)
458{
459 FILE *fin;
460 FILE *pout;
461 char *pout_command;
462 char *syscall;
463 char *file_to_analyze;
464 char *strtmp = NULL;
465 int i;
466
467 if (!(fin = fopen(filelist, "r"))) {
468 log_msg(1, "Cannot openin filelist %s", filelist);
469 return (1);
470 }
471 malloc_string(pout_command);
472 sprintf(pout_command, "gzip -c1 > %s", auxlist_fname);
473 if (!(pout = popen(pout_command, "w"))) {
474 log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
475 fclose(fin);
476 paranoid_free(pout_command);
477 return (4);
478 }
479 malloc_string(file_to_analyze);
480 for (fgets(file_to_analyze, MAX_STR_LEN, fin); !feof(fin);
481 fgets(file_to_analyze, MAX_STR_LEN, fin)) {
482 i = strlen(file_to_analyze);
483 if (i > 0 && file_to_analyze[i - 1] < 32) {
484 file_to_analyze[i - 1] = '\0';
485 }
486 log_msg(8, "Analyzing %s", file_to_analyze);
487 asprintf(&strtmp, syscall_sprintf, mr_stresc(file_to_analyze, "`$\\\"", '\\'));
488 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, "/tmp/%d.%d.mask", (int) (random() % 32768),
581 (int) (random() % 32768));
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 iamhere("Unable to openout to syscall_pout");
595 return (1);
596 }
597 pin = popen(syscall_pin, "r");
598 if (!pin) {
599 pclose(pout);
600 iamhere("Unable to openin from syscall");
601 return (1);
602 }
603 faclin = fopen(masklist, "r");
604 if (!faclin) {
605 pclose(pin);
606 pclose(pout);
607 iamhere("Unable to openin masklist");
608 return (1);
609 }
610// printf("Hi there. Starting the loop\n");
611
612 fgets(current_subset_file, MAX_STR_LEN, faclin);
613 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 fgets(current_subset_file, MAX_STR_LEN, faclin);
646 continue;
647 }
648
649 if (!i) {
650 fputs(incoming, pout);
651 }
652 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 fgets(incoming, MAX_STR_LEN, pin);
666 }
667 if (!i) {
668 fgets(current_subset_file, MAX_STR_LEN, faclin);
669 }
670 }
671 while (!feof(pin)) {
672 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(struct s_bkpinfo *bkpinfo)
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 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 (fgets(fname, MAX_STR_LEN, pin); !feof(pin);
919 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(struct s_bkpinfo *bkpinfo)
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 *dir, 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[MAX_STR_LEN];
1360 char *tmp;
1361 char *sth_B;
1362 static int percentage = 0;
1363 char *ith_B;
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 malloc_string(tmp);
1382 malloc_string(sth_B);
1383 malloc_string(ith_B);
1384 malloc_string(new_with_spaces);
1385 p = strrchr(dir, '/');
1386 if (p) {
1387 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
1388 return (0);
1389 }
1390 }
1391
1392 if (!depth) {
1393 malloc_string(name_of_evalcall_form);
1394 malloc_string(find_skeleton_marker);
1395 asprintf(&find_excludes, " ");
1396 while((token = mr_strtok (sth, delims, &lastpos))) {
1397 asprintf(&strtmp,"%s", find_excludes);
1398 paranoid_free(find_excludes);
1399 asprintf(&find_excludes,"%s -path %s -prune -o", strtmp, token);
1400 paranoid_free(strtmp);
1401 paranoid_free(token);
1402 }
1403#if linux
1404 // 2.6 has /sys as a proc-type thing -- must be excluded
1405 asprintf(&strtmp,
1406 "find %s -maxdepth %d -fstype mvfs -prune -o -path /dev/shm -prune -o %s -type d -print > %s 2> /dev/null",
1407 dir, MAX_SKEL_DEPTH, find_excludes, g_skeleton_filelist);
1408#else
1409 // On BSD, for example, /sys is the kernel sources -- don't exclude
1410 asprintf(&strtmp,
1411 "find %s -maxdepth %d -fstype mvfs -prune -o -path /proc -prune -o %s -type d -print > %s 2> /dev/null",
1412 dir, MAX_SKEL_DEPTH, find_excludes, g_skeleton_filelist);
1413#endif
1414 paranoid_free(find_excludes);
1415 log_msg(5, "find command = %s", strtmp);
1416 system(strtmp);
1417 paranoid_free(strtmp);
1418 sprintf(tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
1419 g_skeleton_entries =
1420 1 + atol(call_program_and_get_last_line_of_output(tmp));
1421 sprintf(name_of_evalcall_form, "Making catalog of %s", dir);
1422 open_evalcall_form(name_of_evalcall_form);
1423 find_skeleton_marker[0] = '\0';
1424 skeleton_lino = 1;
1425 log_msg(5, "entries = %ld", g_skeleton_entries);
1426 percentage = 0;
1427 } else if (depth <= MAX_SKEL_DEPTH) // update evalcall form if appropriate
1428 {
1429 sprintf(find_skeleton_marker,
1430 "grep -Fv \"%s\" %s > %s.new 2> /dev/null", dir,
1431 g_skeleton_filelist, g_skeleton_filelist);
1432// log_msg(0, "fsm = %s", find_skeleton_marker);
1433 if (!system(find_skeleton_marker)) {
1434 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1435 skeleton_lino++;
1436// log_msg(5, "Found %s", dir);
1437// log_msg(2, "Incrementing skeleton_lino; now %ld/%ld (%d%%)", skeleton_lino, g_skeleton_entries, percentage);
1438 sprintf(find_skeleton_marker, "mv -f %s.new %s",
1439 g_skeleton_filelist, g_skeleton_filelist);
1440// log_msg(6, "fsm = %s", find_skeleton_marker);
1441 run_program_and_log_output(find_skeleton_marker, 8);
1442 time(&this_time);
1443 if (this_time != last_time) {
1444 last_time = this_time;
1445#ifndef _XWIN
1446 if (!g_text_mode) {
1447 sprintf(tmp, "Reading %-68s", dir);
1448 newtDrawRootText(0, g_noof_rows - 3, tmp);
1449 }
1450#endif
1451 update_evalcall_form(percentage);
1452 }
1453 }
1454 }
1455
1456 depth++;
1457
1458// log_msg(0, "Cataloguing %s", dir);
1459 if (sth[0] == ' ') {
1460 skip_these = sth;
1461 } else {
1462 skip_these = sth_B;
1463 sprintf(skip_these, " %s ", sth);
1464 }
1465 sprintf(new_with_spaces, " %s ", dir);
1466 if ((dip = opendir(dir)) == NULL) {
1467 log_OS_error("opendir");
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 strcpy(new, dir);
1475 if (strcmp(dir, "/")) {
1476 strcat(new, "/");
1477 }
1478 strcat(new, dit->d_name);
1479 new_with_spaces[0] = ' ';
1480 strcpy(new_with_spaces + 1, new);
1481 strcat(new_with_spaces, " ");
1482 if (strstr(skip_these, new_with_spaces)) {
1483 fprintf(fout, "%s\n", new);
1484 } else {
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 sprintf(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 }
1508 }
1509 }
1510 }
1511 }
1512 }
1513 }
1514 if (dip) {
1515 if (closedir(dip) == -1) {
1516 log_OS_error("closedir");
1517 }
1518 }
1519 depth--;
1520 if (!depth) {
1521 close_evalcall_form();
1522 paranoid_free(name_of_evalcall_form);
1523 paranoid_free(find_skeleton_marker);
1524 unlink(g_skeleton_filelist);
1525 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1526 }
1527 paranoid_free(tmp);
1528 paranoid_free(sth_B);
1529 paranoid_free(ith_B);
1530 paranoid_free(new_with_spaces);
1531 return (0);
1532}
1533
1534
1535
1536/**
1537 * Get the next entry in the space-separated list in @p incoming.
1538 * So if @p incoming was '"one and two" three four', we would
1539 * return "one and two".
1540 * @param incoming The list to get the next entry from.
1541 * @return The first item in the list (respecting double quotes).
1542 * @note The returned string points to static data that will be overwritten with each call.
1543 */
1544char *next_entry(char *incoming)
1545{
1546 static char sz_res[MAX_STR_LEN];
1547 char *p;
1548 bool in_quotes = FALSE;
1549
1550 strcpy(sz_res, incoming);
1551 p = sz_res;
1552 while ((*p != ' ' || in_quotes) && *p != '\0') {
1553 if (*p == '\"') {
1554 in_quotes = !in_quotes;
1555 }
1556 p++;
1557 }
1558 *p = '\0';
1559 return (sz_res);
1560}
1561
1562
1563
1564/**
1565 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1566 * @param logfile Unused.
1567 * @param tmpdir The tmpdir of the backup.
1568 * @param scratchdir The scratchdir of the backup.
1569 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1570 * @param excp The paths to NOT back up.
1571 * @param differential The differential level (currently only 0 and 1 are supported).
1572 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1573 * @return 0, always.
1574 * @bug @p logfile is unused.
1575 * @bug Return value is meaningless.
1576 */
1577int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1578 char *include_paths, char *excp, int differential,
1579 char *userdef_filelist)
1580{
1581 char sz_datefile_wildcard[] = "/var/cache/mondo-archive/difflevel.%d";
1582 char *p, *q;
1583 char sz_datefile[80];
1584 char *sz_filelist, *exclude_paths, *tmp;
1585 int i;
1586 FILE *fout;
1587 char *command;
1588 time_t time_of_last_full_backup = 0;
1589 struct stat statbuf;
1590
1591 malloc_string(command);
1592 malloc_string(tmp);
1593 malloc_string(sz_filelist);
1594 malloc_string(g_skeleton_filelist);
1595 if (!(exclude_paths = malloc(1000))) {
1596 fatal_error("Cannot malloc exclude_paths");
1597 }
1598 log_msg(3, "Trying to write test string to exclude_paths");
1599 strcpy(exclude_paths, "/blah /froo");
1600 log_msg(3, "...Success!");
1601 sprintf(sz_datefile, sz_datefile_wildcard, 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 sprintf(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
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 sprintf(exclude_paths, " %s %s %s %s %s %s . .. \
1649" MNT_CDROM " " MNT_FLOPPY " /media \
1650/proc /sys /tmp /root/images/mondo /var/cache/mindi ", excp, call_program_and_get_last_line_of_output("locate /win386.swp 2> /dev/null"), call_program_and_get_last_line_of_output("locate /hiberfil.sys 2> /dev/null"), call_program_and_get_last_line_of_output("locate /pagefile.sys 2> /dev/null"), (tmpdir[0] == '/' && tmpdir[1] == '/') ? (tmpdir + 1) : tmpdir, (scratchdir[0] == '/' && scratchdir[1] == '/') ? (scratchdir + 1) : scratchdir);
1651
1652 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1653 log_msg(2,
1654 "Generating skeleton filelist so that we can track our progress");
1655 sprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
1656 make_hole_for_file(g_skeleton_filelist);
1657 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1658 log_msg(2, "Opening out filelist to %s", sz_filelist);
1659 if (!(fout = fopen(sz_filelist, "w"))) {
1660 fatal_error("Cannot openout to sz_filelist");
1661 }
1662 i = 0;
1663 if (strlen(include_paths) == 0) {
1664 log_msg(1, "Including only '/' in %s", sz_filelist);
1665 open_and_list_dir("/", exclude_paths, fout,
1666 time_of_last_full_backup);
1667 } else {
1668 p = include_paths;
1669 while (*p) {
1670 q = next_entry(p);
1671 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1672 open_and_list_dir(q, exclude_paths, fout,
1673 time_of_last_full_backup);
1674 p += strlen(q);
1675 while (*p == ' ') {
1676 p++;
1677 }
1678 }
1679 }
1680 paranoid_fclose(fout);
1681 }
1682 log_msg(2, "Copying new filelist to scratchdir");
1683 sprintf(command, "mkdir -p %s/archives", scratchdir);
1684 paranoid_system(command);
1685 sprintf(command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
1686 paranoid_system(command);
1687 sprintf(command, "mv -f %s %s", sz_filelist, tmpdir);
1688 paranoid_system(command);
1689 log_msg(2, "Freeing variables");
1690 paranoid_free(sz_filelist);
1691 paranoid_free(command);
1692 paranoid_free(exclude_paths);
1693 paranoid_free(tmp);
1694 paranoid_free(g_skeleton_filelist);
1695 log_msg(2, "Exiting");
1696 return (0);
1697}
1698
1699
1700
1701
1702/**
1703 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1704 * @param startnode The node containing the root of the directory tree.
1705 * @param string_to_find The string to look for at @p startnode.
1706 * @return The node containing the last element of @p string_to_find, or NULL if
1707 * it was not found.
1708 */
1709struct s_node *find_string_at_node(struct s_node *startnode,
1710 char *string_to_find)
1711{
1712 /*@ int ******************************************************** */
1713 int noof_chars;
1714 static int depth = 0;
1715 static char original_string[MAX_STR_LEN];
1716
1717 /*@ sturctures ************************************************* */
1718 struct s_node *node;
1719
1720 /*@ char ****************************************************** */
1721 char char_to_find;
1722
1723 /*@ bools ****************************************************** */
1724
1725 if (!depth) {
1726 strcpy(original_string, string_to_find);
1727 }
1728
1729 assert(startnode != NULL);
1730 assert(string_to_find != NULL);
1731
1732 noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
1733
1734 log_msg(7, "starting --- str=%s", string_to_find);
1735
1736/* walk across tree if necessary */
1737 node = startnode;
1738 char_to_find = string_to_find[0];
1739 if (node->right != NULL && node->ch < char_to_find) {
1740 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1741 char_to_find, node->ch, (node->right)->ch);
1742 return (find_string_at_node(node->right, string_to_find));
1743 }
1744
1745/* walk down tree if appropriate */
1746 if (node->down != NULL && node->ch == char_to_find) {
1747 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1748 depth++;
1749 node = find_string_at_node(node->down, string_to_find + 1);
1750 depth--;
1751 return (node);
1752 }
1753
1754 if (char_to_find == '\0' && node->ch == '\0') {
1755 log_msg(7, "%s is in tree", original_string);
1756 return (node);
1757 } else {
1758 log_msg(7, "%s is NOT in tree", original_string);
1759 return (NULL);
1760 }
1761}
1762
1763
1764
1765/**
1766 * Write all entries in @p needles_list_fname which are also in
1767 * @p filelist to @p matches_list_fname.
1768 * @param needles_list_fname A file containing strings to look for, 1 per line.
1769 * @param filelist The node for the root of the directory structure to search in.
1770 * @param matches_list_fname The filename where we should put the matches.
1771 * @return The number of matches found.
1772 */
1773long save_filelist_entries_in_common(char *needles_list_fname,
1774 struct s_node *filelist,
1775 char *matches_list_fname,
1776 bool use_star)
1777{
1778 int retval = 0;
1779 struct s_node *found_node;
1780 FILE *fin;
1781 FILE *fout;
1782 char *fname;
1783 char *tmp;
1784 size_t len = 0; // Scrub's patch doesn't work without that
1785
1786// log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
1787 malloc_string(fname);
1788 malloc_string(tmp);
1789 log_msg(5, "starting");
1790 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1791 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1792 if (!(fin = fopen(needles_list_fname, "r"))) {
1793 fatal_error("Cannot openin needles_list_fname");
1794 }
1795 if (!(fout = fopen(matches_list_fname, "w"))) {
1796 fatal_error("Cannot openout matches_list_fname");
1797 }
1798 while (!feof(fin)) {
1799// fscanf(fin, "%s\n", fname);
1800 len = MAX_STR_LEN - 1;
1801 getline(&fname, &len, fin); // patch by Scrub
1802 if (!use_star) {
1803 if (fname[0] == '/') {
1804 strcpy(tmp, fname);
1805 } else {
1806 tmp[0] = '/';
1807 strcpy(tmp + 1, fname);
1808 }
1809 strcpy(fname, tmp);
1810 }
1811 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1812 fname[strlen(fname) - 1] = '\0';
1813 }
1814
1815/*
1816 if (strlen(fname)>3 && fname[strlen(fname)-1]=='/') { fname[strlen(fname)-1] = '\0'; }
1817 if (strlen(fname)==0) { continue; }
1818 sprintf(temporary_string, "echo \"Looking for '%s'\" >> /tmp/looking.txt", fname);
1819 system(temporary_string);
1820*/
1821
1822 log_msg(5, "Looking for '%s'", fname);
1823 found_node = find_string_at_node(filelist, fname);
1824 if (found_node) {
1825 if (found_node->selected) {
1826// if (use_star)
1827 if (fname[0] == '/') {
1828 strcpy(tmp, fname + 1);
1829 strcpy(fname, tmp);
1830 }
1831 log_msg(5, "Found '%s'", fname);
1832 turn_wildcard_chars_into_literal_chars(tmp, fname);
1833 fprintf(fout, "%s\n", tmp);
1834 retval++;
1835 }
1836 }
1837 }
1838 paranoid_fclose(fout);
1839 paranoid_fclose(fin);
1840 paranoid_free(fname);
1841 paranoid_free(tmp);
1842 return (retval);
1843}
1844
1845
1846
1847
1848
1849
1850/**
1851 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1852 * @p filelist.
1853 * @param filelist The top node of the directory structure to add the files to.
1854 * @param list_of_files_fname The file containing the files to add, 1 per line.
1855 * @param flag_em If TRUE, then flag the added files for restoration.
1856 * @return 0 for success, nonzero for failure.
1857 */
1858int add_list_of_files_to_filelist(struct s_node *filelist,
1859 char *list_of_files_fname, bool flag_em)
1860{
1861 FILE *fin;
1862 char *tmp;
1863 struct s_node *nod;
1864
1865 malloc_string(tmp);
1866 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1867 if (!(fin = fopen(list_of_files_fname, "r"))) {
1868 iamhere(list_of_files_fname);
1869 return (1);
1870 }
1871 for (fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
1872 fgets(tmp, MAX_STR_LEN, fin)) {
1873 if (!tmp[0]) {
1874 continue;
1875 }
1876 if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
1877 && strlen(tmp) > 0) {
1878 tmp[strlen(tmp) - 1] = '\0';
1879 }
1880 log_msg(2, "tmp = '%s'", tmp);
1881 if (!tmp[0]) {
1882 continue;
1883 }
1884 if ((nod = find_string_at_node(filelist, tmp))) {
1885 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1886 } else {
1887 add_string_at_node(filelist, tmp);
1888 nod = find_string_at_node(filelist, tmp);
1889 }
1890
1891 if (nod && flag_em) {
1892 toggle_path_selection(filelist, tmp, TRUE);
1893 log_msg(5, "Flagged '%s'", tmp);
1894 }
1895 }
1896 paranoid_fclose(fin);
1897 paranoid_free(tmp);
1898 return (0);
1899}
1900
1901/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.