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