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

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

r3342@localhost: bruno | 2009-08-14 00:46:51 +0200

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