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

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

r3289@localhost: bruno | 2009-07-21 15:12:29 +0200

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