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