1 /* vi: set sw=4 ts=4: */
3 * busybox patch applet to handle the unified diff format.
4 * Copyright (C) 2003 Glenn McGrath
6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8 * This applet is written to work with patches generated by GNU diff,
9 * where there is equivalent functionality busybox patch shall behave
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
18 * - Patches must apply cleanly or patch (not just one hunk) will fail.
19 * - Reject file isnt saved
24 static unsigned copy_lines(FILE *src_stream, FILE *dest_stream, unsigned lines_count)
26 while (src_stream && lines_count) {
28 line = xmalloc_fgets(src_stream);
32 if (fputs(line, dest_stream) == EOF) {
33 bb_perror_msg_and_die("error writing to new file");
41 /* If patch_level is -1 it will remove all directory names
42 * char *line must be greater than 4 chars
43 * returns NULL if the file doesnt exist or error
44 * returns malloc'ed filename
45 * NB: frees 1st argument!
47 static char *extract_filename_and_free_line(char *line, int patch_level)
49 char *temp, *filename_start_ptr = line + 4;
51 /* Terminate string at end of source filename */
52 temp = strchrnul(filename_start_ptr, '\t');
55 /* Skip over (patch_level) number of leading directories */
56 while (patch_level--) {
57 temp = strchr(filename_start_ptr, '/');
60 filename_start_ptr = temp + 1;
63 temp = xstrdup(filename_start_ptr);
68 int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
69 int patch_main(int argc ATTRIBUTE_UNUSED, char **argv)
71 struct stat saved_stat;
77 xfunc_error_retval = 2;
80 const char *i = "-"; /* compat */
81 getopt32(argv, "p:i:", &p, &i);
82 patch_level = xatoi(p); /* can be negative! */
83 patch_file = xfopen_stdin(i);
86 patch_line = xmalloc_getline(patch_file);
90 char *original_filename;
92 char *backup_filename;
93 unsigned src_cur_line = 1;
94 unsigned dest_cur_line = 0;
95 unsigned dest_beg_line;
96 unsigned bad_hunk_count = 0;
97 unsigned hunk_count = 0;
98 smallint copy_trailing_lines_flag = 0;
100 /* Skip everything upto the "---" marker
101 * No need to parse the lines "Only in <dir>", and "diff <args>"
103 while (strncmp(patch_line, "--- ", 4) != 0) {
105 patch_line = xmalloc_getline(patch_file);
107 bb_error_msg_and_die("invalid patch");
110 /* Extract the filename used before the patch was generated */
111 original_filename = extract_filename_and_free_line(patch_line, patch_level);
113 patch_line = xmalloc_getline(patch_file);
114 if (!patch_line || strncmp(patch_line, "+++ ", 4) != 0)
115 bb_error_msg_and_die("invalid patch");
116 new_filename = extract_filename_and_free_line(patch_line, patch_level);
118 /* Get access rights from the file to be patched, -1 file does not exist */
119 if (stat(new_filename, &saved_stat) != 0) {
121 /* Create leading directories */
122 line_ptr = strrchr(new_filename, '/');
125 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
128 backup_filename = NULL;
129 saved_stat.st_mode = 0644;
131 backup_filename = xasprintf("%s.orig", new_filename);
132 xrename(new_filename, backup_filename);
134 dst_stream = xfopen(new_filename, "w");
135 fchmod(fileno(dst_stream), saved_stat.st_mode);
138 if (backup_filename && !stat(original_filename, &saved_stat)) {
139 src_stream = xfopen((strcmp(original_filename, new_filename)) ?
140 original_filename : backup_filename, "r");
143 printf("patching file %s\n", new_filename);
145 /* Handle all hunks for this file */
146 patch_line = xmalloc_fgets(patch_file);
149 unsigned src_beg_line;
151 unsigned hunk_offset_start = 0;
152 smallint hunk_error = 0;
154 /* This bit should be improved */
155 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4)
156 && (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3)
157 && (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)
159 /* No more hunks for this file */
164 if (src_beg_line && dest_beg_line) {
165 /* Copy unmodified lines upto start of hunk */
166 /* src_beg_line will be 0 if it's a new file */
167 count = src_beg_line - src_cur_line;
168 if (copy_lines(src_stream, dst_stream, count)) {
169 bb_error_msg_and_die("bad src file");
171 src_cur_line += count;
172 dest_cur_line += count;
173 copy_trailing_lines_flag = 1;
175 hunk_offset_start = src_cur_line;
179 patch_line = xmalloc_fgets(patch_file);
180 if (patch_line == NULL) break;
181 if ((*patch_line == '-') || (*patch_line == ' ')) {
182 char *src_line = NULL;
184 src_line = xmalloc_fgets(src_stream);
186 int diff = strcmp(src_line, patch_line + 1);
189 if (diff) src_line = NULL;
192 bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start);
197 if (*patch_line == ' ') {
198 fputs(patch_line + 1, dst_stream);
201 } else if (*patch_line == '+') {
202 fputs(patch_line + 1, dst_stream);
207 } /* end of while loop handling one hunk */
211 } /* end of while loop handling one file */
213 /* Cleanup last patched file */
214 if (copy_trailing_lines_flag) {
215 copy_lines(src_stream, dst_stream, (unsigned)(-1));
223 if (bad_hunk_count) {
225 bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
227 /* It worked, we can remove the backup */
228 if (backup_filename) {
229 unlink(backup_filename);
230 free(backup_filename);
232 if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
233 /* The new patched file is empty, remove it */
234 xunlink(new_filename);
235 if (strcmp(new_filename, original_filename) != 0)
236 xunlink(original_filename);
239 } /* end of "while there are patch lines" */
242 * 1 = Some hunks failed
243 * 2 = More serious problems (exited earlier)