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

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