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

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

r3272@localhost: bruno | 2009-07-18 01:37:19 +0200
Formating

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