source: MondoRescue/branches/3.0/mondo/src/common/libmondo-filelist.c@ 3139

Last change on this file since 3139 was 3139, checked in by Bruno Cornec, 11 years ago

r5341@localhost: bruno | 2013-06-13 03:02:29 +0200

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