source: MondoRescue/trunk/mondo/mondo/common/libmondo-filelist.c@ 87

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