source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-filelist.c@ 1644

Last change on this file since 1644 was 1644, checked in by Bruno Cornec, 17 years ago

Apply patch from Andree Leidenfrost, modified a bit to use bkpinfo->tmpdir instead of /tmp
or MINDI_CACHE when appropriate. Fix security issues in mondo
Thanks al ot Andree for catching all those issues.
Will not compile needs more work as bkpinfo->tmpdir isn't available everywhere
Should become a global in 3.x when only containing pointers.

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