wok-6.x rev 287
Busybox: patch fix
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Thu Feb 28 12:30:56 2008 +0000 (2008-02-28) |
parents | 83a60b321c15 |
children | a432ed283969 |
files | busybox/receipt busybox/stuff/busybox-1.7.3-patch.u busybox/stuff/busybox-1.7.3-script.u |
line diff
1.1 --- a/busybox/receipt Wed Feb 27 16:38:56 2008 +0100 1.2 +++ b/busybox/receipt Thu Feb 28 12:30:56 2008 +0000 1.3 @@ -16,6 +16,7 @@ 1.4 { 1.5 patch -p0 < stuff/$PACKAGE-$VERSION-hexdump.u 1.6 patch -p0 < stuff/$PACKAGE-$VERSION-df.u 1.7 + patch -p0 < stuff/$PACKAGE-$VERSION-patch.u 1.8 patch -p0 < stuff/$PACKAGE-$VERSION-script.u 1.9 cp stuff/$PACKAGE-$VERSION.config $PACKAGE-$VERSION/.config 1.10 cd $PACKAGE-$VERSION
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/busybox/stuff/busybox-1.7.3-patch.u Thu Feb 28 12:30:56 2008 +0000 2.3 @@ -0,0 +1,258 @@ 2.4 +--- busybox-1.7.3/editors/patch.c 2.5 ++++ busybox-1.7.3/editors/patch.c 2.6 +@@ -23,11 +23,9 @@ 2.7 + 2.8 + #include "libbb.h" 2.9 + 2.10 +-static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count) 2.11 ++static unsigned int copy_lines(FILE *src_stream, FILE *dest_stream, unsigned int lines_count) 2.12 + { 2.13 +- unsigned int i = 0; 2.14 +- 2.15 +- while (src_stream && (i < lines_count)) { 2.16 ++ while (src_stream && lines_count) { 2.17 + char *line; 2.18 + line = xmalloc_fgets(src_stream); 2.19 + if (line == NULL) { 2.20 +@@ -37,10 +35,9 @@ 2.21 + bb_perror_msg_and_die("error writing to new file"); 2.22 + } 2.23 + free(line); 2.24 +- 2.25 +- i++; 2.26 ++ lines_count--; 2.27 + } 2.28 +- return i; 2.29 ++ return lines_count; 2.30 + } 2.31 + 2.32 + /* If patch_level is -1 it will remove all directory names 2.33 +@@ -49,26 +46,24 @@ 2.34 + * returns malloc'ed filename 2.35 + */ 2.36 + 2.37 +-static char *extract_filename(char *line, int patch_level) 2.38 ++static char *extract_filename(char *line, unsigned int patch_level) 2.39 + { 2.40 + char *temp, *filename_start_ptr = line + 4; 2.41 +- int i; 2.42 + 2.43 + /* Terminate string at end of source filename */ 2.44 +- temp = strchrnul(filename_start_ptr, '\t'); 2.45 +- *temp = '\0'; 2.46 ++ line[strcspn(line,"\t\n")] = '\0'; 2.47 + 2.48 + /* Skip over (patch_level) number of leading directories */ 2.49 +- if (patch_level == -1) 2.50 +- patch_level = INT_MAX; 2.51 +- for (i = 0; i < patch_level; i++) { 2.52 ++ while (patch_level--) { 2.53 + temp = strchr(filename_start_ptr, '/'); 2.54 + if (!temp) 2.55 + break; 2.56 + filename_start_ptr = temp + 1; 2.57 + } 2.58 + 2.59 +- return xstrdup(filename_start_ptr); 2.60 ++ temp = xstrdup(filename_start_ptr); 2.61 ++ free(line); 2.62 ++ return temp; 2.63 + } 2.64 + 2.65 + static int file_doesnt_exist(const char *filename) 2.66 +@@ -82,22 +77,19 @@ 2.67 + { 2.68 + int patch_level = -1; 2.69 + char *patch_line; 2.70 +- int ret; 2.71 +- FILE *patch_file = NULL; 2.72 ++ FILE *patch_file = stdin; 2.73 + 2.74 + { 2.75 + char *p, *i; 2.76 +- ret = getopt32(argv, "p:i:", &p, &i); 2.77 ++ int ret = getopt32(argv, "p:i:", &p, &i); 2.78 + if (ret & 1) 2.79 + patch_level = xatol_range(p, -1, USHRT_MAX); 2.80 + if (ret & 2) { 2.81 + patch_file = xfopen(i, "r"); 2.82 +- } else { 2.83 +- patch_file = stdin; 2.84 + } 2.85 +- ret = 0; 2.86 + } 2.87 + 2.88 ++ xfunc_error_retval = 2; 2.89 + patch_line = xmalloc_getline(patch_file); 2.90 + while (patch_line) { 2.91 + FILE *src_stream; 2.92 +@@ -122,18 +114,14 @@ 2.93 + /* FIXME: patch_line NULL check?? */ 2.94 + 2.95 + /* Extract the filename used before the patch was generated */ 2.96 +- original_filename = extract_filename(patch_line, patch_level); 2.97 +- free(patch_line); 2.98 ++ original_filename = extract_filename(patch_line, (unsigned int) patch_level); 2.99 + 2.100 + patch_line = xmalloc_getline(patch_file); 2.101 + /* FIXME: NULL check?? */ 2.102 + if (strncmp(patch_line, "+++ ", 4) != 0) { 2.103 +- ret = 2; 2.104 +- bb_error_msg("invalid patch"); 2.105 +- continue; 2.106 ++ bb_error_msg_and_die("invalid patch: %s\n",patch_line); 2.107 + } 2.108 +- new_filename = extract_filename(patch_line, patch_level); 2.109 +- free(patch_line); 2.110 ++ new_filename = extract_filename(patch_line, (unsigned int) patch_level); 2.111 + 2.112 + if (file_doesnt_exist(new_filename)) { 2.113 + char *line_ptr; 2.114 +@@ -144,7 +132,6 @@ 2.115 + bb_make_directory(new_filename, -1, FILEUTILS_RECUR); 2.116 + *line_ptr = '/'; 2.117 + } 2.118 +- dst_stream = xfopen(new_filename, "w+"); 2.119 + backup_filename = NULL; 2.120 + } else { 2.121 + backup_filename = xmalloc(strlen(new_filename) + 6); 2.122 +@@ -154,12 +141,11 @@ 2.123 + bb_perror_msg_and_die("cannot create file %s", 2.124 + backup_filename); 2.125 + } 2.126 +- dst_stream = xfopen(new_filename, "w"); 2.127 + } 2.128 + 2.129 +- if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) { 2.130 +- src_stream = NULL; 2.131 +- } else { 2.132 ++ dst_stream = xfopen(new_filename, "w"); 2.133 ++ src_stream = NULL; 2.134 ++ if (!file_doesnt_exist(original_filename)) { 2.135 + if (strcmp(original_filename, new_filename) == 0) { 2.136 + src_stream = xfopen(backup_filename, "r"); 2.137 + } else { 2.138 +@@ -174,54 +160,59 @@ 2.139 + while (patch_line) { 2.140 + unsigned int count; 2.141 + unsigned int src_beg_line; 2.142 ++ unsigned int src_last_line = 1; 2.143 + unsigned int unused; 2.144 + unsigned int hunk_offset_start = 0; 2.145 +- int hunk_error = 0; 2.146 + 2.147 + /* This bit should be improved */ 2.148 +- if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) && 2.149 +- (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) && 2.150 +- (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) { 2.151 ++ if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, 2.152 ++ &src_last_line, &dest_beg_line, &unused) != 4) && 2.153 ++ (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, 2.154 ++ &src_last_line, &dest_beg_line) != 3) && 2.155 ++ (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, 2.156 ++ &dest_beg_line, &unused) != 3) && 2.157 ++ (sscanf(patch_line, "@@ -%d +%d @@", &src_beg_line, 2.158 ++ &dest_beg_line) != 2)) { 2.159 + /* No more hunks for this file */ 2.160 + break; 2.161 + } 2.162 +- free(patch_line); 2.163 + hunk_count++; 2.164 + 2.165 + if (src_beg_line && dest_beg_line) { 2.166 + /* Copy unmodified lines upto start of hunk */ 2.167 + /* src_beg_line will be 0 if its a new file */ 2.168 + count = src_beg_line - src_cur_line; 2.169 +- if (copy_lines(src_stream, dst_stream, count) != count) { 2.170 ++ if (copy_lines(src_stream, dst_stream, count)) { 2.171 + bb_error_msg_and_die("bad src file"); 2.172 + } 2.173 + src_cur_line += count; 2.174 + dest_cur_line += count; 2.175 + copy_trailing_lines_flag = 1; 2.176 + } 2.177 +- hunk_offset_start = src_cur_line; 2.178 ++ src_last_line += hunk_offset_start = src_cur_line; 2.179 + 2.180 +- while ((patch_line = xmalloc_fgets(patch_file)) != NULL) { 2.181 ++ for (free(patch_line); 2.182 ++ (patch_line = xmalloc_fgets(patch_file)) != NULL; 2.183 ++ free(patch_line)) { 2.184 + if ((*patch_line == '-') || (*patch_line == ' ')) { 2.185 + char *src_line = NULL; 2.186 ++ if (src_cur_line == src_last_line) break; 2.187 + if (src_stream) { 2.188 + src_line = xmalloc_fgets(src_stream); 2.189 +- if (!src_line) { 2.190 +- hunk_error++; 2.191 +- break; 2.192 +- } else { 2.193 ++ if (src_line) { 2.194 ++ int diff = strcmp(src_line, patch_line + 1); 2.195 + src_cur_line++; 2.196 ++ free(src_line); 2.197 ++ if (diff) { 2.198 ++ src_line = NULL; 2.199 ++ } 2.200 + } 2.201 +- if (strcmp(src_line, patch_line + 1) != 0) { 2.202 +- bb_error_msg("hunk #%d FAILED at %d", hunk_count, hunk_offset_start); 2.203 +- hunk_error++; 2.204 +- free(patch_line); 2.205 +- /* Probably need to find next hunk, etc... */ 2.206 +- /* but for now we just bail out */ 2.207 +- patch_line = NULL; 2.208 ++ if (!src_line) { 2.209 ++ bb_error_msg("hunk #%d FAILED at %d", 2.210 ++ hunk_count, hunk_offset_start); 2.211 ++ bad_hunk_count++; 2.212 + break; 2.213 + } 2.214 +- free(src_line); 2.215 + } 2.216 + if (*patch_line == ' ') { 2.217 + fputs(patch_line + 1, dst_stream); 2.218 +@@ -233,16 +224,12 @@ 2.219 + } else { 2.220 + break; 2.221 + } 2.222 +- free(patch_line); 2.223 +- } 2.224 +- if (hunk_error) { 2.225 +- bad_hunk_count++; 2.226 + } 2.227 + } 2.228 + 2.229 + /* Cleanup last patched file */ 2.230 + if (copy_trailing_lines_flag) { 2.231 +- copy_lines(src_stream, dst_stream, -1); 2.232 ++ copy_lines(src_stream, dst_stream, (unsigned int) -1); 2.233 + } 2.234 + if (src_stream) { 2.235 + fclose(src_stream); 2.236 +@@ -251,16 +238,14 @@ 2.237 + fclose(dst_stream); 2.238 + } 2.239 + if (bad_hunk_count) { 2.240 +- if (!ret) { 2.241 +- ret = 1; 2.242 +- } 2.243 + bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count); 2.244 ++ return 1; 2.245 + } else { 2.246 + /* It worked, we can remove the backup */ 2.247 + if (backup_filename) { 2.248 + unlink(backup_filename); 2.249 + } 2.250 +- if ((dest_cur_line == 0) || (dest_beg_line == 0)) { 2.251 ++ if (dest_cur_line == 0) { 2.252 + /* The new patched file is empty, remove it */ 2.253 + xunlink(new_filename); 2.254 + if (strcmp(new_filename, original_filename) != 0) 2.255 +@@ -273,5 +258,5 @@ 2.256 + * 1 = Some hunks failed 2.257 + * 2 = More serious problems 2.258 + */ 2.259 +- return ret; 2.260 ++ return 0; 2.261 + }