3
0

patch.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. // Like GNU patch, we don't require a --- line before the +++, and
  290. // also allow the --- after the +++ line.
  291. int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  292. int patch_main(int argc UNUSED_PARAM, char **argv)
  293. {
  294. int opts;
  295. int reverse, state = 0;
  296. char *oldname = NULL, *newname = NULL;
  297. char *opt_p, *opt_i;
  298. long oldlen = oldlen; /* for compiler */
  299. long newlen = newlen; /* for compiler */
  300. INIT_TT();
  301. opts = getopt32(argv, FLAG_STR, &opt_p, &opt_i);
  302. argv += optind;
  303. reverse = opts & FLAG_REVERSE;
  304. TT.prefix = (opts & FLAG_PATHLEN) ? xatoi(opt_p) : 0; // can be negative!
  305. TT.filein = TT.fileout = -1;
  306. if (opts & FLAG_INPUT) {
  307. xmove_fd(xopen_stdin(opt_i), STDIN_FILENO);
  308. } else {
  309. if (argv[0] && argv[1]) {
  310. xmove_fd(xopen_stdin(argv[1]), STDIN_FILENO);
  311. }
  312. }
  313. // Loop through the lines in the patch
  314. for(;;) {
  315. char *patchline;
  316. patchline = xmalloc_fgetline(stdin);
  317. if (!patchline) break;
  318. // Other versions of patch accept damaged patches,
  319. // so we need to also.
  320. if (!*patchline) {
  321. free(patchline);
  322. patchline = xstrdup(" ");
  323. }
  324. // Are we assembling a hunk?
  325. if (state >= 2) {
  326. if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
  327. dlist_add(&TT.current_hunk, patchline);
  328. if (*patchline != '+') oldlen--;
  329. if (*patchline != '-') newlen--;
  330. // Context line?
  331. if (*patchline==' ' && state==2) TT.context++;
  332. else state=3;
  333. // If we've consumed all expected hunk lines, apply the hunk.
  334. if (!oldlen && !newlen) state = apply_one_hunk();
  335. continue;
  336. }
  337. fail_hunk();
  338. state = 0;
  339. continue;
  340. }
  341. // Open a new file?
  342. if (is_prefixed_with(patchline, "--- ") || is_prefixed_with(patchline, "+++ ")) {
  343. char *s, **name = reverse ? &newname : &oldname;
  344. int i;
  345. if (*patchline == '+') {
  346. name = reverse ? &oldname : &newname;
  347. state = 1;
  348. }
  349. finish_oldfile();
  350. if (!argv[0]) {
  351. free(*name);
  352. // Trim date from end of filename (if any). We don't care.
  353. for (s = patchline+4; *s && *s!='\t'; s++)
  354. if (*s=='\\' && s[1]) s++;
  355. i = atoi(s);
  356. if (i>1900 && i<=1970)
  357. *name = xstrdup("/dev/null");
  358. else {
  359. *s = 0;
  360. *name = xstrdup(patchline+4);
  361. }
  362. }
  363. // We defer actually opening the file because svn produces broken
  364. // patches that don't signal they want to create a new file the
  365. // way the patch man page says, so you have to read the first hunk
  366. // and _guess_.
  367. // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
  368. // but a missing ,value means the value is 1.
  369. } else if (state == 1 && is_prefixed_with(patchline, "@@ -")) {
  370. int i;
  371. char *s = patchline+4;
  372. // Read oldline[,oldlen] +newline[,newlen]
  373. TT.oldlen = oldlen = TT.newlen = newlen = 1;
  374. TT.oldline = strtol(s, &s, 10);
  375. if (*s == ',') TT.oldlen = oldlen = strtol(s+1, &s, 10);
  376. TT.newline = strtol(s+2, &s, 10);
  377. if (*s == ',') TT.newlen = newlen = strtol(s+1, &s, 10);
  378. if (oldlen < 1 && newlen < 1)
  379. bb_error_msg_and_die("Really? %s", patchline);
  380. TT.context = 0;
  381. state = 2;
  382. // If the --- line is missing or malformed, either oldname
  383. // or (for -R) newname could be NULL -- but not both. Like
  384. // GNU patch, proceed based on the +++ line, and avoid SEGVs.
  385. if (!oldname)
  386. oldname = xstrdup("MISSING_FILENAME");
  387. if (!newname)
  388. newname = xstrdup("MISSING_FILENAME");
  389. // If this is the first hunk, open the file.
  390. if (TT.filein == -1) {
  391. int oldsum, newsum, empty = 0;
  392. char *name;
  393. oldsum = TT.oldline + oldlen;
  394. newsum = TT.newline + newlen;
  395. name = reverse ? oldname : newname;
  396. // We're deleting oldname if new file is /dev/null (before -p)
  397. // or if new hunk is empty (zero context) after patching
  398. if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) {
  399. name = reverse ? newname : oldname;
  400. empty = 1;
  401. }
  402. // Handle -p path truncation.
  403. for (i = 0, s = name; *s;) {
  404. if ((option_mask32 & FLAG_PATHLEN) && TT.prefix == i)
  405. break;
  406. if (*s++ != '/')
  407. continue;
  408. while (*s == '/')
  409. s++;
  410. i++;
  411. name = s;
  412. }
  413. // If "patch FILE_TO_PATCH", completely ignore name from patch
  414. if (argv[0])
  415. name = argv[0];
  416. if (empty) {
  417. // File is empty after the patches have been applied
  418. state = 0;
  419. if (option_mask32 & FLAG_RMEMPTY) {
  420. // If flag -E or --remove-empty-files is set
  421. printf("removing %s\n", name);
  422. xunlink(name);
  423. } else {
  424. printf("patching file %s\n", name);
  425. xclose(xopen(name, O_WRONLY | O_TRUNC));
  426. }
  427. // If we've got a file to open, do so.
  428. } else if (!(option_mask32 & FLAG_PATHLEN) || i <= TT.prefix) {
  429. struct stat statbuf;
  430. // If the old file was null, we're creating a new one.
  431. if (!strcmp(oldname, "/dev/null") || !oldsum) {
  432. printf("creating %s\n", name);
  433. s = strrchr(name, '/');
  434. if (s) {
  435. *s = 0;
  436. bb_make_directory(name, -1, FILEUTILS_RECUR);
  437. *s = '/';
  438. }
  439. TT.filein = xopen(name, O_CREAT|O_EXCL|O_RDWR);
  440. } else {
  441. printf("patching file %s\n", name);
  442. TT.filein = xopen(name, O_RDONLY);
  443. }
  444. TT.tempname = xasprintf("%sXXXXXX", name);
  445. TT.fileout = xmkstemp(TT.tempname);
  446. // Set permissions of output file
  447. fstat(TT.filein, &statbuf);
  448. fchmod(TT.fileout, statbuf.st_mode);
  449. TT.linenum = 0;
  450. TT.hunknum = 0;
  451. }
  452. }
  453. TT.hunknum++;
  454. continue;
  455. }
  456. // If we didn't continue above, discard this line.
  457. free(patchline);
  458. }
  459. finish_oldfile();
  460. if (ENABLE_FEATURE_CLEAN_UP) {
  461. free(oldname);
  462. free(newname);
  463. }
  464. return TT.exitval;
  465. }