3
0

patch.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 ATTRIBUTE_UNUSED, 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. xfunc_error_retval = 2;
  72. {
  73. const char *p = "-1";
  74. const char *i = "-"; /* compat */
  75. if (getopt32(argv, "p:i:R", &p, &i) & 4)
  76. plus = '-';
  77. patch_level = xatoi(p); /* can be negative! */
  78. patch_file = xfopen_stdin(i);
  79. }
  80. patch_line = xmalloc_fgetline(patch_file);
  81. while (patch_line) {
  82. FILE *src_stream;
  83. FILE *dst_stream;
  84. //char *old_filename;
  85. char *new_filename;
  86. char *backup_filename;
  87. unsigned src_cur_line = 1;
  88. unsigned dst_cur_line = 0;
  89. unsigned dst_beg_line;
  90. unsigned bad_hunk_count = 0;
  91. unsigned hunk_count = 0;
  92. smallint copy_trailing_lines_flag = 0;
  93. /* Skip everything upto the "---" marker
  94. * No need to parse the lines "Only in <dir>", and "diff <args>"
  95. */
  96. do {
  97. /* Extract the filename used before the patch was generated */
  98. new_filename = extract_filename(patch_line, patch_level, "--- ");
  99. // was old_filename above
  100. patch_line = xmalloc_fgetline(patch_file);
  101. if (!patch_line) goto quit;
  102. } while (!new_filename);
  103. free(new_filename); // "source" filename is irrelevant
  104. new_filename = extract_filename(patch_line, patch_level, "+++ ");
  105. if (!new_filename) {
  106. bb_error_msg_and_die("invalid patch");
  107. }
  108. /* Get access rights from the file to be patched */
  109. if (stat(new_filename, &saved_stat) != 0) {
  110. char *slash = strrchr(new_filename, '/');
  111. if (slash) {
  112. /* Create leading directories */
  113. *slash = '\0';
  114. bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
  115. *slash = '/';
  116. }
  117. backup_filename = NULL;
  118. src_stream = NULL;
  119. saved_stat.st_mode = 0644;
  120. } else {
  121. backup_filename = xasprintf("%s.orig", new_filename);
  122. xrename(new_filename, backup_filename);
  123. src_stream = xfopen(backup_filename, "r");
  124. }
  125. dst_stream = xfopen(new_filename, "w");
  126. fchmod(fileno(dst_stream), saved_stat.st_mode);
  127. printf("patching file %s\n", new_filename);
  128. /* Handle all hunks for this file */
  129. patch_line = xmalloc_fgets(patch_file);
  130. while (patch_line) {
  131. unsigned count;
  132. unsigned src_beg_line;
  133. unsigned hunk_offset_start;
  134. unsigned src_last_line = 1;
  135. unsigned dst_last_line = 1;
  136. if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
  137. && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
  138. ) {
  139. /* No more hunks for this file */
  140. break;
  141. }
  142. if (plus != '+') {
  143. /* reverse patch */
  144. unsigned tmp = src_last_line;
  145. src_last_line = dst_last_line;
  146. dst_last_line = tmp;
  147. tmp = src_beg_line;
  148. src_beg_line = dst_beg_line;
  149. dst_beg_line = tmp;
  150. }
  151. hunk_count++;
  152. if (src_beg_line && dst_beg_line) {
  153. /* Copy unmodified lines upto start of hunk */
  154. /* src_beg_line will be 0 if it's a new file */
  155. count = src_beg_line - src_cur_line;
  156. if (copy_lines(src_stream, dst_stream, count)) {
  157. bb_error_msg_and_die("bad src file");
  158. }
  159. src_cur_line += count;
  160. dst_cur_line += count;
  161. copy_trailing_lines_flag = 1;
  162. }
  163. src_last_line += hunk_offset_start = src_cur_line;
  164. dst_last_line += dst_cur_line;
  165. while (1) {
  166. free(patch_line);
  167. patch_line = xmalloc_fgets(patch_file);
  168. if (patch_line == NULL)
  169. break; /* EOF */
  170. if ((*patch_line != '-') && (*patch_line != '+')
  171. && (*patch_line != ' ')
  172. ) {
  173. break; /* End of hunk */
  174. }
  175. if (*patch_line != plus) { /* '-' or ' ' */
  176. char *src_line = NULL;
  177. if (src_cur_line == src_last_line)
  178. break;
  179. if (src_stream) {
  180. src_line = xmalloc_fgets(src_stream);
  181. if (src_line) {
  182. int diff = strcmp(src_line, patch_line + 1);
  183. src_cur_line++;
  184. free(src_line);
  185. if (diff)
  186. src_line = NULL;
  187. }
  188. }
  189. if (!src_line) {
  190. bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
  191. bad_hunk_count++;
  192. break;
  193. }
  194. if (*patch_line != ' ') { /* '-' */
  195. continue;
  196. }
  197. }
  198. if (dst_cur_line == dst_last_line)
  199. break;
  200. fputs(patch_line + 1, dst_stream);
  201. dst_cur_line++;
  202. } /* end of while loop handling one hunk */
  203. } /* end of while loop handling one file */
  204. /* Cleanup last patched file */
  205. if (copy_trailing_lines_flag) {
  206. copy_lines(src_stream, dst_stream, (unsigned)(-1));
  207. }
  208. if (src_stream) {
  209. fclose(src_stream);
  210. }
  211. fclose(dst_stream);
  212. if (bad_hunk_count) {
  213. ret = 1;
  214. bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
  215. } else {
  216. /* It worked, we can remove the backup */
  217. if (backup_filename) {
  218. unlink(backup_filename);
  219. }
  220. if ((dst_cur_line == 0) || (dst_beg_line == 0)) {
  221. /* The new patched file is empty, remove it */
  222. xunlink(new_filename);
  223. // /* old_filename and new_filename may be the same file */
  224. // unlink(old_filename);
  225. }
  226. }
  227. free(backup_filename);
  228. //free(old_filename);
  229. free(new_filename);
  230. } /* end of "while there are patch lines" */
  231. quit:
  232. /* 0 = SUCCESS
  233. * 1 = Some hunks failed
  234. * 2 = More serious problems (exited earlier)
  235. */
  236. return ret;
  237. }