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

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

r3334@localhost: bruno | 2009-08-08 12:17:37 +0200

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