source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-filelist.c@ 2279

Last change on this file since 2279 was 2279, checked in by Bruno Cornec, 15 years ago

r3272@localhost: bruno | 2009-07-18 01:37:19 +0200
Formating

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