patch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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)) {
  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. if (!plist || strcmp(check->data, plist->data+1)) {
  241. // Match failed. Write out first line of buffered data and
  242. // recheck remaining buffered data for a new match.
  243. if (PATCH_DEBUG)
  244. fdprintf(2, "NOT: %s\n", plist->data);
  245. TT.state = 3;
  246. check = buf;
  247. buf = buf->next;
  248. check->prev->next = buf;
  249. buf->prev = check->prev;
  250. do_line(check);
  251. plist = TT.current_hunk;
  252. // If we've reached the end of the buffer without confirming a
  253. // match, read more lines.
  254. if (check == buf) {
  255. buf = NULL;
  256. break;
  257. }
  258. check = buf;
  259. } else {
  260. if (PATCH_DEBUG)
  261. fdprintf(2, "MAYBE: %s\n", plist->data);
  262. // This line matches. Advance plist, detect successful match.
  263. plist = plist->next;
  264. if (!plist && !matcheof) goto out;
  265. check = check->next;
  266. if (check == buf) break;
  267. }
  268. }
  269. }
  270. out:
  271. // We have a match. Emit changed data.
  272. TT.state = "-+"[reverse ^ dummy_revert];
  273. dlist_free(TT.current_hunk, do_line);
  274. TT.current_hunk = NULL;
  275. TT.state = 1;
  276. done:
  277. if (buf) {
  278. buf->prev->next = NULL;
  279. dlist_free(buf, do_line);
  280. }
  281. return TT.state;
  282. }
  283. // Read a patch file and find hunks, opening/creating/deleting files.
  284. // Call apply_one_hunk() on each hunk.
  285. // state 0: Not in a hunk, look for +++.
  286. // state 1: Found +++ file indicator, look for @@
  287. // state 2: In hunk: counting initial context lines
  288. // state 3: In hunk: getting body
  289. int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  290. int patch_main(int argc UNUSED_PARAM, char **argv)
  291. {
  292. int opts;
  293. int reverse, state = 0;
  294. char *oldname = NULL, *newname = NULL;
  295. char *opt_p, *opt_i;
  296. long oldlen = oldlen; /* for compiler */
  297. long newlen = newlen; /* for compiler */
  298. INIT_TT();
  299. opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
  300. argv += optind;
  301. reverse = opts & FLAG_REVERSE;
  302. TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative!
  303. TT.filein = TT.fileout = -1;
  304. if (opts & FLAG_INPUT) {
  305. xmove_fd(xopen_stdin(opt_i), STDIN_FILENO);
  306. } else {
  307. if (argv[0] && argv[1]) {
  308. xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
  309. }
  310. }
  311. if (argv[0]) {
  312. oldname = xstrdup(argv[0]);
  313. newname = xstrdup(argv[0]);
  314. }
  315. // Loop through the lines in the patch
  316. for(;;) {
  317. char *patchline;
  318. patchline = xmalloc_fgetline(stdin);
  319. if (!patchline) break;
  320. // Other versions of patch accept damaged patches,
  321. // so we need to also.
  322. if (!*patchline) {
  323. free(patchline);
  324. patchline = xstrdup(" ");
  325. }
  326. // Are we assembling a hunk?
  327. if (state >= 2) {
  328. if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
  329. dlist_add(&TT.current_hunk, patchline);
  330. if (*patchline != '+') oldlen--;
  331. if (*patchline != '-') newlen--;
  332. // Context line?
  333. if (*patchline==' ' && state==2) TT.context++;
  334. else state=3;
  335. // If we've consumed all expected hunk lines, apply the hunk.
  336. if (!oldlen && !newlen) state = apply_one_hunk();
  337. continue;
  338. }
  339. fail_hunk();
  340. state = 0;
  341. continue;
  342. }
  343. // Open a new file?
  344. if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
  345. char *s, **name = reverse ? &newname : &oldname;
  346. int i;
  347. if (*patchline == '+') {
  348. name = reverse ? &oldname : &newname;
  349. state = 1;
  350. }
  351. finish_oldfile();
  352. if (!argv[0]) {
  353. free(*name);
  354. // Trim date from end of filename (if any). We don't care.
  355. for (s = patchline+4; *s && *s!='\t'; s++)
  356. if (*s=='\\' && s[1]) s++;
  357. i = atoi(s);
  358. if (i>1900 && i<=1970)
  359. *name = xstrdup("/dev/null");
  360. else {
  361. *s = 0;
  362. *name = xstrdup(patchline+4);
  363. }
  364. }
  365. // We defer actually opening the file because svn produces broken
  366. // patches that don't signal they want to create a new file the
  367. // way the patch man page says, so you have to read the first hunk
  368. // and _guess_.
  369. // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
  370. // but a missing ,value means the value is 1.
  371. } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
  372. int i;
  373. char *s = patchline+4;
  374. // Read oldline[,oldlen] +newline[,newlen]
  375. TT.oldlen = oldlen = TT.newlen = newlen = 1;
  376. TT.oldline = strtol(s, &s, 10);
  377. if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10);
  378. TT.newline = strtol(s+2, &s, 10);
  379. if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10);
  380. if (oldlen < 1 && newlen < 1)
  381. bb_error_msg_and_die("Really? %s", patchline);
  382. TT.context = 0;
  383. state = 2;
  384. // If this is the first hunk, open the file.
  385. if (TT.filein == -1) {
  386. int oldsum, newsum, empty = 0;
  387. char *name;
  388. oldsum = TT.oldline + oldlen;
  389. newsum = TT.newline + newlen;
  390. name = reverse ? oldname : newname;
  391. // We're deleting oldname if new file is /dev/null (before -p)
  392. // or if new hunk is empty (zero context) after patching
  393. if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) {
  394. name = reverse ? newname : oldname;
  395. empty++;
  396. }
  397. // handle -p path truncation.
  398. for (i = 0, s = name; *s;) {
  399. if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i)
  400. break;
  401. if (*s++ != '/')
  402. continue;
  403. while (*s == '/')
  404. s++;
  405. i++;
  406. name = s;
  407. }
  408. if (empty) {
  409. // File is empty after the patches have been applied
  410. state = 0;
  411. if (option_mask32 & FLAG_RMEMPTY) {
  412. // If flag -E or --remove-empty-files is set
  413. printf("removing %s\n", name);
  414. xunlink(name);
  415. } else {
  416. printf("patching file %s\n", name);
  417. xclose(xopen(name, O_WRONLY | O_TRUNC));
  418. }
  419. // If we've got a file to open, do so.
  420. } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) {
  421. struct stat statbuf;
  422. // If the old file was null, we're creating a new one.
  423. if (!strcmp(oldname, "/dev/null") || !oldsum) {
  424. printf("creating %s\n", name);
  425. s = strrchr(name, '/');
  426. if (s) {
  427. *s = 0;
  428. bb_make_directory(name, -1, FILEUTILS_RECUR);
  429. *s = '/';
  430. }
  431. TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR);
  432. } else {
  433. printf("patching file %s\n", name);
  434. TT.filein = xopen(name, O_RDONLY);
  435. }
  436. TT.tempname = xasprintf("%sXXXXXX", name);
  437. TT.fileout = xmkstemp(TT.tempname);
  438. // Set permissions of output file
  439. fstat(TT.filein, &statbuf);
  440. fchmod(TT.fileout, statbuf.st_mode);
  441. TT.linenum = 0;
  442. TT.hunknum = 0;
  443. }
  444. }
  445. TT.hunknum++;
  446. continue;
  447. }
  448. // If we didn't continue above, discard this line.
  449. free(patchline);
  450. }
  451. finish_oldfile();
  452. if (ENABLE_FEATURE_CLEAN_UP) {
  453. free(oldname);
  454. free(newname);
  455. }
  456. return TT.exitval;
  457. }