patch.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * busybox patch applet to handle the unified diff format.
  4. * Copyright (C) 2003 Glenn McGrath
  5. *
  6. * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  7. *
  8. * This applet is written to work with patches generated by GNU diff,
  9. * where there is equivalent functionality busybox patch shall behave
  10. * as per GNU patch.
  11. *
  12. * There is a SUSv3 specification for patch, however it looks to be
  13. * incomplete, it doesnt even mention unified diff format.
  14. * http://www.opengroup.org/onlinepubs/007904975/utilities/patch.html
  15. *
  16. * Issues
  17. * - Non-interactive
  18. * - Patches must apply cleanly or patch (not just one hunk) will fail.
  19. * - Reject file isnt saved
  20. */
  21. #include "libbb.h"
  22. static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count)
  23. {
  24. while (src_stream && lines_count) {
  25. char *line;
  26. line = xmalloc_fgets(src_stream);
  27. if (line == NULL) {
  28. break;
  29. }
  30. if (fputs(line, dst_stream) == EOF) {
  31. bb_perror_msg_and_die("error writing to new file");
  32. }
  33. free(line);
  34. lines_count--;
  35. }
  36. return lines_count;
  37. }
  38. /* If patch_level is -1 it will remove all directory names
  39. * char *line must be greater than 4 chars
  40. * returns NULL if the file doesnt exist or error
  41. * returns malloc'ed filename
  42. * NB: frees 1st argument!
  43. */
  44. static char *extract_filename(char *line, int patch_level, const char *pat)
  45. {
  46. char *temp = NULL, *filename_start_ptr = line + 4;
  47. if (strncmp(line, pat, 4) == 0) {
  48. /* Terminate string at end of source filename */
  49. line[strcspn(line, "\t\n\r")] = '\0';
  50. /* Skip over (patch_level) number of leading directories */
  51. while (patch_level--) {
  52. temp = strchr(filename_start_ptr, '/');
  53. if (!temp)
  54. break;
  55. filename_start_ptr = temp + 1;
  56. }
  57. temp = xstrdup(filename_start_ptr);
  58. }
  59. free(line);
  60. return temp;
  61. }
  62. int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  63. int patch_main(int argc UNUSED_PARAM, char **argv)
  64. {
  65. struct stat saved_stat;
  66. char *patch_line;
  67. FILE *patch_file;
  68. int patch_level;
  69. int ret = 0;
  70. char plus = '+';
  71. unsigned opt;
  72. enum {
  73. OPT_R = (1 << 2),
  74. OPT_N = (1 << 3),
  75. /*OPT_f = (1 << 4), ignored */
  76. /*OPT_E = (1 << 5), ignored, this is the default */
  77. /*OPT_g = (1 << 6), ignored */
  78. OPT_dry_run = (1 << 7) * ENABLE_LONG_OPTS,
  79. };
  80. xfunc_error_retval = 2;
  81. {
  82. const char *p = "-1";
  83. const char *i = "-"; /* compat */
  84. #if ENABLE_LONG_OPTS
  85. static const char patch_longopts[] ALIGN1 =
  86. "strip\0" Required_argument "p"
  87. "input\0" Required_argument "i"
  88. "reverse\0" No_argument "R"
  89. "forward\0" No_argument "N"
  90. /* "Assume user knows what [s]he is doing, do not ask any questions": */
  91. "force\0" No_argument "f" /*ignored*/
  92. # if ENABLE_DESKTOP
  93. "remove-empty-files\0" No_argument "E" /*ignored*/
  94. /* "Controls actions when a file is under RCS or SCCS control,
  95. * and does not exist or is read-only and matches the default version,
  96. * or when a file is under ClearCase control and does not exist..."
  97. * IOW: rather obscure option.
  98. * But Gentoo's portage does use -g0 */
  99. "get\0" Required_argument "g" /*ignored*/
  100. # endif
  101. "dry-run\0" No_argument "\xfd"
  102. # if ENABLE_DESKTOP
  103. "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/
  104. "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/
  105. # endif
  106. ;
  107. applet_long_options = patch_longopts;
  108. #endif
  109. /* -f,-E,-g are ignored */
  110. opt = getopt32(argv, "p:i:RN""fEg:", &p, &i, NULL);
  111. if (opt & OPT_R)
  112. plus = '-';
  113. patch_level = xatoi(p); /* can be negative! */
  114. patch_file = xfopen_stdin(i);
  115. }
  116. patch_line = xmalloc_fgetline(patch_file);
  117. while (patch_line) {
  118. FILE *src_stream;
  119. FILE *dst_stream;
  120. //char *old_filename;
  121. char *new_filename;
  122. char *backup_filename = NULL;
  123. unsigned src_cur_line = 1;
  124. unsigned dst_cur_line = 0;
  125. unsigned dst_beg_line;
  126. unsigned bad_hunk_count = 0;
  127. unsigned hunk_count = 0;
  128. smallint copy_trailing_lines_flag = 0;
  129. /* Skip everything upto the "---" marker
  130. * No need to parse the lines "Only in <dir>", and "diff <args>"
  131. */
  132. do {
  133. /* Extract the filename used before the patch was generated */
  134. new_filename = extract_filename(patch_line, patch_level, "--- ");
  135. // was old_filename above
  136. patch_line = xmalloc_fgetline(patch_file);
  137. if (!patch_line) goto quit;
  138. } while (!new_filename);
  139. free(new_filename); // "source" filename is irrelevant
  140. new_filename = extract_filename(patch_line, patch_level, "+++ ");
  141. if (!new_filename) {
  142. bb_error_msg_and_die("invalid patch");
  143. }
  144. /* Get access rights from the file to be patched */
  145. if (stat(new_filename, &saved_stat) != 0) {
  146. char *slash = strrchr(new_filename, '/');
  147. if (slash) {
  148. /* Create leading directories */
  149. *slash = '\0';
  150. bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
  151. *slash = '/';
  152. }
  153. src_stream = NULL;
  154. saved_stat.st_mode = 0644;
  155. } else if (!(opt & OPT_dry_run)) {
  156. backup_filename = xasprintf("%s.orig", new_filename);
  157. xrename(new_filename, backup_filename);
  158. src_stream = xfopen_for_read(backup_filename);
  159. } else
  160. src_stream = xfopen_for_read(new_filename);
  161. if (opt & OPT_dry_run) {
  162. dst_stream = xfopen_for_write("/dev/null");
  163. } else {
  164. dst_stream = xfopen_for_write(new_filename);
  165. fchmod(fileno(dst_stream), saved_stat.st_mode);
  166. }
  167. printf("patching file %s\n", new_filename);
  168. /* Handle all hunks for this file */
  169. patch_line = xmalloc_fgets(patch_file);
  170. while (patch_line) {
  171. unsigned count;
  172. unsigned src_beg_line;
  173. unsigned hunk_offset_start;
  174. unsigned src_last_line = 1;
  175. unsigned dst_last_line = 1;
  176. if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
  177. && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
  178. ) {
  179. /* No more hunks for this file */
  180. break;
  181. }
  182. if (plus != '+') {
  183. /* reverse patch */
  184. unsigned tmp = src_last_line;
  185. src_last_line = dst_last_line;
  186. dst_last_line = tmp;
  187. tmp = src_beg_line;
  188. src_beg_line = dst_beg_line;
  189. dst_beg_line = tmp;
  190. }
  191. hunk_count++;
  192. if (src_beg_line && dst_beg_line) {
  193. /* Copy unmodified lines upto start of hunk */
  194. /* src_beg_line will be 0 if it's a new file */
  195. count = src_beg_line - src_cur_line;
  196. if (copy_lines(src_stream, dst_stream, count)) {
  197. bb_error_msg_and_die("bad src file");
  198. }
  199. src_cur_line += count;
  200. dst_cur_line += count;
  201. copy_trailing_lines_flag = 1;
  202. }
  203. src_last_line += hunk_offset_start = src_cur_line;
  204. dst_last_line += dst_cur_line;
  205. while (1) {
  206. free(patch_line);
  207. patch_line = xmalloc_fgets(patch_file);
  208. if (patch_line == NULL)
  209. break; /* EOF */
  210. if (!*patch_line) {
  211. /* whitespace-damaged patch with "" lines */
  212. free(patch_line);
  213. patch_line = xstrdup(" ");
  214. }
  215. if ((*patch_line != '-') && (*patch_line != '+')
  216. && (*patch_line != ' ')
  217. ) {
  218. break; /* End of hunk */
  219. }
  220. if (*patch_line != plus) { /* '-' or ' ' */
  221. char *src_line = NULL;
  222. if (src_cur_line == src_last_line)
  223. break;
  224. if (src_stream) {
  225. src_line = xmalloc_fgets(src_stream);
  226. if (src_line) {
  227. int diff = strcmp(src_line, patch_line + 1);
  228. src_cur_line++;
  229. free(src_line);
  230. if (diff)
  231. src_line = NULL;
  232. }
  233. }
  234. /* Do not patch an already patched hunk with -N */
  235. if (src_line == 0 && (opt & OPT_N)) {
  236. continue;
  237. }
  238. if (!src_line) {
  239. bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
  240. bad_hunk_count++;
  241. break;
  242. }
  243. if (*patch_line != ' ') { /* '-' */
  244. continue;
  245. }
  246. }
  247. if (dst_cur_line == dst_last_line)
  248. break;
  249. fputs(patch_line + 1, dst_stream);
  250. dst_cur_line++;
  251. } /* end of while loop handling one hunk */
  252. } /* end of while loop handling one file */
  253. /* Cleanup last patched file */
  254. if (copy_trailing_lines_flag) {
  255. copy_lines(src_stream, dst_stream, (unsigned)(-1));
  256. }
  257. if (src_stream) {
  258. fclose(src_stream);
  259. }
  260. fclose(dst_stream);
  261. if (bad_hunk_count) {
  262. ret = 1;
  263. bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
  264. } else {
  265. /* It worked, we can remove the backup */
  266. if (backup_filename) {
  267. unlink(backup_filename);
  268. }
  269. if (!(opt & OPT_dry_run)
  270. && ((dst_cur_line == 0) || (dst_beg_line == 0))
  271. ) {
  272. /* The new patched file is empty, remove it */
  273. xunlink(new_filename);
  274. // /* old_filename and new_filename may be the same file */
  275. // unlink(old_filename);
  276. }
  277. }
  278. free(backup_filename);
  279. //free(old_filename);
  280. free(new_filename);
  281. } /* end of "while there are patch lines" */
  282. quit:
  283. /* 0 = SUCCESS
  284. * 1 = Some hunks failed
  285. * 2 = More serious problems (exited earlier)
  286. */
  287. return ret;
  288. }