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