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

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

r3728@localhost: bruno | 2010-01-05 23:43:31 +0100

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