3
0

patch_bbox.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 GPLv2 or later, see file LICENSE in this source tree.
  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_simple_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. #endif
  108. /* -f,-E,-g are ignored */
  109. opt = getopt32long(argv, "p:i:RN""fEg:", patch_longopts, &p, &i, NULL);
  110. if (opt & OPT_R)
  111. plus = '-';
  112. patch_level = xatoi(p); /* can be negative! */
  113. patch_file = xfopen_stdin(i);
  114. }
  115. patch_line = xmalloc_fgetline(patch_file);
  116. while (patch_line) {
  117. FILE *src_stream;
  118. FILE *dst_stream;
  119. //char *old_filename;
  120. char *new_filename;
  121. char *backup_filename = NULL;
  122. unsigned src_cur_line = 1;
  123. unsigned dst_cur_line = 0;
  124. unsigned dst_beg_line;
  125. unsigned bad_hunk_count = 0;
  126. unsigned hunk_count = 0;
  127. smallint copy_trailing_lines_flag = 0;
  128. /* Skip everything upto the "---" marker
  129. * No need to parse the lines "Only in <dir>", and "diff <args>"
  130. */
  131. do {
  132. /* Extract the filename used before the patch was generated */
  133. new_filename = extract_filename(patch_line, patch_level, "--- ");
  134. // was old_filename above
  135. patch_line = xmalloc_fgetline(patch_file);
  136. if (!patch_line) goto quit;
  137. } while (!new_filename);
  138. free(new_filename); // "source" filename is irrelevant
  139. new_filename = extract_filename(patch_line, patch_level, "+++ ");
  140. if (!new_filename) {
  141. bb_simple_error_msg_and_die("invalid patch");
  142. }
  143. /* Get access rights from the file to be patched */
  144. if (stat(new_filename, &saved_stat) != 0) {
  145. char *slash = strrchr(new_filename, '/');
  146. if (slash) {
  147. /* Create leading directories */
  148. *slash = '\0';
  149. bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
  150. *slash = '/';
  151. }
  152. src_stream = NULL;
  153. saved_stat.st_mode = 0644;
  154. } else if (!(opt & OPT_dry_run)) {
  155. backup_filename = xasprintf("%s.orig", new_filename);
  156. xrename(new_filename, backup_filename);
  157. src_stream = xfopen_for_read(backup_filename);
  158. } else
  159. src_stream = xfopen_for_read(new_filename);
  160. if (opt & OPT_dry_run) {
  161. dst_stream = xfopen_for_write("/dev/null");
  162. } else {
  163. dst_stream = xfopen_for_write(new_filename);
  164. fchmod(fileno(dst_stream), saved_stat.st_mode);
  165. }
  166. printf("patching file %s\n", new_filename);
  167. /* Handle all hunks for this file */
  168. patch_line = xmalloc_fgets(patch_file);
  169. while (patch_line) {
  170. unsigned count;
  171. unsigned src_beg_line;
  172. unsigned hunk_offset_start;
  173. unsigned src_last_line = 1;
  174. unsigned dst_last_line = 1;
  175. if ((sscanf(patch_line, "@@ -%u,%u +%u,%u", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
  176. && (sscanf(patch_line, "@@ -%u +%u,%u", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
  177. ) {
  178. /* No more hunks for this file */
  179. break;
  180. }
  181. if (plus != '+') {
  182. /* reverse patch */
  183. unsigned tmp = src_last_line;
  184. src_last_line = dst_last_line;
  185. dst_last_line = tmp;
  186. tmp = src_beg_line;
  187. src_beg_line = dst_beg_line;
  188. dst_beg_line = tmp;
  189. }
  190. hunk_count++;
  191. if (src_beg_line && dst_beg_line) {
  192. /* Copy unmodified lines upto start of hunk */
  193. /* src_beg_line will be 0 if it's a new file */
  194. count = src_beg_line - src_cur_line;
  195. if (copy_lines(src_stream, dst_stream, count)) {
  196. bb_simple_error_msg_and_die("bad src file");
  197. }
  198. src_cur_line += count;
  199. dst_cur_line += count;
  200. copy_trailing_lines_flag = 1;
  201. }
  202. src_last_line += hunk_offset_start = src_cur_line;
  203. dst_last_line += dst_cur_line;
  204. while (1) {
  205. free(patch_line);
  206. patch_line = xmalloc_fgets(patch_file);
  207. if (patch_line == NULL)
  208. break; /* EOF */
  209. if (!*patch_line) {
  210. /* whitespace-damaged patch with "" lines */
  211. free(patch_line);
  212. patch_line = xstrdup(" ");
  213. }
  214. if ((*patch_line != '-') && (*patch_line != '+')
  215. && (*patch_line != ' ')
  216. ) {
  217. break; /* End of hunk */
  218. }
  219. if (*patch_line != plus) { /* '-' or ' ' */
  220. char *src_line = NULL;
  221. if (src_cur_line == src_last_line)
  222. break;
  223. if (src_stream) {
  224. src_line = xmalloc_fgets(src_stream);
  225. if (src_line) {
  226. int diff = strcmp(src_line, patch_line + 1);
  227. src_cur_line++;
  228. free(src_line);
  229. if (diff)
  230. src_line = NULL;
  231. }
  232. }
  233. /* Do not patch an already patched hunk with -N */
  234. if (src_line == 0 && (opt & OPT_N)) {
  235. continue;
  236. }
  237. if (!src_line) {
  238. bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
  239. bad_hunk_count++;
  240. break;
  241. }
  242. if (*patch_line != ' ') { /* '-' */
  243. continue;
  244. }
  245. }
  246. if (dst_cur_line == dst_last_line)
  247. break;
  248. fputs(patch_line + 1, dst_stream);
  249. dst_cur_line++;
  250. } /* end of while loop handling one hunk */
  251. } /* end of while loop handling one file */
  252. /* Cleanup last patched file */
  253. if (copy_trailing_lines_flag) {
  254. copy_lines(src_stream, dst_stream, (unsigned)(-1));
  255. }
  256. if (src_stream) {
  257. fclose(src_stream);
  258. }
  259. fclose(dst_stream);
  260. if (bad_hunk_count) {
  261. ret = 1;
  262. bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
  263. } else {
  264. /* It worked, we can remove the backup */
  265. if (backup_filename) {
  266. unlink(backup_filename);
  267. }
  268. if (!(opt & OPT_dry_run)
  269. && ((dst_cur_line == 0) || (dst_beg_line == 0))
  270. ) {
  271. /* The new patched file is empty, remove it */
  272. xunlink(new_filename);
  273. // /* old_filename and new_filename may be the same file */
  274. // unlink(old_filename);
  275. }
  276. }
  277. free(backup_filename);
  278. //free(old_filename);
  279. free(new_filename);
  280. } /* end of "while there are patch lines" */
  281. quit:
  282. /* 0 = SUCCESS
  283. * 1 = Some hunks failed
  284. * 2 = More serious problems (exited earlier)
  285. */
  286. return ret;
  287. }