inp.c revision 287223
1221716Sattilio/*-
2221716Sattilio * Copyright 1986, Larry Wall
3221716Sattilio *
4221716Sattilio * Redistribution and use in source and binary forms, with or without
5221716Sattilio * modification, are permitted provided that the following condition is met:
6221716Sattilio * 1. Redistributions of source code must retain the above copyright notice,
7221716Sattilio * this condition and the following disclaimer.
8221716Sattilio *
9221716Sattilio * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
10221716Sattilio * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
11221716Sattilio * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12221716Sattilio * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
13221716Sattilio * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14221716Sattilio * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15221716Sattilio * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16221716Sattilio * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
17221716Sattilio * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
18221716Sattilio * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
19221716Sattilio * SUCH DAMAGE.
20221716Sattilio *
21221716Sattilio * patch - a program to apply diffs to original files
22221716Sattilio *
23221716Sattilio * -C option added in 1998, original code by Marc Espie, based on FreeBSD
24221716Sattilio * behaviour
25221716Sattilio *
26221716Sattilio * $OpenBSD: inp.c,v 1.36 2012/04/10 14:46:34 ajacoutot Exp $
27221716Sattilio * $FreeBSD: stable/10/usr.bin/patch/inp.c 287223 2015-08-27 21:52:09Z delphij $
28221716Sattilio */
29221716Sattilio
30221716Sattilio#include <sys/types.h>
31221716Sattilio#include <sys/file.h>
32221716Sattilio#include <sys/stat.h>
33221716Sattilio#include <sys/mman.h>
34221716Sattilio#include <sys/wait.h>
35221716Sattilio
36221716Sattilio#include <ctype.h>
37221716Sattilio#include <errno.h>
38221716Sattilio#include <libgen.h>
39221716Sattilio#include <paths.h>
40221716Sattilio#include <spawn.h>
41221716Sattilio#include <stddef.h>
42221716Sattilio#include <stdint.h>
43221716Sattilio#include <stdio.h>
44221716Sattilio#include <stdlib.h>
45221716Sattilio#include <string.h>
46221716Sattilio#include <unistd.h>
47221716Sattilio
48221716Sattilio#include "common.h"
49221716Sattilio#include "util.h"
50221716Sattilio#include "pch.h"
51221716Sattilio#include "inp.h"
52221716Sattilio
53221716Sattilio
54221716Sattilio/* Input-file-with-indexable-lines abstract type */
55221716Sattilio
56221716Sattiliostatic size_t	i_size;		/* size of the input file */
57221716Sattiliostatic char	*i_womp;	/* plan a buffer for entire file */
58221716Sattiliostatic char	**i_ptr;	/* pointers to lines in i_womp */
59221716Sattiliostatic char	empty_line[] = { '\0' };
60221716Sattilio
61221716Sattiliostatic int	tifd = -1;	/* plan b virtual string array */
62221716Sattiliostatic char	*tibuf[2];	/* plan b buffers */
63221716Sattiliostatic LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
64221716Sattiliostatic LINENUM	lines_per_buf;	/* how many lines per buffer */
65221716Sattiliostatic int	tireclen;	/* length of records in tmp file */
66221716Sattilio
67221716Sattiliostatic bool	rev_in_string(const char *);
68221716Sattiliostatic bool	reallocate_lines(size_t *);
69221716Sattilio
70221716Sattilio/* returns false if insufficient memory */
71221716Sattiliostatic bool	plan_a(const char *);
72221716Sattilio
73221716Sattiliostatic void	plan_b(const char *);
74221716Sattilio
75221716Sattilio/* New patch--prepare to edit another file. */
76221716Sattilio
77221716Sattiliovoid
78221716Sattiliore_input(void)
79221716Sattilio{
80221716Sattilio	if (using_plan_a) {
81221716Sattilio		free(i_ptr);
82221716Sattilio		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 = realloc(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, pstat;
140	char		*p, *s, lbuf[INITLINELEN];
141	struct stat	filestat;
142	ptrdiff_t	sz;
143	size_t		i;
144	size_t		iline, lines_allocated;
145	pid_t		pid;
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		char	*argp[4] = { NULL };
184		posix_spawn_file_actions_t file_actions;
185
186		tmp_filename1 = strdup(filename);
187		tmp_filename2 = strdup(filename);
188		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
189			fatal("strdupping filename");
190
191		filebase = basename(tmp_filename1);
192		filedir = dirname(tmp_filename2);
193
194		memset(argp, 0, sizeof(argp));
195
196#define try(f, a1, a2, a3) \
197	(snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0)
198
199		/*
200		 * else we can't write to it but it's not under a version
201		 * control system, so just proceed.
202		 */
203		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
204		    try("%s/RCS/%s%s", filedir, filebase, "") ||
205		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
206			if (!statfailed) {
207				if ((filestat.st_mode & 0222) != 0)
208					/* The owner can write to it.  */
209					fatal("file %s seems to be locked "
210					    "by somebody else under RCS\n",
211					    filename);
212				/*
213				 * It might be checked out unlocked.  See if
214				 * it's safe to check out the default version
215				 * locked.
216				 */
217				if (verbose)
218					say("Comparing file %s to default "
219					    "RCS version...\n", filename);
220
221				argp[0] = __DECONST(char *, RCSDIFF);
222				argp[1] = __DECONST(char *, filename);
223				posix_spawn_file_actions_init(&file_actions);
224				posix_spawn_file_actions_addopen(&file_actions,
225				    STDOUT_FILENO, _PATH_DEVNULL, O_WRONLY, 0);
226				if (posix_spawn(&pid, RCSDIFF, &file_actions,
227				    NULL, argp, NULL) == 0) {
228					pid = waitpid(pid, &pstat, 0);
229					if (pid == -1 || WEXITSTATUS(pstat) != 0)
230						fatal("can't check out file %s: "
231						    "differs from default RCS version\n",
232						    filename);
233				} else
234					fatal("posix_spawn: %s\n", strerror(errno));
235				posix_spawn_file_actions_destroy(&file_actions);
236			}
237
238			if (verbose)
239				say("Checking out file %s from RCS...\n",
240				    filename);
241
242			argp[0] = __DECONST(char *, CHECKOUT);
243			argp[1] = __DECONST(char *, "-l");
244			argp[2] = __DECONST(char *, filename);
245			if (posix_spawn(&pid, CHECKOUT, NULL, NULL, argp,
246			    NULL) == 0) {
247				pid = waitpid(pid, &pstat, 0);
248				if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
249				    stat(filename, &filestat))
250					fatal("can't check out file %s from RCS\n",
251					    filename);
252			} else
253				fatal("posix_spawn: %s\n", strerror(errno));
254		} else if (statfailed) {
255			fatal("can't find %s\n", filename);
256		}
257		free(tmp_filename1);
258		free(tmp_filename2);
259	}
260
261	filemode = filestat.st_mode;
262	if (!S_ISREG(filemode))
263		fatal("%s is not a normal file--can't patch\n", filename);
264	if ((uint64_t)filestat.st_size > SIZE_MAX) {
265		say("block too large to mmap\n");
266		return false;
267	}
268	i_size = (size_t)filestat.st_size;
269	if (out_of_mem) {
270		set_hunkmax();	/* make sure dynamic arrays are allocated */
271		out_of_mem = false;
272		return false;	/* force plan b because plan a bombed */
273	}
274	if ((ifd = open(filename, O_RDONLY)) < 0)
275		pfatal("can't open file %s", filename);
276
277	if (i_size) {
278		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
279		if (i_womp == MAP_FAILED) {
280			perror("mmap failed");
281			i_womp = NULL;
282			close(ifd);
283			return false;
284		}
285	} else {
286		i_womp = NULL;
287	}
288
289	close(ifd);
290	if (i_size)
291		madvise(i_womp, i_size, MADV_SEQUENTIAL);
292
293	/* estimate the number of lines */
294	lines_allocated = i_size / 25;
295	if (lines_allocated < 100)
296		lines_allocated = 100;
297
298	if (!reallocate_lines(&lines_allocated))
299		return false;
300
301	/* now scan the buffer and build pointer array */
302	iline = 1;
303	i_ptr[iline] = i_womp;
304	/* test for NUL too, to maintain the behavior of the original code */
305	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
306		if (*s == '\n') {
307			if (iline == lines_allocated) {
308				if (!reallocate_lines(&lines_allocated))
309					return false;
310			}
311			/* these are NOT NUL terminated */
312			i_ptr[++iline] = s + 1;
313		}
314	}
315	/* if the last line contains no EOL, append one */
316	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
317		last_line_missing_eol = true;
318		/* fix last line */
319		sz = s - i_ptr[iline];
320		p = malloc(sz + 1);
321		if (p == NULL) {
322			free(i_ptr);
323			i_ptr = NULL;
324			munmap(i_womp, i_size);
325			i_womp = NULL;
326			return false;
327		}
328
329		memcpy(p, i_ptr[iline], sz);
330		p[sz] = '\n';
331		i_ptr[iline] = p;
332		/* count the extra line and make it point to some valid mem */
333		i_ptr[++iline] = empty_line;
334	} else
335		last_line_missing_eol = false;
336
337	input_lines = iline - 1;
338
339	/* now check for revision, if any */
340
341	if (revision != NULL) {
342		if (!rev_in_string(i_womp)) {
343			if (force) {
344				if (verbose)
345					say("Warning: this file doesn't appear "
346					    "to be the %s version--patching anyway.\n",
347					    revision);
348			} else if (batch) {
349				fatal("this file doesn't appear to be the "
350				    "%s version--aborting.\n",
351				    revision);
352			} else {
353				ask("This file doesn't appear to be the "
354				    "%s version--patch anyway? [n] ",
355				    revision);
356				if (*buf != 'y')
357					fatal("aborted\n");
358			}
359		} else if (verbose)
360			say("Good.  This file appears to be the %s version.\n",
361			    revision);
362	}
363	return true;		/* plan a will work */
364}
365
366/* Keep (virtually) nothing in memory. */
367
368static void
369plan_b(const char *filename)
370{
371	FILE	*ifp;
372	size_t	i = 0, j, maxlen = 1;
373	char	*p;
374	bool	found_revision = (revision == NULL);
375
376	using_plan_a = false;
377	if ((ifp = fopen(filename, "r")) == NULL)
378		pfatal("can't open file %s", filename);
379	unlink(TMPINNAME);
380	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
381		pfatal("can't open file %s", TMPINNAME);
382	while (fgets(buf, buf_size, ifp) != NULL) {
383		if (revision != NULL && !found_revision && rev_in_string(buf))
384			found_revision = true;
385		if ((i = strlen(buf)) > maxlen)
386			maxlen = i;	/* find longest line */
387	}
388	last_line_missing_eol = i > 0 && buf[i - 1] != '\n';
389	if (last_line_missing_eol && maxlen == i)
390		maxlen++;
391
392	if (revision != NULL) {
393		if (!found_revision) {
394			if (force) {
395				if (verbose)
396					say("Warning: this file doesn't appear "
397					    "to be the %s version--patching anyway.\n",
398					    revision);
399			} else if (batch) {
400				fatal("this file doesn't appear to be the "
401				    "%s version--aborting.\n",
402				    revision);
403			} else {
404				ask("This file doesn't appear to be the %s "
405				    "version--patch anyway? [n] ",
406				    revision);
407				if (*buf != 'y')
408					fatal("aborted\n");
409			}
410		} else if (verbose)
411			say("Good.  This file appears to be the %s version.\n",
412			    revision);
413	}
414	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
415	lines_per_buf = BUFFERSIZE / maxlen;
416	tireclen = maxlen;
417	tibuf[0] = malloc(BUFFERSIZE + 1);
418	if (tibuf[0] == NULL)
419		fatal("out of memory\n");
420	tibuf[1] = malloc(BUFFERSIZE + 1);
421	if (tibuf[1] == NULL)
422		fatal("out of memory\n");
423	for (i = 1;; i++) {
424		p = tibuf[0] + maxlen * (i % lines_per_buf);
425		if (i % lines_per_buf == 0)	/* new block */
426			if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
427				pfatal("can't write temp file");
428		if (fgets(p, maxlen + 1, ifp) == NULL) {
429			input_lines = i - 1;
430			if (i % lines_per_buf != 0)
431				if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
432					pfatal("can't write temp file");
433			break;
434		}
435		j = strlen(p);
436		/* These are '\n' terminated strings, so no need to add a NUL */
437		if (j == 0 || p[j - 1] != '\n')
438			p[j] = '\n';
439	}
440	fclose(ifp);
441	close(tifd);
442	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
443		pfatal("can't reopen file %s", TMPINNAME);
444}
445
446/*
447 * Fetch a line from the input file, \n terminated, not necessarily \0.
448 */
449char *
450ifetch(LINENUM line, int whichbuf)
451{
452	if (line < 1 || line > input_lines) {
453		if (warn_on_invalid_line) {
454			say("No such line %ld in input file, ignoring\n", line);
455			warn_on_invalid_line = false;
456		}
457		return NULL;
458	}
459	if (using_plan_a)
460		return i_ptr[line];
461	else {
462		LINENUM	offline = line % lines_per_buf;
463		LINENUM	baseline = line - offline;
464
465		if (tiline[0] == baseline)
466			whichbuf = 0;
467		else if (tiline[1] == baseline)
468			whichbuf = 1;
469		else {
470			tiline[whichbuf] = baseline;
471
472			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
473			    BUFFERSIZE), SEEK_SET) < 0)
474				pfatal("cannot seek in the temporary input file");
475
476			if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
477				pfatal("error reading tmp file %s", TMPINNAME);
478		}
479		return tibuf[whichbuf] + (tireclen * offline);
480	}
481}
482
483/*
484 * True if the string argument contains the revision number we want.
485 */
486static bool
487rev_in_string(const char *string)
488{
489	const char	*s;
490	size_t		patlen;
491
492	if (revision == NULL)
493		return true;
494	patlen = strlen(revision);
495	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
496		return true;
497	for (s = string; *s; s++) {
498		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
499		    isspace((unsigned char)s[patlen + 1])) {
500			return true;
501		}
502	}
503	return false;
504}
505