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