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. |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003 Glenn McGrath <bug1@iinet.net.au> |
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 |
| 18 | * - Patches must apply cleanly or the hunk will fail. |
| 19 | * - Reject file isnt saved |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 20 | * - |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <getopt.h> |
| 24 | #include <string.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <unistd.h> |
| 27 | #include "busybox.h" |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 28 | |
"Vladimir N. Oleynik" | dfe6e74 | 2006-01-31 09:44:04 +0000 | [diff] [blame] | 29 | static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 30 | { |
"Vladimir N. Oleynik" | dfe6e74 | 2006-01-31 09:44:04 +0000 | [diff] [blame] | 31 | unsigned int i = 0; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 | |
"Vladimir N. Oleynik" | dfe6e74 | 2006-01-31 09:44:04 +0000 | [diff] [blame] | 55 | static char *extract_filename(char *line, int patch_level) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 91 | int patch_main(int argc, char **argv) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 92 | { |
"Vladimir N. Oleynik" | dfe6e74 | 2006-01-31 09:44:04 +0000 | [diff] [blame] | 93 | int patch_level = -1; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 94 | char *patch_line; |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 95 | int ret; |
| 96 | FILE *patch_file = NULL; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 97 | |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 98 | { |
| 99 | char *p, *i; |
| 100 | ret = bb_getopt_ulflags(argc, argv, "p:i:", &p, &i); |
| 101 | if (ret & 1) |
Bernhard Reutner-Fischer | ee9cf48 | 2005-10-27 06:59:05 +0000 | [diff] [blame] | 102 | patch_level = bb_xgetlarg(p, 10, -1, USHRT_MAX); |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 103 | if (ret & 2) { |
| 104 | patch_file = bb_xfopen(i, "r"); |
Bernhard Reutner-Fischer | 554a9ff | 2005-10-10 13:34:19 +0000 | [diff] [blame] | 105 | } else { |
| 106 | patch_file = stdin; |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 107 | } |
| 108 | ret = 0; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 111 | patch_line = bb_get_line_from_file(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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); |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 130 | patch_line = bb_get_line_from_file(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 | |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 137 | patch_line = bb_get_line_from_file(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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) { |
Bernhard Reutner-Fischer | 554a9ff | 2005-10-10 13:34:19 +0000 | [diff] [blame] | 162 | bb_perror_msg_and_die("Couldnt create file %s", |
| 163 | backup_filename); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 */ |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 181 | patch_line = bb_get_line_from_file(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 | |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 212 | while ((patch_line = bb_get_line_from_file(patch_file)) != NULL) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 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 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 280 | /* 0 = SUCCESS |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 281 | * 1 = Some hunks failed |
| 282 | * 2 = More serious problems |
| 283 | */ |
| 284 | return(ret); |
| 285 | } |