blob: 59b70210fa263d6ea28b57429a3fbac913069fc4 [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"
42#include "libbb.h"
43
44static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
45{
46 int i = 0;
47
48 while (src_stream && (i < lines_count)) {
49 char *line;
50 line = bb_get_line_from_file(src_stream);
51 if (line == NULL) {
52 break;
53 }
54 if (fputs(line, dest_stream) == EOF) {
55 bb_perror_msg_and_die("Error writing to new file");
56 }
57 free(line);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000058
Glenn L McGrath655d8142003-06-22 15:32:41 +000059 i++;
60 }
61 return(i);
62}
63
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
68 */
69
70static unsigned char *extract_filename(char *line, unsigned short patch_level)
71{
72 char *filename_start_ptr = line + 4;
73 int i;
74
75 /* Terminate string at end of source filename */
76 {
77 char *line_ptr;
78 line_ptr = strchr(filename_start_ptr, '\t');
79 if (!line_ptr) {
80 bb_perror_msg("Malformed line %s", line);
81 return(NULL);
82 }
83 *line_ptr = '\0';
84 }
85
86 /* Skip over (patch_level) number of leading directories */
87 for (i = 0; i < patch_level; i++) {
88 char *dirname_ptr;
89
90 dirname_ptr = strchr(filename_start_ptr, '/');
91 if (!dirname_ptr) {
92 break;
93 }
94 filename_start_ptr = dirname_ptr + 1;
95 }
96
97 return(bb_xstrdup(filename_start_ptr));
98}
99
100static int file_doesnt_exist(const char *filename)
101{
102 struct stat statbuf;
103 return(stat(filename, &statbuf));
104}
105
106extern int patch_main(int argc, char **argv)
107{
108 unsigned int patch_level = -1;
109 char *patch_line;
Rob Landley078bacf2005-09-01 03:02:23 +0000110 int ret;
111 FILE *patch_file = NULL;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000112
Rob Landley078bacf2005-09-01 03:02:23 +0000113 {
114 char *p, *i;
115 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
116 if (ret & 1)
117 patch_level = bb_xgetularg10_bnd(p, -1, USHRT_MAX);
118 if (ret & 2) {
119 patch_file = bb_xfopen(i, "r");
120 }
121 ret = 0;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000122 }
123
Rob Landley078bacf2005-09-01 03:02:23 +0000124 if (!patch_file)
125 patch_file = stdin;
126
127 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000128 while (patch_line) {
129 FILE *src_stream;
130 FILE *dst_stream;
131 char *original_filename;
132 char *new_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;
140
141 /* Skip everything upto the "---" marker
142 * No need to parse the lines "Only in <dir>", and "diff <args>"
143 */
144 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
145 free(patch_line);
Rob Landley078bacf2005-09-01 03:02:23 +0000146 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000147 }
148
149 /* Extract the filename used before the patch was generated */
150 original_filename = extract_filename(patch_line, patch_level);
151 free(patch_line);
152
Rob Landley078bacf2005-09-01 03:02:23 +0000153 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000154 if (strncmp(patch_line, "+++ ", 4) != 0) {
155 ret = 2;
156 bb_error_msg("Invalid patch");
157 continue;
158 }
159 new_filename = extract_filename(patch_line, patch_level);
160 free(patch_line);
161
162 if (file_doesnt_exist(new_filename)) {
163 char *line_ptr;
164 /* Create leading directories */
165 line_ptr = strrchr(new_filename, '/');
166 if (line_ptr) {
167 *line_ptr = '\0';
168 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
169 *line_ptr = '/';
170 }
171 dst_stream = bb_xfopen(new_filename, "w+");
172 backup_filename = NULL;
173 } else {
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);
179 }
180 dst_stream = bb_xfopen(new_filename, "w");
181 }
182
183 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
184 src_stream = NULL;
185 } else {
186 if (strcmp(original_filename, new_filename) == 0) {
187 src_stream = bb_xfopen(backup_filename, "r");
188 } else {
189 src_stream = bb_xfopen(original_filename, "r");
190 }
191 }
192
193 printf("patching file %s\n", new_filename);
194
195 /* Handle each hunk */
Rob Landley078bacf2005-09-01 03:02:23 +0000196 patch_line = bb_get_line_from_file(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000197 while (patch_line) {
198 unsigned int count;
199 unsigned int src_beg_line;
200 unsigned int unused;
201 unsigned int hunk_offset_start = 0;
202 int hunk_error = 0;
203
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 */
209 break;
210 }
211 free(patch_line);
212 hunk_count++;
213
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");
220 }
221 src_cur_line += count;
222 dest_cur_line += count;
223 copy_trailing_lines_flag = 1;
224 }
225 hunk_offset_start = src_cur_line;
226
Rob Landley078bacf2005-09-01 03:02:23 +0000227 while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
Glenn L McGrath655d8142003-06-22 15:32:41 +0000228 if ((*patch_line == '-') || (*patch_line == ' ')) {
229 char *src_line = NULL;
230 if (src_stream) {
231 src_line = bb_get_line_from_file(src_stream);
232 if (!src_line) {
233 hunk_error++;
234 break;
235 } else {
236 src_cur_line++;
237 }
238 if (strcmp(src_line, patch_line + 1) != 0) {
239 bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
240 hunk_error++;
241 free(patch_line);
242 break;
243 }
244 free(src_line);
245 }
246 if (*patch_line == ' ') {
247 fputs(patch_line + 1, dst_stream);
248 dest_cur_line++;
249 }
250 } else if (*patch_line == '+') {
251 fputs(patch_line + 1, dst_stream);
252 dest_cur_line++;
253 } else {
254 break;
255 }
256 free(patch_line);
257 }
258 if (hunk_error) {
259 bad_hunk_count++;
260 }
261 }
262
263 /* Cleanup last patched file */
264 if (copy_trailing_lines_flag) {
265 copy_lines(src_stream, dst_stream, -1);
266 }
267 if (src_stream) {
268 fclose(src_stream);
269 }
270 if (dst_stream) {
271 fclose(dst_stream);
272 }
273 if (bad_hunk_count) {
274 if (!ret) {
275 ret = 1;
276 }
277 bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
278 } else {
279 /* It worked, we can remove the backup */
280 if (backup_filename) {
281 unlink(backup_filename);
282 }
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);
287 }
288 if (unlink(original_filename) == -1) {
289 bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
290 }
291 }
292 }
293 }
294
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000295 /* 0 = SUCCESS
Glenn L McGrath655d8142003-06-22 15:32:41 +0000296 * 1 = Some hunks failed
297 * 2 = More serious problems
298 */
299 return(ret);
300}