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