source: MondoRescue/branches/stable/mondo/src/common/libmondo-filelist.c@ 1548

Last change on this file since 1548 was 1548, checked in by Bruno Cornec, 17 years ago
  • Add the possibiilty to edit in interactive mode mtab and device.map for grub
  • Remove blkid cache files after restore to avoid problems in cloning mode
  • Fix what seems to appear a huge number of bugs in hack-fstab (illustration of 1 LOC = 1 bug :-)
  • Especially improve LABEL and UUID support.
  • Should fix #185
  • Exclude_path should be 4*MAX_STR_LEN everywhere. Fixed now.
  • Increasing that value will allow to having larger exclude paths.
  • Should solve bug #137 (and maybe #3 as well)
  • Adds support for fedora 7

(merge -r 1533:1547 $SVN_M/branches/2.2.5)

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