1 /* vi: set sw=4 ts=4: */
3 * busybox patch applet to handle the unified diff format.
4 * Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 * This applet is written to work with patches generated by GNU diff,
9 * where there is equivalent functionality busybox patch shall behave
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
18 * - Patches must apply cleanly or the hunk will fail.
19 * - Reject file isnt saved
29 static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
33 while (src_stream && (i < lines_count)) {
35 line = bb_get_line_from_file(src_stream);
39 if (fputs(line, dest_stream) == EOF) {
40 bb_perror_msg_and_die("Error writing to new file");
49 /* If patch_level is -1 it will remove all directory names
50 * char *line must be greater than 4 chars
51 * returns NULL if the file doesnt exist or error
52 * returns malloc'ed filename
55 static char *extract_filename(char *line, int patch_level)
57 char *filename_start_ptr = line + 4;
60 /* Terminate string at end of source filename */
63 line_ptr = strchr(filename_start_ptr, '\t');
65 bb_perror_msg("Malformed line %s", line);
71 /* Skip over (patch_level) number of leading directories */
72 for (i = 0; i < patch_level; i++) {
75 dirname_ptr = strchr(filename_start_ptr, '/');
79 filename_start_ptr = dirname_ptr + 1;
82 return(bb_xstrdup(filename_start_ptr));
85 static int file_doesnt_exist(const char *filename)
88 return(stat(filename, &statbuf));
91 extern int patch_main(int argc, char **argv)
96 FILE *patch_file = NULL;
100 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
102 patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX);
104 patch_file = bb_xfopen(i, "r");
111 patch_line = bb_get_line_from_file(patch_file);
115 char *original_filename;
117 char *backup_filename;
118 unsigned int src_cur_line = 1;
119 unsigned int dest_cur_line = 0;
120 unsigned int dest_beg_line;
121 unsigned int bad_hunk_count = 0;
122 unsigned int hunk_count = 0;
123 char copy_trailing_lines_flag = 0;
125 /* Skip everything upto the "---" marker
126 * No need to parse the lines "Only in <dir>", and "diff <args>"
128 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
130 patch_line = bb_get_line_from_file(patch_file);
133 /* Extract the filename used before the patch was generated */
134 original_filename = extract_filename(patch_line, patch_level);
137 patch_line = bb_get_line_from_file(patch_file);
138 if (strncmp(patch_line, "+++ ", 4) != 0) {
140 bb_error_msg("Invalid patch");
143 new_filename = extract_filename(patch_line, patch_level);
146 if (file_doesnt_exist(new_filename)) {
148 /* Create leading directories */
149 line_ptr = strrchr(new_filename, '/');
152 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
155 dst_stream = bb_xfopen(new_filename, "w+");
156 backup_filename = NULL;
158 backup_filename = xmalloc(strlen(new_filename) + 6);
159 strcpy(backup_filename, new_filename);
160 strcat(backup_filename, ".orig");
161 if (rename(new_filename, backup_filename) == -1) {
162 bb_perror_msg_and_die("Couldnt create file %s",
165 dst_stream = bb_xfopen(new_filename, "w");
168 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
171 if (strcmp(original_filename, new_filename) == 0) {
172 src_stream = bb_xfopen(backup_filename, "r");
174 src_stream = bb_xfopen(original_filename, "r");
178 printf("patching file %s\n", new_filename);
180 /* Handle each hunk */
181 patch_line = bb_get_line_from_file(patch_file);
184 unsigned int src_beg_line;
186 unsigned int hunk_offset_start = 0;
189 /* This bit should be improved */
190 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
191 (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
192 (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
193 /* No more hunks for this file */
199 if (src_beg_line && dest_beg_line) {
200 /* Copy unmodified lines upto start of hunk */
201 /* src_beg_line will be 0 if its a new file */
202 count = src_beg_line - src_cur_line;
203 if (copy_lines(src_stream, dst_stream, count) != count) {
204 bb_error_msg_and_die("Bad src file");
206 src_cur_line += count;
207 dest_cur_line += count;
208 copy_trailing_lines_flag = 1;
210 hunk_offset_start = src_cur_line;
212 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
213 if ((*patch_line == '-') || (*patch_line == ' ')) {
214 char *src_line = NULL;
216 src_line = bb_get_line_from_file(src_stream);
223 if (strcmp(src_line, patch_line + 1) != 0) {
224 bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
231 if (*patch_line == ' ') {
232 fputs(patch_line + 1, dst_stream);
235 } else if (*patch_line == '+') {
236 fputs(patch_line + 1, dst_stream);
248 /* Cleanup last patched file */
249 if (copy_trailing_lines_flag) {
250 copy_lines(src_stream, dst_stream, -1);
258 if (bad_hunk_count) {
262 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
264 /* It worked, we can remove the backup */
265 if (backup_filename) {
266 unlink(backup_filename);
268 if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
269 /* The new patched file is empty, remove it */
270 if (unlink(new_filename) == -1) {
271 bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
273 if (unlink(original_filename) == -1) {
274 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
281 * 1 = Some hunks failed
282 * 2 = More serious problems