source: MondoRescue/branches/3.3/mondo/src/common/libmondo-filelist.c@ 3871

Last change on this file since 3871 was 3871, checked in by Bruno Cornec, 4 months ago

Fix syntax issues

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