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

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