OLD | NEW |
(Empty) | |
| 1 From f8c00fab208cb07e46bc3cb7f651cead857adf07 Mon Sep 17 00:00:00 2001 |
| 2 From: Andrew de los Reyes <adlr@chromium.org> |
| 3 Date: Mon, 19 Apr 2010 15:05:56 -0700 |
| 4 Subject: [PATCH] bspatch: support input/output positioning |
| 5 |
| 6 For autoupdate, we need bspatch to read specific blocks from the |
| 7 filesystem devic and write back to specific blocks of the device. |
| 8 |
| 9 Review URL: http://codereview.chromium.org/1595025 |
| 10 --- |
| 11 files/bspatch.c | 270 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- |
| 12 1 files changed, 259 insertions(+), 11 deletions(-) |
| 13 |
| 14 diff --git a/files/bspatch.c b/files/bspatch.c |
| 15 index f4b821c..48ac79a 100644 |
| 16 --- a/files/bspatch.c |
| 17 +++ b/files/bspatch.c |
| 18 @@ -3,7 +3,7 @@ |
| 19 * All rights reserved |
| 20 * |
| 21 * Redistribution and use in source and binary forms, with or without |
| 22 - * modification, are permitted providing that the following conditions |
| 23 + * modification, are permitted providing that the following conditions |
| 24 * are met: |
| 25 * 1. Redistributions of source code must retain the above copyright |
| 26 * notice, this list of conditions and the following disclaimer. |
| 27 @@ -29,6 +29,9 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1
2005/08/06 01:59: |
| 28 #endif |
| 29 |
| 30 #include <bzlib.h> |
| 31 +#include <errno.h> |
| 32 +#include <inttypes.h> |
| 33 +#include <stdint.h> |
| 34 #include <stdlib.h> |
| 35 #include <stdio.h> |
| 36 #include <string.h> |
| 37 @@ -36,6 +39,235 @@ __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1
.1 2005/08/06 01:59: |
| 38 #include <unistd.h> |
| 39 #include <fcntl.h> |
| 40 |
| 41 +#define JOIN(a, b) __JOIN(a, b) |
| 42 +#define __JOIN(a, b) a ## b |
| 43 +#define COMPILE_ASSERT(expr, message) \ |
| 44 + typedef char JOIN(message, JOIN(_, __LINE__)) [(expr) ? 1 : -1] |
| 45 + |
| 46 +COMPILE_ASSERT(sizeof(int64_t) == 8, int64_t_64_bit); |
| 47 + |
| 48 +#define MIN(a, b) \ |
| 49 + ((a) < (b) ? (a) : (b)) |
| 50 + |
| 51 +// Reads next int from *ints. The int should be terminated with a comma |
| 52 +// or NULL char. *ints will be updated to the space right after the comma |
| 53 +// or set to NULL if this was the last number. This assumes the input is |
| 54 +// a valid string, as validated with PositionsStringIsValid(). |
| 55 +// Returns 1 on success. |
| 56 +int NextInt64(const char** ints, int64_t *out) { |
| 57 + if (!ints[0]) |
| 58 + return 0; |
| 59 + int r = sscanf(*ints, "%" PRIi64, out); |
| 60 + if (r == 1) { |
| 61 + const char* next_comma = strchr(*ints, ','); |
| 62 + const char* next_colon = strchr(*ints, ':'); |
| 63 + if (!next_comma && !next_colon) |
| 64 + *ints = NULL; |
| 65 + else if (!next_comma) |
| 66 + *ints = next_colon + 1; |
| 67 + else if (!next_colon) |
| 68 + *ints = next_comma + 1; |
| 69 + else |
| 70 + *ints = MIN(next_comma, next_colon) + 1; |
| 71 + return 1; |
| 72 + } |
| 73 + return 0; |
| 74 +} |
| 75 + |
| 76 +COMPILE_ASSERT(sizeof(intmax_t) == 8, intmax_t_not_64_bit); |
| 77 + |
| 78 +// Returns 1 if str can be converted to int64_t without over/underflowing. |
| 79 +// str is assumed to point to an optional negative sign followed by numbers, |
| 80 +// optionally followed by non-numeric characters, followed by '\0'. |
| 81 +int IsValidInt64(const char* str) { |
| 82 + const char* end_ptr; |
| 83 + errno = 0; |
| 84 + intmax_t result = strtoimax(str, &end_ptr, /* base: */ 10); |
| 85 + return errno == 0; |
| 86 +} |
| 87 + |
| 88 +// Input validator. Make sure the positions string is well formatted. |
| 89 +// All numerical values are checked to make sure they don't over/underflow |
| 90 +// int64_t. Returns 1 if valid. |
| 91 +int PositionsStringIsValid(const char* positions) { |
| 92 + if (positions == NULL) |
| 93 + errx(1, "bad string"); |
| 94 + |
| 95 + // Special case: empty string is valid |
| 96 + if (!positions[0]) |
| 97 + return 1; |
| 98 + |
| 99 + // Use a state machine to determine if the string is valid. |
| 100 + // Key: (s): state, ((s)) valid end state. |
| 101 + // n (negative_valid) is a boolean that starts out as true. |
| 102 + // If n is true, ':' is the delimiter, otherwise ','. |
| 103 + // |
| 104 + // .--------------------------. |
| 105 + // | | n ? ':' : ',' ; n = !n |
| 106 + // V '-'&&n 0-9 | |
| 107 + // start->(0)------------->(1)----->((2))---. |
| 108 + // `---------------------> <--' 0-9 |
| 109 + // 0-9 |
| 110 + int state = 0; |
| 111 + int negative_valid = 1; |
| 112 + const char* number_start = positions; |
| 113 + for (;; positions++) { |
| 114 + char c = *positions; |
| 115 + switch (state) { |
| 116 + case 0: |
| 117 + if (c == '-' && negative_valid) { |
| 118 + state = 1; |
| 119 + continue; |
| 120 + } |
| 121 + if (isdigit(c)) { |
| 122 + state = 2; |
| 123 + continue; |
| 124 + } |
| 125 + return 0; |
| 126 + case 1: |
| 127 + if (isdigit(c)) { |
| 128 + state = 2; |
| 129 + continue; |
| 130 + } |
| 131 + return 0; |
| 132 + case 2: |
| 133 + if (isdigit(c)) |
| 134 + continue; |
| 135 + // number_start must point to a valid number |
| 136 + if (!IsValidInt64(number_start)) { |
| 137 + return 0; |
| 138 + } |
| 139 + if ((negative_valid && c == ':') || |
| 140 + (!negative_valid && c == ',')) { |
| 141 + state = 0; |
| 142 + number_start = positions + 1; |
| 143 + negative_valid = !negative_valid; |
| 144 + continue; |
| 145 + } |
| 146 + return (c == '\0'); |
| 147 + } |
| 148 + } |
| 149 +} |
| 150 + |
| 151 +// Reads into a buffer a series of byte ranges from filename. |
| 152 +// Each range is a pair of comma-separated ints from positions. |
| 153 +// -1 as an offset means a sparse-hole. |
| 154 +// E.g. If positions were "1,5:23,4:-1,8:3,7", then we would return a buffer |
| 155 +// consisting of 5 bytes from offset 1 of the file, followed by |
| 156 +// 4 bytes from offset 23, then 8 bytes of all zeros, then 7 bytes from |
| 157 +// offset 3 in the file. |
| 158 +// Returns NULL on error. |
| 159 +static char* PositionedRead(const char* filename, |
| 160 + const char* positions, |
| 161 + ssize_t* old_size) { |
| 162 + if (!PositionsStringIsValid(positions)) { |
| 163 + errx(1, "invalid positions string for read\n"); |
| 164 + } |
| 165 + |
| 166 + // Get length |
| 167 + const char* p = positions; |
| 168 + int64_t length = 0; |
| 169 + for (;;) { |
| 170 + int64_t value; |
| 171 + if (0 == NextInt64(&p, &value)) { |
| 172 + break; |
| 173 + } |
| 174 + int r = NextInt64(&p, &value); |
| 175 + if (r == 0) { |
| 176 + errx(1, "bad length parse\n"); |
| 177 + } |
| 178 + if (value < 0) { |
| 179 + errx(1, "length can't be negative\n"); |
| 180 + } |
| 181 + length += value; |
| 182 + } |
| 183 + |
| 184 + // Malloc |
| 185 + if (length > 0x40000000) { // 1 GiB; sanity check |
| 186 + errx(1, "Read length too long (exceeds 1 GiB)"); |
| 187 + } |
| 188 + // Following bsdiff convention, allocate length + 1 to avoid malloc(0) |
| 189 + char* buf = malloc(length + 1); |
| 190 + if (buf == NULL) { |
| 191 + errx(1, "malloc failed\n"); |
| 192 + } |
| 193 + char* buf_tail = buf; |
| 194 + |
| 195 + int fd = open(filename, O_RDONLY); |
| 196 + if (fd < 0) { |
| 197 + errx(1, "open failed for read\n"); |
| 198 + } |
| 199 + |
| 200 + // Read bytes |
| 201 + p = positions; |
| 202 + for (;;) { |
| 203 + int64_t offset, read_length; |
| 204 + if (NextInt64(&p, &offset) == 0) { |
| 205 + break; |
| 206 + } |
| 207 + if (offset < 0) { |
| 208 + errx(1, "no support for sparse positions " |
| 209 + "yet during read\n"); |
| 210 + } |
| 211 + if (NextInt64(&p, &read_length) == 0) { |
| 212 + errx(1, "bad length parse (should never happen)\n"); |
| 213 + } |
| 214 + if (read_length < 0) { |
| 215 + errx(1, "length can't be negative " |
| 216 + "(should never happen)\n"); |
| 217 + } |
| 218 + ssize_t rc = pread(fd, buf_tail, read_length, offset); |
| 219 + if (rc != read_length) { |
| 220 + errx(1, "read failed\n"); |
| 221 + } |
| 222 + buf_tail += rc; |
| 223 + } |
| 224 + close(fd); |
| 225 + *old_size = length; |
| 226 + return buf; |
| 227 +} |
| 228 + |
| 229 +static void PositionedWrite(const char* filename, |
| 230 + const char* positions, |
| 231 + const char* buf, |
| 232 + ssize_t new_size) { |
| 233 + if (!PositionsStringIsValid(positions)) { |
| 234 + errx(1, "invalid positions string for write\n"); |
| 235 + } |
| 236 + int fd = open(filename, O_WRONLY | O_CREAT, 0666); |
| 237 + if (fd < 0) { |
| 238 + errx(1, "open failed for write\n"); |
| 239 + } |
| 240 + |
| 241 + for (;;) { |
| 242 + int64_t offset, length; |
| 243 + if (NextInt64(&positions, &offset) == 0) { |
| 244 + break; |
| 245 + } |
| 246 + if (NextInt64(&positions, &length) == 0) { |
| 247 + errx(1, "bad length parse for write\n"); |
| 248 + } |
| 249 + if (length < 0) { |
| 250 + errx(1, "length can't be negative for write\n"); |
| 251 + } |
| 252 + |
| 253 + if (offset < 0) { |
| 254 + // Sparse hole. Skip. |
| 255 + } else { |
| 256 + ssize_t rc = pwrite(fd, buf, length, offset); |
| 257 + if (rc != length) { |
| 258 + errx(1, "write failed\n"); |
| 259 + } |
| 260 + } |
| 261 + buf += length; |
| 262 + new_size -= length; |
| 263 + } |
| 264 + if (new_size != 0) { |
| 265 + errx(1, "output position length doesn't match new size\n"); |
| 266 + } |
| 267 + close(fd); |
| 268 +} |
| 269 + |
| 270 static off_t offtin(u_char *buf) |
| 271 { |
| 272 off_t y; |
| 273 @@ -69,7 +301,13 @@ int main(int argc,char * argv[]) |
| 274 off_t lenread; |
| 275 off_t i; |
| 276 |
| 277 - if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]); |
| 278 + if ((argc != 6) && (argc != 4)) { |
| 279 + errx(1,"usage: %s oldfile newfile patchfile \\\n" |
| 280 + " [in_offset,in_length,in_offset,in_length,... \\\n" |
| 281 + " out_offset,out_length," |
| 282 + "out_offset,out_length,...]\n",argv[0]); |
| 283 + } |
| 284 + int using_positioning = (argc == 6); |
| 285 |
| 286 /* Open patch file */ |
| 287 if ((f = fopen(argv[3], "r")) == NULL) |
| 288 @@ -132,12 +370,18 @@ int main(int argc,char * argv[]) |
| 289 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) |
| 290 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); |
| 291 |
| 292 - if(((fd=open(argv[1],O_RDONLY,0))<0) || |
| 293 - ((oldsize=lseek(fd,0,SEEK_END))==-1) || |
| 294 - ((old=malloc(oldsize+1))==NULL) || |
| 295 - (lseek(fd,0,SEEK_SET)!=0) || |
| 296 - (read(fd,old,oldsize)!=oldsize) || |
| 297 - (close(fd)==-1)) err(1,"%s",argv[1]); |
| 298 + // Read |
| 299 + |
| 300 + if (!using_positioning) { |
| 301 + if(((fd=open(argv[1],O_RDONLY,0))<0) || |
| 302 + ((oldsize=lseek(fd,0,SEEK_END))==-1) || |
| 303 + ((old=malloc(oldsize+1))==NULL) || |
| 304 + (lseek(fd,0,SEEK_SET)!=0) || |
| 305 + (read(fd,old,oldsize)!=oldsize) || |
| 306 + (close(fd)==-1)) err(1,"%s",argv[1]); |
| 307 + } else { |
| 308 + old = PositionedRead(argv[1], argv[4], &oldsize); |
| 309 + } |
| 310 if((new=malloc(newsize+1))==NULL) err(1,NULL); |
| 311 |
| 312 oldpos=0;newpos=0; |
| 313 @@ -193,9 +437,13 @@ int main(int argc,char * argv[]) |
| 314 err(1, "fclose(%s)", argv[3]); |
| 315 |
| 316 /* Write the new file */ |
| 317 - if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || |
| 318 - (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) |
| 319 - err(1,"%s",argv[2]); |
| 320 + if (!using_positioning) { |
| 321 + if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) || |
| 322 + (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) |
| 323 + err(1,"%s",argv[2]); |
| 324 + } else { |
| 325 + PositionedWrite(argv[2], argv[5], new, newsize); |
| 326 + } |
| 327 |
| 328 free(new); |
| 329 free(old); |
| 330 -- |
| 331 1.6.4.4 |
| 332 |
OLD | NEW |