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;
111 FILE *patch_file = NULL;
115 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
117 patch_level = bb_xgetularg10_bnd(p, -1, USHRT_MAX);
119 patch_file = bb_xfopen(i, "r");
127 patch_line = bb_get_line_from_file(patch_file);
131 char *original_filename;
133 char *backup_filename;
134 unsigned int src_cur_line = 1;
135 unsigned int dest_cur_line = 0;
136 unsigned int dest_beg_line;
137 unsigned int bad_hunk_count = 0;
138 unsigned int hunk_count = 0;
139 char copy_trailing_lines_flag = 0;
141 /* Skip everything upto the "---" marker
142 * No need to parse the lines "Only in <dir>", and "diff <args>"
144 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
146 patch_line = bb_get_line_from_file(patch_file);
149 /* Extract the filename used before the patch was generated */
150 original_filename = extract_filename(patch_line, patch_level);
153 patch_line = bb_get_line_from_file(patch_file);
154 if (strncmp(patch_line, "+++ ", 4) != 0) {
156 bb_error_msg("Invalid patch");
159 new_filename = extract_filename(patch_line, patch_level);
162 if (file_doesnt_exist(new_filename)) {
164 /* Create leading directories */
165 line_ptr = strrchr(new_filename, '/');
168 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
171 dst_stream = bb_xfopen(new_filename, "w+");
172 backup_filename = NULL;
174 backup_filename = xmalloc(strlen(new_filename) + 6);
175 strcpy(backup_filename, new_filename);
176 strcat(backup_filename, ".orig");
177 if (rename(new_filename, backup_filename) == -1) {
178 bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
180 dst_stream = bb_xfopen(new_filename, "w");
183 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
186 if (strcmp(original_filename, new_filename) == 0) {
187 src_stream = bb_xfopen(backup_filename, "r");
189 src_stream = bb_xfopen(original_filename, "r");
193 printf("patching file %s\n", new_filename);
195 /* Handle each hunk */
196 patch_line = bb_get_line_from_file(patch_file);
199 unsigned int src_beg_line;
201 unsigned int hunk_offset_start = 0;
204 /* This bit should be improved */
205 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
206 (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
207 (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
208 /* No more hunks for this file */
214 if (src_beg_line && dest_beg_line) {
215 /* Copy unmodified lines upto start of hunk */
216 /* src_beg_line will be 0 if its a new file */
217 count = src_beg_line - src_cur_line;
218 if (copy_lines(src_stream, dst_stream, count) != count) {
219 bb_error_msg_and_die("Bad src file");
221 src_cur_line += count;
222 dest_cur_line += count;
223 copy_trailing_lines_flag = 1;
225 hunk_offset_start = src_cur_line;
227 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
228 if ((*patch_line == '-') || (*patch_line == ' ')) {
229 char *src_line = NULL;
231 src_line = bb_get_line_from_file(src_stream);
238 if (strcmp(src_line, patch_line + 1) != 0) {
239 bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
246 if (*patch_line == ' ') {
247 fputs(patch_line + 1, dst_stream);
250 } else if (*patch_line == '+') {
251 fputs(patch_line + 1, dst_stream);
263 /* Cleanup last patched file */
264 if (copy_trailing_lines_flag) {
265 copy_lines(src_stream, dst_stream, -1);
273 if (bad_hunk_count) {
277 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
279 /* It worked, we can remove the backup */
280 if (backup_filename) {
281 unlink(backup_filename);
283 if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
284 /* The new patched file is empty, remove it */
285 if (unlink(new_filename) == -1) {
286 bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
288 if (unlink(original_filename) == -1) {
289 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
296 * 1 = Some hunks failed
297 * 2 = More serious problems