source: MondoRescue/branches/3.1/mondo/src/common/libmondo-filelist.c@ 3147

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