3 * Apply a "universal" diff.
4 * Adapted from toybox's patch implementation.
6 * Copyright 2007 Rob Landley <rob@landley.net>
8 * see http://www.opengroup.org/onlinepubs/009695399/utilities/patch.html
9 * (But only does -u, because who still cares about "ed"?)
13 * -l treat all whitespace as a single space
15 * -D define wrap #ifdef and #ifndef around changes
16 * -o outfile output here instead of in place
17 * -r rejectfile write rejected hunks to this file
19 * -f force (no questions asked)
20 * -F fuzz (number, default 2)
21 * [file] which file to patch
24 //config: bool "patch (9.1 kb)"
27 //config: Apply a unified diff formatted patch.
29 //applet:IF_PATCH(APPLET(patch, BB_DIR_USR_BIN, BB_SUID_DROP))
31 //kbuild:lib-$(CONFIG_PATCH) += patch.o
33 //usage:#define patch_trivial_usage
34 //usage: "[OPTIONS] [ORIGFILE [PATCHFILE]]"
35 //usage:#define patch_full_usage "\n\n"
36 //usage: " -p N Strip N leading components from file names"
37 //usage: "\n -i DIFF Read DIFF instead of stdin"
38 //usage: "\n -R Reverse patch"
39 //usage: "\n -N Ignore already applied patches"
40 //usage: "\n -E Remove output files if they become empty"
41 //usage: IF_LONG_OPTS(
42 //usage: "\n --dry-run Don't actually change files"
44 /* -u "interpret as unified diff" is supported but not documented: this info is not useful for --help */
46 //usage:#define patch_example_usage
47 //usage: "$ patch -p1 < example.diff\n"
48 //usage: "$ patch -p0 -i example.diff"
57 struct double_list *next;
58 struct double_list *prev;
62 // Free all the elements of a linked list
63 // Call freeit() on each element before freeing it.
64 static void dlist_free(struct double_list *list, void (*freeit)(void *data))
70 // Bail out also if list is circular.
71 if (list == pop) break;
75 // Add an entry before "list" element in (circular) doubly linked list
76 static struct double_list *dlist_add(struct double_list **list, char *data)
78 struct double_list *llist;
79 struct double_list *line = xmalloc(sizeof(*line));
84 struct double_list *p;
86 p = line->prev = llist->prev;
87 // (list is circular, we assume p is never NULL)
91 *list = line->next = line->prev = line;
101 struct double_list *current_hunk;
103 long oldline, oldlen, newline, newlen;
105 int context, state, hunknum;
111 #define TT (*ptr_to_globals)
112 #define INIT_TT() do { \
113 SET_PTR_TO_GLOBALS(xzalloc(sizeof(TT))); \
117 #define FLAG_STR "Rup:i:NEfg"
118 /* FLAG_REVERSE must be == 1! Code uses this fact. */
119 #define FLAG_REVERSE (1 << 0)
120 #define FLAG_u (1 << 1)
121 #define FLAG_PATHLEN (1 << 2)
122 #define FLAG_INPUT (1 << 3)
123 #define FLAG_IGNORE (1 << 4)
124 #define FLAG_RMEMPTY (1 << 5)
125 #define FLAG_f_unused (1 << 6)
126 #define FLAG_g_unused (1 << 7)
127 #define FLAG_dry_run ((1 << 8) * ENABLE_LONG_OPTS)
130 // Dispose of a line of input, either by writing it out or discarding it.
132 // state < 2: just free
133 // state = 2: write whole line to stderr
134 // state = 3: write whole line to fileout
135 // state > 3: write line+1 to fileout when *line != state
137 static void do_line(void *data)
139 struct double_list *dlist = data;
141 if (TT.state>1 && *dlist->data != TT.state)
142 fdprintf(TT.state == 2 ? 2 : TT.fileout,
143 "%s\n", dlist->data+(TT.state>3 ? 1 : 0));
145 if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
151 static void finish_oldfile(void)
154 // Copy the rest of the data and replace the original with the copy.
157 if (TT.filein != -1) {
158 bb_copyfd_eof(TT.filein, TT.fileout);
163 if (!ENABLE_LONG_OPTS || TT.tempname[0]) { /* not --dry-run? */
164 temp = xstrdup(TT.tempname);
165 temp[strlen(temp) - 6] = '\0';
166 rename(TT.tempname, temp);
173 TT.fileout = TT.filein = -1;
176 static void fail_hunk(void)
178 if (!TT.current_hunk) return;
180 fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline);
183 // If we got to this point, we've seeked to the end. Discard changes to
184 // this file and advance to next file.
187 TT.current_hunk->prev->next = NULL;
188 dlist_free(TT.current_hunk, do_line);
189 TT.current_hunk = NULL;
191 // Abort the copy and delete the temporary file.
194 if (!ENABLE_LONG_OPTS || TT.tempname[0]) { /* not --dry-run? */
203 // Given a hunk of a unified diff, make the appropriate change to the file.
204 // This does not use the location information, but instead treats a hunk
205 // as a sort of regex. Copies data from input to output until it finds
206 // the change to be made, then outputs the changed data and returns.
207 // (Finding EOF first is an error.) This is a single pass operation, so
208 // multiple hunks must occur in order in the file.
210 static int apply_one_hunk(void)
212 struct double_list *plist, *buf = NULL, *check;
213 int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0;
214 /* Do we try "dummy" revert to check whether
215 * to silently skip this hunk? Used to implement -N.
217 int dummy_revert = 0;
219 // Break doubly linked list so we can use singly linked traversal function.
220 TT.current_hunk->prev->next = NULL;
222 // Match EOF if there aren't as many ending context lines as beginning
223 for (plist = TT.current_hunk; plist; plist = plist->next) {
224 if (plist->data[0]==' ') matcheof++;
226 if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
228 matcheof = !matcheof || matcheof < TT.context;
230 if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
232 // Loop through input data searching for this hunk. Match all context
233 // lines and all lines to be removed until we've found the end of a
235 plist = TT.current_hunk;
237 if (reverse ? TT.oldlen : TT.newlen) for (;;) {
238 //FIXME: this performs 1-byte reads:
239 char *data = xmalloc_reads(TT.filein, NULL);
243 // Figure out which line of hunk to compare with next. (Skip lines
244 // of the hunk we'd be adding.)
245 while (plist && *plist->data == "+-"[reverse]) {
246 if (data && strcmp(data, plist->data+1) == 0) {
248 backwarn = TT.linenum;
249 if (option_mask32 & FLAG_IGNORE) {
261 if (PATCH_DEBUG) fdprintf(2, "INEOF\n");
263 // Does this hunk need to match EOF?
264 if (!plist && matcheof) break;
267 fdprintf(2,"Possibly reversed hunk %d at %ld\n",
268 TT.hunknum, TT.linenum);
270 // File ended before we found a place for this hunk.
275 if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
276 check = dlist_add(&buf, data);
278 // Compare this line with next expected line of hunk.
279 // todo: teach the strcmp() to ignore whitespace.
281 // A match can fail because the next line doesn't match, or because
282 // we hit the end of a hunk that needed EOF, and this isn't EOF.
284 // If match failed, flush first line of buffered data and
285 // recheck buffered data for a new match until we find one or run
289 while (plist && *plist->data == "+-"[reverse]) {
290 if (strcmp(check->data, plist->data+1) == 0
293 backwarn = TT.linenum;
294 if (option_mask32 & FLAG_IGNORE) {
301 if (!plist || strcmp(check->data, plist->data+1)) {
302 // Match failed. Write out first line of buffered data and
303 // recheck remaining buffered data for a new match.
306 fdprintf(2, "NOT: %s\n", plist ? plist->data : "EOF");
311 check->prev->next = buf;
312 buf->prev = check->prev;
314 plist = TT.current_hunk;
316 // If we've reached the end of the buffer without confirming a
317 // match, read more lines.
325 fdprintf(2, "MAYBE: %s\n", plist->data);
326 // This line matches. Advance plist, detect successful match.
328 if (!plist && !matcheof) goto out;
330 if (check == buf) break;
335 // We have a match. Emit changed data.
336 TT.state = "-+"[reverse ^ dummy_revert];
337 dlist_free(TT.current_hunk, do_line);
338 TT.current_hunk = NULL;
342 buf->prev->next = NULL;
343 dlist_free(buf, do_line);
349 // Read a patch file and find hunks, opening/creating/deleting files.
350 // Call apply_one_hunk() on each hunk.
352 // state 0: Not in a hunk, look for +++.
353 // state 1: Found +++ file indicator, look for @@
354 // state 2: In hunk: counting initial context lines
355 // state 3: In hunk: getting body
356 // Like GNU patch, we don't require a --- line before the +++, and
357 // also allow the --- after the +++ line.
359 int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
360 int patch_main(int argc UNUSED_PARAM, char **argv)
363 int reverse, state = 0;
364 char *oldname = NULL, *newname = NULL;
366 long oldlen = oldlen; /* for compiler */
367 long newlen = newlen; /* for compiler */
370 static const char patch_longopts[] ALIGN1 =
371 "reverse\0" No_argument "R"
372 "unified\0" No_argument "u"
373 "strip\0" Required_argument "p"
374 "input\0" Required_argument "i"
375 "forward\0" No_argument "N"
377 "remove-empty-files\0" No_argument "E" /*ignored*/
378 /* "debug" Required_argument "x" */
380 /* "Assume user knows what [s]he is doing, do not ask any questions": */
381 "force\0" No_argument "f" /*ignored*/
383 /* "Controls actions when a file is under RCS or SCCS control,
384 * and does not exist or is read-only and matches the default version,
385 * or when a file is under ClearCase control and does not exist..."
386 * IOW: rather obscure option.
387 * But Gentoo's portage does use -g0
389 "get\0" Required_argument "g" /*ignored*/
391 "dry-run\0" No_argument "\xfd"
393 "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/
394 "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/
402 opts = getopt32long(argv, FLAG_STR, patch_longopts, &opt_p, &opt_i);
404 opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
406 //bb_error_msg_and_die("opts:%x", opts);
409 reverse = opts & FLAG_REVERSE;
410 TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative!
411 TT.filein = TT.fileout = -1;
412 if (opts & FLAG_INPUT) {
413 xmove_fd(xopen_stdin(opt_i), STDIN_FILENO);
415 if (argv[0] && argv[1]) {
416 xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
420 // Loop through the lines in the patch
424 patchline = xmalloc_fgetline(stdin);
425 if (!patchline) break;
427 // Other versions of patch accept damaged patches,
428 // so we need to also.
431 patchline = xstrdup(" ");
434 // Are we assembling a hunk?
436 if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
437 dlist_add(&TT.current_hunk, patchline);
439 if (*patchline != '+') oldlen--;
440 if (*patchline != '-') newlen--;
443 if (*patchline==' ' && state==2) TT.context++;
446 // If we've consumed all expected hunk lines, apply the hunk.
448 if (!oldlen && !newlen) state = apply_one_hunk();
457 if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) {
458 char *s, **name = reverse ? &newname : &oldname;
461 if (*patchline == '+') {
462 name = reverse ? &oldname : &newname;
470 // Trim date from end of filename (if any). We don't care.
471 for (s = patchline+4; *s && *s!='\t'; s++)
472 if (*s=='\\' && s[1]) s++;
474 if (i>1900 && i<=1970)
475 *name = xstrdup("/dev/null");
478 *name = xstrdup(patchline+4);
482 // We defer actually opening the file because svn produces broken
483 // patches that don't signal they want to create a new file the
484 // way the patch man page says, so you have to read the first hunk
487 // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
488 // but a missing ,value means the value is 1.
489 } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) {
491 char *s = patchline+4;
493 // Read oldline[,oldlen] +newline[,newlen]
495 TT.oldlen = oldlen = TT.newlen = newlen = 1;
496 TT.oldline = strtol(s, &s, 10);
497 if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10);
498 TT.newline = strtol(s+2, &s, 10);
499 if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10);
501 if (oldlen < 1 && newlen < 1)
502 bb_error_msg_and_die("Really? %s", patchline);
507 // If the --- line is missing or malformed, either oldname
508 // or (for -R) newname could be NULL -- but not both. Like
509 // GNU patch, proceed based on the +++ line, and avoid SEGVs.
511 oldname = xstrdup("MISSING_FILENAME");
513 newname = xstrdup("MISSING_FILENAME");
515 // If this is the first hunk, open the file.
516 if (TT.filein == -1) {
517 int oldsum, newsum, empty = 0;
520 oldsum = TT.oldline + oldlen;
521 newsum = TT.newline + newlen;
523 name = reverse ? oldname : newname;
525 // We're deleting oldname if new file is /dev/null (before -p)
526 // or if new hunk is empty (zero context) after patching
527 if (strcmp(name, "/dev/null") == 0 || !(reverse ? oldsum : newsum)) {
528 name = reverse ? newname : oldname;
532 // Handle -p path truncation.
533 for (i = 0, s = name; *s;) {
534 if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i)
543 // If "patch FILE_TO_PATCH", completely ignore name from patch
548 // File is empty after the patches have been applied
550 if (option_mask32 & FLAG_RMEMPTY) {
551 // If flag -E or --remove-empty-files is set
552 printf("removing %s\n", name);
553 if (!(opts & FLAG_dry_run))
556 printf("patching file %s\n", name);
557 if (!(opts & FLAG_dry_run))
558 xclose(xopen(name, O_WRONLY | O_TRUNC));
560 // If we've got a file to open, do so.
561 } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) {
564 // If the old file was null, we're creating a new one.
565 if (strcmp(oldname, "/dev/null") == 0 || !oldsum) {
566 printf("creating %s\n", name);
567 if (!(opts & FLAG_dry_run)) {
568 s = strrchr(name, '/');
571 bb_make_directory(name, -1, FILEUTILS_RECUR);
574 TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR);
576 TT.filein = xopen("/dev/null", O_RDONLY);
579 printf("patching file %s\n", name);
580 TT.filein = xopen(name, O_RDONLY);
583 if (!(opts & FLAG_dry_run)) {
584 TT.tempname = xasprintf("%sXXXXXX", name);
585 TT.fileout = xmkstemp(TT.tempname);
586 // Set permissions of output file
587 fstat(TT.filein, &statbuf);
588 fchmod(TT.fileout, statbuf.st_mode);
590 TT.tempname = (char*)"";
591 TT.fileout = xopen("/dev/null", O_WRONLY);
603 // If we didn't continue above, discard this line.
609 if (ENABLE_FEATURE_CLEAN_UP) {