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