• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* patch.c - Apply a "universal" diff.
2  *
3  * Copyright 2007 Rob Landley <rob@landley.net>
4  *
5  * see http://opengroup.org/onlinepubs/9699919799/utilities/patch.html
6  * (But only does -u, because who still cares about "ed"?)
7  *
8  * TODO:
9  * -b backup
10  * -N ignore already applied
11  * -d chdir first
12  * -D define wrap #ifdef and #ifndef around changes
13  * -o outfile output here instead of in place
14  * -r rejectfile write rejected hunks to this file
15  *
16  * -E remove empty files --remove-empty-files
17  * -f force (no questions asked)
18  * -F fuzz (number, default 2)
19  * [file] which file to patch
20 
21 USE_PATCH(NEWTOY(patch, "(dry-run)"USE_TOYBOX_DEBUG("x")"ulp#d:i:Rs(quiet)", TOYFLAG_USR|TOYFLAG_BIN))
22 
23 config PATCH
24   bool "patch"
25   default y
26   help
27     usage: patch [-d DIR] [-i file] [-p depth] [-Rlsu] [--dry-run]
28 
29     Apply a unified diff to one or more files.
30 
31     -d	Modify files in DIR
32     -i	Input file (default=stdin)
33     -l	Loose match (ignore whitespace)
34     -p	Number of '/' to strip from start of file paths (default=all)
35     -R	Reverse patch
36     -s	Silent except for errors
37     -u	Ignored (only handles "unified" diffs)
38     --dry-run Don't change files, just confirm patch applies
39 
40     This version of patch only handles unified diffs, and only modifies
41     a file when all hunks to that file apply.  Patch prints failed hunks
42     to stderr, and exits with nonzero status if any hunks fail.
43 
44     A file compared against /dev/null (or with a date <= the epoch) is
45     created/deleted as appropriate.
46 */
47 
48 #define FOR_patch
49 #include "toys.h"
50 
51 GLOBALS(
52   char *i, *d;
53   long p;
54 
55   struct double_list *current_hunk;
56   long oldline, oldlen, newline, newlen;
57   long linenum;
58   int context, state, filein, fileout, filepatch, hunknum;
59   char *tempname;
60 )
61 
62 // Dispose of a line of input, either by writing it out or discarding it.
63 
64 // state < 2: just free
65 // state = 2: write whole line to stderr
66 // state = 3: write whole line to fileout
67 // state > 3: write line+1 to fileout when *line != state
68 
do_line(void * data)69 static void do_line(void *data)
70 {
71   struct double_list *dlist = (struct double_list *)data;
72 
73   if (TT.state>1 && *dlist->data != TT.state) {
74     char *s = dlist->data+(TT.state>3);
75     int i = TT.state == 2 ? 2 : TT.fileout;
76 
77     xwrite(i, s, strlen(s));
78     xwrite(i, "\n", 1);
79   }
80 
81   if (FLAG(x)) fprintf(stderr, "DO %d: %s\n", TT.state, dlist->data);
82 
83   free(dlist->data);
84   free(data);
85 }
86 
finish_oldfile(void)87 static void finish_oldfile(void)
88 {
89   if (TT.tempname) replace_tempfile(TT.filein, TT.fileout, &TT.tempname);
90   TT.fileout = TT.filein = -1;
91 }
92 
fail_hunk(void)93 static void fail_hunk(void)
94 {
95   if (!TT.current_hunk) return;
96 
97   fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n",
98       TT.hunknum, TT.oldline, TT.newline);
99   toys.exitval = 1;
100 
101   // If we got to this point, we've seeked to the end.  Discard changes to
102   // this file and advance to next file.
103 
104   TT.state = 2;
105   llist_traverse(TT.current_hunk, do_line);
106   TT.current_hunk = NULL;
107   if (!FLAG(dry_run)) delete_tempfile(TT.filein, TT.fileout, &TT.tempname);
108   TT.state = 0;
109 }
110 
111 // Compare ignoring whitespace. Just returns 0/1, no > or <
loosecmp(char * aa,char * bb)112 static int loosecmp(char *aa, char *bb)
113 {
114   int a = 0, b = 0;
115 
116   for (;;) {
117     while (isspace(aa[a])) a++;
118     while (isspace(bb[b])) b++;
119     if (aa[a] != bb[b]) return 1;
120     if (!aa[a]) return 0;
121     a++, b++;
122   }
123 }
124 
125 // Given a hunk of a unified diff, make the appropriate change to the file.
126 // This does not use the location information, but instead treats a hunk
127 // as a sort of regex.  Copies data from input to output until it finds
128 // the change to be made, then outputs the changed data and returns.
129 // (Finding EOF first is an error.)  This is a single pass operation, so
130 // multiple hunks must occur in order in the file.
131 
apply_one_hunk(void)132 static int apply_one_hunk(void)
133 {
134   struct double_list *plist, *buf = NULL, *check;
135   int matcheof, trailing = 0, reverse = FLAG(R), backwarn = 0;
136   int (*lcmp)(char *aa, char *bb);
137 
138   lcmp = FLAG(l) ? (void *)loosecmp : (void *)strcmp;
139   dlist_terminate(TT.current_hunk);
140 
141   // Match EOF if there aren't as many ending context lines as beginning
142   for (plist = TT.current_hunk; plist; plist = plist->next) {
143     if (plist->data[0]==' ') trailing++;
144     else trailing = 0;
145     if (FLAG(x)) fprintf(stderr, "HUNK:%s\n", plist->data);
146   }
147   matcheof = !trailing || trailing < TT.context;
148 
149   if (FLAG(x)) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
150 
151   // Loop through input data searching for this hunk. Match all context
152   // lines and all lines to be removed until we've found the end of a
153   // complete hunk.
154   plist = TT.current_hunk;
155   buf = NULL;
156 
157   for (;;) {
158     char *data = get_line(TT.filein);
159 
160     TT.linenum++;
161     // Figure out which line of hunk to compare with next. (Skip lines
162     // of the hunk we'd be adding.)
163     while (plist && *plist->data == "+-"[reverse]) {
164       if (data && !lcmp(data, plist->data+1))
165         if (!backwarn) backwarn = TT.linenum;
166       plist = plist->next;
167     }
168 
169     // Is this EOF?
170     if (!data) {
171       if (FLAG(x)) fprintf(stderr, "INEOF\n");
172 
173       // Does this hunk need to match EOF?
174       if (!plist && matcheof) break;
175 
176       if (backwarn && !FLAG(s))
177         fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
178             TT.hunknum, TT.linenum);
179 
180       // File ended before we found a place for this hunk.
181       fail_hunk();
182       goto done;
183     } else if (FLAG(x)) fprintf(stderr, "IN: %s\n", data);
184     check = dlist_add(&buf, data);
185 
186     // Compare this line with next expected line of hunk.
187 
188     // A match can fail because the next line doesn't match, or because
189     // we hit the end of a hunk that needed EOF, and this isn't EOF.
190 
191     // If match failed, flush first line of buffered data and
192     // recheck buffered data for a new match until we find one or run
193     // out of buffer.
194 
195     for (;;) {
196       if (!plist || lcmp(check->data, plist->data+1)) {
197         // Match failed.  Write out first line of buffered data and
198         // recheck remaining buffered data for a new match.
199 
200         if (FLAG(x)) {
201           int bug = 0;
202 
203           if (!plist) fprintf(stderr, "NULL plist\n");
204           else {
205             while (plist->data[bug] == check->data[bug]) bug++;
206             fprintf(stderr, "NOT(%d:%d!=%d): %s\n", bug, plist->data[bug],
207               check->data[bug], plist->data);
208           }
209         }
210 
211         // If this hunk must match start of file, fail if it didn't.
212         if (!TT.context || trailing>TT.context) {
213           fail_hunk();
214           goto done;
215         }
216 
217         TT.state = 3;
218         do_line(check = dlist_pop(&buf));
219         plist = TT.current_hunk;
220 
221         // If we've reached the end of the buffer without confirming a
222         // match, read more lines.
223         if (!buf) break;
224         check = buf;
225       } else {
226         if (FLAG(x)) fprintf(stderr, "MAYBE: %s\n", plist->data);
227         // This line matches. Advance plist, detect successful match.
228         plist = plist->next;
229         if (!plist && !matcheof) goto out;
230         check = check->next;
231         if (check == buf) break;
232       }
233     }
234   }
235 out:
236   // We have a match.  Emit changed data.
237   TT.state = "-+"[reverse];
238   llist_traverse(TT.current_hunk, do_line);
239   TT.current_hunk = NULL;
240   TT.state = 1;
241 done:
242   if (buf) {
243     dlist_terminate(buf);
244     llist_traverse(buf, do_line);
245   }
246 
247   return TT.state;
248 }
249 
250 // Read a patch file and find hunks, opening/creating/deleting files.
251 // Call apply_one_hunk() on each hunk.
252 
253 // state 0: Not in a hunk, look for +++.
254 // state 1: Found +++ file indicator, look for @@
255 // state 2: In hunk: counting initial context lines
256 // state 3: In hunk: getting body
257 
patch_main(void)258 void patch_main(void)
259 {
260   int reverse = FLAG(R), state = 0, patchlinenum = 0, strip = 0;
261   char *oldname = NULL, *newname = NULL;
262 
263   if (TT.i) TT.filepatch = xopenro(TT.i);
264   TT.filein = TT.fileout = -1;
265 
266   if (TT.d) xchdir(TT.d);
267 
268   // Loop through the lines in the patch
269   for (;;) {
270     char *patchline;
271 
272     patchline = get_line(TT.filepatch);
273     if (!patchline) break;
274 
275     // Other versions of patch accept damaged patches, so we need to also.
276     if (strip || !patchlinenum++) {
277       int len = strlen(patchline);
278       if (patchline[len-1] == '\r') {
279         if (!strip && !FLAG(s)) fprintf(stderr, "Removing DOS newlines\n");
280         strip = 1;
281         patchline[len-1]=0;
282       }
283     }
284     if (!*patchline) {
285       free(patchline);
286       patchline = xstrdup(" ");
287     }
288 
289     // Are we assembling a hunk?
290     if (state >= 2) {
291       if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
292         dlist_add(&TT.current_hunk, patchline);
293 
294         if (*patchline != '+') TT.oldlen--;
295         if (*patchline != '-') TT.newlen--;
296 
297         // Context line?
298         if (*patchline==' ' && state==2) TT.context++;
299         else state=3;
300 
301         // If we've consumed all expected hunk lines, apply the hunk.
302         if (!TT.oldlen && !TT.newlen) state = apply_one_hunk();
303         continue;
304       }
305       dlist_terminate(TT.current_hunk);
306       fail_hunk();
307       state = 0;
308       continue;
309     }
310 
311     // Open a new file?
312     if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
313       char *s, **name = &oldname;
314       int i;
315 
316       if (*patchline == '+') {
317         name = &newname;
318         state = 1;
319       }
320 
321       free(*name);
322       finish_oldfile();
323 
324       // Trim date from end of filename (if any).  We don't care.
325       for (s = patchline+4; *s && (*s!='\t' || !isdigit(s[1])); s++)
326         if (*s=='\\' && s[1]) s++;
327       i = atoi(s);
328       if (i>1900 && i<=1970) *name = xstrdup("/dev/null");
329       else {
330         *s = 0;
331         *name = xstrdup(patchline+4);
332       }
333 
334       // We defer actually opening the file because svn produces broken
335       // patches that don't signal they want to create a new file the
336       // way the patch man page says, so you have to read the first hunk
337       // and _guess_.
338 
339     // Start a new hunk?  Usually @@ -oldline,oldlen +newline,newlen @@
340     // but a missing ,value means the value is 1.
341     } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
342       int i;
343       char *s = patchline+4;
344 
345       // Read oldline[,oldlen] +newline[,newlen]
346 
347       TT.oldlen = TT.newlen = 1;
348       TT.oldline = strtol(s, &s, 10);
349       if (*s == ',') TT.oldlen=strtol(s+1, &s, 10);
350       TT.newline = strtol(s+2, &s, 10);
351       if (*s == ',') TT.newlen = strtol(s+1, &s, 10);
352 
353       TT.context = 0;
354       state = 2;
355 
356       // If this is the first hunk, open the file.
357       if (TT.filein == -1) {
358         int oldsum, newsum, del = 0;
359         char *name;
360 
361         oldsum = TT.oldline + TT.oldlen;
362         newsum = TT.newline + TT.newlen;
363 
364         name = reverse ? oldname : newname;
365 
366         // We're deleting oldname if new file is /dev/null (before -p)
367         // or if new hunk is empty (zero context) after patching
368         if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum))
369         {
370           name = reverse ? newname : oldname;
371           del++;
372         }
373 
374         // handle -p path truncation.
375         for (i = 0, s = name; *s;) {
376           if (FLAG(p) && TT.p == i) break;
377           if (*s++ != '/') continue;
378           while (*s == '/') s++;
379           name = s;
380           i++;
381         }
382 
383         if (del) {
384           if (!FLAG(s)) printf("removing %s\n", name);
385           xunlink(name);
386           state = 0;
387         // If we've got a file to open, do so.
388         } else if (!FLAG(p) || i <= TT.p) {
389           // If the old file was null, we're creating a new one.
390           if ((!strcmp(oldname, "/dev/null") || !oldsum) && access(name, F_OK))
391           {
392             if (!FLAG(s)) printf("creating %s\n", name);
393             if (mkpath(name)) perror_exit("mkpath %s", name);
394             TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666);
395           } else {
396             if (!FLAG(s)) printf("patching %s\n", name);
397             TT.filein = xopenro(name);
398           }
399           if (FLAG(dry_run)) TT.fileout = xopen("/dev/null", O_RDWR);
400           else TT.fileout = copy_tempfile(TT.filein, name, &TT.tempname);
401           TT.linenum = 0;
402           TT.hunknum = 0;
403         }
404       }
405 
406       TT.hunknum++;
407 
408       continue;
409     }
410 
411     // If we didn't continue above, discard this line.
412     free(patchline);
413   }
414 
415   finish_oldfile();
416 
417   if (CFG_TOYBOX_FREE) {
418     close(TT.filepatch);
419     free(oldname);
420     free(newname);
421   }
422 }
423