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