Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * busybox patch applet to handle the unified diff format. |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003 Glenn McGrath |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 5 | * |
Bernhard Reutner-Fischer | ee9cf48 | 2005-10-27 06:59:05 +0000 | [diff] [blame] | 6 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 |
Denis Vlasenko | 0a10da2 | 2006-12-17 00:49:56 +0000 | [diff] [blame] | 18 | * - Patches must apply cleanly or patch (not just one hunk) will fail. |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 19 | * - Reject file isnt saved |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 22 | #include "libbb.h" |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 24 | static unsigned copy_lines(FILE *src_stream, FILE *dest_stream, unsigned lines_count) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 25 | { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 26 | while (src_stream && lines_count) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 27 | char *line; |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 28 | line = xmalloc_fgets(src_stream); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 29 | if (line == NULL) { |
| 30 | break; |
| 31 | } |
| 32 | if (fputs(line, dest_stream) == EOF) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 33 | bb_perror_msg_and_die("error writing to new file"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 34 | } |
| 35 | free(line); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 36 | lines_count--; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 37 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 38 | return lines_count; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 39 | } |
| 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 Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 45 | * NB: frees 1st argument! |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 46 | */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 47 | static char *extract_filename_and_free_line(char *line, int patch_level) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 48 | { |
Rob Landley | baa89b3 | 2006-05-07 21:10:06 +0000 | [diff] [blame] | 49 | char *temp, *filename_start_ptr = line + 4; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 50 | |
| 51 | /* Terminate string at end of source filename */ |
Denis Vlasenko | eea7212 | 2007-08-06 13:34:10 +0000 | [diff] [blame] | 52 | temp = strchrnul(filename_start_ptr, '\t'); |
| 53 | *temp = '\0'; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | eea7212 | 2007-08-06 13:34:10 +0000 | [diff] [blame] | 55 | /* Skip over (patch_level) number of leading directories */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 56 | while (patch_level--) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 57 | temp = strchr(filename_start_ptr, '/'); |
| 58 | if (!temp) |
| 59 | break; |
Rob Landley | baa89b3 | 2006-05-07 21:10:06 +0000 | [diff] [blame] | 60 | filename_start_ptr = temp + 1; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 63 | temp = xstrdup(filename_start_ptr); |
| 64 | free(line); |
| 65 | return temp; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 68 | int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 69 | int patch_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 70 | { |
Denis Vlasenko | 2a7a451 | 2008-03-18 01:38:04 +0000 | [diff] [blame] | 71 | struct stat saved_stat; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 72 | char *patch_line; |
| 73 | FILE *patch_file; |
| 74 | int patch_level; |
| 75 | int ret = 0; |
| 76 | |
| 77 | xfunc_error_retval = 2; |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 78 | { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 79 | const char *p = "-1"; |
| 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); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Denis Vlasenko | fc77eb5 | 2007-08-01 23:30:54 +0000 | [diff] [blame] | 86 | patch_line = xmalloc_getline(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 87 | while (patch_line) { |
| 88 | FILE *src_stream; |
| 89 | FILE *dst_stream; |
| 90 | char *original_filename; |
| 91 | char *new_filename; |
| 92 | char *backup_filename; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 99 | |
| 100 | /* Skip everything upto the "---" marker |
| 101 | * No need to parse the lines "Only in <dir>", and "diff <args>" |
| 102 | */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 103 | while (strncmp(patch_line, "--- ", 4) != 0) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 104 | free(patch_line); |
Denis Vlasenko | fc77eb5 | 2007-08-01 23:30:54 +0000 | [diff] [blame] | 105 | patch_line = xmalloc_getline(patch_file); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 106 | if (!patch_line) |
| 107 | bb_error_msg_and_die("invalid patch"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* Extract the filename used before the patch was generated */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 111 | original_filename = extract_filename_and_free_line(patch_line, patch_level); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 112 | |
Denis Vlasenko | fc77eb5 | 2007-08-01 23:30:54 +0000 | [diff] [blame] | 113 | patch_line = xmalloc_getline(patch_file); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 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); |
Denis Vlasenko | 2a7a451 | 2008-03-18 01:38:04 +0000 | [diff] [blame] | 117 | |
| 118 | /* Get access rights from the file to be patched, -1 file does not exist */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 119 | if (stat(new_filename, &saved_stat) != 0) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 120 | char *line_ptr; |
| 121 | /* Create leading directories */ |
| 122 | line_ptr = strrchr(new_filename, '/'); |
| 123 | if (line_ptr) { |
| 124 | *line_ptr = '\0'; |
| 125 | bb_make_directory(new_filename, -1, FILEUTILS_RECUR); |
| 126 | *line_ptr = '/'; |
| 127 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 128 | backup_filename = NULL; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 129 | saved_stat.st_mode = 0644; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 130 | } else { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 131 | backup_filename = xasprintf("%s.orig", new_filename); |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 132 | xrename(new_filename, backup_filename); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 133 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 134 | dst_stream = xfopen(new_filename, "w"); |
| 135 | fchmod(fileno(dst_stream), saved_stat.st_mode); |
| 136 | src_stream = NULL; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 137 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 138 | if (backup_filename && !stat(original_filename, &saved_stat)) { |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame^] | 139 | src_stream = xfopen((strcmp(original_filename, new_filename)) ? |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 140 | original_filename : backup_filename, "r"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | printf("patching file %s\n", new_filename); |
| 144 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 145 | /* Handle all hunks for this file */ |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 146 | patch_line = xmalloc_fgets(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 147 | while (patch_line) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 148 | unsigned count; |
| 149 | unsigned src_beg_line; |
| 150 | unsigned unused; |
| 151 | unsigned hunk_offset_start = 0; |
| 152 | smallint hunk_error = 0; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 153 | |
| 154 | /* This bit should be improved */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 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) |
| 158 | ) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 159 | /* No more hunks for this file */ |
| 160 | break; |
| 161 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 162 | hunk_count++; |
| 163 | |
| 164 | if (src_beg_line && dest_beg_line) { |
| 165 | /* Copy unmodified lines upto start of hunk */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 166 | /* src_beg_line will be 0 if it's a new file */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 167 | count = src_beg_line - src_cur_line; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 168 | if (copy_lines(src_stream, dst_stream, count)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 169 | bb_error_msg_and_die("bad src file"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 170 | } |
| 171 | src_cur_line += count; |
| 172 | dest_cur_line += count; |
| 173 | copy_trailing_lines_flag = 1; |
| 174 | } |
| 175 | hunk_offset_start = src_cur_line; |
| 176 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 177 | while (1) { |
| 178 | free(patch_line); |
| 179 | patch_line = xmalloc_fgets(patch_file); |
| 180 | if (patch_line == NULL) break; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 181 | if ((*patch_line == '-') || (*patch_line == ' ')) { |
| 182 | char *src_line = NULL; |
| 183 | if (src_stream) { |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 184 | src_line = xmalloc_fgets(src_stream); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 185 | if (src_line) { |
| 186 | int diff = strcmp(src_line, patch_line + 1); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 187 | src_cur_line++; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 188 | free(src_line); |
| 189 | if (diff) src_line = NULL; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 190 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 191 | if (!src_line) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 192 | bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 193 | hunk_error = 1; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 194 | break; |
| 195 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 196 | } |
| 197 | if (*patch_line == ' ') { |
| 198 | fputs(patch_line + 1, dst_stream); |
| 199 | dest_cur_line++; |
| 200 | } |
| 201 | } else if (*patch_line == '+') { |
| 202 | fputs(patch_line + 1, dst_stream); |
| 203 | dest_cur_line++; |
| 204 | } else { |
| 205 | break; |
| 206 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 207 | } /* end of while loop handling one hunk */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 208 | if (hunk_error) { |
| 209 | bad_hunk_count++; |
| 210 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 211 | } /* end of while loop handling one file */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 212 | |
| 213 | /* Cleanup last patched file */ |
| 214 | if (copy_trailing_lines_flag) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 215 | copy_lines(src_stream, dst_stream, (unsigned)(-1)); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 216 | } |
| 217 | if (src_stream) { |
| 218 | fclose(src_stream); |
| 219 | } |
| 220 | if (dst_stream) { |
| 221 | fclose(dst_stream); |
| 222 | } |
| 223 | if (bad_hunk_count) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 224 | ret = 1; |
| 225 | bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 226 | } else { |
| 227 | /* It worked, we can remove the backup */ |
| 228 | if (backup_filename) { |
| 229 | unlink(backup_filename); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 230 | free(backup_filename); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 231 | } |
| 232 | if ((dest_cur_line == 0) || (dest_beg_line == 0)) { |
| 233 | /* The new patched file is empty, remove it */ |
Denis Vlasenko | 1bb552b | 2007-04-05 21:25:15 +0000 | [diff] [blame] | 234 | xunlink(new_filename); |
| 235 | if (strcmp(new_filename, original_filename) != 0) |
| 236 | xunlink(original_filename); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 239 | } /* end of "while there are patch lines" */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 240 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 241 | /* 0 = SUCCESS |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 242 | * 1 = Some hunks failed |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 243 | * 2 = More serious problems (exited earlier) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 244 | */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 245 | return ret; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 246 | } |