patch.c 15 KB

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