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

Last change on this file since 2475 was 2475, checked in by Bruno Cornec, 14 years ago
  • Update deplist for Debian support
  • Fix #363 where exclude_paths was extended up to memory limit dumping core
  • Default to DVD size when in iso or netfs mode
  • Improve analysis of kernel modules by printing whether it's a live or extra module which has not been found (Matthew Cline) as reported in #362
  • Remove useless pb_log and MONDO_TRACEFILE
  • /dev/shm is now part of the default exclude list
  • some distro hold lvm commands under /usr/sbin
  • bzip2 is under /bin on Debian

(merge from 2.2.9 branch)

  • Property svn:keywords set to Id
File size: 48.8 KB
Line 
1/*
2 $Id: libmondo-filelist.c 2475 2009-11-10 13:52:43Z 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 2475 2009-11-10 13:52:43Z 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
1262
1263
1264/**
1265 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1266 */
1267char *g_skeleton_filelist = NULL;
1268
1269/**
1270 * Number of entries in the skeleton filelist.
1271 */
1272long g_skeleton_entries = 0;
1273
1274
1275/**
1276 * Get the next entry in the space-separated list in @p incoming.
1277 * So if @p incoming was '"one and two" three four', we would
1278 * return "one and two".
1279 * @param incoming The list to get the next entry from.
1280 * @return The first item in the list (respecting double quotes).
1281 * @note The returned string points to static data that will be overwritten with each call.
1282 */
1283char *next_entry(char *incoming)
1284{
1285 char *sz_res;
1286 char *p;
1287 bool in_quotes = FALSE;
1288
1289 mr_asprintf(sz_res, "%s", incoming);
1290 p = sz_res;
1291 while ((*p != ' ' || in_quotes) && *p != '\0') {
1292 if (*p == '\"') {
1293 in_quotes = !in_quotes;
1294 }
1295 p++;
1296 }
1297 *p = '\0';
1298 return (sz_res);
1299}
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#if linux
1359 // 2.6 has /sys as a proc-type thing -- must be excluded
1360 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);
1361#else
1362 // On BSD, for example, /sys is the kernel sources -- don't exclude
1363 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);
1364#endif
1365 mr_free(find_excludes);
1366
1367 log_msg(5, "find command = %s", strtmp);
1368 (void)system(strtmp);
1369 mr_free(strtmp);
1370
1371 mr_asprintf(tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
1372 tmp1 = call_program_and_get_last_line_of_output(tmp);
1373 mr_free(tmp);
1374
1375 g_skeleton_entries = 1 + atol(tmp1);
1376 mr_free(tmp1);
1377
1378 mr_asprintf(name_of_evalcall_form, "Making catalog of %s", dir1);
1379 open_evalcall_form(name_of_evalcall_form);
1380 mr_free(name_of_evalcall_form);
1381
1382 skeleton_lino = 1;
1383 log_msg(5, "entries = %ld", g_skeleton_entries);
1384 percentage = 0;
1385 } else // update evalcall form if appropriate
1386 {
1387 mr_asprintf(find_skeleton_marker, "grep -Fv '%s' %s > %s.new 2> /dev/null", dir, g_skeleton_filelist, g_skeleton_filelist);
1388 if (!system(find_skeleton_marker)) {
1389 percentage = (int) (skeleton_lino * 100 / g_skeleton_entries);
1390 skeleton_lino++;
1391 mr_free(find_skeleton_marker);
1392
1393 mr_asprintf(find_skeleton_marker, "mv -f %s.new %s", g_skeleton_filelist, g_skeleton_filelist);
1394 (void)system(find_skeleton_marker);
1395 time(&this_time);
1396 if (this_time != last_time) {
1397 last_time = this_time;
1398 if (!g_text_mode) {
1399 mr_asprintf(tmp, "Reading %-68s", dir1);
1400 newtDrawRootText(0, g_noof_rows - 3, tmp);
1401 mr_free(tmp);
1402 }
1403 update_evalcall_form(percentage);
1404 }
1405 }
1406 mr_free(find_skeleton_marker);
1407 }
1408
1409 depth++;
1410
1411// log_msg(0, "Cataloguing %s", dir);
1412 if (sth[0] == ' ') {
1413 mr_asprintf(skip_these, "%s", sth);
1414 } else {
1415 mr_asprintf(skip_these, " %s ", sth);
1416 }
1417 mr_asprintf(new_with_spaces, " %s ", dir1);
1418 if ((dip = opendir(dir1)) == NULL) {
1419 mr_asprintf(tmp,"opendir %s", dir1);
1420 log_OS_error(tmp);
1421 mr_free(tmp);
1422 } else if (strstr(skip_these, new_with_spaces)) {
1423 log_msg(10, "Found dir ***%s**** excluded", dir1);
1424 fprintf(fout, "%s\n", dir1); // if excluded dir then print dir ONLY
1425 } else {
1426 log_msg(10, "Found dir ***%s**** parsed", dir1);
1427 fprintf(fout, "%s\n", dir1);
1428 while ((dit = readdir(dip)) != NULL) {
1429 i++;
1430 if (strcmp(dir1, "/")) {
1431 mr_asprintf(new,"%s/%s",dir1,dit->d_name);
1432 } else {
1433 mr_asprintf(new,"%s%s",dir1,dit->d_name);
1434 }
1435 mr_free(new_with_spaces);
1436 mr_asprintf(new_with_spaces, " %s ", new);
1437 if (strstr(skip_these, new_with_spaces)) {
1438 fprintf(fout, "%s\n", new);
1439 log_msg(10, "Found child dir ***%s**** excluded", new);
1440 mr_free(new_with_spaces);
1441 } else {
1442 mr_free(new_with_spaces);
1443 if (!lstat(new, &statbuf)) {
1444 if (!S_ISLNK(statbuf.st_mode)
1445 && S_ISDIR(statbuf.st_mode)) {
1446 log_msg(10, "Found child dir ***%s**** parsed", new);
1447 open_and_list_dir(new, skip_these, fout, time_of_last_full_backup);
1448 } else {
1449 if (time_of_last_full_backup == 0 || time_of_last_full_backup < statbuf.st_ctime) {
1450 log_msg(10, "Found child file ***%s**** parsed", new);
1451 fprintf(fout, "%s\n", new);
1452 if ((counter++) > 128) {
1453 counter = 0;
1454 uberctr++;
1455 mr_asprintf(tmp, " %c ", special_dot_char(uberctr));
1456 if (!g_text_mode) {
1457 newtDrawRootText(77, g_noof_rows - 3, tmp);
1458 newtRefresh();
1459 }
1460 mr_free(tmp);
1461 }
1462 }
1463 }
1464 }
1465 }
1466 mr_free(new);
1467 }
1468 }
1469 mr_free(new_with_spaces);
1470 mr_free(skip_these);
1471 mr_free(dir);
1472
1473 if (dip) {
1474 if (closedir(dip) == -1) {
1475 log_OS_error("closedir");
1476 }
1477 }
1478 depth--;
1479 if (!depth) {
1480 close_evalcall_form();
1481 unlink(g_skeleton_filelist);
1482 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1483 }
1484 return (0);
1485}
1486
1487
1488
1489
1490/**
1491 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1492 * @param logfile Unused.
1493 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1494 * @param excp The paths to NOT back up.
1495 * @param differential The differential level (currently only 0 and 1 are supported).
1496 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1497 * @return 0, always.
1498 * @bug @p logfile is unused.
1499 * @bug Return value is meaningless.
1500 */
1501int mondo_makefilelist(char *logfile, char *include_paths, char *excp, int differential, char *userdef_filelist)
1502{
1503 char *p, *q;
1504 char *sz_datefile;
1505 char *sz_filelist;
1506 char *exclude_paths = NULL;
1507 int i;
1508 FILE *fout;
1509 char *command = NULL;
1510 time_t time_of_last_full_backup = 0;
1511 struct stat statbuf;
1512 char *tmp1 = NULL;
1513 char *tmp2 = NULL;
1514
1515 mr_asprintf(sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
1516 if (!include_paths && !userdef_filelist) {
1517 fatal_error("Please supply either include_paths or userdef_filelist");
1518 }
1519 // make hole for filelist
1520 mr_asprintf(command, "mkdir -p %s/archives", bkpinfo->scratchdir);
1521 paranoid_system(command);
1522 mr_free(command);
1523
1524 mr_asprintf(sz_filelist, "%s/tmpfs/filelist.full", bkpinfo->tmpdir);
1525 make_hole_for_file(sz_filelist);
1526
1527 if (differential == 0) {
1528 // restore last good datefile if it exists
1529 mr_asprintf(command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
1530 run_program_and_log_output(command, 3);
1531 mr_free(command);
1532
1533 // backup last known good datefile just in case :)
1534 if (does_file_exist(sz_datefile)) {
1535 mr_asprintf(command, "mv -f %s %s.aborted", sz_datefile, sz_datefile);
1536 paranoid_system(command);
1537 mr_free(command);
1538 }
1539 make_hole_for_file(sz_datefile);
1540 tmp1 = call_program_and_get_last_line_of_output("date +%s");
1541 write_one_liner_data_file(sz_datefile, tmp1);
1542 mr_free(tmp1);
1543 } else if (lstat(sz_datefile, &statbuf)) {
1544 log_msg(2, "Warning - unable to find date of previous backup. Full backup instead.");
1545 differential = 0;
1546 time_of_last_full_backup = 0;
1547 } else {
1548 time_of_last_full_backup = statbuf.st_mtime;
1549 log_msg(2, "Differential backup. Yay.");
1550 }
1551 paranoid_free(sz_datefile);
1552
1553// use user-specified filelist (if specified)
1554 if (userdef_filelist) {
1555 log_msg(1, "Using the user-specified filelist - %s - instead of calculating one", userdef_filelist);
1556 mr_asprintf(command, "cp -f %s %s", userdef_filelist, sz_filelist);
1557 if (run_program_and_log_output(command, 3)) {
1558 mr_free(command);
1559 fatal_error("Failed to copy user-specified filelist");
1560 }
1561 mr_free(command);
1562 } else {
1563 if (include_paths) {
1564 log_msg(2, "include_paths = '%s'", include_paths);
1565 }
1566 log_msg(1, "Calculating filelist");
1567 tmp2 = call_program_and_get_last_line_of_output("mount | grep -Ew 'ntfs|ntfs-3g|fat|vfat|dos' | awk '{print $3}'");
1568 if (strlen(tmp2) < 1) {
1569 mr_asprintf(tmp1," ");
1570 } else {
1571 log_msg(2, "Found windows FS: %s",tmp2);
1572 mr_asprintf(tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
1573 mr_free(tmp2);
1574 tmp2 = call_program_and_get_last_line_of_output(tmp1);
1575 log_msg(2, "Found windows files: %s",tmp2);
1576 }
1577 mr_free(tmp1);
1578
1579 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);
1580 mr_free(tmp2);
1581
1582 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1583 log_msg(2, "Generating skeleton filelist so that we can track our progress");
1584 mr_asprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", bkpinfo->tmpdir);
1585 make_hole_for_file(g_skeleton_filelist);
1586 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1587 log_msg(2, "Opening out filelist to %s", sz_filelist);
1588 if (!(fout = fopen(sz_filelist, "w"))) {
1589 fatal_error("Cannot openout to sz_filelist");
1590 }
1591 i = 0;
1592 if ((!include_paths) || (strlen(include_paths) == 0)) {
1593 log_msg(1, "Including only '/' in %s", sz_filelist);
1594 open_and_list_dir("/", exclude_paths, fout,
1595 time_of_last_full_backup);
1596 } else {
1597 p = include_paths;
1598 while (*p) {
1599 q = next_entry(p);
1600 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1601 open_and_list_dir(q, exclude_paths, fout,
1602 time_of_last_full_backup);
1603 p += strlen(q);
1604 paranoid_free(q);
1605 while (*p == ' ') {
1606 p++;
1607 }
1608 }
1609 }
1610 mr_free(exclude_paths);
1611 paranoid_fclose(fout);
1612 }
1613 log_msg(2, "Copying new filelist to scratchdir");
1614 mr_asprintf(command, "mkdir -p %s/archives", bkpinfo->scratchdir);
1615 paranoid_system(command);
1616 mr_free(command);
1617
1618 mr_asprintf(command, "cp -f %s %s/archives/", sz_filelist, bkpinfo->scratchdir);
1619 paranoid_system(command);
1620 mr_free(command);
1621
1622 mr_asprintf(command, "mv -f %s %s", sz_filelist, bkpinfo->tmpdir);
1623 paranoid_system(command);
1624 mr_free(command);
1625
1626 paranoid_free(sz_filelist);
1627 log_msg(2, "Freeing variables");
1628 mr_free(g_skeleton_filelist);
1629 log_msg(2, "Exiting");
1630 return (0);
1631}
1632
1633
1634
1635/**
1636 * Wrapper around mondo_makefilelist().
1637 * @param bkpinfo The backup information structure. Fields used:
1638 * - @c bkpinfo->differential
1639 * - @c bkpinfo->exclude_paths
1640 * - @c bkpinfo->include_paths
1641 * - @c bkpinfo->make_filelist
1642 * - @c bkpinfo->scratchdir
1643 * - @c bkpinfo->tmpdir
1644 * @return 0 for success, nonzero for failure.
1645 * @see mondo_makefilelist
1646 */
1647int prepare_filelist()
1648{
1649
1650 /*@ int **************************************************** */
1651 int res = 0;
1652
1653 assert(bkpinfo != NULL);
1654 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir, bkpinfo->scratchdir);
1655 if (bkpinfo->make_filelist) {
1656 mvaddstr_and_log_it(g_currentY, 0,
1657 "Making catalog of files to be backed up");
1658 } else {
1659 mvaddstr_and_log_it(g_currentY, 0,
1660 "Using supplied catalog of files to be backed up");
1661 }
1662
1663 if (bkpinfo->make_filelist) {
1664 res =
1665 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->include_paths, bkpinfo->exclude_paths, bkpinfo->differential, NULL);
1666 } else {
1667 res =
1668 mondo_makefilelist(MONDO_LOGFILE, NULL, bkpinfo->exclude_paths, bkpinfo->differential, bkpinfo->include_paths);
1669 }
1670
1671 if (res) {
1672 log_OS_error("Call to mondo_makefilelist failed");
1673 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
1674 } else {
1675 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
1676 }
1677 return (res);
1678}
1679
1680
1681/**
1682 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1683 * @param startnode The node containing the root of the directory tree.
1684 * @param string_to_find The string to look for at @p startnode.
1685 * @return The node containing the last element of @p string_to_find, or NULL if
1686 * it was not found.
1687 */
1688struct s_node *find_string_at_node(struct s_node *startnode,
1689 char *string_to_find)
1690{
1691 /*@ int ******************************************************** */
1692 int noof_chars;
1693 static int depth = 0;
1694 static char original_string[MAX_STR_LEN];
1695
1696 /*@ sturctures ************************************************* */
1697 struct s_node *node;
1698
1699 /*@ char ****************************************************** */
1700 char char_to_find;
1701
1702 /*@ bools ****************************************************** */
1703
1704 if (!depth) {
1705 strcpy(original_string, string_to_find);
1706 }
1707
1708 assert(startnode != NULL);
1709 assert(string_to_find != NULL);
1710
1711 noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
1712
1713 log_msg(7, "starting --- str=%s", string_to_find);
1714
1715/* walk across tree if necessary */
1716 node = startnode;
1717 char_to_find = string_to_find[0];
1718 if (node->right != NULL && node->ch < char_to_find) {
1719 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1720 char_to_find, node->ch, (node->right)->ch);
1721 return (find_string_at_node(node->right, string_to_find));
1722 }
1723
1724/* walk down tree if appropriate */
1725 if (node->down != NULL && node->ch == char_to_find) {
1726 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1727 depth++;
1728 node = find_string_at_node(node->down, string_to_find + 1);
1729 depth--;
1730 return (node);
1731 }
1732
1733 if (char_to_find == '\0' && node->ch == '\0') {
1734 log_msg(7, "%s is in tree", original_string);
1735 return (node);
1736 } else {
1737 log_msg(7, "%s is NOT in tree", original_string);
1738 return (NULL);
1739 }
1740}
1741
1742
1743
1744/**
1745 * Write all entries in @p needles_list_fname which are also in
1746 * @p filelist to @p matches_list_fname.
1747 * @param needles_list_fname A file containing strings to look for, 1 per line.
1748 * @param filelist The node for the root of the directory structure to search in.
1749 * @param matches_list_fname The filename where we should put the matches.
1750 * @return The number of matches found.
1751 */
1752long save_filelist_entries_in_common(char *needles_list_fname,
1753 struct s_node *filelist,
1754 char *matches_list_fname,
1755 bool use_star)
1756{
1757 int retval = 0;
1758 struct s_node *found_node;
1759 FILE *fin;
1760 FILE *fout;
1761 char *fname = NULL;
1762 char *tmp = NULL;
1763
1764 log_msg(5, "starting");
1765 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1766 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1767 if (!(fin = fopen(needles_list_fname, "r"))) {
1768 fatal_error("Cannot openin needles_list_fname");
1769 }
1770 if (!(fout = fopen(matches_list_fname, "w"))) {
1771 fatal_error("Cannot openout matches_list_fname");
1772 }
1773 while (!feof(fin)) {
1774 mr_getline(fname, fin);
1775 if (!use_star) {
1776 if (fname[0] == '/') {
1777 mr_asprintf(tmp, "%s", fname);
1778 } else {
1779 mr_asprintf(tmp, "/%s", fname);
1780 }
1781 mr_free(fname);
1782 fname = tmp;
1783 }
1784 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1785 fname[strlen(fname) - 1] = '\0';
1786 }
1787
1788 log_msg(5, "Looking for '%s'", fname);
1789 found_node = find_string_at_node(filelist, fname);
1790 if (found_node) {
1791 if (found_node->selected) {
1792 if (fname[0] == '/') {
1793 mr_asprintf(tmp, "%s", fname + 1);
1794 mr_free(fname);
1795 fname = tmp;
1796 }
1797 log_msg(5, "Found '%s'", fname);
1798 tmp = mr_stresc(fname, "[]*?", '\\');
1799 mr_free(fname);
1800 fname = tmp;
1801 fprintf(fout, "%s\n", fname);
1802 retval++;
1803 }
1804 }
1805 mr_free(fname);
1806 }
1807 paranoid_fclose(fout);
1808 paranoid_fclose(fin);
1809 return (retval);
1810}
1811
1812
1813
1814/**
1815 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1816 * @p filelist.
1817 * @param filelist The top node of the directory structure to add the files to.
1818 * @param list_of_files_fname The file containing the files to add, 1 per line.
1819 * @param flag_em If TRUE, then flag the added files for restoration.
1820 * @return 0 for success, nonzero for failure.
1821 */
1822int add_list_of_files_to_filelist(struct s_node *filelist,
1823 char *list_of_files_fname, bool flag_em)
1824{
1825 FILE *fin = NULL;
1826 char *tmp = NULL;
1827 struct s_node *nod = NULL;
1828
1829 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1830 if (!(fin = fopen(list_of_files_fname, "r"))) {
1831 log_it("%s",list_of_files_fname);
1832 return (1);
1833 }
1834 for (mr_getline(tmp, fin); !feof(fin); mr_getline(tmp, fin)) {
1835 if (!tmp[0]) {
1836 mr_free(tmp);
1837 continue;
1838 }
1839 if ((strlen(tmp) > 0) && (tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)) {
1840 tmp[strlen(tmp) - 1] = '\0';
1841 }
1842 log_msg(2, "tmp = '%s'", tmp);
1843 if (!tmp[0]) {
1844 mr_free(tmp);
1845 continue;
1846 }
1847 if ((nod = find_string_at_node(filelist, tmp))) {
1848 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1849 } else {
1850 add_string_at_node(filelist, tmp);
1851 nod = find_string_at_node(filelist, tmp);
1852 }
1853
1854 if (nod && flag_em) {
1855 toggle_path_selection(filelist, tmp, TRUE);
1856 log_msg(5, "Flagged '%s'", tmp);
1857 }
1858 mr_free(tmp);
1859 }
1860 mr_free(tmp);
1861 paranoid_fclose(fin);
1862 return (0);
1863}
1864
1865/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.