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

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

Fix a bug in libmondo-fifo.c where potentially no buffer content could let mondo running forever in case of an exception

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