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

Last change on this file since 2323 was 2323, checked in by Bruno Cornec, 15 years ago

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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