1 /*- 2 * Copyright 1986, Larry Wall 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following condition is met: 6 * 1. Redistributions of source code must retain the above copyright notice, 7 * this condition and the following disclaimer. 8 * 9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY 10 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 11 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 12 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 13 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 14 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 15 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 16 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 17 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 18 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 19 * SUCH DAMAGE. 20 * 21 * patch - a program to apply diffs to original files 22 * 23 * -C option added in 1998, original code by Marc Espie, based on FreeBSD 24 * behaviour 25 * 26 * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $ 27 * $FreeBSD$ 28 */ 29 30 #include <sys/types.h> 31 #include <sys/file.h> 32 #include <sys/stat.h> 33 #include <sys/mman.h> 34 #include <sys/wait.h> 35 36 #include <ctype.h> 37 #include <errno.h> 38 #include <libgen.h> 39 #include <stddef.h> 40 #include <stdint.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 46 #include "common.h" 47 #include "util.h" 48 #include "pch.h" 49 #include "inp.h" 50 51 52 /* Input-file-with-indexable-lines abstract type */ 53 54 static size_t i_size; /* size of the input file */ 55 static char *i_womp; /* plan a buffer for entire file */ 56 static char **i_ptr; /* pointers to lines in i_womp */ 57 static char empty_line[] = { '\0' }; 58 59 static int tifd = -1; /* plan b virtual string array */ 60 static char *tibuf[2]; /* plan b buffers */ 61 static LINENUM tiline[2] = {-1, -1}; /* 1st line in each buffer */ 62 static size_t lines_per_buf; /* how many lines per buffer */ 63 static size_t tibuflen; /* plan b buffer length */ 64 static size_t tireclen; /* length of records in tmp file */ 65 66 static bool rev_in_string(const char *); 67 static bool reallocate_lines(size_t *); 68 69 /* returns false if insufficient memory */ 70 static bool plan_a(const char *); 71 72 static void plan_b(const char *); 73 74 /* New patch--prepare to edit another file. */ 75 76 void 77 re_input(void) 78 { 79 if (using_plan_a) { 80 free(i_ptr); 81 i_ptr = NULL; 82 if (i_womp != NULL) { 83 munmap(i_womp, i_size); 84 i_womp = NULL; 85 } 86 i_size = 0; 87 } else { 88 using_plan_a = true; /* maybe the next one is smaller */ 89 close(tifd); 90 tifd = -1; 91 free(tibuf[0]); 92 free(tibuf[1]); 93 tibuf[0] = tibuf[1] = NULL; 94 tiline[0] = tiline[1] = -1; 95 tireclen = 0; 96 } 97 } 98 99 /* Construct the line index, somehow or other. */ 100 101 void 102 scan_input(const char *filename) 103 { 104 if (!plan_a(filename)) 105 plan_b(filename); 106 if (verbose) { 107 say("Patching file %s using Plan %s...\n", filename, 108 (using_plan_a ? "A" : "B")); 109 } 110 } 111 112 static bool 113 reallocate_lines(size_t *lines_allocated) 114 { 115 char **p; 116 size_t new_size; 117 118 new_size = *lines_allocated * 3 / 2; 119 p = realloc(i_ptr, (new_size + 2) * sizeof(char *)); 120 if (p == NULL) { /* shucks, it was a near thing */ 121 munmap(i_womp, i_size); 122 i_womp = NULL; 123 free(i_ptr); 124 i_ptr = NULL; 125 *lines_allocated = 0; 126 return false; 127 } 128 *lines_allocated = new_size; 129 i_ptr = p; 130 return true; 131 } 132 133 /* Try keeping everything in memory. */ 134 135 static bool 136 plan_a(const char *filename) 137 { 138 int ifd, statfailed, devnull, pstat; 139 char *p, *s, lbuf[INITLINELEN]; 140 struct stat filestat; 141 ptrdiff_t sz; 142 size_t i; 143 size_t iline, lines_allocated; 144 pid_t pid; 145 char *argp[4] = {NULL}; 146 147 #ifdef DEBUGGING 148 if (debug & 8) 149 return false; 150 #endif 151 152 if (filename == NULL || *filename == '\0') 153 return false; 154 155 statfailed = stat(filename, &filestat); 156 if (statfailed && ok_to_create_file) { 157 if (verbose) 158 say("(Creating file %s...)\n", filename); 159 160 /* 161 * in check_patch case, we still display `Creating file' even 162 * though we're not. The rule is that -C should be as similar 163 * to normal patch behavior as possible 164 */ 165 if (check_only) 166 return true; 167 makedirs(filename, true); 168 close(creat(filename, 0666)); 169 statfailed = stat(filename, &filestat); 170 } 171 if (statfailed && check_only) 172 fatal("%s not found, -C mode, can't probe further\n", filename); 173 /* For nonexistent or read-only files, look for RCS versions. */ 174 175 if (statfailed || 176 /* No one can write to it. */ 177 (filestat.st_mode & 0222) == 0 || 178 /* I can't write to it. */ 179 ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) { 180 char *filebase, *filedir; 181 struct stat cstat; 182 char *tmp_filename1, *tmp_filename2; 183 184 tmp_filename1 = strdup(filename); 185 tmp_filename2 = strdup(filename); 186 if (tmp_filename1 == NULL || tmp_filename2 == NULL) 187 fatal("strdupping filename"); 188 189 filebase = basename(tmp_filename1); 190 filedir = dirname(tmp_filename2); 191 192 #define try(f, a1, a2, a3) \ 193 (snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0) 194 195 /* 196 * else we can't write to it but it's not under a version 197 * control system, so just proceed. 198 */ 199 if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) || 200 try("%s/RCS/%s%s", filedir, filebase, "") || 201 try("%s/%s%s", filedir, filebase, RCSSUFFIX)) { 202 if (!statfailed) { 203 if ((filestat.st_mode & 0222) != 0) 204 /* The owner can write to it. */ 205 fatal("file %s seems to be locked " 206 "by somebody else under RCS\n", 207 filename); 208 /* 209 * It might be checked out unlocked. See if 210 * it's safe to check out the default version 211 * locked. 212 */ 213 if (verbose) 214 say("Comparing file %s to default " 215 "RCS version...\n", filename); 216 217 switch (pid = fork()) { 218 case -1: 219 fatal("can't fork: %s\n", 220 strerror(errno)); 221 case 0: 222 devnull = open("/dev/null", O_RDONLY); 223 if (devnull == -1) { 224 fatal("can't open /dev/null: %s", 225 strerror(errno)); 226 } 227 (void)dup2(devnull, STDOUT_FILENO); 228 argp[0] = strdup(RCSDIFF); 229 argp[1] = strdup(filename); 230 execv(RCSDIFF, argp); 231 exit(127); 232 } 233 pid = waitpid(pid, &pstat, 0); 234 if (pid == -1 || WEXITSTATUS(pstat) != 0) { 235 fatal("can't check out file %s: " 236 "differs from default RCS version\n", 237 filename); 238 } 239 } 240 241 if (verbose) 242 say("Checking out file %s from RCS...\n", 243 filename); 244 245 switch (pid = fork()) { 246 case -1: 247 fatal("can't fork: %s\n", strerror(errno)); 248 case 0: 249 argp[0] = strdup(CHECKOUT); 250 argp[1] = strdup("-l"); 251 argp[2] = strdup(filename); 252 execv(CHECKOUT, argp); 253 exit(127); 254 } 255 pid = waitpid(pid, &pstat, 0); 256 if (pid == -1 || WEXITSTATUS(pstat) != 0 || 257 stat(filename, &filestat)) { 258 fatal("can't check out file %s from RCS\n", 259 filename); 260 } 261 } else if (statfailed) { 262 fatal("can't find %s\n", filename); 263 } 264 free(tmp_filename1); 265 free(tmp_filename2); 266 } 267 268 filemode = filestat.st_mode; 269 if (!S_ISREG(filemode)) 270 fatal("%s is not a normal file--can't patch\n", filename); 271 if ((uint64_t)filestat.st_size > SIZE_MAX) { 272 say("block too large to mmap\n"); 273 return false; 274 } 275 i_size = (size_t)filestat.st_size; 276 if (out_of_mem) { 277 set_hunkmax(); /* make sure dynamic arrays are allocated */ 278 out_of_mem = false; 279 return false; /* force plan b because plan a bombed */ 280 } 281 if ((ifd = open(filename, O_RDONLY)) < 0) 282 pfatal("can't open file %s", filename); 283 284 if (i_size) { 285 i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0); 286 if (i_womp == MAP_FAILED) { 287 perror("mmap failed"); 288 i_womp = NULL; 289 close(ifd); 290 return false; 291 } 292 } else { 293 i_womp = NULL; 294 } 295 296 close(ifd); 297 if (i_size) 298 madvise(i_womp, i_size, MADV_SEQUENTIAL); 299 300 /* estimate the number of lines */ 301 lines_allocated = i_size / 25; 302 if (lines_allocated < 100) 303 lines_allocated = 100; 304 305 if (!reallocate_lines(&lines_allocated)) 306 return false; 307 308 /* now scan the buffer and build pointer array */ 309 iline = 1; 310 i_ptr[iline] = i_womp; 311 /* test for NUL too, to maintain the behavior of the original code */ 312 for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) { 313 if (*s == '\n') { 314 if (iline == lines_allocated) { 315 if (!reallocate_lines(&lines_allocated)) 316 return false; 317 } 318 /* these are NOT NUL terminated */ 319 i_ptr[++iline] = s + 1; 320 } 321 } 322 /* if the last line contains no EOL, append one */ 323 if (i_size > 0 && i_womp[i_size - 1] != '\n') { 324 last_line_missing_eol = true; 325 /* fix last line */ 326 sz = s - i_ptr[iline]; 327 p = malloc(sz + 1); 328 if (p == NULL) { 329 free(i_ptr); 330 i_ptr = NULL; 331 munmap(i_womp, i_size); 332 i_womp = NULL; 333 return false; 334 } 335 336 memcpy(p, i_ptr[iline], sz); 337 p[sz] = '\n'; 338 i_ptr[iline] = p; 339 /* count the extra line and make it point to some valid mem */ 340 i_ptr[++iline] = empty_line; 341 } else 342 last_line_missing_eol = false; 343 344 input_lines = iline - 1; 345 346 /* now check for revision, if any */ 347 348 if (revision != NULL) { 349 if (i_womp == NULL || !rev_in_string(i_womp)) { 350 if (force) { 351 if (verbose) 352 say("Warning: this file doesn't appear " 353 "to be the %s version--patching anyway.\n", 354 revision); 355 } else if (batch) { 356 fatal("this file doesn't appear to be the " 357 "%s version--aborting.\n", 358 revision); 359 } else { 360 ask("This file doesn't appear to be the " 361 "%s version--patch anyway? [n] ", 362 revision); 363 if (*buf != 'y') 364 fatal("aborted\n"); 365 } 366 } else if (verbose) 367 say("Good. This file appears to be the %s version.\n", 368 revision); 369 } 370 return true; /* plan a will work */ 371 } 372 373 /* Keep (virtually) nothing in memory. */ 374 375 static void 376 plan_b(const char *filename) 377 { 378 FILE *ifp; 379 size_t i = 0, j, len, maxlen = 1; 380 char *lbuf = NULL, *p; 381 bool found_revision = (revision == NULL); 382 383 using_plan_a = false; 384 if ((ifp = fopen(filename, "r")) == NULL) 385 pfatal("can't open file %s", filename); 386 unlink(TMPINNAME); 387 if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0) 388 pfatal("can't open file %s", TMPINNAME); 389 while ((p = fgetln(ifp, &len)) != NULL) { 390 if (p[len - 1] == '\n') 391 p[len - 1] = '\0'; 392 else { 393 /* EOF without EOL, copy and add the NUL */ 394 if ((lbuf = malloc(len + 1)) == NULL) 395 fatal("out of memory\n"); 396 memcpy(lbuf, p, len); 397 lbuf[len] = '\0'; 398 p = lbuf; 399 400 last_line_missing_eol = true; 401 len++; 402 } 403 if (revision != NULL && !found_revision && rev_in_string(p)) 404 found_revision = true; 405 if (len > maxlen) 406 maxlen = len; /* find longest line */ 407 } 408 free(lbuf); 409 if (ferror(ifp)) 410 pfatal("can't read file %s", filename); 411 412 if (revision != NULL) { 413 if (!found_revision) { 414 if (force) { 415 if (verbose) 416 say("Warning: this file doesn't appear " 417 "to be the %s version--patching anyway.\n", 418 revision); 419 } else if (batch) { 420 fatal("this file doesn't appear to be the " 421 "%s version--aborting.\n", 422 revision); 423 } else { 424 ask("This file doesn't appear to be the %s " 425 "version--patch anyway? [n] ", 426 revision); 427 if (*buf != 'y') 428 fatal("aborted\n"); 429 } 430 } else if (verbose) 431 say("Good. This file appears to be the %s version.\n", 432 revision); 433 } 434 fseek(ifp, 0L, SEEK_SET); /* rewind file */ 435 tireclen = maxlen; 436 tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE; 437 lines_per_buf = tibuflen / maxlen; 438 tibuf[0] = malloc(tibuflen + 1); 439 if (tibuf[0] == NULL) 440 fatal("out of memory\n"); 441 tibuf[1] = malloc(tibuflen + 1); 442 if (tibuf[1] == NULL) 443 fatal("out of memory\n"); 444 for (i = 1;; i++) { 445 p = tibuf[0] + maxlen * (i % lines_per_buf); 446 if (i % lines_per_buf == 0) /* new block */ 447 if (write(tifd, tibuf[0], tibuflen) != 448 (ssize_t) tibuflen) 449 pfatal("can't write temp file"); 450 if (fgets(p, maxlen + 1, ifp) == NULL) { 451 input_lines = i - 1; 452 if (i % lines_per_buf != 0) 453 if (write(tifd, tibuf[0], tibuflen) != 454 (ssize_t) tibuflen) 455 pfatal("can't write temp file"); 456 break; 457 } 458 j = strlen(p); 459 /* These are '\n' terminated strings, so no need to add a NUL */ 460 if (j == 0 || p[j - 1] != '\n') 461 p[j] = '\n'; 462 } 463 fclose(ifp); 464 close(tifd); 465 if ((tifd = open(TMPINNAME, O_RDONLY)) < 0) 466 pfatal("can't reopen file %s", TMPINNAME); 467 } 468 469 /* 470 * Fetch a line from the input file, \n terminated, not necessarily \0. 471 */ 472 char * 473 ifetch(LINENUM line, int whichbuf) 474 { 475 if (line < 1 || line > input_lines) { 476 if (warn_on_invalid_line) { 477 say("No such line %ld in input file, ignoring\n", line); 478 warn_on_invalid_line = false; 479 } 480 return NULL; 481 } 482 if (using_plan_a) 483 return i_ptr[line]; 484 else { 485 LINENUM offline = line % lines_per_buf; 486 LINENUM baseline = line - offline; 487 488 if (tiline[0] == baseline) 489 whichbuf = 0; 490 else if (tiline[1] == baseline) 491 whichbuf = 1; 492 else { 493 tiline[whichbuf] = baseline; 494 495 if (lseek(tifd, (off_t) (baseline / lines_per_buf * 496 tibuflen), SEEK_SET) < 0) 497 pfatal("cannot seek in the temporary input file"); 498 499 if (read(tifd, tibuf[whichbuf], tibuflen) != 500 (ssize_t) tibuflen) 501 pfatal("error reading tmp file %s", TMPINNAME); 502 } 503 return tibuf[whichbuf] + (tireclen * offline); 504 } 505 } 506 507 /* 508 * True if the string argument contains the revision number we want. 509 */ 510 static bool 511 rev_in_string(const char *string) 512 { 513 const char *s; 514 size_t patlen; 515 516 if (revision == NULL) 517 return true; 518 patlen = strlen(revision); 519 if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen])) 520 return true; 521 for (s = string; *s; s++) { 522 if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) && 523 isspace((unsigned char)s[patlen + 1])) { 524 return true; 525 } 526 } 527 return false; 528 } 529