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

Last change on this file since 2446 was 2444, checked in by Bruno Cornec, 16 years ago
  • priority given to env var for tmpdir
  • fuse FS excluded from tmpdir computation
  • Improves udev timeout handling
  • fuse device is needed at restore time for sshfs
  • Avoids comment analysis in the new code handling deplist.d
  • Avoids error message if some ssh conf files do not exist
  • Improvements on sshfs/ssh management in restore mode
  • Include now all minimal requirements for sshfs
  • Fix udev support in init for Mandriva 2009.1 (udevadm settle is blocking)
  • Improve deplist.d/* files
  • adds libnss for ssh support in boot disk
  • Solve a memory management issue in DSF module (strtok => mr_strtok)
  • DSF now also supports partitions in addition to full disks
  • Adds debug in open_and_list_dir
  • Improve user management for NetFS cases
  • ssh in the restore env needs some dlopened libs (libnss_compat|files)
  • Warning emited in case of duplicate mountpoints or devices in fstab found
  • Use only the AWK veriable everywhere and not the awk command
  • Simplify GetInitrdFilesystemToUse which doesn't need to detect everything, but just know if it's initramfs (new mode) or a compressed FS ext2/3 (older mode) as initrd is anyway re-made by mindi. Should fix #353

(Backport from 2.2.9)

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