3
0

patch.c 17 KB

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