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