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

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

log_msg => mr_msg for common files

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