patch.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* patch.c - Apply a "universal" diff.
  2. *
  3. * Copyright 2007 Rob Landley <rob@landley.net>
  4. *
  5. * see http://opengroup.org/onlinepubs/9699919799/utilities/patch.html
  6. * (But only does -u, because who still cares about "ed"?)
  7. *
  8. * TODO:
  9. * -b backup
  10. * -N ignore already applied
  11. * -D define wrap #ifdef and #ifndef around changes
  12. * -o outfile output here instead of in place
  13. * -r rejectfile write rejected hunks to this file
  14. * -E remove empty files --remove-empty-files
  15. * git syntax (rename, etc)
  16. USE_PATCH(NEWTOY(patch, ">2(no-backup-if-mismatch)(dry-run)"USE_TOYBOX_DEBUG("x")"F#g#fulp#d:i:Rs(quiet)", TOYFLAG_USR|TOYFLAG_BIN))
  17. config PATCH
  18. bool "patch"
  19. default y
  20. help
  21. usage: patch [-Rlsu] [-d DIR] [-i PATCH] [-p DEPTH] [-F FUZZ] [--dry-run] [FILE [PATCH]]
  22. Apply a unified diff to one or more files.
  23. -d Modify files in DIR
  24. -i Input patch file (default=stdin)
  25. -l Loose match (ignore whitespace)
  26. -p Number of '/' to strip from start of file paths (default=all)
  27. -R Reverse patch
  28. -s Silent except for errors
  29. -u Ignored (only handles "unified" diffs)
  30. --dry-run Don't change files, just confirm patch applies
  31. This version of patch only handles unified diffs, and only modifies
  32. a file when all hunks to that file apply. Patch prints failed hunks
  33. to stderr, and exits with nonzero status if any hunks fail.
  34. A file compared against /dev/null (or with a date <= the epoch) is
  35. created/deleted as appropriate.
  36. */
  37. #define FOR_patch
  38. #include "toys.h"
  39. GLOBALS(
  40. char *i, *d;
  41. long p, g, F;
  42. void *current_hunk;
  43. long oldline, oldlen, newline, newlen, linenum, outnum;
  44. int context, state, filein, fileout, filepatch, hunknum;
  45. char *tempname;
  46. )
  47. // TODO xgetline() instead, but replace_tempfile() wants fd...
  48. char *get_line(int fd)
  49. {
  50. char c, *buf = NULL;
  51. long len = 0;
  52. for (;;) {
  53. if (1>read(fd, &c, 1)) break;
  54. if (!(len & 63)) buf=xrealloc(buf, len+65);
  55. if ((buf[len++]=c) == '\n') break;
  56. }
  57. if (buf) {
  58. buf[len]=0;
  59. if (buf[--len]=='\n') buf[len]=0;
  60. }
  61. return buf;
  62. }
  63. // Dispose of a line of input, either by writing it out or discarding it.
  64. // state < 2: just free
  65. // state = 2: write whole line to stderr
  66. // state = 3: write whole line to fileout
  67. // state > 3: write line+1 to fileout when *line != state
  68. static void do_line(void *data)
  69. {
  70. struct double_list *dlist = data;
  71. TT.outnum++;
  72. if (TT.state>1)
  73. if (0>dprintf(TT.state==2 ? 2 : TT.fileout,"%s\n",dlist->data+(TT.state>3)))
  74. perror_exit("write");
  75. if (FLAG(x))
  76. fprintf(stderr, "DO %d %ld: %s\n", TT.state, TT.outnum, dlist->data);
  77. llist_free_double(data);
  78. }
  79. static void finish_oldfile(void)
  80. {
  81. if (TT.tempname) replace_tempfile(TT.filein, TT.fileout, &TT.tempname);
  82. TT.fileout = TT.filein = -1;
  83. }
  84. static void fail_hunk(void)
  85. {
  86. if (!TT.current_hunk) return;
  87. fprintf(stderr, "Hunk %d FAILED %ld/%ld.\n",
  88. TT.hunknum, TT.oldline, TT.newline);
  89. toys.exitval = 1;
  90. // If we got to this point, we've seeked to the end. Discard changes to
  91. // this file and advance to next file.
  92. TT.state = 2;
  93. llist_traverse(TT.current_hunk, do_line);
  94. TT.current_hunk = NULL;
  95. if (!FLAG(dry_run)) delete_tempfile(TT.filein, TT.fileout, &TT.tempname);
  96. TT.state = 0;
  97. }
  98. // Compare ignoring whitespace. Just returns 0/1, no > or <
  99. static int loosecmp(char *aa, char *bb)
  100. {
  101. int a = 0, b = 0;
  102. for (;;) {
  103. while (isspace(aa[a])) a++;
  104. while (isspace(bb[b])) b++;
  105. if (aa[a] != bb[b]) return 1;
  106. if (!aa[a]) return 0;
  107. a++, b++;
  108. }
  109. }
  110. // Given a hunk of a unified diff, make the appropriate change to the file.
  111. // This does not use the location information, but instead treats a hunk
  112. // as a sort of regex. Copies data from input to output until it finds
  113. // the change to be made, then outputs the changed data and returns.
  114. // (Finding EOF first is an error.) This is a single pass operation, so
  115. // multiple hunks must occur in order in the file.
  116. static int apply_one_hunk(void)
  117. {
  118. struct double_list *plist, *buf = 0, *check;
  119. int matcheof, trail = 0, reverse = FLAG(R), backwarn = 0, allfuzz, fuzz, i;
  120. int (*lcmp)(char *aa, char *bb) = FLAG(l) ? (void *)loosecmp : (void *)strcmp;
  121. // Match EOF if there aren't as many ending context lines as beginning
  122. dlist_terminate(TT.current_hunk);
  123. for (fuzz = 0, plist = TT.current_hunk; plist; plist = plist->next) {
  124. char c = *plist->data, *s;
  125. if (c==' ') trail++;
  126. else trail = 0;
  127. // Only allow fuzz if 2 context lines have multiple nonwhitespace chars.
  128. // avoids the "all context was blank or } lines" issue. Removed lines
  129. // count as context since they're matched.
  130. if (c==' ' || c=="-+"[reverse]) {
  131. s = plist->data+1;
  132. while (isspace(*s)) s++;
  133. if (*s && s[1] && !isspace(s[1])) fuzz++;
  134. }
  135. if (FLAG(x)) fprintf(stderr, "HUNK:%s\n", plist->data);
  136. }
  137. matcheof = !trail || trail < TT.context;
  138. if (fuzz<2) allfuzz = 0;
  139. else allfuzz = FLAG(F) ? TT.F : (TT.context ? TT.context-1 : 0);
  140. if (FLAG(x)) fprintf(stderr,"MATCHEOF=%c\n", matcheof ? 'Y' : 'N');
  141. // Loop through input data searching for this hunk. Match all context
  142. // lines and lines to be removed until we've found end of complete hunk.
  143. plist = TT.current_hunk;
  144. fuzz = 0;
  145. for (;;) {
  146. char *data = get_line(TT.filein);
  147. // Figure out which line of hunk to compare with next. (Skip lines
  148. // of the hunk we'd be adding.)
  149. while (plist && *plist->data == "+-"[reverse]) {
  150. if (data && !lcmp(data, plist->data+1))
  151. if (!backwarn) backwarn = TT.linenum;
  152. plist = plist->next;
  153. }
  154. // Is this EOF?
  155. if (!data) {
  156. if (FLAG(x)) fprintf(stderr, "INEOF\n");
  157. // Does this hunk need to match EOF?
  158. if (!plist && matcheof) break;
  159. if (backwarn && !FLAG(s))
  160. fprintf(stderr, "Possibly reversed hunk %d at %ld\n",
  161. TT.hunknum, TT.linenum);
  162. // File ended before we found a place for this hunk.
  163. fail_hunk();
  164. goto done;
  165. } else {
  166. TT.linenum++;
  167. if (FLAG(x)) fprintf(stderr, "IN: %s\n", data);
  168. }
  169. check = dlist_add(&buf, data);
  170. // Compare this line with next expected line of hunk. Match can fail
  171. // because next line doesn't match, or because we hit end of a hunk that
  172. // needed EOF and this isn't EOF.
  173. for (i = 0;; i++) {
  174. if (!plist || lcmp(check->data, plist->data+1)) {
  175. // Match failed: can we fuzz it?
  176. if (plist && *plist->data == ' ' && fuzz<allfuzz) {
  177. if (FLAG(x))
  178. fprintf(stderr, "FUZZED: %ld %s\n", TT.linenum, plist->data);
  179. fuzz++;
  180. goto fuzzed;
  181. }
  182. if (FLAG(x)) {
  183. int bug = 0;
  184. if (!plist) fprintf(stderr, "NULL plist\n");
  185. else {
  186. while (plist->data[bug] == check->data[bug]) bug++;
  187. fprintf(stderr, "NOT(%d:%d!=%d): %s\n", bug, plist->data[bug],
  188. check->data[bug], plist->data);
  189. }
  190. }
  191. // If this hunk must match start of file, fail if it didn't.
  192. if (!TT.context || trail>TT.context) {
  193. fail_hunk();
  194. goto done;
  195. }
  196. // Write out first line of buffer and recheck rest for new match.
  197. TT.state = 3;
  198. do_line(check = dlist_pop(&buf));
  199. plist = TT.current_hunk;
  200. fuzz = 0;
  201. // If end of the buffer without finishing a match, read more lines.
  202. if (!buf) break;
  203. check = buf;
  204. } else {
  205. if (FLAG(x)) fprintf(stderr, "MAYBE: %s\n", plist->data);
  206. fuzzed:
  207. // This line matches. Advance plist, detect successful match.
  208. plist = plist->next;
  209. if (!plist && !matcheof) goto out;
  210. check = check->next;
  211. if (check == buf) break;
  212. }
  213. }
  214. }
  215. out:
  216. // We have a match. Emit changed data.
  217. TT.state = "-+"[reverse];
  218. while ((plist = dlist_pop(&TT.current_hunk))) {
  219. if (TT.state == *plist->data || *plist->data == ' ') {
  220. if (*plist->data == ' ') dprintf(TT.fileout, "%s\n", buf->data);
  221. llist_free_double(dlist_pop(&buf));
  222. } else dprintf(TT.fileout, "%s\n", plist->data+1);
  223. llist_free_double(plist);
  224. }
  225. TT.current_hunk = 0;
  226. TT.state = 1;
  227. done:
  228. llist_traverse(buf, do_line);
  229. return TT.state;
  230. }
  231. // read a filename that has been quoted or escaped
  232. static char *unquote_file(char *filename)
  233. {
  234. char *s = filename, *t;
  235. // Return copy of file that wasn't quoted
  236. if (*s++ != '"' || !*s) return xstrdup(filename);
  237. // quoted and escaped filenames are larger than the original
  238. for (t = filename = xmalloc(strlen(s) + 1); *s != '"'; s++) {
  239. if (!s[1]) error_exit("bad %s", filename);
  240. // don't accept escape sequences unless the filename is quoted
  241. if (*s != '\\') *t++ = *s;
  242. else if (*++s >= '0' && *s < '8') {
  243. *t++ = strtoul(s, &s, 8);
  244. s--;
  245. } else {
  246. if (!(*t = unescape(*s))) *t = *s;;
  247. t++;
  248. }
  249. }
  250. *t = 0;
  251. return filename;
  252. }
  253. // Read a patch file and find hunks, opening/creating/deleting files.
  254. // Call apply_one_hunk() on each hunk.
  255. // state 0: Not in a hunk, look for +++.
  256. // state 1: Found +++ file indicator, look for @@
  257. // state 2: In hunk: counting initial context lines
  258. // state 3: In hunk: getting body
  259. void patch_main(void)
  260. {
  261. int reverse = FLAG(R), state = 0, patchlinenum = 0, strip = 0;
  262. char *oldname = NULL, *newname = NULL;
  263. if (toys.optc == 2) TT.i = toys.optargs[1];
  264. if (TT.i) TT.filepatch = xopenro(TT.i);
  265. TT.filein = TT.fileout = -1;
  266. if (TT.d) xchdir(TT.d);
  267. // Loop through the lines in the patch
  268. for (;;) {
  269. char *patchline;
  270. patchline = get_line(TT.filepatch);
  271. if (!patchline) break;
  272. // Other versions of patch accept damaged patches, so we need to also.
  273. if (strip || !patchlinenum++) {
  274. int len = strlen(patchline);
  275. if (len && patchline[len-1] == '\r') {
  276. if (!strip && !FLAG(s)) fprintf(stderr, "Removing DOS newlines\n");
  277. strip = 1;
  278. patchline[len-1]=0;
  279. }
  280. }
  281. if (!*patchline) {
  282. free(patchline);
  283. patchline = xstrdup(" ");
  284. }
  285. // Are we assembling a hunk?
  286. if (state >= 2) {
  287. if (*patchline==' ' || *patchline=='+' || *patchline=='-') {
  288. dlist_add((void *)&TT.current_hunk, patchline);
  289. if (*patchline != '+') TT.oldlen--;
  290. if (*patchline != '-') TT.newlen--;
  291. // Context line?
  292. if (*patchline==' ' && state==2) TT.context++;
  293. else state=3;
  294. // If we've consumed all expected hunk lines, apply the hunk.
  295. if (!TT.oldlen && !TT.newlen) state = apply_one_hunk();
  296. continue;
  297. }
  298. dlist_terminate(TT.current_hunk);
  299. fail_hunk();
  300. state = 0;
  301. continue;
  302. }
  303. // Open a new file?
  304. if (!strncmp("--- ", patchline, 4) || !strncmp("+++ ", patchline, 4)) {
  305. char *s, **name = &oldname;
  306. int i;
  307. if (*patchline == '+') {
  308. name = &newname;
  309. state = 1;
  310. }
  311. free(*name);
  312. finish_oldfile();
  313. // Trim date from end of filename (if any). We don't care.
  314. for (s = patchline+4; *s && *s!='\t'; s++);
  315. i = atoi(s);
  316. if (i>1900 && i<=1970) *name = xstrdup("/dev/null");
  317. else {
  318. *s = 0;
  319. *name = unquote_file(patchline+4);
  320. }
  321. // We defer actually opening the file because svn produces broken
  322. // patches that don't signal they want to create a new file the
  323. // way the patch man page says, so you have to read the first hunk
  324. // and _guess_.
  325. // Start a new hunk? Usually @@ -oldline,oldlen +newline,newlen @@
  326. // but a missing ,value means the value is 1.
  327. } else if (state == 1 && !strncmp("@@ -", patchline, 4)) {
  328. int i;
  329. char *s = patchline+4;
  330. // Read oldline[,oldlen] +newline[,newlen]
  331. TT.oldlen = TT.newlen = 1;
  332. TT.oldline = strtol(s, &s, 10);
  333. if (*s == ',') TT.oldlen=strtol(s+1, &s, 10);
  334. TT.newline = strtol(s+2, &s, 10);
  335. if (*s == ',') TT.newlen = strtol(s+1, &s, 10);
  336. TT.context = 0;
  337. state = 2;
  338. // If this is the first hunk, open the file.
  339. if (TT.filein == -1) {
  340. int oldsum, newsum, del = 0;
  341. char *name;
  342. oldsum = TT.oldline + TT.oldlen;
  343. newsum = TT.newline + TT.newlen;
  344. // If an original file was provided on the command line, it overrides
  345. // *all* files mentioned in the patch, not just the first.
  346. if (toys.optc) {
  347. char **which = reverse ? &oldname : &newname;
  348. free(*which);
  349. *which = strdup(toys.optargs[0]);
  350. // The supplied path should be taken literally with or without -p.
  351. toys.optflags |= FLAG_p;
  352. TT.p = 0;
  353. }
  354. name = reverse ? oldname : newname;
  355. // We're deleting oldname if new file is /dev/null (before -p)
  356. // or if new hunk is empty (zero context) after patching
  357. if (!strcmp(name, "/dev/null") || !(reverse ? oldsum : newsum)) {
  358. name = reverse ? newname : oldname;
  359. del++;
  360. }
  361. // handle -p path truncation.
  362. for (i = 0, s = name; *s;) {
  363. if (FLAG(p) && TT.p == i) break;
  364. if (*s++ != '/') continue;
  365. while (*s == '/') s++;
  366. name = s;
  367. i++;
  368. }
  369. if (del) {
  370. if (!FLAG(s)) printf("removing %s\n", name);
  371. xunlink(name);
  372. state = 0;
  373. // If we've got a file to open, do so.
  374. } else if (!FLAG(p) || i <= TT.p) {
  375. // If the old file was null, we're creating a new one.
  376. if ((!strcmp(oldname, "/dev/null") || !oldsum) && access(name, F_OK))
  377. {
  378. if (!FLAG(s)) printf("creating %s\n", name);
  379. if (mkpath(name)) perror_exit("mkpath %s", name);
  380. TT.filein = xcreate(name, O_CREAT|O_EXCL|O_RDWR, 0666);
  381. } else {
  382. if (!FLAG(s)) printf("patching %s\n", name);
  383. TT.filein = xopenro(name);
  384. }
  385. if (FLAG(dry_run)) TT.fileout = xopen("/dev/null", O_RDWR);
  386. else TT.fileout = copy_tempfile(TT.filein, name, &TT.tempname);
  387. TT.linenum = TT.outnum = TT.hunknum = 0;
  388. }
  389. }
  390. TT.hunknum++;
  391. continue;
  392. }
  393. // If we didn't continue above, discard this line.
  394. free(patchline);
  395. }
  396. finish_oldfile();
  397. if (CFG_TOYBOX_FREE) {
  398. close(TT.filepatch);
  399. free(oldname);
  400. free(newname);
  401. }
  402. }