- correct boilerplate
[platform/upstream/busybox.git] / editors / patch.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  busybox patch applet to handle the unified diff format.
4  *  Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au>
5  *
6  *  Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
7  *
8  *  This applet is written to work with patches generated by GNU diff,
9  *  where there is equivalent functionality busybox patch shall behave
10  *  as per GNU patch.
11  *
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
15  *
16  *  Issues
17  *   - Non-interactive
18  *   - Patches must apply cleanly or the hunk will fail.
19  *   - Reject file isnt saved
20  *   -
21  */
22
23 #include <getopt.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include "busybox.h"
28
29 static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
30 {
31         int i = 0;
32
33         while (src_stream && (i < lines_count)) {
34                 char *line;
35                 line = bb_get_line_from_file(src_stream);
36                 if (line == NULL) {
37                         break;
38                 }
39                 if (fputs(line, dest_stream) == EOF) {
40                         bb_perror_msg_and_die("Error writing to new file");
41                 }
42                 free(line);
43
44                 i++;
45         }
46         return(i);
47 }
48
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
53  */
54
55 static unsigned char *extract_filename(char *line, unsigned short patch_level)
56 {
57         char *filename_start_ptr = line + 4;
58         int i;
59
60         /* Terminate string at end of source filename */
61         {
62                 char *line_ptr;
63                 line_ptr = strchr(filename_start_ptr, '\t');
64                 if (!line_ptr) {
65                         bb_perror_msg("Malformed line %s", line);
66                         return(NULL);
67                 }
68                 *line_ptr = '\0';
69         }
70
71         /* Skip over (patch_level) number of leading directories */
72         for (i = 0; i < patch_level; i++) {
73                 char *dirname_ptr;
74
75                 dirname_ptr = strchr(filename_start_ptr, '/');
76                 if (!dirname_ptr) {
77                         break;
78                 }
79                 filename_start_ptr = dirname_ptr + 1;
80         }
81
82         return(bb_xstrdup(filename_start_ptr));
83 }
84
85 static int file_doesnt_exist(const char *filename)
86 {
87         struct stat statbuf;
88         return(stat(filename, &statbuf));
89 }
90
91 extern int patch_main(int argc, char **argv)
92 {
93         unsigned int patch_level = -1;
94         char *patch_line;
95         int ret;
96         FILE *patch_file = NULL;
97
98         {
99                 char *p, *i;
100                 ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i);
101                 if (ret & 1)
102                         patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX);
103                 if (ret & 2) {
104                         patch_file = bb_xfopen(i, "r");
105                 } else {
106                         patch_file = stdin;
107                 }
108                 ret = 0;
109         }
110
111         patch_line = bb_get_line_from_file(patch_file);
112         while (patch_line) {
113                 FILE *src_stream;
114                 FILE *dst_stream;
115                 char *original_filename;
116                 char *new_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;
124
125                 /* Skip everything upto the "---" marker
126                  * No need to parse the lines "Only in <dir>", and "diff <args>"
127                  */
128                 while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
129                         free(patch_line);
130                         patch_line = bb_get_line_from_file(patch_file);
131                 }
132
133                 /* Extract the filename used before the patch was generated */
134                 original_filename = extract_filename(patch_line, patch_level);
135                 free(patch_line);
136
137                 patch_line = bb_get_line_from_file(patch_file);
138                 if (strncmp(patch_line, "+++ ", 4) != 0) {
139                         ret = 2;
140                         bb_error_msg("Invalid patch");
141                         continue;
142                 }
143                 new_filename = extract_filename(patch_line, patch_level);
144                 free(patch_line);
145
146                 if (file_doesnt_exist(new_filename)) {
147                         char *line_ptr;
148                         /* Create leading directories */
149                         line_ptr = strrchr(new_filename, '/');
150                         if (line_ptr) {
151                                 *line_ptr = '\0';
152                                 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
153                                 *line_ptr = '/';
154                         }
155                         dst_stream = bb_xfopen(new_filename, "w+");
156                         backup_filename = NULL;
157                 } else {
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",
163                                                 backup_filename);
164                         }
165                         dst_stream = bb_xfopen(new_filename, "w");
166                 }
167
168                 if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
169                         src_stream = NULL;
170                 } else {
171                         if (strcmp(original_filename, new_filename) == 0) {
172                                 src_stream = bb_xfopen(backup_filename, "r");
173                         } else {
174                                 src_stream = bb_xfopen(original_filename, "r");
175                         }
176                 }
177
178                 printf("patching file %s\n", new_filename);
179
180                 /* Handle each hunk */
181                 patch_line = bb_get_line_from_file(patch_file);
182                 while (patch_line) {
183                         unsigned int count;
184                         unsigned int src_beg_line;
185                         unsigned int unused;
186                         unsigned int hunk_offset_start = 0;
187                         int hunk_error = 0;
188
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 */
194                                 break;
195                         }
196                         free(patch_line);
197                         hunk_count++;
198
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");
205                                 }
206                                 src_cur_line += count;
207                                 dest_cur_line += count;
208                                 copy_trailing_lines_flag = 1;
209                         }
210                         hunk_offset_start = src_cur_line;
211
212                         while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) {
213                                 if ((*patch_line == '-') || (*patch_line == ' ')) {
214                                         char *src_line = NULL;
215                                         if (src_stream) {
216                                                 src_line = bb_get_line_from_file(src_stream);
217                                                 if (!src_line) {
218                                                         hunk_error++;
219                                                         break;
220                                                 } else {
221                                                         src_cur_line++;
222                                                 }
223                                                 if (strcmp(src_line, patch_line + 1) != 0) {
224                                                         bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
225                                                         hunk_error++;
226                                                         free(patch_line);
227                                                         break;
228                                                 }
229                                                 free(src_line);
230                                         }
231                                         if (*patch_line == ' ') {
232                                                 fputs(patch_line + 1, dst_stream);
233                                                 dest_cur_line++;
234                                         }
235                                 } else if (*patch_line == '+') {
236                                         fputs(patch_line + 1, dst_stream);
237                                         dest_cur_line++;
238                                 } else {
239                                         break;
240                                 }
241                                 free(patch_line);
242                         }
243                         if (hunk_error) {
244                                 bad_hunk_count++;
245                         }
246                 }
247
248                 /* Cleanup last patched file */
249                 if (copy_trailing_lines_flag) {
250                         copy_lines(src_stream, dst_stream, -1);
251                 }
252                 if (src_stream) {
253                         fclose(src_stream);
254                 }
255                 if (dst_stream) {
256                         fclose(dst_stream);
257                 }
258                 if (bad_hunk_count) {
259                         if (!ret) {
260                                 ret = 1;
261                         }
262                         bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
263                 } else {
264                         /* It worked, we can remove the backup */
265                         if (backup_filename) {
266                                 unlink(backup_filename);
267                         }
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);
272                                 }
273                                 if (unlink(original_filename) == -1) {
274                                         bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
275                                 }
276                         }
277                 }
278         }
279
280         /* 0 = SUCCESS
281          * 1 = Some hunks failed
282          * 2 = More serious problems
283          */
284         return(ret);
285 }