blob: f90d6854d9fdc4d00e37eab8d006f5417810bcc0 [file] [log] [blame]
Rob Landley1bbc0cd2010-08-13 15:50:26 +02001/* vi: set sw=4 ts=4:
2 *
Denys Vlasenkob82ae982010-11-21 05:53:34 +01003 * Apply a "universal" diff.
4 * Adapted from toybox's patch implementation.
Rob Landley1bbc0cd2010-08-13 15:50:26 +02005 *
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 Landley1bbc0cd2010-08-13 15:50:26 +020014 * -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 Landley1bbc0cd2010-08-13 15:50:26 +020019 * -f force (no questions asked)
20 * -F fuzz (number, default 2)
21 * [file] which file to patch
Denys Vlasenkob82ae982010-11-21 05:53:34 +010022 */
Denys Vlasenkob82ae982010-11-21 05:53:34 +010023//config:config PATCH
Denys Vlasenkob097a842018-12-28 03:20:17 +010024//config: bool "patch (9.4 kb)"
Denys Vlasenkob82ae982010-11-21 05:53:34 +010025//config: default y
26//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020027//config: Apply a unified diff formatted patch.
Rob Landley1bbc0cd2010-08-13 15:50:26 +020028
Denys Vlasenko416e9782011-06-19 01:40:31 +020029//applet:IF_PATCH(APPLET(patch, BB_DIR_USR_BIN, BB_SUID_DROP))
30
31//kbuild:lib-$(CONFIG_PATCH) += patch.o
32
Denys Vlasenkob82ae982010-11-21 05:53:34 +010033//usage:#define patch_trivial_usage
Denys Vlasenko84d5edd2020-12-13 22:34:05 +010034//usage: "[-RNE] [-p N] [-i DIFF] [ORIGFILE [PATCHFILE]]"
Denys Vlasenkob82ae982010-11-21 05:53:34 +010035//usage:#define patch_full_usage "\n\n"
Denys Vlasenkob82ae982010-11-21 05:53:34 +010036//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 Vlasenko75568352018-04-14 16:05:26 +020041//usage: IF_LONG_OPTS(
42//usage: "\n --dry-run Don't actually change files"
Denys Vlasenkob82ae982010-11-21 05:53:34 +010043//usage: )
Denys Vlasenko416e9782011-06-19 01:40:31 +020044/* -u "interpret as unified diff" is supported but not documented: this info is not useful for --help */
Denys Vlasenkob82ae982010-11-21 05:53:34 +010045//usage:
46//usage:#define patch_example_usage
47//usage: "$ patch -p1 < example.diff\n"
48//usage: "$ patch -p0 -i example.diff"
Rob Landley1bbc0cd2010-08-13 15:50:26 +020049
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000050#include "libbb.h"
Glenn L McGrath655d8142003-06-22 15:32:41 +000051
Denys Vlasenko75568352018-04-14 16:05:26 +020052#define PATCH_DEBUG 0
Denys Vlasenko400ff222010-11-21 05:54:28 +010053
54// libbb candidate?
55
Rob Landley1bbc0cd2010-08-13 15:50:26 +020056struct double_list {
57 struct double_list *next;
58 struct double_list *prev;
59 char *data;
60};
61
Rob Landley1bbc0cd2010-08-13 15:50:26 +020062// Free all the elements of a linked list
Denys Vlasenko400ff222010-11-21 05:54:28 +010063// Call freeit() on each element before freeing it.
Rob Landleyc5f78472011-08-22 04:58:32 +020064static void dlist_free(struct double_list *list, void (*freeit)(void *data))
Glenn L McGrath655d8142003-06-22 15:32:41 +000065{
Rob Landley1bbc0cd2010-08-13 15:50:26 +020066 while (list) {
Denys Vlasenko400ff222010-11-21 05:54:28 +010067 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 McGrath655d8142003-06-22 15:32:41 +000072 }
Glenn L McGrath655d8142003-06-22 15:32:41 +000073}
74
Denys Vlasenko400ff222010-11-21 05:54:28 +010075// Add an entry before "list" element in (circular) doubly linked list
Rob Landleyc5f78472011-08-22 04:58:32 +020076static struct double_list *dlist_add(struct double_list **list, char *data)
Rob Landley1bbc0cd2010-08-13 15:50:26 +020077{
Denys Vlasenko400ff222010-11-21 05:54:28 +010078 struct double_list *llist;
79 struct double_list *line = xmalloc(sizeof(*line));
Rob Landley1bbc0cd2010-08-13 15:50:26 +020080
81 line->data = data;
Denys Vlasenko400ff222010-11-21 05:54:28 +010082 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 Landley1bbc0cd2010-08-13 15:50:26 +020092
93 return line;
94}
95
Rob Landley1bbc0cd2010-08-13 15:50:26 +020096
Rob Landley1bbc0cd2010-08-13 15:50:26 +020097struct globals {
98 char *infile;
99 long prefix;
100
101 struct double_list *current_hunk;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100102
Rob Landley760d0eb2010-08-13 16:40:21 +0200103 long oldline, oldlen, newline, newlen;
104 long linenum;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100105 int context, state, hunknum;
106 int filein, fileout;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200107 char *tempname;
108
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200109 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 Vlasenko75568352018-04-14 16:05:26 +0200117#define FLAG_STR "Rup:i:NEfg"
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200118/* FLAG_REVERSE must be == 1! Code uses this fact. */
Denys Vlasenko75568352018-04-14 16:05:26 +0200119#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 Landley1bbc0cd2010-08-13 15:50:26 +0200129
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 Landley1bbc0cd2010-08-13 15:50:26 +0200137static void do_line(void *data)
138{
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100139 struct double_list *dlist = data;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200140
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200141 if (TT.state > 1 && *dlist->data != TT.state)
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200142 fdprintf(TT.state == 2 ? 2 : TT.fileout,
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200143 "%s\n", dlist->data + (TT.state > 3 ? 1 : 0));
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200144
145 if (PATCH_DEBUG) fdprintf(2, "DO %d: %s\n", TT.state, dlist->data);
146
147 free(dlist->data);
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100148 free(dlist);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200149}
150
151static void finish_oldfile(void)
152{
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100153 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 Vlasenko75568352018-04-14 16:05:26 +0200163 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 Vlasenkob82ae982010-11-21 05:53:34 +0100170
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100171 TT.tempname = NULL;
172 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200173 TT.fileout = TT.filein = -1;
174}
175
176static void fail_hunk(void)
177{
178 if (!TT.current_hunk) return;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200179
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 Vlasenko400ff222010-11-21 05:54:28 +0100187 TT.current_hunk->prev->next = NULL;
188 dlist_free(TT.current_hunk, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200189 TT.current_hunk = NULL;
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100190
191 // Abort the copy and delete the temporary file.
192 close(TT.filein);
193 close(TT.fileout);
Denys Vlasenko75568352018-04-14 16:05:26 +0200194 if (!ENABLE_LONG_OPTS || TT.tempname[0]) { /* not --dry-run? */
195 unlink(TT.tempname);
196 free(TT.tempname);
197 }
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100198 TT.tempname = NULL;
199
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200200 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
210static 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 Vlasenkocda81592010-08-17 01:31:40 +0200214 /* 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 Landley1bbc0cd2010-08-13 15:50:26 +0200218
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 Vlasenkofe73c8d2022-08-30 16:41:17 +0200224 if (plist->data[0] == ' ') matcheof++;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200225 else matcheof = 0;
226 if (PATCH_DEBUG) fdprintf(2, "HUNK:%s\n", plist->data);
227 }
Rob Landleyc5f78472011-08-22 04:58:32 +0200228 matcheof = !matcheof || matcheof < TT.context;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200229
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 Landley8027a202010-11-29 03:24:51 +0100237 if (reverse ? TT.oldlen : TT.newlen) for (;;) {
Denys Vlasenko75568352018-04-14 16:05:26 +0200238//FIXME: this performs 1-byte reads:
Denys Vlasenko80c5b682011-05-08 21:21:10 +0200239 char *data = xmalloc_reads(TT.filein, NULL);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200240
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 Vlasenko1d3a04a2016-11-28 01:22:57 +0100246 if (data && strcmp(data, plist->data+1) == 0) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200247 if (!backwarn) {
Rob Landley9d113ca2010-10-04 00:49:48 +0200248 backwarn = TT.linenum;
Denys Vlasenkocda81592010-08-17 01:31:40 +0200249 if (option_mask32 & FLAG_IGNORE) {
250 dummy_revert = 1;
251 reverse ^= 1;
252 continue;
253 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200254 }
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 Landley9d113ca2010-10-04 00:49:48 +0200266 if (backwarn)
Denys Vlasenko5fa5c4b2020-06-23 21:28:19 +0200267 fdprintf(2, "Possibly reversed hunk %d at %ld\n",
Rob Landley9d113ca2010-10-04 00:49:48 +0200268 TT.hunknum, TT.linenum);
269
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200270 // File ended before we found a place for this hunk.
271 fail_hunk();
272 goto done;
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100273 }
274
275 if (PATCH_DEBUG) fdprintf(2, "IN: %s\n", data);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200276 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 Koskinencb810c42016-11-22 00:19:51 +0200289 while (plist && *plist->data == "+-"[reverse]) {
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100290 if (strcmp(check->data, plist->data+1) == 0
291 && !backwarn
292 ) {
Aaro Koskinencb810c42016-11-22 00:19:51 +0200293 backwarn = TT.linenum;
294 if (option_mask32 & FLAG_IGNORE) {
295 dummy_revert = 1;
296 reverse ^= 1;
297 }
298 }
299 plist = plist->next;
300 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200301 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 Koskinen2eff5962016-11-22 00:19:50 +0200306 fdprintf(2, "NOT: %s\n", plist ? plist->data : "EOF");
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200307
308 TT.state = 3;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100309 check = buf;
310 buf = buf->next;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200311 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 Vlasenkob82ae982010-11-21 05:53:34 +0100318 if (check == buf) {
319 buf = NULL;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200320 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 }
334out:
335 // We have a match. Emit changed data.
Denys Vlasenkocda81592010-08-17 01:31:40 +0200336 TT.state = "-+"[reverse ^ dummy_revert];
Denys Vlasenko400ff222010-11-21 05:54:28 +0100337 dlist_free(TT.current_hunk, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200338 TT.current_hunk = NULL;
339 TT.state = 1;
340done:
341 if (buf) {
342 buf->prev->next = NULL;
Denys Vlasenko400ff222010-11-21 05:54:28 +0100343 dlist_free(buf, do_line);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200344 }
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 Vlasenkofe8b5802015-03-11 18:01:34 +0100356// Like GNU patch, we don't require a --- line before the +++, and
357// also allow the --- after the +++ line.
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200358
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000359int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000360int patch_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath655d8142003-06-22 15:32:41 +0000361{
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200362 int opts;
363 int reverse, state = 0;
364 char *oldname = NULL, *newname = NULL;
365 char *opt_p, *opt_i;
Rob Landley8027a202010-11-29 03:24:51 +0100366 long oldlen = oldlen; /* for compiler */
367 long newlen = newlen; /* for compiler */
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000368
Denys Vlasenko75568352018-04-14 16:05:26 +0200369#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 Landley1bbc0cd2010-08-13 15:50:26 +0200399 INIT_TT();
400
Denys Vlasenko75568352018-04-14 16:05:26 +0200401#if ENABLE_LONG_OPTS
402 opts = getopt32long(argv, FLAG_STR, patch_longopts, &opt_p, &opt_i);
403#else
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200404 opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
Denys Vlasenko75568352018-04-14 16:05:26 +0200405#endif
406 //bb_error_msg_and_die("opts:%x", opts);
407
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200408 argv += optind;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200409 reverse = opts & FLAG_REVERSE;
410 TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative!
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200411 TT.filein = TT.fileout = -1;
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200412 if (opts & FLAG_INPUT) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100413 xmove_fd(xopen_stdin(opt_i), STDIN_FILENO);
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200414 } else {
415 if (argv[0] && argv[1]) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100416 xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200417 }
418 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200419
420 // Loop through the lines in the patch
Denys Vlasenko1e825ac2022-01-18 00:31:27 +0100421 for (;;) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200422 char *patchline;
423
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100424 patchline = xmalloc_fgetline(stdin);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200425 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 Vlasenkofe73c8d2022-08-30 16:41:17 +0200436 if (*patchline == ' ' || *patchline == '+' || *patchline == '-') {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200437 dlist_add(&TT.current_hunk, patchline);
438
Rob Landley8027a202010-11-29 03:24:51 +0100439 if (*patchline != '+') oldlen--;
440 if (*patchline != '-') newlen--;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200441
442 // Context line?
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200443 if (*patchline == ' ' && state == 2) TT.context++;
444 else state = 3;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200445
446 // If we've consumed all expected hunk lines, apply the hunk.
447
Rob Landley8027a202010-11-29 03:24:51 +0100448 if (!oldlen && !newlen) state = apply_one_hunk();
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200449 continue;
450 }
451 fail_hunk();
452 state = 0;
453 continue;
454 }
455
456 // Open a new file?
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100457 if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200458 char *s, **name = reverse ? &newname : &oldname;
459 int i;
460
461 if (*patchline == '+') {
462 name = reverse ? &oldname : &newname;
463 state = 1;
464 }
465
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200466 finish_oldfile();
467
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200468 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 Vlasenkofe73c8d2022-08-30 16:41:17 +0200472 if (*s == '\\' && s[1]) s++;
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200473 i = atoi(s);
Denys Vlasenkofe73c8d2022-08-30 16:41:17 +0200474 if (i > 1900 && i <= 1970)
Denys Vlasenkoe7b0a9e2010-08-22 05:39:15 +0200475 *name = xstrdup("/dev/null");
476 else {
477 *s = 0;
478 *name = xstrdup(patchline+4);
479 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200480 }
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 Landley760d0eb2010-08-13 16:40:21 +0200487 // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
488 // but a missing ,value means the value is 1.
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100489 } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200490 int i;
Rob Landley760d0eb2010-08-13 16:40:21 +0200491 char *s = patchline+4;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200492
Rob Landley760d0eb2010-08-13 16:40:21 +0200493 // Read oldline[,oldlen] +newline[,newlen]
494
Rob Landley8027a202010-11-29 03:24:51 +0100495 TT.oldlen = oldlen = TT.newlen = newlen = 1;
Rob Landley760d0eb2010-08-13 16:40:21 +0200496 TT.oldline = strtol(s, &s, 10);
Rob Landley8027a202010-11-29 03:24:51 +0100497 if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10);
Rob Landley760d0eb2010-08-13 16:40:21 +0200498 TT.newline = strtol(s+2, &s, 10);
Rob Landley8027a202010-11-29 03:24:51 +0100499 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 Landley1bbc0cd2010-08-13 15:50:26 +0200503
504 TT.context = 0;
505 state = 2;
506
Denys Vlasenkofe8b5802015-03-11 18:01:34 +0100507 // 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 Landley1bbc0cd2010-08-13 15:50:26 +0200515 // If this is the first hunk, open the file.
516 if (TT.filein == -1) {
Lukas Huba08187352010-10-21 00:43:00 +0200517 int oldsum, newsum, empty = 0;
Rob Landley760d0eb2010-08-13 16:40:21 +0200518 char *name;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200519
Rob Landley8027a202010-11-29 03:24:51 +0100520 oldsum = TT.oldline + oldlen;
521 newsum = TT.newline + newlen;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200522
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 Vlasenko1d3a04a2016-11-28 01:22:57 +0100527 if (strcmp(name, "/dev/null") == 0 || !(reverse ? oldsum : newsum)) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200528 name = reverse ? newname : oldname;
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100529 empty = 1;
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200530 }
531
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100532 // Handle -p path truncation.
Rob Landley39ec6a22011-10-10 19:59:38 +0200533 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 Landley1bbc0cd2010-08-13 15:50:26 +0200542 }
Denys Vlasenkoeb509102016-01-23 05:13:15 +0100543 // If "patch FILE_TO_PATCH", completely ignore name from patch
544 if (argv[0])
545 name = argv[0];
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200546
Lukas Huba08187352010-10-21 00:43:00 +0200547 if (empty) {
548 // File is empty after the patches have been applied
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200549 state = 0;
Lukas Huba08187352010-10-21 00:43:00 +0200550 if (option_mask32 & FLAG_RMEMPTY) {
551 // If flag -E or --remove-empty-files is set
552 printf("removing %s\n", name);
Denys Vlasenko75568352018-04-14 16:05:26 +0200553 if (!(opts & FLAG_dry_run))
554 xunlink(name);
Lukas Huba08187352010-10-21 00:43:00 +0200555 } else {
556 printf("patching file %s\n", name);
Denys Vlasenko75568352018-04-14 16:05:26 +0200557 if (!(opts & FLAG_dry_run))
558 xclose(xopen(name, O_WRONLY | O_TRUNC));
Lukas Huba08187352010-10-21 00:43:00 +0200559 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200560 // If we've got a file to open, do so.
561 } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) {
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100562 struct stat statbuf;
563
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200564 // If the old file was null, we're creating a new one.
Denys Vlasenko1d3a04a2016-11-28 01:22:57 +0100565 if (strcmp(oldname, "/dev/null") == 0 || !oldsum) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200566 printf("creating %s\n", name);
Denys Vlasenko75568352018-04-14 16:05:26 +0200567 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 Landley1bbc0cd2010-08-13 15:50:26 +0200577 }
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200578 } else {
579 printf("patching file %s\n", name);
Rob Landley9d113ca2010-10-04 00:49:48 +0200580 TT.filein = xopen(name, O_RDONLY);
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200581 }
Denys Vlasenkob82ae982010-11-21 05:53:34 +0100582
Denys Vlasenko75568352018-04-14 16:05:26 +0200583 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 Landley1bbc0cd2010-08-13 15:50:26 +0200593 TT.linenum = 0;
594 TT.hunknum = 0;
595 }
Denys Vlasenko5fa5c4b2020-06-23 21:28:19 +0200596 fflush_all(); // make "patching file F" visible
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200597 }
598
599 TT.hunknum++;
600
601 continue;
602 }
603
604 // If we didn't continue above, discard this line.
605 free(patchline);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000606 }
607
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200608 finish_oldfile();
Glenn L McGrath655d8142003-06-22 15:32:41 +0000609
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200610 if (ENABLE_FEATURE_CLEAN_UP) {
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200611 free(oldname);
612 free(newname);
613 }
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000614
Rob Landley1bbc0cd2010-08-13 15:50:26 +0200615 return TT.exitval;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000616}