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 * 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.
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.
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.
22 * This applet is written to work with patches generated by GNU diff,
23 * where there is equivalent functionality busybox patch shall behave
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
32 * - Patches must apply cleanly or the hunk will fail.
33 * - Reject file isnt saved
44 static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
48 while (src_stream && (i < lines_count)) {
50 line = bb_get_line_from_file(src_stream);
54 if (fputs(line, dest_stream) == EOF) {
55 bb_perror_msg_and_die("Error writing to new file");
64 /* If patch_level is -1 it will remove all directory names
65 * char *line must be greater than 4 chars
66 * returns NULL if the file doesnt exist or error
67 * returns malloc'ed filename
70 static unsigned char *extract_filename(char *line, unsigned short patch_level)
72 char *filename_start_ptr = line + 4;
75 /* Terminate string at end of source filename */
78 line_ptr = strchr(filename_start_ptr, '\t');
80 bb_perror_msg("Malformed line %s", line);
86 /* Skip over (patch_level) number of leading directories */
87 for (i = 0; i < patch_level; i++) {
90 dirname_ptr = strchr(filename_start_ptr, '/');
94 filename_start_ptr = dirname_ptr + 1;
97 return(bb_xstrdup(filename_start_ptr));
100 static int file_doesnt_exist(const char *filename)
103 return(stat(filename, &statbuf));
106 extern int patch_main(int argc, char **argv)
108 unsigned int patch_level = -1;
112 /* Handle 'p' option */
113 if (argv[1] && (argv[1][0] == '-') && (argv[1][1] == 'p')) {
114 patch_level = atoi(&argv[1][2]);
117 patch_line = bb_get_line_from_file(stdin);
121 char *original_filename;
123 char *backup_filename;
124 unsigned int src_cur_line = 1;
125 unsigned int dest_cur_line = 0;
126 unsigned int dest_beg_line;
127 unsigned int bad_hunk_count = 0;
128 unsigned int hunk_count = 0;
129 char copy_trailing_lines_flag = 0;
131 /* Skip everything upto the "---" marker
132 * No need to parse the lines "Only in <dir>", and "diff <args>"
134 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
136 patch_line = bb_get_line_from_file(stdin);
139 /* Extract the filename used before the patch was generated */
140 original_filename = extract_filename(patch_line, patch_level);
143 patch_line = bb_get_line_from_file(stdin);
144 if (strncmp(patch_line, "+++ ", 4) != 0) {
146 bb_error_msg("Invalid patch");
149 new_filename = extract_filename(patch_line, patch_level);
152 if (file_doesnt_exist(new_filename)) {
154 /* Create leading directories */
155 line_ptr = strrchr(new_filename, '/');
158 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
161 dst_stream = bb_xfopen(new_filename, "w+");
162 backup_filename = NULL;
164 backup_filename = xmalloc(strlen(new_filename) + 6);
165 strcpy(backup_filename, new_filename);
166 strcat(backup_filename, ".orig");
167 if (rename(new_filename, backup_filename) == -1) {
168 bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
170 dst_stream = bb_xfopen(new_filename, "w");
173 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
176 if (strcmp(original_filename, new_filename) == 0) {
177 src_stream = bb_xfopen(backup_filename, "r");
179 src_stream = bb_xfopen(original_filename, "r");
183 printf("patching file %s\n", new_filename);
185 /* Handle each hunk */
186 patch_line = bb_get_line_from_file(stdin);
189 unsigned int src_beg_line;
191 unsigned int hunk_offset_start = 0;
194 /* This bit should be improved */
195 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
196 (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
197 (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
198 /* No more hunks for this file */
204 if (src_beg_line && dest_beg_line) {
205 /* Copy unmodified lines upto start of hunk */
206 /* src_beg_line will be 0 if its a new file */
207 count = src_beg_line - src_cur_line;
208 if (copy_lines(src_stream, dst_stream, count) != count) {
209 bb_error_msg_and_die("Bad src file");
211 src_cur_line += count;
212 dest_cur_line += count;
213 copy_trailing_lines_flag = 1;
215 hunk_offset_start = src_cur_line;
217 while ((patch_line = bb_get_line_from_file(stdin)) != NULL) {
218 if ((*patch_line == '-') || (*patch_line == ' ')) {
219 char *src_line = NULL;
221 src_line = bb_get_line_from_file(src_stream);
228 if (strcmp(src_line, patch_line + 1) != 0) {
229 bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
236 if (*patch_line == ' ') {
237 fputs(patch_line + 1, dst_stream);
240 } else if (*patch_line == '+') {
241 fputs(patch_line + 1, dst_stream);
253 /* Cleanup last patched file */
254 if (copy_trailing_lines_flag) {
255 copy_lines(src_stream, dst_stream, -1);
263 if (bad_hunk_count) {
267 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
269 /* It worked, we can remove the backup */
270 if (backup_filename) {
271 unlink(backup_filename);
273 if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
274 /* The new patched file is empty, remove it */
275 if (unlink(new_filename) == -1) {
276 bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
278 if (unlink(original_filename) == -1) {
279 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
286 * 1 = Some hunks failed
287 * 2 = More serious problems