blob: 980c37f400e52da09d35ebfc0f2fc3a4be5ce584 [file] [log] [blame]
Glenn L McGrath655d8142003-06-22 15:32:41 +00001/* vi: set sw=4 ts=4: */
2/*
3 * busybox patch applet to handle the unified diff format.
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
Glenn L McGrath655d8142003-06-22 15:32:41 +00005 *
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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000034 * -
Glenn L McGrath655d8142003-06-22 15:32:41 +000035 */
36
37#include <getopt.h>
38#include <string.h>
39#include <stdlib.h>
40#include <unistd.h>
41#include "busybox.h"
Glenn L McGrath655d8142003-06-22 15:32:41 +000042
43static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
44{
45 int i = 0;
46
47 while (src_stream && (i < lines_count)) {
48 char *line;
49 line = bb_get_line_from_file(src_stream);
50 if (line == NULL) {
51 break;
52 }
53 if (fputs(line, dest_stream) == EOF) {
54 bb_perror_msg_and_die("Error writing to new file");
55 }
56 free(line);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000057
Glenn L McGrath655d8142003-06-22 15:32:41 +000058 i++;
59 }
60 return(i);
61}
62
63/* If patch_level is -1 it will remove all directory names
64 * char *line must be greater than 4 chars
65 * returns NULL if the file doesnt exist or error
66 * returns malloc'ed filename
67 */
68
69static unsigned char *extract_filename(char *line, unsigned short patch_level)
70{
71 char *filename_start_ptr = line + 4;
72 int i;
73
74 /* Terminate string at end of source filename */
75 {
76 char *line_ptr;
77 line_ptr = strchr(filename_start_ptr, '\t');
78 if (!line_ptr) {
79 bb_perror_msg("Malformed line %s", line);
80 return(NULL);
81 }
82 *line_ptr = '\0';
83 }
84
85 /* Skip over (patch_level) number of leading directories */
86 for (i = 0; i < patch_level; i++) {
87 char *dirname_ptr;
88
89 dirname_ptr = strchr(filename_start_ptr, '/');
90 if (!dirname_ptr) {
91 break;
92 }
93 filename_start_ptr = dirname_ptr + 1;
94 }
95
96 return(bb_xstrdup(filename_start_ptr));
97}
98
99static int file_doesnt_exist(const char *filename)
100{
101 struct stat statbuf;
102 return(stat(filename, &statbuf));
103}
104
105extern int patch_main(int argc, char **argv)
106{
107 unsigned int patch_level = -1;
108 char *patch_line;
Rob Landley078bacf2005-09-01 03:02:23 +0000109 int ret;
110 FILE *patch_file = NULL;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000111
Rob Landley078bacf2005-09-01 03:02:23 +0000112 {
113 char *p, *i;
114 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
115 if (ret & 1)
116 patch_level = bb_xgetularg10_bnd(p, -1, USHRT_MAX);
117 if (ret & 2) {
118 patch_file = bb_xfopen(i, "r");
119 }
120 ret = 0;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000121 }
122
Rob Landley078bacf2005-09-01 03:02:23 +0000123 if (!patch_file)
124 patch_file = stdin;
125
126 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000127 while (patch_line) {
128 FILE *src_stream;
129 FILE *dst_stream;
130 char *original_filename;
131 char *new_filename;
132 char *backup_filename;
133 unsigned int src_cur_line = 1;
134 unsigned int dest_cur_line = 0;
135 unsigned int dest_beg_line;
136 unsigned int bad_hunk_count = 0;
137 unsigned int hunk_count = 0;
138 char copy_trailing_lines_flag = 0;
139
140 /* Skip everything upto the "---" marker
141 * No need to parse the lines "Only in <dir>", and "diff <args>"
142 */
143 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
144 free(patch_line);
Rob Landley078bacf2005-09-01 03:02:23 +0000145 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000146 }
147
148 /* Extract the filename used before the patch was generated */
149 original_filename = extract_filename(patch_line, patch_level);
150 free(patch_line);
151
Rob Landley078bacf2005-09-01 03:02:23 +0000152 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000153 if (strncmp(patch_line, "+++ ", 4) != 0) {
154 ret = 2;
155 bb_error_msg("Invalid patch");
156 continue;
157 }
158 new_filename = extract_filename(patch_line, patch_level);
159 free(patch_line);
160
161 if (file_doesnt_exist(new_filename)) {
162 char *line_ptr;
163 /* Create leading directories */
164 line_ptr = strrchr(new_filename, '/');
165 if (line_ptr) {
166 *line_ptr = '\0';
167 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
168 *line_ptr = '/';
169 }
170 dst_stream = bb_xfopen(new_filename, "w+");
171 backup_filename = NULL;
172 } else {
173 backup_filename = xmalloc(strlen(new_filename) + 6);
174 strcpy(backup_filename, new_filename);
175 strcat(backup_filename, ".orig");
176 if (rename(new_filename, backup_filename) == -1) {
177 bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
178 }
179 dst_stream = bb_xfopen(new_filename, "w");
180 }
181
182 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
183 src_stream = NULL;
184 } else {
185 if (strcmp(original_filename, new_filename) == 0) {
186 src_stream = bb_xfopen(backup_filename, "r");
187 } else {
188 src_stream = bb_xfopen(original_filename, "r");
189 }
190 }
191
192 printf("patching file %s\n", new_filename);
193
194 /* Handle each hunk */
Rob Landley078bacf2005-09-01 03:02:23 +0000195 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000196 while (patch_line) {
197 unsigned int count;
198 unsigned int src_beg_line;
199 unsigned int unused;
200 unsigned int hunk_offset_start = 0;
201 int hunk_error = 0;
202
203 /* This bit should be improved */
204 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
205 (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
206 (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
207 /* No more hunks for this file */
208 break;
209 }
210 free(patch_line);
211 hunk_count++;
212
213 if (src_beg_line && dest_beg_line) {
214 /* Copy unmodified lines upto start of hunk */
215 /* src_beg_line will be 0 if its a new file */
216 count = src_beg_line - src_cur_line;
217 if (copy_lines(src_stream, dst_stream, count) != count) {
218 bb_error_msg_and_die("Bad src file");
219 }
220 src_cur_line += count;
221 dest_cur_line += count;
222 copy_trailing_lines_flag = 1;
223 }
224 hunk_offset_start = src_cur_line;
225
Rob Landley078bacf2005-09-01 03:02:23 +0000226 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
Glenn L McGrath655d8142003-06-22 15:32:41 +0000227 if ((*patch_line == '-') || (*patch_line == ' ')) {
228 char *src_line = NULL;
229 if (src_stream) {
230 src_line = bb_get_line_from_file(src_stream);
231 if (!src_line) {
232 hunk_error++;
233 break;
234 } else {
235 src_cur_line++;
236 }
237 if (strcmp(src_line, patch_line + 1) != 0) {
238 bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
239 hunk_error++;
240 free(patch_line);
241 break;
242 }
243 free(src_line);
244 }
245 if (*patch_line == ' ') {
246 fputs(patch_line + 1, dst_stream);
247 dest_cur_line++;
248 }
249 } else if (*patch_line == '+') {
250 fputs(patch_line + 1, dst_stream);
251 dest_cur_line++;
252 } else {
253 break;
254 }
255 free(patch_line);
256 }
257 if (hunk_error) {
258 bad_hunk_count++;
259 }
260 }
261
262 /* Cleanup last patched file */
263 if (copy_trailing_lines_flag) {
264 copy_lines(src_stream, dst_stream, -1);
265 }
266 if (src_stream) {
267 fclose(src_stream);
268 }
269 if (dst_stream) {
270 fclose(dst_stream);
271 }
272 if (bad_hunk_count) {
273 if (!ret) {
274 ret = 1;
275 }
276 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
277 } else {
278 /* It worked, we can remove the backup */
279 if (backup_filename) {
280 unlink(backup_filename);
281 }
282 if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
283 /* The new patched file is empty, remove it */
284 if (unlink(new_filename) == -1) {
285 bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
286 }
287 if (unlink(original_filename) == -1) {
288 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
289 }
290 }
291 }
292 }
293
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000294 /* 0 = SUCCESS
Glenn L McGrath655d8142003-06-22 15:32:41 +0000295 * 1 = Some hunks failed
296 * 2 = More serious problems
297 */
298 return(ret);
299}