source: MondoRescue/branches/stable/mondo/mondo/common/libmondo-filelist.c@ 501

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