blob: 4a97151449751e5fc21c8d1369aaf28110ac679b [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.
Denis Vlasenko0beaff82007-09-21 13:16:32 +00004 * Copyright (C) 2003 Glenn McGrath
Glenn L McGrath655d8142003-06-22 15:32:41 +00005 *
Bernhard Reutner-Fischeree9cf482005-10-27 06:59:05 +00006 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath655d8142003-06-22 15:32:41 +00007 *
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
Denis Vlasenko0a10da22006-12-17 00:49:56 +000018 * - Patches must apply cleanly or patch (not just one hunk) will fail.
Glenn L McGrath655d8142003-06-22 15:32:41 +000019 * - Reject file isnt saved
Glenn L McGrath655d8142003-06-22 15:32:41 +000020 */
21
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
Glenn L McGrath655d8142003-06-22 15:32:41 +000023
Denis Vlasenko08ec67b2008-03-26 13:32:30 +000024static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count)
Glenn L McGrath655d8142003-06-22 15:32:41 +000025{
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000026 while (src_stream && lines_count) {
Glenn L McGrath655d8142003-06-22 15:32:41 +000027 char *line;
Denis Vlasenko2d5ca602006-10-12 22:43:20 +000028 line = xmalloc_fgets(src_stream);
Glenn L McGrath655d8142003-06-22 15:32:41 +000029 if (line == NULL) {
30 break;
31 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +000032 if (fputs(line, dst_stream) == EOF) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000033 bb_perror_msg_and_die("error writing to new file");
Glenn L McGrath655d8142003-06-22 15:32:41 +000034 }
35 free(line);
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000036 lines_count--;
Glenn L McGrath655d8142003-06-22 15:32:41 +000037 }
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000038 return lines_count;
Glenn L McGrath655d8142003-06-22 15:32:41 +000039}
40
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
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000045 * NB: frees 1st argument!
Glenn L McGrath655d8142003-06-22 15:32:41 +000046 */
Denis Vlasenko64a76d72008-03-24 18:18:03 +000047static char *extract_filename(char *line, int patch_level, const char *pat)
Glenn L McGrath655d8142003-06-22 15:32:41 +000048{
Denis Vlasenko64a76d72008-03-24 18:18:03 +000049 char *temp = NULL, *filename_start_ptr = line + 4;
Glenn L McGrath655d8142003-06-22 15:32:41 +000050
Denis Vlasenko64a76d72008-03-24 18:18:03 +000051 if (strncmp(line, pat, 4) == 0) {
52 /* Terminate string at end of source filename */
Denis Vlasenkof5d8c902008-06-26 14:32:57 +000053 line[strcspn(line, "\t\n\r")] = '\0';
Glenn L McGrath655d8142003-06-22 15:32:41 +000054
Denis Vlasenko64a76d72008-03-24 18:18:03 +000055 /* Skip over (patch_level) number of leading directories */
56 while (patch_level--) {
57 temp = strchr(filename_start_ptr, '/');
58 if (!temp)
59 break;
60 filename_start_ptr = temp + 1;
61 }
62 temp = xstrdup(filename_start_ptr);
Glenn L McGrath655d8142003-06-22 15:32:41 +000063 }
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000064 free(line);
65 return temp;
Glenn L McGrath655d8142003-06-22 15:32:41 +000066}
67
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000068int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000069int patch_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath655d8142003-06-22 15:32:41 +000070{
Denis Vlasenko2a7a4512008-03-18 01:38:04 +000071 struct stat saved_stat;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000072 char *patch_line;
73 FILE *patch_file;
74 int patch_level;
75 int ret = 0;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +000076 char plus = '+';
Denys Vlasenkod7874ed2009-08-20 23:33:19 +020077 unsigned opt;
78 enum {
79 OPT_R = (1 << 2),
80 OPT_N = (1 << 3),
81 };
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000082
Denis Vlasenkoc6938402008-03-24 02:18:03 +000083 xfunc_error_retval = 2;
Rob Landley078bacf2005-09-01 03:02:23 +000084 {
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000085 const char *p = "-1";
86 const char *i = "-"; /* compat */
Denys Vlasenkod7874ed2009-08-20 23:33:19 +020087 opt = getopt32(argv, "p:i:RN", &p, &i);
88 if (opt & OPT_R)
Denis Vlasenko08ec67b2008-03-26 13:32:30 +000089 plus = '-';
Denis Vlasenkoc93b1622008-03-23 22:55:25 +000090 patch_level = xatoi(p); /* can be negative! */
91 patch_file = xfopen_stdin(i);
Glenn L McGrath655d8142003-06-22 15:32:41 +000092 }
93
Denis Vlasenko8ee649a2008-03-26 20:04:27 +000094 patch_line = xmalloc_fgetline(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +000095 while (patch_line) {
96 FILE *src_stream;
97 FILE *dst_stream;
Denis Vlasenkocc2965f2008-03-25 14:15:39 +000098 //char *old_filename;
Glenn L McGrath655d8142003-06-22 15:32:41 +000099 char *new_filename;
100 char *backup_filename;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000101 unsigned src_cur_line = 1;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000102 unsigned dst_cur_line = 0;
103 unsigned dst_beg_line;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000104 unsigned bad_hunk_count = 0;
105 unsigned hunk_count = 0;
106 smallint copy_trailing_lines_flag = 0;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000107
108 /* Skip everything upto the "---" marker
109 * No need to parse the lines "Only in <dir>", and "diff <args>"
110 */
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000111 do {
112 /* Extract the filename used before the patch was generated */
Denis Vlasenkocc2965f2008-03-25 14:15:39 +0000113 new_filename = extract_filename(patch_line, patch_level, "--- ");
114 // was old_filename above
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000115 patch_line = xmalloc_fgetline(patch_file);
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000116 if (!patch_line) goto quit;
Denis Vlasenkocc2965f2008-03-25 14:15:39 +0000117 } while (!new_filename);
118 free(new_filename); // "source" filename is irrelevant
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000119
120 new_filename = extract_filename(patch_line, patch_level, "+++ ");
121 if (!new_filename) {
122 bb_error_msg_and_die("invalid patch");
Glenn L McGrath655d8142003-06-22 15:32:41 +0000123 }
124
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000125 /* Get access rights from the file to be patched */
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000126 if (stat(new_filename, &saved_stat) != 0) {
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000127 char *slash = strrchr(new_filename, '/');
128 if (slash) {
129 /* Create leading directories */
130 *slash = '\0';
Glenn L McGrath655d8142003-06-22 15:32:41 +0000131 bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000132 *slash = '/';
Glenn L McGrath655d8142003-06-22 15:32:41 +0000133 }
Glenn L McGrath655d8142003-06-22 15:32:41 +0000134 backup_filename = NULL;
Denis Vlasenkocc2965f2008-03-25 14:15:39 +0000135 src_stream = NULL;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000136 saved_stat.st_mode = 0644;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000137 } else {
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000138 backup_filename = xasprintf("%s.orig", new_filename);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000139 xrename(new_filename, backup_filename);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000140 src_stream = xfopen_for_read(backup_filename);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000141 }
Denis Vlasenko5415c852008-07-21 23:05:26 +0000142 dst_stream = xfopen_for_write(new_filename);
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000143 fchmod(fileno(dst_stream), saved_stat.st_mode);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000144
145 printf("patching file %s\n", new_filename);
146
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000147 /* Handle all hunks for this file */
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000148 patch_line = xmalloc_fgets(patch_file);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000149 while (patch_line) {
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000150 unsigned count;
151 unsigned src_beg_line;
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000152 unsigned hunk_offset_start;
153 unsigned src_last_line = 1;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000154 unsigned dst_last_line = 1;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000155
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000156 if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
157 && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
158 ) {
159 /* No more hunks for this file */
Glenn L McGrath655d8142003-06-22 15:32:41 +0000160 break;
161 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000162 if (plus != '+') {
163 /* reverse patch */
164 unsigned tmp = src_last_line;
165 src_last_line = dst_last_line;
166 dst_last_line = tmp;
167 tmp = src_beg_line;
168 src_beg_line = dst_beg_line;
169 dst_beg_line = tmp;
170 }
Glenn L McGrath655d8142003-06-22 15:32:41 +0000171 hunk_count++;
172
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000173 if (src_beg_line && dst_beg_line) {
Glenn L McGrath655d8142003-06-22 15:32:41 +0000174 /* Copy unmodified lines upto start of hunk */
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000175 /* src_beg_line will be 0 if it's a new file */
Glenn L McGrath655d8142003-06-22 15:32:41 +0000176 count = src_beg_line - src_cur_line;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000177 if (copy_lines(src_stream, dst_stream, count)) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000178 bb_error_msg_and_die("bad src file");
Glenn L McGrath655d8142003-06-22 15:32:41 +0000179 }
180 src_cur_line += count;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000181 dst_cur_line += count;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000182 copy_trailing_lines_flag = 1;
183 }
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000184 src_last_line += hunk_offset_start = src_cur_line;
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000185 dst_last_line += dst_cur_line;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000186
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000187 while (1) {
188 free(patch_line);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000189 patch_line = xmalloc_fgets(patch_file);
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000190 if (patch_line == NULL)
191 break; /* EOF */
192 if ((*patch_line != '-') && (*patch_line != '+')
193 && (*patch_line != ' ')
194 ) {
195 break; /* End of hunk */
196 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000197 if (*patch_line != plus) { /* '-' or ' ' */
Glenn L McGrath655d8142003-06-22 15:32:41 +0000198 char *src_line = NULL;
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000199 if (src_cur_line == src_last_line)
200 break;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000201 if (src_stream) {
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000202 src_line = xmalloc_fgets(src_stream);
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000203 if (src_line) {
204 int diff = strcmp(src_line, patch_line + 1);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000205 src_cur_line++;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000206 free(src_line);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000207 if (diff)
208 src_line = NULL;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000209 }
Glenn L McGrath655d8142003-06-22 15:32:41 +0000210 }
Denys Vlasenkod7874ed2009-08-20 23:33:19 +0200211 /* Do not patch an already patched hunk with -N */
212 if (src_line == 0 && (opt & OPT_N)) {
213 continue;
214 }
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000215 if (!src_line) {
216 bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
217 bad_hunk_count++;
218 break;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000219 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000220 if (*patch_line != ' ') { /* '-' */
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000221 continue;
222 }
Glenn L McGrath655d8142003-06-22 15:32:41 +0000223 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000224 if (dst_cur_line == dst_last_line)
225 break;
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000226 fputs(patch_line + 1, dst_stream);
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000227 dst_cur_line++;
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000228 } /* end of while loop handling one hunk */
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000229 } /* end of while loop handling one file */
Glenn L McGrath655d8142003-06-22 15:32:41 +0000230
231 /* Cleanup last patched file */
232 if (copy_trailing_lines_flag) {
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000233 copy_lines(src_stream, dst_stream, (unsigned)(-1));
Glenn L McGrath655d8142003-06-22 15:32:41 +0000234 }
235 if (src_stream) {
236 fclose(src_stream);
237 }
Denis Vlasenko64a76d72008-03-24 18:18:03 +0000238 fclose(dst_stream);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000239 if (bad_hunk_count) {
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000240 ret = 1;
241 bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000242 } else {
243 /* It worked, we can remove the backup */
244 if (backup_filename) {
245 unlink(backup_filename);
246 }
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000247 if ((dst_cur_line == 0) || (dst_beg_line == 0)) {
Glenn L McGrath655d8142003-06-22 15:32:41 +0000248 /* The new patched file is empty, remove it */
Denis Vlasenko1bb552b2007-04-05 21:25:15 +0000249 xunlink(new_filename);
Denis Vlasenkocc2965f2008-03-25 14:15:39 +0000250 // /* old_filename and new_filename may be the same file */
251 // unlink(old_filename);
Glenn L McGrath655d8142003-06-22 15:32:41 +0000252 }
253 }
Denis Vlasenkocc2965f2008-03-25 14:15:39 +0000254 free(backup_filename);
255 //free(old_filename);
256 free(new_filename);
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000257 } /* end of "while there are patch lines" */
Denis Vlasenko08ec67b2008-03-26 13:32:30 +0000258 quit:
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000259 /* 0 = SUCCESS
Glenn L McGrath655d8142003-06-22 15:32:41 +0000260 * 1 = Some hunks failed
Denis Vlasenkoc93b1622008-03-23 22:55:25 +0000261 * 2 = More serious problems (exited earlier)
Glenn L McGrath655d8142003-06-22 15:32:41 +0000262 */
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000263 return ret;
Glenn L McGrath655d8142003-06-22 15:32:41 +0000264}