source: MondoRescue/branches/3.0/mondo/src/common/libmondo-filelist.c@ 3107

Last change on this file since 3107 was 3060, checked in by Bruno Cornec, 11 years ago

r5035@localhost: bruno | 2012-11-09 03:17:01 +0100

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