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