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

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