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

Last change on this file since 2576 was 2576, checked in by Bruno Cornec, 14 years ago

svn merge -r 2567:2574 /mondorescue/branches/2.2.9
(At parity with 2.2.9.2)

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