xref: /freebsd/usr.bin/patch/inp.c (revision 66df505066f51e6d8411b966765d828817f88971)
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.44 2015/07/26 14:32:19 millert 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 <paths.h>
40 #include <spawn.h>
41 #include <stddef.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "common.h"
49 #include "util.h"
50 #include "pch.h"
51 #include "inp.h"
52 
53 
54 /* Input-file-with-indexable-lines abstract type */
55 
56 static size_t	i_size;		/* size of the input file */
57 static char	*i_womp;	/* plan a buffer for entire file */
58 static char	**i_ptr;	/* pointers to lines in i_womp */
59 static char	empty_line[] = { '\0' };
60 
61 static int	tifd = -1;	/* plan b virtual string array */
62 static char	*tibuf[2];	/* plan b buffers */
63 static LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
64 static size_t	lines_per_buf;	/* how many lines per buffer */
65 static size_t	tibuflen;	/* plan b buffer length */
66 static size_t	tireclen;	/* length of records in tmp file */
67 
68 static bool	rev_in_string(const char *);
69 static bool	reallocate_lines(size_t *);
70 
71 /* returns false if insufficient memory */
72 static bool	plan_a(const char *);
73 
74 static void	plan_b(const char *);
75 
76 /* New patch--prepare to edit another file. */
77 
78 void
79 re_input(void)
80 {
81 	if (using_plan_a) {
82 		free(i_ptr);
83 		i_ptr = NULL;
84 		if (i_womp != NULL) {
85 			munmap(i_womp, i_size);
86 			i_womp = NULL;
87 		}
88 		i_size = 0;
89 	} else {
90 		using_plan_a = true;	/* maybe the next one is smaller */
91 		close(tifd);
92 		tifd = -1;
93 		free(tibuf[0]);
94 		free(tibuf[1]);
95 		tibuf[0] = tibuf[1] = NULL;
96 		tiline[0] = tiline[1] = -1;
97 		tireclen = 0;
98 	}
99 }
100 
101 /* Construct the line index, somehow or other. */
102 
103 void
104 scan_input(const char *filename)
105 {
106 	if (!plan_a(filename))
107 		plan_b(filename);
108 	if (verbose) {
109 		say("Patching file %s using Plan %s...\n", filename,
110 		    (using_plan_a ? "A" : "B"));
111 	}
112 }
113 
114 static bool
115 reallocate_lines(size_t *lines_allocated)
116 {
117 	char	**p;
118 	size_t	new_size;
119 
120 	new_size = *lines_allocated * 3 / 2;
121 	p = reallocarray(i_ptr, new_size + 2, sizeof(char *));
122 	if (p == NULL) {	/* shucks, it was a near thing */
123 		munmap(i_womp, i_size);
124 		i_womp = NULL;
125 		free(i_ptr);
126 		i_ptr = NULL;
127 		*lines_allocated = 0;
128 		return false;
129 	}
130 	*lines_allocated = new_size;
131 	i_ptr = p;
132 	return true;
133 }
134 
135 /* Try keeping everything in memory. */
136 
137 static bool
138 plan_a(const char *filename)
139 {
140 	int		ifd, statfailed;
141 	char		*p, *s;
142 	struct stat	filestat;
143 	ptrdiff_t	sz;
144 	size_t		i;
145 	size_t		iline, lines_allocated;
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)
172 		fatal("can't find %s\n", filename);
173 	filemode = filestat.st_mode;
174 	if (!S_ISREG(filemode))
175 		fatal("%s is not a normal file--can't patch\n", filename);
176 	if ((uint64_t)filestat.st_size > SIZE_MAX) {
177 		say("block too large to mmap\n");
178 		return false;
179 	}
180 	i_size = (size_t)filestat.st_size;
181 	if (out_of_mem) {
182 		set_hunkmax();	/* make sure dynamic arrays are allocated */
183 		out_of_mem = false;
184 		return false;	/* force plan b because plan a bombed */
185 	}
186 	if ((ifd = open(filename, O_RDONLY)) < 0)
187 		pfatal("can't open file %s", filename);
188 
189 	if (i_size) {
190 		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
191 		if (i_womp == MAP_FAILED) {
192 			perror("mmap failed");
193 			i_womp = NULL;
194 			close(ifd);
195 			return false;
196 		}
197 	} else {
198 		i_womp = NULL;
199 	}
200 
201 	close(ifd);
202 	if (i_size)
203 		madvise(i_womp, i_size, MADV_SEQUENTIAL);
204 
205 	/* estimate the number of lines */
206 	lines_allocated = i_size / 25;
207 	if (lines_allocated < 100)
208 		lines_allocated = 100;
209 
210 	if (!reallocate_lines(&lines_allocated))
211 		return false;
212 
213 	/* now scan the buffer and build pointer array */
214 	iline = 1;
215 	i_ptr[iline] = i_womp;
216 	/*
217 	 * Testing for NUL here actively breaks files that innocently use NUL
218 	 * for other reasons. mmap(2) succeeded, just scan the whole buffer.
219 	 */
220 	for (s = i_womp, i = 0; i < i_size; s++, i++) {
221 		if (*s == '\n') {
222 			if (iline == lines_allocated) {
223 				if (!reallocate_lines(&lines_allocated))
224 					return false;
225 			}
226 			/* these are NOT NUL terminated */
227 			i_ptr[++iline] = s + 1;
228 		}
229 	}
230 	/* if the last line contains no EOL, append one */
231 	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
232 		last_line_missing_eol = true;
233 		/* fix last line */
234 		sz = s - i_ptr[iline];
235 		p = malloc(sz + 1);
236 		if (p == NULL) {
237 			free(i_ptr);
238 			i_ptr = NULL;
239 			munmap(i_womp, i_size);
240 			i_womp = NULL;
241 			return false;
242 		}
243 
244 		memcpy(p, i_ptr[iline], sz);
245 		p[sz] = '\n';
246 		i_ptr[iline] = p;
247 		/* count the extra line and make it point to some valid mem */
248 		i_ptr[++iline] = empty_line;
249 	} else
250 		last_line_missing_eol = false;
251 
252 	input_lines = iline - 1;
253 
254 	/* now check for revision, if any */
255 
256 	if (revision != NULL) {
257 		if (i_womp == NULL || !rev_in_string(i_womp)) {
258 			if (force) {
259 				if (verbose)
260 					say("Warning: this file doesn't appear "
261 					    "to be the %s version--patching anyway.\n",
262 					    revision);
263 			} else if (batch) {
264 				fatal("this file doesn't appear to be the "
265 				    "%s version--aborting.\n",
266 				    revision);
267 			} else {
268 				ask("This file doesn't appear to be the "
269 				    "%s version--patch anyway? [n] ",
270 				    revision);
271 				if (*buf != 'y')
272 					fatal("aborted\n");
273 			}
274 		} else if (verbose)
275 			say("Good.  This file appears to be the %s version.\n",
276 			    revision);
277 	}
278 	return true;		/* plan a will work */
279 }
280 
281 /* Keep (virtually) nothing in memory. */
282 
283 static void
284 plan_b(const char *filename)
285 {
286 	FILE	*ifp;
287 	size_t	i, j, len, maxlen;
288 	char	*lbuf = NULL, *p;
289 	bool	found_revision = (revision == NULL);
290 
291 	using_plan_a = false;
292 	if ((ifp = fopen(filename, "r")) == NULL)
293 		pfatal("can't open file %s", filename);
294 	unlink(TMPINNAME);
295 	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
296 		pfatal("can't open file %s", TMPINNAME);
297 	len = 0;
298 	maxlen = 1;
299 	while ((p = fgetln(ifp, &len)) != NULL) {
300 		if (p[len - 1] == '\n')
301 			p[len - 1] = '\0';
302 		else {
303 			/* EOF without EOL, copy and add the NUL */
304 			if ((lbuf = malloc(len + 1)) == NULL)
305 				fatal("out of memory\n");
306 			memcpy(lbuf, p, len);
307 			lbuf[len] = '\0';
308 			p = lbuf;
309 
310 			last_line_missing_eol = true;
311 			len++;
312 		}
313 		if (revision != NULL && !found_revision && rev_in_string(p))
314 			found_revision = true;
315 		if (len > maxlen)
316 			maxlen = len;   /* find longest line */
317 	}
318 	free(lbuf);
319 	if (ferror(ifp))
320 		pfatal("can't read file %s", filename);
321 
322 	if (revision != NULL) {
323 		if (!found_revision) {
324 			if (force) {
325 				if (verbose)
326 					say("Warning: this file doesn't appear "
327 					    "to be the %s version--patching anyway.\n",
328 					    revision);
329 			} else if (batch) {
330 				fatal("this file doesn't appear to be the "
331 				    "%s version--aborting.\n",
332 				    revision);
333 			} else {
334 				ask("This file doesn't appear to be the %s "
335 				    "version--patch anyway? [n] ",
336 				    revision);
337 				if (*buf != 'y')
338 					fatal("aborted\n");
339 			}
340 		} else if (verbose)
341 			say("Good.  This file appears to be the %s version.\n",
342 			    revision);
343 	}
344 	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
345 	tireclen = maxlen;
346 	tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE;
347 	lines_per_buf = tibuflen / maxlen;
348 	tibuf[0] = malloc(tibuflen + 1);
349 	if (tibuf[0] == NULL)
350 		fatal("out of memory\n");
351 	tibuf[1] = malloc(tibuflen + 1);
352 	if (tibuf[1] == NULL)
353 		fatal("out of memory\n");
354 	for (i = 1;; i++) {
355 		p = tibuf[0] + maxlen * (i % lines_per_buf);
356 		if (i % lines_per_buf == 0)	/* new block */
357 			if (write(tifd, tibuf[0], tibuflen) !=
358 			    (ssize_t) tibuflen)
359 				pfatal("can't write temp file");
360 		if (fgets(p, maxlen + 1, ifp) == NULL) {
361 			input_lines = i - 1;
362 			if (i % lines_per_buf != 0)
363 				if (write(tifd, tibuf[0], tibuflen) !=
364 				    (ssize_t) tibuflen)
365 					pfatal("can't write temp file");
366 			break;
367 		}
368 		j = strlen(p);
369 		/* These are '\n' terminated strings, so no need to add a NUL */
370 		if (j == 0 || p[j - 1] != '\n')
371 			p[j] = '\n';
372 	}
373 	fclose(ifp);
374 	close(tifd);
375 	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
376 		pfatal("can't reopen file %s", TMPINNAME);
377 }
378 
379 /*
380  * Fetch a line from the input file, \n terminated, not necessarily \0.
381  */
382 char *
383 ifetch(LINENUM line, int whichbuf)
384 {
385 	if (line < 1 || line > input_lines) {
386 		if (warn_on_invalid_line) {
387 			say("No such line %ld in input file, ignoring\n", line);
388 			warn_on_invalid_line = false;
389 		}
390 		return NULL;
391 	}
392 	if (using_plan_a)
393 		return i_ptr[line];
394 	else {
395 		LINENUM	offline = line % lines_per_buf;
396 		LINENUM	baseline = line - offline;
397 
398 		if (tiline[0] == baseline)
399 			whichbuf = 0;
400 		else if (tiline[1] == baseline)
401 			whichbuf = 1;
402 		else {
403 			tiline[whichbuf] = baseline;
404 
405 			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
406 			    tibuflen), SEEK_SET) < 0)
407 				pfatal("cannot seek in the temporary input file");
408 
409 			if (read(tifd, tibuf[whichbuf], tibuflen) !=
410 			    (ssize_t) tibuflen)
411 				pfatal("error reading tmp file %s", TMPINNAME);
412 		}
413 		return tibuf[whichbuf] + (tireclen * offline);
414 	}
415 }
416 
417 /*
418  * True if the string argument contains the revision number we want.
419  */
420 static bool
421 rev_in_string(const char *string)
422 {
423 	const char	*s;
424 	size_t		patlen;
425 
426 	if (revision == NULL)
427 		return true;
428 	patlen = strlen(revision);
429 	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
430 		return true;
431 	for (s = string; *s; s++) {
432 		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
433 		    isspace((unsigned char)s[patlen + 1])) {
434 			return true;
435 		}
436 	}
437 	return false;
438 }
439