Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: |
| 2 | * |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 3 | * Apply a "universal" diff. |
| 4 | * Adapted from toybox's patch implementation. |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 5 | * |
| 6 | * Copyright 2007 Rob Landley <rob@landley.net> |
| 7 | * |
| 8 | * see http://www.opengroup.org/onlinepubs/009695399/utilities/patch.html |
| 9 | * (But only does -u, because who still cares about "ed"?) |
| 10 | * |
| 11 | * TODO: |
| 12 | * -b backup |
| 13 | * -l treat all whitespace as a single space |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 14 | * -d chdir first |
| 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 |
| 18 | * |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 19 | * -f force (no questions asked) |
| 20 | * -F fuzz (number, default 2) |
| 21 | * [file] which file to patch |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 22 | */ |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 23 | //config:config PATCH |
Denys Vlasenko | b097a84 | 2018-12-28 03:20:17 +0100 | [diff] [blame] | 24 | //config: bool "patch (9.4 kb)" |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 25 | //config: default y |
| 26 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 27 | //config: Apply a unified diff formatted patch. |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 28 | |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 29 | //applet:IF_PATCH(APPLET(patch, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 30 | |
| 31 | //kbuild:lib-$(CONFIG_PATCH) += patch.o |
| 32 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 33 | //usage:#define patch_trivial_usage |
Denys Vlasenko | 84d5edd | 2020-12-13 22:34:05 +0100 | [diff] [blame] | 34 | //usage: "[-RNE] [-p N] [-i DIFF] [ORIGFILE [PATCHFILE]]" |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 35 | //usage:#define patch_full_usage "\n\n" |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 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" |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 41 | //usage: IF_LONG_OPTS( |
| 42 | //usage: "\n --dry-run Don't actually change files" |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 43 | //usage: ) |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 44 | /* -u "interpret as unified diff" is supported but not documented: this info is not useful for --help */ |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 45 | //usage: |
| 46 | //usage:#define patch_example_usage |
| 47 | //usage: "$ patch -p1 < example.diff\n" |
| 48 | //usage: "$ patch -p0 -i example.diff" |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 49 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 50 | #include "libbb.h" |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 51 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 52 | #define PATCH_DEBUG 0 |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 53 | |
| 54 | // libbb candidate? |
| 55 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 56 | struct double_list { |
| 57 | struct double_list *next; |
| 58 | struct double_list *prev; |
| 59 | char *data; |
| 60 | }; |
| 61 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 62 | // Free all the elements of a linked list |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 63 | // Call freeit() on each element before freeing it. |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 64 | static void dlist_free(struct double_list *list, void (*freeit)(void *data)) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 65 | { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 66 | while (list) { |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 67 | void *pop = list; |
| 68 | list = list->next; |
| 69 | freeit(pop); |
| 70 | // Bail out also if list is circular. |
| 71 | if (list == pop) break; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 72 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 75 | // Add an entry before "list" element in (circular) doubly linked list |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 76 | static struct double_list *dlist_add(struct double_list **list, char *data) |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 77 | { |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 78 | struct double_list *llist; |
| 79 | struct double_list *line = xmalloc(sizeof(*line)); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 80 | |
| 81 | line->data = data; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 82 | llist = *list; |
| 83 | if (llist) { |
| 84 | struct double_list *p; |
| 85 | line->next = llist; |
| 86 | p = line->prev = llist->prev; |
| 87 | // (list is circular, we assume p is never NULL) |
| 88 | p->next = line; |
| 89 | llist->prev = line; |
| 90 | } else |
| 91 | *list = line->next = line->prev = line; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 92 | |
| 93 | return line; |
| 94 | } |
| 95 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 96 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 97 | struct globals { |
| 98 | char *infile; |
| 99 | long prefix; |
| 100 | |
| 101 | struct double_list *current_hunk; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 102 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 103 | long oldline, oldlen, newline, newlen; |
| 104 | long linenum; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 105 | int context, state, hunknum; |
| 106 | int filein, fileout; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 107 | char *tempname; |
| 108 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 109 | int exitval; |
| 110 | }; |
| 111 | #define TT (*ptr_to_globals) |
| 112 | #define INIT_TT() do { \ |
| 113 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(TT))); \ |
| 114 | } while (0) |
| 115 | |
| 116 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 117 | #define FLAG_STR "Rup:i:NEfg" |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 118 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 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) |
| 128 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 129 | |
| 130 | // Dispose of a line of input, either by writing it out or discarding it. |
| 131 | |
| 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 |
| 136 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 137 | static void do_line(void *data) |
| 138 | { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 139 | struct double_list *dlist = data; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 140 | |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 141 | if (TT.state > 1 && *dlist->data != TT.state) |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 142 | fdprintf(TT.state == 2 ? 2 : TT.fileout, |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 143 | "%s\n", dlist->data + (TT.state > 3 ? 1 : 0)); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 144 | |
| 145 | if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data); |
| 146 | |
| 147 | free(dlist->data); |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 148 | free(dlist); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void finish_oldfile(void) |
| 152 | { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 153 | if (TT.tempname) { |
| 154 | // Copy the rest of the data and replace the original with the copy. |
| 155 | char *temp; |
| 156 | |
| 157 | if (TT.filein != -1) { |
| 158 | bb_copyfd_eof(TT.filein, TT.fileout); |
| 159 | xclose(TT.filein); |
| 160 | } |
| 161 | xclose(TT.fileout); |
| 162 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 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); |
| 167 | free(temp); |
| 168 | free(TT.tempname); |
| 169 | } |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 170 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 171 | TT.tempname = NULL; |
| 172 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 173 | TT.fileout = TT.filein = -1; |
| 174 | } |
| 175 | |
| 176 | static void fail_hunk(void) |
| 177 | { |
| 178 | if (!TT.current_hunk) return; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 179 | |
| 180 | fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline); |
| 181 | TT.exitval = 1; |
| 182 | |
| 183 | // If we got to this point, we've seeked to the end. Discard changes to |
| 184 | // this file and advance to next file. |
| 185 | |
| 186 | TT.state = 2; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 187 | TT.current_hunk->prev->next = NULL; |
| 188 | dlist_free(TT.current_hunk, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 189 | TT.current_hunk = NULL; |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 190 | |
| 191 | // Abort the copy and delete the temporary file. |
| 192 | close(TT.filein); |
| 193 | close(TT.fileout); |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 194 | if (!ENABLE_LONG_OPTS || TT.tempname[0]) { /* not --dry-run? */ |
| 195 | unlink(TT.tempname); |
| 196 | free(TT.tempname); |
| 197 | } |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 198 | TT.tempname = NULL; |
| 199 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 200 | TT.state = 0; |
| 201 | } |
| 202 | |
| 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. |
| 209 | |
| 210 | static int apply_one_hunk(void) |
| 211 | { |
| 212 | struct double_list *plist, *buf = NULL, *check; |
| 213 | int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0; |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 214 | /* Do we try "dummy" revert to check whether |
| 215 | * to silently skip this hunk? Used to implement -N. |
| 216 | */ |
| 217 | int dummy_revert = 0; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 218 | |
| 219 | // Break doubly linked list so we can use singly linked traversal function. |
| 220 | TT.current_hunk->prev->next = NULL; |
| 221 | |
| 222 | // Match EOF if there aren't as many ending context lines as beginning |
| 223 | for (plist = TT.current_hunk; plist; plist = plist->next) { |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 224 | if (plist->data[0] == ' ') matcheof++; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 225 | else matcheof = 0; |
| 226 | if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data); |
| 227 | } |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 228 | matcheof = !matcheof || matcheof < TT.context; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 229 | |
| 230 | if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); |
| 231 | |
| 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 |
| 234 | // complete hunk. |
| 235 | plist = TT.current_hunk; |
| 236 | buf = NULL; |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 237 | if (reverse ? TT.oldlen : TT.newlen) for (;;) { |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 238 | //FIXME: this performs 1-byte reads: |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 239 | char *data = xmalloc_reads(TT.filein, NULL); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 240 | |
| 241 | TT.linenum++; |
| 242 | |
| 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]) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 246 | if (data && strcmp(data, plist->data+1) == 0) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 247 | if (!backwarn) { |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 248 | backwarn = TT.linenum; |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 249 | if (option_mask32 & FLAG_IGNORE) { |
| 250 | dummy_revert = 1; |
| 251 | reverse ^= 1; |
| 252 | continue; |
| 253 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | plist = plist->next; |
| 257 | } |
| 258 | |
| 259 | // Is this EOF? |
| 260 | if (!data) { |
| 261 | if (PATCH_DEBUG) fdprintf(2, "INEOF\n"); |
| 262 | |
| 263 | // Does this hunk need to match EOF? |
| 264 | if (!plist && matcheof) break; |
| 265 | |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 266 | if (backwarn) |
Denys Vlasenko | 5fa5c4b | 2020-06-23 21:28:19 +0200 | [diff] [blame] | 267 | fdprintf(2, "Possibly reversed hunk %d at %ld\n", |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 268 | TT.hunknum, TT.linenum); |
| 269 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 270 | // File ended before we found a place for this hunk. |
| 271 | fail_hunk(); |
| 272 | goto done; |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 276 | check = dlist_add(&buf, data); |
| 277 | |
| 278 | // Compare this line with next expected line of hunk. |
| 279 | // todo: teach the strcmp() to ignore whitespace. |
| 280 | |
| 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. |
| 283 | |
| 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 |
| 286 | // out of buffer. |
| 287 | |
| 288 | for (;;) { |
Aaro Koskinen | cb810c4 | 2016-11-22 00:19:51 +0200 | [diff] [blame] | 289 | while (plist && *plist->data == "+-"[reverse]) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 290 | if (strcmp(check->data, plist->data+1) == 0 |
| 291 | && !backwarn |
| 292 | ) { |
Aaro Koskinen | cb810c4 | 2016-11-22 00:19:51 +0200 | [diff] [blame] | 293 | backwarn = TT.linenum; |
| 294 | if (option_mask32 & FLAG_IGNORE) { |
| 295 | dummy_revert = 1; |
| 296 | reverse ^= 1; |
| 297 | } |
| 298 | } |
| 299 | plist = plist->next; |
| 300 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 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. |
| 304 | |
| 305 | if (PATCH_DEBUG) |
Aaro Koskinen | 2eff596 | 2016-11-22 00:19:50 +0200 | [diff] [blame] | 306 | fdprintf(2, "NOT: %s\n", plist ? plist->data : "EOF"); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 307 | |
| 308 | TT.state = 3; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 309 | check = buf; |
| 310 | buf = buf->next; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 311 | check->prev->next = buf; |
| 312 | buf->prev = check->prev; |
| 313 | do_line(check); |
| 314 | plist = TT.current_hunk; |
| 315 | |
| 316 | // If we've reached the end of the buffer without confirming a |
| 317 | // match, read more lines. |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 318 | if (check == buf) { |
| 319 | buf = NULL; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 320 | break; |
| 321 | } |
| 322 | check = buf; |
| 323 | } else { |
| 324 | if (PATCH_DEBUG) |
| 325 | fdprintf(2, "MAYBE: %s\n", plist->data); |
| 326 | // This line matches. Advance plist, detect successful match. |
| 327 | plist = plist->next; |
| 328 | if (!plist && !matcheof) goto out; |
| 329 | check = check->next; |
| 330 | if (check == buf) break; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | out: |
| 335 | // We have a match. Emit changed data. |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 336 | TT.state = "-+"[reverse ^ dummy_revert]; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 337 | dlist_free(TT.current_hunk, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 338 | TT.current_hunk = NULL; |
| 339 | TT.state = 1; |
| 340 | done: |
| 341 | if (buf) { |
| 342 | buf->prev->next = NULL; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 343 | dlist_free(buf, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | return TT.state; |
| 347 | } |
| 348 | |
| 349 | // Read a patch file and find hunks, opening/creating/deleting files. |
| 350 | // Call apply_one_hunk() on each hunk. |
| 351 | |
| 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 |
Denys Vlasenko | fe8b580 | 2015-03-11 18:01:34 +0100 | [diff] [blame] | 356 | // Like GNU patch, we don't require a --- line before the +++, and |
| 357 | // also allow the --- after the +++ line. |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 358 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 359 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 360 | int patch_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 361 | { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 362 | int opts; |
| 363 | int reverse, state = 0; |
| 364 | char *oldname = NULL, *newname = NULL; |
| 365 | char *opt_p, *opt_i; |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 366 | long oldlen = oldlen; /* for compiler */ |
| 367 | long newlen = newlen; /* for compiler */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 368 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 369 | #if ENABLE_LONG_OPTS |
| 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" |
| 376 | # if ENABLE_DESKTOP |
| 377 | "remove-empty-files\0" No_argument "E" /*ignored*/ |
| 378 | /* "debug" Required_argument "x" */ |
| 379 | # endif |
| 380 | /* "Assume user knows what [s]he is doing, do not ask any questions": */ |
| 381 | "force\0" No_argument "f" /*ignored*/ |
| 382 | # if ENABLE_DESKTOP |
| 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 |
| 388 | */ |
| 389 | "get\0" Required_argument "g" /*ignored*/ |
| 390 | # endif |
| 391 | "dry-run\0" No_argument "\xfd" |
| 392 | # if ENABLE_DESKTOP |
| 393 | "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/ |
| 394 | "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/ |
| 395 | # endif |
| 396 | ; |
| 397 | #endif |
| 398 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 399 | INIT_TT(); |
| 400 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 401 | #if ENABLE_LONG_OPTS |
| 402 | opts = getopt32long(argv, FLAG_STR, patch_longopts, &opt_p, &opt_i); |
| 403 | #else |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 404 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 405 | #endif |
| 406 | //bb_error_msg_and_die("opts:%x", opts); |
| 407 | |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 408 | argv += optind; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 409 | reverse = opts & FLAG_REVERSE; |
| 410 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 411 | TT.filein = TT.fileout = -1; |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 412 | if (opts & FLAG_INPUT) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 413 | xmove_fd(xopen_stdin(opt_i), STDIN_FILENO); |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 414 | } else { |
| 415 | if (argv[0] && argv[1]) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 416 | xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO); |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 417 | } |
| 418 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 419 | |
| 420 | // Loop through the lines in the patch |
Denys Vlasenko | 1e825ac | 2022-01-18 00:31:27 +0100 | [diff] [blame] | 421 | for (;;) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 422 | char *patchline; |
| 423 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 424 | patchline = xmalloc_fgetline(stdin); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 425 | if (!patchline) break; |
| 426 | |
| 427 | // Other versions of patch accept damaged patches, |
| 428 | // so we need to also. |
| 429 | if (!*patchline) { |
| 430 | free(patchline); |
| 431 | patchline = xstrdup(" "); |
| 432 | } |
| 433 | |
| 434 | // Are we assembling a hunk? |
| 435 | if (state >= 2) { |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 436 | if (*patchline == ' ' || *patchline == '+' || *patchline == '-') { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 437 | dlist_add(&TT.current_hunk, patchline); |
| 438 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 439 | if (*patchline != '+') oldlen--; |
| 440 | if (*patchline != '-') newlen--; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 441 | |
| 442 | // Context line? |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 443 | if (*patchline == ' ' && state == 2) TT.context++; |
| 444 | else state = 3; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 445 | |
| 446 | // If we've consumed all expected hunk lines, apply the hunk. |
| 447 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 448 | if (!oldlen && !newlen) state = apply_one_hunk(); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 449 | continue; |
| 450 | } |
| 451 | fail_hunk(); |
| 452 | state = 0; |
| 453 | continue; |
| 454 | } |
| 455 | |
| 456 | // Open a new file? |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 457 | if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 458 | char *s, **name = reverse ? &newname : &oldname; |
| 459 | int i; |
| 460 | |
| 461 | if (*patchline == '+') { |
| 462 | name = reverse ? &oldname : &newname; |
| 463 | state = 1; |
| 464 | } |
| 465 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 466 | finish_oldfile(); |
| 467 | |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 468 | if (!argv[0]) { |
| 469 | free(*name); |
| 470 | // Trim date from end of filename (if any). We don't care. |
| 471 | for (s = patchline+4; *s && *s!='\t'; s++) |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 472 | if (*s == '\\' && s[1]) s++; |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 473 | i = atoi(s); |
Denys Vlasenko | fe73c8d | 2022-08-30 16:41:17 +0200 | [diff] [blame] | 474 | if (i > 1900 && i <= 1970) |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 475 | *name = xstrdup("/dev/null"); |
| 476 | else { |
| 477 | *s = 0; |
| 478 | *name = xstrdup(patchline+4); |
| 479 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 480 | } |
| 481 | |
| 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 |
| 485 | // and _guess_. |
| 486 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 487 | // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@ |
| 488 | // but a missing ,value means the value is 1. |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 489 | } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 490 | int i; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 491 | char *s = patchline+4; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 492 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 493 | // Read oldline[,oldlen] +newline[,newlen] |
| 494 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 495 | TT.oldlen = oldlen = TT.newlen = newlen = 1; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 496 | TT.oldline = strtol(s, &s, 10); |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 497 | if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10); |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 498 | TT.newline = strtol(s+2, &s, 10); |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 499 | if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10); |
| 500 | |
| 501 | if (oldlen < 1 && newlen < 1) |
| 502 | bb_error_msg_and_die("Really? %s", patchline); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 503 | |
| 504 | TT.context = 0; |
| 505 | state = 2; |
| 506 | |
Denys Vlasenko | fe8b580 | 2015-03-11 18:01:34 +0100 | [diff] [blame] | 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. |
| 510 | if (!oldname) |
| 511 | oldname = xstrdup("MISSING_FILENAME"); |
| 512 | if (!newname) |
| 513 | newname = xstrdup("MISSING_FILENAME"); |
| 514 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 515 | // If this is the first hunk, open the file. |
| 516 | if (TT.filein == -1) { |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 517 | int oldsum, newsum, empty = 0; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 518 | char *name; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 519 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 520 | oldsum = TT.oldline + oldlen; |
| 521 | newsum = TT.newline + newlen; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 522 | |
| 523 | name = reverse ? oldname : newname; |
| 524 | |
| 525 | // We're deleting oldname if new file is /dev/null (before -p) |
| 526 | // or if new hunk is empty (zero context) after patching |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 527 | if (strcmp(name, "/dev/null") == 0 || !(reverse ? oldsum : newsum)) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 528 | name = reverse ? newname : oldname; |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 529 | empty = 1; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 530 | } |
| 531 | |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 532 | // Handle -p path truncation. |
Rob Landley | 39ec6a2 | 2011-10-10 19:59:38 +0200 | [diff] [blame] | 533 | for (i = 0, s = name; *s;) { |
| 534 | if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i) |
| 535 | break; |
| 536 | if (*s++ != '/') |
| 537 | continue; |
| 538 | while (*s == '/') |
| 539 | s++; |
| 540 | i++; |
| 541 | name = s; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 542 | } |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 543 | // If "patch FILE_TO_PATCH", completely ignore name from patch |
| 544 | if (argv[0]) |
| 545 | name = argv[0]; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 546 | |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 547 | if (empty) { |
| 548 | // File is empty after the patches have been applied |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 549 | state = 0; |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 550 | if (option_mask32 & FLAG_RMEMPTY) { |
| 551 | // If flag -E or --remove-empty-files is set |
| 552 | printf("removing %s\n", name); |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 553 | if (!(opts & FLAG_dry_run)) |
| 554 | xunlink(name); |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 555 | } else { |
| 556 | printf("patching file %s\n", name); |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 557 | if (!(opts & FLAG_dry_run)) |
| 558 | xclose(xopen(name, O_WRONLY | O_TRUNC)); |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 559 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 560 | // If we've got a file to open, do so. |
| 561 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 562 | struct stat statbuf; |
| 563 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 564 | // If the old file was null, we're creating a new one. |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 565 | if (strcmp(oldname, "/dev/null") == 0 || !oldsum) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 566 | printf("creating %s\n", name); |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 567 | if (!(opts & FLAG_dry_run)) { |
| 568 | s = strrchr(name, '/'); |
| 569 | if (s) { |
| 570 | *s = '\0'; |
| 571 | bb_make_directory(name, -1, FILEUTILS_RECUR); |
| 572 | *s = '/'; |
| 573 | } |
| 574 | TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR); |
| 575 | } else { |
| 576 | TT.filein = xopen("/dev/null", O_RDONLY); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 577 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 578 | } else { |
| 579 | printf("patching file %s\n", name); |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 580 | TT.filein = xopen(name, O_RDONLY); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 581 | } |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 582 | |
Denys Vlasenko | 7556835 | 2018-04-14 16:05:26 +0200 | [diff] [blame] | 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); |
| 589 | } else { |
| 590 | TT.tempname = (char*)""; |
| 591 | TT.fileout = xopen("/dev/null", O_WRONLY); |
| 592 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 593 | TT.linenum = 0; |
| 594 | TT.hunknum = 0; |
| 595 | } |
Denys Vlasenko | 5fa5c4b | 2020-06-23 21:28:19 +0200 | [diff] [blame] | 596 | fflush_all(); // make "patching file F" visible |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | TT.hunknum++; |
| 600 | |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | // If we didn't continue above, discard this line. |
| 605 | free(patchline); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 608 | finish_oldfile(); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 609 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 610 | if (ENABLE_FEATURE_CLEAN_UP) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 611 | free(oldname); |
| 612 | free(newname); |
| 613 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 614 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 615 | return TT.exitval; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 616 | } |