source: MondoRescue/trunk/mondo/mondo/common/libmondo-filelist.c@ 783

Last change on this file since 783 was 783, checked in by Bruno Cornec, 18 years ago
  • Massive rewrite continues for memory management.
  • main structure should now have all parameters allocated dynamically
  • new lib libmr.a + dir + build process reviewed to support it.
  • new include subdir to host external definitions of the new lib
  • code now compiles. Still one remaining link issues for mondorestore. This should allow for some tests soon.

(goal is to separate completely reviewed code and functions and provide clean interfaces)

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