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 | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 24 | static unsigned copy_lines(FILE *src_stream, FILE *dst_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 | } |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 32 | if (fputs(line, dst_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 | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 47 | static char *extract_filename(char *line, int patch_level, const char *pat) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 48 | { |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 49 | char *temp = NULL, *filename_start_ptr = line + 4; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 51 | if (strncmp(line, pat, 4) == 0) { |
| 52 | /* Terminate string at end of source filename */ |
Denis Vlasenko | f5d8c90 | 2008-06-26 14:32:57 +0000 | [diff] [blame] | 53 | line[strcspn(line, "\t\n\r")] = '\0'; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 54 | |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 55 | /* 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 McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 63 | } |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 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 | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 69 | int patch_main(int argc UNUSED_PARAM, 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; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 76 | char plus = '+'; |
Denys Vlasenko | d7874ed | 2009-08-20 23:33:19 +0200 | [diff] [blame] | 77 | unsigned opt; |
| 78 | enum { |
| 79 | OPT_R = (1 << 2), |
| 80 | OPT_N = (1 << 3), |
Denys Vlasenko | feadfe7 | 2010-01-22 04:17:07 +0100 | [diff] [blame] | 81 | /*OPT_f = (1 << 4), ignored */ |
| 82 | /*OPT_E = (1 << 5), ignored, this is the default */ |
Denys Vlasenko | 8b8d32e | 2010-01-22 05:10:33 +0100 | [diff] [blame] | 83 | /*OPT_g = (1 << 6), ignored */ |
| 84 | OPT_dry_run = (1 << 7) * ENABLE_LONG_OPTS, |
Denys Vlasenko | d7874ed | 2009-08-20 23:33:19 +0200 | [diff] [blame] | 85 | }; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 87 | xfunc_error_retval = 2; |
Rob Landley | 078bacf | 2005-09-01 03:02:23 +0000 | [diff] [blame] | 88 | { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 89 | const char *p = "-1"; |
| 90 | const char *i = "-"; /* compat */ |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 91 | #if ENABLE_LONG_OPTS |
| 92 | static const char patch_longopts[] ALIGN1 = |
Denys Vlasenko | feadfe7 | 2010-01-22 04:17:07 +0100 | [diff] [blame] | 93 | "strip\0" Required_argument "p" |
| 94 | "input\0" Required_argument "i" |
| 95 | "reverse\0" No_argument "R" |
| 96 | "forward\0" No_argument "N" |
| 97 | /* "Assume user knows what [s]he is doing, do not ask any questions": */ |
| 98 | "force\0" No_argument "f" /*ignored*/ |
| 99 | # if ENABLE_DESKTOP |
| 100 | "remove-empty-files\0" No_argument "E" /*ignored*/ |
| 101 | /* "Controls actions when a file is under RCS or SCCS control, |
| 102 | * and does not exist or is read-only and matches the default version, |
| 103 | * or when a file is under ClearCase control and does not exist..." |
Denys Vlasenko | 8b8d32e | 2010-01-22 05:10:33 +0100 | [diff] [blame] | 104 | * IOW: rather obscure option. |
| 105 | * But Gentoo's portage does use -g0 */ |
| 106 | "get\0" Required_argument "g" /*ignored*/ |
Denys Vlasenko | feadfe7 | 2010-01-22 04:17:07 +0100 | [diff] [blame] | 107 | # endif |
| 108 | "dry-run\0" No_argument "\xfd" |
| 109 | # if ENABLE_DESKTOP |
| 110 | "backup-if-mismatch\0" No_argument "\xfe" /*ignored*/ |
| 111 | "no-backup-if-mismatch\0" No_argument "\xff" /*ignored*/ |
| 112 | # endif |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 113 | ; |
| 114 | applet_long_options = patch_longopts; |
| 115 | #endif |
Denys Vlasenko | 8b8d32e | 2010-01-22 05:10:33 +0100 | [diff] [blame] | 116 | /* -f,-E,-g are ignored */ |
| 117 | opt = getopt32(argv, "p:i:RN""fEg:", &p, &i, NULL); |
Denys Vlasenko | d7874ed | 2009-08-20 23:33:19 +0200 | [diff] [blame] | 118 | if (opt & OPT_R) |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 119 | plus = '-'; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 120 | patch_level = xatoi(p); /* can be negative! */ |
| 121 | patch_file = xfopen_stdin(i); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 124 | patch_line = xmalloc_fgetline(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 125 | while (patch_line) { |
| 126 | FILE *src_stream; |
| 127 | FILE *dst_stream; |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 128 | //char *old_filename; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 129 | char *new_filename; |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 130 | char *backup_filename = NULL; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 131 | unsigned src_cur_line = 1; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 132 | unsigned dst_cur_line = 0; |
| 133 | unsigned dst_beg_line; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 134 | unsigned bad_hunk_count = 0; |
| 135 | unsigned hunk_count = 0; |
| 136 | smallint copy_trailing_lines_flag = 0; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 137 | |
| 138 | /* Skip everything upto the "---" marker |
| 139 | * No need to parse the lines "Only in <dir>", and "diff <args>" |
| 140 | */ |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 141 | do { |
| 142 | /* Extract the filename used before the patch was generated */ |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 143 | new_filename = extract_filename(patch_line, patch_level, "--- "); |
| 144 | // was old_filename above |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 145 | patch_line = xmalloc_fgetline(patch_file); |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 146 | if (!patch_line) goto quit; |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 147 | } while (!new_filename); |
| 148 | free(new_filename); // "source" filename is irrelevant |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 149 | |
| 150 | new_filename = extract_filename(patch_line, patch_level, "+++ "); |
| 151 | if (!new_filename) { |
| 152 | bb_error_msg_and_die("invalid patch"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 155 | /* Get access rights from the file to be patched */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 156 | if (stat(new_filename, &saved_stat) != 0) { |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 157 | char *slash = strrchr(new_filename, '/'); |
| 158 | if (slash) { |
| 159 | /* Create leading directories */ |
| 160 | *slash = '\0'; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 161 | bb_make_directory(new_filename, -1, FILEUTILS_RECUR); |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 162 | *slash = '/'; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 163 | } |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 164 | src_stream = NULL; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 165 | saved_stat.st_mode = 0644; |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 166 | } else if (!(opt & OPT_dry_run)) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 167 | backup_filename = xasprintf("%s.orig", new_filename); |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 168 | xrename(new_filename, backup_filename); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 169 | src_stream = xfopen_for_read(backup_filename); |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 170 | } else |
| 171 | src_stream = xfopen_for_read(new_filename); |
| 172 | |
| 173 | if (opt & OPT_dry_run) { |
| 174 | dst_stream = xfopen_for_write("/dev/null"); |
| 175 | } else { |
| 176 | dst_stream = xfopen_for_write(new_filename); |
| 177 | fchmod(fileno(dst_stream), saved_stat.st_mode); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 178 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 179 | |
| 180 | printf("patching file %s\n", new_filename); |
| 181 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 182 | /* Handle all hunks for this file */ |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 183 | patch_line = xmalloc_fgets(patch_file); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 184 | while (patch_line) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 185 | unsigned count; |
| 186 | unsigned src_beg_line; |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 187 | unsigned hunk_offset_start; |
| 188 | unsigned src_last_line = 1; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 189 | unsigned dst_last_line = 1; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 191 | if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3) |
| 192 | && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2) |
| 193 | ) { |
| 194 | /* No more hunks for this file */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 195 | break; |
| 196 | } |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 197 | if (plus != '+') { |
| 198 | /* reverse patch */ |
| 199 | unsigned tmp = src_last_line; |
| 200 | src_last_line = dst_last_line; |
| 201 | dst_last_line = tmp; |
| 202 | tmp = src_beg_line; |
| 203 | src_beg_line = dst_beg_line; |
| 204 | dst_beg_line = tmp; |
| 205 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 206 | hunk_count++; |
| 207 | |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 208 | if (src_beg_line && dst_beg_line) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 209 | /* Copy unmodified lines upto start of hunk */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 210 | /* 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] | 211 | count = src_beg_line - src_cur_line; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 212 | if (copy_lines(src_stream, dst_stream, count)) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 213 | bb_error_msg_and_die("bad src file"); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 214 | } |
| 215 | src_cur_line += count; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 216 | dst_cur_line += count; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 217 | copy_trailing_lines_flag = 1; |
| 218 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 219 | src_last_line += hunk_offset_start = src_cur_line; |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 220 | dst_last_line += dst_cur_line; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 222 | while (1) { |
| 223 | free(patch_line); |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 224 | patch_line = xmalloc_fgets(patch_file); |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 225 | if (patch_line == NULL) |
| 226 | break; /* EOF */ |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 227 | if (!*patch_line) { |
| 228 | /* whitespace-damaged patch with "" lines */ |
| 229 | free(patch_line); |
| 230 | patch_line = xstrdup(" "); |
| 231 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 232 | if ((*patch_line != '-') && (*patch_line != '+') |
| 233 | && (*patch_line != ' ') |
| 234 | ) { |
| 235 | break; /* End of hunk */ |
| 236 | } |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 237 | if (*patch_line != plus) { /* '-' or ' ' */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 238 | char *src_line = NULL; |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 239 | if (src_cur_line == src_last_line) |
| 240 | break; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 241 | if (src_stream) { |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 242 | src_line = xmalloc_fgets(src_stream); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 243 | if (src_line) { |
| 244 | int diff = strcmp(src_line, patch_line + 1); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 245 | src_cur_line++; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 246 | free(src_line); |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 247 | if (diff) |
| 248 | src_line = NULL; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 249 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 250 | } |
Denys Vlasenko | d7874ed | 2009-08-20 23:33:19 +0200 | [diff] [blame] | 251 | /* Do not patch an already patched hunk with -N */ |
| 252 | if (src_line == 0 && (opt & OPT_N)) { |
| 253 | continue; |
| 254 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 255 | if (!src_line) { |
| 256 | bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start); |
| 257 | bad_hunk_count++; |
| 258 | break; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 259 | } |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 260 | if (*patch_line != ' ') { /* '-' */ |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 261 | continue; |
| 262 | } |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 263 | } |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 264 | if (dst_cur_line == dst_last_line) |
| 265 | break; |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 266 | fputs(patch_line + 1, dst_stream); |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 267 | dst_cur_line++; |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 268 | } /* end of while loop handling one hunk */ |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 269 | } /* end of while loop handling one file */ |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 270 | |
| 271 | /* Cleanup last patched file */ |
| 272 | if (copy_trailing_lines_flag) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 273 | copy_lines(src_stream, dst_stream, (unsigned)(-1)); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 274 | } |
| 275 | if (src_stream) { |
| 276 | fclose(src_stream); |
| 277 | } |
Denis Vlasenko | 64a76d7 | 2008-03-24 18:18:03 +0000 | [diff] [blame] | 278 | fclose(dst_stream); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 279 | if (bad_hunk_count) { |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 280 | ret = 1; |
| 281 | 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] | 282 | } else { |
| 283 | /* It worked, we can remove the backup */ |
| 284 | if (backup_filename) { |
| 285 | unlink(backup_filename); |
| 286 | } |
Denys Vlasenko | 17c838b | 2009-10-24 17:11:55 +0200 | [diff] [blame] | 287 | if (!(opt & OPT_dry_run) |
| 288 | && ((dst_cur_line == 0) || (dst_beg_line == 0)) |
| 289 | ) { |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 290 | /* The new patched file is empty, remove it */ |
Denis Vlasenko | 1bb552b | 2007-04-05 21:25:15 +0000 | [diff] [blame] | 291 | xunlink(new_filename); |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 292 | // /* old_filename and new_filename may be the same file */ |
| 293 | // unlink(old_filename); |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 294 | } |
| 295 | } |
Denis Vlasenko | cc2965f | 2008-03-25 14:15:39 +0000 | [diff] [blame] | 296 | free(backup_filename); |
| 297 | //free(old_filename); |
| 298 | free(new_filename); |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 299 | } /* end of "while there are patch lines" */ |
Denis Vlasenko | 08ec67b | 2008-03-26 13:32:30 +0000 | [diff] [blame] | 300 | quit: |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 301 | /* 0 = SUCCESS |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 302 | * 1 = Some hunks failed |
Denis Vlasenko | c93b162 | 2008-03-23 22:55:25 +0000 | [diff] [blame] | 303 | * 2 = More serious problems (exited earlier) |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 304 | */ |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 305 | return ret; |
Glenn L McGrath | 655d814 | 2003-06-22 15:32:41 +0000 | [diff] [blame] | 306 | } |