xref: /freebsd-src/usr.bin/patch/inp.c (revision 2cf624f68a6208facda1dc42ed5e06e9defb442e)
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 #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 = realloc(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, pstat;
141 	char		*p, *s, lbuf[INITLINELEN];
142 	struct stat	filestat;
143 	ptrdiff_t	sz;
144 	size_t		i;
145 	size_t		iline, lines_allocated;
146 	pid_t		pid;
147 
148 #ifdef DEBUGGING
149 	if (debug & 8)
150 		return false;
151 #endif
152 
153 	if (filename == NULL || *filename == '\0')
154 		return false;
155 
156 	statfailed = stat(filename, &filestat);
157 	if (statfailed && ok_to_create_file) {
158 		if (verbose)
159 			say("(Creating file %s...)\n", filename);
160 
161 		/*
162 		 * in check_patch case, we still display `Creating file' even
163 		 * though we're not. The rule is that -C should be as similar
164 		 * to normal patch behavior as possible
165 		 */
166 		if (check_only)
167 			return true;
168 		makedirs(filename, true);
169 		close(creat(filename, 0666));
170 		statfailed = stat(filename, &filestat);
171 	}
172 	if (statfailed && check_only)
173 		fatal("%s not found, -C mode, can't probe further\n", filename);
174 	/* For nonexistent or read-only files, look for RCS versions.  */
175 
176 	if (statfailed ||
177 	    /* No one can write to it.  */
178 	    (filestat.st_mode & 0222) == 0 ||
179 	    /* I can't write to it.  */
180 	    ((filestat.st_mode & 0022) == 0 && filestat.st_uid != getuid())) {
181 		char	*filebase, *filedir;
182 		struct stat	cstat;
183 		char	*tmp_filename1, *tmp_filename2;
184 		char	*argp[4] = { NULL };
185 		posix_spawn_file_actions_t file_actions;
186 
187 		tmp_filename1 = strdup(filename);
188 		tmp_filename2 = strdup(filename);
189 		if (tmp_filename1 == NULL || tmp_filename2 == NULL)
190 			fatal("strdupping filename");
191 
192 		filebase = basename(tmp_filename1);
193 		filedir = dirname(tmp_filename2);
194 
195 		memset(argp, 0, sizeof(argp));
196 
197 #define try(f, a1, a2, a3) \
198 	(snprintf(lbuf, sizeof(lbuf), f, a1, a2, a3), stat(lbuf, &cstat) == 0)
199 
200 		/*
201 		 * else we can't write to it but it's not under a version
202 		 * control system, so just proceed.
203 		 */
204 		if (try("%s/RCS/%s%s", filedir, filebase, RCSSUFFIX) ||
205 		    try("%s/RCS/%s%s", filedir, filebase, "") ||
206 		    try("%s/%s%s", filedir, filebase, RCSSUFFIX)) {
207 			if (!statfailed) {
208 				if ((filestat.st_mode & 0222) != 0)
209 					/* The owner can write to it.  */
210 					fatal("file %s seems to be locked "
211 					    "by somebody else under RCS\n",
212 					    filename);
213 				/*
214 				 * It might be checked out unlocked.  See if
215 				 * it's safe to check out the default version
216 				 * locked.
217 				 */
218 				if (verbose)
219 					say("Comparing file %s to default "
220 					    "RCS version...\n", filename);
221 
222 				argp[0] = strdup(RCSDIFF);
223 				argp[1] = strdup(filename);
224 				posix_spawn_file_actions_init(&file_actions);
225 				posix_spawn_file_actions_addopen(&file_actions,
226 				    STDOUT_FILENO, _PATH_DEVNULL, O_WRONLY, 0);
227 				if (posix_spawn(&pid, RCSDIFF, &file_actions,
228 				    NULL, argp, NULL) == 0) {
229 					pid = waitpid(pid, &pstat, 0);
230 					if (pid == -1 || WEXITSTATUS(pstat) != 0)
231 						fatal("can't check out file %s: "
232 						    "differs from default RCS version\n",
233 						    filename);
234 				} else
235 					fatal("posix_spawn: %s\n", strerror(errno));
236 				posix_spawn_file_actions_destroy(&file_actions);
237 				free(argp[1]);
238 				free(argp[0]);
239 			}
240 
241 			if (verbose)
242 				say("Checking out file %s from RCS...\n",
243 				    filename);
244 
245 			argp[0] = strdup(CHECKOUT);
246 			argp[1] = strdup("-l");
247 			argp[2] = strdup(filename);
248 			if (posix_spawn(&pid, CHECKOUT, NULL, NULL, argp,
249 			    NULL) == 0) {
250 				pid = waitpid(pid, &pstat, 0);
251 				if (pid == -1 || WEXITSTATUS(pstat) != 0 ||
252 				    stat(filename, &filestat))
253 					fatal("can't check out file %s from RCS\n",
254 					    filename);
255 			} else
256 				fatal("posix_spawn: %s\n", strerror(errno));
257 			free(argp[2]);
258 			free(argp[1]);
259 			free(argp[0]);
260 		} else if (statfailed) {
261 			fatal("can't find %s\n", filename);
262 		}
263 		free(tmp_filename1);
264 		free(tmp_filename2);
265 	}
266 
267 	filemode = filestat.st_mode;
268 	if (!S_ISREG(filemode))
269 		fatal("%s is not a normal file--can't patch\n", filename);
270 	if ((uint64_t)filestat.st_size > SIZE_MAX) {
271 		say("block too large to mmap\n");
272 		return false;
273 	}
274 	i_size = (size_t)filestat.st_size;
275 	if (out_of_mem) {
276 		set_hunkmax();	/* make sure dynamic arrays are allocated */
277 		out_of_mem = false;
278 		return false;	/* force plan b because plan a bombed */
279 	}
280 	if ((ifd = open(filename, O_RDONLY)) < 0)
281 		pfatal("can't open file %s", filename);
282 
283 	if (i_size) {
284 		i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
285 		if (i_womp == MAP_FAILED) {
286 			perror("mmap failed");
287 			i_womp = NULL;
288 			close(ifd);
289 			return false;
290 		}
291 	} else {
292 		i_womp = NULL;
293 	}
294 
295 	close(ifd);
296 	if (i_size)
297 		madvise(i_womp, i_size, MADV_SEQUENTIAL);
298 
299 	/* estimate the number of lines */
300 	lines_allocated = i_size / 25;
301 	if (lines_allocated < 100)
302 		lines_allocated = 100;
303 
304 	if (!reallocate_lines(&lines_allocated))
305 		return false;
306 
307 	/* now scan the buffer and build pointer array */
308 	iline = 1;
309 	i_ptr[iline] = i_womp;
310 	/* test for NUL too, to maintain the behavior of the original code */
311 	for (s = i_womp, i = 0; i < i_size && *s != '\0'; s++, i++) {
312 		if (*s == '\n') {
313 			if (iline == lines_allocated) {
314 				if (!reallocate_lines(&lines_allocated))
315 					return false;
316 			}
317 			/* these are NOT NUL terminated */
318 			i_ptr[++iline] = s + 1;
319 		}
320 	}
321 	/* if the last line contains no EOL, append one */
322 	if (i_size > 0 && i_womp[i_size - 1] != '\n') {
323 		last_line_missing_eol = true;
324 		/* fix last line */
325 		sz = s - i_ptr[iline];
326 		p = malloc(sz + 1);
327 		if (p == NULL) {
328 			free(i_ptr);
329 			i_ptr = NULL;
330 			munmap(i_womp, i_size);
331 			i_womp = NULL;
332 			return false;
333 		}
334 
335 		memcpy(p, i_ptr[iline], sz);
336 		p[sz] = '\n';
337 		i_ptr[iline] = p;
338 		/* count the extra line and make it point to some valid mem */
339 		i_ptr[++iline] = empty_line;
340 	} else
341 		last_line_missing_eol = false;
342 
343 	input_lines = iline - 1;
344 
345 	/* now check for revision, if any */
346 
347 	if (revision != NULL) {
348 		if (i_womp == NULL || !rev_in_string(i_womp)) {
349 			if (force) {
350 				if (verbose)
351 					say("Warning: this file doesn't appear "
352 					    "to be the %s version--patching anyway.\n",
353 					    revision);
354 			} else if (batch) {
355 				fatal("this file doesn't appear to be the "
356 				    "%s version--aborting.\n",
357 				    revision);
358 			} else {
359 				ask("This file doesn't appear to be the "
360 				    "%s version--patch anyway? [n] ",
361 				    revision);
362 				if (*buf != 'y')
363 					fatal("aborted\n");
364 			}
365 		} else if (verbose)
366 			say("Good.  This file appears to be the %s version.\n",
367 			    revision);
368 	}
369 	return true;		/* plan a will work */
370 }
371 
372 /* Keep (virtually) nothing in memory. */
373 
374 static void
375 plan_b(const char *filename)
376 {
377 	FILE	*ifp;
378 	size_t	i = 0, j, len, maxlen = 1;
379 	char	*lbuf = NULL, *p;
380 	bool	found_revision = (revision == NULL);
381 
382 	using_plan_a = false;
383 	if ((ifp = fopen(filename, "r")) == NULL)
384 		pfatal("can't open file %s", filename);
385 	unlink(TMPINNAME);
386 	if ((tifd = open(TMPINNAME, O_EXCL | O_CREAT | O_WRONLY, 0666)) < 0)
387 		pfatal("can't open file %s", TMPINNAME);
388 	while ((p = fgetln(ifp, &len)) != NULL) {
389 		if (p[len - 1] == '\n')
390 			p[len - 1] = '\0';
391 		else {
392 			/* EOF without EOL, copy and add the NUL */
393 			if ((lbuf = malloc(len + 1)) == NULL)
394 				fatal("out of memory\n");
395 			memcpy(lbuf, p, len);
396 			lbuf[len] = '\0';
397 			p = lbuf;
398 
399 			last_line_missing_eol = true;
400 			len++;
401 		}
402 		if (revision != NULL && !found_revision && rev_in_string(p))
403 			found_revision = true;
404 		if (len > maxlen)
405 			maxlen = len;   /* find longest line */
406 	}
407 	free(lbuf);
408 	if (ferror(ifp))
409 		pfatal("can't read file %s", filename);
410 
411 	if (revision != NULL) {
412 		if (!found_revision) {
413 			if (force) {
414 				if (verbose)
415 					say("Warning: this file doesn't appear "
416 					    "to be the %s version--patching anyway.\n",
417 					    revision);
418 			} else if (batch) {
419 				fatal("this file doesn't appear to be the "
420 				    "%s version--aborting.\n",
421 				    revision);
422 			} else {
423 				ask("This file doesn't appear to be the %s "
424 				    "version--patch anyway? [n] ",
425 				    revision);
426 				if (*buf != 'y')
427 					fatal("aborted\n");
428 			}
429 		} else if (verbose)
430 			say("Good.  This file appears to be the %s version.\n",
431 			    revision);
432 	}
433 	fseek(ifp, 0L, SEEK_SET);	/* rewind file */
434 	tireclen = maxlen;
435 	tibuflen = maxlen > BUFFERSIZE ? maxlen : BUFFERSIZE;
436 	lines_per_buf = tibuflen / maxlen;
437 	tibuf[0] = malloc(tibuflen + 1);
438 	if (tibuf[0] == NULL)
439 		fatal("out of memory\n");
440 	tibuf[1] = malloc(tibuflen + 1);
441 	if (tibuf[1] == NULL)
442 		fatal("out of memory\n");
443 	for (i = 1;; i++) {
444 		p = tibuf[0] + maxlen * (i % lines_per_buf);
445 		if (i % lines_per_buf == 0)	/* new block */
446 			if (write(tifd, tibuf[0], tibuflen) !=
447 			    (ssize_t) tibuflen)
448 				pfatal("can't write temp file");
449 		if (fgets(p, maxlen + 1, ifp) == NULL) {
450 			input_lines = i - 1;
451 			if (i % lines_per_buf != 0)
452 				if (write(tifd, tibuf[0], tibuflen) !=
453 				    (ssize_t) tibuflen)
454 					pfatal("can't write temp file");
455 			break;
456 		}
457 		j = strlen(p);
458 		/* These are '\n' terminated strings, so no need to add a NUL */
459 		if (j == 0 || p[j - 1] != '\n')
460 			p[j] = '\n';
461 	}
462 	fclose(ifp);
463 	close(tifd);
464 	if ((tifd = open(TMPINNAME, O_RDONLY)) < 0)
465 		pfatal("can't reopen file %s", TMPINNAME);
466 }
467 
468 /*
469  * Fetch a line from the input file, \n terminated, not necessarily \0.
470  */
471 char *
472 ifetch(LINENUM line, int whichbuf)
473 {
474 	if (line < 1 || line > input_lines) {
475 		if (warn_on_invalid_line) {
476 			say("No such line %ld in input file, ignoring\n", line);
477 			warn_on_invalid_line = false;
478 		}
479 		return NULL;
480 	}
481 	if (using_plan_a)
482 		return i_ptr[line];
483 	else {
484 		LINENUM	offline = line % lines_per_buf;
485 		LINENUM	baseline = line - offline;
486 
487 		if (tiline[0] == baseline)
488 			whichbuf = 0;
489 		else if (tiline[1] == baseline)
490 			whichbuf = 1;
491 		else {
492 			tiline[whichbuf] = baseline;
493 
494 			if (lseek(tifd, (off_t) (baseline / lines_per_buf *
495 			    tibuflen), SEEK_SET) < 0)
496 				pfatal("cannot seek in the temporary input file");
497 
498 			if (read(tifd, tibuf[whichbuf], tibuflen) !=
499 			    (ssize_t) tibuflen)
500 				pfatal("error reading tmp file %s", TMPINNAME);
501 		}
502 		return tibuf[whichbuf] + (tireclen * offline);
503 	}
504 }
505 
506 /*
507  * True if the string argument contains the revision number we want.
508  */
509 static bool
510 rev_in_string(const char *string)
511 {
512 	const char	*s;
513 	size_t		patlen;
514 
515 	if (revision == NULL)
516 		return true;
517 	patlen = strlen(revision);
518 	if (strnEQ(string, revision, patlen) && isspace((unsigned char)string[patlen]))
519 		return true;
520 	for (s = string; *s; s++) {
521 		if (isspace((unsigned char)*s) && strnEQ(s + 1, revision, patlen) &&
522 		    isspace((unsigned char)s[patlen + 1])) {
523 			return true;
524 		}
525 	}
526 	return false;
527 }
528