[2053] | 1 | /* file manipulation
|
---|
[116] | 2 | $Id: libmondo-files.c 3829 2024-03-04 16:11:25Z bruno $
|
---|
[1] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /**
|
---|
| 6 | * @file
|
---|
| 7 | * Functions to manipulate files.
|
---|
| 8 | */
|
---|
| 9 |
|
---|
| 10 |
|
---|
| 11 | #include "my-stuff.h"
|
---|
[2242] | 12 | #include "mr_mem.h"
|
---|
[3613] | 13 | #include "mr_file.h"
|
---|
[1] | 14 | #include "mondostructures.h"
|
---|
| 15 | #include "libmondo-files.h"
|
---|
| 16 |
|
---|
[541] | 17 | #include "lib-common-externs.h"
|
---|
| 18 |
|
---|
[1] | 19 | #include "libmondo-tools-EXT.h"
|
---|
[541] | 20 | #include "libmondo-gui-EXT.h"
|
---|
[1] | 21 | #include "libmondo-devices-EXT.h"
|
---|
| 22 | #include "libmondo-fork-EXT.h"
|
---|
| 23 | #include "libmondo-string-EXT.h"
|
---|
| 24 |
|
---|
| 25 | /*@unused@*/
|
---|
[116] | 26 | //static char cvsid[] = "$Id: libmondo-files.c 3829 2024-03-04 16:11:25Z bruno $";
|
---|
[1] | 27 |
|
---|
| 28 | extern char err_log_lines[NOOF_ERR_LINES][MAX_STR_LEN];
|
---|
| 29 |
|
---|
| 30 | extern int g_currentY;
|
---|
| 31 | extern char *g_mondo_home;
|
---|
| 32 |
|
---|
[3350] | 33 | extern bool g_text_mode;
|
---|
| 34 |
|
---|
[1645] | 35 | /* Reference to global bkpinfo */
|
---|
| 36 | extern struct s_bkpinfo *bkpinfo;
|
---|
| 37 |
|
---|
[1] | 38 | /**
|
---|
| 39 | * @addtogroup fileGroup
|
---|
| 40 | * @{
|
---|
| 41 | */
|
---|
| 42 | /**
|
---|
| 43 | * Get an md5 checksum of the specified file.
|
---|
| 44 | * @param filename The file to checksum.
|
---|
| 45 | * @return The 32-character ASCII representation of the 128-bit checksum.
|
---|
| 46 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 47 | */
|
---|
[116] | 48 | char *calc_checksum_of_file(char *filename)
|
---|
[1] | 49 | {
|
---|
[116] | 50 | /*@ buffers ***************************************************** */
|
---|
| 51 | static char output[MAX_STR_LEN];
|
---|
[3191] | 52 | char *command = NULL;
|
---|
[1] | 53 |
|
---|
[116] | 54 | /*@ pointers **************************************************** */
|
---|
[3191] | 55 | char *p = NULL;
|
---|
[3060] | 56 | char *q;
|
---|
[116] | 57 | FILE *fin;
|
---|
[1] | 58 |
|
---|
[116] | 59 | /*@ initialize pointers ***************************************** */
|
---|
[1] | 60 |
|
---|
[116] | 61 | p = output;
|
---|
[1] | 62 |
|
---|
[116] | 63 | /*@************************************************************** */
|
---|
[1] | 64 |
|
---|
[116] | 65 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 66 | if (does_file_exist(filename)) {
|
---|
[3191] | 67 | mr_asprintf(command, "md5sum \"%s\"", filename);
|
---|
[116] | 68 | fin = popen(command, "r");
|
---|
| 69 | if (fin) {
|
---|
[3060] | 70 | q = fgets(output, MAX_STR_LEN, fin);
|
---|
| 71 | if (!q) {
|
---|
| 72 | // FIXME
|
---|
| 73 | }
|
---|
| 74 | q = strchr(output, ' ');
|
---|
[116] | 75 | paranoid_pclose(fin);
|
---|
| 76 | }
|
---|
[3191] | 77 | mr_free(command);
|
---|
[116] | 78 | } else {
|
---|
[3191] | 79 | log_it("File '%s' not found; cannot calc checksum", filename);
|
---|
[1] | 80 | }
|
---|
[116] | 81 | if (p) {
|
---|
| 82 | *p = '\0';
|
---|
| 83 | }
|
---|
| 84 | return (output);
|
---|
[1] | 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
| 88 | /**
|
---|
| 89 | * Get a not-quite-unique representation of some of the file's @c stat properties.
|
---|
| 90 | * The returned string has the form <tt>size-mtime-ctime</tt>.
|
---|
| 91 | * @param curr_fname The file to generate the "checksum" for.
|
---|
| 92 | * @return The "checksum".
|
---|
| 93 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 94 | */
|
---|
[116] | 95 | char *calc_file_ugly_minichecksum(char *curr_fname)
|
---|
[1] | 96 | {
|
---|
| 97 |
|
---|
[116] | 98 | /*@ buffers ***************************************************** */
|
---|
| 99 | static char curr_cksum[1000];
|
---|
[1] | 100 |
|
---|
[116] | 101 | /*@ pointers **************************************************** */
|
---|
[1] | 102 |
|
---|
[116] | 103 | /*@ structures ************************************************** */
|
---|
| 104 | struct stat buf;
|
---|
[1] | 105 |
|
---|
[116] | 106 | /*@ initialize data *************************************************** */
|
---|
| 107 | curr_cksum[0] = '\0';
|
---|
[1] | 108 |
|
---|
[116] | 109 | /*@************************************************************** */
|
---|
[1] | 110 |
|
---|
[116] | 111 | assert_string_is_neither_NULL_nor_zerolength(curr_fname);
|
---|
| 112 | if (lstat(curr_fname, &buf)) {
|
---|
| 113 | return (curr_cksum); // empty
|
---|
| 114 | }
|
---|
[1] | 115 |
|
---|
[116] | 116 | sprintf(curr_cksum, "%ld-%ld-%ld", (long) (buf.st_size),
|
---|
| 117 | (long) (buf.st_mtime), (long) (buf.st_ctime));
|
---|
| 118 | return (curr_cksum);
|
---|
[1] | 119 | }
|
---|
| 120 |
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | /**
|
---|
| 124 | * Get the number of lines in @p filename.
|
---|
| 125 | * @param filename The file to count lines in.
|
---|
| 126 | * @return The number of lines in @p filename.
|
---|
| 127 | * @bug This function uses the shell and "wc -l"; it should probably be rewritten in C.
|
---|
| 128 | */
|
---|
[116] | 129 | long count_lines_in_file(char *filename)
|
---|
[1] | 130 | {
|
---|
| 131 |
|
---|
[116] | 132 | /*@ buffers ***************************************************** */
|
---|
[3191] | 133 | char *command = NULL;
|
---|
| 134 | char *incoming = NULL;
|
---|
[1] | 135 |
|
---|
[116] | 136 | /*@ long ******************************************************** */
|
---|
| 137 | long noof_lines = -1L;
|
---|
[1] | 138 |
|
---|
[116] | 139 | /*@ pointers **************************************************** */
|
---|
| 140 | FILE *fin;
|
---|
[1] | 141 |
|
---|
[116] | 142 | assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 143 | if (!does_file_exist(filename)) {
|
---|
[3191] | 144 | log_it("%s does not exist, so I cannot found the number of lines in it", filename);
|
---|
[116] | 145 | return (0);
|
---|
[1] | 146 | }
|
---|
[3191] | 147 | mr_asprintf(command, "cat %s | wc -l", filename);
|
---|
[116] | 148 | if (!does_file_exist(filename)) {
|
---|
[3191] | 149 | mr_free(command);
|
---|
[116] | 150 | return (-1);
|
---|
[1] | 151 | }
|
---|
[116] | 152 | fin = popen(command, "r");
|
---|
[3191] | 153 | mr_free(command);
|
---|
| 154 |
|
---|
[116] | 155 | if (fin) {
|
---|
| 156 | if (feof(fin)) {
|
---|
| 157 | noof_lines = 0;
|
---|
| 158 | } else {
|
---|
[3191] | 159 | mr_getline(incoming, fin);
|
---|
| 160 | while (strlen(incoming) > 0 && incoming[strlen(incoming) - 1] < 32) {
|
---|
[116] | 161 | incoming[strlen(incoming) - 1] = '\0';
|
---|
| 162 | }
|
---|
| 163 | noof_lines = atol(incoming);
|
---|
[3191] | 164 | mr_free(incoming);
|
---|
[116] | 165 | }
|
---|
| 166 | paranoid_pclose(fin);
|
---|
| 167 | }
|
---|
| 168 | return (noof_lines);
|
---|
[1] | 169 | }
|
---|
| 170 |
|
---|
| 171 |
|
---|
| 172 | /**
|
---|
| 173 | * Check for existence of given @p filename.
|
---|
| 174 | * @param filename The file to check for.
|
---|
| 175 | * @return TRUE if it exists, FALSE otherwise.
|
---|
| 176 | */
|
---|
[116] | 177 | bool does_file_exist(char *filename)
|
---|
[1] | 178 | {
|
---|
| 179 |
|
---|
[116] | 180 | /*@ structures ************************************************** */
|
---|
| 181 | struct stat buf;
|
---|
[1] | 182 |
|
---|
[116] | 183 | /*@************************************************************** */
|
---|
[1] | 184 |
|
---|
[116] | 185 | assert(filename != NULL);
|
---|
| 186 | // assert_string_is_neither_NULL_nor_zerolength(filename);
|
---|
| 187 | if (lstat(filename, &buf)) {
|
---|
| 188 | log_msg(20, "%s does not exist", filename);
|
---|
| 189 | return (FALSE);
|
---|
| 190 | } else {
|
---|
| 191 | log_msg(20, "%s exists", filename);
|
---|
| 192 | return (TRUE);
|
---|
| 193 | }
|
---|
[1] | 194 | }
|
---|
| 195 |
|
---|
| 196 |
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | /**
|
---|
| 202 | * Modify @p inout (a file containing a list of files) to only contain files
|
---|
| 203 | * that exist.
|
---|
| 204 | * @param inout The filelist to operate on.
|
---|
| 205 | * @note The original file is renamed beforehand, so it will not be accessible
|
---|
| 206 | * while the modification is in progress.
|
---|
| 207 | */
|
---|
[116] | 208 | void exclude_nonexistent_files(char *inout)
|
---|
[1] | 209 | {
|
---|
[3191] | 210 | char *infname = NULL;
|
---|
| 211 | char *outfname = NULL;
|
---|
| 212 | char *tmp = NULL;
|
---|
| 213 | char *incoming = NULL;
|
---|
[1] | 214 |
|
---|
[116] | 215 | /*@ int ********************************************************* */
|
---|
| 216 | int i;
|
---|
[1] | 217 |
|
---|
[116] | 218 | /*@ pointers **************************************************** */
|
---|
| 219 | FILE *fin, *fout;
|
---|
[1] | 220 |
|
---|
| 221 |
|
---|
[116] | 222 | /*@ end vars *********************************************************** */
|
---|
[1] | 223 |
|
---|
[116] | 224 | assert_string_is_neither_NULL_nor_zerolength(inout);
|
---|
[3191] | 225 | mr_asprintf(infname, "%s.in", inout);
|
---|
| 226 | mr_asprintf(tmp, "cp -f %s %s", inout, infname);
|
---|
[116] | 227 | run_program_and_log_output(tmp, FALSE);
|
---|
[3191] | 228 | mr_free(tmp);
|
---|
| 229 |
|
---|
[116] | 230 | if (!(fin = fopen(infname, "r"))) {
|
---|
| 231 | log_OS_error("Unable to openin infname");
|
---|
[3191] | 232 | mr_free(infname);
|
---|
[116] | 233 | return;
|
---|
[1] | 234 | }
|
---|
[3191] | 235 |
|
---|
| 236 | mr_asprintf(outfname, "%s", inout);
|
---|
[116] | 237 | if (!(fout = fopen(outfname, "w"))) {
|
---|
| 238 | log_OS_error("Unable to openout outfname");
|
---|
[3191] | 239 | mr_free(infname);
|
---|
| 240 | mr_free(outfname);
|
---|
[116] | 241 | return;
|
---|
[1] | 242 | }
|
---|
[3191] | 243 | mr_free(outfname);
|
---|
| 244 |
|
---|
| 245 | for (mr_getline(incoming, fin); !feof(fin); mr_getline(incoming, fin)) {
|
---|
[116] | 246 | i = strlen(incoming) - 1;
|
---|
| 247 | if (i >= 0 && incoming[i] < 32) {
|
---|
| 248 | incoming[i] = '\0';
|
---|
| 249 | }
|
---|
| 250 | if (does_file_exist(incoming)) {
|
---|
| 251 | fprintf(fout, "%s\n", incoming);
|
---|
| 252 | } else {
|
---|
[3191] | 253 | log_it("Excluding '%s'-nonexistent\n", incoming);
|
---|
[116] | 254 | }
|
---|
[3191] | 255 | mr_free(incoming);
|
---|
[1] | 256 | }
|
---|
[3191] | 257 | mr_free(incoming);
|
---|
| 258 |
|
---|
[116] | 259 | paranoid_fclose(fout);
|
---|
| 260 | paranoid_fclose(fin);
|
---|
| 261 | unlink(infname);
|
---|
[3191] | 262 | mr_free(infname);
|
---|
[1] | 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 | /**
|
---|
| 267 | * Attempt to find the user's kernel by calling Mindi.
|
---|
| 268 | * If Mindi can't find the kernel, ask user. If @p kernel is not empty,
|
---|
| 269 | * don't do anything.
|
---|
| 270 | * @param kernel Where to put the found kernel.
|
---|
[3829] | 271 | * @return 0 for success aborts otherwise
|
---|
[1] | 272 | */
|
---|
[116] | 273 | int figure_out_kernel_path_interactively_if_necessary(char *kernel)
|
---|
[1] | 274 | {
|
---|
[3191] | 275 | char *tmp = NULL;
|
---|
| 276 | char *command = NULL;
|
---|
[1] | 277 |
|
---|
[3829] | 278 | if (kernel == NULL) {
|
---|
| 279 | kernel = call_program_and_get_last_line_of_output("mindi --findkernel 2> /dev/null");
|
---|
[116] | 280 | }
|
---|
[392] | 281 | // If we didn't get anything back, check whether mindi raised a fatal error
|
---|
[3829] | 282 | if (strcmp(kernel,"") == 0) {
|
---|
[3292] | 283 | mr_asprintf(command, "grep 'Fatal error' %s", MINDI_LOGFILE);
|
---|
[3610] | 284 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[392] | 285 | if (strlen(tmp) > 1) {
|
---|
| 286 | popup_and_OK(tmp);
|
---|
[3191] | 287 | mr_free(tmp);
|
---|
| 288 | mr_free(command);
|
---|
[3292] | 289 | fatal_error("Mindi gave a fatal error. Please check "MINDI_LOGFILE);
|
---|
[392] | 290 | }
|
---|
[3191] | 291 | mr_free(tmp);
|
---|
| 292 | mr_free(command);
|
---|
[3350] | 293 | } else {
|
---|
| 294 | // If we've found the kernel, allow sysadmin to modify it if he wants
|
---|
[3829] | 295 | while ((kernel == NULL) || (strcmp(kernel,"") == 0)) {
|
---|
| 296 | if (!g_text_mode) {
|
---|
| 297 | tmp = mr_popup_and_get_string("Kernel path", "We found this full path name for your kernel. You should just use it in most cases", kernel);
|
---|
| 298 | if (tmp == NULL) {
|
---|
| 299 | fatal_error("Kernel not found. Please specify with the '-k' flag.");
|
---|
| 300 | } else {
|
---|
| 301 | mr_free(kernel);
|
---|
| 302 | kernel = tmp;
|
---|
| 303 | }
|
---|
[3350] | 304 | }
|
---|
| 305 | }
|
---|
[392] | 306 | }
|
---|
[3350] | 307 | log_it("User says kernel is at %s", kernel);
|
---|
| 308 |
|
---|
[116] | 309 | log_it("Calling Mindi with kernel path of '%s'", kernel);
|
---|
[3829] | 310 | return(0);
|
---|
[1] | 311 | }
|
---|
| 312 |
|
---|
| 313 |
|
---|
| 314 | /**
|
---|
| 315 | * Find location of specified executable in user's PATH.
|
---|
| 316 | * @param fname The basename of the executable to search for (e.g. @c afio).
|
---|
| 317 | * @return The full path to the executable, or "" if it does not exist, or NULL if @c file could not be found.
|
---|
| 318 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 319 | * @bug The checks with @c file and @c dirname seem pointless. If @c incoming is "", then you're calling
|
---|
| 320 | * <tt>dirname 2\>/dev/null</tt> or <tt>file 2\>/dev/null | cut -d':' -f1 2\>/dev/null</tt>, which basically amounts
|
---|
| 321 | * to nothing.
|
---|
| 322 | */
|
---|
[116] | 323 | char *find_home_of_exe(char *fname)
|
---|
[1] | 324 | {
|
---|
[116] | 325 | /*@ buffers ********************* */
|
---|
| 326 | static char output[MAX_STR_LEN];
|
---|
[3191] | 327 | char *incoming = NULL;
|
---|
| 328 | char *command = NULL;
|
---|
[1] | 329 |
|
---|
[116] | 330 | /*@******************************* */
|
---|
[1] | 331 |
|
---|
[116] | 332 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
[3191] | 333 | mr_asprintf(command, "which %s 2> /dev/null", fname);
|
---|
[3610] | 334 | incoming = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 335 | mr_free(command);
|
---|
| 336 |
|
---|
[116] | 337 | if (incoming[0] == '\0') {
|
---|
| 338 | if (system("which file > /dev/null 2> /dev/null")) {
|
---|
[3191] | 339 | mr_free(incoming);
|
---|
[116] | 340 | output[0] = '\0';
|
---|
[3194] | 341 | return(NULL); // forget it :)
|
---|
[116] | 342 | }
|
---|
[3191] | 343 | mr_asprintf(command, "file %s 2> /dev/null | cut -d':' -f1 2> /dev/null", incoming);
|
---|
[3194] | 344 | mr_free(incoming);
|
---|
[3191] | 345 |
|
---|
[3610] | 346 | incoming = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 347 | mr_free(command);
|
---|
[116] | 348 | }
|
---|
[3191] | 349 | if (incoming[0] == '\0') {
|
---|
| 350 | mr_asprintf(command, "dirname %s 2> /dev/null", incoming);
|
---|
| 351 | mr_free(incoming);
|
---|
| 352 |
|
---|
[3610] | 353 | incoming = call_program_and_get_last_line_of_output(command);
|
---|
| 354 | mr_free(command);
|
---|
[1] | 355 | }
|
---|
[116] | 356 | strcpy(output, incoming);
|
---|
| 357 | if (output[0] != '\0' && does_file_exist(output)) {
|
---|
[3194] | 358 | log_msg(4, "find_home_of_exe --- Found %s at %s", fname, incoming);
|
---|
[116] | 359 | } else {
|
---|
| 360 | output[0] = '\0';
|
---|
[3194] | 361 | log_msg(4, "find_home_of_exe --- Could not find %s", fname);
|
---|
[116] | 362 | }
|
---|
[3191] | 363 | mr_free(incoming);
|
---|
[116] | 364 | if (!output[0]) {
|
---|
[3194] | 365 | return(NULL);
|
---|
[116] | 366 | } else {
|
---|
[3194] | 367 | return(output);
|
---|
[116] | 368 | }
|
---|
[1] | 369 | }
|
---|
| 370 |
|
---|
| 371 |
|
---|
| 372 | /**
|
---|
| 373 | * Get the last sequence of digits surrounded by non-digits in the first 32k of
|
---|
| 374 | * a file.
|
---|
| 375 | * @param logfile The file to look in.
|
---|
| 376 | * @return The number found, or 0 if none.
|
---|
| 377 | */
|
---|
[116] | 378 | int get_trackno_from_logfile(char *logfile)
|
---|
[1] | 379 | {
|
---|
| 380 |
|
---|
[116] | 381 | /*@ pointers ********************************************************* */
|
---|
| 382 | FILE *fin;
|
---|
[1] | 383 |
|
---|
[116] | 384 | /*@ int ************************************************************** */
|
---|
| 385 | int trackno = 0;
|
---|
| 386 | size_t len = 0;
|
---|
[1] | 387 |
|
---|
[116] | 388 | /*@ buffer ************************************************************ */
|
---|
| 389 | char datablock[32701];
|
---|
[1] | 390 |
|
---|
[116] | 391 | assert_string_is_neither_NULL_nor_zerolength(logfile);
|
---|
| 392 | if (!(fin = fopen(logfile, "r"))) {
|
---|
| 393 | log_OS_error("Unable to open logfile");
|
---|
| 394 | fatal_error("Unable to open logfile to read trackno");
|
---|
| 395 | }
|
---|
| 396 | len = fread(datablock, 1, 32700, fin);
|
---|
| 397 | paranoid_fclose(fin);
|
---|
| 398 | if (len <= 0) {
|
---|
| 399 | return (0);
|
---|
| 400 | }
|
---|
| 401 | for (; len > 0 && !isdigit(datablock[len - 1]); len--);
|
---|
| 402 | datablock[len--] = '\0';
|
---|
| 403 | for (; len > 0 && isdigit(datablock[len - 1]); len--);
|
---|
| 404 | trackno = atoi(datablock + len);
|
---|
| 405 | return (trackno);
|
---|
[1] | 406 | }
|
---|
| 407 |
|
---|
| 408 |
|
---|
| 409 |
|
---|
| 410 |
|
---|
| 411 |
|
---|
| 412 |
|
---|
| 413 |
|
---|
| 414 | /**
|
---|
| 415 | * Get a percentage from the last line of @p filename. We look for the string
|
---|
| 416 | * "% done" on the last line and, if we find it, grab the number before the last % sign.
|
---|
| 417 | * @param filename The file to get the percentage from.
|
---|
| 418 | * @return The percentage found, or 0 for error.
|
---|
| 419 | */
|
---|
[116] | 420 | int grab_percentage_from_last_line_of_file(char *filename)
|
---|
[1] | 421 | {
|
---|
| 422 |
|
---|
[116] | 423 | /*@ buffers ***************************************************** */
|
---|
[3191] | 424 | char *lastline = NULL;
|
---|
| 425 | char *command = NULL;
|
---|
[116] | 426 | /*@ pointers **************************************************** */
|
---|
[3191] | 427 | char *p = NULL;
|
---|
[1] | 428 |
|
---|
[116] | 429 | /*@ int's ******************************************************* */
|
---|
[3191] | 430 | int i = 0;
|
---|
[1] | 431 |
|
---|
[3191] | 432 | for (i = NOOF_ERR_LINES - 1; i >= 0 && !strstr(err_log_lines[i], "% Done") && !strstr(err_log_lines[i], "% done"); i--);
|
---|
[116] | 433 | if (i < 0) {
|
---|
[3191] | 434 | mr_asprintf(command, "tail -n3 %s | grep -Fi \"%c\" | tail -n1 | awk '{print $0;}'", filename, '%');
|
---|
[3610] | 435 | lastline = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 436 | mr_free(command);
|
---|
[116] | 437 | if (!lastline[0]) {
|
---|
[3191] | 438 | mr_free(lastline);
|
---|
[3194] | 439 | return(0);
|
---|
[116] | 440 | }
|
---|
| 441 | } else {
|
---|
[3191] | 442 | mr_asprintf(lastline, "%s", err_log_lines[i]);
|
---|
[1] | 443 | }
|
---|
| 444 |
|
---|
[116] | 445 | p = strrchr(lastline, '%');
|
---|
| 446 | if (p) {
|
---|
| 447 | *p = '\0';
|
---|
[3191] | 448 | } else {
|
---|
| 449 | mr_free(lastline);
|
---|
[3194] | 450 | return(0);
|
---|
[116] | 451 | }
|
---|
[3191] | 452 |
|
---|
| 453 | for (p--; isdigit(*p) && p != lastline; p--);
|
---|
[116] | 454 | if (p != lastline) {
|
---|
| 455 | p++;
|
---|
| 456 | }
|
---|
| 457 | i = atoi(p);
|
---|
[3191] | 458 | mr_free(lastline);
|
---|
[3194] | 459 | return(i);
|
---|
[1] | 460 | }
|
---|
| 461 |
|
---|
| 462 |
|
---|
| 463 |
|
---|
| 464 |
|
---|
| 465 |
|
---|
| 466 | /**
|
---|
| 467 | * Return the last line of @p filename.
|
---|
| 468 | * @param filename The file to get the last line of.
|
---|
| 469 | * @return The last line of the file.
|
---|
| 470 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
| 471 | */
|
---|
[116] | 472 | char *last_line_of_file(char *filename)
|
---|
[1] | 473 | {
|
---|
[116] | 474 | /*@ buffers ***************************************************** */
|
---|
| 475 | static char output[MAX_STR_LEN];
|
---|
[3191] | 476 | char *command = NULL;
|
---|
[3194] | 477 | char *p = NULL;
|
---|
[1] | 478 |
|
---|
[116] | 479 | /*@ pointers **************************************************** */
|
---|
| 480 | FILE *fin;
|
---|
[1] | 481 |
|
---|
[116] | 482 | /*@ end vars **************************************************** */
|
---|
[1] | 483 |
|
---|
[116] | 484 | if (!does_file_exist(filename)) {
|
---|
[3194] | 485 | log_it("Trying to get last line of nonexistent file (%s)", filename);
|
---|
[116] | 486 | output[0] = '\0';
|
---|
| 487 | return (output);
|
---|
| 488 | }
|
---|
[3191] | 489 | mr_asprintf(command, "tail -n1 %s", filename);
|
---|
[116] | 490 | fin = popen(command, "r");
|
---|
[3191] | 491 | mr_free(command);
|
---|
[3060] | 492 | p = fgets(output, MAX_STR_LEN, fin);
|
---|
| 493 | if (p == NULL) {
|
---|
| 494 | // FIXME
|
---|
| 495 | }
|
---|
[116] | 496 | paranoid_pclose(fin);
|
---|
| 497 | while (strlen(output) > 0 && output[strlen(output) - 1] < 32) {
|
---|
| 498 | output[strlen(output) - 1] = '\0';
|
---|
| 499 | }
|
---|
[3194] | 500 | return(output);
|
---|
[1] | 501 | }
|
---|
| 502 |
|
---|
| 503 | /**
|
---|
| 504 | * Get the length of @p filename in bytes.
|
---|
| 505 | * @param filename The file to get the length of.
|
---|
| 506 | * @return The length of the file, or -1 for error.
|
---|
| 507 | */
|
---|
[684] | 508 | off_t length_of_file(char *filename)
|
---|
[1] | 509 | {
|
---|
[116] | 510 | /*@ pointers *************************************************** */
|
---|
| 511 | FILE *fin;
|
---|
[1] | 512 |
|
---|
[116] | 513 | /*@ long long ************************************************* */
|
---|
[684] | 514 | off_t length;
|
---|
[1] | 515 |
|
---|
[116] | 516 | fin = fopen(filename, "r");
|
---|
| 517 | if (!fin) {
|
---|
| 518 | log_it("filename=%s", filename);
|
---|
| 519 | log_OS_error("Unable to openin filename");
|
---|
[3194] | 520 | return(-1);
|
---|
[116] | 521 | }
|
---|
[415] | 522 | fseeko(fin, 0, SEEK_END);
|
---|
[684] | 523 | length = ftello(fin);
|
---|
[116] | 524 | paranoid_fclose(fin);
|
---|
[3194] | 525 | return(length);
|
---|
[1] | 526 | }
|
---|
| 527 |
|
---|
| 528 |
|
---|
| 529 |
|
---|
| 530 | /**
|
---|
| 531 | * ?????
|
---|
| 532 | * @bug I don't know what this function does. However, it seems orphaned, so it should probably be removed.
|
---|
| 533 | */
|
---|
[3191] | 534 | int make_checksum_list_file(char *filelist, char *cksumlist, char *comppath) {
|
---|
[116] | 535 | /*@ pointers **************************************************** */
|
---|
| 536 | FILE *fin;
|
---|
| 537 | FILE *fout;
|
---|
[1] | 538 |
|
---|
[116] | 539 | /*@ int ******************************************************* */
|
---|
| 540 | int percentage;
|
---|
| 541 | int i;
|
---|
| 542 | int counter = 0;
|
---|
[1] | 543 |
|
---|
[116] | 544 | /*@ buffer ****************************************************** */
|
---|
[3191] | 545 | char *stub_fname = NULL;
|
---|
| 546 | char *curr_fname = NULL;
|
---|
| 547 | char *curr_cksum = NULL;
|
---|
| 548 | char *tmp = NULL;
|
---|
[1] | 549 |
|
---|
[116] | 550 | /*@ long [long] ************************************************* */
|
---|
[684] | 551 | off_t filelist_length;
|
---|
| 552 | off_t curr_pos;
|
---|
[116] | 553 | long start_time;
|
---|
| 554 | long current_time;
|
---|
| 555 | long time_taken;
|
---|
| 556 | long time_remaining;
|
---|
[1] | 557 |
|
---|
[116] | 558 | /*@ end vars *************************************************** */
|
---|
[1] | 559 |
|
---|
[116] | 560 | start_time = get_time();
|
---|
| 561 | filelist_length = length_of_file(filelist);
|
---|
[3191] | 562 | log_it("filelist = %s; cksumlist = %s", filelist, cksumlist);
|
---|
| 563 |
|
---|
[116] | 564 | fin = fopen(filelist, "r");
|
---|
| 565 | if (fin == NULL) {
|
---|
| 566 | log_OS_error("Unable to fopen-in filelist");
|
---|
| 567 | log_to_screen("Can't open filelist");
|
---|
[3194] | 568 | return(1);
|
---|
[1] | 569 | }
|
---|
[116] | 570 | fout = fopen(cksumlist, "w");
|
---|
| 571 | if (fout == NULL) {
|
---|
| 572 | log_OS_error("Unable to openout cksumlist");
|
---|
| 573 | paranoid_fclose(fin);
|
---|
[541] | 574 | log_to_screen("Can't open checksum list");
|
---|
[3194] | 575 | return(1);
|
---|
[1] | 576 | }
|
---|
[3191] | 577 | for (mr_getline(stub_fname, fin); !feof(fin); mr_getline(stub_fname, fin)) {
|
---|
[3194] | 578 | i = strlen(stub_fname) - 1;
|
---|
| 579 | if (stub_fname[i] < 32) {
|
---|
[116] | 580 | stub_fname[i] = '\0';
|
---|
| 581 | }
|
---|
[3191] | 582 | mr_asprintf(tmp, "%s%s", comppath, stub_fname);
|
---|
| 583 | mr_free(stub_fname);
|
---|
| 584 |
|
---|
| 585 | mr_asprintf(curr_fname, "%s", tmp + 1);
|
---|
| 586 | mr_free(tmp);
|
---|
| 587 |
|
---|
| 588 | mr_asprintf(curr_cksum, "%s", calc_file_ugly_minichecksum(curr_fname));
|
---|
[116] | 589 | fprintf(fout, "%s\t%s\n", curr_fname, curr_cksum);
|
---|
[3191] | 590 | mr_free(curr_cksum);
|
---|
| 591 |
|
---|
[116] | 592 | if (counter++ > 12) {
|
---|
| 593 | current_time = get_time();
|
---|
| 594 | counter = 0;
|
---|
[3430] | 595 | /* TODO: 37 really ? */
|
---|
[116] | 596 | curr_fname[37] = '\0';
|
---|
[684] | 597 | curr_pos = ftello(fin) / 1024;
|
---|
[116] | 598 | percentage = (int) (curr_pos * 100 / filelist_length);
|
---|
| 599 | time_taken = current_time - start_time;
|
---|
[3191] | 600 | if (percentage != 0) {
|
---|
| 601 | time_remaining = time_taken * 100 / (long) (percentage) - time_taken;
|
---|
| 602 | log_to_screen("%02d%% done %02d:%02d taken %02d:%02d remaining %-37s\r", percentage, (int) (time_taken / 60), (int) (time_taken % 60), (int) (time_remaining / 60), (int) (time_remaining % 60), curr_fname);
|
---|
[116] | 603 | }
|
---|
| 604 | sync();
|
---|
| 605 | }
|
---|
[3191] | 606 | mr_free(curr_fname);
|
---|
[116] | 607 | }
|
---|
[3191] | 608 | mr_free(stub_fname);
|
---|
| 609 |
|
---|
[116] | 610 | paranoid_fclose(fout);
|
---|
| 611 | paranoid_fclose(fin);
|
---|
| 612 | log_it("Done.");
|
---|
[3194] | 613 | return(0);
|
---|
[1] | 614 | }
|
---|
| 615 |
|
---|
| 616 |
|
---|
| 617 | /**
|
---|
| 618 | * Create the directory @p outdir_fname and all parent directories. Equivalent to <tt>mkdir -p</tt>.
|
---|
| 619 | * @param outdir_fname The directory to create.
|
---|
| 620 | * @return The return value of @c mkdir.
|
---|
| 621 | */
|
---|
[3191] | 622 | int make_hole_for_dir(const char *outdir_fname)
|
---|
[1] | 623 | {
|
---|
[3191] | 624 | char *tmp = NULL;
|
---|
[116] | 625 | int res = 0;
|
---|
[1] | 626 |
|
---|
[116] | 627 | assert_string_is_neither_NULL_nor_zerolength(outdir_fname);
|
---|
[3191] | 628 | mr_asprintf(tmp, "mkdir -p %s", outdir_fname);
|
---|
[116] | 629 | res = system(tmp);
|
---|
[3191] | 630 | mr_free(tmp);
|
---|
[3194] | 631 | return(res);
|
---|
[1] | 632 | }
|
---|
| 633 |
|
---|
| 634 |
|
---|
| 635 | /**
|
---|
| 636 | * Create the parent directories of @p outfile_fname.
|
---|
| 637 | * @param outfile_fname The file to make a "hole" for.
|
---|
| 638 | * @return 0, always.
|
---|
| 639 | * @bug Return value unnecessary.
|
---|
| 640 | */
|
---|
[116] | 641 | int make_hole_for_file(char *outfile_fname)
|
---|
[1] | 642 | {
|
---|
[116] | 643 | /*@ buffer ****************************************************** */
|
---|
[3191] | 644 | char *command = NULL;
|
---|
[1] | 645 |
|
---|
[116] | 646 | /*@ int ******************************************************** */
|
---|
| 647 | int res = 0;
|
---|
[1] | 648 |
|
---|
[116] | 649 | /*@ end vars *************************************************** */
|
---|
[1] | 650 |
|
---|
[116] | 651 | assert_string_is_neither_NULL_nor_zerolength(outfile_fname);
|
---|
| 652 | assert(!strstr(outfile_fname, MNT_CDROM));
|
---|
| 653 | assert(!strstr(outfile_fname, "/dev/cdrom"));
|
---|
[3191] | 654 | mr_asprintf(command, "mkdir -p \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 655 | res += system(command);
|
---|
[3191] | 656 | mr_free(command);
|
---|
| 657 |
|
---|
| 658 | mr_asprintf(command, "rmdir \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 659 | res += system(command);
|
---|
[3191] | 660 | mr_free(command);
|
---|
| 661 |
|
---|
| 662 | mr_asprintf(command, "rm -f \"%s\" 2> /dev/null", outfile_fname);
|
---|
[116] | 663 | res += system(command);
|
---|
[3191] | 664 | mr_free(command);
|
---|
| 665 |
|
---|
[116] | 666 | unlink(outfile_fname);
|
---|
[3194] | 667 | return(0);
|
---|
[1] | 668 | }
|
---|
| 669 |
|
---|
| 670 |
|
---|
| 671 | /**
|
---|
| 672 | * Get the number of lines in @p filelist_fname that contain the string @p wildcard.
|
---|
| 673 | * @param filelist_fname The file to search through.
|
---|
| 674 | * @param wildcard The string to search for. This is @e not a shell glob or a regular expression.
|
---|
| 675 | * @return The number of lines matched.
|
---|
| 676 | */
|
---|
[116] | 677 | long noof_lines_that_match_wildcard(char *filelist_fname, char *wildcard)
|
---|
[1] | 678 | {
|
---|
[116] | 679 | /*@ long ******************************************************* */
|
---|
| 680 | long matches = 0;
|
---|
[1] | 681 |
|
---|
[116] | 682 | /*@ pointers *************************************************** */
|
---|
| 683 | FILE *fin;
|
---|
[1] | 684 |
|
---|
[116] | 685 | /*@ buffers **************************************************** */
|
---|
[3191] | 686 | char *incoming = NULL;
|
---|
[1] | 687 |
|
---|
[116] | 688 | /*@ end vars *************************************************** */
|
---|
[1] | 689 |
|
---|
| 690 |
|
---|
[116] | 691 | fin = fopen(filelist_fname, "r");
|
---|
[1] | 692 |
|
---|
[116] | 693 | if (!fin) {
|
---|
| 694 | log_OS_error("Unable to openin filelist_fname");
|
---|
[3194] | 695 | return(0);
|
---|
[1] | 696 | }
|
---|
[3191] | 697 | mr_getline(incoming, fin);
|
---|
| 698 | while (!feof(fin)) {
|
---|
[116] | 699 | if (strstr(incoming, wildcard)) {
|
---|
| 700 | matches++;
|
---|
| 701 | }
|
---|
[3191] | 702 | mr_free(incoming);
|
---|
| 703 | mr_getline(incoming, fin);
|
---|
[116] | 704 | }
|
---|
[3191] | 705 | mr_free(incoming);
|
---|
[116] | 706 | paranoid_fclose(fin);
|
---|
[3194] | 707 | return(matches);
|
---|
[1] | 708 | }
|
---|
| 709 |
|
---|
| 710 |
|
---|
| 711 |
|
---|
| 712 | /**
|
---|
| 713 | * Determine the size (in KB) of @p dev in the mountlist in <tt>tmpdir</tt>/mountlist.txt.
|
---|
| 714 | * @param tmpdir The tempdir where the mountlist is stored.
|
---|
| 715 | * @param dev The device to search for.
|
---|
| 716 | * @return The size of the partition in KB.
|
---|
| 717 | */
|
---|
[116] | 718 | long size_of_partition_in_mountlist_K(char *tmpdir, char *dev)
|
---|
[1] | 719 | {
|
---|
[3191] | 720 | char *command = NULL;
|
---|
| 721 | char *mountlist = NULL;
|
---|
| 722 | char *sz_res = NULL;
|
---|
[116] | 723 | long file_len_K;
|
---|
| 724 |
|
---|
[3191] | 725 | mr_asprintf(mountlist, "%s/mountlist.txt", tmpdir);
|
---|
| 726 | mr_asprintf(command, "grep \"%s \" %s/mountlist.txt | head -n1 | awk '{print $4}'", dev, tmpdir);
|
---|
| 727 | mr_free(mountlist);
|
---|
| 728 |
|
---|
[116] | 729 | log_it(command);
|
---|
[3610] | 730 | sz_res = call_program_and_get_last_line_of_output(command);
|
---|
[116] | 731 | file_len_K = atol(sz_res);
|
---|
| 732 | log_msg(4, "%s --> %s --> %ld", command, sz_res, file_len_K);
|
---|
[3191] | 733 | mr_free(sz_res);
|
---|
| 734 | mr_free(command);
|
---|
| 735 |
|
---|
[3194] | 736 | return(file_len_K);
|
---|
[1] | 737 | }
|
---|
| 738 |
|
---|
| 739 | /**
|
---|
| 740 | * Calculate the total size (in KB) of all the biggiefiles in this backup.
|
---|
| 741 | * @param bkpinfo The backup information structure. Only the @c bkpinfo->tmpdir field is used.
|
---|
| 742 | * @return The total size of all biggiefiles in KB.
|
---|
| 743 | */
|
---|
[1645] | 744 | long size_of_all_biggiefiles_K()
|
---|
[1] | 745 | {
|
---|
[116] | 746 | /*@ buffers ***************************************************** */
|
---|
[3191] | 747 | char *fname = NULL;
|
---|
| 748 | char *biggielist = NULL;
|
---|
| 749 | char *tmp = NULL;
|
---|
| 750 | char *command = NULL;
|
---|
[1] | 751 |
|
---|
[116] | 752 | /*@ long ******************************************************** */
|
---|
| 753 | long scratchL = 0;
|
---|
| 754 | long file_len_K;
|
---|
[1] | 755 |
|
---|
[116] | 756 | /*@ pointers *************************************************** */
|
---|
| 757 | FILE *fin = NULL;
|
---|
[1] | 758 |
|
---|
[116] | 759 | /*@ end vars *************************************************** */
|
---|
[1] | 760 |
|
---|
[116] | 761 | log_it("Calculating size of all biggiefiles (in total)");
|
---|
[3191] | 762 | mr_asprintf(biggielist, "%s/biggielist.txt", bkpinfo->tmpdir);
|
---|
[116] | 763 | log_it("biggielist = %s", biggielist);
|
---|
[3191] | 764 | fin = fopen(biggielist, "r");
|
---|
| 765 | mr_free(biggielist);
|
---|
| 766 |
|
---|
| 767 | if (!(fin)) {
|
---|
| 768 | log_OS_error("Cannot open biggielist. OK, so estimate is based on filesets only.");
|
---|
[116] | 769 | } else {
|
---|
| 770 | log_msg(4, "Reading it...");
|
---|
[3191] | 771 | for (mr_getline(fname, fin); !feof(fin); mr_getline(fname, fin)) {
|
---|
[116] | 772 | if (fname[strlen(fname) - 1] <= 32) {
|
---|
| 773 | fname[strlen(fname) - 1] = '\0';
|
---|
| 774 | }
|
---|
| 775 | if (0 == strncmp(fname, "/dev/", 5)) {
|
---|
[296] | 776 | if (is_dev_an_NTFS_dev(fname)) {
|
---|
| 777 | if ( !find_home_of_exe("ntfsresize")) {
|
---|
[3191] | 778 | mr_free(tmp);
|
---|
| 779 | mr_free(fname);
|
---|
[296] | 780 | fatal_error("ntfsresize not found");
|
---|
| 781 | }
|
---|
[3191] | 782 | mr_free(tmp);
|
---|
| 783 |
|
---|
| 784 | mr_asprintf(command, "ntfsresize --force --info %s|grep '^You might resize at '|cut -d' ' -f5", fname);
|
---|
[296] | 785 | log_it("command = %s", command);
|
---|
[3610] | 786 | tmp = call_program_and_get_last_line_of_output(command);
|
---|
[3191] | 787 | mr_free(command);
|
---|
| 788 |
|
---|
[296] | 789 | log_it("res of it = %s", tmp);
|
---|
| 790 | file_len_K = atoll(tmp) / 1024L;
|
---|
[3191] | 791 | mr_free(tmp);
|
---|
[296] | 792 | } else {
|
---|
| 793 | file_len_K = get_phys_size_of_drive(fname) * 1024L;
|
---|
| 794 | }
|
---|
[116] | 795 | } else {
|
---|
[3430] | 796 | /* TODO: more than long here ??? */
|
---|
[116] | 797 | file_len_K = (long) (length_of_file(fname) / 1024);
|
---|
| 798 | }
|
---|
| 799 | if (file_len_K > 0) {
|
---|
| 800 | scratchL += file_len_K;
|
---|
| 801 | log_msg(4, "%s --> %ld K", fname, file_len_K);
|
---|
| 802 | }
|
---|
[3191] | 803 | log_msg(4, "After adding %s, scratchL+%ld now equals %ld", fname, file_len_K, scratchL);
|
---|
[116] | 804 | if (feof(fin)) {
|
---|
| 805 | break;
|
---|
| 806 | }
|
---|
[3191] | 807 | mr_free(fname);
|
---|
[116] | 808 | }
|
---|
[3191] | 809 | mr_free(fname);
|
---|
[1] | 810 | }
|
---|
[116] | 811 | log_it("Closing...");
|
---|
| 812 | paranoid_fclose(fin);
|
---|
| 813 | log_it("Finished calculating total size of all biggiefiles");
|
---|
[3206] | 814 | log_it("Total size of all biggiefiles = %ld Kb", scratchL);
|
---|
[3194] | 815 | return(scratchL);
|
---|
[1] | 816 | }
|
---|
| 817 |
|
---|
| 818 | /**
|
---|
| 819 | * Determine the amount of space (in KB) occupied by a mounted CD.
|
---|
| 820 | * This can also be used to find the space used for other directories.
|
---|
| 821 | * @param mountpt The mountpoint/directory to check.
|
---|
| 822 | * @return The amount of space occupied in KB.
|
---|
| 823 | */
|
---|
[116] | 824 | long long space_occupied_by_cd(char *mountpt)
|
---|
[1] | 825 | {
|
---|
[116] | 826 | /*@ buffer ****************************************************** */
|
---|
[3191] | 827 | char *tmp = NULL;
|
---|
| 828 | char *command = NULL;
|
---|
[116] | 829 | long long llres;
|
---|
| 830 | /*@ pointers **************************************************** */
|
---|
| 831 | char *p;
|
---|
| 832 | FILE *fin;
|
---|
[1] | 833 |
|
---|
[116] | 834 | /*@ end vars *************************************************** */
|
---|
[1] | 835 |
|
---|
[3191] | 836 | mr_asprintf(command, "du -sk %s", mountpt);
|
---|
[816] | 837 | errno = 0;
|
---|
[116] | 838 | fin = popen(command, "r");
|
---|
[816] | 839 | if (errno) {
|
---|
| 840 | log_it("popen() FAILED: command=%s, mountpt=%s, fin=%d, errno=%d, strerror=%s", command, mountpt, fin, errno, strerror(errno));
|
---|
| 841 | llres = 0;
|
---|
| 842 | } else {
|
---|
[3191] | 843 | mr_getline(tmp, fin);
|
---|
[816] | 844 | paranoid_pclose(fin);
|
---|
| 845 | p = strchr(tmp, '\t');
|
---|
| 846 | if (p) {
|
---|
| 847 | *p = '\0';
|
---|
| 848 | }
|
---|
| 849 | for (p = tmp, llres = 0; *p != '\0'; p++) {
|
---|
| 850 | llres *= 10;
|
---|
| 851 | llres += (int) (*p - '0');
|
---|
| 852 | }
|
---|
[3191] | 853 | mr_free(tmp);
|
---|
[116] | 854 | }
|
---|
[3191] | 855 | mr_free(command);
|
---|
[816] | 856 |
|
---|
[3194] | 857 | return(llres);
|
---|
[1] | 858 | }
|
---|
| 859 |
|
---|
| 860 |
|
---|
| 861 | /**
|
---|
| 862 | * Update a CRC checksum to include another character.
|
---|
| 863 | * @param crc The original CRC checksum.
|
---|
| 864 | * @param c The character to add.
|
---|
| 865 | * @return The new CRC checksum.
|
---|
| 866 | * @ingroup utilityGroup
|
---|
| 867 | */
|
---|
[116] | 868 | unsigned int updcrc(unsigned int crc, unsigned int c)
|
---|
[1] | 869 | {
|
---|
[116] | 870 | unsigned int tmp;
|
---|
| 871 | tmp = (crc >> 8) ^ c;
|
---|
| 872 | crc = (crc << 8) ^ crctttab[tmp & 255];
|
---|
[3194] | 873 | return(crc);
|
---|
[1] | 874 | }
|
---|
| 875 |
|
---|
| 876 | /**
|
---|
| 877 | * Update a reverse CRC checksum to include another character.
|
---|
| 878 | * @param crc The original CRC checksum.
|
---|
| 879 | * @param c The character to add.
|
---|
| 880 | * @return The new CRC checksum.
|
---|
| 881 | * @ingroup utilityGroup
|
---|
| 882 | */
|
---|
[116] | 883 | unsigned int updcrcr(unsigned int crc, unsigned int c)
|
---|
[1] | 884 | {
|
---|
[116] | 885 | unsigned int tmp;
|
---|
| 886 | tmp = crc ^ c;
|
---|
| 887 | crc = (crc >> 8) ^ crc16tab[tmp & 0xff];
|
---|
[3194] | 888 | return(crc);
|
---|
[1] | 889 | }
|
---|
| 890 |
|
---|
| 891 |
|
---|
| 892 |
|
---|
| 893 |
|
---|
| 894 | /**
|
---|
| 895 | * Check for an executable on the user's system; write a message to the
|
---|
| 896 | * screen and the log if we can't find it.
|
---|
| 897 | * @param fname The executable basename to look for.
|
---|
| 898 | * @return 0 if it's found, nonzero if not.
|
---|
| 899 | */
|
---|
[116] | 900 | int whine_if_not_found(char *fname)
|
---|
[1] | 901 | {
|
---|
[116] | 902 | /*@ buffers *** */
|
---|
[3191] | 903 | char *command = NULL;
|
---|
| 904 | int res = 0;
|
---|
[1] | 905 |
|
---|
[3191] | 906 | mr_asprintf(command, "which %s > /dev/null 2> /dev/null", fname);
|
---|
| 907 | res = system(command);
|
---|
| 908 | mr_free(command);
|
---|
[1] | 909 |
|
---|
[3191] | 910 | if (res) {
|
---|
| 911 | log_to_screen("Please install '%s'. I cannot find it on your system.", fname);
|
---|
| 912 | log_to_screen("There may be hyperlink at http://www.mondorescue.org which");
|
---|
[541] | 913 | log_to_screen("will take you to the relevant (missing) package.");
|
---|
[3194] | 914 | return(1);
|
---|
[116] | 915 | } else {
|
---|
[3194] | 916 | return(0);
|
---|
[116] | 917 | }
|
---|
[1] | 918 | }
|
---|
| 919 |
|
---|
| 920 |
|
---|
| 921 |
|
---|
| 922 |
|
---|
| 923 |
|
---|
| 924 |
|
---|
| 925 | /**
|
---|
| 926 | * Create a data file at @p fname containing @p contents.
|
---|
| 927 | * The data actually can be multiple lines, despite the name.
|
---|
| 928 | * @param fname The file to create.
|
---|
| 929 | * @param contents The data to put in it.
|
---|
| 930 | * @return 0 for success, 1 for failure.
|
---|
| 931 | */
|
---|
[116] | 932 | int write_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 933 | {
|
---|
[116] | 934 | /*@ pointers *************************************************** */
|
---|
| 935 | FILE *fout;
|
---|
| 936 | int res = 0;
|
---|
[1] | 937 |
|
---|
[116] | 938 | /*@ end vars *************************************************** */
|
---|
[1] | 939 |
|
---|
[116] | 940 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 941 | if (!contents) {
|
---|
| 942 | log_it("%d: Warning - writing NULL to %s", __LINE__, fname);
|
---|
| 943 | }
|
---|
| 944 | if (!(fout = fopen(fname, "w"))) {
|
---|
| 945 | log_it("fname=%s");
|
---|
| 946 | log_OS_error("Unable to openout fname");
|
---|
| 947 | return (1);
|
---|
| 948 | }
|
---|
| 949 | fprintf(fout, "%s\n", contents);
|
---|
| 950 | paranoid_fclose(fout);
|
---|
| 951 | return (res);
|
---|
[1] | 952 | }
|
---|
| 953 |
|
---|
| 954 |
|
---|
| 955 |
|
---|
| 956 | /**
|
---|
| 957 | * Read @p fname into @p contents.
|
---|
| 958 | * @param fname The file to read.
|
---|
| 959 | * @param contents Where to put its contents.
|
---|
| 960 | * @return 0 for success, nonzero for failure.
|
---|
| 961 | */
|
---|
[116] | 962 | int read_one_liner_data_file(char *fname, char *contents)
|
---|
[1] | 963 | {
|
---|
[116] | 964 | /*@ pointers *************************************************** */
|
---|
| 965 | FILE *fin;
|
---|
| 966 | int res = 0;
|
---|
| 967 | int i;
|
---|
[1] | 968 |
|
---|
[116] | 969 | /*@ end vars *************************************************** */
|
---|
[1] | 970 |
|
---|
[116] | 971 | assert_string_is_neither_NULL_nor_zerolength(fname);
|
---|
| 972 | if (!contents) {
|
---|
| 973 | log_it("%d: Warning - reading NULL from %s", __LINE__, fname);
|
---|
| 974 | }
|
---|
| 975 | if (!(fin = fopen(fname, "r"))) {
|
---|
| 976 | log_it("fname=%s", fname);
|
---|
| 977 | log_OS_error("Unable to openin fname");
|
---|
[3194] | 978 | return(1);
|
---|
[116] | 979 | }
|
---|
[3060] | 980 | res = fscanf(fin, "%s\n", contents);
|
---|
[116] | 981 | i = strlen(contents);
|
---|
| 982 | if (i > 0 && contents[i - 1] < 32) {
|
---|
| 983 | contents[i - 1] = '\0';
|
---|
| 984 | }
|
---|
| 985 | paranoid_fclose(fin);
|
---|
[3060] | 986 | res = 0;
|
---|
[3194] | 987 | return(res);
|
---|
[1] | 988 | }
|
---|
| 989 |
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 |
|
---|
| 993 |
|
---|
| 994 |
|
---|
| 995 | /**
|
---|
| 996 | * Copy the files that Mondo/Mindi need to run to the scratchdir or tempdir.
|
---|
| 997 | * Currently this includes: copy Mondo's home directory to scratchdir, untar "mondo_home/payload.tgz"
|
---|
| 998 | * if it exists, copy LAST-FILELIST-NUMBER to scratchdir, copy mondorestore
|
---|
| 999 | * and post-nuke.tgz (if it exists) to tmpdir, and run "hostname > scratchdir/HOSTNAME".
|
---|
| 1000 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1001 | * - @c bkpinfo->postnuke_tarball
|
---|
| 1002 | * - @c bkpinfo->scratchdir
|
---|
| 1003 | * - @c bkpinfo->tmpdir
|
---|
| 1004 | */
|
---|
[1645] | 1005 | void copy_mondo_and_mindi_stuff_to_scratchdir()
|
---|
[1] | 1006 | {
|
---|
[116] | 1007 | /*@ Char buffers ** */
|
---|
[3191] | 1008 | char *command = NULL;
|
---|
| 1009 | char *tmp = NULL;
|
---|
[3613] | 1010 | char *old_pwd = NULL;
|
---|
[3191] | 1011 | int res = 0;
|
---|
[1] | 1012 |
|
---|
[3191] | 1013 | mvaddstr_and_log_it(g_currentY, 0, "Copying Mondo's core files to the scratch directory");
|
---|
[1] | 1014 |
|
---|
[116] | 1015 | log_msg(4, "g_mondo_home='%s'", g_mondo_home);
|
---|
| 1016 | if (strlen(g_mondo_home) < 2) {
|
---|
| 1017 | find_and_store_mondoarchives_home(g_mondo_home);
|
---|
| 1018 | }
|
---|
[3191] | 1019 | mr_asprintf(command, CP_BIN " --parents -pRdf %s %s", g_mondo_home, bkpinfo->scratchdir);
|
---|
[1] | 1020 |
|
---|
[116] | 1021 | log_msg(4, "command = %s", command);
|
---|
[3191] | 1022 | res = run_program_and_log_output(command, 1);
|
---|
| 1023 | mr_free(command);
|
---|
| 1024 |
|
---|
| 1025 | if (res) {
|
---|
[116] | 1026 | fatal_error("Failed to copy Mondo's stuff to scratchdir");
|
---|
| 1027 | }
|
---|
[1] | 1028 |
|
---|
[3191] | 1029 | mr_asprintf(tmp, "%s/payload.tgz", g_mondo_home);
|
---|
[116] | 1030 | if (does_file_exist(tmp)) {
|
---|
[3191] | 1031 | log_it("Untarring payload %s to scratchdir %s", tmp, bkpinfo->scratchdir);
|
---|
[3613] | 1032 | old_pwd = mr_getcwd();
|
---|
[3060] | 1033 | if (chdir(bkpinfo->scratchdir)) {
|
---|
| 1034 | // FIXME
|
---|
| 1035 | }
|
---|
[3191] | 1036 | mr_asprintf(command, "tar -zxvf %s", tmp);
|
---|
| 1037 | res = run_program_and_log_output(command, FALSE);
|
---|
| 1038 | if (res) {
|
---|
| 1039 | mr_free(command);
|
---|
| 1040 | mr_free(tmp);
|
---|
[116] | 1041 | fatal_error("Failed to untar payload");
|
---|
| 1042 | }
|
---|
[3191] | 1043 | mr_free(command);
|
---|
[3060] | 1044 | if (chdir(old_pwd)) {
|
---|
| 1045 | // FIXME
|
---|
| 1046 | }
|
---|
[3613] | 1047 | mr_free(old_pwd);
|
---|
[116] | 1048 | }
|
---|
[3191] | 1049 | mr_free(tmp);
|
---|
[1] | 1050 |
|
---|
[3191] | 1051 | mr_asprintf(command, "cp -f %s/LAST-FILELIST-NUMBER %s", bkpinfo->tmpdir, bkpinfo->scratchdir);
|
---|
[1] | 1052 |
|
---|
[116] | 1053 | if (run_program_and_log_output(command, FALSE)) {
|
---|
| 1054 | fatal_error("Failed to copy LAST-FILELIST-NUMBER to scratchdir");
|
---|
| 1055 | }
|
---|
[3191] | 1056 | mr_free(command);
|
---|
[1] | 1057 |
|
---|
[3610] | 1058 | tmp = call_program_and_get_last_line_of_output("which mondorestore");
|
---|
[116] | 1059 | if (!tmp[0]) {
|
---|
[3191] | 1060 | mr_free(tmp);
|
---|
| 1061 | fatal_error("'which mondorestore' returned null. Where's your mondorestore? `which` can't find it. That's odd. Did you install mondorestore?");
|
---|
[116] | 1062 | }
|
---|
[3191] | 1063 |
|
---|
| 1064 | mr_asprintf(command, "cp -f %s %s", tmp, bkpinfo->tmpdir);
|
---|
| 1065 | mr_free(tmp);
|
---|
| 1066 | res = run_program_and_log_output(command, FALSE);
|
---|
| 1067 | if (res) {
|
---|
| 1068 | mr_free(command);
|
---|
[116] | 1069 | fatal_error("Failed to copy mondorestore to tmpdir");
|
---|
| 1070 | }
|
---|
[3191] | 1071 | mr_free(command);
|
---|
[1] | 1072 |
|
---|
[3191] | 1073 | mr_asprintf(command, "hostname > %s/HOSTNAME", bkpinfo->scratchdir);
|
---|
[116] | 1074 | paranoid_system(command);
|
---|
[3191] | 1075 | mr_free(command);
|
---|
[1] | 1076 |
|
---|
[3826] | 1077 | if (bkpinfo->postnuke_tarball != NULL) {
|
---|
[3191] | 1078 | mr_asprintf(command, "cp -f %s %s/post-nuke.tgz", bkpinfo->postnuke_tarball, bkpinfo->tmpdir);
|
---|
| 1079 | res = run_program_and_log_output(command, FALSE);
|
---|
| 1080 | mr_free(command);
|
---|
| 1081 |
|
---|
| 1082 | if (res) {
|
---|
[116] | 1083 | fatal_error("Unable to copy post-nuke tarball to tmpdir");
|
---|
| 1084 | }
|
---|
| 1085 | }
|
---|
[1] | 1086 |
|
---|
[116] | 1087 | mvaddstr_and_log_it(g_currentY++, 74, "Done.");
|
---|
[1] | 1088 | }
|
---|
| 1089 |
|
---|
| 1090 |
|
---|
| 1091 | /**
|
---|
| 1092 | * Determine the approximate number of media that the backup will take up,
|
---|
| 1093 | * and tell the user. The uncompressed size is estimated as size_of_all_biggiefiles_K()
|
---|
| 1094 | * plus (noof_sets x bkpinfo->optimal_set_size). The compression factor is estimated as
|
---|
| 1095 | * 2/3 for LZO and 1/2 for bzip2. The data is not saved anywhere. If there are any
|
---|
| 1096 | * "imagedevs", the estimate is not shown as it will be wildly inaccurate.
|
---|
| 1097 | * If there are more than 50 media estimated, the estimate will not be shown.
|
---|
| 1098 | * @param bkpinfo The backup information structure. Fields used:
|
---|
| 1099 | * - @c bkpinfo->backup_media_type
|
---|
| 1100 | * - @c bkpinfo->media_size
|
---|
| 1101 | * - @c bkpinfo->optimal_set_size
|
---|
| 1102 | * - @c bkpinfo->use_lzo
|
---|
| 1103 | * @param noof_sets The number of filesets created.
|
---|
| 1104 | * @ingroup archiveGroup
|
---|
| 1105 | */
|
---|
| 1106 | void
|
---|
[1645] | 1107 | estimate_noof_media_required(long noof_sets)
|
---|
[1] | 1108 | {
|
---|
[116] | 1109 | /*@ buffers *************** */
|
---|
[3191] | 1110 | char *tmp = NULL;
|
---|
[3611] | 1111 | char *tmp1 = NULL;
|
---|
[2242] | 1112 | char *mds = NULL;
|
---|
[1] | 1113 |
|
---|
[116] | 1114 | /*@ long long ************* */
|
---|
| 1115 | long long scratchLL;
|
---|
[1] | 1116 |
|
---|
[3150] | 1117 | if (bkpinfo->media_size <= 0) {
|
---|
[116] | 1118 | log_to_screen("Number of media required: UNKNOWN");
|
---|
| 1119 | return;
|
---|
| 1120 | }
|
---|
[1] | 1121 |
|
---|
[116] | 1122 | log_it("Estimating number of media required...");
|
---|
[3191] | 1123 | scratchLL = (long long) (noof_sets) * (long long) (bkpinfo->optimal_set_size) + (long long) (size_of_all_biggiefiles_K());
|
---|
[3150] | 1124 | scratchLL = (scratchLL / 1024) / bkpinfo->media_size;
|
---|
[116] | 1125 | scratchLL++;
|
---|
| 1126 | if (bkpinfo->use_lzo) {
|
---|
| 1127 | scratchLL = (scratchLL * 2) / 3;
|
---|
[1002] | 1128 | } else if (bkpinfo->use_gzip) {
|
---|
[998] | 1129 | scratchLL = (scratchLL * 2) / 3;
|
---|
[3191] | 1130 | } else if (bkpinfo->use_lzma) {
|
---|
| 1131 | scratchLL = (scratchLL * 2) / 3;
|
---|
[116] | 1132 | } else {
|
---|
| 1133 | scratchLL = scratchLL / 2;
|
---|
| 1134 | }
|
---|
| 1135 | if (!scratchLL) {
|
---|
| 1136 | scratchLL++;
|
---|
| 1137 | }
|
---|
[3611] | 1138 | tmp1 = number_to_text((int) (scratchLL + 1));
|
---|
[116] | 1139 | if (scratchLL <= 1) {
|
---|
[2242] | 1140 | mds = media_descriptor_string(bkpinfo->backup_media_type);
|
---|
[3191] | 1141 | mr_asprintf(tmp, "Your backup will probably occupy a single %s. Maybe two.", mds);
|
---|
[2242] | 1142 | mr_free(mds);
|
---|
[3611] | 1143 | } else if (scratchLL > 5) {
|
---|
| 1144 | mr_asprintf(tmp, "Your backup will occupy one meeeeellion media! (maybe %s)", tmp1);
|
---|
[116] | 1145 | } else {
|
---|
[3611] | 1146 | mr_asprintf(tmp, "Your backup will occupy approximately %s media.", tmp1);
|
---|
[116] | 1147 | }
|
---|
[3191] | 1148 | if (scratchLL < 50) {
|
---|
[116] | 1149 | log_to_screen(tmp);
|
---|
| 1150 | }
|
---|
[3611] | 1151 | mr_free(tmp1);
|
---|
[3191] | 1152 | mr_free(tmp);
|
---|
[1] | 1153 | }
|
---|
| 1154 |
|
---|
| 1155 |
|
---|
| 1156 | /**
|
---|
| 1157 | * Determine whether a file is compressed. This is done
|
---|
| 1158 | * by reading through the "do-not-compress-these" file distributed with Mondo.
|
---|
| 1159 | * @param filename The file to check.
|
---|
| 1160 | * @return TRUE if it's compressed, FALSE if not.
|
---|
| 1161 | */
|
---|
[116] | 1162 | bool is_this_file_compressed(char *filename)
|
---|
[1] | 1163 | {
|
---|
[3191] | 1164 | char *do_not_compress_these = NULL;
|
---|
| 1165 | char *tmp = NULL;
|
---|
[116] | 1166 | char *p;
|
---|
[1236] | 1167 | char *q = NULL;
|
---|
[1] | 1168 |
|
---|
[1236] | 1169 | q = strrchr(filename, '.');
|
---|
| 1170 | if (q == NULL) {
|
---|
| 1171 | return (FALSE);
|
---|
| 1172 | }
|
---|
| 1173 |
|
---|
[3191] | 1174 | mr_asprintf(tmp, "%s/do-not-compress-these", g_mondo_home);
|
---|
[116] | 1175 | if (!does_file_exist(tmp)) {
|
---|
[3191] | 1176 | mr_free(tmp);
|
---|
[116] | 1177 | return (FALSE);
|
---|
| 1178 | }
|
---|
[3430] | 1179 | /* TODO: This is just plain WRONG !! */
|
---|
[3191] | 1180 | mr_asprintf(do_not_compress_these,"%s", last_line_of_file(tmp));
|
---|
| 1181 | mr_free(tmp);
|
---|
[1236] | 1182 |
|
---|
[116] | 1183 | for (p = do_not_compress_these; p != NULL; p++) {
|
---|
[3191] | 1184 | mr_asprintf(tmp, "%s", p);
|
---|
[116] | 1185 | if (strchr(tmp, ' ')) {
|
---|
| 1186 | *(strchr(tmp, ' ')) = '\0';
|
---|
| 1187 | }
|
---|
[1236] | 1188 | if (!strcmp(q, tmp)) {
|
---|
[3191] | 1189 | mr_free(tmp);
|
---|
| 1190 | mr_free(do_not_compress_these);
|
---|
[116] | 1191 | return (TRUE);
|
---|
| 1192 | }
|
---|
| 1193 | if (!(p = strchr(p, ' '))) {
|
---|
| 1194 | break;
|
---|
| 1195 | }
|
---|
[3191] | 1196 | mr_free(tmp);
|
---|
[116] | 1197 | }
|
---|
[3191] | 1198 | mr_free(do_not_compress_these);
|
---|
[116] | 1199 | return (FALSE);
|
---|
[1] | 1200 | }
|
---|
| 1201 |
|
---|
| 1202 | /* @} - end fileGroup */
|
---|