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

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

place the code of mondo_makefilelist before its call

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