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

Last change on this file since 2704 was 2704, checked in by Bruno Cornec, 13 years ago

r4180@localhost: bruno | 2011-01-27 10:26:41 +0100

  • Fix multiple port issues from 2.2.9 that prevented mindi to work
  • Change interface of call_program_and_get_last_line_of_output to allow not logging (call to mindi)
  • Status is working boot, but NFS interface broken due to conf file issues
  • Property svn:keywords set to Id
File size: 49.2 KB
Line 
1/*
2 $Id: libmondo-filelist.c 2704 2011-01-27 18:31:24Z 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 2704 2011-01-27 18:31:24Z 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->scratchdir);
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->scratchdir);
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 if (incoming[i] < 32) {
264 incoming[i] = '\0';
265 }
266 if (!strncmp(incoming, "/dev/", 5)) {
267 siz = 1;
268 } else if (lstat(incoming, &buf) != 0) {
269 siz = 0;
270 } else {
271 siz = (long) (buf.st_size >> 10);
272 }
273 if (siz > max_sane_size_for_a_file) {
274 log_msg(10, "Adding %s to big files\n", incoming);
275 fprintf(fbig, "%s\n", incoming);
276 } else {
277 curr_set_size += siz;
278 log_msg(10, "Adding %s to filelist %d\n", incoming, curr_set_no);
279 fprintf(fout, "%s\n", incoming);
280 if (curr_set_size > maxsetsizeK) {
281 paranoid_fclose(fout);
282 sort_file(outfname);
283 mr_free(outfname);
284 curr_set_no++;
285 curr_set_size = 0;
286
287 mr_asprintf(outfname, "%s/filelist.%ld", bkpinfo->tmpdir, curr_set_no);
288 if (!(fout = fopen(outfname, "w"))) {
289 log_OS_error("Unable to openout outfname");
290 err++;
291 mr_free(outfname);
292 mr_free(biggie_fname);
293 mr_free(incoming);
294 return (curr_set_no + 1);
295 }
296 update_evalcall_form((int) (lino * 100 / noof_lines));
297 }
298 }
299 mr_free(incoming);
300 mr_getline(incoming, fin);
301 }
302 mr_free(incoming);
303
304 paranoid_fclose(fin);
305 paranoid_fclose(fout);
306 paranoid_fclose(fbig);
307
308 if (length_of_file(outfname) <= 2) {
309 unlink(outfname);
310 g_noof_sets--;
311 }
312 g_noof_sets = curr_set_no;
313 sort_file(outfname);
314 mr_free(outfname);
315
316 sort_file(biggie_fname);
317 mr_free(biggie_fname);
318
319 mr_asprintf(tmp, "echo 'last-filelist-number %ld' >> "MINDI_CACHE"/mondorestore.cfg", curr_set_no);
320 paranoid_system(tmp);
321 mr_free(tmp);
322
323 if (curr_set_no == 0) {
324 log_msg(1, "Only one fileset. Fine.");
325 } else {
326 log_msg(1, "Filelist divided into %ld sets", curr_set_no + 1);
327 }
328 close_evalcall_form();
329 /* This is to work around an obscure bug in Newt; open a form, close it,
330 carry on... I don't know why it works but it works. If you don't do this
331 then update_progress_form() won't show the "time taken / time remaining"
332 line. The bug only crops up AFTER the call to chop_filelist(). Weird. */
333 if (!g_text_mode) {
334 open_progress_form("", "", "", "", 100);
335 newtPopHelpLine();
336 newtFormDestroy(g_progressForm);
337 newtPopWindow();
338 }
339 return (err ? 0 : curr_set_no + 1);
340}
341
342
343
344
345
346/**
347 * Free all the memory used by a filelist structure.
348 * Since this may take a long time for large filelists, a progress bar will be displayed.
349 * @param filelist The filelist to free.
350 */
351void free_filelist(struct s_node *filelist)
352{
353 /*@ int's ******************************************************* */
354 static int depth = 0;
355 int percentage;
356
357 /*@ long's ****************************************************** */
358 static long i = 0;
359
360 /*@ end vars **************************************************** */
361
362 assert(filelist != NULL);
363 if (depth == 0) {
364 open_evalcall_form("Freeing memory");
365 log_to_screen("Freeing memory formerly occupied by filelist");
366 }
367 depth++;
368
369 if (filelist->ch == '\0') {
370 if (!(i++ % 1111)) {
371 percentage =
372 (int) (i * 100 / g_original_noof_lines_in_filelist);
373 update_evalcall_form(percentage);
374
375 }
376 }
377
378 if (filelist->right) {
379 free_filelist(filelist->right);
380 filelist->right = NULL;
381 }
382 if (filelist->down) {
383/* if (!(i++ %39999)) { update_evalcall_form(0); } */
384 free_filelist(filelist->down);
385 filelist->down = NULL;
386 }
387 filelist->ch = '\0';
388 paranoid_free(filelist);
389 depth--;
390 if (depth == 0) {
391 close_evalcall_form();
392 log_it("Finished freeing memory");
393 }
394}
395
396
397int call_exe_and_pipe_output_to_fd(char *syscall, FILE * pout)
398{
399 FILE *pattr = NULL;
400 char *tmp = NULL;
401
402 pattr = popen(syscall, "r");
403 if (!pattr) {
404 log_msg(1, "Failed to open fattr() %s", syscall);
405 return (1);
406 }
407 if (feof(pattr)) {
408 log_msg(1, "Failed to call fattr() %s", syscall);
409 paranoid_pclose(pattr);
410 return (2);
411 }
412 for (mr_getline(tmp, pattr); !feof(pattr); mr_getline(tmp, pattr)) {
413 fputs(tmp, pout);
414 mr_free(tmp);
415 }
416 mr_free(tmp);
417 paranoid_pclose(pattr);
418 return (0);
419}
420
421
422
423int gen_aux_list(char *filelist, char *syscall_sprintf,
424 char *auxlist_fname)
425{
426 FILE *fin = NULL;
427 FILE *pout = NULL;
428 char *pout_command = NULL;
429 char *syscall = NULL;
430 char *file_to_analyze = NULL;
431 char *strtmp = NULL;
432 char *tmp = NULL;
433 int i;
434
435 if (!(fin = fopen(filelist, "r"))) {
436 log_msg(1, "Cannot openin filelist %s", filelist);
437 return (1);
438 }
439 mr_asprintf(pout_command, "gzip -c1 > %s", auxlist_fname);
440 if (!(pout = popen(pout_command, "w"))) {
441 log_msg(1, "Cannot openout auxlist_fname %s", auxlist_fname);
442 fclose(fin);
443 mr_free(pout_command);
444 return (4);
445 }
446 mr_free(pout_command);
447
448 for (mr_getline(file_to_analyze, fin); !feof(fin); mr_getline(file_to_analyze, fin)) {
449 i = strlen(file_to_analyze);
450 if (i > 0 && file_to_analyze[i - 1] < 32) {
451 file_to_analyze[i - 1] = '\0';
452 }
453 log_msg(8, "Analyzing %s", file_to_analyze);
454 tmp = mr_stresc(file_to_analyze, " `$\\\"(){}[]'*?&|!#~", '\\');
455 mr_asprintf(strtmp, syscall_sprintf, tmp);
456 mr_free(tmp);
457
458 mr_asprintf(syscall, "%s 2>> /dev/null", strtmp); // " MONDO_LOGFILE);
459 mr_free(strtmp);
460
461 log_msg(20,"calling %s\n",syscall);
462 call_exe_and_pipe_output_to_fd(syscall, pout);
463 mr_free(syscall);
464 mr_free(file_to_analyze);
465 }
466 mr_free(file_to_analyze);
467 paranoid_fclose(fin);
468 paranoid_pclose(pout);
469 return (0);
470}
471
472
473int get_acl_list(char *filelist, char *facl_fname)
474{
475 char *command = NULL;
476 int retval = 0;
477
478 if (g_getfacl != NULL) {
479 mr_asprintf(command, "touch \"%s\"", facl_fname);
480 run_program_and_log_output(command, 8);
481 mr_free(command);
482
483 retval =
484 gen_aux_list(filelist, "getfacl --all-effective -P \"%s\"", facl_fname);
485 }
486 return (retval);
487}
488
489
490int get_fattr_list(char *filelist, char *fattr_fname)
491{
492 char *command;
493 int retval = 0;
494
495 if (g_getfattr != NULL) {
496 mr_asprintf(command, "touch \"%s\"", fattr_fname);
497 run_program_and_log_output(command, 8);
498 mr_free(command);
499 retval =
500 gen_aux_list(filelist, "getfattr --en=hex -m - -h -d \"%s\"", fattr_fname);
501 }
502 return (retval);
503}
504
505
506int set_EXAT_list(char *orig_msklist, char *original_exat_fname,
507 char *executable)
508{
509 const int my_depth = 8;
510 char *command = NULL;
511 char *syscall_pin = NULL;
512 char *syscall_pout = NULL;
513 char *incoming = NULL;
514 char *current_subset_file = NULL;
515 char *current_master_file = NULL;
516 char *masklist = NULL;
517 int retval = 0;
518 int i;
519 char *p, *q;
520 char *tmp = NULL;
521 FILE *pin, *pout, *faclin;
522
523 log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist, original_exat_fname, executable);
524 if (!orig_msklist || !orig_msklist[0]
525 || !does_file_exist(orig_msklist)) {
526 log_msg(1, "No masklist provided. I shall therefore set ALL attributes.");
527 if (strstr(executable,"acl")) {
528 /* setfacl needs no additional option for physical walk */
529 mr_asprintf(tmp,"");
530 } else {
531 /* setfattr needs option -h for physical walk */
532 mr_asprintf(tmp,"-h");
533 }
534 mr_asprintf(command, "gzip -dc %s | %s %s --restore - 2>> %s", original_exat_fname, executable, tmp, MONDO_LOGFILE);
535 mr_free(tmp);
536 log_msg(1, "command = %s", command);
537 retval = system(command);
538 mr_free(command);
539 log_msg(1, "Returning w/ retval=%d", retval);
540 return (retval);
541 }
542 if (length_of_file(original_exat_fname) <= 0) {
543 log_msg(1, "original_exat_fname %s is empty or missing, so no need to set EXAT list", original_exat_fname);
544 return (0);
545 }
546 mr_asprintf(masklist, "%s/masklist", bkpinfo->tmpdir);
547 mr_asprintf(command, "cp -f %s %s", orig_msklist, masklist);
548 run_program_and_log_output(command, 1);
549 mr_free(command);
550
551 sort_file(masklist);
552
553 mr_asprintf(syscall_pout, "%s --restore - 2>> %s", executable, MONDO_LOGFILE);
554 log_msg(1, "syscall_pout = %s", syscall_pout);
555 pout = popen(syscall_pout, "w");
556 mr_free(syscall_pout);
557
558 if (!pout) {
559 log_it("Unable to openout to syscall_pout");
560 mr_free(masklist);
561 return (1);
562 }
563
564 mr_asprintf(syscall_pin, "gzip -dc %s", original_exat_fname);
565 log_msg(1, "syscall_pin = %s", syscall_pin);
566 pin = popen(syscall_pin, "r");
567 mr_free(syscall_pin);
568
569 if (!pin) {
570 pclose(pout);
571 log_it("Unable to openin from syscall");
572 return (1);
573 }
574 faclin = fopen(masklist, "r");
575 if (!faclin) {
576 pclose(pin);
577 pclose(pout);
578 log_it("Unable to openin masklist");
579 mr_free(masklist);
580 return (1);
581 }
582// printf("Hi there. Starting the loop\n");
583
584 mr_getline(current_subset_file, faclin);
585 mr_getline(incoming, pin);
586 while (!feof(pin) && !feof(faclin)) {
587 mr_asprintf(current_master_file, "%s", incoming + 8);
588
589 p = current_subset_file;
590 if (*p == '/') {
591 p++;
592 }
593 i = strlen(p);
594 if (i > 0 && p[i - 1] < 32) {
595 p[i - 1] = '\0';
596 }
597
598
599 q = current_master_file;
600 if (*q == '/') {
601 q++;
602 }
603 i = strlen(q);
604 if (i > 0 && q[i - 1] < 32) {
605 q[i - 1] = '\0';
606 }
607
608 i = strcmp(p, q);
609 log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
610
611// printf("%s v %s --> %d\n", p, q, i);
612
613 if (i < 0) { // read another subset file in.
614 log_msg(my_depth, "Reading next subset line in\n\n");
615 mr_free(current_subset_file);
616 mr_getline(current_subset_file, faclin);
617 continue;
618 }
619
620 if (!i) {
621 fputs(incoming, pout);
622 }
623 mr_free(incoming);
624 mr_getline(incoming, pin);
625 if (!i) {
626 log_msg(my_depth, "Copying master %s", q);
627 }
628
629 while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
630 if (!i) {
631 fputs(incoming, pout);
632 }
633 mr_free(incoming);
634 mr_getline(incoming, pin);
635 }
636 if (!i) {
637 mr_free(current_subset_file);
638 mr_getline(current_subset_file, faclin);
639 }
640 mr_free(current_master_file);
641 }
642 mr_free(current_subset_file);
643 mr_free(incoming);
644 fclose(faclin);
645 pclose(pin);
646 pclose(pout);
647
648 unlink(masklist);
649 mr_free(masklist);
650
651 return (retval);
652}
653
654
655int set_fattr_list(char *masklist, char *fattr_fname) {
656
657 char *tmp = NULL;
658
659 tmp = find_home_of_exe("setfattr");
660 if (tmp) {
661 mr_free(tmp);
662 return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
663 } else {
664 mr_free(tmp);
665 log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
666 return(0);
667 }
668}
669
670
671
672int set_acl_list(char *masklist, char *acl_fname) {
673
674 char *tmp = NULL;
675
676 tmp = find_home_of_exe("setfacl");
677 if (tmp) {
678 mr_free(tmp);
679 return (set_EXAT_list(masklist, acl_fname, "setfacl"));
680 } else {
681 mr_free(tmp);
682 log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
683 return(0);
684 }
685}
686
687
688/**
689 * Get the number of the last fileset in the backup.
690 * @return The last filelist number.
691 * @note This function should only be called at restore-time.
692 */
693int get_last_filelist_number()
694{
695 /*@ buffers ***************************************************** */
696 char *val_sz = NULL;
697 char *cfg_fname = NULL;
698
699 /*@ long ******************************************************** */
700 int val_i;
701
702 /*@ end vars **************************************************** */
703
704 assert(bkpinfo != NULL);
705
706 mr_asprintf(cfg_fname, "%s/mondorestore.cfg", MINDI_CACHE);
707 val_sz = read_cfg_var(cfg_fname, "last-filelist-number");
708 mr_free(cfg_fname);
709
710 if (val_sz == NULL) {
711 mr_asprintf(val_sz, "");
712 }
713 val_i = atoi(val_sz);
714 mr_free(val_sz);
715
716 if (val_i <= 0) {
717 val_i = 500;
718 }
719 return (val_i);
720}
721
722
723/**
724 * Add a string at @p startnode.
725 * @param startnode The node to start at when searching for where to add the string.
726 * @param string_to_add The string to add.
727 * @return 0 for success, 1 for failure.
728 * @bug I don't understand this function. Would someone care to explain it?
729 */
730int add_string_at_node(struct s_node *startnode, char *string_to_add)
731{
732
733
734 /*@ int ******************************************************** */
735 int noof_chars;
736 int i;
737 int res;
738
739 /*@ sturctures ************************************************* */
740 struct s_node *node, *newnode;
741
742 /*@ char ****************************************************** */
743 char char_to_add;
744
745 /*@ bools ****************************************************** */
746
747 const bool sosodef = FALSE;
748
749 static int depth = 0;
750 static char original_string[MAX_STR_LEN];
751
752 assert(startnode != NULL);
753 assert(string_to_add != NULL);
754
755 if (!depth) {
756 strcpy(original_string, string_to_add);
757 }
758
759 noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
760
761 /* walk across tree if necessary */
762 node = startnode;
763 char_to_add = string_to_add[0];
764 if (node->right != NULL && node->ch < char_to_add) {
765 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
766 char_to_add, node->ch, (node->right)->ch);
767 return (add_string_at_node(node->right, string_to_add));
768 }
769
770 /* walk down tree if appropriate */
771 if (node->down != NULL && node->ch == char_to_add) {
772 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
773 depth++;
774 res = add_string_at_node(node->down, string_to_add + 1);
775 depth--;
776 return (res);
777 }
778
779 if (char_to_add == '\0' && node->ch == '\0') {
780 log_msg(6, "%s already in tree", original_string);
781 return (1);
782 }
783
784 /* add here */
785 if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
786 log_to_screen("failed to malloc");
787 depth--;
788 return (1);
789 }
790 if (char_to_add < node->ch) // add to the left of node
791 {
792 log_msg(7, "depth=%d char=%c --- adding (left)", depth,
793 char_to_add);
794 memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
795 node->right = newnode;
796 } else if (char_to_add > node->ch) // add to the right of node
797 {
798 log_msg(7, "depth=%d char=%c --- adding (right)", depth,
799 char_to_add);
800 newnode->right = node->right; // newnode is to the RIGHT of node
801 node->right = newnode;
802 node = newnode;
803 }
804 // from now on, we're working on 'node'
805 node->down = NULL;
806 node->ch = char_to_add;
807 node->expanded = node->selected = FALSE;
808 if (char_to_add == '\0') {
809 log_msg(6, "Added %s OK", original_string);
810 return (0);
811 }
812// add the rest
813 log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
814 for (i = 1; i < noof_chars; i++) {
815 if (!
816 (node->down =
817 (struct s_node *) malloc(sizeof(struct s_node)))) {
818 log_to_screen("%s - failed to malloc", string_to_add);
819 return (1);
820 }
821 node = node->down;
822 char_to_add = string_to_add[i];
823 log_msg(6, "Adding '%c'", char_to_add);
824 node->ch = char_to_add;
825 node->right = node->down = NULL;
826 node->expanded = node->selected = FALSE;
827 if (!node->ch) {
828 node->selected = sosodef;
829 }
830 }
831 log_msg(6, "Finally - added %s OK", original_string);
832 return (0);
833}
834
835
836
837
838/**
839 * Load a filelist into a <tt>struct s_node</tt>.
840 * When you are done with the filelist, call free_filelist().
841 * @param filelist_fname The file to load the filelist from.
842 * @return A filelist tree structure.
843 */
844struct s_node *load_filelist(char *filelist_fname)
845{
846
847 /*@ structures ************************************************* */
848 struct s_node *filelist;
849
850 /*@ pointers *************************************************** */
851 FILE *pin;
852
853 /*@ buffers **************************************************** */
854 char *command_to_open_fname = NULL;
855 char *fname = NULL;
856 char *tmp = NULL;
857 char *tmp1 = NULL;
858 int pos_in_fname;
859 /*@ int ******************************************************** */
860 int percentage;
861
862 /*@ long ******************************************************* */
863 long lines_in_filelist;
864 long lino = 0;
865 /*@ end vars *************************************************** */
866
867 assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
868
869 if (!does_file_exist(filelist_fname)) {
870 fatal_error("filelist does not exist -- cannot load it");
871 }
872 log_to_screen("Loading filelist");
873 mr_asprintf(tmp, "zcat %s | wc -l", filelist_fname);
874 log_msg(6, "tmp = %s", tmp);
875
876 tmp1 = call_program_and_get_last_line_of_output(tmp,TRUE);
877 mr_free(tmp);
878
879 lines_in_filelist = atol(tmp1);
880 mr_free(tmp1);
881
882 if (lines_in_filelist < 3) {
883 log_to_screen("Warning - surprisingly short filelist.");
884 }
885 g_original_noof_lines_in_filelist = lines_in_filelist;
886 if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
887 return (NULL);
888 }
889 filelist->ch = '/';
890 filelist->right = NULL;
891 filelist->down = malloc(sizeof(struct s_node));
892 filelist->expanded = filelist->selected = FALSE;
893 (filelist->down)->ch = '\0';
894 (filelist->down)->right = (filelist->down)->down = FALSE;
895 (filelist->down)->expanded = (filelist->down)->selected = FALSE;
896
897 mr_asprintf(command_to_open_fname, "gzip -dc %s", filelist_fname);
898 if (!(pin = popen(command_to_open_fname, "r"))) {
899 log_OS_error("Unable to openin filelist_fname");
900 mr_free(command_to_open_fname);
901 return (NULL);
902 }
903 mr_free(command_to_open_fname);
904
905 open_evalcall_form("Loading filelist from disk");
906 for (mr_getline(fname, pin); !feof(pin); mr_getline(fname, pin)) {
907 if ((strlen(fname) > 0) && (fname[strlen(fname) - 1] == 13 || fname[strlen(fname) - 1] == 10)) {
908 fname[strlen(fname) - 1] = '\0';
909 }
910 if (!strlen(fname)) {
911 mr_free(fname);
912 continue;
913 }
914 for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
915 if (fname[pos_in_fname] != '/') {
916 continue;
917 }
918 mr_asprintf(tmp, "%s", fname);
919 tmp[pos_in_fname] = '\0';
920 if (strlen(tmp)) {
921 add_string_at_node(filelist, tmp);
922 }
923 mr_free(tmp);
924 }
925 add_string_at_node(filelist, fname);
926
927 if (!(++lino % 1111)) {
928 percentage = (int) (lino * 100 / lines_in_filelist);
929 update_evalcall_form(percentage);
930 }
931 mr_free(fname);
932 }
933 mr_free(fname);
934
935 paranoid_pclose(pin);
936 close_evalcall_form();
937 log_it("Finished loading filelist");
938 return (filelist);
939}
940
941
942/**
943 * Log a list of files in @p node.
944 * @param node The toplevel node to use.
945 */
946void show_filelist(struct s_node *node)
947{
948 static int depth = 0;
949 static char current_string[200];
950
951 if (depth == 0) {
952 log_msg(0, "----------------show filelist--------------");
953 }
954 current_string[depth] = node->ch;
955
956 log_msg(3, "depth=%d", depth);
957 if (node->down) {
958 log_msg(3, "moving down");
959 depth++;
960 show_filelist(node->down);
961 depth--;
962 }
963
964 if (!node->ch) {
965 log_msg(0, "%s\n", current_string);
966 }
967
968 if (node->right) {
969 log_msg(3, "moving right");
970 show_filelist(node->right);
971 }
972 if (depth == 0) {
973 log_msg(0, "----------------show filelist--------------");
974 }
975 return;
976}
977
978
979
980
981/**
982 * Reset the filelist to the state it was when it was loaded. This does not
983 * touch the file on disk.
984 * @param filelist The filelist tree structure.
985 */
986void reload_filelist(struct s_node *filelist)
987{
988 assert(filelist != NULL);
989 toggle_node_selection(filelist, FALSE);
990 toggle_path_expandability(filelist, "/", FALSE);
991 toggle_all_root_dirs_on(filelist);
992}
993
994
995
996/**
997 * Save a filelist tree structure to disk.
998 * @param filelist The filelist tree structure to save.
999 * @param outfname Where to save it.
1000 */
1001void save_filelist(struct s_node *filelist, char *outfname)
1002{
1003 /*@ int ********************************************************* */
1004 static int percentage;
1005 static int depth = 0;
1006
1007 /*@ buffers ***************************************************** */
1008 static char str[MAX_STR_LEN];
1009
1010 /*@ structures ************************************************** */
1011 struct s_node *node;
1012
1013 /*@ pointers **************************************************** */
1014 static FILE *fout = NULL;
1015
1016 /*@ long ******************************************************** */
1017 static long lines_in_filelist = 0;
1018 static long lino = 0;
1019
1020 /*@ end vars *************************************************** */
1021
1022 assert(filelist != NULL);
1023 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1024 if (depth == 0) {
1025 log_to_screen("Saving filelist");
1026 if (!(fout = fopen(outfname, "w"))) {
1027 fatal_error("Cannot openout/save filelist");
1028 }
1029 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
1030 open_evalcall_form("Saving selection to disk");
1031 }
1032 for (node = filelist; node != NULL; node = node->right) {
1033 str[depth] = node->ch;
1034 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1035 if (!node->ch) {
1036// if (node->selected)
1037// {
1038 fprintf(fout, "%s\n", str);
1039// }
1040 if (!(++lino % 1111)) {
1041 percentage = (int) (lino * 100 / lines_in_filelist);
1042 update_evalcall_form(percentage);
1043 }
1044 }
1045 if (node->down) {
1046 depth++;
1047 save_filelist(node->down, "");
1048 depth--;
1049 }
1050 }
1051 if (depth == 0) {
1052 paranoid_fclose(fout);
1053 close_evalcall_form();
1054 log_it("Finished saving filelist");
1055 }
1056}
1057
1058
1059
1060/**
1061 * Toggle all root dirs on.
1062 * @param filelist The filelist tree structure to operate on.
1063 * @bug I don't understand this function. Would someone care to explain it?
1064 */
1065void toggle_all_root_dirs_on(struct s_node *filelist)
1066{
1067 /*@ structures ************************************************** */
1068 struct s_node *node;
1069
1070 /*@ int ********************************************************* */
1071 static int depth = 0;
1072 static int root_dirs_expanded;
1073
1074 /*@ buffers ***************************************************** */
1075 static char filename[MAX_STR_LEN];
1076
1077 /*@ end vars *************************************************** */
1078
1079 assert(filelist != NULL);
1080 if (depth == 0) {
1081 log_it("Toggling all root dirs ON");
1082 root_dirs_expanded = 0;
1083 }
1084 for (node = filelist; node != NULL; node = node->right) {
1085 filename[depth] = node->ch;
1086 if (node->ch == '\0' && strlen(filename) > 1
1087 && (!strchr(filename + 1, '/'))) {
1088 node->selected = FALSE;
1089 node->expanded = TRUE;
1090// log_it (filename);
1091 root_dirs_expanded++;
1092 }
1093 if (node->down) {
1094 depth++;
1095 toggle_all_root_dirs_on(node->down);
1096 depth--;
1097 }
1098 }
1099 if (depth == 0) {
1100 log_it("Finished toggling all root dirs ON");
1101 }
1102}
1103
1104
1105/**
1106 * Toggle the expandability of a path.
1107 * @param filelist The filelist tree to operate on.
1108 * @param pathname The path to toggle expandability of.
1109 * @param on_or_off Whether to toggle it on or off.
1110 * @bug I don't understand this function. Would someone care to explain it?
1111 */
1112void
1113toggle_path_expandability(struct s_node *filelist, char *pathname,
1114 bool on_or_off)
1115{
1116
1117 /*@ int ******************************************************** */
1118 static int depth = 0;
1119 static int total_expanded;
1120 static int root_depth;
1121 int j;
1122 /*@ structures ************************************************* */
1123 struct s_node *node;
1124
1125 /*@ buffers **************************************************** */
1126 static char current_filename[MAX_STR_LEN];
1127
1128/* char tmp[MAX_STR_LEN+2]; */
1129
1130 /*@ end vars *************************************************** */
1131
1132 assert(filelist != NULL);
1133 assert_string_is_neither_NULL_nor_zerolength(pathname);
1134 if (depth == 0) {
1135 total_expanded = 0;
1136// log_it ("Toggling path's expandability");
1137 for (root_depth = (int) strlen(pathname);
1138 root_depth > 0 && pathname[root_depth - 1] != '/';
1139 root_depth--);
1140 if (root_depth < 2) {
1141 root_depth = (int) strlen(pathname);
1142 }
1143 }
1144 for (node = filelist; node != NULL; node = node->right) {
1145 current_filename[depth] = node->ch;
1146 if (node->down) {
1147 depth++;
1148 toggle_path_expandability(node->down, pathname, on_or_off);
1149 depth--;
1150 }
1151 if (node->ch == '\0') {
1152 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1153 for (j = root_depth;
1154 current_filename[j] != '/'
1155 && current_filename[j] != '\0'; j++);
1156 if (current_filename[j] != '\0') {
1157 for (j++;
1158 current_filename[j] != '/'
1159 && current_filename[j] != '\0'; j++);
1160 }
1161 if (current_filename[j] == '\0') {
1162 node->expanded =
1163 (!strcmp(pathname, current_filename) ? TRUE :
1164 on_or_off);
1165 }
1166 }
1167 }
1168 if (node->expanded) {
1169 if (total_expanded < ARBITRARY_MAXIMUM - 32
1170 || !strrchr(current_filename + strlen(pathname), '/')) {
1171 total_expanded++;
1172 } else {
1173 node->expanded = FALSE;
1174 }
1175 }
1176 }
1177 if (depth == 0) {
1178// log_it ("Finished toggling expandability");
1179 }
1180}
1181
1182/**
1183 * Toggle whether a path is selected.
1184 * @param filelist The filelist tree to operate on.
1185 * @param pathname The path to toggle selection of.
1186 * @param on_or_off Whether to toggle it on or off.
1187 * @bug I don't understand this function. Would someone care to explain it?
1188 */
1189void
1190toggle_path_selection(struct s_node *filelist, char *pathname,
1191 bool on_or_off)
1192{
1193 /*@ int ********************************************************* */
1194 static int depth = 0;
1195 int j;
1196
1197 /*@ structures ************************************************** */
1198 struct s_node *node;
1199
1200 /*@ buffers ***************************************************** */
1201 static char current_filename[MAX_STR_LEN];
1202
1203 /*@ end vars *************************************************** */
1204 assert(filelist != NULL);
1205 assert_string_is_neither_NULL_nor_zerolength(pathname);
1206 if (depth == 0) {
1207 log_it("Toggling path's selection");
1208 }
1209 for (node = filelist; node != NULL; node = node->right) {
1210 current_filename[depth] = node->ch;
1211 if (node->down) {
1212 depth++;
1213 toggle_path_selection(node->down, pathname, on_or_off);
1214 depth--;
1215 }
1216 if (node->ch == '\0') {
1217 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1218 for (j = 0;
1219 pathname[j] != '\0'
1220 && pathname[j] == current_filename[j]; j++);
1221 if (current_filename[j] == '/'
1222 || current_filename[j] == '\0') {
1223 node->selected = on_or_off;
1224 }
1225 }
1226 }
1227 }
1228 if (depth == 0) {
1229 log_it("Finished toggling selection");
1230 }
1231}
1232
1233
1234/**
1235 * Toggle node selection of a filelist tree.
1236 * @param filelist The filelist tree to operate on.
1237 * @param on_or_off Whether to toggle selection on or off.
1238 * @bug I don't understand this function. Would someone care to explain it?
1239 */
1240void toggle_node_selection(struct s_node *filelist, bool on_or_off)
1241{
1242 /*@ structure ************************************************** */
1243 struct s_node *node;
1244
1245 /*@ end vars *************************************************** */
1246 assert(filelist != NULL);
1247 for (node = filelist; node != NULL; node = node->right) {
1248 if (node->ch == '/') {
1249 continue;
1250 } /* don't go deep */
1251 if (node->ch == '\0') {
1252 node->selected = on_or_off;
1253 }
1254 if (node->down) {
1255 toggle_node_selection(node->down, on_or_off);
1256 }
1257 }
1258}
1259
1260
1261
1262/**
1263 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1264 */
1265char *g_skeleton_filelist = NULL;
1266
1267
1268
1269/**
1270 * Get the next entry in the space-separated list in @p incoming.
1271 * So if @p incoming was '"one and two" three four', we would
1272 * return "one and two".
1273 * @param incoming The list to get the next entry from.
1274 * @return The first item in the list (respecting double quotes).
1275 * @note The returned string points to static data that will be overwritten with each call.
1276 */
1277char *next_entry(char *incoming)
1278{
1279 char *sz_res;
1280 char *p;
1281 bool in_quotes = FALSE;
1282
1283 mr_asprintf(sz_res, "%s", incoming);
1284 p = sz_res;
1285 while ((*p != ' ' || in_quotes) && *p != '\0') {
1286 if (*p == '\"') {
1287 in_quotes = !in_quotes;
1288 }
1289 p++;
1290 }
1291 *p = '\0';
1292 return (sz_res);
1293}
1294
1295
1296/**
1297 * Number of entries in the skeleton filelist.
1298 */
1299long g_skeleton_entries = 0;
1300
1301
1302/**
1303 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1304 * @param dir The directory to list to @p fout.
1305 * @param sth The directories to skip (exclude).
1306 * @param fout The file to write everything to.
1307 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1308 * @return 0, always.
1309 * @bug Return value should be @c void.
1310 */
1311int open_and_list_dir(char *dir1, char *sth, FILE * fout, time_t time_of_last_full_backup) {
1312
1313 const char delims[] = " ";
1314
1315 DIR *dip;
1316 struct dirent *dit;
1317 struct stat statbuf;
1318 char *new;
1319 char *tmp = NULL;
1320 char *tmp1 = NULL;
1321 char *dir = NULL;
1322 static int percentage = 0;
1323 char *skip_these = NULL;
1324 char *new_with_spaces = NULL;
1325 char *strtmp;
1326 char *token;
1327 char *find_excludes = NULL;
1328 char *name_of_evalcall_form = NULL;
1329 char *find_skeleton_marker = NULL;
1330 int i;
1331 int lastpos = 0;
1332 static int depth = 0;
1333 char *p;
1334 static int counter = 0;
1335 static int uberctr = 0;
1336 static long skeleton_lino = 0L;
1337 static time_t last_time = (time_t)0;
1338 time_t this_time;
1339
1340 /* dir is needed when we pass it to the shell */
1341 dir = mr_stresc(dir1, "`$\\\"(){}'[]&*?|!#~", '\\');
1342 p = strrchr(dir1, '/');
1343 if (p) {
1344 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
1345 mr_free(dir);
1346 return (0);
1347 }
1348 }
1349
1350 if (!depth) {
1351 mr_asprintf(find_excludes, " ");
1352 if (sth != NULL) {
1353 while((token = mr_strtok(sth, delims, &lastpos))) {
1354 mr_strcat(find_excludes," -path %s -prune -o", token);
1355 mr_free(token);
1356 }
1357 }
1358
1359/**
1360 * The maximum depth of directories to put in the skeleton filelist.
1361 * This is a balance between performance and a good progress indicator.
1362 */
1363#define MAX_SKEL_DEPTH 3
1364
1365#if linux
1366 // 2.6 has /sys as a proc-type thing -- must be excluded
1367 mr_asprintf(strtmp, "find '%s' -maxdepth %d -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 -fstype none -prune -o %s -type d -print > %s 2> /dev/null", dir, MAX_SKEL_DEPTH, 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' -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);
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,TRUE);
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 if (depth <= MAX_SKEL_DEPTH) // 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",TRUE);
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}'",TRUE);
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,TRUE);
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.