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

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

r3289@localhost: bruno | 2009-07-21 15:12:29 +0200

  • Replace sprintf by mr_asprintf in mondorestore.c
  • Remove usless option -pih in mondorestore
  • Property svn:keywords set to Id
File size: 50.4 KB
RevLine 
[2030]1/*
[128]2 $Id: libmondo-filelist.c 2287 2009-07-21 13:26:16Z 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 2287 2009-07-21 13:26:16Z 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
[2287]545 log_msg(1, "set_EXAT_list(%s, %s, %s)", orig_msklist, original_exat_fname, executable);
[128]546 if (!orig_msklist || !orig_msklist[0]
547 || !does_file_exist(orig_msklist)) {
[2265]548 log_msg(1, "No masklist provided. I shall therefore set ALL attributes.");
549 mr_asprintf(&command, "gzip -dc %s | %s --restore - 2>> %s", original_exat_fname, executable, MONDO_LOGFILE);
[128]550 log_msg(1, "command = %s", command);
551 retval = system(command);
[2265]552 mr_free(command);
[128]553 log_msg(1, "Returning w/ retval=%d", retval);
554 return (retval);
555 }
556 if (length_of_file(original_exat_fname) <= 0) {
[2287]557 log_msg(1, "original_exat_fname %s is empty or missing, so no need to set EXAT list", original_exat_fname);
[128]558 return (0);
559 }
560 malloc_string(incoming);
561 malloc_string(current_subset_file);
562 malloc_string(current_master_file);
[2265]563 mr_asprintf(&masklist, "%s/masklist", bkpinfo->tmpdir);
564 mr_asprintf(&command, "cp -f %s %s", orig_msklist, masklist);
[128]565 run_program_and_log_output(command, 1);
[2265]566 mr_free(command);
567
[128]568 sort_file(masklist);
569 current_subset_file[0] = current_master_file[0] = '\0';
[1]570
[2265]571 mr_asprintf(&syscall_pout, "%s --restore - 2>> %s", executable, MONDO_LOGFILE);
[128]572 log_msg(1, "syscall_pout = %s", syscall_pout);
573 pout = popen(syscall_pout, "w");
[2265]574 mr_free(syscall_pout);
575
[128]576 if (!pout) {
[2230]577 log_it("Unable to openout to syscall_pout");
[2265]578 mr_free(masklist);
[128]579 return (1);
580 }
[2265]581
582 mr_asprintf(&syscall_pin, "gzip -dc %s", original_exat_fname);
583 log_msg(1, "syscall_pin = %s", syscall_pin);
[128]584 pin = popen(syscall_pin, "r");
[2265]585 mr_free(syscall_pin);
586
[128]587 if (!pin) {
588 pclose(pout);
[2230]589 log_it("Unable to openin from syscall");
[128]590 return (1);
591 }
592 faclin = fopen(masklist, "r");
593 if (!faclin) {
594 pclose(pin);
595 pclose(pout);
[2230]596 log_it("Unable to openin masklist");
[2265]597 mr_free(masklist);
[128]598 return (1);
599 }
600// printf("Hi there. Starting the loop\n");
[1]601
[2202]602 (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
603 (void)fgets(incoming, MAX_STR_LEN, pin);
[128]604 while (!feof(pin) && !feof(faclin)) {
[1]605// printf("incoming = %s", incoming);
606
[128]607 strcpy(current_master_file, incoming + 8);
[1]608
[128]609 p = current_subset_file;
610 if (*p == '/') {
611 p++;
612 }
613 i = strlen(p);
614 if (i > 0 && p[i - 1] < 32) {
615 p[i - 1] = '\0';
616 }
617
618
619 q = current_master_file;
620 if (*q == '/') {
621 q++;
622 }
623 i = strlen(q);
624 if (i > 0 && q[i - 1] < 32) {
625 q[i - 1] = '\0';
626 }
627
628 i = strcmp(p, q);
629 log_msg(my_depth, "'%s' v '%s' --> %d\n", p, q, i);
630
[1]631// printf("%s v %s --> %d\n", p, q, i);
[128]632
633 if (i < 0) { // read another subset file in.
634 log_msg(my_depth, "Reading next subset line in\n\n");
[2202]635 (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
[128]636 continue;
637 }
638
639 if (!i) {
640 fputs(incoming, pout);
641 }
[2202]642 (void)fgets(incoming, MAX_STR_LEN, pin);
[128]643 if (!i) {
644 log_msg(my_depth, "Copying master %s", q);
645 }
646// if (!i) { printf("Match --- %s\n", q); }
647
648 while (!feof(pin) && strncmp(incoming, "# file: ", 8)) {
649 if (!i) {
650
651// printf("%s", incoming);
652
653 fputs(incoming, pout);
654 }
[2202]655 (void)fgets(incoming, MAX_STR_LEN, pin);
[128]656 }
657 if (!i) {
[2202]658 (void)fgets(current_subset_file, MAX_STR_LEN, faclin);
[128]659 }
[1]660 }
[128]661 while (!feof(pin)) {
[2202]662 (void)fgets(incoming, MAX_STR_LEN, pin);
[128]663 }
664 fclose(faclin);
665 pclose(pin);
666 pclose(pout);
[1]667
668// printf("OK, loop is done\n");
669
[128]670 unlink(masklist);
[2265]671 mr_free(masklist);
672
[128]673 paranoid_free(current_subset_file);
674 paranoid_free(current_master_file);
675 paranoid_free(incoming);
676 return (retval);
677}
[1]678
679
[128]680int set_fattr_list(char *masklist, char *fattr_fname)
[1]681{
[912]682 if (find_home_of_exe("setfattr")) {
683 return (set_EXAT_list(masklist, fattr_fname, "setfattr"));
684 } else {
685 log_msg(1, "ERROR: set_EXAT_list: setfattr doesn't exist");
686 return(0);
687 }
[1]688}
689
690
691
[128]692int set_acl_list(char *masklist, char *acl_fname)
[1]693{
[912]694 if (find_home_of_exe("setfacl")) {
695 return (set_EXAT_list(masklist, acl_fname, "setfacl"));
696 } else {
697 log_msg(1, "ERROR: set_EXAT_list: setfacl doesn't exist");
698 return(0);
699 }
[1]700}
701
702
703/**
704 * Get the number of the last fileset in the backup.
705 * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
706 * @return The last filelist number.
707 * @note This function should only be called at restore-time.
708 */
[1645]709int get_last_filelist_number()
[1]710{
[128]711 /*@ buffers ***************************************************** */
712 char val_sz[MAX_STR_LEN];
[2265]713 char *cfg_fname = NULL;
[1]714
[128]715 /*@ long ******************************************************** */
716 int val_i;
[1]717
[128]718 /*@ end vars **************************************************** */
[1]719
[128]720 assert(bkpinfo != NULL);
[1]721
[1552]722 strcpy(val_sz,"");
[2265]723 mr_asprintf(&cfg_fname, "%s/mondo-restore.cfg", bkpinfo->tmpdir);
[128]724 read_cfg_var(cfg_fname, "last-filelist-number", val_sz);
[2265]725 mr_free(cfg_fname);
726
[128]727 val_i = atoi(val_sz);
728 if (val_i <= 0) {
729 val_i = 500;
730 }
731 return (val_i);
[1]732}
733
734
735/**
736 * Add a string at @p startnode.
737 * @param startnode The node to start at when searching for where to add the string.
738 * @param string_to_add The string to add.
739 * @return 0 for success, 1 for failure.
740 * @bug I don't understand this function. Would someone care to explain it?
741 */
[128]742int add_string_at_node(struct s_node *startnode, char *string_to_add)
[1]743{
744
745
[128]746 /*@ int ******************************************************** */
747 int noof_chars;
748 int i;
749 int res;
[1]750
[128]751 /*@ sturctures ************************************************* */
752 struct s_node *node, *newnode;
[1]753
[128]754 /*@ char ****************************************************** */
755 char char_to_add;
[1]756
[128]757 /*@ bools ****************************************************** */
[1]758
[128]759 const bool sosodef = FALSE;
[1]760
[128]761 static int depth = 0;
762 static char original_string[MAX_STR_LEN];
[1]763
[128]764 assert(startnode != NULL);
765 assert(string_to_add != NULL);
[1]766
[128]767 if (!depth) {
768 strcpy(original_string, string_to_add);
769 }
[1]770
[128]771 noof_chars = strlen(string_to_add) + 1; /* we include the '\0' */
[1]772
773/* walk across tree if necessary */
[128]774 node = startnode;
775 char_to_add = string_to_add[0];
776 if (node->right != NULL && node->ch < char_to_add) {
777 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
778 char_to_add, node->ch, (node->right)->ch);
779 return (add_string_at_node(node->right, string_to_add));
780 }
[1]781
782/* walk down tree if appropriate */
[128]783 if (node->down != NULL && node->ch == char_to_add) {
784 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_add);
785 depth++;
786 res = add_string_at_node(node->down, string_to_add + 1);
787 depth--;
788 return (res);
789 }
[1]790
[128]791 if (char_to_add == '\0' && node->ch == '\0') {
792 log_msg(6, "%s already in tree", original_string);
793 return (1);
794 }
[1]795
796/* add here */
[128]797 if (!(newnode = (struct s_node *) malloc(sizeof(struct s_node)))) {
[541]798 log_to_screen("failed to malloc");
[128]799 depth--;
800 return (1);
801 }
802 if (char_to_add < node->ch) // add to the left of node
[1]803 {
[128]804 log_msg(7, "depth=%d char=%c --- adding (left)", depth,
805 char_to_add);
806 memcpy((void *) newnode, (void *) node, sizeof(struct s_node));
807 node->right = newnode;
808 } else if (char_to_add > node->ch) // add to the right of node
809 {
810 log_msg(7, "depth=%d char=%c --- adding (right)", depth,
811 char_to_add);
812 newnode->right = node->right; // newnode is to the RIGHT of node
813 node->right = newnode;
814 node = newnode;
[1]815 }
[128]816 // from now on, we're working on 'node'
817 node->down = NULL;
818 node->ch = char_to_add;
819 node->expanded = node->selected = FALSE;
820 if (char_to_add == '\0') {
821 log_msg(6, "Added %s OK", original_string);
822 return (0);
823 }
824// add the rest
825 log_msg(6, "Adding remaining chars ('%s')", string_to_add + 1);
826 for (i = 1; i < noof_chars; i++) {
827 if (!
828 (node->down =
829 (struct s_node *) malloc(sizeof(struct s_node)))) {
[541]830 log_to_screen("%s - failed to malloc", string_to_add);
[128]831 return (1);
832 }
833 node = node->down;
834 char_to_add = string_to_add[i];
835 log_msg(6, "Adding '%c'", char_to_add);
836 node->ch = char_to_add;
837 node->right = node->down = NULL;
838 node->expanded = node->selected = FALSE;
839 if (!node->ch) {
840 node->selected = sosodef;
841 }
842 }
843 log_msg(6, "Finally - added %s OK", original_string);
844 return (0);
[1]845}
846
847
848
849
850/**
851 * Load a filelist into a <tt>struct s_node</tt>.
852 * When you are done with the filelist, call free_filelist().
853 * @param filelist_fname The file to load the filelist from.
854 * @return A filelist tree structure.
855 */
[128]856struct s_node *load_filelist(char *filelist_fname)
[1]857{
858
[128]859 /*@ structures ************************************************* */
860 struct s_node *filelist;
[1]861
[128]862 /*@ pointers *************************************************** */
863 FILE *pin;
[1]864
[128]865 /*@ buffers **************************************************** */
[2265]866 char *command_to_open_fname = NULL;
[128]867 char fname[MAX_STR_LEN];
868 char tmp[MAX_STR_LEN];
[2265]869 char *tmp1 = NULL;
[128]870 int pos_in_fname;
871 /*@ int ******************************************************** */
872 int percentage;
[1]873
[128]874 /*@ long ******************************************************* */
875 long lines_in_filelist;
876 long lino = 0;
877 /*@ end vars *************************************************** */
[1]878
[128]879 assert_string_is_neither_NULL_nor_zerolength(filelist_fname);
[1]880
[128]881 if (!does_file_exist(filelist_fname)) {
882 fatal_error("filelist does not exist -- cannot load it");
[1]883 }
[541]884 log_to_screen("Loading filelist");
[2265]885 mr_asprintf(&tmp1, "zcat %s | wc -l", filelist_fname);
886 log_msg(6, "tmp1 = %s", tmp1);
887 lines_in_filelist = atol(call_program_and_get_last_line_of_output(tmp1));
888 mr_free(tmp1);
889
[128]890 if (lines_in_filelist < 3) {
[541]891 log_to_screen("Warning - surprisingly short filelist.");
[1]892 }
[128]893 g_original_noof_lines_in_filelist = lines_in_filelist;
894 if (!(filelist = (struct s_node *) malloc(sizeof(struct s_node)))) {
895 return (NULL);
[1]896 }
[128]897 filelist->ch = '/';
898 filelist->right = NULL;
899 filelist->down = malloc(sizeof(struct s_node));
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;
[2265]904
905 mr_asprintf(&command_to_open_fname, "gzip -dc %s", filelist_fname);
[128]906 if (!(pin = popen(command_to_open_fname, "r"))) {
907 log_OS_error("Unable to openin filelist_fname");
[2265]908 mr_free(command_to_open_fname);
[128]909 return (NULL);
910 }
[2265]911 mr_free(command_to_open_fname);
912
[541]913 open_evalcall_form("Loading filelist from disk");
[2202]914 for ((void)fgets(fname, MAX_STR_LEN, pin); !feof(pin);
915 (void)fgets(fname, MAX_STR_LEN, pin)) {
[128]916 if ((fname[strlen(fname) - 1] == 13
917 || fname[strlen(fname) - 1] == 10) && strlen(fname) > 0) {
918 fname[strlen(fname) - 1] = '\0';
919 }
920// strip_spaces (fname);
921 if (!strlen(fname)) {
922 continue;
923 }
924 for (pos_in_fname = 0; fname[pos_in_fname] != '\0'; pos_in_fname++) {
925 if (fname[pos_in_fname] != '/') {
926 continue;
927 }
928 strcpy(tmp, fname);
929 tmp[pos_in_fname] = '\0';
930 if (strlen(tmp)) {
931 add_string_at_node(filelist, tmp);
932 }
933 }
934 add_string_at_node(filelist, fname);
935 if (!(++lino % 1111)) {
936 percentage = (int) (lino * 100 / lines_in_filelist);
937 update_evalcall_form(percentage);
938 }
939 }
940 paranoid_pclose(pin);
941 close_evalcall_form();
942 log_it("Finished loading filelist");
943 return (filelist);
[1]944}
945
946
947/**
948 * Log a list of files in @p node.
949 * @param node The toplevel node to use.
950 */
[128]951void show_filelist(struct s_node *node)
[1]952{
[128]953 static int depth = 0;
954 static char current_string[200];
[1]955
[128]956 if (depth == 0) {
957 log_msg(0, "----------------show filelist--------------");
958 }
959 current_string[depth] = node->ch;
[1]960
[128]961 log_msg(3, "depth=%d", depth);
962 if (node->down) {
963 log_msg(3, "moving down");
964 depth++;
965 show_filelist(node->down);
966 depth--;
967 }
[1]968
[128]969 if (!node->ch) {
970 log_msg(0, "%s\n", current_string);
971 }
[1]972
[128]973 if (node->right) {
974 log_msg(3, "moving right");
975 show_filelist(node->right);
976 }
977 if (depth == 0) {
978 log_msg(0, "----------------show filelist--------------");
979 }
980 return;
[1]981}
982
983
984
985
986/**
987 * Reset the filelist to the state it was when it was loaded. This does not
988 * touch the file on disk.
989 * @param filelist The filelist tree structure.
990 */
[128]991void reload_filelist(struct s_node *filelist)
[1]992{
[128]993 assert(filelist != NULL);
994 toggle_node_selection(filelist, FALSE);
995 toggle_path_expandability(filelist, "/", FALSE);
996 toggle_all_root_dirs_on(filelist);
[1]997}
998
999
1000
1001/**
1002 * Save a filelist tree structure to disk.
1003 * @param filelist The filelist tree structure to save.
1004 * @param outfname Where to save it.
1005 */
[128]1006void save_filelist(struct s_node *filelist, char *outfname)
[1]1007{
[128]1008 /*@ int ********************************************************* */
1009 static int percentage;
1010 static int depth = 0;
[1]1011
[128]1012 /*@ buffers ***************************************************** */
1013 static char str[MAX_STR_LEN];
[1]1014
[128]1015 /*@ structures ************************************************** */
1016 struct s_node *node;
[1]1017
[128]1018 /*@ pointers **************************************************** */
1019 static FILE *fout = NULL;
[1]1020
[128]1021 /*@ long ******************************************************** */
1022 static long lines_in_filelist = 0;
1023 static long lino = 0;
[1]1024
[128]1025 /*@ end vars *************************************************** */
[1]1026
[128]1027 assert(filelist != NULL);
1028 assert(outfname != NULL); // will be zerolength if save_filelist() is called by itself
1029 if (depth == 0) {
[541]1030 log_to_screen("Saving filelist");
[128]1031 if (!(fout = fopen(outfname, "w"))) {
1032 fatal_error("Cannot openout/save filelist");
1033 }
1034 lines_in_filelist = g_original_noof_lines_in_filelist; /* set by load_filelist() */
[541]1035 open_evalcall_form("Saving selection to disk");
[1]1036 }
[128]1037 for (node = filelist; node != NULL; node = node->right) {
1038 str[depth] = node->ch;
1039 log_msg(5, "depth=%d ch=%c", depth, node->ch);
1040 if (!node->ch) {
1041// if (node->selected)
1042// {
1043 fprintf(fout, "%s\n", str);
1044// }
1045 if (!(++lino % 1111)) {
1046 percentage = (int) (lino * 100 / lines_in_filelist);
1047 update_evalcall_form(percentage);
1048 }
1049 }
1050 if (node->down) {
1051 depth++;
1052 save_filelist(node->down, "");
1053 depth--;
1054 }
[1]1055 }
[128]1056 if (depth == 0) {
1057 paranoid_fclose(fout);
1058 close_evalcall_form();
1059 log_it("Finished saving filelist");
1060 }
[1]1061}
1062
1063
1064
1065/**
1066 * Toggle all root dirs on.
1067 * @param filelist The filelist tree structure to operate on.
1068 * @bug I don't understand this function. Would someone care to explain it?
1069 */
[128]1070void toggle_all_root_dirs_on(struct s_node *filelist)
[1]1071{
[128]1072 /*@ structures ************************************************** */
1073 struct s_node *node;
[1]1074
[128]1075 /*@ int ********************************************************* */
1076 static int depth = 0;
1077 static int root_dirs_expanded;
[1]1078
[128]1079 /*@ buffers ***************************************************** */
1080 static char filename[MAX_STR_LEN];
[1]1081
[128]1082 /*@ end vars *************************************************** */
[1]1083
[128]1084 assert(filelist != NULL);
1085 if (depth == 0) {
1086 log_it("Toggling all root dirs ON");
1087 root_dirs_expanded = 0;
[1]1088 }
[128]1089 for (node = filelist; node != NULL; node = node->right) {
1090 filename[depth] = node->ch;
1091 if (node->ch == '\0' && strlen(filename) > 1
1092 && (!strchr(filename + 1, '/'))) {
1093 node->selected = FALSE;
1094 node->expanded = TRUE;
1095// log_it (filename);
1096 root_dirs_expanded++;
1097 }
1098 if (node->down) {
1099 depth++;
1100 toggle_all_root_dirs_on(node->down);
1101 depth--;
1102 }
[1]1103 }
[128]1104 if (depth == 0) {
1105 log_it("Finished toggling all root dirs ON");
1106 }
[1]1107}
1108
1109
1110/**
1111 * Toggle the expandability of a path.
1112 * @param filelist The filelist tree to operate on.
1113 * @param pathname The path to toggle expandability of.
1114 * @param on_or_off Whether to toggle it on or off.
1115 * @bug I don't understand this function. Would someone care to explain it?
1116 */
1117void
[128]1118toggle_path_expandability(struct s_node *filelist, char *pathname,
1119 bool on_or_off)
[1]1120{
1121
[128]1122 /*@ int ******************************************************** */
1123 static int depth = 0;
1124 static int total_expanded;
1125 static int root_depth;
1126 int j;
1127 /*@ structures ************************************************* */
1128 struct s_node *node;
[1]1129
[128]1130 /*@ buffers **************************************************** */
1131 static char current_filename[MAX_STR_LEN];
[1]1132
1133/* char tmp[MAX_STR_LEN+2]; */
1134
[128]1135 /*@ end vars *************************************************** */
[1]1136
[128]1137 assert(filelist != NULL);
1138 assert_string_is_neither_NULL_nor_zerolength(pathname);
1139 if (depth == 0) {
1140 total_expanded = 0;
[1]1141// log_it ("Toggling path's expandability");
[128]1142 for (root_depth = (int) strlen(pathname);
1143 root_depth > 0 && pathname[root_depth - 1] != '/';
1144 root_depth--);
1145 if (root_depth < 2) {
1146 root_depth = (int) strlen(pathname);
1147 }
[1]1148 }
[128]1149 for (node = filelist; node != NULL; node = node->right) {
1150 current_filename[depth] = node->ch;
1151 if (node->down) {
1152 depth++;
1153 toggle_path_expandability(node->down, pathname, on_or_off);
1154 depth--;
[1]1155 }
[128]1156 if (node->ch == '\0') {
1157 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1158 for (j = root_depth;
1159 current_filename[j] != '/'
1160 && current_filename[j] != '\0'; j++);
1161 if (current_filename[j] != '\0') {
1162 for (j++;
1163 current_filename[j] != '/'
1164 && current_filename[j] != '\0'; j++);
1165 }
1166 if (current_filename[j] == '\0') {
1167 node->expanded =
1168 (!strcmp(pathname, current_filename) ? TRUE :
1169 on_or_off);
1170 }
1171 }
[1]1172 }
[128]1173 if (node->expanded) {
1174 if (total_expanded < ARBITRARY_MAXIMUM - 32
1175 || !strrchr(current_filename + strlen(pathname), '/')) {
1176 total_expanded++;
1177 } else {
1178 node->expanded = FALSE;
1179 }
1180 }
[1]1181 }
[128]1182 if (depth == 0) {
1183// log_it ("Finished toggling expandability");
[1]1184 }
1185}
1186
1187/**
1188 * Toggle whether a path is selected.
1189 * @param filelist The filelist tree to operate on.
1190 * @param pathname The path to toggle selection of.
1191 * @param on_or_off Whether to toggle it on or off.
1192 * @bug I don't understand this function. Would someone care to explain it?
1193 */
1194void
[128]1195toggle_path_selection(struct s_node *filelist, char *pathname,
1196 bool on_or_off)
[1]1197{
[128]1198 /*@ int ********************************************************* */
1199 static int depth = 0;
1200 int j;
[1]1201
[128]1202 /*@ structures ************************************************** */
1203 struct s_node *node;
[1]1204
[128]1205 /*@ buffers ***************************************************** */
1206 static char current_filename[MAX_STR_LEN];
[1]1207
[128]1208 /*@ end vars *************************************************** */
1209 assert(filelist != NULL);
1210 assert_string_is_neither_NULL_nor_zerolength(pathname);
1211 if (depth == 0) {
1212 log_it("Toggling path's selection");
[1]1213 }
[128]1214 for (node = filelist; node != NULL; node = node->right) {
1215 current_filename[depth] = node->ch;
1216 if (node->down) {
1217 depth++;
1218 toggle_path_selection(node->down, pathname, on_or_off);
1219 depth--;
[1]1220 }
[128]1221 if (node->ch == '\0') {
1222 if (!strncmp(pathname, current_filename, strlen(pathname))) {
1223 for (j = 0;
1224 pathname[j] != '\0'
1225 && pathname[j] == current_filename[j]; j++);
[2265]1226 if (current_filename[j] == '/'
1227 || current_filename[j] == '\0') {
1228 node->selected = on_or_off;
[128]1229 }
1230 }
1231 }
[1]1232 }
[128]1233 if (depth == 0) {
1234 log_it("Finished toggling selection");
1235 }
[1]1236}
1237
1238
1239/**
1240 * Toggle node selection of a filelist tree.
1241 * @param filelist The filelist tree to operate on.
1242 * @param on_or_off Whether to toggle selection on or off.
1243 * @bug I don't understand this function. Would someone care to explain it?
1244 */
[128]1245void toggle_node_selection(struct s_node *filelist, bool on_or_off)
[1]1246{
[128]1247 /*@ structure ************************************************** */
1248 struct s_node *node;
[1]1249
[128]1250 /*@ end vars *************************************************** */
1251 assert(filelist != NULL);
1252 for (node = filelist; node != NULL; node = node->right) {
1253 if (node->ch == '/') {
1254 continue;
1255 } /* don't go deep */
1256 if (node->ch == '\0') {
1257 node->selected = on_or_off;
1258 }
1259 if (node->down) {
1260 toggle_node_selection(node->down, on_or_off);
1261 }
[1]1262 }
1263}
1264
1265
1266
1267
1268
1269
1270
1271/**
1272 * The pathname to the skeleton filelist, used to give better progress reporting for mondo_makefilelist().
1273 */
[128]1274char *g_skeleton_filelist = NULL;
[1]1275
1276/**
1277 * Number of entries in the skeleton filelist.
1278 */
[128]1279long g_skeleton_entries = 0;
[1]1280
1281/**
1282 * Wrapper around mondo_makefilelist().
1283 * @param bkpinfo The backup information structure. Fields used:
1284 * - @c bkpinfo->differential
1285 * - @c bkpinfo->exclude_paths
1286 * - @c bkpinfo->include_paths
1287 * - @c bkpinfo->make_filelist
1288 * - @c bkpinfo->scratchdir
1289 * - @c bkpinfo->tmpdir
1290 * @return 0 for success, nonzero for failure.
1291 * @see mondo_makefilelist
1292 */
[1645]1293int prepare_filelist()
[1]1294{
1295
[128]1296 /*@ int **************************************************** */
1297 int res = 0;
[1]1298
[128]1299 assert(bkpinfo != NULL);
1300 log_it("tmpdir=%s; scratchdir=%s", bkpinfo->tmpdir,
1301 bkpinfo->scratchdir);
1302 if (bkpinfo->make_filelist) {
1303 mvaddstr_and_log_it(g_currentY, 0,
[541]1304 "Making catalog of files to be backed up");
[128]1305 } else {
1306 mvaddstr_and_log_it(g_currentY, 0,
[541]1307 "Using supplied catalog of files to be backed up");
[128]1308 }
[1]1309
[128]1310 if (bkpinfo->make_filelist) {
1311 res =
1312 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1313 bkpinfo->scratchdir, bkpinfo->include_paths,
1314 bkpinfo->exclude_paths,
1315 bkpinfo->differential, NULL);
1316 } else {
1317 res =
1318 mondo_makefilelist(MONDO_LOGFILE, bkpinfo->tmpdir,
1319 bkpinfo->scratchdir, NULL,
1320 bkpinfo->exclude_paths,
1321 bkpinfo->differential,
1322 bkpinfo->include_paths);
1323 }
[1]1324
[128]1325 if (res) {
[676]1326 log_OS_error("Call to mondo_makefilelist failed");
[541]1327 mvaddstr_and_log_it(g_currentY++, 74, "Failed.");
[128]1328 } else {
[541]1329 mvaddstr_and_log_it(g_currentY++, 74, "Done.");
[128]1330 }
1331 return (res);
[1]1332}
1333
1334
1335/**
1336 * Recursively list all files in @p dir newer than @p time_of_last_full_backup to @p fout.
1337 * @param dir The directory to list to @p fout.
1338 * @param sth The directories to skip (exclude).
1339 * @param fout The file to write everything to.
1340 * @param time_of_last_full_backup Only backup files newer than this (0 to disable).
1341 * @return 0, always.
1342 * @bug Return value should be @c void.
1343 */
[2198]1344int open_and_list_dir(char *dir1, char *sth, FILE * fout,
[128]1345 time_t time_of_last_full_backup)
[1]1346{
[820]1347 const char delims[] = " ";
1348
[128]1349 DIR *dip;
1350 struct dirent *dit;
1351 struct stat statbuf;
[2099]1352 char *new;
[128]1353 char *tmp;
[2198]1354 char *dir = NULL;
[128]1355 static int percentage = 0;
1356 char *skip_these;
1357 char *new_with_spaces;
[820]1358 char *strtmp;
1359 char *token;
1360 char *find_excludes;
[128]1361 static char *name_of_evalcall_form;
1362 int i;
[820]1363 int lastpos = 0;
[128]1364 static int depth = 0;
1365 char *p;
1366 static int counter = 0;
1367 static int uberctr = 0;
1368 static char *find_skeleton_marker;
1369 static long skeleton_lino = 0;
1370 static time_t last_time = 0;
1371 time_t this_time;
[1]1372
[2206]1373 dir = mr_stresc(dir1, "`$\\\"(){}'[]&*?|!#~", '\\');
[128]1374 p = strrchr(dir, '/');
1375 if (p) {
1376 if (!strcmp(p, "/.") || !strcmp(p, "/..")) {
[2241]1377 mr_free(dir);
[128]1378 return (0);
1379 }
1380 }
1381
1382 if (!depth) {
1383 malloc_string(name_of_evalcall_form);
1384 malloc_string(find_skeleton_marker);
[2211]1385 mr_asprintf(&find_excludes, " ");
[820]1386 while((token = mr_strtok (sth, delims, &lastpos))) {
[2211]1387 mr_asprintf(&strtmp,"%s", find_excludes);
[820]1388 paranoid_free(find_excludes);
[2211]1389 mr_asprintf(&find_excludes,"%s -path %s -prune -o", strtmp, token);
[820]1390 paranoid_free(strtmp);
[2241]1391 mr_free(token);
[820]1392 }
[1]1393#if linux
[128]1394 // 2.6 has /sys as a proc-type thing -- must be excluded
[2211]1395 mr_asprintf(&strtmp,
[2198]1396 "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]1397 dir, find_excludes, g_skeleton_filelist);
[1]1398#else
[128]1399 // On BSD, for example, /sys is the kernel sources -- don't exclude
[2211]1400 mr_asprintf(&strtmp,
[2198]1401 "find '%s' -fstype mvfs -prune -o -path /proc -prune -o %s -type d -print > %s 2> /dev/null",
[2190]1402 dir, find_excludes, g_skeleton_filelist);
[1]1403#endif
[820]1404 paranoid_free(find_excludes);
1405 log_msg(5, "find command = %s", strtmp);
[2202]1406 (void)system(strtmp);
[820]1407 paranoid_free(strtmp);
[2211]1408 mr_asprintf(&tmp, "wc -l %s | awk '{print $1;}'", g_skeleton_filelist);
[128]1409 g_skeleton_entries =
1410 1 + atol(call_program_and_get_last_line_of_output(tmp));
[2099]1411 paranoid_free(tmp);
[128]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;
[2190]1418 } else // update evalcall form if appropriate
[128]1419 {
1420 sprintf(find_skeleton_marker,
[2198]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) {
[2211]1438 mr_asprintf(&tmp, "Reading %-68s", dir);
[128]1439 newtDrawRootText(0, g_noof_rows - 3, tmp);
[2099]1440 paranoid_free(tmp);
[128]1441 }
[1]1442#endif
[128]1443 update_evalcall_form(percentage);
1444 }
[1]1445 }
[128]1446 }
[1]1447
[128]1448 depth++;
[1]1449
1450// log_msg(0, "Cataloguing %s", dir);
[128]1451 if (sth[0] == ' ') {
[2211]1452 mr_asprintf(&skip_these, "%s", sth);
[128]1453 } else {
[2211]1454 mr_asprintf(&skip_these, " %s ", sth);
[128]1455 }
[2211]1456 mr_asprintf(&new_with_spaces, " %s ", dir);
[128]1457 if ((dip = opendir(dir)) == NULL) {
[2211]1458 mr_asprintf(&tmp,"opendir %s", dir);
[1260]1459 log_OS_error(tmp);
[2099]1460 paranoid_free(tmp);
[128]1461 } else if (strstr(skip_these, new_with_spaces)) {
1462 fprintf(fout, "%s\n", dir); // if excluded dir then print dir ONLY
1463 } else {
1464 fprintf(fout, "%s\n", dir);
1465 while ((dit = readdir(dip)) != NULL) {
1466 i++;
1467 if (strcmp(dir, "/")) {
[2211]1468 mr_asprintf(&new,"%s/%s",dir,dit->d_name);
[2099]1469 } else {
[2211]1470 mr_asprintf(&new,"%s%s",dir,dit->d_name);
[128]1471 }
[2099]1472 paranoid_free(new_with_spaces);
[2211]1473 mr_asprintf(&new_with_spaces, " %s ", new);
[128]1474 if (strstr(skip_these, new_with_spaces)) {
1475 fprintf(fout, "%s\n", new);
[2099]1476 paranoid_free(new_with_spaces);
[128]1477 } else {
[2099]1478 paranoid_free(new_with_spaces);
[128]1479 if (!lstat(new, &statbuf)) {
1480 if (!S_ISLNK(statbuf.st_mode)
1481 && S_ISDIR(statbuf.st_mode)) {
1482 open_and_list_dir(new, skip_these, fout,
1483 time_of_last_full_backup);
1484 } else {
1485 if (time_of_last_full_backup == 0
1486 || time_of_last_full_backup <
1487 statbuf.st_ctime) {
1488 fprintf(fout, "%s\n", new);
1489 if ((counter++) > 128) {
1490 counter = 0;
1491 uberctr++;
[2211]1492 mr_asprintf(&tmp, " %c ",
[128]1493 special_dot_char(uberctr));
[1]1494#ifndef _XWIN
[128]1495 if (!g_text_mode) {
1496 newtDrawRootText(77, g_noof_rows - 3,
1497 tmp);
1498 newtRefresh();
1499 }
[1]1500#endif
[2099]1501 paranoid_free(tmp);
[128]1502 }
1503 }
1504 }
1505 }
1506 }
[2099]1507 paranoid_free(new);
[1]1508 }
[128]1509 }
[2099]1510 paranoid_free(new_with_spaces);
1511 paranoid_free(skip_these);
[2241]1512 mr_free(dir);
[2099]1513
[128]1514 if (dip) {
1515 if (closedir(dip) == -1) {
1516 log_OS_error("closedir");
1517 }
1518 }
1519 depth--;
1520 if (!depth) {
1521 close_evalcall_form();
1522 paranoid_free(name_of_evalcall_form);
1523 paranoid_free(find_skeleton_marker);
1524 unlink(g_skeleton_filelist);
1525 log_msg(5, "g_skeleton_entries = %ld", g_skeleton_entries);
1526 }
1527 return (0);
[1]1528}
1529
1530
1531
1532/**
1533 * Get the next entry in the space-separated list in @p incoming.
1534 * So if @p incoming was '"one and two" three four', we would
1535 * return "one and two".
1536 * @param incoming The list to get the next entry from.
1537 * @return The first item in the list (respecting double quotes).
1538 * @note The returned string points to static data that will be overwritten with each call.
1539 */
[128]1540char *next_entry(char *incoming)
[1]1541{
[2100]1542 char *sz_res;
[128]1543 char *p;
1544 bool in_quotes = FALSE;
[1]1545
[2211]1546 mr_asprintf(&sz_res, "%s", incoming);
[128]1547 p = sz_res;
1548 while ((*p != ' ' || in_quotes) && *p != '\0') {
1549 if (*p == '\"') {
1550 in_quotes = !in_quotes;
1551 }
1552 p++;
1553 }
1554 *p = '\0';
1555 return (sz_res);
[1]1556}
1557
1558
1559
1560/**
1561 * Create the filelist for the backup. It will be stored in [scratchdir]/archives/filelist.full.
1562 * @param logfile Unused.
1563 * @param tmpdir The tmpdir of the backup.
1564 * @param scratchdir The scratchdir of the backup.
1565 * @param include_paths The paths to back up, or NULL if you're using a user-defined filelist.
1566 * @param excp The paths to NOT back up.
1567 * @param differential The differential level (currently only 0 and 1 are supported).
1568 * @param userdef_filelist The user-defined filelist, or NULL if you're using @p include_paths.
1569 * @return 0, always.
1570 * @bug @p logfile is unused.
1571 * @bug Return value is meaningless.
1572 */
[128]1573int mondo_makefilelist(char *logfile, char *tmpdir, char *scratchdir,
1574 char *include_paths, char *excp, int differential,
1575 char *userdef_filelist)
[1]1576{
1577 char *p, *q;
[2099]1578 char *sz_datefile;
[1]1579 char *sz_filelist, *exclude_paths, *tmp;
1580 int i;
[128]1581 FILE *fout;
[2265]1582 char *command = NULL;
[128]1583 time_t time_of_last_full_backup = 0;
[1]1584 struct stat statbuf;
[1854]1585 char *tmp1 = NULL;
[1856]1586 char *tmp2 = NULL;
[1]1587
[128]1588 malloc_string(tmp);
1589 malloc_string(g_skeleton_filelist);
[1540]1590 if (!(exclude_paths = malloc(8*MAX_STR_LEN))) {
[128]1591 fatal_error("Cannot malloc exclude_paths");
1592 }
[2211]1593 mr_asprintf(&sz_datefile,MONDO_CACHE"/difflevel.%d" , 0);
[128]1594 if (!include_paths && !userdef_filelist) {
1595 fatal_error
1596 ("Please supply either include_paths or userdef_filelist");
1597 }
[2279]1598 // make hole for filelist
[2265]1599 mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
[128]1600 paranoid_system(command);
[2265]1601 mr_free(command);
1602
[2211]1603 mr_asprintf(&sz_filelist, "%s/tmpfs/filelist.full", tmpdir);
[128]1604 make_hole_for_file(sz_filelist);
[1]1605
[128]1606 if (differential == 0) {
1607 // restore last good datefile if it exists
[2265]1608 mr_asprintf(&command, "cp -f %s.aborted %s", sz_datefile, sz_datefile);
[128]1609 run_program_and_log_output(command, 3);
[2265]1610 mr_free(command);
1611
[128]1612 // backup last known good datefile just in case :)
1613 if (does_file_exist(sz_datefile)) {
[2265]1614 mr_asprintf(&command, "mv -f %s %s.aborted", sz_datefile,
[128]1615 sz_datefile);
1616 paranoid_system(command);
[2265]1617 mr_free(command);
[128]1618 }
1619 make_hole_for_file(sz_datefile);
1620 write_one_liner_data_file(sz_datefile,
1621 call_program_and_get_last_line_of_output
1622 ("date +%s"));
1623 } else if (lstat(sz_datefile, &statbuf)) {
1624 log_msg(2,
1625 "Warning - unable to find date of previous backup. Full backup instead.");
1626 differential = 0;
1627 time_of_last_full_backup = 0;
1628 } else {
1629 time_of_last_full_backup = statbuf.st_mtime;
1630 log_msg(2, "Differential backup. Yay.");
1631 }
[2099]1632 paranoid_free(sz_datefile);
[1]1633
1634// use user-specified filelist (if specified)
[128]1635 if (userdef_filelist) {
1636 log_msg(1,
1637 "Using the user-specified filelist - %s - instead of calculating one",
1638 userdef_filelist);
[2265]1639 mr_asprintf(&command, "cp -f %s %s", userdef_filelist, sz_filelist);
[128]1640 if (run_program_and_log_output(command, 3)) {
[2265]1641 mr_free(command);
[128]1642 fatal_error("Failed to copy user-specified filelist");
1643 }
[2265]1644 mr_free(command);
[128]1645 } else {
1646 log_msg(2, "include_paths = '%s'", include_paths);
1647 log_msg(1, "Calculating filelist");
[2211]1648 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]1649 if (strlen(tmp2) < 1) {
[2211]1650 mr_asprintf(&tmp1," ");
[1856]1651 } else {
1652 log_msg(2, "Found windows FS: %s",tmp2);
[2211]1653 mr_asprintf(&tmp1, "find %s -name '/win386.swp' -o -name '/hiberfil.sys' -o -name '/pagefile.sys' 2> /dev/null\n",tmp2);
[1856]1654 paranoid_free(tmp2);
[2211]1655 mr_asprintf(&tmp2, "%s", call_program_and_get_last_line_of_output(tmp1));
[1856]1656 log_msg(2, "Found windows files: %s",tmp2);
1657 }
[1854]1658 paranoid_free(tmp1);
[1]1659
[1856]1660 snprintf(exclude_paths, (size_t)8*MAX_STR_LEN," %s %s %s %s %s . .. \
[1884]1661" MNT_CDROM " " MNT_FLOPPY " /media /tmp \
[2059]1662/proc /sys " MINDI_CACHE, MONDO_CACHE, excp, tmp2, (tmpdir[0] == '/' && tmpdir[1] == '/') ? (tmpdir + 1) : tmpdir, (scratchdir[0] == '/' && scratchdir[1] == '/') ? (scratchdir + 1) : scratchdir);
[1856]1663 paranoid_free(tmp2);
1664
[128]1665 log_msg(2, "Excluding paths = '%s'", exclude_paths);
1666 log_msg(2,
1667 "Generating skeleton filelist so that we can track our progress");
1668 sprintf(g_skeleton_filelist, "%s/tmpfs/skeleton.txt", tmpdir);
1669 make_hole_for_file(g_skeleton_filelist);
1670 log_msg(4, "g_skeleton_entries = %ld", g_skeleton_entries);
1671 log_msg(2, "Opening out filelist to %s", sz_filelist);
1672 if (!(fout = fopen(sz_filelist, "w"))) {
1673 fatal_error("Cannot openout to sz_filelist");
[1]1674 }
[128]1675 i = 0;
1676 if (strlen(include_paths) == 0) {
1677 log_msg(1, "Including only '/' in %s", sz_filelist);
1678 open_and_list_dir("/", exclude_paths, fout,
1679 time_of_last_full_backup);
1680 } else {
1681 p = include_paths;
1682 while (*p) {
1683 q = next_entry(p);
1684 log_msg(1, "Including %s in filelist %s", q, sz_filelist);
1685 open_and_list_dir(q, exclude_paths, fout,
1686 time_of_last_full_backup);
1687 p += strlen(q);
[2100]1688 paranoid_free(q);
[128]1689 while (*p == ' ') {
1690 p++;
1691 }
[1]1692 }
1693 }
[128]1694 paranoid_fclose(fout);
1695 }
1696 log_msg(2, "Copying new filelist to scratchdir");
[2265]1697 mr_asprintf(&command, "mkdir -p %s/archives", scratchdir);
[128]1698 paranoid_system(command);
[2265]1699 mr_free(command);
1700
1701 mr_asprintf(&command, "cp -f %s %s/archives/", sz_filelist, scratchdir);
[128]1702 paranoid_system(command);
[2265]1703 mr_free(command);
1704
1705 mr_asprintf(&command, "mv -f %s %s", sz_filelist, tmpdir);
[128]1706 paranoid_system(command);
[2265]1707 mr_free(command);
1708
[2099]1709 paranoid_free(sz_filelist);
[128]1710 log_msg(2, "Freeing variables");
1711 paranoid_free(exclude_paths);
1712 paranoid_free(tmp);
1713 paranoid_free(g_skeleton_filelist);
1714 log_msg(2, "Exiting");
1715 return (0);
[1]1716}
1717
1718
1719
1720
1721/**
1722 * Locate the string @p string_to_find in the tree rooted at @p startnode.
1723 * @param startnode The node containing the root of the directory tree.
1724 * @param string_to_find The string to look for at @p startnode.
1725 * @return The node containing the last element of @p string_to_find, or NULL if
1726 * it was not found.
1727 */
[128]1728struct s_node *find_string_at_node(struct s_node *startnode,
1729 char *string_to_find)
[1]1730{
[128]1731 /*@ int ******************************************************** */
1732 int noof_chars;
1733 static int depth = 0;
1734 static char original_string[MAX_STR_LEN];
[1]1735
[128]1736 /*@ sturctures ************************************************* */
1737 struct s_node *node;
[1]1738
[128]1739 /*@ char ****************************************************** */
1740 char char_to_find;
[1]1741
[128]1742 /*@ bools ****************************************************** */
[1]1743
[128]1744 if (!depth) {
1745 strcpy(original_string, string_to_find);
1746 }
[1]1747
[128]1748 assert(startnode != NULL);
1749 assert(string_to_find != NULL);
[1]1750
[128]1751 noof_chars = strlen(string_to_find) + 1; /* we include the '\0' */
[1]1752
[128]1753 log_msg(7, "starting --- str=%s", string_to_find);
[1]1754
1755/* walk across tree if necessary */
[128]1756 node = startnode;
1757 char_to_find = string_to_find[0];
1758 if (node->right != NULL && node->ch < char_to_find) {
1759 log_msg(7, "depth=%d --- going RIGHT ... %c-->%c", depth,
1760 char_to_find, node->ch, (node->right)->ch);
1761 return (find_string_at_node(node->right, string_to_find));
1762 }
[1]1763
1764/* walk down tree if appropriate */
[128]1765 if (node->down != NULL && node->ch == char_to_find) {
1766 log_msg(7, "depth=%d char=%c --- going DOWN", depth, char_to_find);
1767 depth++;
1768 node = find_string_at_node(node->down, string_to_find + 1);
1769 depth--;
1770 return (node);
1771 }
[1]1772
[128]1773 if (char_to_find == '\0' && node->ch == '\0') {
1774 log_msg(7, "%s is in tree", original_string);
1775 return (node);
1776 } else {
1777 log_msg(7, "%s is NOT in tree", original_string);
1778 return (NULL);
1779 }
[1]1780}
1781
1782
1783
1784/**
1785 * Write all entries in @p needles_list_fname which are also in
1786 * @p filelist to @p matches_list_fname.
1787 * @param needles_list_fname A file containing strings to look for, 1 per line.
1788 * @param filelist The node for the root of the directory structure to search in.
1789 * @param matches_list_fname The filename where we should put the matches.
1790 * @return The number of matches found.
1791 */
[128]1792long save_filelist_entries_in_common(char *needles_list_fname,
1793 struct s_node *filelist,
1794 char *matches_list_fname,
1795 bool use_star)
[1]1796{
[128]1797 int retval = 0;
1798 struct s_node *found_node;
1799 FILE *fin;
1800 FILE *fout;
1801 char *fname;
1802 char *tmp;
1803 size_t len = 0; // Scrub's patch doesn't work without that
[1]1804
1805// log_msg(1, "use_star = %s", (use_star)?"TRUE":"FALSE");
[128]1806 malloc_string(fname);
1807 malloc_string(tmp);
1808 log_msg(5, "starting");
1809 log_msg(5, "needles_list_fname = %s", needles_list_fname);
1810 log_msg(5, "matches_list_fname = %s", matches_list_fname);
1811 if (!(fin = fopen(needles_list_fname, "r"))) {
1812 fatal_error("Cannot openin needles_list_fname");
1813 }
1814 if (!(fout = fopen(matches_list_fname, "w"))) {
1815 fatal_error("Cannot openout matches_list_fname");
1816 }
1817 while (!feof(fin)) {
[1]1818// fscanf(fin, "%s\n", fname);
[128]1819 len = MAX_STR_LEN - 1;
[2202]1820 (void)getline(&fname, &len, fin); // patch by Scrub
[128]1821 if (!use_star) {
1822 if (fname[0] == '/') {
1823 strcpy(tmp, fname);
1824 } else {
1825 tmp[0] = '/';
1826 strcpy(tmp + 1, fname);
1827 }
1828 strcpy(fname, tmp);
1829 }
1830 while (strlen(fname) > 0 && fname[strlen(fname) - 1] < 32) {
1831 fname[strlen(fname) - 1] = '\0';
1832 }
[1]1833
[128]1834 log_msg(5, "Looking for '%s'", fname);
1835 found_node = find_string_at_node(filelist, fname);
1836 if (found_node) {
1837 if (found_node->selected) {
1838 if (fname[0] == '/') {
1839 strcpy(tmp, fname + 1);
1840 strcpy(fname, tmp);
1841 }
1842 log_msg(5, "Found '%s'", fname);
1843 turn_wildcard_chars_into_literal_chars(tmp, fname);
1844 fprintf(fout, "%s\n", tmp);
1845 retval++;
1846 }
[1]1847 }
1848 }
[128]1849 paranoid_fclose(fout);
1850 paranoid_fclose(fin);
1851 paranoid_free(fname);
1852 paranoid_free(tmp);
1853 return (retval);
[1]1854}
1855
1856
1857
1858
1859
1860
1861/**
1862 * Add all files listed in @p list_of_files_fname to the directory structure rooted at
1863 * @p filelist.
1864 * @param filelist The top node of the directory structure to add the files to.
1865 * @param list_of_files_fname The file containing the files to add, 1 per line.
1866 * @param flag_em If TRUE, then flag the added files for restoration.
1867 * @return 0 for success, nonzero for failure.
1868 */
[128]1869int add_list_of_files_to_filelist(struct s_node *filelist,
1870 char *list_of_files_fname, bool flag_em)
[1]1871{
[128]1872 FILE *fin;
1873 char *tmp;
1874 struct s_node *nod;
[1]1875
[128]1876 malloc_string(tmp);
1877 log_msg(3, "Adding %s to filelist", list_of_files_fname);
1878 if (!(fin = fopen(list_of_files_fname, "r"))) {
[2227]1879 log_it("%s",list_of_files_fname);
[128]1880 return (1);
1881 }
[2202]1882 for ((void)fgets(tmp, MAX_STR_LEN, fin); !feof(fin);
1883 (void)fgets(tmp, MAX_STR_LEN, fin)) {
[128]1884 if (!tmp[0]) {
1885 continue;
1886 }
1887 if ((tmp[strlen(tmp) - 1] == 13 || tmp[strlen(tmp) - 1] == 10)
1888 && strlen(tmp) > 0) {
1889 tmp[strlen(tmp) - 1] = '\0';
1890 }
1891 log_msg(2, "tmp = '%s'", tmp);
1892 if (!tmp[0]) {
1893 continue;
1894 }
1895 if ((nod = find_string_at_node(filelist, tmp))) {
1896 log_msg(5, "Found '%s' in filelist already. Cool.", tmp);
1897 } else {
1898 add_string_at_node(filelist, tmp);
1899 nod = find_string_at_node(filelist, tmp);
1900 }
[1]1901
[128]1902 if (nod && flag_em) {
1903 toggle_path_selection(filelist, tmp, TRUE);
1904 log_msg(5, "Flagged '%s'", tmp);
1905 }
[1]1906 }
[128]1907 paranoid_fclose(fin);
1908 paranoid_free(tmp);
1909 return (0);
[1]1910}
1911
1912/* @} - end of filelistGroup */
Note: See TracBrowser for help on using the repository browser.