source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-filelist.c@ 2324

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

r3335@localhost: bruno | 2009-08-08 23:04:12 +0200

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