source: MondoRescue/branches/3.2/mondo/src/common/libmondo-filelist.c@ 3271

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