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

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