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