]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - usr.bin/patch/inp.c
- Copy stable/10@296371 to releng/10.3 in preparation for 10.3-RC1
[FreeBSD/releng/10.3.git] / usr.bin / patch / inp.c
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 LINENUM  lines_per_buf;  /* how many lines per buffer */
65 static int      tireclen;       /* length of records in tmp file */
66
67 static bool     rev_in_string(const char *);
68 static bool     reallocate_lines(size_t *);
69
70 /* returns false if insufficient memory */
71 static bool     plan_a(const char *);
72
73 static void     plan_b(const char *);
74
75 /* New patch--prepare to edit another file. */
76
77 void
78 re_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
102 void
103 scan_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
113 static bool
114 reallocate_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
136 static bool
137 plan_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
368 static void
369 plan_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  */
449 char *
450 ifetch(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  */
486 static bool
487 rev_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 }