3
0

patch.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * busybox patch applet to handle the unified diff format.
  4. * Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. *
  20. *
  21. *
  22. * This applet is written to work with patches generated by GNU diff,
  23. * where there is equivalent functionality busybox patch shall behave
  24. * as per GNU patch.
  25. *
  26. * There is a SUSv3 specification for patch, however it looks to be
  27. * incomplete, it doesnt even mention unified diff format.
  28. * http://www.opengroup.org/onlinepubs/007904975/utilities/patch.html
  29. *
  30. * Issues
  31. * - Non-interactive
  32. * - Patches must apply cleanly or the hunk will fail.
  33. * - Reject file isnt saved
  34. * -
  35. */
  36. #include <getopt.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include "busybox.h"
  41. #include "libbb.h"
  42. static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
  43. {
  44. int i = 0;
  45. while (src_stream && (i < lines_count)) {
  46. char *line;
  47. line = bb_get_line_from_file(src_stream);
  48. if (line == NULL) {
  49. break;
  50. }
  51. if (fputs(line, dest_stream) == EOF) {
  52. bb_perror_msg_and_die("Error writing to new file");
  53. }
  54. free(line);
  55. i++;
  56. }
  57. return(i);
  58. }
  59. /* If patch_level is -1 it will remove all directory names
  60. * char *line must be greater than 4 chars
  61. * returns NULL if the file doesnt exist or error
  62. * returns malloc'ed filename
  63. */
  64. static unsigned char *extract_filename(char *line, unsigned short patch_level)
  65. {
  66. char *filename_start_ptr = line + 4;
  67. int i;
  68. /* Terminate string at end of source filename */
  69. {
  70. char *line_ptr;
  71. line_ptr = strchr(filename_start_ptr, '\t');
  72. if (!line_ptr) {
  73. bb_perror_msg("Malformed line %s", line);
  74. return(NULL);
  75. }
  76. *line_ptr = '\0';
  77. }
  78. /* Skip over (patch_level) number of leading directories */
  79. for (i = 0; i < patch_level; i++) {
  80. char *dirname_ptr;
  81. dirname_ptr = strchr(filename_start_ptr, '/');
  82. if (!dirname_ptr) {
  83. break;
  84. }
  85. filename_start_ptr = dirname_ptr + 1;
  86. }
  87. return(bb_xstrdup(filename_start_ptr));
  88. }
  89. static int file_doesnt_exist(const char *filename)
  90. {
  91. struct stat statbuf;
  92. return(stat(filename, &statbuf));
  93. }
  94. extern int patch_main(int argc, char **argv)
  95. {
  96. unsigned int patch_level = -1;
  97. char *patch_line;
  98. int ret = 0;
  99. /* Handle 'p' option */
  100. if (argv[1] && (argv[1][0] == '-') && (argv[1][1] == 'p')) {
  101. patch_level = atoi(&argv[1][2]);
  102. }
  103. patch_line = bb_get_line_from_file(stdin);
  104. while (patch_line) {
  105. FILE *src_stream;
  106. FILE *dst_stream;
  107. char *original_filename;
  108. char *new_filename;
  109. char *backup_filename;
  110. unsigned int src_cur_line = 1;
  111. unsigned int dest_cur_line = 0;
  112. unsigned int dest_beg_line;
  113. unsigned int bad_hunk_count = 0;
  114. unsigned int hunk_count = 0;
  115. char copy_trailing_lines_flag = 0;
  116. /* Skip everything upto the "---" marker
  117. * No need to parse the lines "Only in <dir>", and "diff <args>"
  118. */
  119. while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
  120. free(patch_line);
  121. patch_line = bb_get_line_from_file(stdin);
  122. }
  123. /* Extract the filename used before the patch was generated */
  124. original_filename = extract_filename(patch_line, patch_level);
  125. free(patch_line);
  126. patch_line = bb_get_line_from_file(stdin);
  127. if (strncmp(patch_line, "+++ ", 4) != 0) {
  128. ret = 2;
  129. bb_error_msg("Invalid patch");
  130. continue;
  131. }
  132. new_filename = extract_filename(patch_line, patch_level);
  133. free(patch_line);
  134. if (file_doesnt_exist(new_filename)) {
  135. char *line_ptr;
  136. /* Create leading directories */
  137. line_ptr = strrchr(new_filename, '/');
  138. if (line_ptr) {
  139. *line_ptr = '\0';
  140. bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
  141. *line_ptr = '/';
  142. }
  143. dst_stream = bb_xfopen(new_filename, "w+");
  144. backup_filename = NULL;
  145. } else {
  146. backup_filename = xmalloc(strlen(new_filename) + 6);
  147. strcpy(backup_filename, new_filename);
  148. strcat(backup_filename, ".orig");
  149. if (rename(new_filename, backup_filename) == -1) {
  150. bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
  151. }
  152. dst_stream = bb_xfopen(new_filename, "w");
  153. }
  154. if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
  155. src_stream = NULL;
  156. } else {
  157. if (strcmp(original_filename, new_filename) == 0) {
  158. src_stream = bb_xfopen(backup_filename, "r");
  159. } else {
  160. src_stream = bb_xfopen(original_filename, "r");
  161. }
  162. }
  163. printf("patching file %s\n", new_filename);
  164. /* Handle each hunk */
  165. patch_line = bb_get_line_from_file(stdin);
  166. while (patch_line) {
  167. unsigned int count;
  168. unsigned int src_beg_line;
  169. unsigned int unused;
  170. unsigned int hunk_offset_start = 0;
  171. int hunk_error = 0;
  172. /* This bit should be improved */
  173. if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
  174. (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
  175. (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
  176. /* No more hunks for this file */
  177. break;
  178. }
  179. free(patch_line);
  180. hunk_count++;
  181. if (src_beg_line && dest_beg_line) {
  182. /* Copy unmodified lines upto start of hunk */
  183. /* src_beg_line will be 0 if its a new file */
  184. count = src_beg_line - src_cur_line;
  185. if (copy_lines(src_stream, dst_stream, count) != count) {
  186. bb_error_msg_and_die("Bad src file");
  187. }
  188. src_cur_line += count;
  189. dest_cur_line += count;
  190. copy_trailing_lines_flag = 1;
  191. }
  192. hunk_offset_start = src_cur_line;
  193. while ((patch_line = bb_get_line_from_file(stdin)) != NULL) {
  194. if ((*patch_line == '-') || (*patch_line == ' ')) {
  195. char *src_line = NULL;
  196. if (src_stream) {
  197. src_line = bb_get_line_from_file(src_stream);
  198. if (!src_line) {
  199. hunk_error++;
  200. break;
  201. } else {
  202. src_cur_line++;
  203. }
  204. if (strcmp(src_line, patch_line + 1) != 0) {
  205. bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
  206. hunk_error++;
  207. free(patch_line);
  208. break;
  209. }
  210. free(src_line);
  211. }
  212. if (*patch_line == ' ') {
  213. fputs(patch_line + 1, dst_stream);
  214. dest_cur_line++;
  215. }
  216. } else if (*patch_line == '+') {
  217. fputs(patch_line + 1, dst_stream);
  218. dest_cur_line++;
  219. } else {
  220. break;
  221. }
  222. free(patch_line);
  223. }
  224. if (hunk_error) {
  225. bad_hunk_count++;
  226. }
  227. }
  228. /* Cleanup last patched file */
  229. if (copy_trailing_lines_flag) {
  230. copy_lines(src_stream, dst_stream, -1);
  231. }
  232. if (src_stream) {
  233. fclose(src_stream);
  234. }
  235. if (dst_stream) {
  236. fclose(dst_stream);
  237. }
  238. if (bad_hunk_count) {
  239. if (!ret) {
  240. ret = 1;
  241. }
  242. bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
  243. } else {
  244. /* It worked, we can remove the backup */
  245. if (backup_filename) {
  246. unlink(backup_filename);
  247. }
  248. if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
  249. /* The new patched file is empty, remove it */
  250. if (unlink(new_filename) == -1) {
  251. bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
  252. }
  253. if (unlink(original_filename) == -1) {
  254. bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
  255. }
  256. }
  257. }
  258. }
  259. /* 0 = SUCCESS
  260. * 1 = Some hunks failed
  261. * 2 = More serious problems
  262. */
  263. return(ret);
  264. }