xref: /freebsd/usr.bin/patch/inp.c (revision 3e5645b78f476816ca3b5acc28b29bbafbb9c444)
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.36 2012/04/10 14:46:34 ajacoutot 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 
35 #include <ctype.h>
36 #include <libgen.h>
37 #include <stddef.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "common.h"
45 #include "util.h"
46 #include "pch.h"
47 #include "inp.h"
48 
49 
50 /* Input-file-with-indexable-lines abstract type */
51 
52 static size_t	i_size;		/* size of the input file */
53 static char	*i_womp;	/* plan a buffer for entire file */
54 static char	**i_ptr;	/* pointers to lines in i_womp */
55 static char	empty_line[] = { '\0' };
56 
57 static int	tifd = -1;	/* plan b virtual string array */
58 static char	*tibuf[2];	/* plan b buffers */
59 static LINENUM	tiline[2] = {-1, -1};	/* 1st line in each buffer */
60 static size_t	lines_per_buf;	/* how many lines per buffer */
61 static size_t	tibuflen;	/* plan b buffer length */
62 static size_t	tireclen;	/* length of records in tmp file */
63 
64 static bool	rev_in_string(const char *);
65 static bool	reallocate_lines(size_t *);
66 
67 /* returns false if insufficient memory */
68 static bool	plan_a(const char *);
69 
70 static void	plan_b(const char *);
71 
72 /* New patch--prepare to edit another file. */
73 
74 void
75 re_input(void)
76 {
77 	if (using_plan_a) {
78 		free(i_ptr);
79 		i_ptr = NULL;
80 		if (i_womp != NULL) {
81 			munmap(i_womp, i_size);
82 			i_womp = NULL;
83 		}
84 		i_size = 0;
85 	} else {
86 		using_plan_a = true;	/* maybe the next one is smaller */
87 		close(tifd);
88 		tifd = -1;
89 		free(tibuf[0]);
90 		free(tibuf[1]);
91 		tibuf[0] = tibuf[1] = NULL;
92 		tiline[0] = tiline[1] = -1;
93 		tireclen = 0;
94 	}
95 }
96 
97 /* Construct the line index, somehow or other. */
98 
99 void
100 scan_input(const char *filename)
101 {
102 	if (!plan_a(filename))
103 		plan_b(filename);
104 	if (verbose) {
105 		say("Patching file %s using Plan %s...\n", filename,
106 		    (using_plan_a ? "A" : "B"));
107 	}
108 }
109 
110 static bool
111 reallocate_lines(size_t *lines_allocated)
112 {
113 	char	**p;
114 	size_t	new_size;
115 
116 	new_size = *lines_allocated * 3 / 2;
117 	p = realloc(i_ptr, (new_size + 2) * sizeof(char *));
118 	if (p == NULL) {	/* shucks, it was a near thing */
119 		munmap(i_womp, i_size);
120 		i_womp = NULL;
121 		free(i_ptr);
122 		i_ptr = NULL;
123 		*lines_allocated = 0;
124 		return false;
125 	}
126 	*lines_allocated = new_size;
127 	i_ptr = p;
128 	return true;
129 }
130 
131 /* Try keeping everything in memory. */
132 
133 static bool
134 plan_a(const char *filename)
135 {
136 	int		ifd, statfailed;
137 	char		*p, *s, lbuf[INITLINELEN];
138 	struct stat	filestat;
139 	ptrdiff_t	sz;
140 	size_t		i;
141 	size_t		iline, lines_allocated;
142 
143 #ifdef DEBUGGING
144 	if (debug & 8)
145 		return false;
146 #endif
147 
148 	if (filename == NULL || *filename == '\0')
149 		return false;
150 
151 	statfailed = stat(filename, &filestat);
152 	if (statfailed && ok_to_create_file) {
153 		if (verbose)
154 			say("(Creating file %s...)\n", filename);
155 
156 		/*
157 		 * in check_patch case, we still display `Creating file' even
158 		 * though we're not. The rule is that -C should be as similar
159 		 * to normal patch behavior as possible
160 		 */
161 		if (check_only)
162 			return true;
163 		makedirs(filename, true);
164 		close(creat(filename, 0666));
165 		statfailed = stat(filename, &filestat);
166 	}
167 	if (statfailed && check_only)
168 		fatal("%s not found, -C mode, can't probe further\n", filename);
169 	/* For nonexistent or read-only files, look for RCS or SCCS versions.  */
170 	if (statfailed ||
171 	    /* No one can write to it.  */
172 	    (filestat.st_mode & 0222) == 0 ||
173 	    /* I can't write to it.  */
174 	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
175 		const char	*cs = NULL, *filebase, *filedir;
176 		struct stat	cstat;
177 		char *tmp_filename1, *tmp_filename2;
178 
179 		tmp_filename1 = strdup(filename);
180 		tmp_filename2 = strdup(filename);
181 		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
182 			fatal("strdupping filename");
183 		filebase = basename(tmp_filename1);
184 		filedir = dirname(tmp_filename2);
185 
186 		/* Leave room in lbuf for the diff command.  */
187 		s = lbuf + 20;
188 
189 #define try(f, a1, a2, a3) \
190 	(snprintf(s, buf_size - 20, f, a1, a2, a3), stat(s, &cstat) == 0)
191 
192 		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
193 		    try("%s/RCS/%s%s", filedir, filebase, "") ||
194 		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
195 			snprintf(buf, buf_size, CHECKOUT, filename);
196 			snprintf(lbuf, sizeof lbuf, RCSDIFF, filename);
197 			cs = "RCS";
198 		} else if (try("%s/SCCS/%s%s", filedir, SCCSPREFIX, filebase) ||
199 		    try("%s/%s%s", filedir, SCCSPREFIX, filebase)) {
200 			snprintf(buf, buf_size, GET, s);
201 			snprintf(lbuf, sizeof lbuf, SCCSDIFF, s, filename);
202 			cs = "SCCS";
203 		} else if (statfailed)
204 			fatal("can't find %s\n", filename);
205 
206 		free(tmp_filename1);
207 		free(tmp_filename2);
208 
209 		/*
210 		 * else we can't write to it but it's not under a version
211 		 * control system, so just proceed.
212 		 */
213 		if (cs) {
214 			if (!statfailed) {
215 				if ((filestat.st_mode & 0222) != 0)
216 					/* The owner can write to it.  */
217 					fatal("file %s seems to be locked "
218 					    "by somebody else under %s\n",
219 					    filename, cs);
220 				/*
221 				 * It might be checked out unlocked.  See if
222 				 * it's safe to check out the default version
223 				 * locked.
224 				 */
225 				if (verbose)
226 					say("Comparing file %s to default "
227 					    "%s version...\n",
228 					    filename, cs);
229 				if (system(lbuf))
230 					fatal("can't check out file %s: "
231 					    "differs from default %s version\n",
232 					    filename, cs);
233 			}
234 			if (verbose)
235 				say("Checking out file %s from %s...\n",
236 				    filename, cs);
237 			if (system(buf) || stat(filename, &filestat))
238 				fatal("can't check out file %s from %s\n",
239 				    filename, cs);
240 		}
241 	}
242 	filemode = filestat.st_mode;
243 	if (!S_ISREG(filemode))
244 		fatal("%s is not a normal file--can't patch\n", filename);
245 	if ((uint64_t)filestat.st_size > SIZE_MAX) {
246 		say("block too large to mmap\n");
247 		return false;
248 	}
249 	i_size = (size_t)filestat.st_size;
250 	if (out_of_mem) {
251 		set_hunkmax();	/* make sure dynamic arrays are allocated */
252 		out_of_mem = false;
253 		return false;	/* force plan b because plan a bombed */
254 	}
255 	if ((ifd = open(filename, O_RDONLY)) < 0)
256 		pfatal("can't open file %s", filename);
257 
258 	if (i_size) {
259 		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
260 		if (i_womp == MAP_FAILED) {
261 			perror("mmap failed");
262 			i_womp = NULL;
263 			close(ifd);
264 			return false;
265 		}
266 	} else {
267 		i_womp = NULL;
268 	}
269 
270 	close(ifd);
271 	if (i_size)
272 		madvise(i_womp, i_size, MADV_SEQUENTIAL);
273 
274 	/* estimate the number of lines */
275 	lines_allocated = i_size / 25;
276 	if (lines_allocated < 100)
277 		lines_allocated = 100;
278 
279 	if (!reallocate_lines(&lines_allocated))
280 		return false;
281 
282 	/* now scan the buffer and build pointer array */
283 	iline = 1;
284 	i_ptr[iline] = i_womp;
285 	/* test for NUL too, to maintain the behavior of the original code */
286 	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
287 		if (*s == '\n') {
288 			if (iline == lines_allocated) {
289 				if (!reallocate_lines(&lines_allocated))
290 					return false;
291 			}
292 			/* these are NOT NUL terminated */
293 			i_ptr[++iline] = s + 1;
294 		}
295 	}
296 	/* if the last line contains no EOL, append one */
297 	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
298 		last_line_missing_eol = true;
299 		/* fix last line */
300 		sz = s - i_ptr[iline];
301 		p = malloc(sz + 1);
302 		if (p == NULL) {
303 			free(i_ptr);
304 			i_ptr = NULL;
305 			munmap(i_womp, i_size);
306 			i_womp = NULL;
307 			return false;
308 		}
309 
310 		memcpy(p, i_ptr[iline], sz);
311 		p[sz] = '\n';
312 		i_ptr[iline] = p;
313 		/* count the extra line and make it point to some valid mem */
314 		i_ptr[++iline] = empty_line;
315 	} else
316 		last_line_missing_eol = false;
317 
318 	input_lines = iline - 1;
319 
320 	/* now check for revision, if any */
321 
322 	if (revision != NULL) {
323 		if (i_womp == NULL || !rev_in_string(i_womp)) {
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 "
335 				    "%s 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 	return true;		/* plan a will work */
345 }
346 
347 /* Keep (virtually) nothing in memory. */
348 
349 static void
350 plan_b(const char *filename)
351 {
352 	FILE	*ifp;
353 	size_t	i = 0, j, len, maxlen = 1;
354 	char	*lbuf = NULL, *p;
355 	bool	found_revision = (revision == NULL);
356 
357 	using_plan_a = false;
358 	if ((ifp = fopen(filename, "r")) == NULL)
359 		pfatal("can't open file %s", filename);
360 	unlink(TMPINNAME);
361 	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
362 		pfatal("can't open file %s", TMPINNAME);
363 	while ((p = fgetln(ifp, &len)) != NULL) {
364 		if (p[len - 1] == '\n')
365 			p[len - 1] = '\0';
366 		else {
367 			/* EOF without EOL, copy and add the NUL */
368 			if ((lbuf = malloc(len + 1)) == NULL)
369 				fatal("out of memory\n");
370 			memcpy(lbuf, p, len);
371 			lbuf[len] = '\0';
372 			p = lbuf;
373 
374 			last_line_missing_eol = true;
375 			len++;
376 		}
377 		if (revision != NULL && !found_revision && rev_in_string(p))
378 			found_revision = true;
379 		if (len > maxlen)
380 			maxlen = len;   /* find longest line */
381 	}
382 	free(lbuf);
383 	if (ferror(ifp))
384 		pfatal("can't read file %s", filename);
385 
386 	if (revision != NULL) {
387 		if (!found_revision) {
388 			if (force) {
389 				if (verbose)
390 					say("Warning: this file doesn't appear "
391 					    "to be the %s version--patching anyway.\n",
392 					    revision);
393 			} else if (batch) {
394 				fatal("this file doesn't appear to be the "
395 				    "%s version--aborting.\n",
396 				    revision);
397 			} else {
398 				ask("This file doesn't appear to be the %s "
399 				    "version--patch anyway? [n] ",
400 				    revision);
401 				if (*buf != 'y')
402 					fatal("aborted\n");
403 			}
404 		} else if (verbose)
405 			say("Good.  This file appears to be the %s version.\n",
406 			    revision);
407 	}
408 	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
409 	tireclen = maxlen;
410 	tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE;
411 	lines_per_buf = tibuflen / maxlen;
412 	tibuf[0] = malloc(tibuflen + 1);
413 	if (tibuf[0] == NULL)
414 		fatal("out of memory\n");
415 	tibuf[1] = malloc(tibuflen + 1);
416 	if (tibuf[1] == NULL)
417 		fatal("out of memory\n");
418 	for (i = 1;; i++) {
419 		p = tibuf[0] + maxlen * (i % lines_per_buf);
420 		if (i % lines_per_buf == 0)	/* new block */
421 			if (write(tifd, tibuf[0], tibuflen) !=
422 			    (ssize_t) tibuflen)
423 				pfatal("can't write temp file");
424 		if (fgets(p, maxlen + 1, ifp) == NULL) {
425 			input_lines = i - 1;
426 			if (i % lines_per_buf != 0)
427 				if (write(tifd, tibuf[0], tibuflen) !=
428 				    (ssize_t) tibuflen)
429 					pfatal("can't write temp file");
430 			break;
431 		}
432 		j = strlen(p);
433 		/* These are '\n' terminated strings, so no need to add a NUL */
434 		if (j == 0 || p[j - 1] != '\n')
435 			p[j] = '\n';
436 	}
437 	fclose(ifp);
438 	close(tifd);
439 	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
440 		pfatal("can't reopen file %s", TMPINNAME);
441 }
442 
443 /*
444  * Fetch a line from the input file, \n terminated, not necessarily \0.
445  */
446 char *
447 ifetch(LINENUM line, int whichbuf)
448 {
449 	if (line < 1 || line > input_lines) {
450 		if (warn_on_invalid_line) {
451 			say("No such line %ld in input file, ignoring\n", line);
452 			warn_on_invalid_line = false;
453 		}
454 		return NULL;
455 	}
456 	if (using_plan_a)
457 		return i_ptr[line];
458 	else {
459 		LINENUM	offline = line % lines_per_buf;
460 		LINENUM	baseline = line - offline;
461 
462 		if (tiline[0] == baseline)
463 			whichbuf = 0;
464 		else if (tiline[1] == baseline)
465 			whichbuf = 1;
466 		else {
467 			tiline[whichbuf] = baseline;
468 
469 			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
470 			    tibuflen), SEEK_SET) < 0)
471 				pfatal("cannot seek in the temporary input file");
472 
473 			if (read(tifd, tibuf[whichbuf], tibuflen) !=
474 			    (ssize_t) tibuflen)
475 				pfatal("error reading tmp file %s", TMPINNAME);
476 		}
477 		return tibuf[whichbuf] + (tireclen * offline);
478 	}
479 }
480 
481 /*
482  * True if the string argument contains the revision number we want.
483  */
484 static bool
485 rev_in_string(const char *string)
486 {
487 	const char	*s;
488 	size_t		patlen;
489 
490 	if (revision == NULL)
491 		return true;
492 	patlen = strlen(revision);
493 	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
494 		return true;
495 	for (s = string; *s; s++) {
496 		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
497 		    isspace((unsigned char)s[patlen + 1])) {
498 			return true;
499 		}
500 	}
501 	return false;
502 }
503