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