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

Last change on this file since 2715 was 2715, checked in by Bruno Cornec, 13 years ago
  • Document in man page and HOWTO the previous syntax modifications for -E/-I options and '|' separator
  • Fix #444. mondoarchive now supports inclusion/exclusion of dirs with spaces in their name. this required a syntax change for the -E/-I options where the list of dirs needs to be separated by '|' now instead of ' '.
  • Fix issus with mindi when launched from a dir containing spaces
  • Fix a but when no -E option is give on CLI (null) string generated
  • Fix #459 by correcting an old README containing info on the edit-mountlist binary which doesn't exist.

svn merge -r 2709:2714 svn+ssh://bruno@svn.mondorescue.org/mondo/svn/mondorescue/branches/2.2.9 .

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