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 |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 18 | * --dry-run (regression!) |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 19 | * |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 20 | * -f force (no questions asked) |
| 21 | * -F fuzz (number, default 2) |
| 22 | * [file] which file to patch |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 23 | */ |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 24 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 25 | //config:config PATCH |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 26 | //config: bool "patch (9.1 kb)" |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 27 | //config: default y |
| 28 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 29 | //config: Apply a unified diff formatted patch. |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 30 | |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 31 | //applet:IF_PATCH(APPLET(patch, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 32 | |
| 33 | //kbuild:lib-$(CONFIG_PATCH) += patch.o |
| 34 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 35 | //usage:#define patch_trivial_usage |
| 36 | //usage: "[OPTIONS] [ORIGFILE [PATCHFILE]]" |
| 37 | //usage:#define patch_full_usage "\n\n" |
| 38 | //usage: IF_LONG_OPTS( |
| 39 | //usage: " -p,--strip N Strip N leading components from file names" |
| 40 | //usage: "\n -i,--input DIFF Read DIFF instead of stdin" |
| 41 | //usage: "\n -R,--reverse Reverse patch" |
| 42 | //usage: "\n -N,--forward Ignore already applied patches" |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 43 | /*usage: "\n --dry-run Don't actually change files" - TODO */ |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 44 | //usage: "\n -E,--remove-empty-files Remove output files if they become empty" |
| 45 | //usage: ) |
| 46 | //usage: IF_NOT_LONG_OPTS( |
| 47 | //usage: " -p N Strip N leading components from file names" |
| 48 | //usage: "\n -i DIFF Read DIFF instead of stdin" |
| 49 | //usage: "\n -R Reverse patch" |
| 50 | //usage: "\n -N Ignore already applied patches" |
| 51 | //usage: "\n -E Remove output files if they become empty" |
| 52 | //usage: ) |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 53 | /* -u "interpret as unified diff" is supported but not documented: this info is not useful for --help */ |
| 54 | /* -x "debug" is supported but does nothing */ |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 55 | //usage: |
| 56 | //usage:#define patch_example_usage |
| 57 | //usage: "$ patch -p1 < example.diff\n" |
| 58 | //usage: "$ patch -p0 -i example.diff" |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 59 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 60 | #include "libbb.h" |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 61 | |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 62 | |
| 63 | // libbb candidate? |
| 64 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 65 | struct double_list { |
| 66 | struct double_list *next; |
| 67 | struct double_list *prev; |
| 68 | char *data; |
| 69 | }; |
| 70 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 71 | // Free all the elements of a linked list |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 72 | // Call freeit() on each element before freeing it. |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 73 | 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] | 74 | { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 75 | while (list) { |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 76 | void *pop = list; |
| 77 | list = list->next; |
| 78 | freeit(pop); |
| 79 | // Bail out also if list is circular. |
| 80 | if (list == pop) break; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 81 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 84 | // Add an entry before "list" element in (circular) doubly linked list |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 85 | static struct double_list *dlist_add(struct double_list **list, char *data) |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 86 | { |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 87 | struct double_list *llist; |
| 88 | struct double_list *line = xmalloc(sizeof(*line)); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 89 | |
| 90 | line->data = data; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 91 | llist = *list; |
| 92 | if (llist) { |
| 93 | struct double_list *p; |
| 94 | line->next = llist; |
| 95 | p = line->prev = llist->prev; |
| 96 | // (list is circular, we assume p is never NULL) |
| 97 | p->next = line; |
| 98 | llist->prev = line; |
| 99 | } else |
| 100 | *list = line->next = line->prev = line; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 101 | |
| 102 | return line; |
| 103 | } |
| 104 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 105 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 106 | struct globals { |
| 107 | char *infile; |
| 108 | long prefix; |
| 109 | |
| 110 | struct double_list *current_hunk; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 111 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 112 | long oldline, oldlen, newline, newlen; |
| 113 | long linenum; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 114 | int context, state, hunknum; |
| 115 | int filein, fileout; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 116 | char *tempname; |
| 117 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 118 | int exitval; |
| 119 | }; |
| 120 | #define TT (*ptr_to_globals) |
| 121 | #define INIT_TT() do { \ |
| 122 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(TT))); \ |
| 123 | } while (0) |
| 124 | |
| 125 | |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 126 | #define FLAG_STR "Rup:i:NEx" |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 127 | /* FLAG_REVERSE must be == 1! Code uses this fact. */ |
| 128 | #define FLAG_REVERSE (1 << 0) |
| 129 | #define FLAG_u (1 << 1) |
| 130 | #define FLAG_PATHLEN (1 << 2) |
| 131 | #define FLAG_INPUT (1 << 3) |
Denys Vlasenko | a4160e1 | 2010-08-16 01:33:57 +0200 | [diff] [blame] | 132 | #define FLAG_IGNORE (1 << 4) |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 133 | #define FLAG_RMEMPTY (1 << 5) |
Denys Vlasenko | 416e978 | 2011-06-19 01:40:31 +0200 | [diff] [blame] | 134 | /* Enable this bit and use -x for debug output: */ |
| 135 | #define FLAG_DEBUG (0 << 6) |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 136 | |
| 137 | // Dispose of a line of input, either by writing it out or discarding it. |
| 138 | |
| 139 | // state < 2: just free |
| 140 | // state = 2: write whole line to stderr |
| 141 | // state = 3: write whole line to fileout |
| 142 | // state > 3: write line+1 to fileout when *line != state |
| 143 | |
| 144 | #define PATCH_DEBUG (option_mask32 & FLAG_DEBUG) |
| 145 | |
| 146 | static void do_line(void *data) |
| 147 | { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 148 | struct double_list *dlist = data; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 149 | |
| 150 | if (TT.state>1 && *dlist->data != TT.state) |
| 151 | fdprintf(TT.state == 2 ? 2 : TT.fileout, |
| 152 | "%s\n", dlist->data+(TT.state>3 ? 1 : 0)); |
| 153 | |
| 154 | if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data); |
| 155 | |
| 156 | free(dlist->data); |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 157 | free(dlist); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void finish_oldfile(void) |
| 161 | { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 162 | if (TT.tempname) { |
| 163 | // Copy the rest of the data and replace the original with the copy. |
| 164 | char *temp; |
| 165 | |
| 166 | if (TT.filein != -1) { |
| 167 | bb_copyfd_eof(TT.filein, TT.fileout); |
| 168 | xclose(TT.filein); |
| 169 | } |
| 170 | xclose(TT.fileout); |
| 171 | |
| 172 | temp = xstrdup(TT.tempname); |
| 173 | temp[strlen(temp) - 6] = '\0'; |
| 174 | rename(TT.tempname, temp); |
| 175 | free(temp); |
| 176 | |
| 177 | free(TT.tempname); |
| 178 | TT.tempname = NULL; |
| 179 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 180 | TT.fileout = TT.filein = -1; |
| 181 | } |
| 182 | |
| 183 | static void fail_hunk(void) |
| 184 | { |
| 185 | if (!TT.current_hunk) return; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 186 | |
| 187 | fdprintf(2, "Hunk %d FAILED %ld/%ld.\n", TT.hunknum, TT.oldline, TT.newline); |
| 188 | TT.exitval = 1; |
| 189 | |
| 190 | // If we got to this point, we've seeked to the end. Discard changes to |
| 191 | // this file and advance to next file. |
| 192 | |
| 193 | TT.state = 2; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 194 | TT.current_hunk->prev->next = NULL; |
| 195 | dlist_free(TT.current_hunk, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 196 | TT.current_hunk = NULL; |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 197 | |
| 198 | // Abort the copy and delete the temporary file. |
| 199 | close(TT.filein); |
| 200 | close(TT.fileout); |
| 201 | unlink(TT.tempname); |
| 202 | free(TT.tempname); |
| 203 | TT.tempname = NULL; |
| 204 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 205 | TT.state = 0; |
| 206 | } |
| 207 | |
| 208 | // Given a hunk of a unified diff, make the appropriate change to the file. |
| 209 | // This does not use the location information, but instead treats a hunk |
| 210 | // as a sort of regex. Copies data from input to output until it finds |
| 211 | // the change to be made, then outputs the changed data and returns. |
| 212 | // (Finding EOF first is an error.) This is a single pass operation, so |
| 213 | // multiple hunks must occur in order in the file. |
| 214 | |
| 215 | static int apply_one_hunk(void) |
| 216 | { |
| 217 | struct double_list *plist, *buf = NULL, *check; |
| 218 | int matcheof = 0, reverse = option_mask32 & FLAG_REVERSE, backwarn = 0; |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 219 | /* Do we try "dummy" revert to check whether |
| 220 | * to silently skip this hunk? Used to implement -N. |
| 221 | */ |
| 222 | int dummy_revert = 0; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 223 | |
| 224 | // Break doubly linked list so we can use singly linked traversal function. |
| 225 | TT.current_hunk->prev->next = NULL; |
| 226 | |
| 227 | // Match EOF if there aren't as many ending context lines as beginning |
| 228 | for (plist = TT.current_hunk; plist; plist = plist->next) { |
| 229 | if (plist->data[0]==' ') matcheof++; |
| 230 | else matcheof = 0; |
| 231 | if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data); |
| 232 | } |
Rob Landley | c5f7847 | 2011-08-22 04:58:32 +0200 | [diff] [blame] | 233 | matcheof = !matcheof || matcheof < TT.context; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 234 | |
| 235 | if (PATCH_DEBUG) fdprintf(2,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N'); |
| 236 | |
| 237 | // Loop through input data searching for this hunk. Match all context |
| 238 | // lines and all lines to be removed until we've found the end of a |
| 239 | // complete hunk. |
| 240 | plist = TT.current_hunk; |
| 241 | buf = NULL; |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 242 | if (reverse ? TT.oldlen : TT.newlen) for (;;) { |
Denys Vlasenko | 80c5b68 | 2011-05-08 21:21:10 +0200 | [diff] [blame] | 243 | char *data = xmalloc_reads(TT.filein, NULL); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 244 | |
| 245 | TT.linenum++; |
| 246 | |
| 247 | // Figure out which line of hunk to compare with next. (Skip lines |
| 248 | // of the hunk we'd be adding.) |
| 249 | while (plist && *plist->data == "+-"[reverse]) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 250 | if (data && strcmp(data, plist->data+1) == 0) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 251 | if (!backwarn) { |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 252 | backwarn = TT.linenum; |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 253 | if (option_mask32 & FLAG_IGNORE) { |
| 254 | dummy_revert = 1; |
| 255 | reverse ^= 1; |
| 256 | continue; |
| 257 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | plist = plist->next; |
| 261 | } |
| 262 | |
| 263 | // Is this EOF? |
| 264 | if (!data) { |
| 265 | if (PATCH_DEBUG) fdprintf(2, "INEOF\n"); |
| 266 | |
| 267 | // Does this hunk need to match EOF? |
| 268 | if (!plist && matcheof) break; |
| 269 | |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 270 | if (backwarn) |
| 271 | fdprintf(2,"Possibly reversed hunk %d at %ld\n", |
| 272 | TT.hunknum, TT.linenum); |
| 273 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 274 | // File ended before we found a place for this hunk. |
| 275 | fail_hunk(); |
| 276 | goto done; |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 280 | check = dlist_add(&buf, data); |
| 281 | |
| 282 | // Compare this line with next expected line of hunk. |
| 283 | // todo: teach the strcmp() to ignore whitespace. |
| 284 | |
| 285 | // A match can fail because the next line doesn't match, or because |
| 286 | // we hit the end of a hunk that needed EOF, and this isn't EOF. |
| 287 | |
| 288 | // If match failed, flush first line of buffered data and |
| 289 | // recheck buffered data for a new match until we find one or run |
| 290 | // out of buffer. |
| 291 | |
| 292 | for (;;) { |
Aaro Koskinen | cb810c4 | 2016-11-22 00:19:51 +0200 | [diff] [blame] | 293 | while (plist && *plist->data == "+-"[reverse]) { |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 294 | if (strcmp(check->data, plist->data+1) == 0 |
| 295 | && !backwarn |
| 296 | ) { |
Aaro Koskinen | cb810c4 | 2016-11-22 00:19:51 +0200 | [diff] [blame] | 297 | backwarn = TT.linenum; |
| 298 | if (option_mask32 & FLAG_IGNORE) { |
| 299 | dummy_revert = 1; |
| 300 | reverse ^= 1; |
| 301 | } |
| 302 | } |
| 303 | plist = plist->next; |
| 304 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 305 | if (!plist || strcmp(check->data, plist->data+1)) { |
| 306 | // Match failed. Write out first line of buffered data and |
| 307 | // recheck remaining buffered data for a new match. |
| 308 | |
| 309 | if (PATCH_DEBUG) |
Aaro Koskinen | 2eff596 | 2016-11-22 00:19:50 +0200 | [diff] [blame] | 310 | fdprintf(2, "NOT: %s\n", plist ? plist->data : "EOF"); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 311 | |
| 312 | TT.state = 3; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 313 | check = buf; |
| 314 | buf = buf->next; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 315 | check->prev->next = buf; |
| 316 | buf->prev = check->prev; |
| 317 | do_line(check); |
| 318 | plist = TT.current_hunk; |
| 319 | |
| 320 | // If we've reached the end of the buffer without confirming a |
| 321 | // match, read more lines. |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 322 | if (check == buf) { |
| 323 | buf = NULL; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | check = buf; |
| 327 | } else { |
| 328 | if (PATCH_DEBUG) |
| 329 | fdprintf(2, "MAYBE: %s\n", plist->data); |
| 330 | // This line matches. Advance plist, detect successful match. |
| 331 | plist = plist->next; |
| 332 | if (!plist && !matcheof) goto out; |
| 333 | check = check->next; |
| 334 | if (check == buf) break; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | out: |
| 339 | // We have a match. Emit changed data. |
Denys Vlasenko | cda8159 | 2010-08-17 01:31:40 +0200 | [diff] [blame] | 340 | TT.state = "-+"[reverse ^ dummy_revert]; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 341 | dlist_free(TT.current_hunk, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 342 | TT.current_hunk = NULL; |
| 343 | TT.state = 1; |
| 344 | done: |
| 345 | if (buf) { |
| 346 | buf->prev->next = NULL; |
Denys Vlasenko | 400ff22 | 2010-11-21 05:54:28 +0100 | [diff] [blame] | 347 | dlist_free(buf, do_line); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | return TT.state; |
| 351 | } |
| 352 | |
| 353 | // Read a patch file and find hunks, opening/creating/deleting files. |
| 354 | // Call apply_one_hunk() on each hunk. |
| 355 | |
| 356 | // state 0: Not in a hunk, look for +++. |
| 357 | // state 1: Found +++ file indicator, look for @@ |
| 358 | // state 2: In hunk: counting initial context lines |
| 359 | // state 3: In hunk: getting body |
Denys Vlasenko | fe8b580 | 2015-03-11 18:01:34 +0100 | [diff] [blame] | 360 | // Like GNU patch, we don't require a --- line before the +++, and |
| 361 | // also allow the --- after the +++ line. |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 362 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 363 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 364 | int patch_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 365 | { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 366 | int opts; |
| 367 | int reverse, state = 0; |
| 368 | char *oldname = NULL, *newname = NULL; |
| 369 | char *opt_p, *opt_i; |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 370 | long oldlen = oldlen; /* for compiler */ |
| 371 | long newlen = newlen; /* for compiler */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 372 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 373 | INIT_TT(); |
| 374 | |
| 375 | opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i); |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 376 | argv += optind; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 377 | reverse = opts & FLAG_REVERSE; |
| 378 | TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative! |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 379 | TT.filein = TT.fileout = -1; |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 380 | if (opts & FLAG_INPUT) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 381 | xmove_fd(xopen_stdin(opt_i), STDIN_FILENO); |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 382 | } else { |
| 383 | if (argv[0] && argv[1]) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 384 | xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO); |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 385 | } |
| 386 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 387 | |
| 388 | // Loop through the lines in the patch |
| 389 | for(;;) { |
| 390 | char *patchline; |
| 391 | |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 392 | patchline = xmalloc_fgetline(stdin); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 393 | if (!patchline) break; |
| 394 | |
| 395 | // Other versions of patch accept damaged patches, |
| 396 | // so we need to also. |
| 397 | if (!*patchline) { |
| 398 | free(patchline); |
| 399 | patchline = xstrdup(" "); |
| 400 | } |
| 401 | |
| 402 | // Are we assembling a hunk? |
| 403 | if (state >= 2) { |
| 404 | if (*patchline==' ' || *patchline=='+' || *patchline=='-') { |
| 405 | dlist_add(&TT.current_hunk, patchline); |
| 406 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 407 | if (*patchline != '+') oldlen--; |
| 408 | if (*patchline != '-') newlen--; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 409 | |
| 410 | // Context line? |
| 411 | if (*patchline==' ' && state==2) TT.context++; |
| 412 | else state=3; |
| 413 | |
| 414 | // If we've consumed all expected hunk lines, apply the hunk. |
| 415 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 416 | if (!oldlen && !newlen) state = apply_one_hunk(); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 417 | continue; |
| 418 | } |
| 419 | fail_hunk(); |
| 420 | state = 0; |
| 421 | continue; |
| 422 | } |
| 423 | |
| 424 | // Open a new file? |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 425 | if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 426 | char *s, **name = reverse ? &newname : &oldname; |
| 427 | int i; |
| 428 | |
| 429 | if (*patchline == '+') { |
| 430 | name = reverse ? &oldname : &newname; |
| 431 | state = 1; |
| 432 | } |
| 433 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 434 | finish_oldfile(); |
| 435 | |
Denys Vlasenko | e7b0a9e | 2010-08-22 05:39:15 +0200 | [diff] [blame] | 436 | if (!argv[0]) { |
| 437 | free(*name); |
| 438 | // Trim date from end of filename (if any). We don't care. |
| 439 | for (s = patchline+4; *s && *s!='\t'; s++) |
| 440 | if (*s=='\\' && s[1]) s++; |
| 441 | i = atoi(s); |
| 442 | if (i>1900 && i<=1970) |
| 443 | *name = xstrdup("/dev/null"); |
| 444 | else { |
| 445 | *s = 0; |
| 446 | *name = xstrdup(patchline+4); |
| 447 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | // We defer actually opening the file because svn produces broken |
| 451 | // patches that don't signal they want to create a new file the |
| 452 | // way the patch man page says, so you have to read the first hunk |
| 453 | // and _guess_. |
| 454 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 455 | // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@ |
| 456 | // but a missing ,value means the value is 1. |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 457 | } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 458 | int i; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 459 | char *s = patchline+4; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 460 | |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 461 | // Read oldline[,oldlen] +newline[,newlen] |
| 462 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 463 | TT.oldlen = oldlen = TT.newlen = newlen = 1; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 464 | TT.oldline = strtol(s, &s, 10); |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 465 | if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10); |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 466 | TT.newline = strtol(s+2, &s, 10); |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 467 | if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10); |
| 468 | |
| 469 | if (oldlen < 1 && newlen < 1) |
| 470 | bb_error_msg_and_die("Really? %s", patchline); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 471 | |
| 472 | TT.context = 0; |
| 473 | state = 2; |
| 474 | |
Denys Vlasenko | fe8b580 | 2015-03-11 18:01:34 +0100 | [diff] [blame] | 475 | // If the --- line is missing or malformed, either oldname |
| 476 | // or (for -R) newname could be NULL -- but not both. Like |
| 477 | // GNU patch, proceed based on the +++ line, and avoid SEGVs. |
| 478 | if (!oldname) |
| 479 | oldname = xstrdup("MISSING_FILENAME"); |
| 480 | if (!newname) |
| 481 | newname = xstrdup("MISSING_FILENAME"); |
| 482 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 483 | // If this is the first hunk, open the file. |
| 484 | if (TT.filein == -1) { |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 485 | int oldsum, newsum, empty = 0; |
Rob Landley | 760d0eb | 2010-08-13 16:40:21 +0200 | [diff] [blame] | 486 | char *name; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 487 | |
Rob Landley | 8027a20 | 2010-11-29 03:24:51 +0100 | [diff] [blame] | 488 | oldsum = TT.oldline + oldlen; |
| 489 | newsum = TT.newline + newlen; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 490 | |
| 491 | name = reverse ? oldname : newname; |
| 492 | |
| 493 | // We're deleting oldname if new file is /dev/null (before -p) |
| 494 | // or if new hunk is empty (zero context) after patching |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 495 | if (strcmp(name, "/dev/null") == 0 || !(reverse ? oldsum : newsum)) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 496 | name = reverse ? newname : oldname; |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 497 | empty = 1; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 500 | // Handle -p path truncation. |
Rob Landley | 39ec6a2 | 2011-10-10 19:59:38 +0200 | [diff] [blame] | 501 | for (i = 0, s = name; *s;) { |
| 502 | if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i) |
| 503 | break; |
| 504 | if (*s++ != '/') |
| 505 | continue; |
| 506 | while (*s == '/') |
| 507 | s++; |
| 508 | i++; |
| 509 | name = s; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 510 | } |
Denys Vlasenko | eb50910 | 2016-01-23 05:13:15 +0100 | [diff] [blame] | 511 | // If "patch FILE_TO_PATCH", completely ignore name from patch |
| 512 | if (argv[0]) |
| 513 | name = argv[0]; |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 514 | |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 515 | if (empty) { |
| 516 | // File is empty after the patches have been applied |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 517 | state = 0; |
Lukas Huba | 0818735 | 2010-10-21 00:43:00 +0200 | [diff] [blame] | 518 | if (option_mask32 & FLAG_RMEMPTY) { |
| 519 | // If flag -E or --remove-empty-files is set |
| 520 | printf("removing %s\n", name); |
| 521 | xunlink(name); |
| 522 | } else { |
| 523 | printf("patching file %s\n", name); |
| 524 | xclose(xopen(name, O_WRONLY | O_TRUNC)); |
| 525 | } |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 526 | // If we've got a file to open, do so. |
| 527 | } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) { |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 528 | struct stat statbuf; |
| 529 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 530 | // If the old file was null, we're creating a new one. |
Denys Vlasenko | 1d3a04a | 2016-11-28 01:22:57 +0100 | [diff] [blame] | 531 | if (strcmp(oldname, "/dev/null") == 0 || !oldsum) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 532 | printf("creating %s\n", name); |
| 533 | s = strrchr(name, '/'); |
| 534 | if (s) { |
| 535 | *s = 0; |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 536 | bb_make_directory(name, -1, FILEUTILS_RECUR); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 537 | *s = '/'; |
| 538 | } |
Denys Vlasenko | c05387d | 2010-10-18 02:38:27 +0200 | [diff] [blame] | 539 | TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 540 | } else { |
| 541 | printf("patching file %s\n", name); |
Rob Landley | 9d113ca | 2010-10-04 00:49:48 +0200 | [diff] [blame] | 542 | TT.filein = xopen(name, O_RDONLY); |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 543 | } |
Denys Vlasenko | b82ae98 | 2010-11-21 05:53:34 +0100 | [diff] [blame] | 544 | |
| 545 | TT.tempname = xasprintf("%sXXXXXX", name); |
| 546 | TT.fileout = xmkstemp(TT.tempname); |
| 547 | // Set permissions of output file |
| 548 | fstat(TT.filein, &statbuf); |
| 549 | fchmod(TT.fileout, statbuf.st_mode); |
| 550 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 551 | TT.linenum = 0; |
| 552 | TT.hunknum = 0; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | TT.hunknum++; |
| 557 | |
| 558 | continue; |
| 559 | } |
| 560 | |
| 561 | // If we didn't continue above, discard this line. |
| 562 | free(patchline); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 565 | finish_oldfile(); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 566 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 567 | if (ENABLE_FEATURE_CLEAN_UP) { |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 568 | free(oldname); |
| 569 | free(newname); |
| 570 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 571 | |
Rob Landley | 1bbc0cd | 2010-08-13 15:50:26 +0200 | [diff] [blame] | 572 | return TT.exitval; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 573 | } |