source: MondoRescue/branches/3.0/mondo/src/common/libmondo-filelist.c@ 2918

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